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;
}