Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cassert>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10.  
  11.  
  12. /*struct cmplx {
  13.     double x, y;
  14.     cmplx() { x = y = 0; }
  15.     cmplx(dbl x_, dbl y_) : x(x_), y(y_) {}
  16. };
  17.  
  18. inline cmplx operator+(cmplx a, cmplx b) { return cmplx(a.x + b.x, a.y + b.y); }
  19. inline cmplx operator-(cmplx a, cmplx b) { return cmplx(a.x - b.x, a.y - b.y); }
  20. inline cmplx operator*(cmplx a, cmplx b) { return cmplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
  21. inline cmplx conj(cmplx a) { return cmplx(a.x, -a.y); }*/
  22.  
  23. const int mod = 786433;
  24. const int root = 3;
  25. const int root_rev = 524289;
  26. const int root_pw = 1 << 18;
  27.  
  28. pair<int, int> ext_gcd(int a, int b)
  29. {
  30.     if (b == 0)
  31.         return {1, 0};
  32.        
  33.     auto ans = ext_gcd(b, a % b);
  34.     return {ans.second, ans.first - (a / b) * ans.second};
  35. }
  36.  
  37. int reverse(int a)
  38. {
  39.     int ans = ext_gcd(a, mod).first;
  40.     ans %= mod;
  41.     if (ans < 0)
  42.         ans += mod;
  43.     return ans;
  44. }
  45.  
  46.  
  47. void bit_reverse(vector<int>& a, int n, int q)
  48. {
  49.     int rev[n];
  50.     rev[0] = 0;
  51.     for (int j = 1; j < n; j++)
  52.         rev[j] = ((rev[j >> 1]) >> 1) ^ ((j & 1) << (q - 1));
  53.     for (int j = 0; j < n; j++)
  54.         if (j < rev[j])
  55.             swap(a[j], a[rev[j]]);
  56. }
  57.  
  58.  
  59. void fft (vector<int>& a, int n, int q, bool is_inverse) {
  60.     int w = is_inverse ? root_rev : root;
  61.     bit_reverse(a, n, q);
  62.  
  63.     for (int len = 2; len <= n; len <<= 1)
  64.     {
  65.         int wlen = w;
  66.         for (int i = len; i < root_pw; i <<= 1)
  67.             wlen = (int)((ll)wlen * wlen % mod);
  68.         for (int i = 0; i < n; i += len)
  69.         {
  70.             int x = 1;
  71.             for (int j = 0; j < len / 2; j++) {
  72.                 int u = a[i + j];
  73.                 int v = (int)((ll)a[i + j + len/2] * x % mod);
  74.                 a[i + j] = (u + v) % mod;
  75.                 a[i + j + len/2] = (u - v + mod) % mod;
  76.                 x = (int)((ll)x * wlen % mod);
  77.             }
  78.         }
  79.     }
  80.     if (is_inverse)
  81.     {
  82.         int nrev = (reverse(n) + mod) % mod;
  83.         for (int i = 0; i < n; i++)
  84.             a[i] = (int)(((ll)a[i] *  nrev) % mod);
  85.     }
  86. }
  87.  
  88. vector<int> operator+(const vector<int>& a, const vector<int>& b)
  89. {
  90.     vector<int> sum;
  91.     sum.assign(max(a.size(), b.size()), 0);
  92.     for (int i = 0; i < (int)a.size(); i++)
  93.         sum[i] += a[i];
  94.     for (int i = 0; i < (int)b.size(); i++)
  95.         sum[i] = (sum[i] + b[i]) % mod;
  96.     return sum;
  97. }
  98.  
  99.  
  100. vector<int> operator*(int x, const vector<int>& a)
  101. {
  102.     vector<int> prod(a);
  103.     for (int i = 0; i < (int)a.size(); i++)
  104.         prod[i] = (prod[i] * x) % mod;
  105.     return prod;
  106. }
  107.  
  108.  
  109. vector<int> operator*(const vector<int>& a, const vector<int>& b)
  110. {
  111.     cout << "{ ";
  112.     for (int el: a)
  113.         cout << el << " ";
  114.     cout << "}" << endl;
  115.     cout << "*" << endl;
  116.     cout << "{ ";
  117.     for (int el: a)
  118.         cout << el << " ";
  119.     cout << "}" << endl;
  120.     vector<int> fa(a);
  121.     vector<int> fb(b);
  122.     size_t n = 1, q = 0;
  123.     size_t k = a.size(), m = b.size();
  124.     for (; n < k + m + 1; n <<= 1 , q += 1);
  125.     fa.resize(n);
  126.     fb.resize(n);
  127.  
  128.     fft(fa, n, q, false),  fft (fb, n, q, false);
  129.     for (size_t i = 0; i < n; i++)
  130.         fa[i] *= fb[i];
  131.     fft (fa, n, q, true);
  132.     cout << "=\n{ ";
  133.     for (int el: fa)
  134.         cout << el << " ";
  135.     cout << "}" << endl << endl;
  136.     return fa;
  137. }
  138.  
  139.  
  140. int main()
  141. {
  142.     size_t n;
  143.     int h;
  144.     cin >> n >> h;
  145.     vector<int> a, b, c;
  146.     a = {1};
  147.     b = {0, 1};
  148.     for (int i = 2; i <= h; i++)
  149.     {
  150.         c = b * b + 2 * a * b;
  151.         a = b;
  152.         b = c;
  153.         cout << "#" << i << ": ";
  154.         for (int j: b)
  155.             cout << j << " ";
  156.         cout << endl;
  157.     }
  158.     cout << (b.size() < n + 1) ? 0 : b[n] % mod;
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement