题解:CF2189D1 Little String (Easy Version)
Notorious_ · · 题解
这题涉及了关于 mex 的非常重要的性质:
对于一个
这个性质很好证明,但如果从未接触过未必能在短时间内想到。
根据这个性质,可将题意转化为:
- 若
w_i=1 ,则i 为排列p 的一个前缀最小值或后缀最小值。 - 若
w_i=0 ,则i 一定不为排列p 的一个前缀最小值或后缀最小值。
不难发现,若
当填充到数字
- 若
w_i=1 ,则i 应填充至p_l 或p_r 。可用反证法:若i 没有被填充至p_l 或p_r ,那么在后续填充的过程中i-1,i-2...2,1,0 中的两个数必然会分别占据p_l 或p_r 。此时,i 的左端和右端都会存在比它小的数。这就与“i 为排列p 的一个前缀最小值或后缀最小值。”的要求不符。故i 必须填充至p_l 或p_r 中的一个,有{2 \choose 1}=2 中填法。 - 若
w_i=0 ,则i 应被填充至除去p_l 和p_r 的任意未被填充的格子中,有i-1 种填法。
综上,可用乘法原理求出符合要求的排列数。
const int mod=1e9+7;
void solve(){
int n,c;
cin>>n>>c;
string tep;
cin>>tep;
vector<char> s(n+5);
for(int i=1;i<=n;i++)s[i]=tep[i-1];
int res1=1,res2=1;
if(s[1]=='0'||s[n]=='0'){
cout<<-1<<endl;return;
}
int x=n-2;
for(int i=n-1;i;i--){
if(s[i]=='0'){
res1=(res1*x)%mod;
res2=(res2*x)%c;
}
else{
res1=(res1*2)%mod;
res2=(res2*2)%c;
}
x--;
}
if(res2==0)cout<<-1<<endl;
else cout<<res1<<endl;
}