[题解]CF1830B The BOSS Can Count Pairs

思路

观察到 ai,bin,又要求 ai×aj=bi+bj 的数量,那么显然有 ai×aj=bi+bj2n

并且显然有 aiaj 其中一个一定小于 2n。不妨枚举 iaj 的值 x,那么 bj 可以被表示为 ai×xbi

于是我们可以直接开一个桶记录满足 aj=xbj。其次为了避免计算重复,应当对 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;
}

作者:WaterSun

出处:https://www.cnblogs.com/WaterSun/p/18266720

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   WBIKPS  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示