Advertisement
Guest User

Untitled

a guest
Jun 12th, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FOR(i,a,b) for(int i=(a);i<(b);++i)
  4. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  5. #define VAR(v, i) __typeof(i) v=(i)
  6. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  7. #define all(v) (v).begin(),(v).end()
  8.  
  9. #define PII pair<int,int>
  10. #define mp make_pair
  11. #define st first
  12. #define nd second
  13. #define pb push_back
  14. #define lint long long int
  15. #define VI vector<int>
  16.  
  17. #define debug(x) {cerr <<#x <<" = " <<x <<endl; }
  18. #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; }
  19. #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; }
  20. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
  21. #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}
  22.  
  23. #define make( x) int (x); scanf("%d",&(x));
  24. #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
  25. #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
  26. #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
  27. #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);}
  28. #define IOS ios_base::sync_with_stdio(0)
  29. #define HEAP priority_queue
  30.  
  31. #define read( x) scanf("%d",&(x));
  32. #define read2( x, y) scanf("%d%d",&(x),&(y));
  33. #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
  34. #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
  35. #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}
  36.  
  37.  
  38. using namespace std;
  39.  
  40.  
  41. #define CD complex<double>
  42.  
  43.  
  44. int mod = 1e9+7;
  45. const int max_n = 1e6+5;
  46. const double eps = 1e-8;
  47. CD roots[max_n];
  48. CD t1[max_n], t2[max_n];
  49.  
  50. int N;
  51. int n1, n2; // INPUT
  52. VI A, B;
  53. int ANS[max_n];
  54.  
  55. void FFT(CD t[]) {
  56.     FOR(i,0,N) {
  57.         int j = 0;
  58.         for (int k=1; k<N; k<<=1, j<<=1) if(k&i) j++;
  59.         j>>=1;
  60.         if(i<j) swap(t[i], t[j]);
  61.     }
  62.     int step = 1, n_step = 0;
  63.     while ((1<<n_step)<N) n_step++;
  64.     for (int step=1; step<N; step<<=1) {
  65.         --n_step;
  66.         for(int i = 0; i<N; i+=2*step) FOR(j,0,step) {
  67.             CD u = t[i+j], v = t[i+j+step];
  68.             t[i+j] = u + v*roots[j<<n_step];
  69.             t[i+j+step] = u-v*roots[j<<n_step];
  70.         }
  71.     }
  72. }
  73.  
  74. VI conv(VI& v1, VI& v2) {
  75.     n1 = v1.size(); n2 = v2.size();
  76.     N = 1;
  77.     while(N<n1 || N<n2) N<<=1;
  78.     N<<=1;
  79.     FOR(i,0,N) {
  80.         t1[i] = CD(0.0,0.0);
  81.         t2[i] = CD(0.0,0.0);
  82.         roots[i] = CD(cos(2*M_PI*i/N), -sin(2*M_PI*i/N));
  83.     }
  84.     FOR(i,0,n1) t1[i] = CD(v1[i],0.0);
  85.     FOR(i,0,n2) t2[i] = CD(v2[i],0.0);
  86.     FFT(t1); FFT(t2);
  87.     double s = 1.0/N;
  88.     FOR(i,0,N) {
  89.         t1[i] *= t2[i]*s;
  90.         roots[i] = CD(real(roots[i]), -imag(roots[i]));
  91.     }
  92.     FFT(t1);
  93.    
  94.     FOR(i,0,N) {
  95.         ANS[i] = ((lint) (real(t1[i])+0.5))%mod;
  96.         roots[i] = CD(real(roots[i]), -imag(roots[i]));
  97.     }
  98.     VI ans; FOR(i,0,n1+n2) ans.pb(ANS[i]%mod);
  99.     return ans;
  100. }
  101.  
  102. VI v;
  103.  
  104. int SQ = 30000;
  105.  
  106. VI conv2(VI& v1, VI& v2) {
  107.     VI w1, w2, w3, w4;
  108.     FOR(i,0,v1.size()) w1.pb(v1[i]%SQ), w3.pb(v1[i]/SQ);
  109.     FOR(i,0,v2.size()) w2.pb(v2[i]%SQ), w4.pb(v2[i]/SQ);
  110.     VI ans1 = conv(w1, w2);
  111.     VI ans2 = conv(w1, w4);
  112.     VI ans3 = conv(w3, w2);
  113.     VI ans4 = conv(w3, w4);
  114.     FOR(i,0,ans1.size()) {
  115.         int myns = (ans1[i] + ans2[i] *1LL*SQ + ans3[i] * 1LL * SQ + ans4[i] * 1LL * SQ * 1LL * SQ) % mod;
  116.         ans1[i] = myns;
  117.     }
  118.     return ans1;
  119. }
  120.  
  121. VI rob(int a, int b) {
  122.     if (a==b) {
  123.         VI ans(2);
  124.         ans[0] = v[a];
  125.         ans[1] = 1;
  126.         return ans;
  127.     } else {
  128.         int mid = (a+b)/2;
  129.         VI v1 = rob(a, mid);
  130.         VI v2 = rob(mid+1, b);
  131.         return conv2(v1, v2);
  132.     }
  133. }
  134.  
  135.  
  136.  
  137.  
  138. int main() {
  139.     make(myn);
  140.     readv(v, myn);
  141.     VI ans = rob(0, myn - 1);
  142.     int loc = 0;
  143.     FOR(i,0,ans.size()) {
  144.         loc = (loc + (myn-i)*1LL*ans[i])%mod;
  145.     }
  146.     printf("%d\n", loc);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement