ZOJ.1938 Binomial Showdown【组合数】 2015/09/23
2015-09-23 16:40
218 查看
Binomial Showdown
Time Limit: 2 Seconds
Memory Limit: 65536 KB
In how many ways can you choose k elements out of n elements, not taking order into account?
Write a program to compute this number.
Input
The input will contain one or more test cases.
Each test case consists of one line containing two integers n (n >= 1) and k (0 <= k <= n).
Input is terminated by two zeroes for n and k.
Output
For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2^31.
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
Source: University of Ulm Local Contest 1997
求C(n,k),但是要注意是C(n,k)快还是C(n,n-k)快
Time Limit: 2 Seconds
Memory Limit: 65536 KB
In how many ways can you choose k elements out of n elements, not taking order into account?
Write a program to compute this number.
Input
The input will contain one or more test cases.
Each test case consists of one line containing two integers n (n >= 1) and k (0 <= k <= n).
Input is terminated by two zeroes for n and k.
Output
For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2^31.
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
Source: University of Ulm Local Contest 1997
求C(n,k),但是要注意是C(n,k)快还是C(n,n-k)快
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int min(int a,int b){ return a>b?b:a; } long long cal(int n,int k){ long long ret = 1; k = min(k,n-k); for( int i = 0 ; i < k ; ++i ) ret = (ret * (n-i) / (i+1) ); // for( int i = 2 ; i <= k ; ++i ) // ret /= i; return ret; } int main(){ int n,k; while( ~scanf("%d %d",&n,&k) ){ if( !n&&!k ) break; printf("%lld\n",cal(n,k)); } return 0; }
相关文章推荐
- 题目1019:简单计算器
- Android读写文件操作
- 过拟合与模型选择
- html学习笔记-XML
- 多个Jar包的合并操作
- 【数位DP】Hdu 3652:B-number
- Java EE : 二、图解 Cookie(小甜饼)
- JAVA问题总结之28--读取某目录下所有文件
- OpenWrt下基于Bird的AdHoc无线多跳网络组建与测试
- Mybatis获取插入记录的自增长ID
- linux下清理缓存的方法
- struts2<s:iterator>遍历map小结
- Inveigh is a Windows PowerShell LLMNR/NBNS spoofer with challenge/response capture over HTTP/SMB
- IOS多线程
- linux编译多文件程序
- Ember.js 入门指南——{{link-to}} 助手
- 分布式与集群的区别
- ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理
- ubuntu上安装mysql
- 第二次作业(1.个人项目实践)