2019 Shanghai race icpc network optimization convolution + C FFT small-scale violence

https://nanti.jisuanke.com/t/41400

 

For large, we turn to the right to a an array of values ​​and b, a and c, c and b deconvolution, then the answer is $ n ^ 3 $, after subtracting the convolution illegal situation

However, because each time range $ $ 1E5, and the convolution to be performed six times, the complexity of a single set each time $ 6 * 2 ^ {ceil (log (2n))} ceil (log (2n)), n = 1e5, ceil (x) $ is rounded up,

A case where the test data set 100, can reach $ 2.8e9 $

However, we found that only one-fifth of data to meet the $ n> 1000 $

Therefore, for small-scale data, we directly use the $ n ^ 2 $ violence

#include<bits/stdc++.h>
#define ll long long
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
using namespace std;//head
const int maxn=4e5+10,maxm=2e6+10;
const ll INF=0x3f3f3f3f,mod=1e9+7;
int casn,n,m,k,kase=1,l,r;
namespace fastio{//@支持读取整数,字符串,输出整数@
bool isdigit(char c){return c>=48&&c<=57;}
const int maxsz=1e7;
class fast_iostream{public:
  char ch=get_char();
  bool endf=1,flag;
  char get_char(){
    static char buffer[maxsz],*a=buffer,*b=buffer;
    return b==a&&(b=(a=buffer)+fread(buffer,1,maxsz, stdin),b==a)?EOF:*a++;
  }
  template<typename type>bool get_int(type& tmp){
    flag=tmp=0;
    while(!isdigit(ch)&&ch!=EOF){flag=ch=='-';ch=get_char();};
    if(ch==EOF)return endf=0;
    do{tmp=ch-48+tmp*10;}while(isdigit(ch=get_char()));
    if(flag)tmp=-tmp;
    return 1;
  }
  int get_str(char* str){
    char* tmp=str;
    while(ch=='\r'||ch=='\n'||ch==' ')ch=get_char();
    if(ch==EOF)return(endf=0),*tmp=0;
    do{*(tmp++)=ch;ch=get_char();}while(ch!='\r'&&ch!='\n'&&ch!=' '&&ch!=EOF);
    *(tmp++)=0;
    return(int)(tmp-str-1);
  }
  fast_iostream& operator>>(char* tmp){get_str(tmp);return *this;}
  template<typename type>fast_iostream& operator>>(type& tmp){get_int(tmp);return *this;}
  operator bool() const {return endf;}
};
}fastio::fast_iostream io;

const double pi=acos(-1.0);
struct cp{double x,y;};
cp operator*(cp a,cp b){return {a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
cp operator+(cp a,cp b){return {a.x+b.x,a.y+b.y};}
cp operator-(cp a,cp b){return {a.x-b.x,a.y-b.y};}
class fourier{public:
  int rev[maxn],len,pw;
  void init(int n){
    len=1,pw=0;
    while(len<=n) len<<=1,pw++;
    rep(i,0,len-1) rev[i]=0;
    rep(i,0,len-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<(pw-1));
  }
  void transform(cp*a,int flag){
    rep(i,0,len-1) if(i<rev[i]) swap(a[i],a[rev[i]]);
    for(int mid=1;mid<len;mid<<=1){
      cp wn={cos(pi/mid),flag*sin(pi/mid)};
      for(int r=mid<<1,j=0;j<len;j+=r){
        cp t={1,0};
        for(int k=0;k<mid;k++,t=t*wn){
          cp x=a[j+k],y=t*a[mid+j+k];
          a[j+k]=x+y,a[j+k+mid]=x-y;
        }
      }
    }
    if(flag==-1) rep(i,0,len) a[i].x/=len;
  }
}fft;
int a[maxn],b[maxn],c[maxn];
cp cnta[maxn],cntb[maxn],cntc[maxn];
cp resa[maxn],resb[maxn],resc[maxn];
ll suma[maxn],sumb[maxn],sumc[maxn];
#define cin io
int main(){
  cin>>casn;
  while(kase<=casn){
    cin>>n;
    fft.init(1e5+1e5+2);
    if(n<=1000){
      rep(i,1,n)cin>>a[i];
      rep(i,1,n)cin>>b[i];
      rep(i,1,n)cin>>c[i];
      ll ans=1ll*n*n*n;
      Luckily (c + 1, n + 1 + c); 
      Luckily (b + 1, b + n + 1); 
      Luckily (a + 1, a + n + 1); 
      receives (i, 1, c [n]) sum [i] = 0; 
      receives (i, 1, n) receives (j 1, n) ++ sum [a [i] + b [j]]; 
      receives (i, 1, c [n]) sum [i] + = sum [i-1]; 
      receives (i, 1, n) = sum ans- [c [i] -1]; 
      receives (i, 1, b [n]) sum [i] = 0; 
      receives (i, 1, n) receives (j 1, n) ++ sum [a [i] + c [j]]; 
      receives (i, 1, b [n]) sum [i] + = sum [i-1]; 
      receives (i, 1, n) = sum ans- [b [i] -1]; 
      receives (and one in [n]) sum [i] = 0; 
      rep(i,1,n)rep(j,1,n) ++suma[c[i]+b[j]];
      receives (and one in [n]) sum [i] + = sum [i-1]; 
      receives (i, 1, n) = sum ans- [a [i] -1]; 
      printf ( "Case #% d:% lld \ n" Kase ++ years); 
      continue; 
    } 
    Receives (i, 1, n) { 
      cin >> a [i];
      cnta[a[i]].x++;
    }
    rep(i,1,n) {
      cin>>b[i];
      cntb[b[i]].x++;
    }
    rep(i,1,n) {
      cin>>c[i];
      cntc[c[i]].x++;
    }
    fft.transform(cnta,1);fft.transform(cntb,1);fft.transform(cntc,1);
    rep(i,0,fft.len-1){
      resa[i]=cntb[i]*cntc[i];
      resb[i]=cnta[i]*cntc[i];
      resc[i]=cntb[i]*cnta[i];
    }
    fft.transform(resa,-1);fft.transform(resb,-1);fft.transform(resc,-1);
    rep(i,1,fft.len-1){
      suma[i]=suma[i-1]+(ll)(resa[i].x+0.5);
      sumb[i]=sumb[i-1]+(ll)(resb[i].x+0.5);
      sumc[i]=sumc[i-1]+(ll)(resc[i].x+0.5);
    }
    ll ans=n*1ll*n*n;
    rep(i,1,n){
      ans-=suma[a[i]-1]+sumb[b[i]-1]+sumc[c[i]-1];
    }
    printf("Case #%d: %lld\n",kase++,ans);
    rep(i,0,fft.len){
      cnta[i]=cntb[i]=cntc[i]=(cp){0,0};
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/nervendnig/p/11525240.html