# BZOJ 3771: Triple(生成函数 FFT)

Time Limit: 20 Sec  Memory Limit: 64 MB
Submit: 911  Solved: 528
[Submit][Status][Discuss]

## 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.

## Source

$C(x) = x ^ i$表示价值为$i$的拿了三把的方案数

$(x, y, y)$有三种组合排列方式，所以要乘$3$，但$(x, x, x)$只有一种排列方式，所以最终统计答案时要加上$2 * C(x)$

$A + \frac{A * A - B}{2!} + \frac{A * A * A - 3 * A * B + 2C}{3!}$

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
//#include<iostream>
const double pi = acos(-1);
using namespace std;
const int MAXN = 150000;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {x = xx, y = yy;}
complex operator + (const complex &rhs) {
return complex(x + rhs.x, y + rhs.y);
}
complex operator - (const complex &rhs) {
return complex(x - rhs.x, y - rhs.y);
}
complex operator * (const complex &rhs) {
return complex(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x);
}
complex operator * (const double &rhs) {
return complex(x * rhs, y * rhs);
}
complex operator / (const double &rhs) {
return complex(x / rhs, y / rhs);
}
}A[MAXN], B[MAXN], C[MAXN];
int val, n, N, L, len, r[MAXN];
void FFT(complex *A, int type) {
for(int i = 0; i < N; i++) if(i < r[i]) swap(A[i], A[r[i]]);
for(int mid = 1; mid < N; mid <<= 1) {
complex Wn(cos(pi / mid), type * sin(pi / mid));
for(int j = 0; j < N; j += (mid << 1)) {
complex w = complex(1, 0);
for(int i = 0; i < mid; i++, w = w * Wn) {
complex x = A[j + i], y = w * A[j + i + mid];
A[j + i] = x + y;
A[j + i + mid] = x - y;
}
}
}
if(type == -1) {
for(int i = 0; i < N; i++)
A[i].x /= N;
}
}
void print(complex *a) {
for(int i = 0; i < N; i++)
printf("%d %lf %lf\n", i, a[i].x, a[i].y);
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
freopen("b.out", "w", stdout);
#endif
for(int i = 1; i <= n; i++)
A[val].x = 1,
B[2 * val].x = 1,
C[3 * val].x = 1,
len = max(3 * val, len);
len = len + 1;//tag
for(N = 1; N <= len; N <<= 1, L++);
for(int i = 0; i < N; i++)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (L - 1));

FFT(C, 1);
FFT(A, 1);
FFT(B, 1);
for(int i = 0; i < N; i++)
A[i] = A[i] + (A[i] * A[i] - B[i]) / 2.0 + (A[i] * A[i] * A[i] - A[i] * B[i] * 3.0 + C[i] * 2.0) / 6.0;
FFT(A, -1);

for(int i = 0; i < N; i++) {
long long cur = (long long )(A[i].x + 0.5);
if(cur)
printf("%d %lld\n", i, cur);
}
return 0;
}

posted @ 2018-06-07 21:41  自为风月马前卒  阅读(307)  评论(0编辑  收藏  举报