2013资格赛——Binomial Showdown
来源:互联网 发布:淘宝达人后台管理 编辑:程序博客网 时间:2024/06/15 23:44
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[n][k]); } return 0;}
- 2013资格赛——Binomial Showdown
- Binomial Showdown
- Binomial Showdown
- TOJ 1138. Binomial Showdown
- poj 2249 Binomial showdown
- UVa530 - Binomial Showdown
- Binomial Showdown(P2249)
- poj 2249Binomial Showdown
- HOJ 1446 Binomial Showdown
- ZOJ 1938 Binomial Showdown
- Poj 2249 Binomial Showdown
- poj 2249 Binomial Showdown
- UVa 530 - Binomial Showdown
- UVA-530-Binomial Showdown
- POJ2249 Binomial Showdown
- poj2249 Binomial Showdown
- poj 2249 Binomial Showdown
- POJ 2249 Binomial Showdown 笔记
- Deformable Part Model 学习
- 北京一卡车碾过小轿车致4人当场死亡
- 基数排序的链表实现!!
- hdu - 1402 - A * B Problem Plus(FFT)
- 图的深度优先搜索(DFS)、广度优先搜索(BFS)
- 2013资格赛——Binomial Showdown
- 详谈分布式测试系统
- 动态内存分配
- 如何用Javascript识别IE6
- windows7家庭普通版(win7 home basic)安装SQL server 2005 开发版
- 一网打尽当下NoSQL类型、适用场景及使用公司
- ZOJ 3551 Bloodsucker
- 数据库范式——通俗易懂
- 源文件夹已是 NetBeans 项目文件夹 (可能仅在内存中)。