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.
Forget the values' order in the permutation for a second. Track where each value sits: = the position of value .
For mex to appear in some subarray, that subarray must contain all values and must avoid value .
Let be the smallest interval covering the positions of . Mex exists exactly when is outside this interval.
So means becomes a new minimum or new maximum among . If , it must land strictly inside the current min/max range.
Use insertion ranks. When inserting among the first positions, there are outside choices and inside choices. Also mex always exists, so must be .
Let be the position of value in the permutation.
For , a subarray has mex iff it contains every value and does not contain value .
Look at the minimum interval covering all positions of the smaller values:
Any subarray containing all values must contain the whole interval .
So:
Therefore, for :
Also, mex always exists: the full array contains all values , and value does not exist. So if , there are zero valid permutations. Mex is not taking requests.
Now we only need to count permutations of the sequence
with this prescribed inside/outside pattern.
Use insertion ranks. When we insert among the first positions, its rank can be any value from to . This online rank sequence is a bijection with permutations, so we can multiply choices independently.
For :
So if :
If any factor is , then . This happens, for example, when , because value cannot be inside the range of only . There is no room. Brutal, but clean.
In the easy version there are no question marks, so there is only one candidate string: . Thus the answer is:
We compute the product twice: once modulo to test divisibility, and once modulo for the output.
Complexity per test case is time and extra memory.
#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 ll MOD = 1000000007LL;
int T;
cin >> T;
while (T--) {
int n;
ll c;
string s;
cin >> n >> c >> s;
if (s.back() == '0') {
cout << -1 << '\n';
continue;
}
ll modC = 1 % c;
ll modAns = 1;
for (int i = 1; i <= n - 1; i++) {
ll ways = (s[i - 1] == '1' ? 2LL : (ll)i - 1);
modC = (modC * (ways % c)) % c;
modAns = (modAns * (ways % MOD)) % MOD;
}
if (modC == 0) cout << -1 << '\n';
else cout << modAns << '\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();
const ll MOD = 1000000007LL;
int T;
cin >> T;
while (T--) {
int n;
ll c;
string s;
cin >> n >> c >> s;
if (s.back() == '0') {
cout << -1 << '\n';
continue;
}
ll modC = 1 % c;
ll modAns = 1;
for (int i = 1; i <= n - 1; i++) {
ll ways = (s[i - 1] == '1' ? 2LL : (ll)i - 1);
modC = (modC * (ways % c)) % c;
modAns = (modAns * (ways % MOD)) % MOD;
}
if (modC == 0) cout << -1 << '\n';
else cout << modAns << '\n';
}
return 0;
}