[BZOJ3771]Triple

[BZOJ3771]Triple

试题描述

我们讲一个悲伤的故事。
从前有一个贫穷的樵夫在河边砍柴。
这时候河里出现了一个水神,夺过了他的斧头,说:
“这把斧头,是不是你的?”
樵夫一看:“是啊是啊!”
水神把斧头扔在一边,又拿起一个东西问:
“这把斧头,是不是你的?”
樵夫看不清楚,但又怕真的是自己的斧头,只好又答:“是啊是啊!”
水神又把手上的东西扔在一边,拿起第三个东西问:
“这把斧头,是不是你的?”
樵夫还是看不清楚,但是他觉得再这样下去他就没法砍柴了。
于是他又一次答:“是啊是啊!真的是!”
水神看着他,哈哈大笑道:
“你看看你现在的样子,真是丑陋!”
之后就消失了。
 
樵夫觉得很坑爹,他今天不仅没有砍到柴,还丢了一把斧头给那个水神。
于是他准备回家换一把斧头。
回家之后他才发现真正坑爹的事情才刚开始。
水神拿着的的确是他的斧头。
但是不一定是他拿出去的那把,还有可能是水神不知道怎么偷偷从他家里拿走的。
换句话说,水神可能拿走了他的一把,两把或者三把斧头。
 
樵夫觉得今天真是倒霉透了,但不管怎么样日子还得过。
他想统计他的损失。
樵夫的每一把斧头都有一个价值,不同斧头的价值不同。总损失就是丢掉的斧头价值和。
他想对于每个可能的总损失,计算有几种可能的方案。
注意:如果水神拿走了两把斧头a和b,(a,b)和(b,a)视为一种方案。拿走三把斧头时,(a,b,c),(b,c,a),(c,a,b),(c,b,a),(b,a,c),(a,c,b)视为一种方案。

输入

第一行是整数N,表示有N把斧头。
接下来n行升序输入N个数字Ai,表示每把斧头的价值。

输出

若干行,按升序对于所有可能的总损失输出一行x y,x为损失值,y为方案数。

输入示例

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

数据规模及约定

所有数据满足:Ai<=40000

题解

这题用了一个叫母函数的东西。其实就是搞一个多项式 A,其中第 xi 的系数为数字 i 的出现次数,那么容易发现两个 A 相乘可以得到两个数加和组成的每一个数不考虑顺序和重复的方法,具体来说设两个数加起来和为 n 的方案为 Bn ,数 n 的出现次数为 An 则有,容易发现这是个卷积的形式,所以是两个多项式 A 相乘。注意需要排除重复和顺序不当的情况,所以还需要设置一个多项式 A2,其中第 xi 表示 i/2 的出现次数,那么 (A*A-A2)/2 就是两个数加起来考虑顺序和重复的方案数了。同理三个数的也可以自己yy一下推出来。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
 
const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = Getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
    return x * f;
}
 
#define maxn 240010
#define LL long long
const double pi = acos(-1.0);
struct Complex {
    double a, b;
    Complex() { a = b = 0.0; }
    Complex operator + (const Complex& t) const {
        Complex ans;
        ans.a = a + t.a;
        ans.b = b + t.b;
        return ans;
    }
    Complex operator += (const Complex& t) {
        *this = *this + t;
        return *this;
    }
    Complex operator - (const Complex& t) const {
        Complex ans;
        ans.a = a - t.a;
        ans.b = b - t.b;
        return ans;
    }
    Complex operator * (const Complex& t) const {
        Complex ans;
        ans.a = a * t.a - b * t.b;
        ans.b = a * t.b + b * t.a;
        return ans;
    }
    Complex operator * (const double& t) const {
        Complex ans;
        ans.a = a * t;
        ans.b = b * t;
        return ans;
    }
    Complex operator *= (const Complex& t) {
        *this = *this * t;
        return *this;
    }
} A[maxn], A2[maxn], A3[maxn], ans[maxn];
int n, m;
 
int Ord[maxn];
void FFT(Complex a[], int n, int tp) {
    for(int i = 0; i < n; i++) if(i < Ord[i]) swap(a[i], a[Ord[i]]);
    for(int i = 1; i < n; i <<= 1) {
        Complex w, wn; wn.a = cos(pi / i); wn.b = sin(pi / i) * tp;
        for(int j = 0; j < n; j += (i << 1)) {
            w.a = 1.0; w.b = 0.0;
            for(int k = 0; k < i; k++) {
                Complex t1 = a[j+k], t2 = w * a[j+k+i];
                a[j+k] = t1 + t2;
                a[j+k+i] = t1 - t2;
                w *= wn;
            }
        }
    }
    if(tp < 0) for(int i = 0; i <= n; i++) a[i].a = (int)(a[i].a / n + .5);
    return ;
}
 
int main() {
    int t = read();
    for(int i = 1; i <= t; i++) {
        int x = read();
        A[x].a += 1.0; A2[x<<1].a += 1.0; A3[x*3].a += 1.0;
        n = max(n, x);
    }
     
    m = n * 3; int L = 0;
    for(n = 1; n <= m; n <<= 1) L++;
    for(int i = 0; i < n; i++) Ord[i] = (Ord[i>>1] >> 1) | ((i & 1) << L - 1);
    FFT(A, n, 1); FFT(A2, n, 1); FFT(A3, n, 1);
    for(int i = 0; i <= n; i++) ans[i] = A[i] + (A[i] * A[i] - A2[i]) * .5 + (A[i] * A[i] * A[i] - A[i] * A2[i] * 3.0 + A3[i] * 2.0) * (1.0 / 6.0);
    FFT(ans, n, -1);
     
    for(int i = 1; i <= m; i++) if((int)ans[i].a)
        printf("%d %d\n", i, (int)ans[i].a);
     
    return 0;
}

 

posted @ 2016-08-05 09:27  xjr01  阅读(314)  评论(0编辑  收藏  举报