【bzoj3771】Triple FFT+容斥原理

题目描述

樵夫的每一把斧头都有一个价值,不同斧头的价值不同。总损失就是丢掉的斧头价值和。

他想对于每个可能的总损失,计算有几种可能的方案。

注意:如果水神拿走了两把斧头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



题解

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;
}
时间: 2025-01-13 13:32:32

【bzoj3771】Triple FFT+容斥原理的相关文章

bzoj3771 Triple

3771: Triple Time Limit: 20 Sec  Memory Limit: 64 MB Submit: 313  Solved: 174 [Submit][Status][Discuss] Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: "这把斧头,是不是你的?" 樵夫一看:"是啊是啊!" 水神把斧头扔在一边,又拿起一个东西问: "这把斧头,是不是你的?&q

SPOJ Triple Sums(FFT+容斥原理)

# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <complex> # include <set> # includ

【BZOJ 3771】 3771: Triple (FFT+容斥)

3771: Triple Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 547  Solved: 307 Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: "这把斧头,是不是你的?" 樵夫一看:"是啊是啊!" 水神把斧头扔在一边,又拿起一个东西问: "这把斧头,是不是你的?" 樵夫看不清楚,但又怕真的是自己的斧头,只好又

【BZOJ3771】Triple 生成函数+FFT

[BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看:“是啊是啊!” 水神把斧头扔在一边,又拿起一个东西问: “这把斧头,是不是你的?” 樵夫看不清楚,但又怕真的是自己的斧头,只好又答:“是啊是啊!” 水神又把手上的东西扔在一边,拿起第三个东西问: “这把斧头,是不是你的?” 樵夫还是看不清楚,但是他觉得再这样下去他就没法砍柴了. 于是他又一次答:“

SPOJ - TSUM Triple Sums FFT+容斥

Triple Sums You're given a sequence s of N distinct integers.Consider all the possible sums of three integers from the sequence at three different indicies.For each obtainable sum output the number of different triples of indicies that generate it.Co

BZOJ 3771: Triple [快速傅里叶变换 生成函数 容斥原理]

题意:n个物品,可以用1/2/3个不同的物品组成不同的价值,求每种价值有多少种方案(顺序不同算一种) 挖坑过会再写 生成函数系数为方案数,次数为价值 A(x) 选一个 B(x) A每项平方 选两个 C(x) A每项三次方 选三个 然后容斥原理算答案 注意计算的时候可以一直用点值,最后在变系数表示 #include <iostream> #include <cstdio> #include <string> #include <algorithm> #incl

SPOJ TSUM Triple Sums(FFT + 容斥)

题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct integers.Consider all the possible sums of three integers from the sequence at three different indicies.For each obtainable sum output the number of diff

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/t/41400 题意: 给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得 \[ \begin{array}{l}{\left|A_{i}-B_{j}\right| \leq C_{k}, \text { and }} \\ {\left|B_{j}-C_{k}\right| \leq

BZOJ 3771 Triple 母函数+FFT

题目大意:给定n个物品,可以用一个/两个/三个不同的物品凑出不同的价值,求每种价值有多少种拼凑方案(顺序不同算一种) 首先搞出这n个物品的母函数a 将a的每项的平方求和得到多项式b 将a的每项的立方求和得到多项式c 那么如果不考虑顺序和重复 那么方案数就是a+b+c 现在考虑顺序和重复后 三个物品的方案数为(a^3-3*a*b+2*c)/6 两个物品的方案数为(a^2-b)/2 一个物品的方案数为a 故最终答案为(a^3-3*a*b+2*c)/6+(a^2-b)/2+a 用FFT搞一下就好了-