Live2d Test Env

zoj 1938 Binomial Showdown 组合数裸基础

Binomial Showdown

Time Limit: 2 Seconds      Memory Limit: 65536 KB

In how many ways can you choose k elements out of n elements, not taking order into account?

Write a program to compute this number.


Input

The input will contain one or more test cases.

Each test case consists of one line containing two integers n (n >= 1) and k (0 <= k <= n).

Input is terminated by two zeroes for n and k.


Output

For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2^31.


Sample Input

4 2
10 5
49 6
0 0


Sample Output

6
252
13983816

之前遇到的组合数都稍微复杂一点,所以习惯用唯一分解来解决。这道题就是纠结了一下,还是懒得写唯一分解了,麻烦。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;

int main()
{
	long long  n,m;
	long long ans;
	while(cin>>n>>m)
	{
		if(n==0) return 0;
		if(n-m<m) m=n-m;
		ans=1;
		for(int i=1;i<=m;i++)
		 ans=ans*(n-i+1)/i;//不过先乘后除会不会超出longlong呢??? 
		cout<<ans<<endl;
	}
	return 0;
}
假如先乘法感觉有数据会超吧,怎么处理呢???求大神指教。




posted @ 2017-09-27 21:06  nimphy  阅读(75)  评论(0编辑  收藏  举报