您的位置:首页 > 其它

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代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: