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 ;
}

最新文章

  1. 深入理解IIS的多线程工作机制
  2. HTML5的新语义化的标签
  3. 两种文件上传的实现-Ajax和form+iframe
  4. EF框架step by step(8)—Code First DataAnnotations(2)
  5. Unity3D基础学习 加载场景时隐藏物体,点击显示时显示物体
  6. REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件
  7. 玩转Windows服务系列汇总(9篇文章)
  8. 一些Windows API导致的Crash以及使用问题总结
  9. [Cocos2d-x]创建项目
  10. Socket 学习(三)
  11. Delphi 日期时间函数
  12. C++笔记002:VS2010报错:LINK fatal error LNK1123 转换到 COFF 期间失败文件无效或损坏
  13. ubuntu 删除自带软件的方法
  14. 3Ds Max 2014
  15. 一次Java解析数独的经历
  16. WordPress主题开发实例:get_term_by()获取指定分类链接
  17. SpringBoot @Transactional声明事务无效问题
  18. python 连加
  19. Linux下访问window挂载的磁盘
  20. 使用autoconfig

热门文章

  1. nginx配置文件解答
  2. hdu 2181暴搜
  3. ES6__字符串、数组、对象的扩展
  4. Python基础教程笔记——第3章:使用字符串
  5. jxls使用模版导出Excel
  6. 2017icpc乌鲁木齐网络赛Colored Graph (构造)
  7. hdu6110(线段树+lca)
  8. Meteor check
  9. 【effective c++】模板与泛型编程
  10. redis connetced refused remote