Advertisement
STANAANDREY

combi rev int

Jun 14th, 2021
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. constexpr int NMAX = 5e5 + 3;
  4. constexpr int MOD = 1e9 + 7;
  5. int fact[NMAX] = {1};
  6.  
  7. void precomputeFact() {
  8.     for (int i = 1; i < NMAX; i++) {
  9.         fact[i] = 1LL * i * fact[i - 1] % MOD;
  10.     }
  11. }
  12.  
  13. int logpow(int base, int exp) {
  14.     int ans = 1;
  15.     while (exp) {
  16.         if (exp & 1) {
  17.             ans = 1LL * ans * base % MOD;
  18.         }
  19.         base = 1LL * base * base % MOD;
  20.         exp >>= 1;
  21.     }
  22.     return ans;
  23. }
  24.  
  25. int getComb(int n, int k) {
  26.     if (!k || !n) {
  27.         return 1;
  28.     }
  29.     return 1LL * fact[n] * logpow(1LL * fact[k] * fact[n - k] % MOD, MOD - 2) % MOD;
  30. }
  31.  
  32. void solve() {
  33.     int n, k;
  34.     cin >> n >> k;
  35.     cout << getComb(n, k) << endl;
  36. }
  37.  
  38. signed main() {
  39.     int t(1);
  40.     cin >> t;
  41.     precomputeFact();
  42.     while (t--) {
  43.         solve();
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement