# BZOJ3771: Triple【生成函数】

## Description

“这把斧头，是不是你的？”

“这把斧头，是不是你的？”

“这把斧头，是不是你的？”

“你看看你现在的样子，真是丑陋！”

4
4
5
6
7

## Sample Output

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

11有两种方案是4+7和5+6，其他损失值都有唯一方案，例如4=4,5=5,10=4+6,18=5+6+7.

## 思路

#include<bits/stdc++.h>

using namespace std;

const int N = 3e5 + 10;
const double eps = 1e-6;
const double PI = acos(-1);

typedef complex<double> Complex;

Complex f[N], g[N], h[N], p[N], q[N], w[N][2];

void init() {
for (int i = 1; i < (1 << 18); i <<= 1) {
w[i][0] = w[i][1] = Complex(1, 0);
Complex wn(cos(PI / i), sin(PI / i));
for (int j = 1; j < i; j++)
w[i + j][1] = w[i + j - 1][1] * wn;
wn = Complex(cos(PI / i), -sin(PI / i));
for (int j = 1; j < i; j++)
w[i + j][0] = w[i + j - 1][0] * wn;
}
}

void transform(Complex t[N], int len, int typ) {
for (int i = 0, j = 0, k; j < len; j++) {
if (i > j) swap(t[i], t[j]);
for (k = (len >> 1); k & i; k >>= 1) i ^= k;
i ^= k;
}
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += (i << 1)) {
for (int k = 0; k < i; k++) {
Complex x = t[j + k], y = t[j + k + i] * w[i + k][typ];
t[j + k] = x + y;
t[j + k + i] = x - y;
}
}
}
if (typ) return;
for (int i = 0; i < len; i++)
t[i] = Complex(t[i].real() / (double) len, t[i].imag());
}

int n, a[N];

int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
init();
int maxval = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
maxval = max(maxval, a[i]);
}
maxval = maxval * 3 + 1;
for (int i = 1; i <= n; i++) {
f[a[i]] = Complex(f[a[i]].real() + 1, f[a[i]].imag());
p[a[i] * 2] = Complex(p[a[i] * 2].real() + 1, p[a[i] * 2].imag());
q[a[i] * 3] = Complex(q[a[i] * 3].real() + 1, q[a[i] * 3].imag());
}
int len = 1 << (int) ceil(log2(maxval * 2 - 1));
transform(f, len, 1);
for (int i = 0; i < len; i++)
g[i] = f[i] * f[i];
transform(g, len, 0);
for (int i = 0; i < len; i++)
g[i] -= p[i];
transform(g, len, 1);
for (int i = 0; i < len; i++)
h[i] = f[i] * f[i] * f[i];
transform(h, len, 0);
transform(p, len, 1);
for (int i = 0; i < len; i++)
p[i] = f[i] * p[i];
transform(g, len, 0);
transform(f, len, 0);
transform(p, len, 0);
for (int i = 0; i <= len; i++) {
h[i] -= p[i] + p[i] + p[i];
h[i] += q[i] + q[i];
}
for (int i = 0; i <= maxval; i++)
if ((int) (round(f[i].real()) + round(g[i].real()) / 2 + round(h[i].real()) / 6))
printf("%d %d\n", i, (int) (round(f[i].real()) + round(g[i].real()) / 2 + round(h[i].real()) / 6));
return 0;
}

posted @ 2019-01-08 19:48  Dream_maker_yk  阅读(237)  评论(0编辑  收藏  举报