SPOJ:Triple Sums(母函数+FFT)
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.
Constraints:
N <= 40000, |si| <= 20000
Input
The first line of input contains a single integer N.
Each of the next N lines contain an element of s.
Output
Print the solution for each possible sum in the following format:
sum_value : number_of_triples
Smaller sum values should be printed first.
Example
Input: 5
-1
2
3
0
5
Output:
1 : 1
2 : 1
4 : 2
5 : 1
6 : 1
7 : 2
8 : 1
10 : 1
Explanation:
4 can be obtained using triples ( 0, 1, 2 ) and ( 0, 3, 4 ).
7 can be obtained using triples ( 0, 2, 4 ) and ( 1, 3, 4 ).
Note: a triple is considered the same as any of its permutations.
2018/4/4,因为前几天一直在做LCT,然后基础题都长得差不多,难一点的现在还没有相同,然后搞得有些烦躁。就干脆转移下注意力。
于是学习了下FFT,算是以为数学大渣又跨越了一小步。
-------------------------------------分界线-----------------------------------------------
这题可以先用母函数表示出选一个的方案(系数是物品出现次数,指数是物品价值) A(x)=a*x^1+b*x^2+c*x^3 所以不考虑重复,在物品中选出三个的方案就是 1/6*A(x)^3
现在用 B(x),C(x) 分别表示一种物品选了 2 次和 3 次的方案 B(x)=a*x^2+b*x^4+c*x^6 C(x)=a*x^3+b*x^6+c*x^9 选三个有可能是 AAB, ABA, BAA, AAA 这几种重复情况,所以扣掉后方案就是 [A(x)^3−3*A(x)⋅B(x)+2*C(x)]/6
由于这里用到了多项式乘法,用FFT优化即可
由于x的范围可能为负,所以假设每个数都加20000,使之变为正,最后再减去即可。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
const int maxn=<<;
const double pi=acos(-1.0);
using namespace std;
struct complex{
double r,i;
complex(){};
complex(double rr,double ii):r(rr),i(ii){}
complex friend operator +(complex a,complex b){return (complex){a.r+b.r,a.i+b.i};}
complex friend operator -(complex a,complex b){return (complex){a.r-b.r,a.i-b.i};}
complex friend operator *(complex a,complex b){return (complex){a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r};}
}tmp[maxn];
struct DFT{
complex a[maxn];
void fft(int sz,int bg,int step,int opt){
if(sz==) return; int m=sz>>;
fft(m,bg,step<<,opt); fft(m,bg+step,step<<,opt);
complex w=complex(,),t=complex(cos(2.0*pi/sz),sin(2.0*pi*opt/sz));
for(int k=;k<m;k++)
{
int pos=*step*k;
tmp[k]=a[pos+bg]+w*a[pos+bg+step];
tmp[k+m]=a[pos+bg]-w*a[pos+bg+step];
w=w*t;
}
for(int i=;i!=sz;i++) a[i*step+bg]=tmp[i];
}
}A,B,C;
int a[maxn],b[maxn],c[maxn];
int main(){
int n; scanf("%d",&n);
for(int i=,x;i<n;i++) scanf("%d",&x),a[x+]++,b[*(x+)]++,c[*(x+)]++;
for(int i=;i<maxn;i++) A.a[i].r=a[i], B.a[i].r=b[i], C.a[i].r=c[i];
A.fft(maxn,,,), B.fft(maxn,,,);
for(int i=;i<maxn;i++) C.a[i]=A.a[i]*(A.a[i]*A.a[i]-(complex){3.0,0.0}*B.a[i]);
C.fft(maxn,,,-);
for(int i=;i<maxn;i++){
ll ans=((ll)(C.a[i].r/maxn+0.5)+*c[i])/;
if (ans) printf("%d : %I64d\n",i-,ans);
}
return ;
return ;
}
SPOJ:Triple Sums(母函数+FFT)的更多相关文章
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- 2018.11.18 spoj Triple Sums(容斥原理+fft)
传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai,aj,ak( ...
- SPOJ Triple Sums(FFT+容斥原理)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...
- SPOJ - Triple Sums
[传送门] FFT第一题! 构造多项式 $A(x) = \sum x ^ {s_i}$. 不考虑题目中 $i < j < k$ 的条件,那么 $A^3(x)$ 每一项对应的系数就是答案了. ...
- spoj TSUM - Triple Sums fft+容斥
题目链接 首先忽略 i < j < k这个条件.那么我们构造多项式$$A(x) = \sum_{1现在我们考虑容斥:1. $ (\sum_{}x)^3 = \sum_{}x^3 + 3\s ...
- SPOJ - TSUM 母函数+FFT+容斥
题意:n个数,任取三个加起来,问每个可能的结果的方案数. 题解:构造母函数ABC,比如现在有 1 2 3 三个数.则 其中B表示同一个数加两次,C表示用三次.然后考虑去重. A^3表示可重复地拿三个. ...
- BZOJ.3771.Triple(母函数 FFT 容斥)
题目链接 \(Description\) 有\(n\)个物品(斧头),每个物品价值不同且只有一件,问取出一件.两件.三件物品,所有可能得到的价值和及其方案数.\((a,b),(b,a)\)算作一种方案 ...
- Spoj 8372 Triple Sums
题意:给你n个数字,对于任意s,s满足\(s=u_i+u_j+u_k,i<j<k\),要求出所有的s和对应满足条件的i,j,k的方案数 Solution: 构造一个函数:\(A(x)=\s ...
- UVa12298 Super Poker II(母函数 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 Description I have a set of super poker cards, ...
随机推荐
- php引入PHPMailer发送邮件
昨天做了一个发送邮件的功能,如果直接用mail()函数,需要拥有自己的邮件服务器,所有引入PHPMailer类方便快捷,简单写一下开发步骤: 一.拥有自己的邮箱账号(作为发件人邮箱) 分两种情况: 1 ...
- [codeforces722C]Destroying Array
[codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...
- js删除数组对象中符合条件的数据
var data = [{}, {}, {}, {Id:1}] var datawilldele = [];//2,4,5 data.forEach(function (v, i,arry) { if ...
- 下载数据到Excel,工具类
使用反射将model数据下载到Excel中 package test.upload.utils; import java.lang.reflect.Method; import java.math.B ...
- MySQL注释(转)
MySQL支持3种注释风格: 1.从‘#’字符从行尾. 2.从‘-- ’序列到行尾.请注意‘-- ’(双破折号)注释风格要求第2个破折号后面至少跟一个空格符(例如空格.tab.换行符等等). 3.从/ ...
- Python开发的一个IDE推荐,Sublime Text 3
Sublime Text 3 官网下载地址为, LINK. 目前最新版本是3114. 这里转载泱泱长空的授权文件(注册码)文章[1],将几个可以用的注册码列举如下: 补充:2016.05 最近经过测试 ...
- Go --- 设计模式(工厂模式)
简易工厂主要是用来解决对象“创建”的问题.以下的例子取自<大话设计模式>中第一章,实现一个可扩展的“计算器”.当增加新的功能时,并不需改动原来已经实现的算法.由于是简易工厂,所以我们还是需 ...
- centos7备份还原与grub2引导和rescue模式修改root密码
一.centos7备份1.su -2.cd /3.tar -zpPcvf backup.tgz --exclude=/sys --exclude=/mnt --exclude=/proc --excl ...
- Office EXCEL 的绝对引用和相对引用如何理解
比如C1 = A1+B1,则我把C1的单元格往下拖拉的时候,C2会自动等于A2+B2,C3会自动等于A3+B3,而如果让G1 = $E$1+$F$1,则把G1单元格往下拖拉的时候,G2G3单元格都不会 ...
- poj 1258 Agri-Net(Prim)(基础)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44487 Accepted: 18173 Descri ...