sdssudhu

FFT

Aug 15th, 2018
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1.     #define PI acos(-1.0)
  2.     class point
  3.     {
  4.         public:
  5.             double x,y;
  6.             point(double _x=0,double _y=0){x=_x;y=_y;}
  7.             point operator+(const point &t)const{return point(x+t.x,y+t.y);}
  8.             point operator-(const point &t)const{return point(x-t.x,y-t.y);}
  9.             point operator*(const point &t)const{return point(x*t.x-y*t.y,x*t.y+y*t.x);}
  10.             void operator/=(const int n){x=x/n; y=y/n; return;}
  11.             point conj()const{return point(x,-y);}
  12.             double real()
  13.             {
  14.                 return x;
  15.             }
  16.     };
  17.  
  18.     #define base point
  19.     void fft(vector<base> &a,bool invert)
  20.     {
  21.  
  22.         int n=a.size(),i,j;
  23.  
  24.         for(i=1,j=0;i<n;++i)    //Bit reverse
  25.         {
  26.             int bit=n>>1;
  27.             for(;j>=bit;bit>>=1)
  28.                 j=j-bit;
  29.             j=j+bit;
  30.             if(i<j)
  31.                 swap(a[i],a[j]);
  32.         }
  33.  
  34.  
  35.         for(int len=2;len<=n;len<<=1)
  36.         {
  37.             ld ang=2*PI/len*(invert?-1:1);
  38.             base wlen(cos(ang),sin(ang));
  39.             for(i=0;i<n;i+=len)
  40.             {
  41.                 base w(1);
  42.                 for(int j=0;j<len/2;++j)
  43.                 {
  44.                     base u=a[i+j],v=a[i+j+len/2]*w;
  45.                     a[i+j]=u+v;
  46.                     a[i+j+len/2]=u-v;
  47.                     w=w*wlen;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         if(invert)
  53.         {
  54.             for(i=0;i<n;++i)
  55.                 a[i]/=n;
  56.         }
  57.     }
  58.     vector<ll> res,a,b;
  59.  
  60.     void mul(const vector<ll> &a,const vector<ll> &b)
  61.     {
  62.         vector<base> fa(a.begin(),a.end()),fb(b.begin(),b.end());
  63.         size_t n=1;
  64.         int i;
  65.         while(n<max(a.size(),b.size())) n<<=1;
  66.         n<<=1;
  67.         fa.resize(n),fb.resize(n);
  68.         fft(fa,false);fft(fb,false);
  69.         for(i=0;i<n;++i)
  70.         {
  71.             fa[i]=fa[i]*fb[i];
  72.         }
  73.         fft(fa,true);
  74.         res.resize(n);
  75.         for(i=0;i<n;++i)
  76.             res[i]=(ll)(fa[i].real()+0.5);
  77.     }
  78.     ll s,sq;    //s=ceil(sqrt(mod)) and sq=(s*s)%mod
  79.     void fft_modulo(const vector<ll> &a,const vector<ll> &b)
  80.     {
  81.         vector<ll> res1,res2,res3,res4,a1,a2,b1,b2;
  82.        
  83.         int i;
  84.         ll x;
  85.         for(i=0;i<a.size();++i)
  86.         {
  87.             x=a[i]%mod;
  88.             a1.pb(x%s);
  89.             a2.pb(x/s);
  90.         }
  91.         for(i=0;i<b.size();++i)
  92.         {
  93.             x=b[i]%mod;
  94.             b1.pb(x%s);
  95.             b2.pb(x/s);
  96.         }
  97.         mul(a1,b1);
  98.         res1=res;
  99.         res.clear();
  100.         mul(a1,b2);
  101.         res2=res;
  102.         res.clear();
  103.         mul(a2,b1);
  104.         res3=res;
  105.         res.clear();
  106.         mul(a2,b2);
  107.         res4=res;
  108.         for(i=0;i<res.size();++i)
  109.         {
  110.             res[i]=(res1[i]+(((res2[i]+res3[i]))*s)+(((res4[i])*sq)))%mod;  //Add mods if needed for overflow
  111.         }
  112.     }
Add Comment
Please, Sign In to add comment