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.
For a fixed position , split the visible wizards into two groups: left-cape wizards at positions , and right-cape wizards at positions .
Let if wizard wears the cape on the left, and if he wears it on the right. Then is determined by prefix sums of and suffix sums of .
Look at instead of itself. Moving one step right only changes visibility because of wizard and wizard .
The magic equation is So every adjacent pair must satisfy
Once you choose , every other is forced. Try and , reject invalid binary sequences, then check the absolute value . That's it. The modulo is basically decorative.
Let describe wizard 's cape:
From position , Harry sees:
So
The important move: compare neighbors
Instead of trying to count from scratch, compare and .
When Harry moves from position to :
Everything else stays exactly the same. No hidden drama.
So:
Rearrange:
Therefore every adjacent pair must satisfy:
Since , the right side must be , , or . That means every difference must be , , or , otherwise the answer is instantly .
But we do not even need a separate check. The construction below naturally rejects bad values.
Why there are at most two answers
Once is chosen, the equation
forces
So there are only two possible starting choices:
Each one either generates a valid binary sequence or crashes into something outside .
That is the whole reason the count is tiny. The modulus looks scary, but the answer is never more than . Classic Codeforces smoke machine.
The final absolute check
The adjacent equations only guarantee the differences between consecutive values. They do not guarantee the actual starting value .
For a generated cape arrangement, let
be the number of left-cape wizards.
At position , Harry sees:
There are right-cape wizards, so the generated value is
The candidate arrangement is valid iff
If this holds, then all later values match too, because the adjacent differences were built to match.
Algorithm
For each test case:
Complexity is per test case and extra memory besides the input array.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int MOD = 676767677;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
int ans = 0;
for (int start = 0; start <= 1; start++) {
int cur = start;
int left_count = cur;
bool ok = true;
for (int i = 0; i + 1 < n; i++) {
int need_sum = a[i + 1] - a[i] + 1;
int nxt = need_sum - cur;
if (nxt < 0 || nxt > 1) {
ok = false;
break;
}
cur = nxt;
left_count += cur;
}
if (!ok) continue;
int generated_a1 = start + (n - left_count);
if (generated_a1 == a[0]) ans++;
}
cout << ans % MOD << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int MOD = 676767677;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
int ans = 0;
for (int start = 0; start <= 1; start++) {
int cur = start;
int left_count = cur;
bool ok = true;
for (int i = 0; i + 1 < n; i++) {
int need_sum = a[i + 1] - a[i] + 1;
int nxt = need_sum - cur;
if (nxt < 0 || nxt > 1) {
ok = false;
break;
}
cur = nxt;
left_count += cur;
}
if (!ok) continue;
int generated_a1 = start + (n - left_count);
if (generated_a1 == a[0]) ans++;
}
cout << ans % MOD << '\n';
}
}