版本比较
10.21提高组模拟赛
正文
增加 62 行删除 2 行
1233456789101112131415161718107 行未改动全部展开126127128129119130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179[https://share.note.youdao.com/s/ZfivG2hb] **(icebin的tj)**## 01 串 (binary)简单构造,思路详见icebin的tj和代码有个规律:
2 ^ 0 mod 3 = 1
2 ^ 1 mod 3 = 2
2 ^ 2 mod 3 = 1
2 ^ 3 mod 3 = 2...
发现2的次幕mod3是1,2,1,2交替,那么在二进制中相邻的两个1mod3=0,按照此规律构造即可。## 正确代码```cpp#include <bits/stdc++.h>using namespace std;107 行未改动全部展开 return 0;}```## T682365 括号序列(bracket)详见icbin的tj用栈记录左括号位置,每次与右括号配对,记录配对的左括号的下标与右括号下标,用a数组和b数组分记录以i结尾的数量,以i开头的数量,最后统计答案,因为前面的括号配对或后面的括号配对i都是这些的子串。## 正确代码```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e7 + 5, mod = 1e9 + 7;int ans[N], stk[N], up[N], m[N], n;int a[N], b[N], top; char s[N];
int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> s + 1; n = strlen(s + 1); for (int i = 1; i <= n; ++i) { if (s[i] == '(') { up[i] = stk[top]; stk[++top] = i; } else if (top) { m[stk[top]] = i; m[i] = stk[top]; top--; } } for (int i = 1; i <= n; ++i) { if (!m[i] || s[i] == '(') continue; b[i] = b[m[i] - 1] + 1; } for (int i = n; i >= 1; --i) { if (!m[i] || s[i] == ')') continue; a[i] = a[m[i] + 1] + 1; } for (int i = 1; i <= n; ++i) { if (!m[i] || s[i] == ')') continue; ans[i] = 1ll * a[i] * b[m[i]] % mod; if (up[i]) { ans[i] = (ans[i] + ans[up[i]]) % mod; } ans[m[i]] = ans[i]; } long long res = 0; for (int i = 1; i <= n; ++i) { res += 1ll * ans[i] * i % mod; } cout << res; return 0;}```