Advertisement
leoanjos

Infinite 2D Array

Oct 17th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define long long long int
  6.  
  7. const int MAX = 2e6 + 5;
  8. const int MOD = 1e9 + 7;
  9.  
  10. long facts[MAX] = {1LL};
  11.  
  12. void extended_euclidean(long a, long b, long &x, long &y) {
  13.     if (!b) {
  14.         x = 1LL;
  15.         y = 0LL;
  16.     } else {
  17.         extended_euclidean(b, a % b, x, y);
  18.         long aux = x; x = y;
  19.         y = aux - a / b * y;
  20.     }
  21. }
  22.  
  23. long mod_mult_inv(long a, long m) {
  24.     long x, y;
  25.     extended_euclidean(a, m, x, y);
  26.     return (x % m + m) % m;
  27. }
  28.  
  29. long comb(int n, int k) {
  30.     long num = facts[n];
  31.     long den = (facts[k] * facts[n - k]) % MOD;
  32.     return (num * mod_mult_inv(den, MOD)) % MOD;
  33. }
  34.  
  35. int main() {
  36.     ios_base::sync_with_stdio(false);
  37.     cin.tie(NULL);
  38.  
  39.     for (int i = 1; i < MAX; i++)
  40.         facts[i] = (facts[i - 1] * i) % MOD;
  41.    
  42.     long fibs[MAX] = {0LL};
  43.     fibs[1] = 1LL;
  44.  
  45.     for (int i = 2; i < MAX; i++)
  46.         fibs[i] = (fibs[i - 1] + fibs[i - 2]) % MOD;
  47.  
  48.     int x, y;
  49.     cin >> x >> y;
  50.  
  51.     long ans = 0LL, last = 0LL;
  52.     for (int i = x; i > 0; i--) {
  53.         long dist = x - i + y;
  54.         long cnt = comb(dist, x - i);
  55.         long aux = (cnt - last + MOD) % MOD;
  56.  
  57.         ans = (ans + aux * fibs[i]) % MOD;
  58.         last = cnt;
  59.     }
  60.  
  61.     last = 0LL;
  62.     for (int i = y; i > 0; i--) {
  63.         long dist = x + y - i;
  64.         long cnt = comb(dist, y - i);
  65.         long aux = (cnt - last + MOD) % MOD;
  66.  
  67.         ans = (ans + aux * fibs[i]) % MOD;
  68.         last = cnt;
  69.     }
  70.  
  71.     cout << ans << "\n";
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement