2013资格赛——Binomial Showdown
2013-08-24 16:12
288 查看
Description
This is a problem yesterday.
ACM summer exercise is too popular...The seats is not enough, now, during the n ACMers in 573, k of them will carry the stools, how many possible situation here?
hh wants you find this.
Input
Each test case contains two integers n (n >= 1) and k (0 <= k <= n).
Input is ended by two zeroes for n and k.
Output
For each test case, print one line containing the required number.
You can suppose the answer is below 2^31-1;
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
分析:题目很简单,就是计算C(n,k),很容易想到的是开一个数组,直接用 杨辉三角 ……
其状态方程为 dp[i][j]=dp[i-1][j]+dp[i-1][j-1];但是题目中提到结果是小于2^32-1的,这就要考虑几种情况了,比如当n=2^32-1,k=1的时候,你开的数组不可能这么大的,一般开个1000*1000的数组,所以单独考虑k<4的情况(注意,如果n-k<k此时需要处理一下k,即k=n-k)。所以代码就很简单了,当然也可以直接计算,边乘边除进行计算C(n,k);但是如果进行多次查询的时候,可能比较费时,不如一次性直接把结果存进数组中。
AC代码:
This is a problem yesterday.
ACM summer exercise is too popular...The seats is not enough, now, during the n ACMers in 573, k of them will carry the stools, how many possible situation here?
hh wants you find this.
Input
Each test case contains two integers n (n >= 1) and k (0 <= k <= n).
Input is ended by two zeroes for n and k.
Output
For each test case, print one line containing the required number.
You can suppose the answer is below 2^31-1;
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
分析:题目很简单,就是计算C(n,k),很容易想到的是开一个数组,直接用 杨辉三角 ……
其状态方程为 dp[i][j]=dp[i-1][j]+dp[i-1][j-1];但是题目中提到结果是小于2^32-1的,这就要考虑几种情况了,比如当n=2^32-1,k=1的时候,你开的数组不可能这么大的,一般开个1000*1000的数组,所以单独考虑k<4的情况(注意,如果n-k<k此时需要处理一下k,即k=n-k)。所以代码就很简单了,当然也可以直接计算,边乘边除进行计算C(n,k);但是如果进行多次查询的时候,可能比较费时,不如一次性直接把结果存进数组中。
AC代码:
#include<stdio.h> long long dp[1000][1000]; int main() { int n,k,ans,i,j; long long sum; for(i=0;i<1000;i++)dp[i][0]=1; for(i=1;i<1000;i++) { for(j=1;j<=i;j++) { dp[i][j]=dp[i-1][j]+dp[i-1][j-1]; } } while(~scanf("%d%d",&n,&k)&&(n||k)) { if(n-k<k)k=n-k; if(k<4) { if(k==0)printf("1\n"); else if(k==1)printf("%d\n",n); else if (k==2)printf("%d\n",1LL*n*(n-1)/2); else printf("%d\n",1LL*n*(n-1)*(n-2)/6); } else printf("%d\n",dp [k]); } return 0; }
相关文章推荐
- poj2249 Binomial Showdown(二项式系数)
- 2013资格赛——电话记录
- facebook hacker cup 2013资格赛第二题
- 2013 微软编程之美全国挑战赛之资格赛 传话游戏
- 宋体数据2013编程之美全国挑战赛资格赛之传话游戏
- poj 2249 Binomial Showdown
- 2013资格赛——Who Is In Front of Me
- UVa 530 - Binomial Showdown
- 2013编程之美资格赛【传话游戏】
- poj2249 Binomial Showdown 求组合数
- [ACM] poj 2249 Binomial Showdown (排列组合公式优化)
- 2013资格赛——桃花群岛
- Binomial Showdown(P2249)
- 从博弈论的角度理解2013微软编程之美资格赛题目——竞价
- (组合数学3.1.2.1)POJ 2249 Binomial Showdown(排列组合公式的实现)
- 2013编程之美资格赛【长方形】
- 编程之美 2013资格赛 传话游戏
- [ACM] poj 2249 Binomial Showdown (排列组合公式优化)
- 2013资格赛——取石子游戏
- zoj 1938 Binomial Showdown 组合数裸基础