385 字
2 分钟
CF1079 D 题解
D. Another Problem about Beautiful Pairs
题意
给定一个长度为 的数组 ,统计满足 的 数对的数量
题解
显然有枚举 的算法,然后 check 处是否满足 的值,复杂度
瓶颈显然在 较小的时候
不妨考虑根号分治,令
- 对于 的情况,直接枚举 即可,复杂度
由于 与 的情况不能被同时满足,不需要考虑重叠
- 对于 , 情况已经被前面统计,枚举 即可,复杂度
存在重复考虑的情况,只向一个方向统计
总复杂度 ,提交记录
34 collapsed lines
#include <bits/stdc++.h>using namespace std;using ll = long long;const int N = 2e5 + 5;ll n, a[N];void solve() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; int B = 1; while((B + 1) * (B + 1) <= n) B++;
ll res = 0; for(ll i = 1; i <= n; i++) { if(a[i] <= B) { for(ll j = 1; j <= B; j++) if(i + a[i] * j <= n && a[i + a[i] * j] == j) res++; } else { for(ll j = 1; j <= B + 1; j++) { if(i - a[i] * j > 0 && a[i - a[i] * j] == j) res++; if(i + a[i] * j <= n && a[i + a[i] * j] == j) res++; } } } cout << res << endl;}int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); // freopen("A.in", "r", stdin); // freopen("A.out", "w", stdout); int T = 1; cin >> T; while(T--) solve();} CF1079 D 题解
https://dk-qwq.github.io/blog/posts/cf2197d/