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.
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
#define M 105 ll n,k; int main()
{
ll i,j;
while(cin>>n>>k)
{
if(n==0&&k==0)
break;
if(k==n)
{
cout<<n<<" things taken "<<k<<" at a time is "<<1<<" exactly."<<endl;
continue;
}
cout<<n<<" things taken "<<k<<" at a time is ";
if(n-k<k)
k=n-k;
ll ans=1;
for(i=1;i<=k;i++)
{
ans=ans*(n-i+1)/i;
}
cout<<ans<<" exactly."<<endl;
}
}

  

poj_1306_Combinations的更多相关文章

随机推荐

  1. owin解决跨域js请求

    最近在用owin打了一个建议的http的api服务,但遇到了js跨域访问的问题,后来在网上找到了答案,已帮助遇到此问题的人 1.首先nuget先按着owin依赖的包,然后至关重要的一步是引用Micro ...

  2. 利用函数回调获取setInterval中返回的值

    我们都知道,定时器里面想返回值如果你用return根本没作用,那么怎么拿到定时器所返回的值呢, 现在只需要利用回调函数,给主函数传一个函数类型的参数callback,然后把想要返回的num再传给cal ...

  3. 18_CGLib动态代理

    [概述] 已知JDK动态代理中的Proxy.newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)方法的 ...

  4. 【SQL Server 2012】按倒序存储“分组统计”结果的临时表到新建表

    程序预先说明: 本文访问的数据库是基于存有RDF三元组的开源数据库Localyago修改的库,其中只有一个表,表中有五个属性:主语subject.谓语predict.宾语object.主语的编号sub ...

  5. css随笔

    1. 关于使用pre标签,且要自动换行 可以使用: word-wrap: break-word; white-space: pre-wrap; 2. 更改placeholder样式 .input::- ...

  6. oozie 完整流程实例

    Oozie概述: Oozie是一个基于Hadoop工作流引擎,也可以称为调度器,它以xml的形式写调度流程,可以调度mr,pig,hive,shell,jar,spark等等.在实际工作中,遇到对数据 ...

  7. [原]Android打包之Eclipse打包

    Android自动打包流程详细图: 步骤一: 在工程中新建一个build.xml. 步骤二: 给工程配置ant工具. 选择ant工具的步骤如下: Windows->Shown view-> ...

  8. Android获取手机安装的浏览器列表

    最近碰到一个同事询问如何查询本地安装的浏览器列表,其使用的代码如下: public static List<ResolveInfo> getBrowserList(Context cont ...

  9. Multi-Thread 1: how to use synchronized

    1. synchronized If two threads are using the same function( here we use output to print out string) ...

  10. HDU 5723 最小生成树上的期望

    题意:求最小生成树,和任意两个点之间距离的期望 官方题解: 最后求两遍点的积的时候,还是要判断父子关系. 注意 long long #include <bits/stdc++.h> usi ...