# [BZOJ3771] Triple

a_i<=40000。

## Code

#include<bits/stdc++.h>
using std::min;
using std::max;
using std::swap;
using std::vector;
typedef double db;
typedef long long ll;
#define pb(A) push_back(A)
#define pii std::pair<int,int>
#define all(A) A.begin(),A.end()
#define mp(A,B) std::make_pair(A,B)
const int N=5e5+5;
const db Pi=acos(-1);

int n,lim,rev[N],val[N],s[N];

struct cp{
db x,y;
cp (db a=0,db b=0){x=a,y=b;}
friend cp operator+(cp a,cp b){return cp(a.x+b.x,a.y+b.y);}
friend cp operator-(cp a,cp b){return cp(a.x-b.x,a.y-b.y);}
friend cp operator*(cp a,cp b){return cp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
}f[N],f2[N],f3[N],h[N],g[N],hf[N];

int getint(){
int X=0,w=0;char ch=getchar();
while(!isdigit(ch))w|=ch=='-',ch=getchar();
while( isdigit(ch))X=X*10+ch-48,ch=getchar();
if(w) return -X;return X;
}

void fft(cp *f,int opt){
for(int i=0;i<lim;i++) if(i<rev[i]) swap(f[i],f[rev[i]]);
for(int mid=1;mid<lim;mid<<=1){
cp tmp=cp(cos(Pi/mid),sin(Pi/mid)*opt);
for(int R=mid<<1,l=0;l<lim;l+=R){
cp w=cp(1,0);
for(int k=0;k<mid;k++,w=w*tmp){
cp x=f[l+k],y=f[l+k+mid]*w;
f[l+k]=x+y,f[l+k+mid]=x-y;
}
}
}
}

signed main(){
n=getint();int mx=0;
for(int i=1;i<=n;i++) val[i]=getint(),s[val[i]]++,mx=max(mx,val[i]),f[val[i]].x++,h[val[i]*2].x++,g[val[i]*3].x++;
lim=1;while(lim<=4*mx) lim<<=1;
for(int i=1;i<lim;i++) rev[i]=(rev[i>>1]>>1)|(i&1?lim>>1:0);
fft(f,1),fft(h,1);
for(int i=0;i<lim;i++) hf[i]=h[i]*f[i],f2[i]=f[i]*f[i],f3[i]=f[i]*f[i]*f[i];
fft(hf,-1),fft(f,-1),fft(f2,-1),fft(f3,-1),fft(h,-1);
for(int i=0;i<lim;i++){
int x=(int)(f[i].x/lim+0.5)+((int)(f2[i].x/lim+0.5)-(int)(h[i].x/lim+0.5))/2+((int)(f3[i].x/lim+0.5)-(int)(hf[i].x/lim+0.5)*3+g[i].x*2)/6;
if(x) printf("%d %d\n",i,x);
} return 0;
}



posted @ 2018-12-12 09:41  YoungNeal  阅读(177)  评论(0编辑  收藏  举报