[题解]CF1830B The BOSS Can Count Pairs
思路
观察到 \(a_i,b_i \leq n\),又要求 \(a_i \times a_j = b_i + b_j\) 的数量,那么显然有 \(a_i \times a_j = b_i + b_j \leq 2n\)。
并且显然有 \(a_i\) 和 \(a_j\) 其中一个一定小于 \(\sqrt{2n}\)。不妨枚举 \(i\) 和 \(a_j\) 的值 \(x\),那么 \(b_j\) 可以被表示为 \(a_i \times x - b_i\)。
于是我们可以直接开一个桶记录满足 \(a_j = x\) 的 \(b_j\)。其次为了避免计算重复,应当对 \(a,b\) 整体进行排序。
Code
#include <bits/stdc++.h>
#define fst first
#define snd second
#define re register
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int N = 2e5 + 10;
int n,vis[N];
pii arr[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline void solve(){
int ans = 0;
n = read();
for (re int i = 1;i <= n;i++) arr[i].fst = read();
for (re int i = 1;i <= n;i++) arr[i].snd = read();
sort(arr + 1,arr + n + 1);
for (re int x = 1;x * x <= 2 * n;x++){
vector<int> del;
for (re int i = 1;i <= n;i++){
int y = x * arr[i].fst - arr[i].snd;
if (1 <= y && y <= n) ans += vis[y];
if (arr[i].fst == x){
del.push_back(arr[i].snd);
vis[arr[i].snd]++;
}
}
for (int p:del) vis[p]--;
}
printf("%lld\n",ans);
}
signed main(){
int T; T = read();
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号