solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = 1; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - 48; return f ? -x : x; } inline void write(int x) { if (x < 10) putchar(x + 48); else write(x / 10), putchar(x % 10 + 48); } inline void writeln(int x) { if (x < 0) x = -x, putchar('-'); write(x); putchar('\n'); } int q[510], dis[510][510], n, m; double c[510], v[510], ans; int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[i][j] = i == j ? 0 : n + 1; for (int i = 1; i <= m; i++) { int a = read(), b = read(); dis[a][b] = dis[b][a] = 1; } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); for (int i = 1; i <= n; i++) { double now = 0; for (int j = 0; j <= n - 1; j++) { memset(v, 0, sizeof v); double s = 0; int u = 0, tot = 0; for (int k = 1; k <= n; k++) if (dis[i][k] == j) { int sz = 0; u++; for (int l = 1; l <= n; l++) sz += dis[k][l] == 1; for (int l = 1; l <= n; l++) if (dis[k][l] == 1) v[l] += 1.0 / sz; } for (int k = 1; k <= n; k++) if (abs(dis[i][k] - j) < 2) q[++tot] = k; for (int k = 1; k <= n; k++) { double w = 0; for (int l = 1; l <= tot; l++) c[dis[k][q[l]]] = max(c[dis[k][q[l]]], v[q[l]]); for (int l = 1; l <= tot; l++) w += c[dis[k][q[l]]], c[dis[k][q[l]]] = 0; s = max(s, w); } if (u) s = max(s, 1.0); now += s / n; } ans = max(ans, now); } printf("%.10lf", ans); }
10
#include <bits/stdc++.h> using namespace std; const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int n; int ans = 0; const int N = 1123; int b[N]; char s[N]; int main() { int i, j; scanf("%d %s", &n, s); for (i = 0; i < n; i++) scanf("%d", &b[i]); map<char, int> mp; int tot = 0; int mx = 0; char cmx; for (i = 0; i < n / 2; i++) { if (s[i] != s[n - i - 1]) continue; mp[s[i]]++; ans += min(b[i], b[n - 1 - i]); mx = max<int>(mx, mp[s[i]]); if (mx == mp[s[i]]) cmx = s[i]; tot++; } if (mx > tot - mx) { mx -= tot - mx; vector<int> vs; for (i = 0; i < n / 2; i++) if (s[i] != s[n - i - 1] && s[i] != cmx && s[n - i - 1] != cmx) vs.push_back(min(b[i], b[n - i - 1])); sort(vs.begin(), vs.end()); for (i = 0; i < mx; i++) ans += vs[i]; } printf("%d\n", accumulate(b, b + n, 0) - ans); }
8
#include <bits/stdc++.h> using namespace std; int i, q1, q2; string str1, str2; int main() { cin >> str1 >> str2; for (i = 0; i < str1.size(); i++) { if (str1[i] == '1') q1++; } for (i = 0; i < str2.size(); i++) { if (str2[i] == '1') q2++; } if (q1 % 2 == 1) q1++; if (q1 >= q2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mx = 105; const long long maxn = 1000001; const long long mod = 1000000007; vector<vector<int>> adj; vector<int> res; vector<int> ord; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; while (t--) { long long n, k, i, j; cin >> n >> k; long long x[n], a, c[n]; for (i = 0; i < n; i++) cin >> x[i]; cin >> a; for (i = 0; i < n; i++) cin >> c[i]; set<long long> bo; long long cp = k, res = 0; int flag = 1; for (i = 0; i < n; i++) { bo.insert(c[i]); while (cp < x[i] && bo.size() > 0) { auto it = bo.begin(); res += *it; cp += a; bo.erase(it); } if (bo.size() == 0 && cp < x[i]) { flag = 0; break; } } if (!flag) { cout << "-1\n"; } else { cout << res << "\n"; } } }
3
#include <bits/stdc++.h> using namespace std; const int N = 200043; int lf[N]; int rg[N]; int n; int ans = 0; int p[N]; int q[N]; void update(int l, int r, int l2, int r2, int sum) { for (int i = l; i <= r; i++) { int o = sum - p[i]; if (o >= 1 && o <= n && l2 <= q[o] && q[o] <= r2) ans++; } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); q[p[i]] = i; } stack<pair<int, int> > s; s.push(make_pair(n + 1, -1)); for (int i = 0; i < n; i++) { while (s.top().first < p[i]) s.pop(); lf[i] = s.top().second; s.push(make_pair(p[i], i)); } while (!s.empty()) s.pop(); s.push(make_pair(n + 1, n)); for (int i = n - 1; i >= 0; i--) { while (s.top().first < p[i]) s.pop(); rg[i] = s.top().second; s.push(make_pair(p[i], i)); } for (int i = 0; i < n; i++) { int lenl = i - lf[i] - 1; int lenr = rg[i] - i - 1; if (lenl == 0 || lenr == 0) continue; update(lf[i] + 1, i - 1, i + 1, rg[i] - 1, p[i]); } cout << ans << endl; }
7
#include <bits/stdc++.h> using namespace std; int main() { int test; cin >> test; for (int i = 0; i < test; i++) { int boo = 0; int poo = 0; long w, h; cin >> w >> h; long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; long w2, h2; cin >> w2 >> h2; long mindist = 0; long maxmarginwidth = 0, maxmarginheight = 0; long w1 = x2 - x1; long h1 = y2 - y1; if (w1 + w2 > w & h1 + h2 > h) { mindist = -1; boo = 1; cout << fixed << setprecision(9) << mindist << endl; } else { if (w - x2 > x1) maxmarginwidth = w - x2; else maxmarginwidth = x1; if (h - y2 > y1) maxmarginheight = h - y2; else maxmarginheight = y1; } if ((maxmarginwidth >= w2 || maxmarginheight >= h2) && boo == 0) { mindist = 0; poo = 1; cout << fixed << setprecision(9) << mindist << endl; } if (std::min(w2 - maxmarginwidth, h2 - maxmarginheight) == w2 - maxmarginwidth && boo == 0 && poo == 0) { if (w1 + w2 > w) { mindist = h2 - maxmarginheight; } else { mindist = w2 - maxmarginwidth; } cout << fixed << setprecision(9) << mindist << endl; } else if (boo == 0 && poo == 0) { if (h1 + h2 > h) { mindist = w2 - maxmarginwidth; } else { mindist = h2 - maxmarginheight; } cout << fixed << setprecision(9) << mindist << endl; } } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 300010; int T; int n; int a[maxn], dis[maxn], mp[maxn]; vector<int> Q[maxn]; int main() { cin >> T; while (T--) { scanf("%d", &n); for (register int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (register int i = 1; i <= n; ++i) mp[i] = dis[i] = 0, Q[i].clear(); for (register int i = 1; i <= n; ++i) { dis[a[i]] = max(dis[a[i]], i - mp[a[i]]); mp[a[i]] = i; } for (register int i = 1; i <= n; ++i) dis[i] = max(dis[i], n + 1 - mp[i]); for (register int i = 1; i <= n; ++i) Q[dis[i]].push_back(i); int ans(0x7f7f7f7f); for (register int i = 1; i <= n; ++i) { for (register int j = 0; j < Q[i].size(); ++j) ans = min(ans, Q[i][j]); if (ans == 0x7f7f7f7f) printf("-1 "); else printf("%d ", ans); } puts(""); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; const long long INF = (1LL << 62) - 1; long long x[MAXN], p[MAXN], c[MAXN], id[MAXN]; long long pre[MAXN], dp[2][MAXN]; void solve(int n, int l, int r, int tl, int tr) { if (l > r) return; int m = (l + r) / 2, tm = tr; long long &res = dp[1][m]; for (int i = tl; i <= tr && i <= m; i++) { if (m - i > c[id[n]]) continue; long long tmp = dp[0][i] + pre[m] - pre[i]; if (res > tmp) res = tmp, tm = i; } solve(n, l, m - 1, tl, tm); solve(n, m + 1, r, tm, tr); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &x[i]); int sum = 0; for (int i = 1; i <= m; i++) scanf("%lld%lld", &p[i], &c[i]), sum += c[i], id[i] = i; if (sum < n) return 0 * printf("-1"); sort(x + 1, x + n + 1); sort(id + 1, id + m + 1, [](long long a, long long b) { return p[a] < p[b]; }); for (int j = 0; j <= n; j++) dp[1][j] = (j > 0) * INF; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) dp[0][j] = dp[1][j], dp[1][j] = INF; for (int j = 1; j <= n; j++) pre[j] = pre[j - 1] + abs(p[id[i]] - x[j]); solve(i, 0, n, 0, n); } return 0 * printf("%lld", dp[1][n]); }
9
#include <bits/stdc++.h> using namespace std; const int mx = 2e5 + 5; int ara[mx]; bool taken[mx]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> ara[i]; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq[3]; for (int i = 0; i < 2 * n; i++) { int x; cin >> x; x--; pq[x].push({ara[i % n], i % n}); } int t; cin >> t; while (t--) { int x; cin >> x; x--; while (!pq[x].empty() && taken[pq[x].top().second]) pq[x].pop(); if (pq[x].empty()) cout << -1; else { cout << pq[x].top().first; taken[pq[x].top().second] = 1; pq[x].pop(); } cout << " "; } cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; void afify() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long oo = (long long)1e13; const double EPS = 1e-4; int main() { afify(); char arr[] = {'a', 'b', 'c', 'd'}; int n; cin >> n; int j = 0; while (n--) { cout << arr[j]; j++; if (j == 4) { j = 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> re(n, 1000000000); vector<int> plus(n, 0); vector<int> A(m), B(m), C(m), D(m); for (int i = 0; i < m; i++) { cin >> A[i] >> B[i] >> C[i] >> D[i]; int a = A[i], b = B[i], c = C[i], d = D[i]; if (a == 1) { for (int k = b - 1; k < c; k++) { plus[k] += d; } } else { bool found = false; for (int k = b - 1; k < c; k++) { int most = d - plus[k]; if (most <= re[k]) { re[k] = most; found = true; } } if (!found) { cout << "NO"; return 0; } } } vector<int> p(n, 0); for (int i = 0; i < m; i++) { int a = A[i], b = B[i], c = C[i], d = D[i]; if (a == 1) { for (int k = b - 1; k < c; k++) { p[k] += d; } } else { bool found = false; for (int k = b - 1; k < c; k++) { int most = d - p[k]; if (most <= re[k]) { found = true; } } if (!found) { cout << "NO"; return 0; } } } cout << "YES" << endl; for (int i : re) { cout << i << " "; } }
4
#include <bits/stdc++.h> using namespace std; string wall[2]; int n, k; int bfs(pair<int, int> start) { vector<vector<int> > visited(n, vector<int>(2, -1)); for (int i = 1; i <= n; i++) { for (int c = 0; c < 2; c++) { if (wall[c][i] == 'X') { visited[i][c] = -2; } } } queue<pair<int, int> > q; visited[start.first][start.second] = 0; q.push(start); while (!q.empty()) { int x = q.front().first; int c = q.front().second; q.pop(); if (x + k >= n) { return true; } int water = visited[x][c]; if (x - 1 > water && visited[x - 1][c] == -1) { visited[x - 1][c] = visited[x][c] + 1; q.push(make_pair(x - 1, c)); } if (visited[x + 1][c] == -1) { visited[x + 1][c] = visited[x][c] + 1; q.push(make_pair(x + 1, c)); } if (visited[x + k][!c] == -1) { visited[x + k][!c] = visited[x][c] + 1; q.push(make_pair(x + k, !c)); } } } int main() { cin >> n >> k; cin >> wall[0] >> wall[1]; if (bfs(make_pair(0, 0)) == 1) { cout << "YES" << endl; } else cout << "NO" << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<string> answer = vector<string>(n, "red"); for (int i = 0; i < n; ++i) { string a; cin >> a; int sum = 0; int i0 = -1; for (int j = 0; j < a.size(); ++j) { int x = a[j] - '0'; sum += x; if (x == 0) i0 = j; } if (sum % 3 != 0 or i0 == -1) { answer[i] = "cyan"; } int ind = 0; for (int j = 0; j < a.size(); ++j) { int x = a[j] - '0'; if (x % 2 == 0 and j != i0) ind = 1; } if (ind == 0) answer[i] = "cyan"; } for (int i = 0; i < n; ++i) { cout << answer[i] << endl; } }
1
#include <bits/stdc++.h> using namespace std; string jiayi(string s) { int l = s.size(); while (s[l - 1] == 'z') { s[l - 1] = 'a'; l--; } s[l - 1] = s[l - 1] + 1; return s; } int main() { string a, b, c; while (cin >> a >> c) { string x = jiayi(a); if (x == c) { cout << "No such string" << endl; continue; } cout << x << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int ntest = 0, test = 0; inline void init(); inline void run(); inline void stop() { ntest = test - 1; } int main() { init(); while (++test <= ntest) { run(); } return 0; } const int INF = (int)1E9 + 5; const double EPS = 1E-11; const long long MOD = (long long)1E9 + 7; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; inline void init() { ntest = 1; } string s, t; int cnt[300]; char ret[5005]; bool doDfs(int level, bool larger) { if (level >= ((int)(s).size())) { if (larger) { for (int i = 0; i < (((int)(s).size())); i++) { printf("%c", ret[i]); } puts(""); return true; } return false; } int MIN = 'a'; if (!larger && ((int)(t).size()) > level) { MIN = t[level]; } for (int ch = (MIN); ch <= ('z'); ch++) { if (cnt[ch] <= 0) { continue; } cnt[ch]--; ret[level] = ch; if (!larger) { if (((int)(t).size()) > level) { if (doDfs(level + 1, ch > t[level])) { return true; } } else { if (doDfs(level + 1, true)) { return true; } } } else { if (doDfs(level + 1, true)) { return true; } } cnt[ch]++; } return false; } inline void run() { getline(cin, s); getline(cin, t); for (typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) { cnt[*it]++; } if (!doDfs(0, false)) { puts("-1"); } }
5
#include <bits/stdc++.h> using namespace std; int n, m; int a[257][257]; bool mx_row[257 * 257]; bool mx_col[257 * 257]; bool pushed[257][257]; int ans[257][257]; vector<pair<int, int> > ord; void input() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &a[i][j]); } } } void solve() { for (int i = 1; i <= n; ++i) { int aux = 0; for (int j = 1; j <= m; ++j) { aux = max(aux, a[i][j]); } mx_row[aux] = true; } for (int j = 1; j <= m; ++j) { int aux = 0; for (int i = 1; i <= n; ++i) { aux = max(aux, a[i][j]); } mx_col[aux] = true; } int x = n + 1, y = m + 1; for (int val = n * m; val >= 1; --val) { if (mx_row[val] == false && mx_col[val] == false) { continue; } if (mx_row[val] == true) { --x; } if (mx_col[val] == true) { --y; } ans[x][y] = val; ord.push_back({x, y}); } int tpsz = 0; queue<pair<int, int> > q; for (int i = n * m; i >= 1; --i) { if (mx_row[i] == true || mx_col[i] == true) { for (int hh = ord[tpsz].first + 1; hh <= n; ++hh) { if (ans[hh][ord[tpsz].second] > 0) { continue; } if (pushed[hh][ord[tpsz].second] == true) { continue; } pushed[hh][ord[tpsz].second] = true; q.push({hh, ord[tpsz].second}); } for (int hh = ord[tpsz].second + 1; hh <= m; ++hh) { if (ans[ord[tpsz].first][hh] > 0) { continue; } if (pushed[ord[tpsz].first][hh] == true) { continue; } pushed[ord[tpsz].first][hh] = true; q.push({ord[tpsz].first, hh}); } ++tpsz; } else { auto [nx, ny] = q.front(); q.pop(); ans[nx][ny] = i; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { printf("%d ", ans[i][j]); } printf("\n"); } } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; while (t--) { input(); solve(); } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 305; const int MOD = 1e9 + 7; int add(int x, int y) { x = x + y; return x >= MOD ? x - MOD : x; } int mul(int x, int y) { return (long long)x * y % MOD; } int dec(int x, int y) { x = x - y; return x < 0 ? x + MOD : x; } int Pow(int x, int y) { if (y == 1) return x; int lalal = Pow(x, y >> 1); lalal = mul(lalal, lalal); if (y & 1) lalal = mul(lalal, x); return lalal; } int JC[N], inv[N]; void Init(int n) { JC[0] = 1; for (int u = 1; u <= n; u++) JC[u] = mul(JC[u - 1], u); inv[n] = Pow(JC[n], MOD - 2); for (int u = n; u >= 1; u--) inv[u - 1] = mul(inv[u], u); } int C(int x, int y) { if (x < y) return 0; return mul(JC[x], mul(inv[y], inv[x - y])); } int solve(int x) { for (int u = 2; u * u <= x; u++) { while (x % (u * u) == 0) x = x / u / u; } return x; } int n, a[N]; int b[N], nn = 0; int f[N][N]; int main() { Init(300); scanf("%d", &n); for (int u = 1; u <= n; u++) { scanf("%d", &a[u]); a[u] = solve(a[u]); } sort(a + 1, a + 1 + n); for (int u = 1; u <= n; u++) { int i = u; while (a[i + 1] == a[u]) i++; b[++nn] = (i - u + 1); u = i; } memset(f, 0, sizeof(f)); int sum = b[1]; f[1][b[1] - 1] = 1; for (int u = 2; u <= nn; u++) { for (int j = 0; j <= sum - 1; j++) { if (f[u - 1][j] == 0) continue; for (int i = 1; i <= b[u]; i++) { for (int k = 0; k <= min(i, j); k++) { int l = (b[u] - 1) - (i - 1); int g = j - k + l; int h = C(b[u] - 1, i - 1); int jj = sum + 1 - j; h = mul(h, mul(C(jj, i - k), C(j, k))); f[u][g] = add(f[u][g], mul(h, f[u - 1][j])); } } } sum = sum + b[u]; } int ans = f[nn][0]; for (int u = 1; u <= nn; u++) ans = mul(ans, JC[b[u]]); printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T x, T y) { if (x < y) swap(x, y); while (y > 0) { T f = x % y; x = y; y = f; } return x; } const int maxn = 100007; const int maxslog = 18; const int maxs = 100007; const int treesize = 10000007; int n; int a[maxn]; int ls[treesize], rs[treesize], tr[maxn], treenode = 0; long long S[treesize]; void update(int& x, int fr, int to, int pos, int val) { if (!x) x = ++treenode; S[x] += val; if (fr == to) return; int m = (fr + to) / 2; if (pos <= m) update(ls[x], fr, m, pos, val); else update(rs[x], m + 1, to, pos, val); } long long ask(int x, int fr, int to, int a, int b) { if (!x || (fr == a && to == b)) return S[x]; int m = (fr + to) / 2; if (b <= m) return ask(ls[x], fr, m, a, b); if (a > m) return ask(rs[x], m + 1, to, a, b); return ask(ls[x], fr, m, a, m) + ask(rs[x], m + 1, to, m + 1, b); } void modify(int x, int y, int val) { if (x == 0 || y == n + 1) return; while (x < maxn) { update(tr[x], 1, n, y, val); x += x & -x; } } long long query(int x, int y) { if (x == 0 || y == n + 1) return 0; long long r = 0; while (x > 0) { r += ask(tr[x], 1, n, 1, y); x -= x & -x; } return r; } set<int> pos[maxn]; void insert_pos(int p, int val) { if (pos[val].empty()) { pos[val] = {0, n + 1}; modify(0, n + 1, n + 1 - (0)); } auto it = pos[val].upper_bound(p); int p_before = *prev(it); int p_after = *it; pos[val].insert(p); modify(p_before, p_after, -(p_after - (p_before))); modify(p_before, p, p - (p_before)); modify(p, p_after, p_after - (p)); } void remove_pos(int p, int val) { auto it = pos[val].find(p); if (it != pos[val].end()) { int p_before = *prev(it); int p_after = *next(it); modify(p_before, p, -(p - (p_before))); modify(p, p_after, -(p_after - (p))); pos[val].erase(p); modify(p_before, p_after, p_after - (p_before)); } } int main(int argc, char* argv[]) { std::cin.sync_with_stdio(false); std::cin.tie(nullptr); int m; cin >> n >> m; { for (int i = 1; i <= n; i++) { cin >> a[i]; insert_pos(i, a[i]); } for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; if (x == 1) remove_pos(y, a[y]), insert_pos(y, a[y] = z); else cout << query(n, z) - query(y - 1, z) << endl; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-12; inline int sgn(double u) { if (u > eps) return 1; else if (u < -eps) return -1; else return 0; } int n; map<pair<int, int>, int> m; pair<int, int> ord[200000 + 10]; int garx[200000 + 10], gary[200000 + 10], lx, ly; int cx[200000 + 10], cy[200000 + 10]; int main() { int ct = 0; ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; ; ord[i] = make_pair(x, y); m[ord[i]]++; garx[lx++] = ord[i].first; gary[ly++] = ord[i].second; } sort(garx, garx + lx); sort(gary, gary + ly); lx = unique(garx, garx + lx) - garx; ly = unique(gary, gary + ly) - gary; for (int i = 0; i < n; i++) { ord[i].first = lower_bound(garx, garx + lx, ord[i].first) - garx; ord[i].second = lower_bound(gary, gary + ly, ord[i].second) - gary; } for (int i = 0; i < n; i++) { cx[ord[i].first]++; cy[ord[i].second]++; } long long ans = 0; for (int i = 0; i < 200005; i++) { ans += (long long)(cx[i] * (long long)(cx[i] - 1)) / 2 + (long long)(cy[i] * (long long)(cy[i] - 1)) / 2; } for (__typeof((m).begin()) it = (m).begin(); it != (m).end(); it++) { long long u = it->second; ans -= u * (u - 1) / 2; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, l, k; cin >> n >> m >> l >> k; long long ans = (k + l) / m; if ((k + l) % m != 0) ans++; if (ans * m > n) cout << "-1"; else cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int Nn = 1e3 + 10; int a[Nn], n, b, c; int main() { int t; cin >> t; while (t--) { scanf("%d", &n); b = c = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) a[i] ? b++ : c++; if (c >= b) { printf("%d\n", n / 2); for (int i = 1; i <= n / 2; i++) printf("0 "); } else { if (c == 0) { printf("%d\n", n); for (int i = 1; i <= n; i++) printf("1 "); } else { if (b & 1) b--; printf("%d\n", b); for (int i = 1; i <= b; i++) printf("1 "); } } printf("\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; long long int fact[maxn * 2 + 1]; long long int ifact[maxn * 2 + 1]; int n, f; long long int fastex(long long int a, long long int b) { long long int ret = 1; while (b) { if (b & 1) { ret = (ret * a) % 1000000007; } a = (a * a) % 1000000007; b = b / 2; } return ret; } long long int ways(int n1, int k) { return (fact[n1 + k - 1] * ((ifact[n1 - 1] * ifact[k]) % 1000000007)) % 1000000007; } vector<long long int> primes; long long int inex() { long long int ret = 0; for (int i = 1; i < 1 << primes.size(); i++) { if (__builtin_popcount(i) % 2) { long long int fact = 1; for (int j = 0; j < primes.size(); j++) { if (i & (1 << j)) { fact *= primes[j]; } } if ((n / fact - f) >= 0) ret = (ret + ways(f, n / fact - f)) % 1000000007; } else { long long int fact = 1; for (int j = 0; j < primes.size(); j++) { if (i & (1 << j)) { fact *= primes[j]; } } if ((n / fact - f) >= 0) ret = (ret + 1000000007 - ways(f, n / fact - f)) % 1000000007; } } return ret % 1000000007; } int main() { int cases; cin >> cases; fact[0] = 1; ifact[0] = fastex(1, 1000000007 - 2); for (int i = 1; i <= maxn * 2; i++) { fact[i] = (fact[i - 1] * i) % 1000000007; ifact[i] = fastex(fact[i], 1000000007 - 2) % 1000000007; } while (cases--) { cin >> n >> f; int m = n; for (int i = 2; i <= m / i; i++) { if (m % i == 0) { while (m % i == 0) m /= i; primes.push_back(i); } } if (m != 1) primes.push_back(m); cout << (ways(f, n - f) + 1000000007 - inex()) % 1000000007 << endl; primes.clear(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct suffix_automaton { vector<map<char, long long> > edges; vector<long long> link; vector<long long> length; vector<long long> ways; vector<bool> kon; vector<long long> dp; long long last; suffix_automaton(string s) { edges.push_back({}); link.push_back(-1); length.push_back(0); last = 0; for (long long p, q, r, qq, i = 0; i < s.size(); i++) { edges.push_back({}); link.push_back(0); length.push_back(i + 1); r = edges.size() - 1; p = last; while (p >= 0 && edges[p].find(s[i]) == edges[p].end()) { edges[p][s[i]] = r; p = link[p]; } if (p >= 0) { q = edges[p][s[i]]; if (length[q] == length[p] + 1) { link[r] = q; } else { edges.push_back(edges[q]); link.push_back(link[q]); length.push_back(length[p] + 1); qq = edges.size() - 1; link[q] = qq; link[r] = qq; while (p >= 0 && edges[p][s[i]] == q) { edges[p][s[i]] = qq; p = link[p]; } } } last = r; } ways.resize(edges.size()); kon.resize(edges.size()); dp.resize(edges.size()); long long x = last; while (x >= 0) { kon[x] = true; x = link[x]; } for (long long i = 0; i < edges.size(); i++) { if (ways[i] == 0) { dfs(i); } } } void dfs(long long where) { ways[where] = kon[where]; for (pair<char, long long> i : edges[where]) { if (ways[i.second] == 0) { dfs(i.second); } ways[where] += ways[i.second]; dp[where] += dp[i.second]; } dp[where] += ways[where]; } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; long long k; cin >> s >> k; suffix_automaton p(s); k += p.ways[0]; long long where = 0; string res = ""; while (k) { bool czy = false; k -= p.ways[where]; if (k <= 0) break; for (pair<char, long long> i : p.edges[where]) { if (p.dp[i.second] >= k) { res.push_back(i.first); where = i.second; czy = true; break; } k -= p.dp[i.second]; } if (!czy) { cout << "No such line."; return 0; } } cout << res; return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T> inline T euclide(T a, T b, T& x, T& y) { if (a < 0) { T d = euclide(-a, b, x, y); x = -x; return d; } if (b < 0) { T d = euclide(a, -b, x, y); y = -y; return d; } if (b == 0) { x = 1; y = 0; return a; } else { T d = euclide(b, a % b, x, y); T t = x; x = y; y = t - (a / b) * y; return d; } } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } template <class T> T Abs(T x) { return x > 0 ? x : -x; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline bool isPrime(T n) { if (n <= 1) return false; for (T i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } template <class T> inline T Mod(T n, T m) { return (n % m + m) % m; } template <class T> string toString(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } long long toLl(string s) { long long r = 0; istringstream sin(s); sin >> r; return r; } template <class T> void debug(const T& e) { cout << e << endl; } template <class T1, class T2> void debug(const T1& e1, const T2& e2) { cout << e1 << "\t" << e2 << endl; } template <class T1, class T2, class T3> void debug(const T1& e1, const T2& e2, const T3& e3) { cout << e1 << "\t" << e2 << "\t" << e3 << endl; } template <class T1, class T2, class T3, class T4> void debug(const T1& e1, const T2& e2, const T3& e3, const T4& e4) { cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void debug(const T1& e1, const T2& e2, const T3& e3, const T4& e4, const T5& e5) { cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << endl; } template <class T> void debug(vector<T>& e) { int i; for (i = 0; i < (int)e.size(); i++) cout << e[i] << " "; cout << endl; } template <class T> void debug(vector<basic_string<T> >& e) { int i, j; for (i = 0; i < (int)e.size(); i++) { for (j = 0; j < (int)e[i].size(); j++) cout << e[i][j]; cout << endl; } cout << endl; } template <class T> void debug(vector<vector<T> >& e, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << e[i][j] << "\t"; cout << endl; } cout << endl; } template <class T> void debug(T e[100][100], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << e[i][j] << " "; cout << endl; } } long long Pow(int B, int P) { long long R = 1; while (P > 0) { if (P % 2 == 1) R = (R * B); P /= 2; B = (B * B); } return R; } int BigMod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return (int)R; } bool flag[110000]; vector<int> prime; void seive() { int i, r; for (int i = 3; i < 110000; i += 2) flag[i] = true; flag[2] = true; prime.push_back(2); for (int i = 3; i < 110000; i += 2) if (flag[i]) { prime.push_back(i); if (i <= 110000 / i) { r = i * 2; for (int j = i * i; j < 110000; j += r) flag[j] = false; } } } int lower_bound(int val) { int low = 0, high = prime.size() - 1; while (low < high) { int mid = (low + high) / 2; if (prime[mid] < val) low = mid + 1; else high = mid; } return prime[low]; } int mat[510][510]; int main() { int i, j, Case = 1, test, r, c, sum, minVal, val; seive(); while (scanf("%d %d", &r, &c) == 2) { for (i = 0; i < r; i++) for (j = 0; j < c; j++) { scanf("%d", &val); mat[i][j] = lower_bound(val) - val; } minVal = 1 << 28; for (i = 0; i < r; i++) { sum = 0; for (j = 0; j < c; j++) sum += mat[i][j]; minVal = min(minVal, sum); } for (j = 0; j < c; j++) { sum = 0; for (i = 0; i < r; i++) sum += mat[i][j]; minVal = min(minVal, sum); } printf("%d\n", minVal); } return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> a(n); for (auto &el : a) cin >> el; ; vector<vector<long long>> dp(n + 4, vector<long long>(3, 0)); long long ans = 0; for (auto u : a) { (dp[u][2] += dp[u][2] + dp[u + 2][0]) %= (long long)(998244353); if (u == 0) { (dp[0][1] += 1 + dp[0][1]) %= (long long)(998244353); } else if (u == 1) { (dp[1][1] += dp[0][1] + dp[1][1]) %= (long long)(998244353); (dp[1][0] += 1 + dp[1][0]) %= (long long)(998244353); } else { (dp[u][0] += dp[u - 2][1] + dp[u][0] + dp[u - 2][2]) %= (long long)(998244353); (dp[u][1] += dp[u - 1][1] + dp[u][1]) %= (long long)(998244353); } } for (long long i = 0; i <= n; i++) { (ans += dp[i][0] + dp[i][1] + dp[i][2]) %= (long long)(998244353); } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long testcases = 1; cin >> testcases; while (testcases--) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef long long Long; const Long MX = 2e5 + 10; Long solve(vector<pair<Long,Long>> &A, vector<pair<Long,Long>> &B){ Long ans = 0; Long pos = 0; multiset<Long> ms; for(Long i = 0; i < A.size(); i++){ while(pos < B.size() && B[pos].first <= A[i].first){ ms.insert(B[pos].second); pos++; } while(!ms.empty()){ auto it = ms.begin(); if((*it) >= A[i].first) break; ms.erase(ms.begin()); } if(ms.empty()) continue; ans = max(min(A[i].second, *ms.rbegin()) - A[i].first, ans); } return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout << fixed; Long n; cin >> n; vector<Long> A(n,0); vector<Long> B(n,0); for(Long i = 0; i < n; i++) cin >> A[i]; for(Long i = 0; i < n; i++) cin >> B[i]; Long ans = 0, mx = 0; for(Long i = 0; i < n; i++) ans += abs(A[i]-B[i]); vector<pair<Long,Long>> int1, int2; for(Long i = 0; i < n; i++){ if(A[i] < B[i]){ int1.push_back({A[i],B[i]}); }else{ int2.push_back({B[i],A[i]}); } } sort(int1.begin(),int1.end()); sort(int2.begin(),int2.end()); mx = max(mx,solve(int1,int2)); mx = max(mx,solve(int2,int1)); cout << ans - 2*mx << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, m, a, b, c; int v[505]; double d; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= m; i++) { cin >> a >> b >> c; d = max(d, 1.0 * ((double)(v[a] + v[b]) / c)); } cout << fixed << setprecision(10) << d << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; string ref = "RGB"; int dp[n + 1][3]; dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = 0; pair<int, int> parent[n + 1][3]; parent[0][0] = {-1, -1}; parent[0][1] = {-1, -1}; parent[0][2] = {-1, -1}; for (int i = 1; i < n + 1; ++i) { for (int j = 0; j < 3; j++) { int k1, k2; if (j == 0) { k1 = 1; k2 = 2; } if (j == 1) { k1 = 0; k2 = 2; } if (j == 2) { k1 = 0; k2 = 1; } if (dp[i - 1][k1] < dp[i - 1][k2]) { dp[i][j] = dp[i - 1][k1]; parent[i][j] = {i - 1, k1}; } else { dp[i][j] = dp[i - 1][k2]; parent[i][j] = {i - 1, k2}; } if (ref[j] != s[i - 1]) dp[i][j]++; } } int last; if (dp[n][0] <= dp[n][1] && dp[n][0] <= dp[n][2]) { last = 0; } else if (dp[n][1] <= dp[n][2]) { last = 1; } else { last = 2; } int u = last; string ans = ""; int ctr = n; while (ctr != 0) { ans = ref[last] + ans; last = parent[ctr][last].second; ctr--; } cout << dp[n][u] << endl; cout << ans; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; vector<int> e[N], v; bool respect[N], res[N]; bool dfs(int i) { int tmp = 0; for (auto j : e[i]) tmp += dfs(j); res[i] = (tmp == (int)e[i].size() && respect[i]); return respect[i]; } int main() { int n, r; cin >> n; for (int i = 1; i <= n; ++i) { int u; cin >> u >> respect[i]; if (u + 1) e[u].push_back(i); else r = i; } dfs(r); int cnt = 0; for (int i = 1; i <= n; ++i) { if (res[i]) { cout << i << ' '; ++cnt; } } if (!cnt) cout << -1; }
3
#include <bits/stdc++.h> using namespace std; const int a = 1 << 18; const int b = 1 << 19; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, t = 0; cin >> n >> x; if (n == 1) cout << "YES" << endl << x << endl; else if (n == 2) { if (x == 0) cout << "NO" << endl; else cout << "YES\n0 " << x << endl; } else { cout << "YES" << endl; for (int i = 0; i < n - 3; i++) { cout << i << " "; t ^= i; } cout << a << " " << b << " " << (a ^ b ^ t ^ x) << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> pr; long long cnt[11111111]; int lp[11111111]; int N = 10000000; bool check(long long a) { for (int i = 0; i < pr.size(); ++i) { if (!cnt[pr[i]]) break; long long kol = 0; long long T = a; do { kol += T / pr[i]; T /= pr[i]; } while (T >= pr[i]); if (kol < cnt[pr[i]]) return 0; } return 1; } int g[11111111]; int main() { for (int i = 2; i <= N; ++i) { if (!lp[i]) { lp[i] = i; pr.push_back(i); } long long c; for (int j = 0; j < int(pr.size()) && pr[j] <= lp[i] && (c = i * pr[j]) <= N; ++j) lp[c] = pr[j]; } int i, j, k, n; cin >> n; for (i = 0; i < n; ++i) { cin >> k; g[k + 1]--; } long long flag = n; for (i = 1; i <= N; ++i) { flag += g[i]; cnt[i] = flag; } for (i = N; i > 1; --i) { if (lp[i] != i) cnt[lp[i]] += cnt[i]; cnt[i / lp[i]] += cnt[i]; } long long mi, lo = 1, hi = 11000000000000LL; for (; lo < hi;) { mi = (lo + hi) / 2ll; if (check(mi)) hi = mi; else lo = mi + 1; } cout << lo; return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; const long long mod = 998244353; struct edge { int u, v, cost, cap; edge() {} edge(int _u, int _v, int _c, int _cap) : u(_u), v(_v), cost(_c), cap(_cap) {} int get(int t) { return t ^ u ^ v; } }; const int inf = 1e9; template <class edge, int inf> class edkarp { public: vector<edge> edges; vector<vector<int>> adj; int n; void init(int _n) { n = _n; adj.assign(n, vector<int>(0)); } inline int comp(int i) { return i ^ 1; } inline int par(int i, int edge_num) { return edges[edge_num].get(i); } void add(int u, int v, int co, int capac) { adj[u].emplace_back(edges.size()); edges.emplace_back(u, v, co, capac); adj[v].emplace_back(edges.size()); edges.emplace_back(v, u, -co, 0); } void shortest_path(int v, vector<int> &d, vector<int> &p) { p.assign(n, -1); d.assign(n, inf); d[v] = 0; bool change = false; for (int i = 0; i < n - 1; i++) { change = false; for (int j = 0; j < edges.size(); j++) { if (d[edges[j].u] == inf || edges[j].cap == 0) continue; if (d[edges[j].v] > d[edges[j].u] + edges[j].cost) { d[edges[j].v] = d[edges[j].u] + edges[j].cost; p[edges[j].v] = j; change = true; } } if (!change) break; } } int mincost(int st, int tar, int k = inf) { vector<int> p, d; int flow = 0, cost = 0; int nf; while (flow < k) { shortest_path(st, d, p); if (d[tar] == inf) break; nf = k - flow; int cr = tar; while (cr != st) { nf = min(nf, edges[p[cr]].cap); cr = par(cr, p[cr]); } flow += nf; cost += nf * d[tar]; cr = tar; while (cr != st) { edges[p[cr]].cap -= nf; edges[comp(p[cr])].cap += nf; cr = par(cr, p[cr]); } } return cost; } }; void prefix_func(string &s, vector<int> &v) { int n = s.size(); v.assign(n, 0); for (int i = 1; i < n; i++) { int j = v[i - 1]; while (j >= 0 && s[j] != s[i]) { j = j == 0 ? -1 : v[j - 1]; } v[i] = j + 1; } } int n, m, p[104], x, cr; string s, w[104]; vector<int> v; edkarp<edge, inf> fo; void solve() { cin >> n >> s; cin >> m; fo.init(n + 2); fo.add(0, 1, 0, 100); for (int i = 1; i <= n; i++) { fo.add(i, i + 1, 0, 100); } for (int i = 0; i < m; i++) { cin >> w[i] >> p[i]; prefix_func(w[i], v); cr = 0; for (int j = 0; j < n; j++) { while (cr >= 0 && s[j] != w[i][cr]) { cr = cr == 0 ? -1 : v[cr - 1]; } cr++; if (cr == (int)w[i].size()) { cr = v[cr - 1]; fo.add(j - (int)w[i].size() + 2, j + 2, -p[i], 1); } } } cin >> x; cout << -fo.mincost(0, n + 1, x) << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } }
8
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5, mod = 1e9 + 7; vector<char> s; long long X; void paste(long long p, long long l, long long cnt) { if (s.size() > X + 3 || l == 0) return; for (long long i = 0; i < cnt; ++i) for (long long j = 0; j < l; ++j) if (s.size() > X + 3) return; else s.push_back(s[p + j]); } long long md(long long a) { if (a < 0) return (a + mod * 2) % mod; else return a % mod; } void solve() { long long x; cin >> x; X = x; string str; cin >> str; s = vector<char>(str.begin(), str.end()); long long p = 0; long long cl = s.size(); while (p < x) { ++p; long long num = s[p - 1] - '0'; paste(p, s.size() - p, num - 1); cl = (cl + (num - 1) * md(cl - p)) % mod; } cout << cl << endl; return; } signed main() { ios::sync_with_stdio(false); long long t; cin >> t; for (long long i = 0; i < t; ++i) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, K[100000]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &K[i]); int ANS = 1000000000; for (int i = 1, sum; i <= n; i++) { sum = 0; for (int j = 1; j <= K[i]; j++) { int c; scanf("%d", &c); sum += c * 5 + 15; } ANS = min(ANS, sum); } printf("%d\n", ANS); return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long N = 2e+5 + 5; long long big = 1e+18 + 3; long long mod = 1e+9 + 7; long long dist[N]; bool vis[N]; vector<long long> gr[N], rgr[N]; map<long long, long long> freq; long long minv = 0; long long maxv = 0; long long n; void bfs(long long src) { for (long long i = 0; i <= n; i++) { dist[i] = 0; vis[i] = false; } queue<long long> nodes; vis[src] = true; nodes.push(src); while (!nodes.empty()) { long long cur = nodes.front(); nodes.pop(); for (auto it : rgr[cur]) { if (!vis[it]) { vis[it] = true; dist[it] = dist[cur] + 1; nodes.push(it); } } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long m; cin >> n >> m; for (long long i = 1; i <= m; i++) { long long u, v; cin >> u >> v; gr[u].push_back(v); rgr[v].push_back(u); } long long len; cin >> len; long long path[len + 1]; for (long long i = 1; i <= len; i++) cin >> path[i]; bfs(path[len]); for (long long i = 2; i <= len; i++) { if (dist[path[i]] >= dist[path[i - 1]]) { minv++; maxv++; continue; } for (auto it : gr[path[i - 1]]) if (it != path[i] && dist[it] == dist[path[i]]) { maxv++; break; } } cout << minv << " " << maxv << "\n"; }
4
#include <bits/stdc++.h> using namespace std; char m[15][20], s[20]; int a[500100], w[500100], p[15][1 << 15], d[1 << 15], v[15]; int main() { int i, j, k, l, t, n, f, q, vs, bv, bk, x, y; for (scanf("%d", &n), i = 0; i < n; scanf("%s", m[i]), i++) ; for (scanf("%d", &q), bk = -1, bv = 0, k = 0; k < q; k++) { for (scanf("%d", &l), i = 0; i < l; i++) for (scanf("%s", s), a[i] = -1, j = 0; j < n; j++) { for (t = 0; m[j][t] && s[t] && s[t] == m[j][t]; t++) ; if (!m[j][t] && !s[t]) a[i] = j; } for (w[l] = 0, i = 0; i < n; v[i] = 0, i++) for (p[i][0] = 0, j = 1; j < (1 << n); p[i][j] = 500100, j++) ; for (vs = 1, i = l - 1; i >= 0; i--) { w[i] = w[i + 1]; if (a[i] != -1 && v[a[i]] != vs) { w[i] |= 1 << a[i]; for (j = 0; j < (1 << n); d[j] = p[a[i]][j], j++) ; for (f = 0, j = 0; j < (1 << n); j++) if ((j >> a[i]) & 1) { y = j ^ (1 << a[i]); for (x = 0, t = 0; t < a[i]; x += (j >> t) & 1, t++) ; for (t = 0; t < n; t++) if (x + p[t][y] < d[j]) { d[j] = x + p[t][y]; f = 1; } } if (f) vs++; for (j = 0; j < (1 << n); p[a[i]][j] = d[j], j++) ; v[a[i]] = vs; } } if (w[0] != (1 << n) - 1) continue; for (j = 500100, i = 0; i < n; j = min(j, p[i][(1 << n) - 1]), i++) ; t = n * (n - 1) / 2 + 1 - j; if (t > bv) { bv = t; bk = k; } } if (bk == -1) printf("Brand new problem!\n"); else { for (printf("%d\n[:", bk + 1), i = 0; i < bv; printf("|"), i++) ; printf(":]\n"); } return 0; }
9
#include <bits/stdc++.h> using namespace std; template <typename T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <typename T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <typename T> T max(T a, T b, T c, T d) { return max(a, max(b, max(c, d))); } template <typename T> void read(vector<T> &arr, long long N) { arr.clear(); arr.resize(N); for (long long i = 0; i < N; i++) cin >> arr[i]; } template <typename T> void read(vector<pair<T, T>> &arr, long long N) { arr.clear(); arr.resize(N); for (long long i = 0; i < (long long)arr.size(); i++) cin >> arr[i].first >> arr[i].second; } template <typename T> void read(vector<vector<T>> &arr, long long N, long long M) { arr.clear(); arr.resize(N, vector<T>(M)); for (long long i = 0; i < N; i++) { for (long long j = 0; j < M; j++) cin >> arr[i][j]; } } template <typename T, size_t N> long long SIZE(const T (&t)[N]) { return N; } template <typename T> long long SIZE(const T &t) { return t.size(); } string to_string(const string s, long long x1 = 0, long long x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, long long x1 = 0, long long x2 = 1e9) { string t = ""; for (long long __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A(&v), long long x1 = 0, long long x2 = 1e9, C... coords); long long l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), long long x1, long long x2, C... coords) { long long rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void printm() { ; } template <typename Heads, typename... Tails> void printm(Heads H, Tails... T) { cout << to_string(H) << " | "; printm(T...); } bool lexi(string &s1, string &s2) { long long I = s1.size(), J = s2.size(), i = 0, j = 0; while (i < I and j < J) { if (s1[i] > s2[j]) return true; if (s1[i] < s2[j]) return false; i++; j++; } if (I >= J) return true; return false; } struct uf { vector<long long> par, size; void init(long long N) { par.resize(N, -1); size.resize(N, 1); } long long root(long long a) { if (par[a] == -1) return a; return par[a] = root(par[a]); } void unite(long long a, long long b) { a = root(a); b = root(b); if (a == b) return; if (size[a] < size[b]) { par[a] = b; size[b] += size[a]; } else { par[b] = a; size[a] += size[b]; } } bool same(long long a, long long b) { if (root(a) == root(b)) return true; return false; } }; struct seg_tree { vector<long long> make; long long siz; vector<long long> arr; void init(vector<long long> &a, bool max, bool range) { arr.clear(); make.clear(); arr = a; siz = arr.size(); make.resize(4 * siz); if (max) build_max(0, 0, siz - 1); else if (range) build_range(0, 0, siz - 1); } long long get_max(long long L, long long R) { return Get_max(0, 0, siz - 1, L, R); } void update_max(long long index, long long val) { Update_max(0, 0, siz - 1, index, val); return; } long long get_sum(long long L, long long R) { return Get_sum(0, 0, siz - 1, L, R); } void update_range(long long index, long long add) { Update_range(0, 0, siz - 1, index, add); return; } long long build_range(long long ind, long long L, long long R) { if (L == R) { make[ind] = arr[L]; return make[ind]; } else { long long mid = (L + R) / 2; long long a = build_range(2 * ind + 1, L, mid); long long b = build_range(2 * ind + 2, mid + 1, R); make[ind] = a + b; return make[ind]; } } long long Get_sum(long long ind, long long L, long long R, long long Left, long long Right) { if (L > Right or R < Left) return 0; if (Left <= L and R <= Right) return make[ind]; long long mid = (L + R) / 2; long long a = Get_sum(2 * ind + 1, L, mid, Left, Right); long long b = Get_sum(2 * ind + 2, mid + 1, R, Left, Right); return a + b; } void Update_range(long long ind, long long L, long long R, long long index, long long add) { if (L == R) { make[ind] = add; arr[index] = add; } else { long long mid = (L + R) / 2; if (L <= index and index <= R) { Update_range(2 * ind + 1, L, mid, index, add); } else { Update_range(2 * ind + 2, mid + 1, R, index, add); } make[ind] = make[2 * ind + 1] + make[2 * ind + 2]; } } long long build_max(long long ind, long long L, long long R) { if (L == R) { make[ind] = arr[L]; return make[ind]; } else { long long mid = (L + R) / 2; return make[ind] = max(build_max(2 * ind + 1, L, mid), build_max(2 * ind + 2, mid + 1, R)); } } long long Get_max(long long ind, long long L, long long R, long long Left, long long Right) { if (R < Left or L > Right) return -1e15; if (Left <= L and R <= Right) return make[ind]; long long mid = (L + R) / 2; return max(Get_max(2 * ind + 1, L, mid, Left, Right), Get_max(2 * ind + 2, mid + 1, R, Left, Right)); } long long Update_max(long long ind, long long L, long long R, long long index, long long val) { if (L == R) { arr[index] = val; make[ind] = val; return val; } else { long long mid = (L + R) / 2; if (L <= index and index <= mid) { make[ind] = Update_max(2 * ind + 1, L, mid, index, val); } else { make[ind] = Update_max(2 * ind + 2, mid + 1, R, index, val); } make[ind] = max(make[2 * ind + 1], make[2 * ind + 2]); return make[ind]; } } }; static bool comp(pair<long long, long long> &a, pair<long long, long long> &b) { if (a.first < b.first) return true; if (a.first == b.first) { if (a.second < b.second) return true; } return false; } static bool comp1(vector<long long> &a, vector<long long> &b) { if (a[0] < b[0]) return true; if (a[0] == b[0] and a[1] > b[1]) return true; return false; } long long Max = 1e17, Min = -1e15; long long N, M, K, D; long long mod = 1e9; long long P = 93; void solve() { cin >> N; vector<long long> arr(N); for (long long i = 0; i < N; i++) { cin >> arr[i]; --arr[i]; } vector<long long> cnt(N); seg_tree var; var.init(cnt, true, false); for (long long i = 0; i < N; i++) { long long val = var.get_max(0, arr[i]) + 1; var.update_max(arr[i], val); } long long ans = 0; for (auto it : var.arr) { ans = max(ans, it); } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); auto start_time = chrono::high_resolution_clock::now(); bool test = 0; if (!test) solve(); else { long long tt; cin >> tt; while (tt--) solve(); } auto end_time = chrono::high_resolution_clock::now(); return 0; }
3
#include <bits/stdc++.h> using namespace std; void init() {} void solve() { int n; cin >> n; vector<pair<int, int> > a(n); for (int _n(n), i(0); i < _n; i++) cin >> a[i].first, a[i].second = i; sort((a).begin(), (a).end()); string s(1000, '0'); vector<string> res(n); for (int _n(n), i(0); i < _n; i++) { int l = a[i].first; if (i > 0) { int ok = 0; for (int j = ((l > a[i - 1].first ? a[i - 1].first - 1 : l - 1)), _b = (0); j >= _b; --j) if (s[j] == '0') { for (int _n(l), k(j + 1); k < _n; k++) s[k] = '0'; s[j] = '1'; ok = 1; break; } if (!ok) { puts("NO"); return; } } res[a[i].second] = s.substr(0, l); } puts("YES"); for (int _n(n), i(0); i < _n; i++) cout << res[i] << endl; } int main() { init(); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long t = 1; void solve() { long long n, m, cod = 0, cev = 0, ans = 0; cin >> n >> m; set<pair<long long, long long> > so, se; map<long long, long long> mapa; vector<long long> v(n + 1, 0); vector<pair<long long, long long> > ex; for (long long i = 1; i < n + 1; i += 1) { cin >> v[i]; if (v[i] % 2 == 0) { if (mapa[v[i]] == 0) { cev++; se.insert(make_pair(v[i], i)); mapa[v[i]]++; } else { ex.push_back(make_pair(v[i], i)); } } else { if (mapa[v[i]] == 0) { cod++; so.insert(make_pair(v[i], i)); mapa[v[i]]++; } else { ex.push_back(make_pair(v[i], i)); } } } set<long long> soo, see; for (long long i = 1; i < min(m + 1, (long long)400005); i += 1) { if (mapa[i] == 0) { if (i % 2 == 0) see.insert(i); else soo.insert(i); } } long long ereq = max((long long)0, n / 2 - int((se).size())), oreq = max((long long)0, n / 2 - int((so).size())); if (ereq > int((see).size()) || int((soo).size()) < oreq) { cout << -1; return; } else { if (int((so).size()) > n / 2) { long long x = int((so).size()) - n / 2; for (long long i = 0; i < x; i += 1) { ex.push_back(*so.begin()); so.erase(so.begin()); } } if (int((se).size()) > n / 2) { long long x = int((se).size()) - n / 2; for (long long i = 0; i < x; i += 1) { ex.push_back(*se.begin()); se.erase(se.begin()); } } for (long long i = 0; i < int((ex).size()); i += 1) { if (ereq > 0) { v[ex[i].second] = *see.begin(); ereq--; ans++; see.erase(see.begin()); } else { v[ex[i].second] = *soo.begin(); oreq--; ans++; soo.erase(soo.begin()); } } } cout << ans << "\n"; for (long long i = 1; i < n + 1; i += 1) cout << v[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (t--) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long r, g, b, w; cin >> r >> g >> b >> w; long long od = r % 2 + b % 2 + g % 2 + w % 2; long long ev = 4 - od; if (od == 4 or ev == 4 or od == 1) cout << "Yes" << endl; else { if (w % 2 and ((r and g and b and ev == 1) or ev == 0)) cout << "Yes" << endl; else if (w % 2 == 0 and od == 3) cout << "Yes" << endl; else cout << "No" << endl; } } }
1
#include <bits/stdc++.h> using namespace std; vector<int> v; int a[15]; int b[15]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i] == b[j]) { v.push_back(a[i]); } } } for (int i = 0; i < v.size(); i++) { cout << v[i] << ' '; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct comp { bool operator()(array<long long, 3> a, array<long long, 3> b) { return a[0] == b[0] ? a[1] == b[1] ? a[2] > b[2] : a[1] < b[1] : a[0] > b[0]; } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, p; cin >> n >> p; long long t[n]; priority_queue<array<long long, 3>, vector<array<long long, 3>>, comp> pq; for (long long i = 0; i < n; ++i) { cin >> t[i]; pq.push({t[i], 0, i}); } set<long long> inq, want; long long qt = 0, cur = 0; long long ans[n]; while (pq.size()) { auto [time, tp, ind] = pq.top(); pq.pop(); qt = max(0LL, qt - (time - cur)); cur = time; if (tp == 0) { want.insert(ind); if (pq.size() == 0 || pq.top()[0] != time) if (inq.size() == 0 || *inq.begin() > *want.begin()) { inq.insert(*want.begin()); pq.push({time + qt + p, 1, *want.begin()}); want.erase(want.begin()); qt += p; } } else { ans[ind] = time; inq.erase(ind); if (want.size() && (pq.size() == 0 || pq.top()[0] != time)) if (inq.size() == 0 || *inq.begin() > *want.begin()) { inq.insert(*want.begin()); pq.push({time + qt + p, 1, *want.begin()}); want.erase(want.begin()); qt += p; } } } for (long long i = 0; i < n; ++i) { cout << ans[i] << " "; } cout << "\n"; }
7
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int month[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, 1}, {1, -1}, {-1, 1}}; const int MAXN = 1e5 + 5; const int INF = 1 << 30; const long long mod = 1e9 + 7; const double eps = 1e-8; const long long inff = 0x3f3f3f3f3f3f3f3f; int main() { int n, a[15]; memset(a, 0, sizeof(a)); string s; cin >> n >> s; for (int i = 0; i <= n - 1; ++i) { if (s[i] == 'L') { for (int j = 0; j <= 9; ++j) if (a[j] == 0) { a[j] = 1; break; } } else if (s[i] == 'R') { for (int j = 9; j >= 0; --j) if (a[j] == 0) { a[j] = 1; break; } } else { a[s[i] - '0'] = 0; } } for (int i = 0; i <= 9; ++i) cout << a[i]; cout << endl; }
0
#include <bits/stdc++.h> using namespace std; ifstream in("in.in"); ofstream out("out.out"); int t, n; char c[200005]; void solve() { int start, End, sol = 0; for (start = 1; start < n && c[start] == c[n]; start++) ; if (start == n) { cout << (n + 2) / 3 << "\n"; return; } for (End = n; End >= 1 && c[End] == c[1]; End--) ; sol += ((start - 1) + (n - End)) / 3; char val = c[start]; int cnt = 0; for (int i = start; i <= End; i++) { if (c[i] == val) { cnt++; } else { val = c[i]; sol += cnt / 3; cnt = 1; } } sol += cnt / 3; cout << sol << "\n"; } int main() { cin >> t; while (t--) { cin >> n >> c + 1; solve(); } }
4
#include <bits/stdc++.h> using namespace std; const long long INF = 1 << 30; const long long LINF = 1LL << 62; const double EPS = 1e-9; const long long MOD = 1e9 + 7; int main() { long long aa = 0, ab = 0, ba = 0, bb = 0; cin >> aa >> ab >> ba >> bb; long long xdif = ba - aa + 1; long long ydif = (bb - ab + 2) / 2; long long res = xdif * ydif; if (xdif % 2) res -= xdif / 2; cout << res << ("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 1005; int n, k; long long int a[maxn], dp[maxn][maxn] = {0}; long long int answer(int x) { dp[0][0] = 1; int li = 0; for (int i = 1; i <= n; i++) { dp[i][0] = 1; while (a[li] + x <= a[i]) li++; if (li != 0) li--; for (int j = 1; j <= i && j <= k; j++) { dp[i][j] = dp[i - 1][j] + dp[li][j - 1]; dp[i][j] %= mod; } } return dp[n][k]; } int main() { long long int ans = 0; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i * (k - 1) <= a[n]; i++) { ans += answer(i); ans %= mod; } printf("%I64d", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int freq[10][10]; int need[10][10]; int main() { cin.sync_with_stdio(0), cin.tie(0); string s; cin >> s; for (int i = 1; i < (int)s.size(); ++i) { ++freq[s[i - 1] - '0'][s[i] - '0']; } for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { memset(need, 63, sizeof(need)); for (int k = 0; k < 10; ++k) { for (int a = 0; a < 10; ++a) { for (int b = (a == 0); b < 10; ++b) { int digit = (k + a * i + b * j) % 10; need[k][digit] = min(need[k][digit], a + b - 1); } } } int ans = 0; for (int a = 0; a < 10; ++a) { for (int b = 0; b < 10; ++b) { if (freq[a][b] > 0 && need[a][b] == inf) { ans = -1; goto out; } ans += freq[a][b] * need[a][b]; } } out: cout << ans << " \n"[j == 9]; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int cnt[100 + 2][N + 2]; string s[100 + 2]; set<int> on[100 + 2], of[100 + 2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> s[i]; s[i] = "#" + s[i] + "#"; for (int j = 1; j <= m; j++) { if (s[i][j] == '.') of[i].insert(j); else on[i].insert(j); } on[i].insert(0); on[i].insert(m + 1); of[i].insert(-1); of[i].insert(m + 2); } long long ans = 0; int a = 1, b = 1, d = 1; while (true) { if (clock() > CLOCKS_PER_SEC * 0.8) cout << "Never\n", exit(0); if (a == n) break; if (d) { int cr = *on[a].lower_bound(b); int nx = *of[a + 1].lower_bound(b); if (nx < cr) { ans += nx - b + 1; a++; b = nx; } else { ans += cr - b; b = cr - 1; d ^= 1; if (s[a][cr] == '+') { on[a].erase(cr); s[a][cr] = '.'; of[a].insert(cr); } } } else { auto it1 = on[a].upper_bound(b); auto it2 = of[a + 1].upper_bound(b); it1--, it2--; int cr = *it1, nx = *it2; if (nx > cr) { ans += b - nx + 1; a++; b = nx; } else { ans += b - cr; b = cr + 1; d ^= 1; if (s[a][cr] == '+') { on[a].erase(cr); s[a][cr] = '.'; of[a].insert(cr); } } } } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int S[200005]; int G[200005]; int ats[200005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> S[i] >> G[i], ats[i] = S[i] + G[i]; for (int i = 2; i <= n; i++) ats[i] = min(ats[i], ats[i - 1] + 1); for (int i = n - 1; i >= 1; i--) ats[i] = min(ats[i], ats[i + 1] + 1); long long sum = 0; for (int i = 1; i <= n; i++) if (ats[i] < S[i]) return cout << -1, 0; else sum += ats[i] - S[i]; cout << sum << "\n"; for (int i = 1; i <= n; i++) cout << ats[i] << " "; }
5
#include <bits/stdc++.h> using namespace std; struct pp { int a, b; }; pp num[25]; int cmp(pp a, pp b) { return a.b > b.b; } int main() { int i, j, m, n, sum = 0, k; scanf("%d%d", &m, &n); for (i = 0; i < n; i++) { scanf("%d%d", &num[i].a, &num[i].b); } i = 0; sort(num, num + n, cmp); while (1) { if (m > num[i].a) { m -= num[i].a; sum += num[i].a * num[i].b; i++; } else { sum += m * num[i].b; i++; break; } if (i >= n) break; } printf("%d", sum); }
0
#include <bits/stdc++.h> int main() { char huruf[100010]; int panjang, temp = 0, jawab = 0, hasil, counter = 0, temp2 = 0; scanf("%s", huruf); getchar(); panjang = strlen(huruf); for (int i = 0; i < panjang; i++) { if (temp == 0 && huruf[i] == ')' || temp == 0 && huruf[i] == '#') { temp = 0; break; } if (huruf[i] == '(') { temp += 1; temp2 += 1; counter += 1; } else if (huruf[i] == '#') { jawab += 1; temp2 -= 1; counter = 0; } else { temp -= 1; temp2 -= 1; counter -= 1; } if (temp2 < 0) { temp = 0; break; } if (huruf[panjang - 1] == '(') { temp = 0; break; } } if (temp == 0 || counter > 0) { printf("-1"); } else { for (int i = 0; i < jawab; i++) { if (i == jawab - 1) { printf("%d", temp); } else { printf("1\n"); temp--; } } } }
3
#include <bits/stdc++.h> using namespace std; const int MAX_N = int(1e5) + 10; const long long INF = 1LL << 60; int n, nQ, v[MAX_N], c[MAX_N]; struct Data { long long memo[MAX_N]; int mx, secondMx; void __init(int n) { fill(memo, memo + n, -INF); mx = 0; secondMx = 1; } long long operator[](int idx) { return memo[idx]; } void update(int idx, long long maximum) { if (maximum <= memo[idx]) return; memo[idx] = maximum; if (idx == mx) return; if (idx == secondMx) { if (memo[secondMx] > memo[mx]) swap(mx, secondMx); return; } if (memo[idx] > memo[secondMx]) { secondMx = idx; if (memo[secondMx] > memo[mx]) swap(mx, secondMx); } } long long curColorIsMaxColorToo(int idx) { if (idx == mx) return memo[secondMx]; else return memo[mx]; } } data; int main() { cin >> n >> nQ; for (__typeof((n)-1) i = (0); i <= ((n)-1); ++i) cin >> v[i]; for (__typeof((n)-1) i = (0); i <= ((n)-1); ++i) cin >> c[i], --c[i]; for (__typeof((nQ)-1) i = (0); i <= ((nQ)-1); ++i) { long long a, b; cin >> a >> b; data.__init(n); long long ans = 0LL; for (__typeof((n)-1) j = (0); j <= ((n)-1); ++j) { int col = c[j]; int val = v[j]; long long same = -INF; if (data[col] != -INF) same = data[col] + val * a; long long other = max(data.curColorIsMaxColorToo(col), 0LL) + val * b; long long best = max(same, other); data.update(col, best); ans = max(ans, best); } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> int _, n, m, ans1, ans2, a[200005], sum[200005]; bool vis[200005]; int main() { int i; for (scanf("%d", &_); _; _--) { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) { vis[i] = bool(a[i] > a[i - 1] && a[i] > a[i + 1]); sum[i] = sum[i - 1] + vis[i]; } ans1 = -1; ans2 = 0; for (i = m; i <= n; i++) if (sum[i] - sum[i - m] - vis[i - m + 1] - vis[i] > ans1) ans1 = sum[i] - sum[i - m] - vis[i - m + 1] - vis[i], ans2 = i - m + 1; printf("%d %d\n", ans1 + 1, ans2); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long modi(long long int sum) { int temp = sum, sum1 = 0; while (temp != 0) { sum1 += (temp % 10); temp /= 10; } return sum1; } int main() { long long int n, i, sum = 0, x, counter = 1; string s; cin >> s; if (s.size() == 1) { cout << "0" << endl; } else { for (i = 0; i < s.size(); i++) { x = s[i] - 48; sum += x; } while (sum >= 10) { sum = modi(sum); counter++; } cout << counter << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int INF = (int)2e9; const long long INFL = (long long)9e18; const int MAXINT = ((~0) ^ (1 << 31)); const long long MAXLL = ((~0) ^ ((long long)1 << 63)); template <class T> inline T pow2(T a) { return a * a; } template <class T> inline bool mineq(T& a, T b) { return (a > b) ? (a = b, true) : false; } template <class T> inline bool maxeq(T& a, T b) { return (a < b) ? (a = b, true) : false; } const int maxn = (int)1e6 + 10; ; int n, T; long double c; long double mean[maxn]; long long num[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> T >> c; for (int i = 1; i < (int)(n + 1); i++) { cin >> num[i]; mean[i] = (mean[i - 1] + (long double)num[i] / T) / c; num[i] += num[i - 1]; } cout << setprecision(10) << fixed; int q; cin >> q; for (int i = 0; i < (int)(q); i++) { int p; cin >> p; long double real = (long double)(num[p] - num[p - T]) / T; long double app = mean[p]; cout << real << " " << app << " " << abs(real - app) / real << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> d(n); for (int i = 0; i < n; i++) { int t; cin >> t; pair<int, int> tmp = make_pair(t, i + 1); d[i] = tmp; } sort(d.begin(), d.end()); for (int i = 0; i < n; i++) { int p1 = 0; int p2 = n - 1; while (p1 != p2) { if (i == p1) { p1++; } else if (i == p2) { p2--; } else { if (d[p1].first + d[p2].first == d[i].first) { cout << d[i].second << " " << d[p2].second << " " << d[p1].second << endl; return 0; } else if (d[p1].first + d[p2].first < d[i].first) { p1++; } else { p2--; } } } } cout << -1 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 100; vector<int> in[MAXN]; int out[MAXN], dp[MAXN]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; in[y].push_back(x); out[x]++; } set<pair<int, int>> st; int s, t; cin >> s >> t; s--; t--; st.insert({0, t}); memset(dp, 63, sizeof dp); dp[t] = 0; while (st.size()) { int v = st.begin()->second; st.erase(st.begin()); for (auto u : in[v]) { out[u]--; if ((dp[v] + bool(out[u])) < dp[u]) { st.erase({dp[u], u}); dp[u] = (dp[v] + bool(out[u])); st.insert({dp[u], u}); } } } cout << (dp[s] > n ? -1 : dp[s]) << '\n'; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int N, flag = 0; char s[maxn]; int main() { scanf("%d", &N); getchar(); for (int i = 1; i <= N; i++) scanf("%c", &s[i]); for (int i = 1; i < N; i++) { if (s[i] > s[i + 1]) { flag = 1; s[i] = '~'; break; } } if (flag) { for (int i = 1; i <= N; i++) { if (s[i] != '~') printf("%c", s[i]); } } else { for (int i = 1; i < N; i++) { printf("%c", s[i]); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; bitset<4096> s[100020]; int a[13][100020]; vector<pair<int, int> > tmp; int main() { int n, k, q, cnt; scanf("%d%d%d", &n, &k, &q), cnt = k; for (int i = 1; i <= k; i++) { for (int j = 0; j < 1 << k; j++) if (j & (1 << i - 1)) s[i][j] = 1; for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); } while (q--) { int op, A, B; scanf("%d%d%d", &op, &A, &B), tmp.clear(); if (op == 1) s[++cnt] = s[A] | s[B]; else if (op == 2) s[++cnt] = s[A] & s[B]; else { for (int i = 1; i <= k; i++) tmp.push_back(pair<int, int>(-a[i][B], i)); sort(tmp.begin(), tmp.end()); int cur = 0; for (auto v : tmp) { cur |= 1 << v.second - 1; if (s[A][cur]) { printf("%d\n", -v.first); break; } } } } }
10
#include <bits/stdc++.h> using namespace std; inline void boost() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); } const long long maxn = 2e5 + 123; const long long inf = 1e9 + 123; const long long linf = 1e18 + 123; const long long mod = 1e9 + 7; const double eps = 1e-9; const double pi = acos(-1); int dx[4] = {0, 1, -1, 0}; int dy[4] = {1, 0, 0, -1}; map<string, long long> m; pair<long long, string> a[maxn]; int main() { boost(); int n; cin >> n; for (int i = 0; i < n; i++) { set<char> s; cin >> a[i].first >> a[i].second; for (int j = 0; j < a[i].second.size(); j++) { s.insert(a[i].second[j]); } string ss = ""; for (set<char>::iterator it = s.begin(); it != s.end(); it++) { ss += (*it); } if (m[ss] == 0) { m[ss] = a[i].first; } else { m[ss] = min(m[ss], a[i].first); } } long long mn = linf; if (m["A"] && m["B"] && m["C"]) { mn = min(mn, m["A"] + m["B"] + m["C"]); } if (m["A"] && m["BC"]) { mn = min(mn, m["A"] + m["BC"]); } if (m["B"] && m["AC"]) { mn = min(mn, m["B"] + m["AC"]); } if (m["C"] && m["AB"]) { mn = min(mn, m["C"] + m["AB"]); } if (m["AB"] && m["BC"]) { mn = min(mn, m["AB"] + m["BC"]); } if (m["AB"] && m["AC"]) { mn = min(mn, m["AB"] + m["AC"]); } if (m["BC"] && m["AC"]) { mn = min(mn, m["BC"] + m["AC"]); } if (m["ABC"]) { mn = min(mn, m["ABC"]); } if (mn == linf) { cout << "-1"; return 0; } cout << mn; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, i, a, b; cin >> t; while (t--) { cin >> n; bool f = false; vector<long long int> p(n), c(n); for (i = 0; i < n; i++) { cin >> a >> b; p[i] = a, c[i] = b; } for (i = 1; i < n; i++) { if (p[i] < p[i - 1]) { f = true; cout << "NO" << "\n"; break; } if (c[i] < c[i - 1]) { f = true; cout << "NO" << "\n"; break; } } if (!f) { for (i = 0; i < n; i++) { if (c[i] > p[i]) { f = true; cout << "NO" << "\n"; break; } } } if (!f) { for (i = 0; i < n - 1; i++) { if (c[i + 1] - c[i] > p[i + 1] - p[i]) { f = true; cout << "NO" << "\n"; break; } } } if (!f) cout << "YES" << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6; int n; char s[maxn]; int arr[maxn]; int k = 1; void Reverse(char *s, int n) { for (int i = 0, j = n - 1; i < j; i++, j--) { char c = s[i]; s[i] = s[j]; s[j] = c; } } int main() { cin >> n >> s; for (int i = 1; i <= n; i++) { if (n % i == 0) { arr[k] = i; k++; } } k--; for (int i = 1; i <= n; i++) { Reverse(s, arr[i]); } cout << s << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> c(n); for (int i = 0; i < n; i++) cin >> c[i]; sort(c.begin(), c.end()); int m = c[0]; for (int i = 1; i < n; i++) { if (c[i] - c[i - 1] > m) m = c[i] - c[i - 1]; } cout << m; cout << '\n'; } return 0; }
1
#pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("sse4.2") #include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <bitset> #include <sstream> #include <deque> #include <queue> #include <complex> #include <random> #include <cassert> #include <chrono> #include <functional> using namespace std; #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FIXED cout << fixed << setprecision(12) #define ll long long #define ld long double #define pii pair<int, int> #define pll pair<ll, ll> #define graph vector<vector<int>> #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define f first #define s second #define hashmap unordered_map #define hashset unordered_set #define eps 1e-9 #define mod 1000000007 #define inf 3000000000000000007ll #define sz(a) signed((a).size()) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #ifdef DEBUG mt19937 gen(857204); #else mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif void flush() { cout << flush; } void flushln() { cout << endl; } void println() { cout << '\n'; } template<class T> void print(const T &x) { cout << x; } template<class T> void read(T &x) { cin >> x; } template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } template<class T, class ...U> void print(const T &x, const U& ... u) { print(x); print(u...); } template<class T, class ...U> void println(const T &x, const U& ... u) { print(x); println(u...); } #ifdef DEBUG template<class T> string pdbg(const T &x) { stringstream ss; ss << x; return ss.str(); } template<class T, class U> string pdbg(const pair<T, U> &p) { return "{" + pdbg(p.f) + "," + pdbg(p.s) + "}"; } string pdbg(const string &s) { return "\"" + s + "\""; } template<class It> string pdbg(It begin, It end, string d); template<class T> string pdbg(const vector<T> &a) { return pdbg(all(a), ""); } template<class T> string pdbg(const vector<vector<T>> &a) { return pdbg(all(a), "\n"); } template<class T> string pdbg(const vector<vector<vector<T>>> &a) { return pdbg(all(a), "\n\n"); } template<class T> string pdbg(const set<T> &a) { return pdbg(all(a), ""); } template<class T> string pdbg(const hashset<T> &a) { return pdbg(all(a), ""); } template<class T, class U> string pdbg(const map<T, U> &a) { return pdbg(all(a), ""); } template<class T, class U> string pdbg(const hashmap<T, U> &a) { return pdbg(all(a), ""); } template<class It> string pdbg(It begin, It end, string d) { string ans; ans += "{"; if (begin != end) ans += pdbg(*begin++); while (begin != end) ans += "," + d + pdbg(*begin++); ans += "}"; return ans; } template<class T> string dbgout(const T &x) { return pdbg(x); } template<class T, class... U> string dbgout(T const &t, const U &... u) { string ans = pdbg(t); ans += ", "; ans += dbgout(u...); return ans; } #define dbg(...) print("[", #__VA_ARGS__, "] = ", dbgout(__VA_ARGS__)), flushln() #define msg(...) print("[", __VA_ARGS__, "]"), flushln() #else #define dbg(...) 0 #define msg(...) 0 #endif template<class T, class U> inline bool chmin(T &x, const U& y) { return y < x ? x = y, 1 : 0; } template<class T, class U> inline bool chmax(T &x, const U& y) { return y > x ? x = y, 1 : 0; } template<class T> inline void sort(T &a) { sort(all(a)); } template<class T> inline void rsort(T &a) { sort(rall(a)); } template<class T> inline void reverse(T &a) { reverse(all(a)); } template<class T, class U> inline istream& operator>>(istream& str, pair<T, U> &p) { return str >> p.f >> p.s; } template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; } template<class T> inline T sorted(T a) { sort(a); return a; } signed main() { FAST; FIXED; int n, m; read(n, m); vector<int> cnt(n); int sq = sqrt(n); for (int t = 0; t < m; ++t) { string s; read(s); vector<int> ind(n); iota(all(ind), 0); sort(all(ind), [&](int i, int j) { return cnt[i] > cnt[j]; }); if (cnt[ind[0]] <= t * 0.6) { println(gen() % 2); cout << flush; } else if (n >= 2 && cnt[ind[0]] > cnt[ind[1]] + 1) { println(s[ind[0]]); cout << flush; } else { vector<char> st; for (int i = 0; i < n && cnt[ind[i]] >= cnt[ind[0]] * 0.9; ++i) { st.pb(s[ind[i]]); if (i + 1 < n && cnt[ind[i]] > cnt[ind[i + 1]] + 1) break; } println(st[gen() % sz(st)]); cout << flush; } char c; read(c); for (int i = 0; i < n; ++i) cnt[i] += c == s[i]; } #ifdef DEBUG cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl; #endif return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int MAXN = (int)1e6 + 6; int T; int main() { cin >> T; while (T--) { int n; cin >> n; unordered_map<char, int> mp; mp.clear(); for (int i = 0; i < n; ++i) { string s; cin >> s; for (int l = 0; l < s.length(); ++l) ++mp[s[l]]; } if (n == 1) { cout << "YES\n"; continue; } int ok = 1; for (auto it = mp.begin(); it != mp.end(); ++it) { if ((int)it->second % n != 0) ok = 0; } if (ok) cout << "YES\n"; else cout << "NO\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; #pragma gcc optimize("ofast"); #pragma g++ optimize("ofast"); const int maxn = 1e2 + 2; int n, b, a; vector<int> r[maxn], res; set<int> s; vector<int> f(vector<int> v1, vector<int> v2) { vector<int> v3; for (int i = int(0); i < int(v1.size()); i++) for (int j = int(0); j < int(v2.size()); j++) if (v1[i] == v2[j]) v3.push_back(v1[i]); return v3; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = int(0); i < int(n); i++) { cin >> b; for (int j = int(0); j < int(b); j++) { cin >> a; r[i].push_back(a); } if (i > 0) res = f(res, r[i]); else res = r[0]; } for (int i = int(0); i < int(res.size()); i++) s.insert(res[i]); set<int>::iterator it; for (it = s.begin(); it != s.end(); ++it) cout << *it << '\t'; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } long long Max(long long a, long long b) { return a > b ? a : b; } long long Min(long long a, long long b) { return a < b ? a : b; } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = ((res % p) * (x % p)) % p; y = y >> 1; x = ((x % p) * (x % p)) % p; } return res; } bool isprime(long long n) { long long z = sqrt(n); if (n == 2) return true; if (n % 2 == 0) return false; for (long long i = 3; i <= z; i += 2) { if (n % i == 0) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long w, h, n; cin >> w >> h >> n; set<long long> hor, ver; map<long long, long long> hord, verd; ver.insert(0); ver.insert(w); hor.insert(0); hor.insert(h); hord[w] = 1; verd[h] = 1; for (long long i = 0; i < n; i++) { char c; long long z; cin >> c >> z; if (c == 'H') { set<long long>::iterator in = hor.lower_bound(z); long long hup = *in; long long hdown = *(--in); hor.insert(z); long long d = abs(hup - hdown); verd[d]--; verd[(abs(hup - z))]++; verd[(abs(hdown - z))]++; if (verd[d] == 0) verd.erase(d); verd.erase(0); } else { set<long long>::iterator in = ver.lower_bound(z); long long vright = *in; long long vleft = *(--in); ver.insert(z); long long d = abs(vright - vleft); hord[d]--; hord[(abs(vright - z))]++; hord[(abs(vleft - z))]++; if (hord[d] == 0) hord.erase(d); hord.erase(0); } cout << ((hord.rbegin())->first) * ((verd.rbegin())->first) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << -1 << endl; return 0; } cout << n << " " << n + 1 << " " << n * n + n << endl; return 0; }
3
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int main() { int NS, NM, NL, NXL, NXXL, k; cin >> NS >> NM >> NL >> NXL >> NXXL >> k; string s; for (int i = 0; i < k; i++) { cin >> s; if (s == "S") { if (NS > 0) { cout << "S"; NS--; } else if (NM > 0) { cout << "M"; NM--; } else if (NL > 0) { cout << "L"; NL--; } else if (NXL > 0) { cout << "XL"; NXL--; } else if (NXXL > 0) { cout << "XXL"; NXXL--; } } else if (s == "M") { if (NM > 0) { cout << "M"; NM--; } else if (NL > 0) { cout << "L"; NL--; } else if (NS > 0) { cout << "S"; NS--; } else if (NXL > 0) { cout << "XL"; NXL--; } else if (NXXL > 0) { cout << "XXL"; NXXL--; } } else if (s == "L") { if (NL > 0) { cout << "L"; NL--; } else if (NXL > 0) { cout << "XL"; NXL--; } else if (NM > 0) { cout << "M"; NM--; } else if (NXXL > 0) { cout << "XXL"; NXXL--; } else if (NS > 0) { cout << "S"; NS--; } } else if (s == "XL") { if (NXL > 0) { cout << "XL"; NXL--; } else if (NXXL > 0) { cout << "XXL"; NXXL--; } else if (NL > 0) { cout << "L"; NL--; } else if (NM > 0) { cout << "M"; NM--; } else if (NS > 0) { cout << "S"; NS--; } } else if (s == "XXL") { if (NXXL > 0) { cout << "XXL"; NXXL--; } else if (NXL > 0) { cout << "XL"; NXL--; } else if (NL > 0) { cout << "L"; NL--; } else if (NM > 0) { cout << "M"; NM--; } else if (NS > 0) { cout << "S"; NS--; } } cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int Set(int mask, int pos) { return mask = mask | (1 << pos); } bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); } int arr[100005], rangeMin[100005]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int test, n; cin >> test; while (test--) { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; rangeMin[i] = 2147483647; } rangeMin[n] = 2147483647; for (int i = n - 1; i >= 0; i--) { rangeMin[i] = min(rangeMin[i + 1], arr[i]); } int maxD = 0; bool found = false; for (int i = n - 1; i >= 0; i--) { if (arr[i] >= rangeMin[i]) { found = true; int d = arr[i] - rangeMin[i]; maxD = max(maxD, d); } } if (!found) { cout << 0 << endl; } else { int cnt = 0; while (maxD) { cnt++; maxD /= 2; } cout << cnt << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline long long input(void) { char t; long long x = 0; int neg = 0; t = getchar(); while ((t < 48 || t > 57) && t != '-') t = getchar(); if (t == '-') { neg = 1; t = getchar(); } while (t >= 48 && t <= 57) { x = (x << 3) + (x << 1) + t - 48; t = getchar(); } if (neg) x = -x; return x; } inline void output(long long x) { char a[20]; int i = 0, j; a[0] = '0'; if (x < 0) { putchar('-'); x = -x; } if (x == 0) putchar('0'); while (x) { a[i++] = x % 10 + 48; x /= 10; } for (j = i - 1; j >= 0; j--) { putchar(a[j]); } putchar('\n'); } int main() { long long n, m, len, i, map[26]; char s[200010], a[2], b[2]; n = input(); m = input(); scanf("%s", s); for (i = 0; i < 26; i++) map[i] = i; while (m--) { scanf("%s", a); scanf("%s", b); for (i = 0; i < 26; i++) { if (map[i] == (a[0] - 'a')) map[i] = b[0] - 'a'; else if (map[i] == (b[0] - 'a')) map[i] = a[0] - 'a'; } } len = strlen(s); for (i = 0; i < len; i++) s[i] = map[s[i] - 'a'] + 'a'; printf("%s\n", s); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = int(1e5 + 3); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; long long countf = 0, countb = 0, pos[N]; cin >> n; for (int i = 0; i < n; i++) { int num; cin >> num; pos[num] = i + 1; } cin >> m; for (int i = 0; i < m; i++) { int a; cin >> a; countf += pos[a]; countb += n - pos[a] + 1; } cout << countf << " " << countb << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { int n, diff = 0; cin >> n; vector<int> arr, arr2; arr.resize(n); arr2.resize(n); for (int i = 0; i < n; i++) { int t; cin >> t; arr[i] = t; arr2[i] = t; } sort(arr2.begin(), arr2.end()); for (int i = 0; i < n; i++) { if (arr[i] != arr2[i]) diff++; } if (diff <= 2) { cout << "YES"; } else { cout << "NO"; } return 0; }
2
#include <bits/stdc++.h> const int fin = 0, maxn = 100010; int n, m; struct rec { int x, y; } a[maxn], b[maxn]; inline int cmp(const rec &a, const rec &b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } int main() { if (fin) { freopen("t.in", "r", stdin); freopen("t.out", "w", stdout); } int i, j, az, bz, ar, br, ans1, ans2; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d%d", &a[i].y, &a[i].x); for (i = 1; i <= m; ++i) scanf("%d%d", &b[i].y, &b[i].x); std::sort(a + 1, a + n + 1, cmp); std::sort(b + 1, b + m + 1, cmp); az = 1; bz = 1; ans1 = 0; ans2 = 0; while (az <= n) { ar = az; while (ar < n && a[az].x == a[ar + 1].x) ++ar; while (bz <= m && b[bz].x < a[az].x) ++bz; if (bz <= m && b[bz].x == a[az].x) { br = bz; while (br < m && b[bz].x == b[br + 1].x) ++br; if (br - bz < ar - az) ans1 += br - bz + 1; else ans1 += ar - az + 1; j = bz; for (i = az; i <= ar; ++i) { while (j <= br && b[j].y < a[i].y) ++j; if (j <= br && a[i].y == b[j].y) { ++ans2; ++j; } } bz = br + 1; } az = ar + 1; } printf("%d %d\n", ans1, ans2); if (fin) { fclose(stdin); fclose(stdout); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, u, r, d[33], F[33][33]; int a[33], b[33], k[33], p[33]; long long res = -1000000000000000LL; scanf("%d%d%d", &n, &u, &r); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) scanf("%d", &k[i]); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) F[0][i] = a[i]; d[0] = 0; if (u % 2 == 0) { long long s = 0; for (int i = 1; i <= n; i++) s += (long long)k[i] * F[0][i]; if (s > res) res = s; } for (int j = 1; j <= u; j++) { d[j] = 0; for (int i = 1; i <= n; i++) F[j][i] = F[j - 1][p[i]] + r; if ((u - j) % 2 == 0) { long long s = 0; for (int i = 1; i <= n; i++) s += (long long)k[i] * F[j][i]; if (s > res) res = s; } } bool stop; while (1) { stop = true; for (int j = u; j > 0; j--) if (d[j] == 0 && d[j - 1] == 0) { d[j] = 1; for (int i = 1; i <= n; i++) F[j][i] = F[j - 1][i] ^ b[i]; if ((u - j) % 2 == 0) { long long s = 0; for (int i = 1; i <= n; i++) s += (long long)k[i] * F[j][i]; if (s > res) res = s; } for (int t = j + 1; t <= u; t++) { d[t] = 0; for (int i = 1; i <= n; i++) F[t][i] = F[t - 1][p[i]] + r; if ((u - t) % 2 == 0) { long long s = 0; for (int i = 1; i <= n; i++) s += (long long)k[i] * F[t][i]; if (s > res) res = s; } } stop = false; break; } if (stop) break; } printf("%I64d\n", res); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, nr; int main() { cin >> a >> b >> c >> d; nr = abs(a * d - b * c); double dr = max(max(abs(a + b + c + d), abs(a - b + c - d)), max(abs(a - b - c + d), abs(a + b - c - d))); printf("%.9f", dr ? nr / dr : 0); }
6
#include <bits/stdc++.h> using namespace std; int main() { long long int y1, y2, y3, m1, m2, m3, c1, c2, c3; cin >> y1 >> y2 >> y3; cin >> m1 >> m2 >> m3; cin >> c1 >> c2 >> c3; long maxx = 1000000; for (int i = y1; i <= 1000; i++) for (int j = y2; j <= 200; j++) for (int k = y3; k <= 100; k++) { int kt = 0, t1 = i, t2 = k, t3 = m1, t4 = m3, dem = 0; while (true) { if (j <= t4) break; if (m2 > t2) { t1 = t1 + t2 - m2; } if (j > t4) { t3 = t3 + t4 - j; } if (t1 > 0 && t3 <= 0) { kt = 1; break; } if (t1 < 0) break; } if (kt == 1) { long tam = (i - y1) * c1 + (j - y2) * c2 + (k - y3) * c3; if (maxx > tam) { maxx = tam; } } } cout << maxx << endl; }
5
#include <bits/stdc++.h> using namespace std; int a[(1 << 22) + 10], n, m, pad; bool vis[(1 << 22) + 10], has[(1 << 22) + 10]; void dfs(int x) { vis[x] = 1; if (has[x]) { int rx = (~x) ^ pad; if (!vis[rx]) dfs(rx); } for (int i = 0; i < n; ++i) { if ((x >> i) & 1) { int nx = x ^ (1 << i); if (!vis[nx]) dfs(nx); } } } int main() { scanf("%d%d", &n, &m); pad = ~((1 << n) - 1); for (int i = 0; i < m; ++i) scanf("%d", a + i), has[a[i]] = 1; int ans = 0; for (int i = 0; i < m; ++i) { if (!vis[a[i]]) { ans++; dfs((~a[i]) ^ pad); } } printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20, sgn[] = {1, -1}; int n, m, tot, h[N], d[N], a[N], b[N], out[N], vis[N]; struct edge { int t, n; } e[N << 1]; inline void input() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", a + i, b + i); } inline void add_edge(int x, int y) { e[++m] = (edge){y, h[x]}, h[x] = m; e[++m] = (edge){x, h[y]}, h[y] = m; } inline void dfs(int u) { for (int &i = h[u], p, v; v = e[p = i].t, i; i = e[i].n) if (!vis[i + 1 >> 1]) vis[i + 1 >> 1] = 1, dfs(v), out[++tot] = p - sgn[p & 1], out[++tot] = p; } inline int check(int x) { memset(h, 0, x + 1 << 2), memset(vis, 0, n + 1 << 2), tot = m = 0; for (int i = 1; i <= n; i++) d[a[i] &= x] ^= 1, d[b[i] &= x] ^= 1, add_edge(a[i], b[i]); for (int i = 0; i <= x; i++) if (d[i]) return memset(d, 0, x + 1 << 2), 0; return dfs(a[1]), tot == n * 2; } inline void work() { for (int i = 20; i >= 0; i--) if (check((1 << i) - 1)) { cout << i << '\n'; for (int j = 1; j <= tot; j++) cout << out[j] << ' '; return; } } int main() { input(); work(); return 0; }
8
#include <bits/stdc++.h> int c[6]; long long solvesystem(int x, int y, int a1, int a2, int b1, int b2, int c1plus, int c1minus, int c2plus, int c2minus) { int A, B; if (a1 == 0) { B = x / b1; A = (y - b2 * B) / a2; } else if (a2 == 0) { B = y / b2; A = (x - b1 * B) / a1; } else if (b1 == 0) { A = x / a1; B = (y - A * a2) / b2; } else { A = y / a2; B = (x - a1 * A) / b1; } long long res = 0LL; if (A > 0) res = res + (long long)c1plus * A; else res = res - (long long)c1minus * A; if (B > 0) res = res + (long long)c2plus * B; else res = res - (long long)c2minus * B; return res; } int main() { int t; scanf("%d", &t); while (t--) { int x, y; scanf("%d%d", &x, &y); for (int i = 0; i < 6; ++i) scanf("%d", &c[i]); long long res = std::min( std::min(solvesystem(x, y, 1, 1, 0, 1, c[0], c[3], c[1], c[4]), solvesystem(x, y, 1, 1, -1, 0, c[0], c[3], c[2], c[5])), solvesystem(x, y, 0, 1, -1, 0, c[1], c[4], c[2], c[5])); printf("%lld\n", res); } return 0; }
5
#include <bits/stdc++.h> using namespace std; map<char, int> mp; int main() { int n, k, j = 0; cin >> n >> k; string s; cin >> s; int answer = 0; for (int i = 0; i < n; i++) { mp[s[i]]++; while (min(mp['a'], mp['b']) > k) { mp[s[j]]--; j++; } answer = max(answer, i - j + 1); } cout << answer << endl; }
3
#include <bits/stdc++.h> const int N = 1 << 16, M = 1000000007; using namespace std; int n, m, sum[503], u[16][4], re, tp, f[170][N], cnt; char S[503]; inline int sig(int x, bool d) { int y = 0; for (int i = 0; i < n; i++) y |= (u[i][x] ^ d) << i; return y; } inline void fmt(int* c, int t, bool d) { for (int i = 1; i < 1 << n; i <<= 1) for (int j = 0; j < 1 << n; j++) if (d ^ !(j & i)) c[j] = (c[j] + t * c[j ^ i]) % M; } inline void mul(int* a, int* b, int* c, bool d) { fmt(a, 1, d), fmt(b, 1, d), fmt(c, 1, d); for (int i = 0; i < 1 << n; i++) c[i] = (c[i] + a[i] * 1ll * b[i]) % M; fmt(a, -1, d), fmt(b, -1, d), fmt(c, -1, d); } inline int cal(int l, int r) { int no = cnt++; if (l == r) { if (S[l] ^ 63) { if (S[l] >= 97) f[no][sig(S[l] - 97, 1)] = 1; else f[no][sig(S[l] - 65, 0)] = 1; } else for (int i = 0; i < 8; i++) f[no][sig(i >> 1, i & 1)]++; } else { int a, b, mi; for (mi = l; sum[mi++] ^ sum[l - 1];) ; a = cal(l + 1, mi - 2), b = cal(mi + 2, r - 1); if (S[mi] ^ 124) mul(f[a], f[b], f[no], 0); if (S[mi] ^ 38) mul(f[a], f[b], f[no], 1); } return no; } int main() { scanf("%s%d", S + 1, &n); m = strlen(S + 1); for (int i = 1; i <= m; i++) sum[i] = sum[i - 1] + (S[i] ^ 40 ? S[i] ^ 41 ? 0 : -1 : 1); for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) scanf("%d", u[i] + j); scanf("%d", &tp), re |= tp << i; } printf("%d", (f[cal(1, m)][re] + M) % M); }
11
#include <bits/stdc++.h> using namespace std; string s; int k, x, ma = 0; map<char, int> val; unsigned long long som = 0; int main() { cin >> s; cin >> k; for (int i = 0; i < 26; i++) { cin >> x; val['a' + i] = x; if (x > ma) { ma = x; } } for (int i = 0; i < s.size(); i++) { som += (i + 1) * val[s[i]]; } int y = (s.size() + k + 1) * (s.size() + k) / 2; int h = (s.size()) * (s.size() + 1) / 2; som += ma * (y - h); cout << som; return (0); }
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int inf = 0x3f3f3f3f; const int N = 2e3 + 11; long long mx[N * 4]; int id[N * 4]; int a[N]; int s[N][N]; int n, m, k, p, tot; int x, y, z; int build(int l, int r, int o) { if (l == r) { mx[o] = a[l]; return id[o] = l; } int mid = ((l + r) >> 1); int t1 = build(l, mid, (o << 1)); int t2 = build(mid + 1, r, ((o << 1) | 1)); mx[o] = max(a[t1], a[t2]); if (a[t1] > a[t2]) return id[o] = t1; return id[o] = t2; } void maintain(int l, int r, int o) { if (r > l) { mx[o] = max(mx[(o << 1)], mx[((o << 1) | 1)]); if (mx[(o << 1)] > mx[((o << 1) | 1)]) id[o] = id[(o << 1)]; else id[o] = id[((o << 1) | 1)]; } } void update(int l, int r, int o) { if (x <= l && r <= y) { mx[o] += z; return; } int mid = ((l + r) >> 1); if (x <= mid) update(l, mid, (o << 1)); if (y > mid) update(mid + 1, r, ((o << 1) | 1)); maintain(l, r, o); } long long r1[1000100], r2[1000100]; int main() { cin >> n >> m >> k >> p; int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) scanf("%d", &s[i][j]); for (i = 1; i <= n; i++) { int tp = 0; for (j = 1; j <= m; j++) { tp += s[i][j]; } a[i] = tp; } build(1, n, 1); z = -p * m; for (i = 1; i <= k; i++) { int tid = id[1]; x = y = tid; r1[i] = mx[1] + r1[i - 1]; update(1, n, 1); } for (j = 1; j <= m; j++) { int tp = 0; for (i = 1; i <= n; i++) { tp += s[i][j]; } a[j] = tp; } build(1, m, 1); z = -p * n; for (i = 1; i <= k; i++) { int tid = id[1]; x = y = tid; r2[i] = mx[1] + r2[i - 1]; update(1, m, 1); } long long ans = -1e18; for (i = 0; i <= k; i++) { ans = max(ans, r1[i] + r2[k - i] - (long long)i * (long long)p * (long long)(k - i)); } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long powermodm(long long x, long long n, long long M) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long power(long long _a, long long _b) { long long _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long gcd(long long a, long long b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } long long lcm(long long a, long long b) { return (max(a, b) / gcd(a, b)) * min(a, b); } long long id[200005]; vector<long long> v[200005]; long long root1(long long x) { long long y = x; while (id[y] != y) { y = id[y]; } return y; } void union1(long long x, long long y) { if (v[x].size() >= v[y].size()) { for (int i = 0; i < v[y].size(); i++) { v[x].push_back(v[y][i]); } id[y] = x; } else { for (int i = 0; i < v[x].size(); i++) { v[y].push_back(v[x][i]); } id[x] = y; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; for (int i = 1; i <= n; i++) { id[i] = i; v[i].push_back(i); } for (int i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; long long x1 = root1(x); long long y1 = root1(y); if (x1 != y1) { union1(x1, y1); } } long long x = root1(1); for (int i = 0; i < v[x].size(); i++) { cout << v[x][i] << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; map<string, bool> ma; char s[45]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { scanf("%s", s + 1); int len = strlen(s + 1); for (int j = 1; j <= 20; j++) { for (int k = 1; k <= len; k++) { if (s[k] == 'u') { s[k] = 'o'; for (int l = len + 1; l >= k + 1; l--) s[l] = s[l - 1]; len++; break; } } for (int k = 1; k <= len - 1; k++) { if (s[k] == 'k' && s[k + 1] == 'h') { for (int l = k; l <= len - 1; l++) s[l] = s[l + 1]; len--; break; } } } s[len + 1] = '\0'; string ts = string(s + 1); ma[ts] = 1; } cout << ma.size() << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, t, prev_cost = 99999999, cur_cost = 0, wanted_t; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (t = 1; t < 102; t++) { cur_cost = 0; for (int i = 0; i < n; i++) { if (arr[i] < t) cur_cost += (t - 1 - arr[i]); else if (arr[i] > t) { cur_cost += (arr[i] - t - 1); } if (cur_cost > prev_cost) break; } if (prev_cost > cur_cost) { prev_cost = cur_cost; wanted_t = t; } } cout << wanted_t << ' ' << prev_cost; return 0; }
1
#include <bits/stdc++.h> using namespace std; int r[3], ans = 0; int main() { int n; cin >> n; int a[10000 + 5]; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] = a[i] % 3; r[a[i]]++; } ans = min(r[1], r[2]) + r[0] / 2; cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; int ok(int a, int b, int c, int d) { int k1 = a * a * (m - 1) + b * b; int k2 = a * a * (n - 1) + c * c; int k3 = c * c * (m - 1) + d * d; int k4 = b * b * (n - 1) + d * d; int kk1 = sqrt(k1); int kk2 = sqrt(k2); int kk3 = sqrt(k3); int kk4 = sqrt(k4); if (kk1 * kk1 != k1) return 0; if (kk2 * kk2 != k2) return 0; if (kk3 * kk3 != k3) return 0; if (kk4 * kk4 != k4) return 0; return 1; } int main() { int t, C = 1; while (scanf("%d%d", &n, &m) != EOF) { int flag = 0, a, b, c, d; for (a = 1; a < 100; a++) { for (b = 1; b < 100; b++) { for (c = 1; c < 100; c++) { for (d = 1; d < 100; d++) { if (ok(a, b, c, d)) flag = 1; if (flag) break; } if (flag) break; } if (flag) break; } if (flag) break; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) printf("%d ", a); printf("%d\n", b); } for (int j = 0; j < m - 1; j++) printf("%d ", c); printf("%d\n", d); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 1, INF = 1e9, MOD = 1e9 + 7; int n, m, sum, d[MAXN], b; vector<int> G[MAXN], ans; map<pair<int, int>, int> pos; bool mark[MAXN]; void dfs(int u = 1, int p = 0) { mark[u] = 1; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!mark[v]) dfs(v, u); } if (d[u]) { d[u] ^= 1; d[p] ^= 1; ans.push_back(pos[pair<int, int>(u, p)]); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> d[i]; if (d[i] >= 0) { sum += d[i]; } else { d[b] = 0; b = i; } } if (sum % 2 == 0) { d[b] = 0; } else { if (b == 0) return cout << -1 << endl, 0; else d[b] = 1; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); pos[pair<int, int>(a, b)] = i + 1; pos[pair<int, int>(b, a)] = i + 1; } dfs(); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<string> num = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; vector<string> tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; int s; cin >> s; if (s < 20) { cout << num[s]; } else { cout << tens[s / 10]; if (s % 10 > 0) { cout << "-" << num[s % 10]; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 1000002; int p[N], d[N], n; vector<int> G[N], idx; int q[N]; int bfs(int xx) { int ans = INF, l = 0, r = 0; q[r++] = xx; for (auto u : idx) d[u] = -1; d[xx] = 0; p[xx] = -1; while (l < r) { int x = q[l++]; for (auto u : G[x]) if (u != p[x]) { if (d[u] == -1) d[u] = d[x] + 1, q[r++] = u, p[u] = x; else ans = min(ans, 1 + d[u] + d[x]); } } return ans; } vector<int> f(int &x) { vector<int> ans; int y = 1; while (x > 1) { int r = p[x], cnt = 0; while (x % r == 0) cnt++, x /= r; if (cnt & 1) ans.push_back(r), y *= r; } x = y; return ans; } int main() { set<int> s; for (int i = 2; i * i < N; i++) if (!p[i]) for (int j = i * i; j < N; j += i) p[j] = i; for (int i = 1; i < N; i++) if (!p[i]) p[i] = i; cin >> n; for (int i = 0, x; i < n; i++) { cin >> x; vector<int> tmp = f(x); if (x == 1) return cout << 1 << endl, 0; s.insert(x); if (tmp.size() == 1) tmp.push_back(1); idx.push_back(tmp[0]); idx.push_back(tmp[1]); G[tmp[0]].push_back(tmp[1]); G[tmp[1]].push_back(tmp[0]); } if (s.size() != n) return cout << 2 << endl, 0; sort(idx.begin(), idx.end()); idx.resize(unique(idx.begin(), idx.end()) - idx.begin()); int ans = INF; for (auto u : idx) if (u <= 1000) ans = min(ans, bfs(u)); if (ans == INF) ans = -1; cout << ans << endl; }
9
#include <bits/stdc++.h> using namespace std; template <typename T, int size> struct segtree_t { int n; vector<int> tree; segtree_t() : n(size), tree(4 * n, 0) {} T operation(const T &a, const T &b) { return max(a, b); } void build() { for (int i = 0; i < n; ++i) tree[i] = operation(tree[2 * i], tree[2 * i + 1]); } T query(int l, int r) { T x = 0, y = 0; r++; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) x = operation(x, tree[l++]); if (r & 1) y = operation(tree[--r], y); } return operation(x, y); } void update(int idx, int val) { for (tree[idx += size] = val; idx > 1; idx >>= 1) tree[idx >> 1] = operation(tree[(idx | 1) ^ 1], tree[idx | 1]); } }; int main() { int n; scanf("%d", &n); segtree_t<int, 100010> segtree; int ret = 0; for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); int b = segtree.query(0, a - 1); ret = max(ret, b + 1); segtree.update(a, b + 1); } printf("%d\n", ret); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10, mod = 1e9 + 7; int n, m, k, t; int main() { int i, j; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); puts(n % m == 0 ? "YES" : "NO"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int nmax = 1e2 + 42; int n, d, x[nmax]; int main() { cin >> n >> d; for (int i = 1; i <= n; i++) cin >> x[i]; sort(x + 1, x + n + 1); int ans = 2; for (int i = 1; i < n; i++) if (x[i + 1] - x[i] == 2 * d) ans++; else if (x[i + 1] - x[i] > 2 * d) ans = ans + 2; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, q, p; long long* pt; long long a[200000], k, sum = 0LL; int main() { cin >> n >> q; for (int i = (0); i < (n); i++) { cin >> a[i]; if (i) a[i] += a[i - 1]; } for (int i = (0); i < (q); i++) { cin >> k; sum += k; pt = upper_bound(a, a + n, sum); p = pt - a; if (p == n) { p = 0; sum = 0LL; } cout << n - p << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; bool isint(float x) { return floor(x) == x; } int main() { int n, m; cin >> n >> m; float a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int sum = 0; for (int i = 0; i < m; i++) { if (a[i] >= 0) break; sum += a[i]; } cout << -1 * sum; ; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int a = s.size(); int b = t.size(); if (a != b) { cout << "NO"; return 0; } for (int i = 0; i < a; i++) if (s[i] != t[a - i - 1]) { cout << "NO"; return 0; } cout << "YES"; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { std ::ios_base ::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); long long n; cin >> n; long long ans = 0; while (n) { ans++; n /= 2; } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; const int M = 998244353LL; struct mat_t { vector<int> d; mat_t() : d(64 * 64) {} mat_t& operator+=(const mat_t& rhs) { for (int i = 0; i < 64 * 64; ++i) { if ((d[i] += rhs.d[i]) >= M) d[i] -= M; } return *this; } mat_t operator*(const mat_t& rhs) const { mat_t r; for (int i = 0; i < 64; ++i) { for (int j = 0; j < 64; ++j) { for (int k = 0; k < 64; ++k) { r.d[i * 64 + j] = (r.d[i * 64 + j] + (long long)d[i * 64 + k] * rhs.d[k * 64 + j]) % M; } } } return r; } }; class Solution { public: Solution(const vector<int>& f) : _m(3), _p(1) { int t = 1; vector<int> v(4); for (int i = 0; i < 3; ++i) { mat_t& m = _m[i]; for (int j = 0; j < 64; ++j) { ++t; for (int k = 0; k < 3; ++k) { if (f[3 * i + k]) v[(j >> (2 * k)) & 3] = t; } int x = 0; while (v[x] == t) ++x; int jj = ((j << 2) & 63) | x; ++m.d[j * 64 + jj]; } _p[0] += m; } } vector<int> process_strip(int n, const vector<pair<int, int>>& s) { vector<int> v(64); v[63] = 1; int l = -1, d; for (const pair<int, int>& si : s) { if ((d = si.first - l - 1) > 0) skip(v, d); skip(v, _m[si.second]); l = si.first; } if ((d = n - l - 1) > 0) skip(v, d); vector<int> r(4); for (int i = 0; i < 64; ++i) { if ((r[i & 3] += v[i]) >= M) r[i & 3] -= M; } return r; } private: void skip(vector<int>& v, mat_t& m) { vector<int> r(64); for (int i = 0; i < 64; ++i) { for (int j = 0; j < 64; ++j) { r[i] = (r[i] + (long long)v[j] * m.d[64 * j + i]) % M; } } v.swap(r); } void skip(vector<int>& v, int x) { mat_t r; for (int i = 0; i < 64; ++i) ++r.d[65 * i]; for (int i = 0; x > 0; ++i, x >>= 1) { if (i >= (int)_p.size()) { _p.push_back(_p.back() * _p.back()); } if (x & 1) skip(v, _p[i]); } } private: vector<mat_t> _m, _p; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int& ai : a) cin >> ai; vector<vector<pair<int, int>>> p(n); int m; cin >> m; while (m--) { int x, y, c; cin >> x >> y >> c; p[x - 1].emplace_back(y - 1, c - 1); } vector<int> f(9); for (int& fi : f) cin >> fi; Solution soln(f); vector<int> r(4); r[0] = 1; for (int i = 0; i < n; ++i) { sort((p[i]).begin(), (p[i]).end()); vector<int> nim = soln.process_strip(a[i], p[i]); vector<int> rr(4); for (int j = 0; j < 4; ++j) { for (int k = 0; k < 4; ++k) { rr[j ^ k] = (rr[j ^ k] + (long long)r[j] * nim[k]) % M; } } r.swap(rr); } cout << r[0] << endl; return 0; }
9