题目大意:
小A沉迷彩票,购买一张彩票需要三元,彩票中奖的金额分别为1,2,3,4且中奖的各种金额几率相等,现在小A购买了n张彩票,问不亏本的概率是多少。
输入描述:
一行一个整数N,为小A购买的彩票数量一行一个整数N,为小A购买的彩票数量。
输出描述:
输出一个最简分数a/b,表示小A不亏本的概率。
若概率为1,则输出1/1,概率为0,则输出0/1。
题目分析:
很容易想到,求出这n张彩票所有的获奖情况,用不亏本的情况除以所有的情况,即是小A不亏本的概率。首先买n张彩票,总的方案数为4^n,然后可以用一个2维的dp数组dp[i][j]表示买i张彩票获奖j元的情况,不亏本的情况则是最终获奖3* n到4* n的情况总数。状态转移方程为dp[i][j]=dp[i-1][j-1]+dp[i-1][j-2]+dp[i-1][j-3]+dp[i-1][j-4]。最后最简分数用gcd化简即可。
#include<iostream> #include<cstdio> #include<vector> #include<cmath> #include<string> #include<cstring> #include<algorithm> #include<queue> #include<map> #include<set> #define endl '\n' #define all(s) s.begin(),s.end() #define lowbit(x) (x&-x) #define rep(i,a,n) for (int i=a;i<=n;i++) #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define mem(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define pb push_back #define PI acos(-1.0) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int mod=1e9+7; const double eps=1e-8; const int inf=0x3f3f3f3f; const int N=2e5+10; ll dp[50][150]; int main() { int n; cin>>n; dp[0][0]=1; for(int i=1;i<=n;i++){ for(int j=i;j<=4*i;j++){ for(int k=1;k<=4;k++){ if(j>=k) dp[i][j]+=dp[i-1][j-k]; } } } ll ans=0; for(int i=3*n;i<=4*n;i++) ans+=dp[n][i]; ll tot=pow(4,n); int k=__gcd(ans,tot); ans/=k; tot/=k; cout<<ans<<'/'<<tot; }