Combinations 组合 思维问题
2012-08-22 23:05
323 查看
Combinations
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
Sample Output
Source
UVA Volume III 369
poj上的思维问题。
就是求组合Cnm
队友们竟然提供了三种方法。
1采用 __int64 利用乘一个分子和下面所有的分母进行约分,约分过的就不用继续约分了。这样能有效的减少存储的位数。
2 利用double来存储长位数,这样知道了分子和分母的总共乘积,就可以求出结果了。
3 杨辉三角 Cnm=Cn-1m+Cn-1m-1
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7499 | Accepted: 3509 |
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.
Source
UVA Volume III 369
poj上的思维问题。
就是求组合Cnm
队友们竟然提供了三种方法。
1采用 __int64 利用乘一个分子和下面所有的分母进行约分,约分过的就不用继续约分了。这样能有效的减少存储的位数。
#include<stdio.h> int a[120]; int main() { __int64 n,m,i,j,sum=1,num; while(scanf("%I64d %I64d",&n,&m)) { if(n==0&&m==0)break; for(i=1;i<=110;i++) a[i]=0; sum=1; for(i=n-m+1;i<=n;i++) { num=i; for(j=2;j<=m;j++) { if(a[j]==1)continue; if(num%j==0){num/=j;a[j]=1;} } sum*=num; } for(j=2;j<=m;j++) { if(a[j]==0)sum/=j; } printf("%I64d things taken %I64d at a time is %I64d exactly.\n",n,m,sum); } }
2 利用double来存储长位数,这样知道了分子和分母的总共乘积,就可以求出结果了。
#include<stdio.h> int main() { int n,m,i; double sum1=1,sum2=1; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0)break; sum1=1; sum2=1; for(i=n-m+1;i<=n;i++) sum1*=double(i); for(i=1;i<=m;i++) sum2*=double(i); printf("%d things taken %d at a time is %.0lf exactly.\n",n,m,sum1/sum2); } }
3 杨辉三角 Cnm=Cn-1m+Cn-1m-1
#include<stdio.h> __int64 a[110][110]; int main() { int i,j; for(i=0;i<=101;i++) a[i][0]=1; for(i=1;i<=101;i++) a[i][i]=1; for(i=2;i<=101;i++) for(j=1;j<i;j++) { a[i][j]=a[i-1][j]+a[i-1][j-1]; } while(scanf("%d %d",&i,&j)!=EOF) { if(i==0&&j==0)break; printf("%d things taken %d at a time is %I64d exactly.\n",i,j,a[i][j]); } }
相关文章推荐
- Letter Combinations of a Phone Number:给定数字求出对应的多个字母的组合可能问题
- LeetCode—*组合问题Combinations
- Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)
- LeetCode Letter Combinations of a Phone Number 经典组合问题
- Leetcode中的组合排列问题:Permutations,Combinations,Letter Combinations of a Phone Number
- HDU6038-Function 思维+组合问题
- 手机键盘上的排列组合问题 Letter Combinations of a Phone Number
- 某基础的组合问题
- POJ 1276 取款机零钱组合问题 动态规划
- 2中方法实现数学中组合问题《在10个数中找出任意6个数的组合,数字不能》
- 第三周项目四 穷举法解决组合问题(百钱买百鸡)
- 利用穷举法解决组合问题,背包问题,变量相等问题
- 组合数学第二发 错排问题 hdu 2049 + hdu 2068
- 数组排列组合问题——BACKTRACKING
- 数的组合问题,从n个数中选出m个数,结果按照字典序排列
- 一个售前思维问题
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
- 引入进制概念解决排列组合问题
- 从排列与组合的python实现到"生日问题"的解释
- 在VMware 上安装各个系统遇到的问题及相应的解决办法,解决办法的思维