O - Combinations (组合数学)
Description
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
Output
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 (组合数学)的更多相关文章
- Python itertools.combinations 和 itertools.permutations 等价代码实现
最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...
- Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [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 ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 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 ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Combination Sum II Combinations
https://leetcode.com/problems/combination-sum-ii/ 题目跟前面几道题很类似,直接写代码: class Solution { public: vector ...
随机推荐
- [K/3Cloud] 隐藏菜单后,如何在插件间接的调用隐藏菜单的操作
使用场景: 动态表单里面挂了个单据的序时薄,序时薄有菜单,但是把序时薄的工具栏隐藏了.新增,修改全部动态表单自己写.删除和过滤我想间接调用下隐藏的序时薄的删除和过滤按钮的操作.在插件里如何实现? 答: ...
- android view自定义
转载自:http://blog.csdn.net/iispring/article/details/50708044
- java 判断一个字符串是否为纯数字
if (getUid().matches("[0-9]+")) { Log.v("纯数字");} else { Log.v("非纯数字"); ...
- Jquery为DIV添加点击事件,Jquery为a标签超链接添加点击事件
<div>1</div> <div>2</div> <div>3</div> <div>4</div> ...
- Bad Luck Island-CodeForce(dp)
链接:http://codeforces.com/problemset/problem/540/D 题目大意: 这个岛上有三种生物 r石头 s剪刀 p布 求最后只剩一种生物的概率 用dp[i][ ...
- cogs——21. [HAOI2005] 希望小学
21. [HAOI2005] 希望小学 ★★ 输入文件:hopeschool.in 输出文件:hopeschool.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述 ...
- 第二十七篇:Windows驱动中的PCI, DMA, ISR, DPC, ScatterGater, MapRegsiter, CommonBuffer, ConfigSpace
近期有些人问我PCI设备驱动的问题, 和他们交流过后, 我建议他们先看一看<<The Windows NT Device Driver Book>>这本书, 个人感觉, 这本书 ...
- docker init 起步
#yum install wget http://fedora.mirror.nexicom.net/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y ...
- USRP内部的寄存器
usrp_regs.hpp #ifndef INCLUDED_USRP2_REGS_HPP #define INCLUDED_USRP2_REGS_HPP ////////////////////// ...
- 下载的php_yal.dll文件添加到php的ext
下载的php_yal.dll文件添加到php的ext http://pecl.php.net/package/yaf/3.0.6/windows yaf. use_namespace=1 ;;exte ...