第一道FFT的题目。

在网上找了很多FFT的资料,但一直都看不懂,最后是看算法导论学的FFT,算法导论上面写的很详细,每一步推导过程都有严格的证明。

下面说这道题

题意:

给一个序列s,有n个不互相同的整数。现在从这个序列中选出一个包含3个不同的整数的集合,对于他们的和为sum来说,求一共有多少种选法。(注意:3个数的先后顺序都看做一种选法)

分析:

构造一个多项式A(x),这n个数作为多项式的指数。

A3(x)中的每一项的指数对应三个数的和,前面的系数是取数的方案数。

然而这并不是题目所求,这样的选法是任意取三个数,可能相同可能不同。

其中多计算了不合法的方案:

任意取三个数的方案数 = 取三个相同的数 + 取两个相同的数和另一个不同的数 + 三个互不相同的数

用式子表达出来就是: (图片来自叉姐PPT)

整理一下,答案就是:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
using namespace std; typedef long long LL;
const double PI = acos(-1.0);
typedef complex<double> Complex; const int maxn = ( << ); void FFT(Complex P[], int n, int oper)
{
for(int i = , j = ; i < n - ; i++)
{
for(int s = n; j ^= s >>= , ~j & s; );
if(i < j) swap(P[i], P[j]);
} int log = ;
while((n & ( << log)) == ) log++;
for(int s = ; s < log; s++)
{
int m = ( << s);
int m2 = m * ;
Complex wm = Complex(cos(PI / m), sin(PI / m) * oper);
for(int k = ; k < n; k += m2)
{
Complex w(, );
for(int j = ; j < m; j++, w = w * wm)
{
Complex t = w * P[k + j + m];
Complex u = P[k + j];
P[k + j] = u + t;
P[k + j + m] = u - t;
}
}
} if(oper == -) for(int i = ; i < n; i++) P[i].real() /= n;
} int A[maxn], A2[maxn], A3[maxn];
Complex a[maxn], b[maxn]; int main()
{
int n; scanf("%d", &n);
while(n--)
{
int x; scanf("%d", &x);
x += ;
A[x]++;
A2[x*]++;
A3[x*]++;
}
for(int i = ; i < maxn; i++) a[i] = A[i], b[i] = A2[i]; FFT(a, maxn, );
FFT(b, maxn, );
for(int i = ; i < maxn; i++) a[i] = a[i] * (a[i] * a[i] - b[i] * 3.0);
FFT(a, maxn, -); for(int i = ; i < maxn; i++)
{
LL ans = (LL)((a[i].real() + 0.5) + A3[i] * ) / ;
if(ans > ) printf("%d : %lld\n", i - , ans);
} return ;
}

代码君

SPOJ FFT TSUM的更多相关文章

  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. spoj TSUM - Triple Sums fft+容斥

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

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

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

  4. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  5. spoj VFMUL FFT快速傅立叶变换模板题

    题意:求两个数相乘. 第一次写非递归的fft,因为一个数组开小了调了两天TAT. #include<iostream> #include<cstring> #include&l ...

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

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

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

  8. SPOJ:Triple Sums(母函数+FFT)

    You're given a sequence s of N distinct integers.Consider all the possible sums of three integers fr ...

  9. SPOJ MAXMATCH - Maximum Self-Matching (FFT)

    题目链接:MAXMATCH - Maximum Self-Matching Description You're given a string s consisting of letters 'a', ...

随机推荐

  1. Java程序运行参数

    Java主函数形式:public static void main(String[] args){......} 也就是说可以向Java程序传递一个String[]. 1.在IDEA中debug.ru ...

  2. 为什么HDFS的副本数通常选择3?

    HDFS采用一种称为机架感知的策略来改进数据的可靠性.可用性和网络带宽的利用率. 在大多数情况下,HDFS的副本系数是3,HDFS的存放策略是一个副本存放在本地机架节点上,另一个副本存放在同一机架的另 ...

  3. xmlHttpRequest在Firefox下不起作用?

    描述: XMLHttpRequest 在IE下正常,在Firefox下不起作用. 原因: XMLHttpRequest 对象的 onreadystatechange 不会在Firefox下执行, 解放 ...

  4. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  5. 通用的MIME类型:application/octet-stream

    按照内容类型排列的 Mime 类型列表 类型/子类型 扩展名 application/envoy evy application/fractals fif application/futurespla ...

  6. C#的位运算

    链接地址: http://www.cnblogs.com/NetBelieve/archive/2012/07/30/2615006.html

  7. HDU 5091 Beam Cannon (扫描线思想)

    题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...

  8. Android(java)学习笔记114:Service生命周期

    1.Service的生命周期         Android中的Service(服务)与Activity不同,它是不能和用户交互,不能自己启动的,运行在后台的程序,如果我们退出应用的时候,Servic ...

  9. Predicate Programming Guide

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/AdditionalChap ...

  10. python零基础学习开篇

    我是某工业大学的一名博士,研究方向是基于人脸的模式识别.很遗憾,毕业后没有继续从事图像处理中模式识别相关研究.多种原因进入了一家国企单位,从事交通方面工作.工作2年中一直迷茫,没有在工作岗位中找到自己 ...