题目链接
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23842
题目大意
给出nn个数字a1,a2,a3...an
a1,a2,a3...an,对于所有存在的SS,求和为SS的三个数ai,aj,ak
ai,aj,ak的有序对(i,j,k)
(i,j,k) (i<j<k)(i<j<k)的个数。
思路
http://blog.csdn.net/qpswwww/article/details/44228381
————————————————————————————————
get new skill
在容斥的时候,如果三个多项式中两个多项式是绑定的(选两个重复的)
相当于两个多项式相加,也就是*2,同样三个多项式相同,乘以3
然后再进行卷积
————————————————————————————————
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <math.h>
#include <memory.h>
#include <bits/stdc++.h>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const LL inf = 0x3f3f3f3f;
const LL MOD =100000000LL;
const int N = 350010;
const double eps = 1e-8;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
const double PI = acos(-1.0);
//复数结构体
struct Complex{
double r,i;
Complex(double _r = 0.0,double _i = 0.0){
r = _r; i = _i;
}
Complex operator +(const Complex &b){
return Complex(r+b.r,i+b.i);
}
Complex operator -(const Complex &b){
return Complex(r-b.r,i-b.i);
}
Complex operator *(const Complex &b){
return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
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]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len/2;
while( j >= k){
j -= k;
k /= 2;
}
if(j < k) j += k;
}
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT,on==-1时是IDFT
*/
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].r /= len;
}
Complex a[N],b[N];
int x1[N];
int x2[N];
int x3[N];
int main()
{
int n,x;
scanf("%d",&n);
memset(x1,0,sizeof(x1));
memset(x2,0,sizeof(x2));
memset(x3,0,sizeof(x3));
for(int i=0;i<n;i++)
{
scanf("%d",&x);
x+=20000;
x1[x]++;
x2[x*2]++;
x3[x*3]++;
}
int len=1<<17;
for(int i=0;i<len;i++)
{
a[i]=Complex(x1[i],0);
b[i]=Complex(x2[i],0);
}
fft(a,len,1);
fft(b,len,1);
for(int i=0;i<len;i++)
{
a[i]=a[i]*a[i]*a[i]-Complex(3,0)*b[i]*a[i];
}
fft(a,len,-1);
for(int i=0;i<len;i++)
{
x3[i]+=x3[i];
x3[i]+=(int)(a[i].r+0.5);
x3[i]/=6;
}
for(int i=0;i<len;i++)
{
if(x3[i]!=0)
printf("%d : %d\n",i-60000,x3[i]);
}
}