组合的个数

Description

 

m个不同元素中取出(≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数。组合数的计算公式如下:

C(mn) = m!/((n)!n!) 

现在请问,如果将组合数C(mn)写成二进制数,请问转这个二进制数末尾有多少个零。

Input

 

第一行是测试样例的个数T,接下来是T个测试样例,每个测试样例占一行,有两个数,依次是mn,其中n ≤ m≤ 1000。

Output

 

分别输出每一个组合数转换成二进制数后末尾零的数量。

Sample Input

2
4 2
1000 500 

Sample Output

1
6 
#include <iostream>
using namespace std;
int main()
{
    int t,a,b;cin>>t;
    while(t--){
        cin>>a>>b;
        int x=0,y=0;
        for(int i=a-b+1;i<=a;i++){
                int t=i;
            while(t>1){
                if(t%2==0&&t!=0)x++;
                else break;
                t/=2;
            }
        }
        for(int i=2;i<=b;i++){
            int t=i;
             while(t>1){
                if(t%2==0&&t!=0)y++;
                else break;
                t/=2;
            }
        }
        cout<<x-y<<endl;
    }
    return 0;
}
View Code

 

posted @ 2015-07-17 21:15  无解大神  阅读(182)  评论(0编辑  收藏  举报