csu A Easy Problem 六十四位无符号组合数

This is a classical problem. Your job is to calculate the combination formula  C ( n , m )  , and  C ( n , m )  fit a 64-bit unsigned integer.(注意答案的范围)


Input

The first line of input there is one integer  T ( T 100), giving the number of test cases in the input. Each test case contains a line with two integer n and m(0 m n <2 64 ).
Output

For each case, output  the answer  C ( n , m ) corresponding to the input.
Sample Input
1
4 2
Sample Output
6



因为数据范围问题,所以min(n,n-m)中最小的一定小于两万

然后通过这个来计算组合数

注意里面的两个特判


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

#define MAXN 20010
#define LL unsigned long long

LL gcd(LL a,LL b)
{
    LL t;
    while(b)
    {
        t=a%b;
        a=b;
        b=t;
    }
    return a;
}

LL demo[MAXN],nume[MAXN];
LL C(LL n,LL m)
{
    if(n==m||m==0)
        return 1;
    if(m==n-1||m==1)
        return n;

    LL cnt1=0,cnt2=0,t,ans=1;
    for(LL i=n-m+1;i<=n;++i)
        nume[cnt1++]=i;
    for(LL i=2;i<=m;++i)
        demo[cnt2++]=i;
    for(LL i=0;i<cnt2;++i){
        for(LL j=0;demo[i]!=1&&j<cnt1;++j){
            t=gcd(demo[i],nume[j]);
            demo[i]/=t;
            nume[j]/=t;
        }
    }
    for(int i=0;i<cnt1;++i)
        ans*=nume[i];
    return ans;
}

int main()
{
    int T;
    LL n,m;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%llu%llu",&n,&m);
        m=min(m,n-m);
        LL ans=C(n,m);
        printf("%llu\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值