C--小A买彩票,小白月赛13(dp)

C--小A买彩票

思路:

所有总共的情况为4^n种情况;

将a[i][j]数组设置为满足买i张彩票,得到j的奖金的方案的数量。

a[i][j] = a[i-1][j-1]+a[i-1][j-2]+a[i-1][j-3]+a[i-1][j-4]这样不断更新。

然后每次输入n表示买了n张彩票,然后统计所有大于金额3*n的方案数的数量。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 200;
typedef long long LL;
LL a[maxn][maxn]={0};
LL POW(LL a,LL b){
	LL ret = 1;
	while(b){
		if(b&1) ret = ret*a;
		a = a*a;
		b>>=1;
	}
	return ret;
}
LL Gcd(LL x,LL y){
	return y==0?x:Gcd(y,x%y);
}
int main(void)
{
	int n,i,j;
	a[0][0] = 1;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		for(j=1;j<=i*4;j++){
			if(j-1>=0) a[i][j]+=a[i-1][j-1];
			if(j-2>=0) a[i][j]+=a[i-1][j-2];
			if(j-3>=0) a[i][j]+=a[i-1][j-3];
			if(j-4>=0) a[i][j]+=a[i-1][j-4];
		}
	}
	LL a1 = 0,b1 = POW(4,n);
	for(i=3*n;i<=4*n;i++){
		a1 += a[n][i];
	}
	LL tp = Gcd(a1,b1);
	a1/=tp;b1/=tp;
	printf("%lld/%lld\n",a1,b1);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/89354690
今日推荐