# 【bzoj3771】Triple FFT+容斥原理

4
4
5
6
7

4 1
5 1
6 1
7 1
9 1
10 1
11 2
12 1
13 1
15 1
16 1
17 1
18 1

FFT+容斥原理

#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 1 << 19
#define pi acos(-1)
using namespace std;
typedef long long ll;
struct data
{
double x , y;
data() {x = y = 0;}
data(double x0 , double y0) {x = x0 , y = y0;}
data operator+(const data a)const {return data(x + a.x , y + a.y);}
data operator-(const data a)const {return data(x - a.x , y - a.y);}
data operator*(const data a)const {return data(x * a.x - y * a.y , x * a.y + y * a.x);}
}a[N] , b[N] , c[N] , d[N] , e[N];
int f[N];
void fft(data *a , int n , int flag)
{
int i , j , k;
for(i = k = 0 ; i < n ; i ++ )
{
if(i > k) swap(a[i] , a[k]);
for(j = (n >> 1) ; (k ^= j) < j ; j >>= 1);
}
for(k = 2 ; k <= n ; k <<= 1)
{
data wn(cos(2 * pi * flag / k) , sin(2 * pi * flag / k));
for(i = 0 ; i < n ; i += k)
{
data t , w(1 , 0);
for(j = i ; j < i + (k >> 1) ; j ++ , w = w * wn)
t = w * a[j + (k >> 1)] , a[j + (k >> 1)] = a[j] - t , a[j] = a[j] + t;
}
}
if(flag == -1)
for(i = 0 ; i < n ; i ++ )
a[i].x /= n;
}
int main()
{
int n , i , t , m = 0 , len;
scanf("%d" , &n);
while(n -- ) scanf("%d" , &t) , a[t].x ++ , b[t * 2].x ++ , c[t * 3].x ++ , d[t].x ++ , e[t * 2].x ++ , f[t] ++ ;
m = t * 3;
for(len = 1 ; len < m ; len <<= 1);
fft(a , len , 1) , fft(b , len ,  1);
for(i = 0 ; i < len ; i ++ ) b[i] = b[i] * a[i] , a[i] = a[i] * a[i] * a[i];
fft(a , len , -1) , fft(b , len , -1);
fft(d , len , 1);
for(i = 0 ; i < len ; i ++ ) d[i] = d[i] * d[i];
fft(d , len , -1);
for(i = 1 ; i <= m ; i ++ )
if((ll)(a[i].x - 3 * b[i].x + 2 * c[i].x + 0.5) / 6 + (ll)(d[i].x - e[i].x + 0.5) / 2 + f[i])
printf("%d %lld\n" , i , (ll)(a[i].x - 3 * b[i].x + 2 * c[i].x + 0.5) / 6 + (ll)(d[i].x - e[i].x + 0.5) / 2 + f[i]);
return 0;
}


|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-08-22 20:22  GXZlegend  阅读(468)  评论(0编辑  收藏  举报