CF1383E Strange Operation
B. CF1383E Strange Operation *2800
考虑一次操作的本质。
若为
直接对操作序列计数难以去重,因此一般均考虑对最终得到的
我们发现每次操作可以减少相邻两个
显然,一个
设原
这个有点困难,我们先观察一次操作对
发现
根据操作的具体内容,不难得到
容易判定
根据判定过程就可以 DP 了。设
两种转移方法,一是枚举下一个
因为贡献系数主要与
有
我们发现,对于
需要使用前缀和优化求出一段区间的
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
char s[N];
int n, lft, rt, sum, ans = 1;
int top, stc[N], pos[N], f[N];
int main() {
cin >> s + 1, n = strlen(s + 1);
lft = 1, rt = n;
while(lft <= n && s[lft] == '0') lft++;
while(rt && s[rt] == '0') rt--;
if(lft > rt) cout << n << endl, exit(0);
stc[0] = 1e9, pos[0] = f[1] = 1;
for(int i = lft, cnt = 2; i < rt; cnt++) {
int p = i + 1, L = 1;
while(s[p] == '0') p++, L++;
int res = 0, ht = 0, ps = cnt - 1;
while(L > stc[top]) {
res = (res + 1ll * (f[ps] - f[pos[top] - 1] + mod) * (L - ht)) % mod;
ht = stc[top], ps = pos[top--] - 1;
}
res = (res + 1ll * (f[ps] - f[pos[top] - 1] + mod) * (L - ht)) % mod;
ans = (ans + res) % mod;
f[cnt] = (res + f[cnt - 1]) % mod;
stc[++top] = L;
pos[top] = cnt;
i = p;
}
cout << 1ll * ans * lft % mod * (n - rt + 1) % mod << endl;
return 0;
}