I: 组合数末尾的零

I: 组合数末尾的零

Submit Page Time Limit: 1 Sec Memory Limit: 128 Mb
Submitted: 53 Solved: 45

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>
#include <set>
#include <map>
using namespace std;

char s[10], c[10];
int A[1000000];
int main()
{
    int T;
    cin >> T;
    while (T--) {
        int m, n, count = 0;
        int m0=0, n0=0, mn0=0;
        cin >> m >> n;
        for (int i = 1; i <= m; i++) {


            int x = i;
            while (x) {
                if (x & 1)break;//注意必须写在里面,这样为奇数时,跳出while,还会进行赋值语句
                count++;
                x /= 2;
            }
            if (i == m - n)mn0 = count;
             if (i == n)n0 = count;
             if (i == m)m0 = count;

        }
        cout << m0 -n0-mn0<< endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值