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)的更多相关文章

  1. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  2. 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​( ...

  3. SPOJ Triple Sums(FFT+容斥原理)

    # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...

  4. SPOJ - Triple Sums

    [传送门] FFT第一题! 构造多项式 $A(x) = \sum x ^ {s_i}$. 不考虑题目中 $i < j < k$ 的条件,那么 $A^3(x)$ 每一项对应的系数就是答案了. ...

  5. spoj TSUM - Triple Sums fft+容斥

    题目链接 首先忽略 i < j < k这个条件.那么我们构造多项式$$A(x) = \sum_{1现在我们考虑容斥:1. $ (\sum_{}x)^3 = \sum_{}x^3 + 3\s ...

  6. SPOJ - TSUM 母函数+FFT+容斥

    题意:n个数,任取三个加起来,问每个可能的结果的方案数. 题解:构造母函数ABC,比如现在有 1 2 3 三个数.则 其中B表示同一个数加两次,C表示用三次.然后考虑去重. A^3表示可重复地拿三个. ...

  7. BZOJ.3771.Triple(母函数 FFT 容斥)

    题目链接 \(Description\) 有\(n\)个物品(斧头),每个物品价值不同且只有一件,问取出一件.两件.三件物品,所有可能得到的价值和及其方案数.\((a,b),(b,a)\)算作一种方案 ...

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

  9. UVa12298 Super Poker II(母函数 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 Description I have a set of super poker cards, ...

随机推荐

  1. zoj 2947 Abbreviation

    Abbreviation Time Limit: 2 Seconds      Memory Limit: 65536 KB When a Little White meets another Lit ...

  2. javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法

    下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...

  3. POJ 1379 (随机算法)模拟退火

    题目大意: 给定一堆点,找到一个点的位置使这个点到所有点中的最小距离最大 这里数据范围很小,精度要求也不高,我们这里可以利用模拟退火的方法,随机找到下一个点,如果下一个点比当前点优秀就更新当前点 参考 ...

  4. [转]SQL SERVER数据库还原的方法

    SQL SERVER数据库还原的方法 在SQL SERVER 2005下还原数据库 1.新建数据库A,右键还原数据库,此时目标数据库为A,选择备份 文件B_db_201311040200.BAK,还原 ...

  5. js9:设置cookie,读取cookie,删除cookie,保存cookie时间,String,Date对象

    原文发布时间为:2008-11-11 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  6. 用DW制作简单的浮动广告

    原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] 浮动广告可以用层和时间轴结合做出,先选择你的dreamweaver“窗口”,然后从“窗口”菜单中选择“时间轴”,时 ...

  7. ubuntu,CentOS永久修改主机名

    1.查看主机名 在Ubuntu系统中,快速查看主机名有多种方法: 其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后: 其二,在终端窗口中输入命令:hostna ...

  8. HDU 6397 组合数学+容斥 母函数

    Character Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  9. Ultra-QuickSort--POJ2299(归并排序求逆序数对)

    http://poj.org/problem?id=2299 归并排序:比如现在有一个序列[l,r),我们可以把这个序列分成两个序列[l,mid),[mid,r),利用递归按照上 述方法逐步缩小序列, ...

  10. JAVA_构造方法

    构造方法: 作用:是给对象的数据进行初始化用的. 特点:1 必须和类的名字一样. 2 和真正的方法是有所不同的,构造方法是没有返回值的类型的 eg:   方法:public  void  User ( ...