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. 七牛云 X 英语流利说:教育 3.0 时代的智能突破

    美国当地时间 2018 年 9 月 27 日,国内领先的人工智能驱动的教育科技公司「英语流利说」正式挂牌纽交所,以其独创的教育 3.0 模式,成为中国「AI+ 教育」第一股. 教育 3.0 时代的智能 ...

  2. bzoj1004 [HNOI2008]Cards 置换群+背包

    [bzoj1004][HNOI2008]Cards 2014年5月26日5,3502 Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿 ...

  3. 调用BOS服务保存一个单据的简化示例

    IMetaDataService metadataService = ServiceHelper.GetService<IMetaDataService>(); // 加载元数据 Form ...

  4. Visual Studio Code Edit

    微软的跨平台编辑器~~ 下载地址(官网):https://code.visualstudio.com/ 下载地址(网盘):http://pan.baidu.com/s/1ntLy8Tr 使用技巧: c ...

  5. POJ 2391 floyd二分+拆点+最大流

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20904   Accepted: 4 ...

  6. Jquery操作事件

    1.文档加载事件 2.DOM单击双击事件 3.DOM获得焦点,失去焦点问题 4.DOM鼠标移入,移出事件 <!DOCTYPE html> <html> <head> ...

  7. FIREDAC保存ORACLE的BLOB字段数据

     FIREDAC默认识别ORACLE的BLOB字段为HUGEBLOB,需要将HBLOB映射为BLOB,才可以保存ORACLE的BLOB字段的数据.

  8. 虚拟社会(Virtual Society)

    虚拟社会(Virtual Society),又称赛博社会(Cyber Society),是指不同网民之间经由计算机.远程通讯终端等技术设备相互连接起来以进行信息的共享.互动与交流,并在其中进行社会交往 ...

  9. ios UITableView 获取点击cell对象

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITabl ...

  10. Ulua_toLua_基本案例(一)

    Ulua_toLua_基本案例 在Untiy中用Lua.必需要LuaInterface.LuaInterface的介绍请看:点击打开链接 能够先光写Lua,生成.lua的纯文件.再Unity中通过,l ...