The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)
The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)
传送门:https://nanti.jisuanke.com/t/41400
题意:
给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得
\]
题解:
上面的不等式经过化简,我们可以得到
我们需要求有多少个三元组,使得\(A_i,B_j,C_k\)可以组成一个三角形
这样组成三角形的题目类似于HDU4609 (https://www.cnblogs.com/buerdepepeqi/p/11236100.html)
但是不同的是 我们需要从三个数组中选择
所以这里就涉及到了选择重复的问题,我们考虑去重
假设拿a+b做一遍卷积,得到长度为a+b的木棍的数量,
我们假设 c是三角形的最长边,那么a,b,c三根木棍不能组成三角形的情况就是c的长度大于a+b的数量
我们枚举(a+b)这个长度,那么不能组成三角形的数量就是 可以组成当前长度的(a,b)的方案数*大于这个长度的c的数量
所以按照这样来说 我们就可以得到 做三遍卷积,分别枚举c为最长边时,b为最长边时, a为最长边时 不和法的数量,然后用所有的三元组的数量减去不合法的三元组的数量就是合法的三元组的数量
这里有一个小技巧,就是小范围暴力??如果小范围不暴力你就会T
(留恋一下机房惨案
我们来分析一下为什么?
我们假设FFT的Complex类带了一个常数 x
在T是100的情况下,我们单纯的跑三遍FFT,那么就是三遍正的FFT,三遍IDFT,那么就是6的一个常数
复杂度最后就是
=100*2^{18}*18*6=2,831,155,200
\]
然而暴力的复杂度是n2
假设有20组大数据 80组小数据(1000)
那么复杂度就是
\]
所以 小范围暴力 大范围FFT的复杂度是比较优秀滴
(感谢NE大佬对于复杂度的分析
代码:
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
LL res[maxn << 2], len;
struct Complex {
double r, i;
Complex(double r = 0, double i = 0) : r(r), i(i) {};
Complex operator+(const Complex &rhs) {
return Complex(r + rhs.r, i + rhs.i);
}
Complex operator-(const Complex &rhs) {
return Complex(r - rhs.r, i - rhs.i);
}
Complex operator*(const Complex &rhs) {
return Complex(r * rhs.r - i * rhs.i, i * rhs.r + r * rhs.i);
}
} va[maxn << 2], vb[maxn << 2];
void rader(Complex F[], int len) { //len = 2^M,reverse F[i] with F[j] j为i二进制反转
int j = len >> 1;
for (int i = 1; i < len - 1; ++i) {
if (i < j) swap(F[i], F[j]); // reverse
int k = len >> 1;
while (j >= k) {
j -= k;
k >>= 1;
}
if (j < k) j += k;
}
}
void FFT(Complex F[], const int &len, const int &t) {
rader(F, len);
for (int h = 2; h <= len; h <<= 1) {
Complex wn(cos(-t * 2 * Pi / h), sin(-t * 2 * Pi / h));
for (int j = 0; j < len; j += h) {
Complex E(1, 0); //旋转因子
for (int k = j; k < j + h / 2; ++k) {
Complex u = F[k];
Complex v = E * F[k + h / 2];
F[k] = u + v;
F[k + h / 2] = u - v;
E = E * wn;
}
}
}
if (t == -1) //IDFT
for (int i = 0; i < len; ++i)
F[i].r /= len;
}
void Conv(Complex a[], Complex b[], const int &len) { //求卷积
FFT(a, len, 1);
FFT(b, len, 1);
for (int i = 0; i < len; ++i) a[i] = a[i] * b[i];
FFT(a, len, -1);
}
void work() {
Conv(va, vb, len);
for (int i = 0; i < len; ++i)res[i] = va[i].r + 0.5;
}
int a[maxn], b[maxn], c[maxn];
int numa[maxn], numb[maxn], numc[maxn];
int suma[maxn], sumb[maxn], sumc[maxn];//长度为i的数量
LL resab[maxn], resbc[maxn], resac[maxn];//长度为i的 a,b的个数
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int T;
int cas = 1;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
int maxlena = 0, maxlenb = 0, maxlenc = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
numa[a[i]]++;
maxlena = max(maxlena, a[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
numb[b[i]]++;
maxlenb = max(maxlenb, b[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
numc[c[i]]++;
maxlenc = max(maxlenc, c[i]);
}
LL ans = 1ll * n * n * n;
if(n <= 1000) {
for(int i = 1; i <= maxlena; i++) {
suma[i] = suma[i - 1] + numa[i];
}
for(int i = 1; i <= maxlenb; i++) {
sumb[i] = sumb[i - 1] + numb[i];
}
for(int i = 1; i <= maxlenc; i++) {
sumc[i] = sumc[i - 1] + numc[i];
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
resab[a[i] + b[j]]++;
resbc[b[i] + c[j]]++;
resac[a[i] + c[j]]++;
}
}
int mx = max(maxlena, max(maxlenb, maxlenc));
for(int i = 1; i <= mx; i++) {
if(i <= maxlenc) ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
if(i <= maxlenb) ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
if(i <= maxlena) ans -= resbc[i] * (suma[maxlena] - suma[i]);
}
memset(suma, 0, sizeof(int) * (maxlena + 2));
memset(sumb, 0, sizeof(int) * (maxlenb + 2));
memset(sumc, 0, sizeof(int) * (maxlenc + 2));
memset(numa, 0, sizeof(int) * (maxlena + 2));
memset(numb, 0, sizeof(int) * (maxlenb + 2));
memset(numc, 0, sizeof(int) * (maxlenc + 2));
memset(resac, 0, sizeof(LL) * (mx * 2 + 2));
memset(resab, 0, sizeof(LL) * (mx * 2 + 2));
memset(resbc, 0, sizeof(LL) * (mx * 2 + 2));
} else {
maxlena++, maxlenb++, maxlenc++;
len = 1;
int mxab = max(maxlena, maxlenb);
while(len < 2 * mxab) len <<= 1;
for(int i = 0; i < len; i++) {
if (i < mxab) {
va[i] = Complex(numa[i], 0);
vb[i] = Complex(numb[i], 0);
} else
va[i] = vb[i] = Complex(0, 0);
}
work();
for (int i = 0; i < len; i++) resab[i] = res[i];
len = 1;
int mxac = max(maxlena, maxlenc);
while(len < 2 * mxac) len <<= 1;
for(int i = 0; i < len; i++) {
if (i < mxac) {
va[i] = Complex(numa[i], 0);
vb[i] = Complex(numc[i], 0);
} else
va[i] = vb[i] = Complex(0, 0);
}
work();
for (int i = 0; i < len; i++) resac[i] = res[i];
len = 1;
int mxbc = max(maxlenb, maxlenc);
while(len < 2 * mxbc) len <<= 1;
for(int i = 0; i < len; i++) {
if (i < mxbc) {
va[i] = Complex(numb[i], 0);
vb[i] = Complex(numc[i], 0);
} else
va[i] = vb[i] = Complex(0, 0);
}
work();
for (int i = 0; i < len; i++) resbc[i] = res[i];
for(int i = 1; i <= maxlena; i++) {
suma[i] = suma[i - 1] + numa[i];
}
for(int i = 1; i <= maxlenb; i++) {
sumb[i] = sumb[i - 1] + numb[i];
}
for(int i = 1; i <= maxlenc; i++) {
sumc[i] = sumc[i - 1] + numc[i];
}
for (int i = 1; i <= 2 * mxab; ++i) {
if (i > maxlenc) break;
ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
}
for (int i = 1; i <= 2 * mxbc; ++i) {
if (i > maxlena) break;
ans -= resbc[i] * (suma[maxlena] - suma[i]);
}
for (int i = 1; i <= 2 * mxac; ++i) {
if (i > maxlenb) break;
ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
}
memset(suma, 0, sizeof(int) * (maxlena + 2));
memset(sumb, 0, sizeof(int) * (maxlenb + 2));
memset(sumc, 0, sizeof(int) * (maxlenc + 2));
memset(numa, 0, sizeof(int) * (maxlena + 2));
memset(numb, 0, sizeof(int) * (maxlenb + 2));
memset(numc, 0, sizeof(int) * (maxlenc + 2));
memset(resac, 0, sizeof(LL) * (mxac * 2 + 2));
memset(resab, 0, sizeof(LL) * (mxab * 2 + 2));
memset(resbc, 0, sizeof(LL) * (mxbc * 2 + 2));
}
printf("Case #%d: %lld\n", cas++, ans);
}
return 0;
}
The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)的更多相关文章
- The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple
[传送门] FFT第三题! 其实就是要求有多少三元组满足两短边之和大于等于第三边. 考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案. 当 $n \le ...
- The Preliminary Contest for ICPC Asia Shanghai 2019
传送门 B. Light bulbs 题意: 起初\(n\)个位置状态为\(0\),\(m\)次操作,每次操作更换区间状态:\(0\)到\(1\),\(1\)到\(0\). 共有\(T,T\leq 1 ...
- 01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: ...
- 给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41422 对每一位进行找循环节规律就行了. #define IOS ios_base::sync_with_stdio(0); cin ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I
传送门 因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门 这里维护虚儿子用的是 $multiset$ ,没写可删堆 #include<i ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum
题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; ][]= ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game
题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 D. Counting Sequences I
题目:https://nanti.jisuanke.com/t/41412思路:dfs 先取ai>2 2^12>3000 因此至多取11个 其余用1补 ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs
题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...
随机推荐
- SQL Server 记录(更新中...)
sys.databases 显示所有数据库信息 sys.tables 显示当前数据库所有的表的信息 Go 向 SQL Server 实用工具发出一批 Transact-SQL 语句已结束的信号,Go本 ...
- Spring读取mybatis在多个jar包下的的mapper文件
刚开始的时候我的配置文件在同名目录下都是在/mapper下,导致只能读取一个jar中的mapper文件.先解决如下: 1.将mapper文件放在不能放在同名的目录下. 比如:user. ...
- js+canvas实现象棋的布局、走棋位置提示、走棋代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- spring配置文件各个属性详解
一.引用外部属性文件 <bean id="propertyConfigurer" class="org.springframework.beans.factory. ...
- 屏蔽指定地区IP访问
<?php if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) { $ip = $HTTP_SERVER_VARS["HT ...
- OpenStack组件系列☞Keystone搭建
一:版本信息 官网:http://docs.openstack.org/newton/install-guide-rdo/keystone.html 二:部署keystone 官网文档:http:// ...
- Linux 使用 Speedtest 测试网速
Speedtest的linux客户端是用python写的一个安装包 安装python包管理器pip yum -y install python-pip 如果提示No package python-pi ...
- MATLAB常用函数, 常见问题
MATLAB常用函数 1.常用取整函数 round(x):四舍五入函数 floor(x) : 向下取整, 即 floor(1.2)=1, floor(1.8) = 1 ceil(x) : 向上取整, ...
- HDU 5463
题意:一个盒子有36个格子.每个格子可以装64个物品,搬运一个箱子是一次搬运,问最少到搬运次数 思路:直接求总需要多少个格子,然后去求盒子,这里求盒子呢有个小技巧,就是用ceil函数 #include ...
- 洛谷P1809 过河问题 经典贪心问题
作者:zifeiy 标签:贪心 题目链接:https://www.luogu.org/problem/P1809 我们假设第 \(i\) 个人过河的耗时是 \(t[i]\) ,并且 \(t[i]\) ...