Advertisement
happy_nesquik

Untitled

Sep 28th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MOD 1300031
  6.  
  7. typedef long long ll;
  8.  
  9. ll fexp(ll a, ll b){
  10.     if(b==0) return 1;
  11.     if(b==1) return a;
  12.     if(b%2==0) return fexp((a*a)%MOD, b/2);
  13.     else return (fexp((a*a)%MOD, (b-1)/2) * a)%MOD;
  14. }
  15.  
  16. int main(){
  17.     int k;
  18.     cin >> k;
  19.     ll n, c;
  20.     ll up;
  21.     ll resp=1;
  22.     for(int i=0; i<k; i++){
  23.         resp = 1;
  24.         cin >> n >> c;
  25.         up = n+c-1;
  26.         if(up==c){
  27.             cout << 1 << endl;
  28.             continue;
  29.         }
  30.         if(up >= MOD){
  31.             cout << 0 << endl;
  32.             continue;
  33.         }
  34.         for(ll i=up; i>up-min(n-1, c); i--){
  35.             resp = (resp*i)%MOD;
  36.         }
  37.         for(ll i=2; i<=min(n-1, c); i++){
  38.             resp = (resp*fexp(i, MOD-2))%MOD;
  39.         }
  40.         cout << resp << endl;
  41.     }
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement