Kattis aplusb A+B Problem FFT

A+B Problem

Given NN integers in the range [−50000,50000][−50000,50000], how many ways are there to pick three integers aiai, ajaj, akak, such that ii, jj, kk are pairwise distinct and ai+aj=ak? Two ways are different if their ordered triples (i,j,k)(i,j,k) of indices are different.

Input

The first line of input consists of a single integer NN (1≤N≤2000001≤N≤200000). The next line consists of NN space-separated integers a1,a2,…,aNa1,a2,…,aN.

Output

Output an integer representing the number of ways.

Sample Input 1 Sample Output 1
4
1 2 3 4
4
Sample Input 2 Sample Output 2
6
1 1 3 3 4 6
10

 

求 ai+aj=ak 的个数,可以转换为多项式的次数(乘法是指数相加

统计数字个数作为系数,因为有负数,所以整体+50000,注意进行多项式乘法后,变为+100000,因为是两数相加/xyx

注意0的处理:
            //不同0之间相加是不应该去掉的    //不需处理
            //相同数不同位置+0是不应该去掉的    //不需处理
            //相同0之间的加法应该去掉    //统一去掉
            //本身+0=本身应该去掉//本身也包括0的可能所以zero*(N-1)    //单独去掉

细节真有趣我嘤我哭搞不清楚的时候清晰地分类列一下要

 

#include 
#include 
#include 
#include 
using namespace std;

typedef long long LL;
const double PI=acos(-1.0);
const int maxn=1<<18+5;

struct Complex
{
	double re,im;
	Complex(double r=0.0,double i=0.0) {re=r,im=i;}
	void print() {printf("%lf %lf\n",re,im);}
};
Complex operator +(const Complex&A,const Complex&B) {return Complex(A.re+B.re,A.im+B.im);}
Complex operator -(const Complex&A,const Complex&B) {return Complex(A.re-B.re,A.im-B.im);}
Complex operator *(const Complex&A,const Complex&B) {return Complex(A.re*B.re-A.im*B.im,A.re*B.im+A.im*B.re);}

Complex a[maxn],W[2][maxn];

int n,N,rev[maxn],cnt[maxn],x[maxn],z;
//bool vst[maxn];//并不需要!
LL ans;

void prework()
{
	n=1<<18;
	for(int i=0;i>=1,k<<=1)
			(y<<=1)|=x&1;
		rev[i]=y;
	}
	for(int i=0;i

 

你可能感兴趣的:(ACM,FFT)