PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: local_glue_76
Tester: raysh07
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
Bitwise operations, combinatorics
PROBLEM:
For N-bit integers, define f(x) = ((x \cdot 2^K) \bmod 2^N) \oplus \left\lfloor x / 2^K \right\rfloor.
Given N, K, M and Y (as binary strings), count the integers 0 \le x \le M with f(x) = Y, modulo 998244353.
EXPLANATION:
Let x_i denote the i-th bit of x, and similarly y_i for y.
Shifting left by K moves bit i-K to position i, and shifting right moves bit i+K to position i.
So, the condition f(x) = Y is just a set of N equations, i.e. we want y_i = x_{i-K} \oplus x_{i+K} for each 0 \le i \lt N.
Here, we assume x_i = 0 for i \lt 0 and i \ge N for convenience.
First, K = 0 is a special case: f(x) = x \oplus x = 0 for every x.
So the answer is M+1 if Y = 0, and 0 otherwise.
From now on, K \ge 1.
Each equation relates two bits whose positions differ by exactly 2K.
So, the bits split into independent “chains” by their remainder modulo 2K: for each 0 \le r \lt 2K, the chain r, r+2K, r+4K, \ldots
Let’s look at the equations involving a single chain b_0 = r, b_1 = r + 2K, b_2 = r + 4K, \ldots
Each equation involves a consecutive pair of indices in this chain; which means that as soon as x_{b_0} is chosen, everything else in the chain is uniquely determined.
Then, there are some boundary conditions:
- If b_0 - K \ge 0, the equation at i = b_0 - K has its other bit out of range, so it says x_{b_0} = y_{b_0 - K}.
Thus the chain is uniquely fixed. - Similarly, if b_t + K \lt N, the equation at i = b_t + K says x_{b_t} = y_{b_t + K}; again the chain is uniquely fixed.
Of course, it’s possible that both boundary conditions occur; in which case they must be compatible (i.e. fix the same set of values for the chain.)
If they are not compatible, no solution exists and the answer is 0.
Thus, every chain is either fixed, or has two possible solutions depending on what we choose x_{b_0} to be.
All that remains is to count solutions that are \le M.
This is fairly straightforward.
Let’s fix the highest bit where x and M differ, say bit b.
Then,
- At all bits higher than b, M matches x.
- At b, M must contain a 1 and x must contain a 0.
- Lower bits are “free”.
However, not all lower bits are free: since the bits \ge b are fixed, all the chains they belong to are fixed as well.
So,
- If the bits fixed by chains do not match the prefix of M, no solution exists where x and M differ at b.
- Otherwise, all lower bits belonging to a fixed chain are also fixed; so if there are k free chains that haven’t been touched at all (by either the prefix, or by being generally fixed by the initial constraints), each of them has two possibilities, so there are 2^k solutions here.
With a bit of simple bookkeeping this can be handled in constant time for a fixed bit b (or in \mathcal{O}(\log N) time if you don’t want to precompute powers of 2), leading to a rather fast solution.
TIME COMPLEXITY:
\mathcal{O}(N) or \mathcal{O}(N\log N) per testcase.
CODE:
Tester's code (C++)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
void Solve()
{
int n, k; cin >> n >> k;
string z; cin >> z;
string y; cin >> y;
vector <int> p2(n + 1);
p2[0] = 1;
for (int i = 1; i <= n; i++){
p2[i] = p2[i - 1] * 2 % mod;
}
if (k == 0){
bool all_zero = true;
for (auto ch : y) all_zero &= ch == '0';
if (all_zero){
int ans = 0;
for (int i = 0; i < n; i++) if (z[i] == '1'){
ans += p2[n - 1 - i];
ans %= mod;
}
ans += 1;
ans %= mod;
cout << ans << "\n";
} else {
cout << 0 << "\n";
}
return;
}
vector<int> offset(n);
for (int i = 0; i < min(n, 2 * k); i++){
int curr = 0;
for (int j = i + 2 * k; j < n; j += 2 * k){
curr ^= y[j - k] - '0';
offset[j] = curr;
}
}
vector <int> frc(n, -1);
bool bad = false;
auto force = [&](int i, int x){
x ^= offset[i];
i %= 2 * k;
if (frc[i] != -1 && frc[i] != x){
bad = true;
}
frc[i] = x;
};
for (int i = 0; i < n; i++){
bool l = ((i - k) >= 0);
bool r = ((i + k) < n);
if (l && r) continue;
if ((!l) && (!r)){
if (y[i] == '1'){
cout << 0 << "\n";
return;
}
continue;
}
if (l) force(i - k, y[i] - '0');
if (r) force(i + k, y[i] - '0');
}
if (bad){
cout << 0 << "\n";
return;
}
int ans = 0;
int und = 0;
for (int i = 0; i < min(n, 2 * k); i++) if (frc[i] == -1){
und++;
}
for (int i = 0; i <= n; i++){
if (i == n){
ans++;
ans %= mod;
continue;
}
if (i >= 2 * k){
frc[i] = frc[i % (2 * k)] ^ offset[i];
}
if (z[i] == '1'){
if (frc[i] == 0){
ans += p2[und];
ans %= mod;
break;
} else if (frc[i] == 1){
} else {
und--;
frc[i] = 1;
ans += p2[und];
ans %= mod;
}
} else {
if (frc[i] == 0){
} else if (frc[i] == 1){
break;
} else {
und--;
frc[i] = 0;
}
}
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}