题意:https://www.luogu.com.cn/problem/AT5799
题解:先把所有数减一。可以发现,把距离运算改成xor,就可以求出答案的奇偶性。
答案的奇偶性为(left(sum { {n-1}choose {i-1}} ight) mod 2)
如果是奇数,直接输出(1),否则考虑是(0)还是(2)。
如果序列存在(1),可以发现答案一定是(0)。证明可以使用数学归纳法,从最终状态往起始状态归纳,就能发现答案是(2)时原序列不存在(1)。
那么剩下的情况就是求一个(02)序列的答案。直接变成(01)序列求奇偶性就行了。
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e6 + 10;
int a[N], n;
char s[N];
int main() {
scanf("%d%s", &n, s); n --;
for(int i = 0; i <= n; i ++) {
a[i] = s[i] - '0' - 1;
}
int ans = 0;
for(int i = 0; i <= n; i ++) {
if((n & i) == i) ans ^= a[i] & 1;
}
if(ans == 1) puts("1");
else {
for(int i = 0; i <= n; i ++) {
if(a[i] == 1) { puts("0"); return 0; }
a[i] >>= 1;
}
ans = 0;
for(int i = 0; i <= n; i ++) {
if((n & i) == i) ans ^= a[i] & 1;
}
printf("%d
", ans ? 2 : 0);
}
return 0;
}