题解:AT_abc384_f [ABC384F] Double Sum 2
[ABC384F] Double Sum 2 题解
solution
题目里的
可以发现,如果设
换个角度想,也就是说对于每个 map 记录每一个权值下的个数与权值和来做了。
code
与上文变量名不同,请注意分辨。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, a[N], ans;
unordered_map <int, int> cnt, mp;
signed main() {
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
int now = 0;
for (int i = 1; i <= n; ++i) now += a[i], ans += now + i * a[i];
for (int i = 2; i < 2e7; i <<= 1) {
int now = 0;
for (int j = 1; j <= n; ++j) {
++cnt[a[j] % i];
mp[a[j] % i] += a[j];
now += mp[(i - a[j] % i) % i] + a[j] * cnt[(i - a[j] % i) % i];
}
for (int j = 1; j <= n; ++j) {
--cnt[a[j] % i];
mp[a[j] % i] -= a[j];
}
ans -= now / i;
}
cout << ans;
}