The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple
FFT第三题!
其实就是要求有多少三元组满足两短边之和大于等于第三边。
考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案。
当 $n \leq 1000$ 时,直接暴力,因为如果继续 FFT 的话复杂度是 $O(slogs)$,$s$ 表示值域,值域都到 $10^5$,$100$ 组吃不消。
比 $1000$ 大就 FFT 做即可。
#include <bits/stdc++.h>
struct Complex {
double r, i;
void clear() { r = i = 0.0; }
Complex(double r = , double i = ): 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 = 5e5 + ;
int n, limit, l, r[N];
void FFT(Complex *a, int n, int pd) {
for (int i = ; i < n; i++)
if (i < r[i])
std::swap(a[i], a[r[i]]);
for (int mid = ; mid < n; mid <<= ) {
Complex wn(cos(pi / mid), pd * sin(pi / mid));
for (int l = mid << , j = ; j < n; j += l) {
Complex w(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 < n; i++)
a[i] = Complex(a[i].r / n, a[i].i / n);
}
#define ll long long
int A[N], B[N], C[N];
ll cntA[N], cntB[N], cntC[N];
void init(int val) {
for (int i = ; i <= val; i++)
cntA[i] = cntB[i] = cntC[i] = ;
}
void solve1() {
int val = * std::max(A[n], std::max(B[n], C[n])) + ;
for (int i = ; i <= val; i++)
cntA[i] += cntA[i - ], cntB[i] += cntB[i - ], cntC[i] += cntC[i - ];
ll ans = ;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
int cur = A[i] + B[j];
ans += cntC[val] - cntC[cur];
cur = A[i] + C[j];
ans += cntB[val] - cntB[cur];
cur = B[i] + C[j];
ans += cntA[val] - cntA[cur];
}
ans = 1LL * n * n * n - ans;
assert(ans >= );
printf("%lld\n", ans);
}
Complex a[N], b[N], c[N], res[N];
void solve2() {
limit = , l = ;
int val = * std::max(A[n], std::max(B[n], C[n])) + ;
while (limit <= val) limit <<= , l++;
for (int i = ; i < limit; i++)
r[i] = r[i >> ] >> | ((i & ) << (l - ));
for (int i = ; i < limit; i++)
a[i] = Complex((double)cntA[i], 0.0), b[i] = Complex((double)cntB[i], 0.0), c[i] = Complex((double)cntC[i], 0.0);
for (int i = ; i <= val; i++)
cntA[i] += cntA[i - ], cntB[i] += cntB[i - ], cntC[i] += cntC[i - ];
FFT(a, limit, ); FFT(b, limit, ); FFT(c, limit, );
for (int i = ; i < limit; i++)
res[i] = a[i] * b[i];
FFT(res, limit, -);
ll ans = ;
for (int i = ; i < limit; i++) {
ll temp = (ll)(res[i].r + 0.5);
ans += (cntC[val] - cntC[i]) * temp;
}
for (int i = ; i < limit; i++)
res[i] = b[i] * c[i];
FFT(res, limit, -);
for (int i = ; i < limit; i++) {
ll temp = (ll)(res[i].r + 0.5);
ans += (cntA[val] - cntA[i]) * temp;
}
for (int i = ; i < limit; i++)
res[i] = a[i] * c[i];
FFT(res, limit, -);
for (int i = ; i < limit; i++) {
ll temp = (ll)(res[i].r + 0.5);
ans += (cntB[val] - cntB[i]) * temp;
}
ans = 1LL * n * n * n - ans;
assert(ans >= );
printf("%lld\n", ans);
}
int main() {
int T;
scanf("%d", &T);
for (int kase = ; kase <= T; kase++) {
scanf("%d", &n);
int x = ;
for (int i = ; i <= n; i++)
scanf("%d", A + i), x = std::max(x, A[i]);
for (int i = ; i <= n; i++)
scanf("%d", B + i), x = std::max(x, B[i]);
for (int i = ; i <= n; i++)
scanf("%d", C + i), x = std::max(x, C[i]);
init(N - );
std::sort(A + , A + + n);
std::sort(B + , B + + n);
std::sort(C + , C + + n);
for (int i = ; i <= n; i++)
cntA[A[i]]++, cntB[B[i]]++, cntC[C[i]]++;
printf("Case #%d: ", kase);
if (n <= ) solve1();
else solve2();
}
return ;
}
The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple的更多相关文章
- 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/ ...
- 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 ...
随机推荐
- 关于XSS攻击
1.XSS XSS(Cross Site Scripting)攻击全称跨站脚本攻击,为了不与CSS(Cascading Style Sheets)混淆,故将跨站脚本攻击缩写为XSS,XSS是一种经常出 ...
- React中的fetch请求相关
fetch在reactjs中等同于 XMLHttpRequest,它提供了许多与XMLHttpRequest相同的功能,但被设计成更具可扩展性和高效性. Fetch 的核心在于对 HTTP 接口的抽象 ...
- mysql不是内部或外部命令--windows环境下报错的解决
安装Mysql后,当我们在cmd中敲入mysql时会出现‘Mysql’不是内部或外部命令,也不是可运行的程序或其处理文件. 处理: 我的电脑右键属性>高级系统设置>高级>环境变量&g ...
- Linux shell自动读mongo数据、远程获取文件大小示例脚本
1.示例1 功能:对mongoDB导出数据,根据sid的不同状态进行统计 技术点:shell bash 读写文件.字符串截取.函数.用多个文件提到的map.grep查找并赋值给变量 #!/bin/b ...
- 大话设计模式Python实现-适配器模式
适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口. 下面是一个适配器模式的demo: #!/usr/bin/env python # -*- coding:u ...
- 【LOJ#3146】[APIO2019]路灯(树套树)
[LOJ#3146][APIO2019]路灯(树套树) 题面 LOJ 题解 考场上因为\(\text{bridge}\)某个\(\text{subtask}\)没有判\(n=1\)的情况导致我卡了\( ...
- Microsoft.Extensions.DependencyInjection 之一:解析实现
[TOC] 前言 项目使用了 Microsoft.Extensions.DependencyInjection 2.x 版本,遇到第2次请求时非常高的内存占用情况,于是作了调查,本文对 3.0 版本仍 ...
- io机制沉思录:分层与管理
io模型的核心是内核kernel与应用(线程)的关系: 内核与应用的联系:数据状态信号和数据本身: 一.分层模型: 应用层——内核层——设备层 https://www.cnblogs.com/feng ...
- virsh console hangs at the escape character “^]”
I am trying to kickstart a newly built VM. I am stuck with the following. Want to start with a conso ...
- opencv 图像旋转
理论 http://www.cnblogs.com/wangguchangqing/p/4045150.html 翻开任意一本图像处理的书,都会讲到图像的几何变换,这里面包括:仿射变换(affine ...