The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

传送门:https://nanti.jisuanke.com/t/41400

题意:

给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得

\[\begin{array}{l}{\left|A_{i}-B_{j}\right| \leq C_{k}, \text { and }} \\ {\left|B_{j}-C_{k}\right| \leq A_{i}, \text { and }} \\ {\left|A_{i}-C_{k}\right| \leq B_{j}}\end{array}
\]

题解:

上面的不等式经过化简,我们可以得到

我们需要求有多少个三元组,使得\(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的一个常数

复杂度最后就是

\[T*2^{log(2n)}*log(2n)*6*x\\
=100*2^{18}*18*6=2,831,155,200‬
\]

然而暴力的复杂度是n2

假设有20组大数据 80组小数据(1000)

那么复杂度就是

\[20*18*2^{18}*6+1000*1000*80=646,231,040
\]

所以 小范围暴力 大范围FFT的复杂度是比较优秀滴

(感谢NE大佬对于复杂度的分析

代码:

  1. /**
  2. *        ┏┓    ┏┓
  3. *        ┏┛┗━━━━━━━┛┗━━━┓
  4. *        ┃       ┃  
  5. *        ┃   ━    ┃
  6. *        ┃ >   < ┃
  7. *        ┃       ┃
  8. *        ┃... ⌒ ...  ┃
  9. *        ┃       ┃
  10. *        ┗━┓   ┏━┛
  11. *          ┃   ┃ Code is far away from bug with the animal protecting          
  12. *          ┃   ┃ 神兽保佑,代码无bug
  13. *          ┃   ┃           
  14. *          ┃   ┃       
  15. *          ┃   ┃
  16. *          ┃   ┃           
  17. *          ┃   ┗━━━┓
  18. *          ┃       ┣┓
  19. *          ┃       ┏┛
  20. *          ┗┓┓┏━┳┓┏┛
  21. *           ┃┫┫ ┃┫┫
  22. *           ┗┻┛ ┗┻┛
  23. */
  24. // warm heart, wagging tail,and a smile just for you!
  25. //
  26. // _ooOoo_
  27. // o8888888o
  28. // 88" . "88
  29. // (| -_- |)
  30. // O\ = /O
  31. // ____/`---'\____
  32. // .' \| |// `.
  33. // / \||| : |||// \
  34. // / _||||| -:- |||||- \
  35. // | | \ - /// | |
  36. // | \_| ''\---/'' | |
  37. // \ .-\__ `-` ___/-. /
  38. // ___`. .' /--.--\ `. . __
  39. // ."" '< `.___\_<|>_/___.' >'"".
  40. // | | : `- \`.;`\ _ /`;.`/ - ` : | |
  41. // \ \ `-. \_ __\ /__ _/ .-` / /
  42. // ======`-.____`-.___\_____/___.-`____.-'======
  43. // `=---='
  44. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  45. // 佛祖保佑 永无BUG
  46. #include <set>
  47. #include <map>
  48. #include <stack>
  49. #include <cmath>
  50. #include <queue>
  51. #include <cstdio>
  52. #include <string>
  53. #include <vector>
  54. #include <cstring>
  55. #include <iostream>
  56. #include <algorithm>
  57. using namespace std;
  58. typedef long long LL;
  59. typedef pair<int, int> pii;
  60. typedef unsigned long long uLL;
  61. #define ls rt<<1
  62. #define rs rt<<1|1
  63. #define lson l,mid,rt<<1
  64. #define rson mid+1,r,rt<<1|1
  65. #define bug printf("*********\n")
  66. #define FIN freopen("input.txt","r",stdin);
  67. #define FON freopen("output.txt","w+",stdout);
  68. #define IO ios::sync_with_stdio(false),cin.tie(0)
  69. #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
  70. #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
  71. #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
  72. const int maxn = 5e5 + 5;
  73. const int INF = 0x3f3f3f3f;
  74. const int mod = 1e9 + 7;
  75. const double Pi = acos(-1);
  76. LL gcd(LL a, LL b) {
  77. return b ? gcd(b, a % b) : a;
  78. }
  79. LL lcm(LL a, LL b) {
  80. return a / gcd(a, b) * b;
  81. }
  82. double dpow(double a, LL b) {
  83. double ans = 1.0;
  84. while(b) {
  85. if(b % 2)ans = ans * a;
  86. a = a * a;
  87. b /= 2;
  88. } return ans;
  89. }
  90. LL quick_pow(LL x, LL y) {
  91. LL ans = 1;
  92. while(y) {
  93. if(y & 1) {
  94. ans = ans * x % mod;
  95. } x = x * x % mod;
  96. y >>= 1;
  97. } return ans;
  98. }
  99. LL res[maxn << 2], len;
  100. struct Complex {
  101. double r, i;
  102. Complex(double r = 0, double i = 0) : r(r), i(i) {};
  103. Complex operator+(const Complex &rhs) {
  104. return Complex(r + rhs.r, i + rhs.i);
  105. }
  106. Complex operator-(const Complex &rhs) {
  107. return Complex(r - rhs.r, i - rhs.i);
  108. }
  109. Complex operator*(const Complex &rhs) {
  110. return Complex(r * rhs.r - i * rhs.i, i * rhs.r + r * rhs.i);
  111. }
  112. } va[maxn << 2], vb[maxn << 2];
  113. void rader(Complex F[], int len) { //len = 2^M,reverse F[i] with F[j] j为i二进制反转
  114. int j = len >> 1;
  115. for (int i = 1; i < len - 1; ++i) {
  116. if (i < j) swap(F[i], F[j]); // reverse
  117. int k = len >> 1;
  118. while (j >= k) {
  119. j -= k;
  120. k >>= 1;
  121. }
  122. if (j < k) j += k;
  123. }
  124. }
  125. void FFT(Complex F[], const int &len, const int &t) {
  126. rader(F, len);
  127. for (int h = 2; h <= len; h <<= 1) {
  128. Complex wn(cos(-t * 2 * Pi / h), sin(-t * 2 * Pi / h));
  129. for (int j = 0; j < len; j += h) {
  130. Complex E(1, 0); //旋转因子
  131. for (int k = j; k < j + h / 2; ++k) {
  132. Complex u = F[k];
  133. Complex v = E * F[k + h / 2];
  134. F[k] = u + v;
  135. F[k + h / 2] = u - v;
  136. E = E * wn;
  137. }
  138. }
  139. }
  140. if (t == -1) //IDFT
  141. for (int i = 0; i < len; ++i)
  142. F[i].r /= len;
  143. }
  144. void Conv(Complex a[], Complex b[], const int &len) { //求卷积
  145. FFT(a, len, 1);
  146. FFT(b, len, 1);
  147. for (int i = 0; i < len; ++i) a[i] = a[i] * b[i];
  148. FFT(a, len, -1);
  149. }
  150. void work() {
  151. Conv(va, vb, len);
  152. for (int i = 0; i < len; ++i)res[i] = va[i].r + 0.5;
  153. }
  154. int a[maxn], b[maxn], c[maxn];
  155. int numa[maxn], numb[maxn], numc[maxn];
  156. int suma[maxn], sumb[maxn], sumc[maxn];//长度为i的数量
  157. LL resab[maxn], resbc[maxn], resac[maxn];//长度为i的 a,b的个数
  158. int main() {
  159. #ifndef ONLINE_JUDGE
  160. FIN
  161. #endif
  162. int T;
  163. int cas = 1;
  164. scanf("%d", &T);
  165. while(T--) {
  166. int n;
  167. scanf("%d", &n);
  168. int maxlena = 0, maxlenb = 0, maxlenc = 0;
  169. for(int i = 1; i <= n; i++) {
  170. scanf("%d", &a[i]);
  171. numa[a[i]]++;
  172. maxlena = max(maxlena, a[i]);
  173. }
  174. for(int i = 1; i <= n; i++) {
  175. scanf("%d", &b[i]);
  176. numb[b[i]]++;
  177. maxlenb = max(maxlenb, b[i]);
  178. }
  179. for(int i = 1; i <= n; i++) {
  180. scanf("%d", &c[i]);
  181. numc[c[i]]++;
  182. maxlenc = max(maxlenc, c[i]);
  183. }
  184. LL ans = 1ll * n * n * n;
  185. if(n <= 1000) {
  186. for(int i = 1; i <= maxlena; i++) {
  187. suma[i] = suma[i - 1] + numa[i];
  188. }
  189. for(int i = 1; i <= maxlenb; i++) {
  190. sumb[i] = sumb[i - 1] + numb[i];
  191. }
  192. for(int i = 1; i <= maxlenc; i++) {
  193. sumc[i] = sumc[i - 1] + numc[i];
  194. }
  195. for(int i = 1; i <= n; i++) {
  196. for(int j = 1; j <= n; j++) {
  197. resab[a[i] + b[j]]++;
  198. resbc[b[i] + c[j]]++;
  199. resac[a[i] + c[j]]++;
  200. }
  201. }
  202. int mx = max(maxlena, max(maxlenb, maxlenc));
  203. for(int i = 1; i <= mx; i++) {
  204. if(i <= maxlenc) ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
  205. if(i <= maxlenb) ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
  206. if(i <= maxlena) ans -= resbc[i] * (suma[maxlena] - suma[i]);
  207. }
  208. memset(suma, 0, sizeof(int) * (maxlena + 2));
  209. memset(sumb, 0, sizeof(int) * (maxlenb + 2));
  210. memset(sumc, 0, sizeof(int) * (maxlenc + 2));
  211. memset(numa, 0, sizeof(int) * (maxlena + 2));
  212. memset(numb, 0, sizeof(int) * (maxlenb + 2));
  213. memset(numc, 0, sizeof(int) * (maxlenc + 2));
  214. memset(resac, 0, sizeof(LL) * (mx * 2 + 2));
  215. memset(resab, 0, sizeof(LL) * (mx * 2 + 2));
  216. memset(resbc, 0, sizeof(LL) * (mx * 2 + 2));
  217. } else {
  218. maxlena++, maxlenb++, maxlenc++;
  219. len = 1;
  220. int mxab = max(maxlena, maxlenb);
  221. while(len < 2 * mxab) len <<= 1;
  222. for(int i = 0; i < len; i++) {
  223. if (i < mxab) {
  224. va[i] = Complex(numa[i], 0);
  225. vb[i] = Complex(numb[i], 0);
  226. } else
  227. va[i] = vb[i] = Complex(0, 0);
  228. }
  229. work();
  230. for (int i = 0; i < len; i++) resab[i] = res[i];
  231. len = 1;
  232. int mxac = max(maxlena, maxlenc);
  233. while(len < 2 * mxac) len <<= 1;
  234. for(int i = 0; i < len; i++) {
  235. if (i < mxac) {
  236. va[i] = Complex(numa[i], 0);
  237. vb[i] = Complex(numc[i], 0);
  238. } else
  239. va[i] = vb[i] = Complex(0, 0);
  240. }
  241. work();
  242. for (int i = 0; i < len; i++) resac[i] = res[i];
  243. len = 1;
  244. int mxbc = max(maxlenb, maxlenc);
  245. while(len < 2 * mxbc) len <<= 1;
  246. for(int i = 0; i < len; i++) {
  247. if (i < mxbc) {
  248. va[i] = Complex(numb[i], 0);
  249. vb[i] = Complex(numc[i], 0);
  250. } else
  251. va[i] = vb[i] = Complex(0, 0);
  252. }
  253. work();
  254. for (int i = 0; i < len; i++) resbc[i] = res[i];
  255. for(int i = 1; i <= maxlena; i++) {
  256. suma[i] = suma[i - 1] + numa[i];
  257. }
  258. for(int i = 1; i <= maxlenb; i++) {
  259. sumb[i] = sumb[i - 1] + numb[i];
  260. }
  261. for(int i = 1; i <= maxlenc; i++) {
  262. sumc[i] = sumc[i - 1] + numc[i];
  263. }
  264. for (int i = 1; i <= 2 * mxab; ++i) {
  265. if (i > maxlenc) break;
  266. ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
  267. }
  268. for (int i = 1; i <= 2 * mxbc; ++i) {
  269. if (i > maxlena) break;
  270. ans -= resbc[i] * (suma[maxlena] - suma[i]);
  271. }
  272. for (int i = 1; i <= 2 * mxac; ++i) {
  273. if (i > maxlenb) break;
  274. ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
  275. }
  276. memset(suma, 0, sizeof(int) * (maxlena + 2));
  277. memset(sumb, 0, sizeof(int) * (maxlenb + 2));
  278. memset(sumc, 0, sizeof(int) * (maxlenc + 2));
  279. memset(numa, 0, sizeof(int) * (maxlena + 2));
  280. memset(numb, 0, sizeof(int) * (maxlenb + 2));
  281. memset(numc, 0, sizeof(int) * (maxlenc + 2));
  282. memset(resac, 0, sizeof(LL) * (mxac * 2 + 2));
  283. memset(resab, 0, sizeof(LL) * (mxab * 2 + 2));
  284. memset(resbc, 0, sizeof(LL) * (mxbc * 2 + 2));
  285. }
  286. printf("Case #%d: %lld\n", cas++, ans);
  287. }
  288. return 0;
  289. }

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)的更多相关文章

  1. The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple

    [传送门] FFT第三题! 其实就是要求有多少三元组满足两短边之和大于等于第三边. 考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案. 当 $n \le ...

  2. The Preliminary Contest for ICPC Asia Shanghai 2019

    传送门 B. Light bulbs 题意: 起初\(n\)个位置状态为\(0\),\(m\)次操作,每次操作更换区间状态:\(0\)到\(1\),\(1\)到\(0\). 共有\(T,T\leq 1 ...

  3. 01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019

    题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: ...

  4. 给定进制下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 ...

  5. The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I

    传送门 因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门 这里维护虚儿子用的是 $multiset$ ,没写可删堆 #include<i ...

  6. The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum

    题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; ][]= ...

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

  8. 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补        ...

  9. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

    题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...

随机推荐

  1. SQL Server 记录(更新中...)

    sys.databases 显示所有数据库信息 sys.tables 显示当前数据库所有的表的信息 Go 向 SQL Server 实用工具发出一批 Transact-SQL 语句已结束的信号,Go本 ...

  2. Spring读取mybatis在多个jar包下的的mapper文件

     刚开始的时候我的配置文件在同名目录下都是在/mapper下,导致只能读取一个jar中的mapper文件.先解决如下: 1.将mapper文件放在不能放在同名的目录下.        比如:user. ...

  3. js+canvas实现象棋的布局、走棋位置提示、走棋代码

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. spring配置文件各个属性详解

    一.引用外部属性文件 <bean id="propertyConfigurer" class="org.springframework.beans.factory. ...

  5. 屏蔽指定地区IP访问

    <?php if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) { $ip = $HTTP_SERVER_VARS["HT ...

  6. OpenStack组件系列☞Keystone搭建

    一:版本信息 官网:http://docs.openstack.org/newton/install-guide-rdo/keystone.html 二:部署keystone 官网文档:http:// ...

  7. Linux 使用 Speedtest 测试网速

    Speedtest的linux客户端是用python写的一个安装包 安装python包管理器pip yum -y install python-pip 如果提示No package python-pi ...

  8. MATLAB常用函数, 常见问题

    MATLAB常用函数 1.常用取整函数 round(x):四舍五入函数 floor(x) : 向下取整, 即 floor(1.2)=1,  floor(1.8) = 1 ceil(x) : 向上取整, ...

  9. HDU 5463

    题意:一个盒子有36个格子.每个格子可以装64个物品,搬运一个箱子是一次搬运,问最少到搬运次数 思路:直接求总需要多少个格子,然后去求盒子,这里求盒子呢有个小技巧,就是用ceil函数 #include ...

  10. 洛谷P1809 过河问题 经典贪心问题

    作者:zifeiy 标签:贪心 题目链接:https://www.luogu.org/problem/P1809 我们假设第 \(i\) 个人过河的耗时是 \(t[i]\) ,并且 \(t[i]\) ...