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. linux shell内置判断

    内置判断,成功的时候返回0,不成功返回非零 test  判断表达式 [ 判断表达式 ]       注意前后必须留空格哦 数值运算 -eq   等于 -ne   不等于 -gt     大于 -ge ...

  2. mysql基本面试题

    1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 1. 主:binlog线程——记录下所有改变了数据库数据的语句,放进master上的binlog中: 2. 从:io线程——在 ...

  3. JS基础学习——对象

    JS基础学习--对象 什么是对象 对象object是JS的一种基本数据类型,除此之外还包括的基本数据类型有string.number.boolean.null.undefined.与其他数据类型不同的 ...

  4. 使用Android Studio搭建Android开发环境

    一.Android Studio简单介绍 2013年GoogleI/O大会首次发布了Android Studio IDE(Android平台集成开发环境).它基于Intellij IDEA开发环境,旨 ...

  5. CocoaPods管理的项目移植到别人电脑后找不到头文件

    CocoaPods管理的项目移植到别人电脑后找不到头文件 在TARGETS -> Search Paths -> User Header Search Paths 中 写入 ${SRCRO ...

  6. java:工具类

    Google guava工具类的介绍和使用:https://blog.csdn.net/wwwdc1012/article/details/82228458 Apache Commons 工具类介绍及 ...

  7. percona mysql 5.7再centerOS 7上的安装

    第一次测试装的,还不是很熟练.很多东西不太对,以后还回改进 一.卸载包检查是否安装有MySQL Server: rpm -qa | grep mysql rpm -qa | grep mariadb ...

  8. 《O2O实战:二维码全渠道营销》读书笔记思维导图(530KB)

  9. 设计模式——抽象工厂模式(AbstractFactoryPattern)

    抽象工厂模式(AbstractFactory):提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. UML图: IFactory: package com.cnblog.clarck ...

  10. Android(java)学习笔记43:Map集合的遍历之键找值

    1. Map集合的遍历之键找值  package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.u ...