大致题意
给3个长度为n的数组,(1<=n<=100000, 1<=ai,bi,ci<=100000) 求每个数组选择一个数凑成广义三角形的个数。(两边之和可以等于第3边)
注:n大于1000的样例有20组
思路
跟HDU4609思路类似,不过这里有3个数组。分别枚举每个数组中的值作为最长边,另外两个数组做FFT,求个前缀和,累计sum[a[i]-1]的数量作为不满足三角形的,然后最终用nnn剪掉。这种容斥方法的正确性可以自己证明一下。
但是这样会T… 因为题目只有20组样例的n大于1000,所以之前的小数据可以直接暴力求出两个数组组合的个数,n^2的复杂度,但是FFT的复杂度是与最大数值相关的,所以即使n很小,但是ai很大,FFT复杂度还是很高。
代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 1000006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define PI acos(-1)
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int T,n;
struct Complex
{
double x, y; // 实部和虚部 x + yi
Complex(double _x = 0.0, double _y = 0.0){x = _x;y = _y;}
Complex operator - (const Complex &b) const{return Complex(x - b.x, y - b.y);}
Complex operator + (const Complex &b) const{return Complex(x + b.x, y + b.y);}
Complex operator * (const Complex &b) const{return Complex(x * b.x - y * b.y, x * b.y + y * b.x);}
};
void change(Complex y[], int len)
{
int i,j,k;
for(i=1,j=len/2;i<len-1;i++){
if(i<j)swap(y[i],y[j]);
k=len/2;
while (j>=k){j-=k;k/=2;}
if (j<k)j+=k;
}
return ;
}
void fft(Complex y[], int len, int on)
{
change(y,len);
for(int h=2;h<=len;h<<=1){
Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j=0;j<len;j+=h){
Complex w(1,0);
for(int k=j;k<j+h/2;k++){
Complex u=y[k];
Complex t=w*y[k+h/2];
y[k]=u+t;
y[k+h/2]=u-t;
w=w*wn;
}
}
}
if(on==-1)for(int i=0;i<len;i++)y[i].x/=len;
}
ll cnt1[maxn],cnt2[maxn],cnt3[maxn],sum[maxn],cnt[maxn];
int a[maxn],b[maxn],c[maxn],ma1,ma2,ma3;
Complex v1[maxn],v2[maxn];
int main()
{
T=read();
int kase=0;
while(T--){
n=read();
if(n<=1000){
ll ans=0;
int ma=-1;
inc(i,1,n)a[i]=read(),ma=max(ma,a[i]);
inc(i,1,n)b[i]=read(),ma=max(ma,b[i]);
inc(i,1,n)c[i]=read(),ma=max(ma,c[i]);
inc(i,1,ma)sum[i]=0;
inc(i,1,n)inc(j,1,n)sum[a[i]+b[j]]++;
inc(i,1,ma)sum[i]+=sum[i-1];
ll ans1=0;
inc(i,1,n)ans1+=sum[c[i]-1];
inc(i,1,ma)sum[i]=0;
inc(i,1,n)inc(j,1,n)sum[a[i]+c[j]]++;
inc(i,1,ma)sum[i]+=sum[i-1];
ll ans2=0;
inc(i,1,n)ans2+=sum[b[i]-1];
inc(i,1,ma)sum[i]=0;
inc(i,1,n)inc(j,1,n)sum[b[i]+c[j]]++;
inc(i,1,ma)sum[i]+=sum[i-1];
ll ans3=0;
inc(i,1,n)ans3+=sum[a[i]-1];
ans=1ll*n*n*n-ans1-ans2-ans3;
printf("Case #%d: %lld\n",++kase,ans);
continue;
}
int ma=-1,x;
ma1=ma2=ma3=0;
inc(i,1,n){
//x=read();
scanf("%d",&x);
ma1=max(ma1,x);
cnt1[x]++;
a[i]=x;
}
inc(i,1,n){
scanf("%d",&x);
ma2=max(ma2,x);
cnt2[x]++;
b[i]=x;
}
inc(i,1,n){
//c[i]=read();
scanf("%d",&c[i]);
ma3=max(ma3,c[i]);
cnt3[c[i]]++;
}
ma=max(ma1,max(ma2,ma3));
int len=1;//ma=max(ma1,ma2);
while(len<2*ma)len<<=1;
inc(i,0,ma)v1[i]=Complex(cnt1[i],0);
inc(i,ma+1,len-1)v1[i]=Complex(0,0);
inc(i,0,ma)v2[i]=Complex(cnt2[i],0);
inc(i,ma+1,len-1)v2[i]=Complex(0,0);
fft(v1,len,1);fft(v2,len,1);
inc(i,0,len-1)v1[i]=v1[i]*v2[i];
fft(v1,len,-1);
inc(i,0,2*ma)cnt[i]=(ll)(v1[i].x+0.5);
inc(i,1,2*ma)sum[i]=sum[i-1]+cnt[i];
ll ans1=0;
inc(i,1,n)ans1+=sum[c[i]-1];
len=1;//ma=max(ma1,ma3);
while(len<2*ma)len<<=1;
inc(i,0,ma)v1[i]=Complex(cnt1[i],0);
inc(i,ma+1,len-1)v1[i]=Complex(0,0);
inc(i,0,ma)v2[i]=Complex(cnt3[i],0);
inc(i,ma+1,len-1)v2[i]=Complex(0,0);
fft(v1,len,1);fft(v2,len,1);
inc(i,0,len-1)v1[i]=v1[i]*v2[i];
fft(v1,len,-1);
inc(i,0,2*ma)cnt[i]=(ll)(v1[i].x+0.5);
inc(i,1,2*ma)sum[i]=sum[i-1]+cnt[i];
ll ans2=0;
inc(i,1,n)ans2+=sum[b[i]-1];
len=1;//ma=max(ma3,ma2);
while(len<2*ma)len<<=1;
inc(i,0,ma)v1[i]=Complex(cnt2[i],0);
inc(i,ma+1,len-1)v1[i]=Complex(0,0);
inc(i,0,ma)v2[i]=Complex(cnt3[i],0);
inc(i,ma+1,len-1)v2[i]=Complex(0,0);
fft(v1,len,1);fft(v2,len,1);
inc(i,0,len-1)v1[i]=v1[i]*v2[i];
fft(v1,len,-1);
inc(i,0,2*ma)cnt[i]=(ll)(v1[i].x+0.5);
inc(i,1,2*ma)sum[i]=sum[i-1]+cnt[i];
ll ans3=0;
inc(i,1,n)ans3+=sum[a[i]-1];
ll ans=n*1ll*n*n-ans1-ans2-ans3;
printf("Case #%d: %lld\n",++kase,ans);
inc(i,0,ma1)cnt1[i]=0;
inc(i,0,ma2)cnt2[i]=0;
inc(i,0,ma3)cnt3[i]=0;
}
return 0;
}