标签:== 原理 soft scanf pre 统计 return ons space
题目描述
输入
输出
样例输入
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+容斥原理
一个数的方案数$f(x)$,就是原序列的生成函数(初学时理解为桶 = =)。
两个数的方案数为$(f*f)(x)$,但其中包含了两次使用了同一个数的方案数$g(x)=f(2x)$,而其余的方案统计了两次,所以方案数为$\frac 12(f*f-g)(x)$。
三个数的方案数为$(f*f*f)(x)$,但其中包含了三次使用了同一个数的方案数$h(x)=f(3x)$,包含了使用了两次同一个数,另一个数不同的方案数$(f*g-h)(x)*3$(里面减掉$h$是因为三次使用同一个数的方案数被重复计算,而乘3是因为在$(f*f*f)(x)$中计算了3次),而其余的方案统计了,所以方案数为$\frac 16((f*f*f-3*f*g+2*h)(x))$。
最后数值不为0的就是答案。
#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;
}
标签:== 原理 soft scanf pre 统计 return ons space
原文地址:http://www.cnblogs.com/GXZlegend/p/7413487.html