Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. #include<set>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. int n, m;
  8. vector<long long> res;
  9. using cd = complex<double>;
  10. const double PI = acos(-1);
  11.  
  12. void fft(vector<cd> & a, bool invert) {
  13.     int n = a.size();
  14.     if (n == 1)
  15.         return;
  16.  
  17.     vector<cd> a0(n / 2), a1(n / 2);
  18.     for (int i = 0; 2 * i < n; i++) {
  19.         a0[i] = a[2*i];
  20.         a1[i] = a[2*i+1];
  21.     }
  22.     fft(a0, invert);
  23.     fft(a1, invert);
  24.  
  25.     double ang = 2 * PI / n * (invert ? -1 : 1);
  26.     cd w(1), wn(cos(ang), sin(ang));
  27.     for (int i = 0; 2 * i < n; i++) {
  28.         a[i] = a0[i] + w * a1[i];
  29.         a[i + n/2] = a0[i] - w * a1[i];
  30.         if (invert) {
  31.             a[i] /= 2;
  32.             a[i + n/2] /= 2;
  33.         }
  34.         w *= wn;
  35.     }
  36. }
  37.  
  38. vector<long long> multiply(vector<long long> const& a, vector<long long> const& b) {
  39.     vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  40.     long long n = 1;
  41.     while (n < a.size() + b.size())
  42.         n <<= 1;
  43.     fa.resize(n);
  44.     fb.resize(n);
  45.  
  46.     fft(fa, false);
  47.     fft(fb, false);
  48.     for (long long i = 0; i < n; i++)
  49.         fa[i] *= fb[i];
  50.     fft(fa, true);
  51.  
  52.     vector<long long> result(n);
  53.     for (long long i = 0; i < n; i++)
  54.         result[i] = round(fa[i].real());
  55.     return result;
  56. }
  57.  
  58. int main(){
  59.     int T;
  60.     cin>>T;
  61.     while (T--){
  62.        
  63.         cin>>n;
  64.         vector<long long> v1(n+1, 0);
  65.         vector<long long> v2(n+1, 0);
  66.         for (int i = 0; i <=n; i++){
  67.             cin>>m;
  68.             v1.push_back(m);
  69.         }
  70.         for (int i = 0; i <=n; i++){
  71.             cin>>m;
  72.             v2.push_back(m);
  73.         }
  74.         reverse(v1.begin(), v1.end());
  75.         reverse(v2.begin(), v2.end());
  76.         res = multiply(v1, v2);
  77.         for (int i = 0; i < 2*n + 1; i++){
  78.             cout<<res[i]<<" ";
  79.         }
  80.         cout<<endl;
  81.     }    
  82.    
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement