题目连接:http://120.78.128.11/Problem.jsp?pid=3266
作为签到题,还是很多人过了的,这个题的做法有两种。
1、动态规划,很简单的,看代码吧:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #define N 11 3 double f[N][N]; 4 int main() 5 { 6 int n; 7 double a,b; 8 scanf("%d",&n); 9 scanf("%lf%lf",&a,&b); 10 f[0][0]=1; 11 for (int i=1;i<=n;++i) 12 { 13 f[0][i]=f[0][i-1]*a; 14 f[i][0]=f[i-1][0]*b; 15 } 16 for (int i=1;i<=n;++i) 17 { 18 for (int j=1;j<=n;++j) 19 { 20 f[i][j]=f[i][j-1]*a+f[i-1][j]*b; 21 } 22 } 23 /* 24 for (int i=0;i<=n;++i) 25 { 26 for (int j=0;j<=n;++j) 27 { 28 printf("%.2f ",f[i][j]); 29 } 30 puts(""); 31 }*/ 32 int q; 33 scanf("%d",&q); 34 int m,x,y; 35 while (q--) 36 { 37 scanf("%d%d%d",&m,&x,&y); 38 if (x+y==m) 39 { 40 printf("%.2f\n",f[y][x]); 41 } 42 else 43 { 44 printf("0.00\n"); 45 } 46 47 } 48 return 0; 49 }
2、数学方法,走m步,有x步向上,所以答案是C(n,x)*a^x*b^y
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<math.h> 3 typedef long long ll; 4 int C(int n,int m) 5 { 6 if(m>n)return 0; 7 ll a=1,b=1; 8 for(int i=1;i<=m;i++) 9 { 10 a*=(n+i-m); 11 b*=i; 12 } 13 return a/b; 14 } 15 int main() 16 { 17 int n; 18 scanf("%d",&n); 19 double a,b; 20 scanf("%lf%lf",&a,&b); 21 int q; 22 scanf("%d",&q); 23 while(q--) 24 { 25 int m,x,y; 26 scanf("%d%d%d",&m,&x,&y); 27 if(x+y!=m) 28 { 29 puts("0.00"); 30 continue; 31 } 32 else 33 { 34 printf("%.2f\n",C(m,x)*pow(a,x)*pow(b,y)); 35 } 36 } 37 return 0; 38 }
这题有个坑,当m!=x+y的时候要直接输出0.00