组合数【组合数学】

组合数

链接:组合数

注意:溢出。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll mod = 1e18;
int main(void){
    ll n,k;
    while(cin>>n>>k){ 
        k = min(k,n-k);
        __int128 res = 1;
        int flag = 0;
        for(ll i = 1; i <= k; i++){
            res = res*(ll)(n-i+1)/(ll)i;
            if(res>mod){
                flag = 1;
                break;
            }
        }
        if(flag){
            cout<<mod<<endl;
        }else{
            cout<<(ll)res<<endl;
        }
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/AC-AC/p/12129451.html