小A买彩票(dp)

传送门

题目大意:一张彩票3元,每次买都会中奖,金额为1、2、3、4元,概率相等,求买n次彩票时,至少不亏的概率。

题解:统计所有不亏的方案/所有方案,所有方案可以知道是4的n次方,问题主要在求不亏的方案,不亏即中奖的

金额在3n到4n的方案统计。用dp统计方案,dp[i][j]表示第i次购买,中奖金额为j的方案数量。

时间复杂度o(n^3)

代码如下

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[35][125];
ll gcd(ll a,ll b){
    if(b!=0)return gcd(b,a%b);
    return a;
}
ll mypow(int x,int n){
    ll ans=1,temp=x;
    while(n){
        if(n&1)ans=temp*ans;
        temp=temp*temp;
        n>>=1;
    }
    return ans;
}
int main(){
    int n;
    scanf("%d",&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)break;
                dp[i][j]+=dp[i-1][j-k];///第i次凑成j元的方案数
            }
        }
    }
    ll maxx=mypow(4,n);
    ll sum=0;
    for(int i=3*n;i<=4*n;i++)sum+=dp[n][i];///统计不亏的方案
    ll b=gcd(maxx,sum);
    maxx/=b;sum/=b;
    cout<<sum<<'/'<<maxx<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mohari/p/12969015.html