洛谷 P6819 PA2012 Binary Dodgeball
一道非常 educational 的题。
考虑移除两个一样的棋子和不移没有本质区别,可以发现若记 \(\operatorname{lowbit}(x) = 2^k\),则 \(x\) 的 sg 值为 \(k\)。
考虑 \(n\) 的答案即为 \(\oplus_i (\left\lfloor{\frac{n}{2^i}}\right\rfloor - \left\lfloor{\frac{n}{2^{i+1}}}\right\rfloor)i\),发现只与 \(\left\lfloor{\frac{n}{2^i}}\right\rfloor - \left\lfloor{\frac{n}{2^{i+1}}}\right\rfloor\) 的奇偶性有关,这个东西模 \(2\) 在二进制上的意义相当于第 \(i\) 位是否等于第 \(i+1\) 位。
据此随便做一个数位 \(dp\) 即可。时间复杂度 \(O(\log^3 n)\),随便过。
code
// Problem: P6819 [PA2012]Binary Dodgeball
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6819
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 70;
ll f[maxn][2][maxn][2], a[maxn];
ll dfs(int pos, bool limit, int sum, int lst) {
if (pos == -1) {
return (sum == 0);
}
if (f[pos][limit][sum][lst] != -1) {
return f[pos][limit][sum][lst];
}
ll ans = 0, up = (limit ? a[pos] : 1);
for (int i = 0; i <= up; ++i) {
ans += dfs(pos - 1, limit & (i == up), sum ^ ((i != lst) * pos), i);
}
return f[pos][limit][sum][lst] = ans;
}
ll calc(ll x) {
mems(f, -1);
int tot = -1;
while (x) {
a[++tot] = (x & 1);
x >>= 1;
}
return dfs(tot, 1, 0, 0) - 1;
}
void solve() {
int k;
scanf("%d", &k);
ll l = 1, r = 1e11, ans = -1;
while (l <= r) {
ll mid = (l + r) >> 1;
if (calc(mid) >= k) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号