Description
问,如果将组合数C(m, n)写成二进制数,请问转这个二进制数末尾有多少个零。
Input
第一行是测试样例的个数T,接下来是T个测试样例,每个测试样例占一行,有两个数,依次是m和n,其中n ≤ m ≤ 1000。
Output
分别输出每一个组合数转换成二进制数后末尾零的数量。
Sample Input
24 21000 500
Sample Output
16
分析:
1、错误:首先想的是打表,把做所有需要的组合数DP出来,再求C(m ,n)的末尾0,,但是,呵呵1000的阶乘~~~~可能吗.
2、正确:不用打表,一个一个算就好了, 二进制末尾0的个数就是能 整除以 2 的次数。O(logn);很快
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int getCnt_0(int x); int main() { int t; int m, n; scanf("%d", &t); while (t--) { scanf("%d %d", &m, &n); int ans = getCnt_0(m) - (getCnt_0(m - n) + getCnt_0(n)); printf("%d\n", ans); } // system("pause"); return 0; } int getCnt_0(int x) { int cnt = 0; for (int i = 1; i <= x; i++) { int temp = i; while ((temp & 1) == 0) { cnt++; temp >>= 1; } } return cnt; }