Problem Description
开学了,杭电又迎来了好多新生。ACMer想为新生准备一个节目。来报名要表演节目的人很多,多达N个,但是只需要从这N个人中选M个就够了,一共有多少种选择方法?
Input
数据的第一行包括一个正整数T,接下来有T组数据,每组数据占一行。
每组数据包含两个整数N(来报名的人数,1<=N<=30),M(节目需要的人数0<=M<=30)
Output
每组数据输出一个整数,每个输出占一行
Sample Input
5
3 2
5 3
4 4
3 6
8 0
Sample Output
3
10
1
0
1
分析: 30!超过了long long 存储空间,直接计算Cn m肯定wrong answer 了,因此我用了如下这个办法!!
//HDOJ2519
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
unsigned long long t1,x,y,sum=1,i,t=1;
double z;
cin>>t1;
while(t1--)
{
cin>>x>>y;
if(x==y&&x==0)
{
cout<<"0"<<endl;
continue;
}
else if(y==0||x==y)
{
cout<<"1"<<endl;
continue;
}
else if(x<y)
{
cout<<"0"<<endl;
continue;
}
else
{
if(x-y<y)y=x-y;
for( i=0;i<y;i++)
{
sum*=(x-i);
t=t*(i+1);
if(sum%t==0)
{
sum=sum/t;
t=1;
}
}
z=sum/t;
// cout<<z<<endl;
printf("%.0lf\n",z);
}
sum=t=1;
}
return 0;
}