Advertisement
STANAANDREY

reclama (8)

Dec 28th, 2021
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll power(ll x, int y, int p)
  6. {
  7.     ll res = 1;
  8.  
  9.     x = x % p;
  10.  
  11.     while (y > 0)
  12.     {
  13.  
  14.         if (y & 1)
  15.             res = (res * x) % p;
  16.  
  17.         y = y >> 1;
  18.         x = (x * x) % p;
  19.     }
  20.     return res;
  21. }
  22.  
  23. ll modInverse(ll n, int p)
  24. {
  25.     return power(n, p - 2, p);
  26. }
  27.  
  28. ll nCrModPFermat(ll n, int k, int p)
  29. {
  30.     if (n < k)
  31.         return 0;
  32.  
  33.     if (k == 0)
  34.         return 1;
  35.  
  36.     ll fac[n + 1];
  37.     fac[0] = 1;
  38.     for (int i = 1; i <= n; i++)
  39.         fac[i] = (fac[i - 1] * i) % p;
  40.  
  41.     return (fac[n] * modInverse(fac[k], p) % p
  42.             * modInverse(fac[n - k], p) % p)
  43.            % p;
  44. }
  45.  
  46. int main()
  47. {
  48.     int n, k = 5, p = 666013;
  49.     cin >> n;
  50.     cout << nCrModPFermat(n, k, p) << endl;
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement