The number of combinations [portfolio Mathematics

The number of combinations

Link: the number of combinations

Note : overflow.

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

Guess you like

Origin www.cnblogs.com/AC-AC/p/12129451.html