Advertisement
osipyonok

CNK

Jul 1st, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. struct CNK{
  2.     const int mod = 1000000007;
  3.     int mx;
  4.    
  5. private:
  6.     vi fac , ifac;
  7.        
  8. public:
  9.     CNK(int max){
  10.         mx = max;
  11.        
  12.         fac.resize(max + 1);
  13.         ifac.resize(max + 1);
  14.        
  15.         fac[0] = ifac[0] = 1;
  16.        
  17.         for(int i = 1 ; i <= max ; ++i){
  18.             fac[i] = 1LL * fac[i - 1] * i % mod;
  19.             ifac[i] = fpow(fac[i], mod - 2);
  20.         }
  21.     }
  22.    
  23.     int fpow(int a , int b){
  24.         int ans = 1;
  25.         while(b > 0){
  26.             if(b & 1){
  27.                 ans = (1LL * ans * a) % mod;
  28.             }
  29.             a = (1LL * a * a) % mod;
  30.             b /= 2;
  31.         }
  32.         return ans;
  33.     }
  34.    
  35.     int cnk(int n , int k){
  36.         assert(mx >= n);
  37.         if (n < 0 || k < 0 || k > n) return 0;
  38.         return 1LL * fac[n] * ifac[n - k] % mod * ifac[k] % mod;
  39.     }
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement