正文
增加 90 行删除 96 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112```cpp#include<bits/stdc++.h>using namespace std;const int N = 2e6 + 7;int n;string s;void Main(){cin >> s;n = s.size();s = '0' + s;int p = 1, d = 0, x = N;bool flg = 0;for(int i = 1; i <= n; i++){for(int j = '0'; j <= '9'; j++){int y = (max(0, j - s[i - 1]) + (i == n ? 0 : max(0, s[i + 1] - j))) - ((max(0, s[i] - s[i - 1]) + (i == n ? 0 : max(0, s[i + 1] - s[i]))));if(y < x){p = i, d = j, x = y;flg = 1;}}}if(flg) s[p] = d;for(int i = 1; i <= s[1] - '0'; i++) cout << '(';cout << s[1];for(int i = 2; i <= n; i++){if(s[i] <= s[i - 1]){for(int j = 1; j <= s[i - 1] - s[i]; j++) cout << ')';cout << s[i];}else{for(int j = 1; j <= s[i] - s[i - 1]; j++) cout << '(';cout << s[i];}}for(int i = 1; i <= s[n] - '0'; i++) cout << ')';cout << '\n';}int main(){freopen("bracket.in", "r", stdin);freopen("bracket.out", "w", stdout);ios::sync_with_stdio(0);cin.tie(0);int Task;cin >> Task;while(Task--) Main();return 0;}``````cpp#include<bits/stdc++.h>using namespace std;#define ll long longconst int M = 26, V = 1e5 + 7, N = V << 1;int n;int m[2], q;ll p;ll a[2][M], b[2][M];int nxt[N][40];ll fpow(ll x, ll y){ll sum = 1;while(y){if(y & 1) sum = sum * x % p;x = x * x % p;y >>= 1;}return sum;}void Build(){for(int j = 1; j <= 30; j++){for(int i = 0; i < n; i++){nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];}}}int main(){freopen("poly.in", "r", stdin);freopen("poly.out", "w", stdout);ios::sync_with_stdio(0);cin.tie(0);cin >> m[0] >> m[1] >> q >> p;for(int j = 0; j < 2; j++){for(int i = 1; i <= m[j]; i++){cin >> a[j][i] >> b[j][i];}}for(int j = 0; j < 2; j++){for(int w = 0; w < p; w++){ll sum = 0;for(int i = 1; i <= m[j]; i++){sum = (sum + a[j][i] * fpow(w, b[j][i]) % p) % p;}nxt[w + j * p][0] = sum + (j ^ 1) * p;}}n = 2 * p;Build();while(q--){int x, y, s;cin >> x >> y >> s;x = x % p + s * p;for(int i = 30; i >= 0; i--){if(y >= (1 << i)){x = nxt[x][i];y -= (1 << i);}}printf("%d\n", x % p);}return 0;}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106```cpp#include<bits/stdc++.h>using namespace std;#define ll long longconst int N = 1e6 + 7;const ll inf = 2e15;int n, m;int a[N], b[N];ll c[N];int flg[N], vis[N];ll dep[N];int rt[N];int dfn[N], sz[N], cnt;struct Edge{int head[N], tot;void Clear(){memset(head, 0, sizeof(int) * (m + 5));tot = 0;}struct edge{int to, pre;}e[N << 1];void add(int x, int y){e[++tot] = {y, head[x]};head[x] = tot;}void dfs(int u){dfn[u] = ++cnt, sz[u] = 1;for(int i = head[u]; i; i = e[i].pre){int v = e[i].to;dep[v] = dep[u] + c[v];rt[v] = rt[u];dfs(v);sz[u] += sz[v];}}}E;void Clear(){E.Clear();for(int i = 1; i <= m; i++){flg[i] = vis[i] = dfn[i] = sz[i] = rt[i] = dep[i] = 0;}cnt = 0;}void Build(int u){vis[u] = 1;if(vis[b[u]] == 1){flg[u] = 1;}else{E.add(b[u], u);if(!vis[b[u]]) Build(b[u]);}vis[u] = -1;}bool in(int x, int y){return dfn[y] >= dfn[x] && dfn[y] <= dfn[x] + sz[x] - 1;}ll solve(int x, int y){if(rt[x] != rt[y]) return inf;else{ll res = inf;if(in(y, x)) res = min(res, dep[x] - dep[y]);if(in(y, b[rt[y]])) res = min(res, dep[x] + c[rt[y]] + (dep[b[rt[y]]] - dep[y]));return res;}}bool check(ll x){for(int i = 1, j = 1; i <= n; i++){while(j <= m && solve(a[i], j) > x) j++;if(j == m + 1) return false;}return true;}void Main(){Clear();cin >> n >> m;for(int i = 1; i <= n; i++) cin >> a[i];for(int i = 1; i <= m; i++) cin >> b[i];for(int i = 1; i <= m; i++) cin >> c[i];for(int i = 1; i <= m; i++){if(!vis[i]) Build(i);}for(int i = 1; i <= m; i++){if(flg[i]) rt[i] = i, E.dfs(i);}ll l = 0, r = inf;while(l <= r){ll mid = (l + r) >> 1;if(check(mid)) r = mid - 1;else l = mid + 1;}printf("%lld\n", (l < inf ? l : -1));}int main(){freopen("transform.in", "r", stdin);freopen("transform.out", "w", stdout);ios::sync_with_stdio(0);cin.tie(0);int Task;cin >> Task;while(Task--) Main();return 0;}```