Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Try to find something an operation cannot change. It flips two bits, so plain total parity is suspicious — but maybe not the whole story.
Positions and always have the same remainder modulo . So the operation never mixes different modulo- groups.
Split positions by their remainder modulo . A move never leaves its bucket, and inside a bucket it flips two bits. So for each residue , is invariant.
Inside one bucket, the positions form a chain: . If the number of ones in this chain is even, you can greedily kill them left-to-right: if current bit is , toggle it with the next position.
The answer is YES iff every modulo- bucket has an even number of ones: positions . In 0-based code, just do parity[i % k] ^= 1 for every s[i] == '1'.
A move chooses and . These positions have the same remainder modulo , so the string splits into independent chains:
for , ignoring terms greater than . No move ever mixes two chains. TV magic? Nope, just modular arithmetic.
Inside one chain, every move flips exactly two bits of that chain. Therefore the parity of the number of ones in the chain is invariant:
The all-zero string has for every . So if any residue class has an odd number of ones, the answer is immediately NO.
This is where people sometimes summon linear algebra. You can, but a greedy proof is cleaner and has fewer demons.
Take one chain:
Scan it left to right. For to :
After this, becomes and will never be touched again. At the end, only could be nonzero. But the parity of the chain never changed, and all previous bits are now zero, so the last bit equals the chain parity. If that parity was even, it is also .
So each chain can be killed independently iff its number of ones is even.
For each test case:
parity of size , initialized to .parity[i % k].YES; otherwise print NO.In 0-based indexing, positions connected by a move still differ by exactly , so they have the same value of .
For one test case, we scan the string and the buckets:
because . Over all tests:
time, with memory. Tiny. No drama.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> parity(k, 0);
for (int i = 0; i < n; ++i) {
if (s[i] == '1') parity[i % k] ^= 1;
}
bool ok = true;
for (int x : parity) {
if (x) {
ok = false;
break;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> parity(k, 0);
for (int i = 0; i < n; ++i) {
if (s[i] == '1') parity[i % k] ^= 1;
}
bool ok = true;
for (int x : parity) {
if (x) {
ok = false;
break;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}