D. The BOSS Can Count Pairs

D. The BOSS Can Count Pairs

You are given two arrays a and b, both of length n.

Your task is to count the number of pairs of integers (i,j) such that 1i<jn and aiaj=bi+bj.

Input

Each test contains multiple test cases. The first line of input contains a single integer t (1t104) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (2n2105) — the length of the arrays.

The second line of each test case contains n integers a1,a2,,an (1ain) — the elements of array a.

The third line of each test case contains n integers b1,b2,,bn (1bin) — the elements of array b.

It is guaranteed that the sum of n across all test cases does not exceed 2105.

Output

For each test case, output the number of good pairs.

Example

input

复制代码
3
3
2 3 2
3 3 1
8
4 2 8 2 1 2 7 5
3 5 8 8 1 1 6 5
8
4 4 8 8 8 8 8 8
8 8 8 8 8 8 8 8
复制代码

output

2
7
1

Note

In the first sample, there are 2 good pairs:

  • (1,2),
  • (1,3).

In the second sample, there are 7 good pairs:

  • (1,2),
  • (1,5),
  • (2,8),
  • (3,4),
  • (4,7),
  • (5,6),
  • (5,7).

 

解题思路

  枚举优化题,先给出我一开始的思路。因为1bin,因此有ai×aj=bi+bj2n,所以当时就想到从1开始枚举到2n作为ai×aj的结果,记作x。然后对x进行约数分解,那么就会得到aiaj。接着枚举所有值为ai的下标u,然后再从所有值为aj的下标中二分出来大于u的最小下标v,然后在所有值为aj的且不超过v的下标中统计数值恰好为ai×ajbu的个数,这就是答案数对的数目。

  很明显这种做法肯定会超时,需要优化的地方为统计数值恰好为ai×ajbu的个数,但很复杂我没写出来,即便写出来时间复杂度也是O(nnlogn),还是会超时。不过如果提前预处理每个数约数分解的结果那么n计算量的分解部分就会大大降低,有可能会过。

  下面给出正解。

  首先我们知道了ai×aj2n,因此必然有min{ai,aj}2n。由于题目中要求统计的数对(i,j)要满足i<j,但如果按照aiaj的规则来统计数对得到的答案是一样的。其中如果有ai<aji>j,那么我们只需交换ij,可以发现得到的数对是一样的,因此接下来我们只统计aiaj的数对。

  由于已经假设aiaj,因此有ai2n,所以我们从12n来枚举ai的值(a中可能不存在这个值),然后再枚举一遍序列a找到大于等于aiaj,此时就可以确定bi的取值,即ai×ajbj。那么满足条件的数对数量就是序列ab中满足(ak,bk)=(ai,bi)的数量(前提是算出的bi要满足1bin)。

  为此我们可以先对数对(ak,bk)ak为关键字进行排序,在枚举确定每一个ai后开一个数组cnt来统计数对(ai,bi)的数量,由于ai已经是一个确定值,因此cnt[x]则表示所有ak=ai并且bk=x的数对数量。由于序列a已经升序排序,因此在枚举aj时边枚举边统计即可,当然只统计aj=ai对应的bj。因此对于某个aj,满足等式的数对数量就是cnt[ai×ajbj](下标严格小于j的数对数量)。这样就可以避免重复枚举,且没有枚举到两个相同下标的情况。

  AC代码如下,时间复杂度为O(nn)

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int a[N], b[N], p[N];
 9 int cnt[N];
10 
11 void solve() {
12     int n;
13     scanf("%d", &n);
14     for (int i = 0; i < n; i++) {
15         scanf("%d", a + i);
16     }
17     for (int i = 0; i < n; i++) {
18         scanf("%d", b + i);
19     }
20     for (int i = 0; i < n; i++) {
21         p[i] = i;
22     }
23     sort(p, p + n, [&](int i, int j) {
24         return a[i] < a[j];
25     });
26     LL ret = 0;
27     for (int i = 1; i * i <= n << 1; i++) {
28         memset(cnt, 0, n + 10 << 2);
29         for (int j = 0; j < n; j++) {
30             int t = i * a[p[j]] - b[p[j]];
31             if (t >= 1 && t <= n) ret += cnt[t];
32             if (i == a[p[j]]) cnt[b[p[j]]]++;
33         }
34     }
35     printf("%lld\n", ret);
36 }
37 
38 int main() {
39     int t;
40     scanf("%d", &t);
41     while (t--) {
42         solve();
43     }
44     
45     return 0;
46 }
复制代码

 

参考资料

  Codeforces Round 875 (Div. 2) D (思维 + 枚举):https://zhuanlan.zhihu.com/p/633099878

posted @   onlyblues  阅读(121)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2021-06-01 浙江大学-陈越、何钦铭-数据结构-习题集解析
Web Analytics
点击右上角即可分享
微信分享提示