ACM:组合数末尾的零


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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值