Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define forn(i, n) for (int i = 0; i < int(n); i++)
  4. #define fore(i, l, r) for (int i = int(l); i < int(r); i++)
  5. #define efor(i, l, r) for (int i = int(r) - 1; i >= int(l); i--)
  6. #define nfor(i, n) efor(i, 0, n)
  7. #define all(a) (a).begin(), (a).end()
  8. #define pb(a) push_back(a)
  9. #define sz(a) int((a).size())
  10. #define mp(x, y) make_pair((x), (y))
  11. #define x first
  12. #define y second
  13.  
  14. using namespace std;
  15.  
  16. template<typename X> inline X abs(const X& a) { return a < 0 ? -a : a; }
  17. template<typename X> inline X sqr(const X& a) { return a * a; }
  18.  
  19. typedef long long li;
  20. typedef long double ld;
  21. typedef pair<int, int> pt;
  22.  
  23. const int INF = int(1e9);
  24. const ld EPS = 1e-9;
  25.  
  26. int reverse(int x, int logn) { // use own complex!!!
  27.     int ans = 0;
  28.     forn(i, logn) if (x & (1 << i)) ans |= 1 << (logn - 1 - i);
  29.     return ans;
  30. }
  31.  
  32. typedef complex<ld> base;
  33. const ld PI = acosl(-1);
  34.  
  35. const int LOGN = 18, N = (1 << LOGN) + 3;
  36. void fft(base a[N], int n, bool inv) {
  37.     static base vv[LOGN][N];
  38.     static bool prepared = false;
  39.     if (!prepared) {
  40.         prepared = true;
  41.         forn(i, LOGN) {
  42.             ld ang = 2 * PI / (1 << (i + 1));
  43.             forn(j, 1 << i) vv[i][j] = base(cos(ang * j), sin(ang * j));
  44.         }
  45.     }
  46.     int logn = 0; while ((1 << logn) < n) logn++;
  47.     static base b[N];
  48.     forn(i, n) b[i] = a[reverse(i, logn)];
  49.     for (int i = 0; (1 << i) < n; i++)
  50.         for (int j = 0; j < n; j += (1 << (i + 1)))
  51.             for (int k = j; k < j + (1 << i); k++) {
  52.                 base cur = inv ? conj(vv[i][k - j]) : vv[i][k - j];
  53.                 base l = b[k], r = cur * b[k + (1 << i)];
  54.                 b[k] = l + r;
  55.                 b[k + (1 << i)] = l - r;
  56.             }
  57.     forn(i, n) a[i] = b[i] / ld(inv ? n : 1);
  58. }
  59.  
  60. void mul(li a[N], int n, li b[N], int m, li ans[N]) {
  61.     static base fp[N], p1[N], p2[N];
  62.     int cnt = n + m;
  63.     while (cnt & (cnt - 1)) cnt++;
  64.    
  65.     forn(i, cnt) fp[i] = base(i < n ? a[i] : 0, i < m ? b[i] : 0);
  66.     fft(fp, cnt, false); // one can simply rework it
  67.     forn(i, cnt) {       // with two calls of fft for p1, p2
  68.         p1[i] = (fp[i] + conj(fp[(cnt - i) % cnt])) / base(2, 0);
  69.         p2[i] = (fp[i] - conj(fp[(cnt - i) % cnt])) / base(0, 2);
  70.     }
  71.     forn(i, cnt) fp[i] = p1[i] * p2[i];
  72.     fft(fp, cnt, true);
  73.  
  74.     forn(i, cnt) ans[i] = li(fp[i].real() + 0.5);
  75. }
  76.  
  77. void mul(int* a, int* b, int n, const int mod) {
  78.     int smod = int(sqrt((ld) mod));
  79.     static li a1[N], a2[N], b1[N], b2[N], za[N], zb[N];
  80.     forn(i, n) {
  81.         a1[i] = a[i] % smod, a2[i] = a[i] / smod;
  82.         b1[i] = b[i] % smod, b2[i] = b[i] / smod;
  83.         za[i] = a1[i] + a2[i];
  84.         zb[i] = b1[i] + b2[i];
  85.     }
  86.     mul(a1, n, b1, n, a1);
  87.     mul(a2, n, b2, n, a2);
  88.     mul(za, n, zb, n, za);
  89.     forn(i, n) a[i] = 0;
  90.     forn(i, 2 * n) {
  91.         li cur = a[i % n];
  92.         cur += a1[i] % mod;
  93.         cur += a2[i] % mod * smod * smod % mod;
  94.         cur += (za[i] - a1[i] - a2[i]) % mod * smod % mod;
  95.         cur %= mod;
  96.         a[i % n] = int(cur < 0 ? (cur + mod) : cur);
  97.     }
  98. }
  99. int n;
  100.  
  101. bool read() {
  102.     return !!(cin >> n);
  103. }
  104.  
  105. const int m = 1000 * 1000 * 1000 + 7;
  106.  
  107. int gcd(int a, int b, int& x, int& y) {
  108.     if (!a) {
  109.         x = 0, y = 1;
  110.         return b;
  111.     }
  112.     int xx, yy, g = gcd(b % a, a, xx, yy);
  113.     x = yy - b / a * xx;
  114.     y = xx;
  115.     return g;
  116. }
  117.  
  118. inline int inv(int a) {
  119.     int x, y;
  120.     assert(gcd(a, m, x, y) == 1);
  121.     x %= m;
  122.     (x < 0) && (x += m);
  123.     return x;
  124. }
  125.  
  126. inline int add(int a, int b) { return a + b >= m ? a + b - m : a + b; }
  127. inline int sub(int a, int b) { return a - b < 0 ? a - b + m : a - b; }
  128. inline int mul(int a, int b) { return int(a * 1ll * b % m); }
  129. inline void inc(int& a, int b) { a = add(a, b); }
  130. inline void dec(int& a, int b) { a = sub(a, b); }
  131.  
  132. int fact[N], ifact[N];
  133.  
  134. inline int getC(int n, int k) {
  135.     if (k < 0 || k >= n) return 0;
  136.     return mul(fact[n], mul(ifact[k], ifact[n - k]));
  137. }
  138.  
  139. int pw[N];
  140. int z1[N], z2[N];
  141. int a[N], b[N], ab[N];
  142.  
  143. void solve() {
  144.     pw[0] = 1; fore(i, 1, N) pw[i] = mul(pw[i - 1], 4);
  145.     fact[0] = 1; fore(i, 1, N) fact[i] = mul(fact[i - 1], i);
  146.     forn(i, N) ifact[i] = inv(fact[i]);
  147.  
  148.     forn(k, N) {
  149.         int v = getC(k, k / 2);
  150.         z1[k] = (k & 1) ? 0 : mul(v, v);
  151.     }
  152.  
  153.     int blen = 10000;
  154.  
  155.     int ans = 0;
  156.     for (int i = 0; i <= n; i += blen) {
  157.         int j = min(n + 1, i + blen);
  158.         //cerr << i << ' ' << j << endl;
  159.         forn(p, n + 1) a[p] = z1[p];
  160.         forn(p, n + 1) b[p] = p < j ? z2[p] : 0;
  161.         mul(a, b, n + 1, m);
  162.  
  163.         fore(k, i, j) {
  164.             z2[k] = pw[k];
  165.             dec(z2[k], int(a[k] % m));
  166.             fore(l, i, k)
  167.                 dec(z2[k], mul(z1[k - l], z2[l]));
  168.             inc(ans, mul(z2[k], pw[n - k]));
  169.         }
  170.     }
  171.  
  172.     cout << ans << endl;
  173. }
  174.  
  175. int main() {
  176. #ifdef SU1
  177.     assert(freopen("input.txt", "rt", stdin));
  178.     //assert(freopen("output.txt", "wt", stdout));
  179. #endif
  180.  
  181.     cout << fixed << setprecision(10);
  182.     cerr << fixed << setprecision(6);
  183.  
  184. #ifdef SU1
  185.     ld startTime = clock() / ld(CLOCKS_PER_SEC);
  186. #endif
  187.  
  188.     while (read()) {
  189.         solve();
  190.         //break;
  191.     }
  192.  
  193. #ifdef SU1 
  194.     cerr << " == Time: " << (clock() / ld(CLOCKS_PER_SEC) - startTime) << " == " << endl;
  195. #endif
  196.  
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement