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

猜你喜欢

转载自blog.csdn.net/sc_jn/article/details/80257274
I