题解:Luogu P12147 【MX-X11-T1】「蓬莱人形 Round 1」仅此而已,就已经足够了
题意
给定
题解
赛时被干废了。
令
接下来,考虑
那么我们每
的余项,它们的和即为
注意到由于低
时间复杂度
代码
#include <iostream>
#include <cmath>
using namespace std;
#define lowbit(x) ((x) & -(x))
#define chk_min(x, v) (x) = min((x), (v))
#define chk_max(x, v) (x) = max((x), (v))
typedef long long ll;
typedef pair<int, int> pii;
ll t, n, k, x, s1, s2;
ll ans;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> t;
while (t--) {
cin >> n >> k, x = n >> k, s1 = s2 = 0;
int b = log2(n);
if (k > b) cout << (ans = (1ll << k) * (n + 1)) << '\n';
else {
ans = (n ^ (n + (1 << k))) * ((n & ((1 << k) - 1)) + 1);
for (int i = k; i <= b; ++i) {
int c = (x - s2 - 1) >> (i - k + 1);
s1 += 1 << i, s2 += 1 << (i - k), ans += ((c + 1) * s1) << k;
}
cout << ans << '\n';
}
}
return 0;
}