【传送门】

FFT第一题!

构造多项式 $A(x) = \sum x ^ {s_i}$。

不考虑题目中 $i < j < k$ 的条件,那么 $A^3(x)$ 每一项对应的系数就是答案了。

考虑容斥。

$$(\sum x)^3 = \sum x^3 + 3 \sum x^2 y + 6\sum xyz$$

$$(\sum x^2) (\sum x)= \sum x^3 + \sum x^2 y$$

所以 $$\sum xyz = \dfrac{(\sum x)^3 - 3 (\sum x^2)(\sum x) + 2 \sum x^3}{6}$$

#include <bits/stdc++.h>

struct Complex {
double r, i;
Complex(double r = 0.0, double i = 0.0): r(r), i(i) {}
Complex operator + (const Complex &p) const { return Complex(r + p.r, i + p.i); }
Complex operator - (const Complex &p) const { return Complex(r - p.r, i - p.i); }
Complex operator * (const Complex &p) const { return Complex(r * p.r - i * p.i, r * p.i + i * p.r); }
}; const double pi = acos(-1.0);
const int N = 2e5 + ;
int n, limit, r[N], l;
int v[N], A[N], B[N], C[N];
Complex a[N], b[N], c[N]; void FFT(Complex *a, int pd) {
for (int i = ; i < limit; i++)
if (i < r[i])
std::swap(a[i], a[r[i]]);
for (int mid = ; mid < limit; mid <<= ) {
Complex wn = Complex(cos(pi / mid), pd * sin(pi / mid));
for (int l = mid << , j = ; j < limit; j += l) {
Complex w = Complex(1.0, 0.0);
for (int k = ; k < mid; k++, w = w * wn) {
Complex u = a[k + j], v = w * a[k + j + mid];
a[k + j] = u + v;
a[k + j + mid] = u - v;
}
}
}
if (pd == -)
for (int i = ; i < limit; i++)
a[i] = Complex(a[i].r / limit, a[i].i / limit);
} int main() {
scanf("%d", &n);
for (int i = ; i < n; i++) {
int x;
scanf("%d", &x);
x += ;
A[x]++;
B[x * ]++;
C[x * ]++;
}
for (int i = ; i <= ; i++)
a[i] = Complex((double)A[i], 0.0);
for (int i = ; i <= ; i++)
b[i] = Complex((double)B[i], 0.0);
limit = ;
while (limit <= + )
limit <<= , l++;
for (int i = ; i < limit; i++)
r[i] = r[i >> ] >> | ((i & ) << (l - ));
FFT(a, );
FFT(b, );
for (int i = ; i < limit; i++)
b[i] = b[i] * a[i];
for (int i = ; i < limit; i++)
a[i] = a[i] * a[i] * a[i];
FFT(a, -);
FFT(b, -);
for (int i = ; i <= ; i++) {
long long ans = (long long)((a[i].r - 3.0 * b[i].r + 2.0 * C[i]) / 6.0 + 0.5);
if (ans > )
printf("%d : %lld\n", i - , ans);
}
return ;
}

SPOJ - Triple Sums的更多相关文章

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

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

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

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

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

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

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

  5. spoj TSUM - Triple Sums fft+容斥

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

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

  7. SPOJ #453. Sums in a Triangle (tutorial)

    It is a small fun problem to solve. Since only a max sum is required (no need to print path), we can ...

  8. [SP8372-TSUM]Triple Sums

    题面在这里 description 某\(B\)姓\(OJ\)权限题 给出\(n\)个正整数\(a[i]\),求\(i<j<k\)且\(S=a[i]+a[j]+a[k]\)的三元组\((i ...

  9. spoj-TSUM Triple Sums

    题目描述 题解: 很吊的容斥+$FFT$,但是并不难. 首先,由于有重复,我们要容斥. 怎么办? 记录三个多项式, 只取一个:$w1$; 相同物体拿两个:$w2$; 相同物体拿三个:$w3$; 然后答 ...

随机推荐

  1. pwntools出现的一些问题

    pwntools用的好好的突然就不能用了总结了一些问题:ImportError:cannot import name ENUM_P_TYPE 解决方法为:将/usr/local/lib/python2 ...

  2. CentOS6.9安装SonarQube7.6

    1 安装前准备 Java (Oracle JRE 8 or OpenJDK 8) MySQL5.6 or MySQL5.7,具体可参考Centos6.9安装MySQL5.6 SonarQube7.6, ...

  3. 物联网架构成长之路(31)-EMQ基于HTTP权限验证

    看过之前的文章就知道,我之前是通过搞插件,或者通过里面的MongoDB来进行EMQ的鉴权登录和权限验证.但是前段时间发现,还是通过HTTP WebHook 方式来调用鉴权接口比较适合实际使用.还是实现 ...

  4. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  5. APP兼容性测试 (一) 机型选择概要

    一.App兼容性问题有哪些 安装失败.启动失败.卸载失败,卸载不干净. 程序运行过程中闪退 部分控件显示不完整或者功能失效 屏幕显示异常 图片展示不全等 二.App兼容性测试的核心要点 测试软件是否能 ...

  6. gperf heap profiler

    前言 gperf tools有很多功能,其中有一个heap profiler,可按函数级别定位分配内存的累积量 原理 gperf tools需要替换libc的malloc库,替换为tcmalloc:t ...

  7. 【题解】Dvoniz [COCI2011]

    [题解]Dvoniz [COCI2011] 没有传送门,只有提供了数据的官网. [题目描述] 对于一个长度为 \(2*K\) 的序列,如果它的前 \(K\) 个元素之和小于等于 \(S\) 且后 \( ...

  8. java函数式编程的形式

    java中没有真正的函数变量: 一.所有的函数(拉姆达)表达式,都被解释为functional interface @FunctionalInterface interface GreetingSer ...

  9. 引用kernel32.dll中的API来进行串口通讯

    串口通讯可以引出kernel32.dll中的API来操作,相关源码如下:using System;using System.Runtime.InteropServices; namespace Tel ...

  10. Windows cmd 和 PowerShell 中文乱码问题解决

    临时方案: 在命令行下输入:chcp 65001 长期方案: 要修改注册表,自己网上搜吧