Description

Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M become very large. Challenges are the stuff of contests. Therefore, you are to make just such a computation given the following: 
GIVEN: 5 <= N <= 100; 5 <= M <= 100; M <= N 
Compute the EXACT value of: C = N! / (N-M)!M! 
You may assume that the final value of C will fit in a 32-bit Pascal LongInt or a C long. For the record, the exact value of 100! is: 
93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,381,621, 468,592,963,895,217,599,993,229,915,608,941,463,976,156,518,286,253, 697,920,827,223,758,251,185,210,916,864,000,000,000,000,000,000,000,000 

Input

The input to this program will be one or more lines each containing zero or more leading spaces, a value for N, one or more spaces, and a value for M. The last line of the input file will contain a dummy N, M pair with both values equal to zero. Your program should terminate when this line is read.

Output

The output from this program should be in the form: 
N things taken M at a time is C exactly. 

Sample Input

100  6
20 5
18 6
0 0

Sample Output

100 things taken 6 at a time is 1192052400 exactly.
20 things taken 5 at a time is 15504 exactly.
18 things taken 6 at a time is 18564 exactly.
解题思路:和上题一样,n很小,最大只有100,直接暴力求解,类型全开long long,水过!
AC代码:
 #include<iostream>
using namespace std;
typedef long long LL;
LL n,k,m,ans;
int main(){
while(cin>>n>>k&&(n+k)){
m=k;//记录原来的取法数量
if(n-k<k)k=n-k;//取最小的取法数量
ans=;
for(LL i=;i<=k;++i)ans=ans*(n-i+)/i;
cout<<n<<" things taken "<<m<<" at a time is "<<ans<<" exactly."<<endl;
}
return ;
}

O - Combinations (组合数学)的更多相关文章

  1. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  2. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  4. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. Leetcode 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  7. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  8. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. Combination Sum II Combinations

    https://leetcode.com/problems/combination-sum-ii/ 题目跟前面几道题很类似,直接写代码: class Solution { public: vector ...

随机推荐

  1. SpringBoot Data JPA 关联表查询的方法

    SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...

  2. [NOIP2006] 普及组

    明明的随机数 STL真是偷懒神器 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstri ...

  3. linux 常见名词及命令(三)

    tar 用于对文件打包压缩或解压. 示例: 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -zxvf 压缩包名.tar.gz -c 创建压缩文件 -x ...

  4. HDU1166 线段树裸题 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. win7笔记本如何设置共享网络供手机WIFI上网?

    第一步 按WIN+R调出“运行”栏,在“运行”菜单栏输入“cmd”,出现命令提示符,输入命令“netsh wlan set hostednetwork mode=allow ssid=xiaoming ...

  6. iPhone 3gs 5.0.1降級到4.3.3 昨晚搞定(有shsh備份)

    經過昨天白天一天的學習和準備,終於一次降級成功. 手機未降級時狀態: 無鎖港版   3GS 16G  固件:5.0.1  基帶:05.16.05 記錄且分享降級完整步驟: 準備以下軟件.工具 官網固件 ...

  7. 使用Swift模拟Window-LFU

    今天參加了某公司2015的校招的机试,大题开放题比較多.有一道大题是Window-LFU比較有意思,当时题目搞了半天没搞明确让干啥- -题目大概是这种:实现一个Window-LFU缓存(事实上就是用数 ...

  8. react组件是怎么来的

    组件的创造方法为React.createClass() ——创造一个类,react系统内部设计了一套类系统,利用它来创造react组件.但这并不是必须的,我们还可以用es6的class类来创造组件,这 ...

  9. java Bean及其使用

    1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...

  10. Delphi各销售版本之间的区别

    初步的区别: http://www.embarcadero.com/products/delphi/product-editions http://www.embarcadero.com/fr/pro ...