ACM:组合数末尾的零
Description
从m个不同元素中取出n (n ≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数。组合数的计算公式如下:
C(m, n) = m!/((m - n)!n!)
现在请问,如果将组合数C(m, n)写成二进制数,请问转这个二进制数末尾有多少个零。
Input
第一行是测试样例的个数T,接下来是T个测试样例,每个测试样例占一行,有两个数,依次是m和n,其中n ≤ m ≤ 1000。
Output
分别输出每一个组合数转换成二进制数后末尾零的数量。
Sample Input
2
4 2
1000 500
**
Sample Output
**
1
6
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 1005
int count1[N]; //N中含有2的几次方
int num[N]; //N阶乘中含有2的几次方
void jiecheng()
{
memset(count1,0,sizeof(count1));
memset(num,0,sizeof(num));
for(int i=1;i<N;i++)
{
int temp=i;
while(temp!=0)
{
if(temp%2==0)
{
count1[i]++;
temp /=2;
}
else
{
break;
}
}
}
for(int j=1;j<N;j++)
for(int i=1;i<=j;i++)
num[j]+=count1[i];
}
int main()
{
int n;
jiecheng();
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
cout<<num[a]-num[a-b]-num[b]<<endl;
}
return 0;
}