Beingamanforever

Partition Dp, nCr, invFact, fact precomputation

Dec 9th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-09 16:01:27
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. const int N = 2005;
  26. int fact[N];
  27. int invfact[N];
  28. // binary exponentiation
  29. int binpow(int a, int b, int m = (int)1e9 + 7)
  30. {
  31.     a %= m;
  32.     int res = 1;
  33.     while (b > 0)
  34.     {
  35.         if (b & 1)
  36.         {
  37.             res = (res * a) % m;
  38.         }
  39.         a = (a * a) % m;
  40.         b >>= 1;
  41.     }
  42.     return res;
  43. }
  44. void precompute()
  45. {
  46.     fact[0] = 1;
  47.     for (int i = 1; i < N; i++)
  48.     {
  49.         fact[i] = (fact[i - 1] * i) % mod;
  50.     }
  51.     invfact[N - 1] = binpow(fact[N - 1], mod - 2); // calculated from back, as invfact[N-1] is the least
  52.     for (int i = N - 2; i >= 0; i--)
  53.     {
  54.         invfact[i] = (invfact[i + 1] * (i + 1)) % mod;
  55.     }
  56. }
  57. int nCr(int n, int r)
  58. {
  59.     if (r < 0 || r > n)
  60.     {
  61.         return 0;
  62.     }
  63.     return (((fact[n] * invfact[r]) % mod) * (invfact[n - r] % mod)) % mod;
  64. }
  65. void solve()
  66. {
  67.     // similar to JEE question, first distribute 2 items each to k groups
  68.     // left with n-2k, now each group be given atleast 1 item, done in (n-2k-1 C k-1) ways
  69.     int n;
  70.     cin >> n;
  71.     precompute();
  72.     if (n < 3)
  73.     {
  74.         cout << 0 << endl;
  75.         return;
  76.     }
  77.     int ans = 0;
  78.     int parts = 1;
  79.     // parts means number of terms in sequence
  80.     while ((n - 3 * parts) >= 0)
  81.     {
  82.         // give 3 to each part
  83.         int left = n - 3 * parts;
  84.         // now distribute left to parts
  85.         ans += nCr(left + parts - 1, parts - 1);
  86.         ans %= mod;
  87.         parts++;
  88.     }
  89.     cout << ans << endl;
  90.     return;
  91. }
  92.  
  93. signed main()
  94. {
  95.     NeedForSpeed;
  96.     int t = 1;
  97.     // cin >> t;
  98.     while (t--)
  99.     {
  100.         solve();
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment