来源:小白月赛13:小A的彩票

题目描述:

小A最近开始沉迷买彩票,并且希望能够通过买彩票发家致富。已知购买一张彩票需要3元,而彩票中奖的金额分别为1,2,3,4元,并且比较独特的是这个彩票中奖的各种金额都是等可能的。现在小A连续购买了n张彩票,他希望你能够告诉他至少能够不亏本的概率是多少。

输入描述:

一行一个整数N,为小A购买的彩票数量。

输出描述:

输出一个最简分数a/b,表示小A不亏本的概率。若概率为1,则输出1/1,概率为0,则输出0/1。

用dpi表示买第i张彩票,中j元的方案数。那么
dpi=dpi-1+dpi-1+dpi-1+dpi-1
边界条件:
dp1=1;
dp1=1;
dp1=1;
dp1=1;
很明显,如果不买,保证不亏。
买了,肯定有可能亏。
因为只要买了,就有可能全中1元,亏了,也有可能全中4元,稳赚。
所以,只有n==0时,概率为1/1,概率为0/1的情况不存在。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 31;
#define ll long long int
ll dp[maxn][maxn*4];
ll power(ll a,ll b ) 
{
    ll r = 1;
    ll base = a;
    while(b != 0 ) 
    {
        if(b & 1)
        {
            r *= base;
        }
        base *= base;
        b/=2;
    }
    return r;
}
ll gcd(ll a,ll b)
{
    if(b==0)
    {
        return a;
    }
    return gcd(b,a%b);
}
int main()
{
    int n; 
    while(cin>>n)
    {
        memset(dp,0,sizeof(dp));
        dp[1][1]=1;
        dp[1][2]=1;
        dp[1][3]=1;
        dp[1][4]=1;
        for(int i=2;i<=n;++i)
        {
            for(int j=1;j<=n*4;++j)
            {
                for(int k=1;k<=4;++k)
                {
                    if(j-k<=0) continue;
                    dp[i][j]+=dp[i-1][j-k];
                }
            }
        }
//        print dp        
//        for(int i=1;i<=n;++i)
//        {
//            for(int j=1;j<=n*4;++j)
//            {
//                cout<<dp[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        
        ll ans = 0;
        for(int i=1;i<=n*4;++i)
        {
            if(i>=n*3)
            {
                ans+=dp[n][i];
            }
        }
        ll a=ans;
        ll b=power(4,n);
        ll c=gcd(a,b);
        if(n==0)
        {
            cout<<"1/1"<<endl;
        }else{
            cout<<a/c<<"/"<<b/c<<endl;            
        }
    }
    return 0;
}
Last modification:September 19th, 2019 at 12:12 am
如果觉得我的文章对你有用,请随意赞赏