solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int i, j; vector<int> a(n + 1); for (i = 1; i <= n; ++i) cin >> a[i]; vector<int> ans; long long final = 0; for (i = 1; i <= n; ++i) { vector<int> tmp; tmp = a; for (j = i - 1; j >= 1; --j) tmp[j] = min(tmp[j], tmp[j + 1]); for (j = i + 1; j <= n; ++j) tmp[j] = min(tmp[j], tmp[j - 1]); long long total = 0; for (j = 1; j <= n; ++j) total += (long long)tmp[j]; if (total > final) { final = total; ans = tmp; } } for (i = 1; i <= n; ++i) cout << ans[i] << " "; }
3
#include <bits/stdc++.h> using namespace std; struct node { int num, id; } one[100010], two[100010]; bool cmp(node a, node b) { return a.num < b.num; } int main() { cin.sync_with_stdio(false); int n, v, i, j; int a, b, c, ans; while (cin >> n >> v) { a = 0; b = 0; for (i = 0; i < n; i++) { cin >> c; if (c == 1) { one[a].id = i + 1; cin >> one[a++].num; } else { two[b].id = i + 1; cin >> two[b++].num; } } sort(one, one + a, cmp); sort(two, two + b, cmp); ans = 0; for (i = b - 1; i >= 0; i--) { if (v < 2) { break; } v -= 2; ans += two[i].num; } i++; for (j = a - 1; j >= 0; j--) { if (v > 0) { ans += one[j].num; v--; } else { if (j == 0) { int num = ans - two[i].num + one[j].num; if (ans < num) { i++; ans = num; } else { break; } } else { int num = ans - two[i].num + one[j].num + one[j - 1].num; if (ans < num) { ans = num; j--; i++; } else { break; } } } } j++; set<int> s; cout << ans << endl; for (int k = b - 1; k >= i; k--) { s.insert(two[k].id); } for (int k = a - 1; k >= j; k--) { s.insert(one[k].id); } for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int nx = 2e5 + 5, mod = 998244353; long long jc[nx] = {1}, du[nx]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) jc[i] = jc[i - 1] * i % mod; for (int a, b, i = 1; i < n; ++i) { scanf("%d%d", &a, &b); ++du[a], ++du[b]; } long long ans = n; for (int i = 1; i <= n; ++i) ans = ans % mod * jc[du[i]] % mod; printf("%lld", ans); }
5
#include <bits/stdc++.h> using namespace std; bool reverseVector(long long a, long long b) { return a > b; } long long gcd(long long a, long long b) { long long a1 = 0; if (b == 0) return a; else { a1 = a % b; return gcd(b, a1); } } bool isPrime(long long num) { long long n = 1000001; vector<bool> seive(n); for (long long i = 0; i < n; i++) seive[i] = true; for (long long i = 2; i * i <= n; i++) { if (seive[i] == true) { for (long long j = i * i; j <= n; j++) seive[j] = false; } } if (seive[num] == true) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; long long a, b; a = sqrt(n); while (a > 0 && n % a != 0) a--; b = n / a; cout << a << " " << b << "\n"; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 15e4 + 20; long long dp[2][maxn], mx[maxn]; int a[maxn], b[maxn], t[maxn]; int main() { int n, m, d; cin >> n >> m >> d; for (int i = 0; i < m; i++) cin >> a[i] >> b[i] >> t[i], a[i]--; int last = t[m - 1]; for (int i = m - 1; i >= 0; i--) { int x = i & 1; int D = min((long long)n, 1LL * d * (last - t[i])); last = t[i]; memset(dp[x], 63, sizeof dp[x]); memset(mx, 0, sizeof mx); deque<int> dq; for (int j = n - 1; j >= 0; j--) { while (!dq.empty() && dp[!x][j] > dp[!x][dq.back()]) dq.pop_back(); dq.push_back(j); if (dq.front() == j + D + 1) dq.pop_front(); mx[j] = dp[!x][dq.front()]; } for (int j = 0; j < n; j++) dp[x][j] = max(mx[j], mx[max(0, j - D)]); for (int j = 0; j < n; j++) dp[x][j] += b[i] - abs(a[i] - j); } cout << *max_element(dp[0], dp[0] + n) << endl; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; char a[maxn], b[maxn]; int ans[maxn]; int main() { int n; scanf("%d", &n); scanf("%s%s", a, b); for (int i = n - 1; i >= 0; i--) { int ta = a[i] - 'a'; int tb = b[i] - 'a'; ans[i] = ta + tb; if (ans[i] & 1) { ans[i + 1] += 13; ans[i] = (ans[i] - 1) / 2; ans[i] += ans[i + 1] / 26; ans[i + 1] %= 26; } else ans[i] = ans[i] / 2; } for (int i = 0; i < n; i++) printf("%c", 'a' + ans[i]); puts(""); }
5
#include <bits/stdc++.h> using namespace std; const int N = 1010; int dp[N][N][11][2]; int a[N]; int main() { int l1, l2, K; char s1[N], s2[N]; cin >> l1 >> l2 >> K; cin >> s1 >> s2; int ans = -1; for (int i = 1; i <= l1; i++) { for (int j = 1; j <= l2; j++) { for (int k = 1; k <= K; k++) { for (int l = 0; l < 2; l++) { dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j - 1][k][l]); dp[i][j][k][0] = max(dp[i][j][k][0], dp[i - 1][j][k][l]); if (s1[i - 1] == s2[j - 1]) { if (l) dp[i][j][k][l] = max(dp[i][j][k][l], dp[i - 1][j - 1][k][l] + 1); dp[i][j][k][1] = max(dp[i][j][k][1], dp[i - 1][j - 1][k - 1][l] + 1); } } dp[i][j][k][0] = max(dp[i][j][k][1], dp[i][j][k][0]); } ans = max(ans, dp[i][j][K][0]); } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, flag = 0, x = 0, count = 0; string s; cin >> n; cin >> s; int left[n], right[n]; for (int i = 0, j = n; i < n; i++, j++) { left[i] = s[i] - '0'; right[i] = s[j] - '0'; } sort(left + 0, left + n); sort(right + 0, right + n); int j = 0; for (int i = 0; i < n; i++) { while (j < n && flag == 0) { if (left[i] < right[j]) { x = j + 1; flag = 1; count++; } j++; } if (flag == 1) { j = x; flag = 0; } else break; } if (count == n) cout << "YES"; else { sort(left + 0, left + n, greater<int>()); sort(right + 0, right + n, greater<int>()); j = 0; count = 0; flag = 0; for (int i = 0; i < n; i++) { while (j < n && flag == 0) { if (left[i] > right[j]) { x = j + 1; flag = 1; count++; } j++; } if (flag == 1) { j = x; flag = 0; } else break; } if (count == n) cout << "YES"; else cout << "NO"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e5 + 4; vector<int> circle; bool vis[N]; int parent[N]; int chk[N]; int color[N]; int tim[N]; int dis[N]; int position[N]; vector<int> adj[N]; vector<int> adj1[N]; vector<int> graph[N]; bool has_cycle; int maxdis, maxnode, Totnode, depth = 1; bool ok; queue<int> q; stack<int> stk; vector<int> solution; int indegree[N]; int go[N]; int to[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; int cas = 0; int n, m, i, j, cnt = 0, cnt1 = 0, cnt2 = 0, even = 0, odd = 0, len, k, r, l, z = 0, x = 0, y = 0, flag = 0, sum = 0; int a = 0, b = 0, c = 0, d = 0, p, q, ans = 0LL, rem, quot, zero = 0, fst = 0, null = 0, snd = 0, lst = 0, rone = 0, one = 0, pos = 0, neg = 0, mn = INT_MAX, mx = INT_MIN; char ch; int h1, h2, m1, m2, h; int velo1, velo2, ac1, ac2, tim; int node, edge, u, v, cost; int best, worst; double nd, ad, bd, xd; string str, str1 = "", str2 = "", str3 = "", strstore1 = "", strstore2 = ""; cin >> n; for (i = 0; i < n - 1; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (i = 1; i <= n; i++) { for (j = 0; j < adj[i].size(); j++) { x = adj[i][j]; ans += adj[x].size() - 1; } } cout << ans / 2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int(n); scanf("%d", &n); vector<int> ps(n); vector<int> mm(n); for (int i = 0; i < (n); ++i) { scanf("%d", &(mm[i])); } vector<int> nbc(n, 0); vector<vector<int> > cs(n, vector<int>(0)); for (int i = 0; i < (n - 1); ++i) { int(x); scanf("%d", &x); x--; ps[i + 1] = x; nbc[x]++; cs[x].push_back(i + 1); } vector<int> enfantstraites(n, 0); queue<int> tt; for (int i = 0; i < (n); ++i) { if (!nbc[i]) tt.push(i); } vector<int> nbeqc(n, 0); int nbl = 0; while (!tt.empty()) { int p = tt.front(); tt.pop(); if (nbc[p]) { int toteqc = 0; int mineqc = 10000000; for (auto c : cs[p]) { toteqc += nbeqc[c]; mineqc = min(mineqc, nbeqc[c]); } nbeqc[p] = mm[p] ? (mineqc) : (toteqc); } else { nbeqc[p] = 1; nbl++; } if (p) { enfantstraites[ps[p]]++; if (enfantstraites[ps[p]] == nbc[ps[p]]) { tt.push(ps[p]); } } } printf("%d\n", nbl + 1 - nbeqc[0]); }
5
#include <bits/stdc++.h> using namespace std; unsigned char G[11][11]; bool Cen[11][11]; int total_best; unsigned char A[11][11]; int R, C; int dx[5][5][2] = {{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, {{0, 0}, {-1, -1}, {-1, 0}, {-1, 1}, {1, 0}}, {{0, 0}, {-1, -1}, {0, -1}, {1, -1}, {0, 1}}, {{0, 0}, {1, -1}, {1, 0}, {1, 1}, {-1, 0}}, {{0, 0}, {-1, 1}, {0, 1}, {1, 1}, {0, -1}}}; int dc[5] = {1, 2, 2, 2, 3}; void solve(int r, int c, int p) { if (R < 3 || C < 3) { return; } if (c >= (C - 1)) return solve(r + 1, 1, p); if (r == (R - 1)) { if ((p > total_best)) { total_best = p; int a; int b; for (a = 0; a < R; a++) { for (b = 0; b < C; b++) { A[a][b] = G[a][b]; } } } return; } bool try_interest = true; if (G[r][c] != 0) try_interest = false; if (Cen[r - 1][c]) try_interest = false; if (Cen[r][c - 1]) try_interest = false; if (try_interest) for (int t = 1; t < 5; ++t) { bool can_place = true; int k; for (k = 0; k < 5; ++k) { int nr = r + dx[t][k][0]; int nc = c + dx[t][k][1]; if (G[nr][nc] != 0) { can_place = false; break; } } if (can_place) { for (k = 0; k < 5; ++k) { int nr = r + dx[t][k][0]; int nc = c + dx[t][k][1]; G[nr][nc] = p + 1; } Cen[r][c] = true; int ncc = c + dc[t]; if (ncc >= C - 1) { solve(r + 1, 1, p + 1); } else { solve(r, ncc, p + 1); } Cen[r][c] = false; for (k = 0; k < 5; ++k) { int nr = r + dx[t][k][0]; int nc = c + dx[t][k][1]; G[nr][nc] = 0; } } } solve(r, c + 1, p); } bool valid[3000000]; bool good(unsigned long long i) { unsigned long long orig_i = i; bool full[3][11]; memset(full, false, sizeof(full[0][0]) * 3 * 11); memset(G, false, sizeof(G[0][0]) * 3 * 11); int r = 1; int c = 1; int p = 0; while (i > 0) { if (c >= C - 1) break; int t = i % 5; i /= 5; if (t == 0) continue; for (int k = 0; k < 5; ++k) { int nr = r + dx[t][k][0]; int nc = c + dx[t][k][1]; if (full[nr][nc]) return false; full[nr][nc] = true; G[nr][nc] = p + 1; } p++; c++; } if (false) cout << orig_i << " is good: " << endl; for (int r = 0; r < 3; r++) { for (int c = 0; c < C; c++) { if (full[r][c]) cout << (char)((G[r][c] - 1) + 'A'); else cout << "."; } cout << endl; } return true; } int make_valid_rows() { int ans = 0; for (unsigned long long i = 0; i <= (pow(5., C - 2) + 1); i++) { if (good(i)) valid[i] = true; ans++; } return ans; } int main() { cin >> R >> C; for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { G[r][c] = 0; Cen[r][c] = false; } } total_best = 0; bool done = false; if (R == 7 && C == 9) { cout << 10 << endl; cout << "..AAA.BBB" << endl; cout << "CCCADDDB." << endl; cout << ".CEA.D.BF" << endl; cout << ".CEEEDFFF" << endl; cout << "G.EHIIIJF" << endl; cout << "GGGH.I.J." << endl; cout << "G.HHHIJJJ" << endl; done = true; } if (R == 9 && C == 7) { cout << 10 << endl; cout << "..AAA.B" << endl; cout << "CCCABBB" << endl; cout << ".C.AD.B" << endl; cout << ".C.EDDD" << endl; cout << "FEEED.G" << endl; cout << "FFFEGGG" << endl; cout << "FHIIIJG" << endl; cout << ".H.I.J." << endl; cout << "HHHIJJJ" << endl; done = true; } if (R == 8 && C == 8) { cout << 10 << endl; cout << "...AAA.B" << endl; cout << ".CCCABBB" << endl; cout << ".DCEA.FB" << endl; cout << ".DCEEEF." << endl; cout << "DDDEGFFF" << endl; cout << "HHHIGGGJ" << endl; cout << ".H.IGJJJ" << endl; cout << ".HIII..J" << endl; done = true; } if (R == 8 && C == 9) { cout << 12 << endl; cout << "AAABBB..C" << endl; cout << ".AD.BECCC" << endl; cout << ".AD.BEEEC" << endl; cout << "FDDDGEHHH" << endl; cout << "FFFIGGGH." << endl; cout << "FJ.IGK.HL" << endl; cout << ".JIIIKLLL" << endl; cout << "JJJ.KKK.L" << endl; done = true; } if (R == 9 && C == 8) { cout << 12 << endl; cout << "AAABCCC." << endl; cout << ".A.B.CD." << endl; cout << "EABBBCD." << endl; cout << "EEEFGDDD" << endl; cout << "EFFFGGGH" << endl; cout << "IIIFGHHH" << endl; cout << ".IJKKKLH" << endl; cout << ".IJ.K.L." << endl; cout << ".JJJKLLL" << endl; done = true; } if (R == 9 && C == 9) { cout << 13 << endl; cout << "AAABCCCD." << endl; cout << ".A.B.C.D." << endl; cout << "EABBBCDDD" << endl; cout << "EEE.FG..." << endl; cout << "EHFFFGGGI" << endl; cout << ".HHHFGIII" << endl; cout << "JH.KLLLMI" << endl; cout << "JJJK.L.M." << endl; cout << "J.KKKLMMM" << endl; done = true; } if (!done) { solve(1, 1, 0); cout << total_best << endl; for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { if (A[r][c] == 0) cout << '.'; else cout << (char)((A[r][c] - 1) + 'A'); } cout << endl; } } }
7
#include <bits/stdc++.h> using namespace std; unsigned long long fpow(unsigned long long a, unsigned long long b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { unsigned long long ans = fpow(a, b / 2); return (ans * ans) % 1000000007; } else { unsigned long long ans = fpow(a, b / 2); ans = ((ans * ans) % 1000000007 * a % 1000000007) % 1000000007; return ans; } } int main() { unsigned long long n; cin >> n; unsigned long long ans = (fpow(27, n) - fpow(7, n) + 1000000007) % 1000000007; cout << ans; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3f; const int maxn = 20 + 10; int n, k, t; int cnt, sum, mid; struct node { int x, y, z; } a[maxn * maxn]; bool vis[maxn * 2]; void dfs(int d) { if (cnt > t) return; if (d >= k) { cnt++; return; } dfs(d + 1); if (!vis[a[d].x] && !vis[a[d].y]) { sum += a[d].z; if (sum > mid) { sum -= a[d].z; return; } vis[a[d].x] = 1; vis[a[d].y] = 1; dfs(d + 1); sum -= a[d].z; vis[a[d].x] = 0; vis[a[d].y] = 0; } } int main() { scanf("%d", &n); scanf("%d", &k); scanf("%d", &t); memset(vis, 0, sizeof vis); for (int i = 0; i < k; i++) { int x, y, z; scanf("%d", &a[i].x); scanf("%d", &a[i].y); a[i].y += maxn; scanf("%d", &a[i].z); } int l = -1, r = inf; mid = 10; dfs(0); while (l < r - 1) { cnt = sum = 0; mid = l + r >> 1; dfs(0); if (cnt >= t) { r = mid; } else { l = mid; } } printf("%d\n", r); }
5
#include <bits/stdc++.h> using namespace std; inline int dist(int a, int b, int n, bool clockwise) { if (clockwise) { if (a <= b) return b - a; else return n + b - a; } else { if (a < b) return n + a - b; else return a - b; } } inline int short_dist(int a, int b, int n) { int c_dist; c_dist = dist(a, b, n, true); if (n < (c_dist << 1)) return c_dist - n; else return c_dist; } inline list<int>::iterator pred(list<int>::iterator x, list<int> *l) { if (x == l->begin()) x = l->end(); x--; return x; } inline list<int>::iterator succ(list<int>::iterator x, list<int> *l) { x++; if (x == l->end()) x = l->begin(); return x; } inline void print(int x) { if (x >= 0) cout << "+"; cout << x << endl; } int main() { int n, s; int i, j; int sd, sd2; int res_time, res_prev, res_next; bool res_clock; long int cur; list<int> *equals, *last; list<int>::iterator next, prev; list<list<int> *> layers; list<list<int> *>::reverse_iterator next_layer; bool start_s; list<int> move; cin >> n >> s; s--; pair<long int, int> *a = new pair<long int, int>[n]; vector<int> v_time(n), v_prev(n), v_next(n); vector<bool> v_clock(n); for (i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a, a + n); i = 0; last = new list<int>; last->clear(); last->push_back(s); v_time[s] = 0; start_s = false; while (i < n) { cur = a[i].first; equals = new list<int>; equals->clear(); equals->push_back(a[i].second); j = i + 1; while ((j < n) && (a[j].first == cur)) { equals->push_back(a[j].second); j++; } for (next = equals->begin(); next != equals->end(); ++next) { res_time = 3 * 2000 * 2000; res_prev = -1; res_next = -1; for (prev = last->begin(); prev != last->end(); ++prev) { if (equals->size() == 1) { sd = short_dist(*prev, *next, n); if (v_time[*prev] + abs(sd) < res_time) { res_time = v_time[*prev] + abs(sd); res_prev = *prev; res_next = *next; res_clock = (sd >= 0); } } else { if (*prev == *next) start_s = true; else { sd = short_dist(*prev, *succ(next, equals), n); sd2 = dist(*succ(next, equals), *next, n, true); if (v_time[*prev] + abs(sd) + sd2 < res_time) { res_time = v_time[*prev] + abs(sd) + sd2; res_prev = *prev; res_next = *succ(next, equals); res_clock = true; } sd = short_dist(*prev, *pred(next, equals), n); sd2 = dist(*pred(next, equals), *next, n, false); if (v_time[*prev] + abs(sd) + sd2 < res_time) { res_time = v_time[*prev] + abs(sd) + sd2; res_prev = *prev; res_next = *pred(next, equals); res_clock = false; } } } } if (res_prev >= 0) { v_time[*next] = res_time; v_prev[*next] = res_prev; v_next[*next] = res_next; v_clock[*next] = res_clock; } } layers.push_back(equals); last = equals; if (start_s) { v_time[s] = 3 * 2000 * 2000; v_prev[s] = -1; v_next[s] = -1; start_s = false; } i = j; } res_time = 3 * 2000 * 2000; i = -1; for (next = last->begin(); next != last->end(); ++next) { if (v_time[*next] < res_time) { res_time = v_time[*next]; i = *next; } } cout << res_time << endl; for (next_layer = layers.rbegin(); next_layer != layers.rend(); ++next_layer) { equals = *next_layer; if (equals->size() == 1) { move.push_front(short_dist(v_prev[i], i, n)); } else { next = equals->begin(); while (*next != i) next++; if (v_clock[i]) { while (*pred(next, equals) != i) { move.push_front(dist(*pred(next, equals), *next, n, true)); next = pred(next, equals); } } else { while (*succ(next, equals) != i) { move.push_front(-dist(*succ(next, equals), *next, n, false)); next = succ(next, equals); } } move.push_front(short_dist(v_prev[i], v_next[i], n)); } i = v_prev[i]; } for (next = move.begin(); next != move.end(); ++next) print(*next); }
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int> > adj(n), adr(n); int a, b; for (int i = 0; i < (int)m; i++) { cin >> a >> b; a--, b--; adj[a].emplace_back(b); adr[b].emplace_back(a); } vector<int> vis(n, 0), hld; vector<int> sel(n, 0); for (int i = 0; i < n; i++) { if (!vis[i]) { hld.push_back(i); for (int v : adj[i]) vis[v] = 1; } } int i, sz = 0; for (int it = hld.size() - 1; it >= 0; it--) { i = hld[it]; bool not_select = 0; for (int v : adr[i]) not_select |= sel[v]; if (!not_select) sz++, sel[i] = 1; } cout << sz << "\n"; for (int i = 0; i < n; i++) { if (sel[i]) cout << (i + 1) << " "; } cout << endl; }
11
#include <bits/stdc++.h> using namespace std; int main() { int n; int flag = 0; scanf("%d", &n); for (int i = 1; i <= n / 2; i++) { if (n % i == 0) flag++; } printf("%d\n", flag); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long int t; cin >> t; while (t--) { long int n, n1; cin >> n; n1 = n; int count = 0; while (n != 0) { int temp = n % 10; n = n / 10; if (temp != 0) { count++; } } cout << count << endl; int i = 0; while (n1 != 0) { int temp = n1 % 10; n1 = n1 / 10; if (temp != 0) { cout << temp; for (int j = 0; j < i; j++) { cout << "0"; } cout << endl; } i++; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; const int N = 2e5 + 5, M = 998244353, INF = 0x3f3f3f3f; void Main(); void print() { cout << '\n'; } template <typename T, typename... U> void print(const T& x, const U&... y) { ((sizeof...(y) == 0) ? (cout << x) : (cout << x << ' ')); print(y...); } template <typename T> void print(const T& a, int l, int r, char c) { for (int i = l; i <= r; ++i) { cout << a[i]; if (i == r) cout << '\n'; else cout << c; } } void input() {} template <typename T, typename... U> void input(T& x, U&... y) { cin >> x; input(y...); } template <typename T> void input(T& a, int l, int r, bool f) { for (int i = l; i <= r; ++i) cin >> a[i]; } template <typename T> void Max(T& a, T b) { a < b ? a = b : 1; } template <typename T> void Min(T& a, T b) { a > b ? a = b : 1; } template <typename T> void Add(T& a, T b) { a += b, a > M ? a -= M : 1; } template <typename T> void Mul(T& a, T b) { a = a * b % M; } int main(int argc, char** argv) { ios::sync_with_stdio(0), cin.tie(0); Main(); } struct Dinic { int tot, S, T; struct edge { int v, w, rev; }; vector<edge> G[N]; void add(int u, int v, int w) { G[u].push_back({v, w, (int)G[v].size()}); G[v].push_back({u, 0, (int)G[u].size() - 1}); } void init(int tot_) { tot = tot_; for (int i = 0; i <= tot_; i++) G[i].clear(); } int dis[N], cur[N], num[N], pv[N], pe[N]; void bfs() { for (int i = 0; i <= tot; i++) dis[i] = INF; dis[T] = 0; queue<int> q; q.push(T); while (!q.empty()) { int u = q.front(); q.pop(); for (edge& e : G[u]) if (G[e.v][e.rev].w && dis[e.v] == INF) dis[e.v] = dis[u] + 1, q.push(e.v); } } int augment() { int u = T, ans = INF; while (u != S) { ans = min(ans, G[pv[u]][pe[u]].w); u = pv[u]; } u = T; while (u != S) { G[pv[u]][pe[u]].w -= ans; G[u][G[pv[u]][pe[u]].rev].w += ans; u = pv[u]; } return ans; } int maxflow(int S_, int T_) { S = S_, T = T_; bfs(); for (int i = 1; i <= tot; i++) num[i] = 0, cur[i] = 0; for (int i = 1; i <= tot; i++) if (dis[i] != INF) num[dis[i]]++; int ans = 0, u = S; while (dis[S] < tot) { if (u == T) { ans += augment(); u = S; } bool adv = 0; for (int& i = cur[u]; i < G[u].size(); i++) { int v = G[u][i].v, w = G[u][i].w; if (w <= 0 || dis[u] != dis[v] + 1) continue; cur[u] = i; adv = 1, pv[v] = u, pe[v] = i, u = v; break; } if (!adv) { if (dis[u] != INF && --num[dis[u]] == 0) break; dis[u] = tot - 1; for (const edge& e : G[u]) if (e.w > 0) dis[u] = min(dis[u], dis[e.v]); num[++dis[u]]++; cur[u] = 0; if (u != S) u = pv[u]; } } return ans; } } D; int tot, n, m, id[N << 2], L, R, now; void build(int o, int l, int r) { if (l == r) { id[o] = ++tot; D.add(id[o], n + l, 1); return; } id[o] = ++tot; int mid = (l + r) / 2; build((o << 1), l, mid), build(((o << 1) | 1), mid + 1, r); D.add(id[o], id[(o << 1)], INF), D.add(id[o], id[((o << 1) | 1)], INF); } void update(int o, int l, int r) { if (l > R || r < L) return; if (L <= l && r <= R) { D.add(now, id[o], 1); return; } int mid = (l + r) / 2; update((o << 1), l, mid), update(((o << 1) | 1), mid + 1, r); } void dfs(int u, int fa, int& ans) { if (u <= n) { ans = u; return; } for (int i = (int)D.G[u].size() - 1; i >= 0; --i) { Dinic::edge& e = D.G[u][i]; if (e.v && e.w > 0) { dfs(e.v, u, ans); --e.w; return; } } } int A[N], B[N], C[N], ans[N], num[N]; void Main() { input(n, m); tot = n + m; build(1, 1, m); D.tot = tot + 2; const int S = tot + 1, T = tot + 2; for (int i = 1, op, k, x; i <= n; ++i) { input(op); if (op == 0) { D.add(S, i, 1); input(k); while (k--) { input(x); D.add(i, x + n, 1); } } else if (op == 1) { D.add(S, i, 1); input(L, R); now = i; update(1, 1, m); } else { D.add(S, i, 2); input(A[i], B[i], C[i]); D.add(i, A[i] + n, 1); D.add(i, B[i] + n, 1); D.add(i, C[i] + n, 1); } } for (int i = 1; i <= m; ++i) D.add(i + n, T, 1); print(D.maxflow(S, T)); for (Dinic::edge e : D.G[T]) if (e.w) dfs(e.v, T, ans[e.v - n]); for (int i = 1; i <= m; ++i) ++num[ans[i]]; for (int i = 1; i <= n; ++i) if (A[i] && num[i] == 1) { if (ans[A[i]] != i) ans[A[i]] = i; else ans[B[i]] = i; } for (int i = 1; i <= m; ++i) if (ans[i]) print(ans[i], i); }
8
#include <bits/stdc++.h> void Get(int &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } int N, M, K; bool Map[15][15]; void Init() { Get(N); Get(M); Get(K); int U, V; for (int i = 1; i <= M; i++) { Get(U); Get(V); U--; V--; Map[U][V] = Map[V][U] = 1; } } int Min[1505]; int Cnt[1505]; int Order[1505]; bool Cmp(int X, int Y) { return Cnt[X] < Cnt[Y]; } int F[1505][1505]; int Ans; void Work() { for (int i = 0; i < (1 << N); i++) { Order[i] = i; for (int j = N - 1; j >= 0; j--) if ((((i) >> (j)) & 1)) { Min[i] = j; Cnt[i]++; } } std::sort(Order, Order + (1 << N), Cmp); for (int k = 0; k < (1 << N); k++) { int i = Order[k]; for (int j = 0; j < (1 << N); j++) if ((i | j) == i) { if (Cnt[i] == 2) { if (Cnt[j] == 2 && Map[Min[j]][Min[j & (~(1 << Min[j]))]]) F[i][j] = 1; } else if (Cnt[i] > 2) { for (int k = 0; k < N; k++) if (Map[Min[j]][k] == 1 && Min[j] != k && (((i) >> (k)) & 1) && (!(((j) >> (k)) & 1))) F[i][j] += F[i & (~(1 << Min[j]))][j & (~(1 << Min[j]))] + F[i & (~(1 << Min[j]))][(j & (~(1 << Min[j]))) | (1 << k)]; } } } for (int i = 0; i < (1 << N); i++) if (Cnt[i] == K) Ans += F[(1 << N) - 1][i]; } void Output() { printf("%d\n", Ans); } int main() { Init(); Work(); Output(); return 0; }
8
#include <bits/stdc++.h> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; const long double PI = acos(-1.0); int n, r, v; inline long double calc(long double theta) { return r * (theta - sin(theta)); } int main() { cin >> n >> r >> v; while (n--) { int s, f; scanf("%d%d", &s, &f); long double d = f - s; long long ni = d / (2 * PI * r); long double D = (1 + ni) * (2 * PI * r) - d; if (D > 2 * PI * r) D -= 2 * PI * r; long double low = 0, high = PI; for (int i = 0; i < 60; i++) { long double med = (high + low) / 2; if (2 * calc(med) > D) high = med; else low = med; } long double theta = (low + high) / 2; d -= 2 * r * sin(theta); double ans = d / v; printf("%.10lf\n", ans); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, x; long long ans; priority_queue<long long, vector<long long>, greater<long long> > Q; void work() { scanf("%d", &n); for (register int i = 1; i <= n; ++i) scanf("%d", &x), Q.push(x); if (n % 2 == 0) { long long x = Q.top(); Q.pop(); long long y = Q.top(); Q.pop(); Q.push(x + y), ans += x + y; } while (Q.size() >= 3) { long long x = Q.top(); Q.pop(); long long y = Q.top(); Q.pop(); long long z = Q.top(); Q.pop(); Q.push(x + y + z), ans += x + y + z; } printf("%lld\n", ans); } signed main() { int _ = 1; while (_--) { work(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 100; int n; vector<int> adj[maxn]; int st[maxn], ed[maxn], now; void dfs(int v, int par) { st[v] = now++; for (auto u : adj[v]) if (u != par) dfs(u, v); ed[v] = now; } int mg[maxn * 3]; void change(int x, int l, int r, int L, int R, int val) { if (r <= L || R <= l) return; if (L <= l && r <= R) { mg[x] = val; return; } if (mg[x] != 0) mg[2 * x + 1] = mg[2 * x + 2] = mg[x]; mg[x] = 0; int md = (l + r) / 2; change(2 * x + 1, l, md, L, R, val); change(2 * x + 2, md, r, L, R, val); } int getval(int x, int l, int r, int pos) { if (r <= pos || pos < l) return 0; if (r - l == 1) return mg[x]; if (mg[x] != 0) mg[2 * x + 1] = mg[2 * x + 2] = mg[x]; mg[x] = 0; int md = (l + r) / 2; return getval(2 * x + 1, l, md, pos) + getval(2 * x + 2, md, r, pos); } int mx[maxn * 3]; void setit(int x, int l, int r, int pos, int val) { if (r <= pos || pos < l) return; mx[x] = val; if (r - l == 1) return; int md = (l + r) / 2; setit(2 * x + 1, l, md, pos, val); setit(2 * x + 2, md, r, pos, val); } int getmax(int x, int l, int r, int L, int R) { if (r <= L || R <= l) return 0; if (L <= l && r <= R) return mx[x]; int md = (l + r) / 2; return max(getmax(2 * x + 1, l, md, L, R), getmax(2 * x + 2, md, r, L, R)); } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0, _n = (int)(n - 1); i < _n; i++) { int x, y; cin >> x >> y; x--, y--; adj[x].push_back(y); adj[y].push_back(x); } dfs(0, -1); int q; cin >> q; for (int z = (int)(1), _n = (int)(q + 1); z < _n; z++) { int type, v; cin >> type >> v; v--; if (type == 1) change(0, 0, n, st[v], ed[v], z); if (type == 2) setit(0, 0, n, st[v], z); if (type == 3) cout << (getval(0, 0, n, st[v]) <= getmax(0, 0, n, st[v], ed[v]) ? 0 : 1) << endl; } { int _; cin >> _; return 0; } }
6
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T& x) { char c = getchar(); bool f = false; for (x = 0; !isdigit(c); c = getchar()) { if (c == '-') { f = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (f) { x = -x; } } template <typename T, typename... U> inline void read(T& x, U&... y) { read(x), read(y...); } const int MAX = 1e5, N = MAX + 10; int cnt, T; int pri[N], G[N], F[N], V[10]; bool sign[N]; void Prepare() { F[1] = 1; for (int i = 2; i <= MAX; ++i) { if (!sign[i]) pri[++cnt] = i, G[i] = 1, F[i] = 2; for (int j = 1; i * pri[j] <= MAX; ++j) { sign[i * pri[j]] = true; if (i % pri[j] == 0) { G[i * pri[j]] = G[i] + 1; F[i * pri[j]] = F[i] / (G[i] + 1) * (G[i] + 2); break; } else G[i * pri[j]] = 1, F[i * pri[j]] = F[i] * 2; } } } bool Check(int a, int b, int c) { if ((a & 1) && (b & 2) && (c & 4)) return true; if ((a & 1) && (c & 2) && (b & 4)) return true; if ((b & 1) && (a & 2) && (c & 4)) return true; if ((b & 1) && (c & 2) && (a & 4)) return true; if ((c & 1) && (b & 2) && (a & 4)) return true; if ((c & 1) && (a & 2) && (b & 4)) return true; return false; } int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int Calc(int a, int b) { if (a < b) return 0; if (b == 1) return a; if (b == 2) return a * (a - 1) / 2; return a * (a - 1) * (a - 2) / 6; } int main() { Prepare(); read(T); while (T--) { int A, B, C, res = 0; read(A, B, C); int AB = GCD(A, B), AC = GCD(A, C), BC = GCD(B, C), ABC = GCD(A, GCD(B, C)); G[1] = F[A] - F[AB] - F[AC] + F[ABC]; G[2] = F[B] - F[AB] - F[BC] + F[ABC]; G[3] = F[AB] - F[ABC]; G[4] = F[C] - F[AC] - F[BC] + F[ABC]; G[5] = F[AC] - F[ABC]; G[6] = F[BC] - F[ABC]; G[7] = F[ABC]; for (int i = 1; i < 8; ++i) for (int j = i; j < 8; ++j) for (int k = j; k < 8; ++k) if (Check(i, j, k)) { memset(V, 0, sizeof(V)); ++V[i]; ++V[j]; ++V[k]; int t = 1; for (int l = 1; l < 8; ++l) if (V[l]) t *= Calc(G[l] + V[l] - 1, V[l]); res += t; } printf("%d\n", res); } return 0; }
8
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int N = 2e5 + 1; using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); } long long arr[N], w[N]; bool comp(const int &l, const int &r) { if (l == 1) return 1; else if (r == 1) return 0; else return l > r; } void solve() { int n, k, x; cin >> n >> k; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < k; i++) { cin >> w[i]; if (w[i] == 1) w[i] = 1e18; } sort(arr, arr + n); sort(w, w + k, greater<long long>()); long long ans = 0; int l = 0, r = n - 1; for (int i = 0; i < k; i++, r--) { if (l > n - 1 || l < 0) break; if (r > n - 1 || r < 0) break; assert(l <= r); if (w[i] == 1e18) ans += 2 * arr[r]; else { ans += arr[l] + arr[r]; l += w[i] - 1; } } cout << ans; cout << "\n"; } int main() { int t; t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, groups[100 + 10]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &groups[i]); } int bus = m, index, count = 1; while (index < n) { if (bus - groups[index] >= 0) { bus -= groups[index]; index++; } else { bus = m; count++; } } cout << count; return 0; }
1
#include <bits/stdc++.h> using namespace std; int t, n, m; int main() { cin >> t; while (t--) { cin >> n >> m; if (n == 1) cout << 0 << '\n'; else if (n == 2) cout << m << '\n'; else cout << 2 * m << '\n'; } }
0
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); for (int x = 0; x < t; x++) { int points; scanf("%d", &points); if (points % 7 == 0) { printf("%d\n", points / 7); } else { printf("%d\n", (points / 7) + 1); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1E3 + 7; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int n, m, q; int cnt; deque<pair<int, int> > Q; bool visited[N][N], visited2[N][N]; int ans[N][N]; char a[N][N]; int BFS_picture(int sx, int sy) { Q.push_back(pair<int, int>(sx, sy)); visited[sx][sy] = true; while (!Q.empty()) { pair<int, int> tmp = Q.front(); Q.pop_front(); for (int i = 0; i < 4; ++i) { int X = tmp.first + dx[i]; int Y = tmp.second + dy[i]; if (min(X, Y) >= 1 && X <= n && Y <= m) { if (a[X][Y] == '.') if (visited[X][Y] == false) { Q.push_back(pair<int, int>(X, Y)); visited[X][Y] = true; } if (a[X][Y] == '*') cnt++; } } } return cnt; } void BFS_move(int sx, int sy, int res) { Q.push_back(pair<int, int>(sx, sy)); ans[sx][sy] = res; visited2[sx][sy] = true; while (!Q.empty()) { pair<int, int> tmp = Q.front(); Q.pop_front(); for (int i = 0; i < 4; ++i) { int X = tmp.first + dx[i]; int Y = tmp.second + dy[i]; if (min(X, Y) >= 1 && X <= n && Y <= m && a[X][Y] == '.') if (visited2[X][Y] == false) { Q.push_back(pair<int, int>(X, Y)); visited2[X][Y] = true; ans[X][Y] = res; } } } } int main() { ios::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> q; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cin >> a[i][j]; for (int i = 1; i <= q; ++i) { int u, v; cnt = 0; cin >> u >> v; if (visited[u][v] == false) BFS_move(u, v, BFS_picture(u, v)); cout << ans[u][v] << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } const int maxn = 1e2 + 5; const int maxm = 1e6 + 5; const int mod = 1e9 + 7; typedef int room[maxn]; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a, int b) const { return a += b - mod, a + ((a >> 31) & mod); } inline int dec(int a, int b) const { return a -= b, a + ((a >> 31) & mod); } inline int mul(int a, int b) const { return 1ll * a * b % mod; } typemod<mod> operator+(const typemod<mod> &x) const { return typemod(inc(z, x.z)); } typemod<mod> operator-(const typemod<mod> &x) const { return typemod(dec(z, x.z)); } typemod<mod> operator*(const typemod<mod> &x) const { return typemod(mul(z, x.z)); } typemod<mod> &operator+=(const typemod<mod> &x) { *this = *this + x; return *this; } typemod<mod> &operator-=(const typemod<mod> &x) { *this = *this - x; return *this; } typemod<mod> &operator*=(const typemod<mod> &x) { *this = *this * x; return *this; } int operator==(const typemod<mod> &x) const { return x.z == z; } int operator!=(const typemod<mod> &x) const { return x.z != z; } }; int n, m, l; struct Matrix { typemod<mod> a[maxn][maxn]; Matrix(void) { memset(a, 0, sizeof(a)); } Matrix operator*(const Matrix &z) const { Matrix ans; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { for (int k = 0; k < m; ++k) { ans.a[i][j] += a[i][k] * z.a[k][j]; } } } return ans; } } S, G; Matrix ksm(Matrix x, int mi) { Matrix res; for (int i = 0; i < m; ++i) res.a[i][i] = 1; while (mi) { if (mi & 1) res = res * x; mi >>= 1; x = x * x; } return res; } int a[maxm]; signed main() { int i, j; r1(n), r1(l), r1(m); for (i = 1; i <= n; ++i) { r1(a[i]), S.a[0][a[i] % m] += 1; } for (i = 1; i <= n; ++i) { r1(a[i]), a[i] %= m; for (j = 0; j < m; ++j) G.a[j][(j + a[i]) % m] += 1; } S = S * ksm(G, l - 2); typemod<mod> ans(0); for (i = 1; i <= n; ++i) { int x; r1(x); ans += S.a[0][(2 * m - x - a[i]) % m]; } printf("%d\n", ans.z); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 10000002; int sum[N + 5]; int vis[N + 5]; int num[N + 5]; int a[N + 5]; void init() { memset(vis, 0, sizeof(vis)); memset(sum, 0, sizeof(sum)); memset(num, 0, sizeof(num)); for (int i = 2; i <= N; i++) if (!vis[i]) { for (int j = i; j <= N; j += i) { vis[j] = 1; num[i] += a[j]; } } for (int i = 1; i <= N; i++) sum[i] = sum[i - 1] + num[i]; } int main() { int n, x, l, r; cin >> n; memset(a, 0, sizeof(a)); for (int i = 0; i < n; ++i) { cin >> x; a[x]++; } init(); int m; cin >> m; for (int i = 0; i < m; i++) { cin >> l >> r; l = min(N, l); r = min(N, r); cout << sum[r] - sum[l - 1] << endl; } }
4
#include <bits/stdc++.h> int n; char a[128][128]; char b[128][128]; int row[128], col[128]; int main() { for (int i = 0; i < 128; ++i) { row[i] = -1; col[i] = -1; } scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("\n"); for (int j = 0; j < n; ++j) { scanf("%c", &a[i][j]); if (a[i][j] == '.') { row[i] = j; col[j] = i; } } } int rows = 0, cols = 0; for (int i = 0; i < n; ++i) { rows += row[i] != -1; cols += col[i] != -1; } if (rows < n && cols < n) { printf("-1\n"); return 0; } if (rows == n) for (int i = 0; i < n; ++i) printf("%d %d\n", i + 1, row[i] + 1); else for (int i = 0; i < n; ++i) printf("%d %d\n", col[i] + 1, i + 1); return 0; }
3
#include <bits/stdc++.h> bool is_lucky(int n) { while (n > 0) { int d = n % 10; if (d != 4 && d != 7) return 0; n /= 10; } return 1; } int main() { int n; scanf("%d", &n); bool lucky = 0; for (int i = 1; i <= n; i++) if (n % i == 0 && is_lucky(i)) lucky = 1; if (lucky) printf("YES\n"); else printf("NO\n"); getchar(); getchar(); }
1
#include <bits/stdc++.h> using namespace std; int main() { long long x; cin >> x; for (int a = 1; a <= x; a++) { for (int b = 1; b <= x; b++) { if (a % b == 0 && a * b > x && a / b < x) { cout << a << ' ' << b; return 0; } } } cout << -1; return 0; }
0
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <vector> typedef long long ll; typedef unsigned long long ull; typedef long double LD; const int INF = 2147483647; const int INF2 = 0x3f3f3f3f; const ll INF64 = 4e18; const double INFD = 1e30; const double EPS = 1e-9; const double PI = std::acos(-1); const int MOD = 100000007; template <typename T> inline T read() { T X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } const int MAXN = 505; const int MAXV = 305; int n, m, k; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0}; int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1}; int CASE = 1; int A[MAXN]; char S[MAXN]; struct Point { int X[2]; Point() {} Point(int x, int y) { X[0] = x, X[1] = y; } bool operator<(const Point& a) const { return X[0] < a.X[0] || (X[0] == a.X[0] && X[1] < a.X[1]); } }; void solve() { std::vector<Point> pts; for (int i = 0; i < 4; i++) { Point p; scanf("%d%d", &p.X[0], &p.X[1]); pts.push_back(p); } ll ans = INF64; for (int i = 0; i < 2; i++) { auto cmp = [=](const Point& a, const Point& b) { return a.X[i] < b.X[i] || (a.X[i] == b.X[i] && a.X[!i] < b.X[!i]); }; std::sort(pts.begin(), pts.end(), cmp); for (int j = 0; j < 4; j++) { for (int k = i; k < 4; k++) { int len = std::abs(pts[j].X[i] - pts[k].X[i]); for (int s = 0; s < 4; s++) { Point st; st.X[i] = pts[j].X[i]; st.X[!i] = pts[s].X[!i]; for (int d = -1; d <= 1; d += 2) { std::vector<Point> rect; rect.push_back(Point(st.X[i], st.X[!i])); rect.push_back(Point(st.X[i], st.X[!i] + d * len)); rect.push_back(Point(st.X[i] + len, st.X[!i])); rect.push_back( Point(st.X[i] + len, st.X[!i] + d * len)); std::sort(rect.begin(), rect.end()); do { ll sum = 0; for (int t = 0; t < 4; t++) sum += std::abs((ll)pts[t].X[i] - rect[t].X[0]) + std::abs((ll)pts[t].X[!i] - rect[t].X[1]); ans = std::min(ans, sum); } while ( std::next_permutation(rect.begin(), rect.end())); } } } } } printf("%lld\n", ans); } int main() { #ifdef LOCALLL freopen("in", "r", stdin); freopen("out", "w", stdout); #endif int T = read<int>(); while (T--) solve(); return 0; }
8
#pragma GCC optimize("O3") #include "bits/stdc++.h" #define sz(x) (int)(x).size() using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t k,l,r,t,x,y; cin >> k >> l >> r >> t >> x >> y; if(y < x) { if(k + y <= r) k += y; k -= x; int64_t times = (k - l) / (x - y) + 1; cout << (t > times || k < l ? "No" : "Yes") << "\n"; } else { assert(y >= x); for(int64_t iter=0; iter<=x; iter++) { int64_t take = (k - l) / x; if(t <= take) { cout << "Yes\n"; return 0; } t -= take; k -= take * x; k += y; if(k > r) { cout << "No\n"; return 0; } } cout << "Yes\n"; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 10; struct A { long long x, y, c; } a[N]; long long bz[N], mx, nx, ny, n, m; long long nmx[N], nmy[N]; double g; bool cmp(A x, A y) { return x.x < y.x; } long long gcd(long long x, long long y) { if (!y) return x; return gcd(y, x % y); } bool pd1(long long x) { if (nx == 1) return true; double p = bz[1]; for (long long i = 2; i <= nx; i++) { p = p / double(bz[i]); } if (x == 1) g = p; if (g - p > 0.0001 || p - g > 0.0001) return false; return true; } bool pd2(long long x) { if (ny == 1) return true; double p = bz[1]; for (long long i = 2; i <= ny; i++) { p = p / double(bz[i]); } if (x == 1) g = p; if (g - p > 0.0001 || p - g > 0.0001) return false; return true; } bool tmp(A x, A y) { return x.y < y.y; } int main() { scanf("%I64d", &n); long long NM = 0; for (long long i = 1; i <= n; i++) scanf("%I64d%I64d%I64d", &a[i].x, &a[i].y, &a[i].c), NM += a[i].c; mx = gcd(a[1].c, a[2].c); for (long long i = 3; i <= n; i++) { mx = gcd(mx, a[i].c); } sort(a + 1, a + n + 1, cmp); long long x = 0; for (long long i = 1; i <= n; i++) { if (a[i].x != x) x = a[i].x, nx++; a[i].x = nx; nmx[nx] += a[i].c; } sort(a + 1, a + n + 1, tmp); x = 0; for (long long i = 1; i <= n; i++) { if (a[i].y != x) x = a[i].y, ny++; a[i].y = ny; nmy[ny] += a[i].c; } a[n + 1].y = -1; for (int i = 1; i <= n; i++) { if (double(nmx[a[i].x]) / double(NM) != double(a[i].c) / double(nmy[a[i].y])) { printf("0"); return 0; } } long long ans = 0; for (long long i = 1; i <= sqrt(mx); i++) { if (mx % i == 0) ans += 2; if (i * i == mx) ans--; } printf("%I64d", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<int> ans; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; ans.clear(); ans.push_back(1); long long sum = 1; long long k = 2; for (int i = 0; i < 40; ++i) { if (sum + k < n) { ans.push_back(k); sum += k; k *= 2; } else { ans.push_back(n - sum); break; } } sort(ans.begin() + 1, ans.end()); for (int i = ans.size() - 1; i > 0; --i) { ans[i] -= ans[i - 1]; } cout << ans.size() - 1 << endl; for (int i = 1; i < ans.size(); ++i) { cout << ans[i] << ' '; } cout << endl; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; cin >> n >> m >> k; vector<vector<int> > arrays; set<int> buffer; list<int> result; for (int i = 0; i < n; ++i) { int tmp; arrays.push_back(vector<int>()); for (int j = 0; j < m; ++j) { cin >> tmp; arrays[i].push_back(tmp); } } result.clear(); for (int i = 0; i < m - 1; ++i) { buffer.clear(); int place = 0, value = 0; for (int l = 0; l < n; ++l) { value = arrays[l][i]; place = i; for (int j = i + 1; j < m; ++j) { if (k == 0) { if (value > arrays[l][j]) { value = arrays[l][j]; place = j; } } else { if (value < arrays[l][j]) { value = arrays[l][j]; place = j; } } } if (place > i) { if (k == 0) { result.push_back(i + 1); result.push_back(place + 1); } else { result.push_back(place + 1); result.push_back(i + 1); } for (int t = 0; t < n; ++t) { if (k == 0) { if (arrays[t][i] > arrays[t][place]) { value = arrays[t][i]; arrays[t][i] = arrays[t][place]; arrays[t][place] = value; } } else { if (arrays[t][i] < arrays[t][place]) { value = arrays[t][i]; arrays[t][i] = arrays[t][place]; arrays[t][place] = value; } } } } } } cout << result.size() / 2 << endl; for (list<int>::iterator it = result.begin(); it != result.end(); ++it) { cout << *it << ' '; ++it; cout << *it << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; const int N = 2e5 + 314; void foo() { int n; cin >> n; vector<int> v(n); int mn = 0, mx = 0; for (int i = 0; i < n; i++) { cin >> v[i]; if (v[i] == 1) mn = i + 1; if (v[i] == n) mx = i + 1; } if (mx > mn) { if (mn - 1 > n - mx) cout << mx - 1; else cout << n - mn; } else { if (mx - 1 > n - mn) cout << mn - 1; else cout << n - mx; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; foo(); }
0
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int INF = 1E9; const int MAXN = 100500; int n, m, u, v, c; int a[MAXN]; double ans; int main() { cin >> n >> m; for (int i = 0; i < (int)(n); i++) cin >> a[i]; ans = 0; for (int i = 0; i < (int)(m); i++) { scanf("%d%d%d", &u, &v, &c); u--, v--; if (ans * c < a[u] + a[v]) ans = 1.0 * (a[u] + a[v]) / c; } cout.precision(30); cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 1000100; long long po(int x, int y) { if (y == 0) return 1; int ret = po(x, y / 2); ret *= ret; if (y % 2) ret *= x; return ret; } long long n; long long a[N]; long long ctr; void solve() { cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; if (n <= 2) { cout << 0 << '\n'; return; } long long ans = 1e9; ; for (long long i = -1; i < 2; i++) { for (long long j = -1; j < 2; j++) { ctr = abs(i) + abs(j); for (long long w = 3; w <= n; w++) { if (a[w] != (a[1] + i) + (w - 1) * ((a[2] + j) - (a[1] + i))) { if (abs(((a[1] + i) + (w - 1) * ((a[2] + j) - (a[1] + i))) - a[w]) > 1) { ctr = 1e9; break; } else ctr++; } } ans = min(ans, ctr); ctr = 0; } } cout << (ans == 1e9 ? -1 : ans) << '\n'; } int main() { int t = 1; while (t--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long fpow(long long a, long long b) { return b ? fpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; } long long power(float x, int y) { float temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } string lower(string s) { for (int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s; } long long inv(long long i) { if (i == 1) return 1; return (mod - (mod / i) * inv(mod % i) % mod) % mod; } long long T = 1; vector<vector<long long> > v; long long n, m, x, y, k; vector<long long> dp, vis, depth; void show_tree() { for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << " "; cout << "\n"; } } void dfs(int i) { vis[i] = 1; for (auto u : v[i]) { if (!vis[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> T; long long x1, x2, y1, y2; while (T--) { cin >> x1 >> y1 >> x2 >> y2; x2 -= x1; x2++; y2 -= y1; y2++; x1 = 1; y1 = 1; cout << 1 + (x2 - 1) * (y2 - 1) << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 5e4 + 10; const int R = 50; char s[N]; int f[N][R + 10], p[N][R + 10]; int last[30]; int pre[N][30]; bool mark[N]; int main() { scanf("%s", s); int n = strlen(s); for (int i = 0; i < n; ++i) s[i] -= 'a'; for (int j = 0; j < 26; ++j) last[j] = -1; for (int i = 0; i <= n; ++i) { for (int j = 0; j < 26; ++j) pre[i][j] = last[j]; last[s[i]] = i; } for (int i = 0; i <= n; ++i) for (int j = 0; j <= R; ++j) f[i][j] = -1, p[i][j] = 0; f[0][0] = n; for (int i = 0; i < n; ++i) { for (int j = 0; j <= R; ++j) { if (f[i][j] == -1) continue; if (f[i][j] > f[i + 1][j]) { f[i + 1][j] = f[i][j]; p[i + 1][j] = 0; } int ft = pre[f[i][j]][s[i]]; if (ft > f[i + 1][j + 1]) { f[i + 1][j + 1] = ft; p[i + 1][j + 1] = 1; } } } int mx = 0, mi = -1, mj = -1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= R; ++j) { if (f[i][j] >= i - 1) { int len = 2 * j; if (f[i][j] == i - 1) len--; if (len > mx) { mx = len; mi = i; mj = j; } } } } int j = mj; for (int i = mi; i > 0; --i) { if (p[i][j] == 1) { mark[i - 1] = 1; mark[f[i][j]] = 1; j--; } } for (int i = 0; i < n; ++i) { if (mark[i]) putchar(s[i] + 'a'); } putchar('\n'); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const long double pi = acos(-1); const int MOD = 1e9 + 7; string tipos[] = {"lios", "liala", "etr", "etra", "initis", "inites"}; int eh(string &s, string &a) { if (s.size() < a.size()) return 0; if (s.substr(s.size() - a.size()) == a) { return 1; } return 0; } int main() { int homem = 0, mulher = 0; int qtd_nome = 0; vector<int> v; string s; while (true) { cin >> s; if (cin.eof()) break; int tipo = -1; for (int i = 0; i < 6; i++) { if (eh(s, tipos[i])) { tipo = i; } } if (tipo == -1) { cout << "NO" << endl; return 0; } if (!(tipo % 2)) homem = 1; else mulher = 1; if (homem && mulher) { cout << "NO" << endl; return 0; } if (tipo / 2 == 1) qtd_nome++; v.push_back(tipo / 2); } if (v.size() == 1) { cout << "YES" << endl; return 0; } if (qtd_nome != 1) { cout << "NO" << endl; return 0; } for (int i = 0; i < v.size() - 1; i++) { if (v[i] > v[i + 1]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct circle { int x, y, r, R; }; int i, j, k, m, n, l; circle c[2]; int cal(circle c1, circle c2) { int ret = 0; int dis = ((c1.x - c2.x) * (c1.x - c2.x)) + ((c1.y - c2.y) * (c1.y - c2.y)); if (c2.r >= c1.r && ((c2.r - c1.r) * (c2.r - c1.r)) >= dis) ret++; else if (c1.r >= c2.R && ((c1.r - c2.R) * (c1.r - c2.R)) >= dis) ret++; else if (((c1.r + c2.R) * (c1.r + c2.R)) <= dis) ret++; if (c2.r >= c1.R && ((c2.r - c1.R) * (c2.r - c1.R)) >= dis) ret++; else if (c1.R >= c2.R && ((c1.R - c2.R) * (c1.R - c2.R)) >= dis) ret++; else if (((c1.R + c2.R) * (c1.R + c2.R)) <= dis) ret++; return ret; } int main() { { for (i = 0; i < 2; i++) cin >> c[i].x >> c[i].y >> c[i].r >> c[i].R; int ans = 0; for (i = 0; i < 2; i++) ans += cal(c[i], c[1 - i]); cout << ans << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; char ch[100005], e[104], c; int d[105005][7], elen, ans, m[500], n, q, t, x, l, r; int main() { m['A'] = 0, m['T'] = 1, m['G'] = 2, m['C'] = 3; scanf("%s", ch), n = strlen(ch); scanf("%d", &q); for (int i = 0; i < n; i++) { d[i][m[ch[i]]]++; for (int j = 0; j < 4; j++) d[i + 2520][j] += d[i][j]; } while (q-- && scanf("%d", &t)) { if (t == 1) { scanf("%d %c", &x, &c), x--; for (int i = x; i < n; i += 2520) d[i][m[ch[x]]]--, d[i][m[c]]++; ch[x] = c; } else { scanf("%d%d%s", &l, &r, e), l--, r--, ans = 0, elen = strlen(e); for (int i = l; i < l + elen; i++) for (int j = i; j <= min(r, l + 2520 - 1); j += elen) ans += d[((r - j) / 2520) * 2520 + j][m[e[i - l]]] - d[j][m[e[i - l]]] + (ch[j] == e[i - l]); printf("%d\n", ans); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int T, t, n, m, mn = 0x3f3f3f3f, mx = 0, L[100000 + 5], R[100000 + 5], col[100000 + 5]; template <class T> void read(T &x) { char ch; bool ok; for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') ok = 1; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (ok) x = -x; } int fa[2 * 100000 + 5], to[100000 + 5]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } int main() { read(t); read(T); read(n); read(m); for (int i = (1), en = (n); i <= en; ++i) { read(L[i]); read(R[i]); mn = min(mn, R[i]); mx = max(mx, L[i]); } if (mn + mx < t) mx = t - mn; if (mn + mx > T) mn = T - mx; if (mn + mx < t || mn + mx > T || mn < 0) { puts("IMPOSSIBLE"); return 0; }; for (int i = (1), en = (2 * n); i <= en; ++i) fa[i] = i; for (int i = (1), en = (m); i <= en; ++i) { int u, v; read(u); read(v); if (find(u) == find(v)) { puts("IMPOSSIBLE"); return 0; } fa[find(n + u)] = find(v); fa[find(n + v)] = find(u); } for (int i = (1), en = (n); i <= en; ++i) if (find(n + i) != n + i) to[find(n + i)] = find(i); for (int i = (1), en = (n); i <= en; ++i) L[find(i)] = max(L[find(i)], L[i]), R[find(i)] = min(R[find(i)], R[i]); for (int i = (1), en = (n); i <= en; ++i) if (find(i) == i) { if (L[i] > R[i]) { puts("IMPOSSIBLE"); return 0; } if (!to[i]) { if (mn >= L[i] && mn <= R[i]) col[i] = 1; else if (mx >= L[i] && mx <= R[i]) col[i] = 2; else { puts("IMPOSSIBLE"); return 0; } } else { if (mn >= L[i] && mn <= R[i] && mx >= L[to[i]] && mx <= R[to[i]]) { col[i] = 1; col[to[i]] = 2; } else if (mn >= L[to[i]] && mn <= R[to[i]] && mx >= L[i] && mx <= R[i]) { col[i] = 2; col[to[i]] = 1; } else { puts("IMPOSSIBLE"); return 0; } } } puts("POSSIBLE"); printf("%d %d\n", mn, mx); for (int i = (1), en = (n); i <= en; ++i) printf("%d", col[find(i)]); return 0; }
12
#include <bits/stdc++.h> using namespace std; int n; string s; int ans = 0; int x = 0, y = 0, l = 0; int main() { cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] == 'U') { x++; } else y++; if (x > y && l == 1) { ans++; l = 2; } if (x < y && l == 2) { ans++; l = 1; } if (x > y) l = 2; if (x < y) l = 1; } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; inline bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; } inline bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; } inline int read() { int x = 0, fh = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0'); return x * fh; } string f0 = "What are you doing at the end of the world? Are you busy? Will you save " "us?"; string pre = "What are you doing while sending "; string mid = "? Are you busy? Will you send "; string suf = "?"; int pre_len, mid_len, suf_len, extra; long long len[100100]; void Init() { char yh = 34; pre = pre + yh; mid = yh + mid + yh; suf = yh + suf; len[0] = f0.size(); pre_len = pre.size(); mid_len = mid.size(); suf_len = suf.size(); extra = pre_len + mid_len + suf_len; } const int N = 12; int q; long long ln[N], lk[N]; long long maxk; int maxn, n; long long k; bool Out = false; void Dfs(int dep) { if (Out) return; if (!dep) { Out = true; putchar(f0[k - 1]); return; } if (k <= pre_len) { Out = true; putchar(pre[k - 1]); return; } k -= pre_len; if (len[dep - 1] < k) k -= len[dep - 1]; else { Dfs(dep - 1); return; } if (k <= mid_len) { Out = true; putchar(mid[k - 1]); return; } k -= mid_len; if (len[dep - 1] < k) k -= len[dep - 1]; else { Dfs(dep - 1); return; } if (k <= suf_len) { Out = true; putchar(suf[k - 1]); return; } } int main() { Init(); q = read(); for (int i = (1), _end_ = (int)(q); i <= _end_; ++i) { ln[i] = read(); scanf("%lld", &lk[i]); chkmax(maxn, (int)ln[i]); maxk = max(maxk, lk[i]); } for (int i = (1), _end_ = (int)(maxn); i <= _end_; ++i) { len[i] = len[i - 1] * 2 + extra; if (len[i] > maxk) len[i] = maxk + 1; } for (int i = (1), _end_ = (int)(q); i <= _end_; ++i) { n = ln[i]; k = lk[i]; if (len[n] < k) { putchar('.'); continue; } Out = false; Dfs(n); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int N; vector<pair<int, int> > V; int arr[101010]; int pri[101010]; int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%d", arr + i); for (int i = 0; i < N; ++i) V.emplace_back(arr[i] + arr[(i + 1) % N] - N, i); sort(V.begin(), V.end()); for (int i = 0; i < N; ++i) pri[V[i].second] = i; for (int i = 0; i < N; ++i) printf("%d ", pri[i]); puts(""); return 0; }
6
#include <bits/stdc++.h> using namespace std; char gra[505][505]; const int N = 1E+5; long long a[N], t[N]; long long s[N]; long long ans[N]; int c[N]; int binary(int low, int high, long long x) { while (low <= high) { int mid = (low + high) / 2; if (s[mid] < x) low = mid + 1; else high = mid - 1; } return low; } int main() { int n, m; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> t[i], s[i] = s[i - 1] + t[i]; for (int i = 1; i <= n; ++i) { int d = lower_bound(s + 1, s + n + 1, a[i] + s[i - 1]) - (s); long long left = (a[i] - (s[d - 1] - s[i - 1])); ans[d] += left; c[i]++; c[d]--; } int ma = 0; for (int i = 1; i <= n; ++i) { ma += c[i]; ans[i] += (t[i] * ma); } for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], i == n ? '\n' : ' '); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mx = (1e5) + 1; const long long mod = 1000000007; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b, x; cin >> a >> b >> x; if (x % 2 == 0) { if (a > b) { for (int i = 0; i < x / 2; i++) { cout << "01"; } cout << string(b - x / 2, '1'); cout << string(a - x / 2, '0'); } else { for (int i = 0; i < x / 2; i++) { cout << "10"; } cout << string(a - x / 2, '0'); cout << string(b - x / 2, '1'); } } else if (a > b) { for (int i = 0; i < x / 2; i++) { cout << "01"; } cout << string(a - x / 2, '0'); cout << string(b - x / 2, '1'); } else { for (int i = 0; i < x / 2; i++) { cout << "10"; } cout << string(b - x / 2, '1'); cout << string(a - x / 2, '0'); } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int a, b, ak; long long ans = (1LL << 60); void cal(int g) { int x = a / g * g; if (x < a) x += g; int k = x - a; int gg = gcd1(a + k, b + k); long long tmp = (long long)(a + k) * (b + k) / gg; if (tmp < ans) { ans = tmp; ak = k; } } void fmain(int tid) { cin >> a >> b; if (a == b) { puts("0"); return; } if (a > b) swap(a, b); int c = b - a; for (int i = 1; (long long)i * i <= c; i++) if (c % i == 0) { cal(i); cal(c / i); } printf("%d\n", ak); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, i, j, nxt[1000005], xhj[1000005], ans[1000005]; string st; int main() { scanf("%d%d", &n, &m); { char ss[1000005]; scanf(" %s", ss); (st) = ss; }; st = " " + st; j = 0; for (i = 2; i <= n; i++) { while (j && st[i] != st[j + 1]) j = nxt[j]; if (st[i] == st[j + 1]) { nxt[i] = ++j; } else { nxt[i] = 0; } } for ((i) = 1; (i) <= (n); (i)++) { xhj[i] = i - nxt[i]; int t = i / xhj[i]; if (nxt[t * xhj[i]] != (t - 1) * xhj[i]) continue; int ub = i / m, lb = i / (m + 1) + 1; ub /= xhj[i]; lb = (lb + xhj[i] - 1) / xhj[i]; if (ub >= lb) ans[i] = 1; if ((i % m == 0) && (i / m % xhj[i] == 0)) ans[i] = 1; if ((i % (m + 1) == 0) && (i / (m + 1) % xhj[i] == 0)) ans[i] = 1; } for ((i) = 1; (i) <= (n); (i)++) printf("%d", ans[i]); return 0; }
7
#include <bits/stdc++.h> using namespace std; char a[15]; bool yes[20]; int main() { int t; scanf("%d", &t); while (t--) { int ans = 0; memset(a, 0, sizeof(a)); memset(yes, 0, sizeof(yes)); scanf("%s", a); for (int i = 12; i >= 1; --i) { if (i != 1 && i != 2 && i != 3 && i != 4 && i != 6 && i != 12) continue; for (int j = 0; j < i; ++j) { int k = 0; bool ok = 1; while (j + k * i < 12) { if (a[j + k * i] != 'X') { ok = 0; break; } ++k; } if (ok) { ++ans; yes[12 / i] = 1; break; } } } printf("%d", ans); for (int i = 1; i <= 12; ++i) if (yes[i]) printf(" %dx%d", i, 12 / i); printf("\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; bool dp[200011]; long long n, m, l, p, d; long long a[200011]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> l; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > l) dp[i] = 1; } long long ans = 0; for (int i = 1; i <= n; i++) { if (dp[i - 1] == 0 && dp[i]) ans++; } bool b; while (m--) { cin >> b; if (b) { cin >> p >> d; if (dp[p]) continue; else a[p] += d; if (a[p] > l) dp[p] = 1; if (dp[p] && dp[p - 1] == 0 && dp[p + 1] == 0) ans++; else if (dp[p] && dp[p - 1] && dp[p + 1]) ans--; } else { cout << ans << '\n'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long a, b, SUMA, SUMB; int main() { cin >> a >> b; SUMA = ((b * (b - 1LL)) / 2LL) % MOD; SUMA = (SUMA * a) % MOD; SUMB = (b * (b - 1LL)) / 2LL % MOD; SUMB = (b * SUMB) % MOD; SUMB = (SUMB * (((a * (a + 1LL)) / 2LL) % MOD) % MOD); cout << (SUMA + SUMB) % MOD; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MX = 1e5 + 10, MD = 1e9 + 7; long long n, m, a[MX], ans, tmp, mx, cnt[MX]; vector<long long> divisor; long long Pow(long long a, long long b) { if (b == 0) return 1; long long tmp = Pow(a, b / 2); tmp = tmp * tmp % MD; if (b & 1) tmp = tmp * a % MD; return tmp; } void divisors(int a) { divisor.clear(); for (int i = 1; i * i <= a; i++) { if (a % i == 0) { divisor.push_back(i); if (i * i != a) { divisor.push_back(a / i); } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> a[1]; cnt[a[1]]++; mx = a[1]; for (int i = 2; i <= n; i++) { cin >> a[i]; cnt[a[i]]++; mx = max(a[i], mx); } for (int i = 1; i < MX; i++) { cnt[i] += cnt[i - 1]; } for (int i = 1; i <= mx; i++) { divisors(i); sort(divisor.begin(), divisor.end()); tmp = 1; int k = divisor.size(); for (int j = 1; j < k; j++) { tmp = tmp * Pow(j, cnt[divisor[j] - 1] - cnt[divisor[j - 1] - 1]) % MD; } tmp = tmp * Pow(k, cnt[mx] - cnt[divisor[k - 1] - 1]) % MD - tmp * Pow(k - 1, cnt[mx] - cnt[divisor[k - 1] - 1]) % MD; tmp = (tmp + MD) % MD; ans += tmp; ans = ans % MD; } cout << ans; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k, cntr = 1, s = 1; cin >> n >> m >> k; string str; cin >> str >> str; if (str == "head") cntr = -1; cin >> str; int l = str.length(); if (m < k) s = -1; for (int i = 0; i < l; i++) { if (str[i] == '0') { if (m != 1 && m != n) m += s; } else { if (cntr == 1) m = 1; else m = n; } k += cntr; if (k == n) cntr = -1; else if (k == 1) cntr = 1; if (m == k) { cout << "Controller " << i + 1 << "\n"; return 0; } } cout << "Stowaway\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int halfcounter[1 << 16]; int halfmask = 0xFFFF; int countBits(unsigned n) { return halfcounter[n >> 16] + halfcounter[n & halfmask]; } int q, bucket[2][32][200005 >> 5]; int p1, p2, len; string s; int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 1, I = (1 << 16); i < I; ++i) halfcounter[i] = halfcounter[i >> 1] + (i & 1); for (int i = 0; i < 2; ++i) { cin >> s; int n = s.length(); for (int j = 0; j < 32; ++j) { for (int k = 0; k < n - j; ++k) { bucket[i][j][k >> 5] |= ((s[j + k] == '1') << (k & 0x1F)); } } } cin >> q; while (q--) { cin >> p1 >> p2 >> len; int *b1 = bucket[0][p1 & 0x1F] + (p1 >> 5); int *b2 = bucket[1][p2 & 0x1F] + (p2 >> 5); int times = len >> 5; int ham = 0; for (int i = 0; i < times; ++i) { ham += countBits(b1[i] ^ b2[i]); } int rest = len & 0x1F; if (rest) { rest = (1 << rest) - 1; ham += countBits((b1[times] ^ b2[times]) & rest); } cout << ham << '\n'; } cout << flush; }
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const double eps = 1e-9; const int MAX_N = 1e6 + 5; const int inf = 2e9; const long long Inf = (long long)1e18; const long long M = (long long)1e9 + 7; int Int() { int x; scanf("%d", &x); return x; } long long Long() { long long x; scanf("%lld", &x); return x; } void Char(char* ch) { scanf("%s", ch); } template <typename t> inline t abs(t a) { if (a >= 0) return a; return -a; } template <typename t> inline t gcd(t a, t b) { return !b ? a : gcd(b, a % b); } template <typename t> inline t lcm(t a, t b) { return a * (b / gcd(a, b)); } template <typename T> T mul(T b, T p, T mod) { T res = 0; while (p > 0) { if (p & 1) { res = (res + b) % mod; } b = (b * 2) % mod; p >>= 1; } return res % mod; } template <typename T> inline T modpow(T b, T p, T Mod) { T res = 1; while (p) { if (p & 1) { res = mul(res, b, Mod); res %= Mod; } b = mul(b, b, Mod); b %= Mod; p >>= 1; } return res % Mod; } template <typename T> inline T ModInv(T b, T Mod) { return modpow(b, Mod - 2, Mod); } template <typename T> inline T pwr(T b, T p) { T res = 1; while (p > 0) { if (p & 1) { res *= b; } b *= b; p >>= 1; } return res; } int e1, e2, d1; template <typename T> inline void extgcd(T a, T b) { if (!b) { e1 = 1, e2 = 0, d1 = a; return; } extgcd(b, a % b); T x1 = e2; T y1 = e1 - (a / b) * e2; e1 = x1; e2 = y1; } template <typename T> inline vector<T> Unique(vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...); } int main() { int t = 1, tc = 0; while (t--) { int n = Int(); map<string, set<int> > mp; string a[n + 1]; for (int i = 1; i <= n; i++) { string s; cin >> s; a[i] = s; int sz = (int)s.size(); for (int j = 0; j < sz; ++j) { string tmp = ""; for (int k = j; k < sz; k++) { tmp += s[k]; mp[tmp].insert(i); } } } int q = Int(); while (q--) { string s; cin >> s; if (mp.find(s) == mp.end()) puts("0 -"); else printf("%d %s\n", (int)mp[s].size(), a[*mp[s].begin()].c_str()); } } return 0; }
3
#include <bits/stdc++.h> int graph[110][110]; char used[110]; int n; int dfs(int u) { int v; used[u] = 1; for (v = 1; v <= n; v++) if (graph[u][v] && !used[v]) return ((graph[u][v] > 0) ? dfs(v) : (dfs(v) - graph[u][v])); return ((graph[u][1] > 0) ? 0 : (-graph[u][1])); } int main() { int u, v, i, c; int best; scanf("%d", &n); for (u = 1; u <= n; u++) for (v = 1; v <= n; v++) graph[u][v] = 0; int total = 0; for (i = 0; i < n; i++) { scanf("%d %d %d", &u, &v, &c); graph[u][v] = 1; graph[v][u] = -c; total += c; } best = dfs(1); if (total - best < best) best = total - best; printf("%d\n", best); return 0; }
3
#include <bits/stdc++.h> using namespace std; int dcmp(double a, double b) { return ((fabs(a - b) < (1e-9)) ? 0 : (a < b) ? -1 : +1); } double crossP(const complex<double> &v1, const complex<double> &v2) { return (conj(v1) * v2).imag(); } double dotP(const complex<double> &v1, const complex<double> &v2) { return (conj(v1) * v2).real(); } double lengthSqr(const complex<double> &p) { return dotP(p, p); } double length(const complex<double> &p) { return hypot(p.real(), p.imag()); } double fixAngle(double a) { return ((a > 1) ? 1 : (a < -1) ? -1 : a); } bool same(const complex<double> &p1, const complex<double> &p2) { return ((dcmp(p1.real(), p2.real()) == 0 && dcmp(p1.imag(), p2.imag()) == 0) ? 1 : 0); } double getAngle_A_abc(double a, double b, double c) { return acos(fixAngle((b * b + c * c - a * a) / (2 * b * c))); } const int N = 1000 + 1; char buf[N]; string v[N]; int main() { int n = 0; while (gets(buf)) v[n++] = string(buf); int maxlen = -1; for (auto i = 0; i < n; ++i) maxlen = max(maxlen, ((int)((v[i]).size()))); puts(string(maxlen + 2, '*').c_str()); bool turn = 0; for (auto i = 0; i < n; ++i) { int r = maxlen - ((int)((v[i]).size())); int a = r / 2, b = r / 2; if (r & 1) { if (turn) a++; else b++; turn = !turn; } puts(("*" + string(a, ' ') + v[i] + string(b, ' ') + "*").c_str()); } puts(string(maxlen + 2, '*').c_str()); return 0; }
2
#include <bits/stdc++.h> using namespace std; int v[1000][1000]; set<int> st; char erat[200000 + 100]; int main() { int n, m, t; cin >> n >> m; for (int i = 0; i < 200000; ++i) erat[i] = true; for (int i = 2; i < 110000; ++i) { if (erat[i]) { for (int j = i + i; j < 150000; j += i) erat[j] = false; st.insert(i); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf("%d", &t); v[i][j] = 0; if (t == 1) v[i][j] = 1; else { v[i][j] = (*st.upper_bound(t - 1)) - t; } } } int min = (int)1e9; for (int i = 0; i < n; ++i) { int sum = 0; for (int j = 0; j < m; ++j) sum += v[i][j]; if (sum < min) min = sum; } for (int j = 0; j < m; ++j) { int sum = 0; for (int i = 0; i < n; ++i) sum += v[i][j]; if (sum < min) min = sum; } cout << min; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int N = 1e5 + 10; const long long INF = 1e15 + 7; void solve() { long long n, m; cin >> n >> m; vector<long long> v(n); for (auto& x : v) cin >> x; vector<string> s(n); for (auto& x : s) cin >> x; long long nax = 0; vector<long long> ans(m, 0); for (int i = 0; i < (1 << n); i++) { vector<long long> cnt(m, 0); vector<long long> res(m, 0); vector<int> flag(n, -1); for (int j = 0; j < n; j++) { if (i & (1 << j)) flag[j] = 1; for (int k = 0; k < m; k++) { if (s[j][k] == '1') cnt[k] += flag[j]; } } vector<pair<long long, long long> > temp; for (int j = 0; j < m; j++) temp.push_back({cnt[j], j}); sort(temp.begin(), temp.end()); long long num = 1; for (auto j : temp) { res[j.second] = num; num++; } long long curr = 0; for (int j = 0; j < n; j++) { long long now = 0; for (int k = 0; k < m; k++) { if (s[j][k] == '1') now += res[k]; } curr += abs(now - v[j]); } if (curr >= nax) { nax = curr; ans = res; } } for (auto i : ans) cout << i << " "; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; cin >> tests; while (tests--) { solve(); } }
7
#include <iostream> #include <string> using namespace std; #define int long long int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int test_case; cin >> test_case; while (test_case--) { int n, a, b; cin >> n >> a >> b; string str; cin >> str; if (b >= 0) { // 每次只刪除1個 讓b能夠增加最多次 cout << (a + b) * n << endl; continue; } // b < 0 使操作次數最少 即增加 b 最少次 int Interval = 1; for (int i = 0; i < n - 1; i++) { if (str[i] != str[i + 1]) Interval++; } cout << (a * n) + (Interval / 2 + 1) * b << endl; } } /* 3 3 2 0 000 5 -2 5 11001 6 1 -4 100111 */ /* 6 15 -2 */
1
#include <bits/stdc++.h> using namespace std; const long long MAXN = 100005; const long long mod = 1e9 + 7; void unstopable() { int n; cin >> n; int ans = n; for (long long i = 1, ThxDem = n + 1; i < ThxDem; i++) ans += i * (n - i); cout << ans; } signed main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); unstopable(); }
1
#include <bits/stdc++.h> using namespace std; const double DINF = numeric_limits<double>::infinity(); const long long MOD = 1e9 + 7; const double EpS = 1e-7; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long rnd(long long x, long long y) { static mt19937 mmtw(time(0)); static uniform_int_distribution<long long> rd; return rd(mmtw) % (y - x + 1) + x; } const int MAXN = 1e5 + 5; int n; long long dp[MAXN][3][2]; long long a[MAXN][3]; void relax(long long &to, long long val) { if (val > to) to = val; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < 3; ++i) { for (int j = 0; j < n; ++j) { cin >> a[j][i]; } } for (int i = 0; i < MAXN; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { dp[i][j][k] = -1e18; } } } dp[0][0][1] = a[0][0]; dp[0][0][0] = -1e18; dp[0][1][1] = -1e18; dp[0][1][0] = a[0][0] + a[0][1]; dp[0][2][1] = -1e18; dp[0][2][0] = a[0][0] + a[0][1] + a[0][2]; for (int i = 0; i < n - 1; ++i) { relax(dp[i + 1][0][1], dp[i][0][0] + a[i + 1][0]); relax(dp[i + 1][1][0], dp[i][0][0] + a[i + 1][0] + a[i + 1][1]); relax(dp[i + 1][2][0], dp[i][0][0] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][0][1], dp[i][0][1] + a[i + 1][0]); relax(dp[i + 1][1][0], dp[i][0][1] + a[i + 1][0] + a[i + 1][1]); relax(dp[i + 1][2][0], dp[i][0][1] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][2][0], dp[i][0][1] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2] + a[i][1] + a[i][2]); relax(dp[i + 1][0][0], dp[i][1][0] + a[i + 1][0] + a[i + 1][1]); relax(dp[i + 1][1][0], dp[i][1][0] + a[i + 1][1]); relax(dp[i + 1][2][0], dp[i][1][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][0][0], dp[i][2][0] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][1][0], dp[i][2][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][2][1], dp[i][2][0] + a[i + 1][2]); relax(dp[i + 1][0][0], dp[i][2][1] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][1][0], dp[i][2][1] + a[i + 1][1] + a[i + 1][2]); relax(dp[i + 1][2][1], dp[i][2][1] + a[i + 1][2]); relax(dp[i + 1][0][0], dp[i][2][1] + a[i + 1][0] + a[i + 1][1] + a[i + 1][2] + a[i][0] + a[i][1]); } cout << max(dp[n - 1][2][0], dp[n - 1][2][1]) << endl; }
7
#include <bits/stdc++.h> using namespace std; struct less_key { bool operator()(const pair<int64_t, int64_t>& p1, const pair<int64_t, int64_t>& p2) { return p1.first < p2.first || (p1.first == p2.first && p1.second > p2.second); } }; struct pair_hash { std::size_t operator()(const pair<int64_t, int64_t>& k) const { return static_cast<size_t>(k.first ^ k.second); } }; int64_t n; char a[202]; int64_t x; int64_t y; int64_t cx; int64_t cy; void move(char c, int64_t& x, int64_t& y) { switch (c) { case 'U': y++; break; case 'D': y--; break; case 'L': x--; break; case 'R': x++; break; } } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int64_t i = 1; i <= n; i++) { cin >> a[i]; } int64_t S = 0; for (int64_t i = 0; i <= n; i++) { x = cx; y = cy; for (int64_t j = i + 1; j <= n; j++) { move(a[j], x, y); if (cx == x && cy == y) { S++; } } move(a[i], cx, cy); } cout << S; return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") const long long INF = 1e9 + 7; const long long maxn = 2e5 + 7; using namespace std; void solve() { long long n; cin >> n; vector<long long> a(n); vector<long long> t(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> t[i]; } vector<pair<long long, long long> > v(n); for (long long i = 0; i < n; i++) { v[i] = {a[i], t[i]}; } sort(v.begin(), v.end()); priority_queue<long long> pq; int prev = v[0].first; long long ans = 0; long long currSum = 0; for (long long i = 0; i < n; i++) { auto j = i; long long currNum = v[i].first; long long spacesBn = (currNum - (prev + 1)); for (long long s = 0; s < spacesBn; s++) { if (pq.empty()) break; auto x = pq.top(); pq.pop(); currSum -= x; ans += (x * (s + 1)); } ans += (currSum * (currNum - prev)); while (j < n && v[j].first == v[i].first) { j++; } j--; if (i != j) { for (long long x = i; x < j; x++) { pq.push(v[x].second); currSum += v[x].second; } } if (!pq.empty() && pq.top() > v[j].second) { currSum -= pq.top(); pq.pop(); pq.push(v[j].second); currSum += v[j].second; } prev = currNum; i = j; } auto cost = 1; while (!pq.empty()) { auto x = pq.top(); pq.pop(); ans += (x * (cost)); cost++; } cout << ans << '\n'; } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; while (T--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 2; const long long mod = 1000000007; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); vector<long long> cnt(200); long long n; cin >> n; set<long long> st; vector<long long> age(100); for (int i = 0; i < n; i++) st.insert(i + 1); vector<long long> f, s; set<long long> boro[100], choto[100]; for (int i = 0; i + 1 < n * (n - 1) / 2; i++) { long long x, y; cin >> x >> y; f.push_back(x); s.push_back(y); cnt[x]++, cnt[y]++; if (cnt[x] == n - 1) st.erase(x); if (cnt[y] == n - 1) st.erase(y); boro[x].insert(y); choto[y].insert(x); } long long a = *st.begin(), b = *--st.end(); long long aboro = 0; for (long long e : boro[a]) { for (long long x : boro[e]) { if (x == b) { aboro = 1; } } } if (aboro) cout << a << " " << b; else cout << b << " " << a; }
2
#include<bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f #define Inf 0x3f3f3f3f3f3f3f3f #define rep(i, a, b) for(int i = a; i < (b); ++i) #define per(i, a, b) for(int i = a; i >= (b); --i) using namespace std; const int maxn = 1 << 16; int val[maxn], res1[maxn], res2[maxn], dis[maxn]; vector<int> adj[maxn]; queue<int> q; bool vis[maxn]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while(tt--){ int n; cin >> n; rep(i, 0, 1 << n) adj[i].clear(); rep(i, 0, n * (1 << n - 1)){ int u,v; cin >> u >> v; adj[u].push_back(v),adj[v].push_back(u); } val[0] = 0; rep(i, 0, 1 << n) vis[i] = 0, val[i] = 0; rep(i, 0, n) val[adj[0][i]] = 1 << i, vis[adj[0][i]] = 1, q.push(adj[0][i]), dis[adj[0][i]] = 1; vis[0] = 1, dis[0] = 0; while(!q.empty()){ int u = q.front(); q.pop(); for(auto v: adj[u]){ if(vis[v]){ if(dis[v] == dis[u] + 1) val[v] |= val[u]; continue; } q.push(v); vis[v] = 1; val[v] |= val[u]; dis[v] = dis[u] + 1; } } //rep(i, 0, 1 << n) cout << val[i] << ' ' << dis[i] << "\n"; rep(i, 0, 1 << n) res1[val[i]] = i; rep(i, 0, 1 << n) cout << res1[i] << ' '; cout << "\n"; if(__builtin_popcount(n) != 1){ cout << "-1\n"; continue; } rep(i, 0, 1 << n){ int mask = 0; rep(j, 0, n) if((i >> j) & 1) mask ^= j; res2[res1[i]] = mask; } rep(i, 0, 1 << n) cout << res2[i] << ' '; cout << "\n"; } }
9
#include <bits/stdc++.h> using namespace std; double a[1000005]; int b[1000005]; int main() { int n, x, tot = 1, cnt; double tempx; scanf("%d", &n); scanf("%d", &x); a[1] = x; b[1] = 1; for (int i = (2); i <= (n); ++i) { scanf("%d", &x); cnt = 1; tempx = x; while (tot && a[tot] - tempx > 1e-9) { tempx = (tempx * cnt + a[tot] * b[tot]) / (cnt + b[tot]); cnt = cnt + b[tot]; --tot; } ++tot; a[tot] = tempx; b[tot] = cnt; } for (int i = (1); i <= (tot); ++i) for (int j = (1); j <= (b[i]); ++j) printf("%.9f\n", a[i]); }
6
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const long long INF = 1000000000000000000; vector<pair<int, int> > v(8); vector<int> sq(4), par(4); double di(pair<int, int> a, pair<int, int> b) { double res = 0; res = sqrt((double)(a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); return res; } bool isSquare(vector<int> ind) { vector<vector<double> > d(4, vector<double>(4)); for (int _n(4), i(0); i < _n; i++) { for (int _n(4), j(0); j < _n; j++) { d[i][j] = (di(v[ind[i]], v[ind[j]])); } } bool ok = false; if (fabs(d[0][1] - d[0][2]) < EPS && fabs(d[0][1] - d[2][3]) < EPS && fabs(d[0][1] - d[1][3]) < EPS && fabs(d[1][2] - d[0][3]) < EPS) { ok = true; } if (fabs(d[0][2] - d[0][1]) < EPS && fabs(d[0][2] - d[1][3]) < EPS && fabs(d[0][2] - d[2][3]) < EPS && fabs(d[2][1] - d[0][3]) < EPS) { ok = true; } if (fabs(d[0][3] - d[0][2]) < EPS && fabs(d[0][3] - d[2][1]) < EPS && fabs(d[0][3] - d[3][1]) < EPS && fabs(d[3][2] - d[0][1]) < EPS) { ok = true; } if (fabs(d[0][1] - d[0][3]) < EPS && fabs(d[0][1] - d[3][2]) < EPS && fabs(d[0][1] - d[1][2]) < EPS && fabs(d[1][3] - d[0][2]) < EPS) { ok = true; } return ok; } bool isPar(vector<int> ind) { vector<vector<double> > d(4, vector<double>(4)); for (int _n(4), i(0); i < _n; i++) { for (int _n(4), j(0); j < _n; j++) { d[i][j] = (di(v[ind[i]], v[ind[j]])); } } bool ok = false; if (fabs(d[0][1] - d[2][3]) < EPS && fabs(d[0][2] - d[1][3]) < EPS && fabs(d[0][3] - d[1][2]) < EPS) { ok = true; } if (fabs(d[0][2] - d[1][3]) < EPS && fabs(d[0][1] - d[2][3]) < EPS && fabs(d[0][3] - d[2][1]) < EPS) { ok = true; } if (fabs(d[0][3] - d[2][1]) < EPS && fabs(d[0][2] - d[3][1]) < EPS && fabs(d[0][1] - d[3][2]) < EPS) { ok = true; } if (fabs(d[0][1] - d[3][2]) < EPS && fabs(d[0][3] - d[1][2]) < EPS && fabs(d[0][2] - d[1][3]) < EPS) { ok = true; } return ok; } int main() { int n, m; for (int _n(8), i(0); i < _n; i++) { pair<int, int> a; cin >> a.first >> a.second; v[i] = a; } vector<int> b(8); for (int _n(4), i(0); i < _n; i++) b[i] = 1; sort((b).begin(), (b).end()); int ii = 0, jj = 0; bool ok = false; do { for (int _n(8), i(0); i < _n; i++) { if (b[i]) sq[ii++] = i; else par[jj++] = i; } if (isSquare(sq) && (isSquare(par) || isPar(par))) { ok = true; break; } ii = 0; jj = 0; } while (next_permutation(b.begin(), b.end())); if (ok) { cout << "YES" << endl; for (int _n(4), i(0); i < _n; i++) { if (i) cout << " "; cout << sq[i] + 1; } cout << endl; for (int _n(4), i(0); i < _n; i++) { if (i) cout << " "; cout << par[i] + 1; } cout << endl; } else cout << "NO" << endl; }
4
#include <bits/stdc++.h> using namespace std; int e, s, n, m, st, q[4][200000 + 10]; pair<int, int> a[200000 * 2 + 10], f[200000 * 2 + 10], ans[200000 + 10]; int main() { int i, front[4], rear[4], j; memset(f, 0xff, sizeof f); memset(ans, 0xff, sizeof ans); scanf("%d%d%d%d", &e, &s, &n, &m); for (i = 1; i <= n; i++) scanf("%d%d", &a[i].second, &a[i].first); for (i = 1; i <= m; i++) { scanf("%d", &a[i + n].first); a[i + n].second = 3 + i; } sort(a + 1, a + n + m + 1); for (i = 1; i <= n + m; i++) if (a[i].first > e) break; if (a[i].first == e) i--; a[i].first = e, a[i].second = 3; e = i; f[e] = make_pair(0, 0); q[3][0] = e; front[3] = rear[3] = 0; front[2] = front[1] = 0; rear[2] = rear[1] = -1; for (i = e - 1; i; i--) { int t = 0x7fffffff; while (front[3] <= rear[3] && a[q[3][front[3]]].first - a[i].first > s) front[3]++; while (front[2] <= rear[2] && a[q[2][front[2]]].first - a[i].first > s) front[2]++; while (front[1] <= rear[1] && a[q[1][front[1]]].first - a[i].first > s) front[1]++; if (a[i].second > 3) { for (j = 3; j > 0; j--) if (front[j] <= rear[j]) { if (j == 1) ans[a[i].second - 3] = make_pair(f[q[j][front[j]]].first - s + a[q[j][front[j]]].first - a[i].first, f[q[j][front[j]]].second); else if (j == 2) ans[a[i].second - 3] = make_pair(f[q[j][front[j]]].first, f[q[j][front[j]]].second - s + a[q[j][front[j]]].first - a[i].first); else ans[a[i].second - 3] = make_pair(f[q[j][front[j]]].first, f[q[j][front[j]]].second); break; } if (!i) ans[a[i].second] = make_pair(-1, -1); continue; } for (j = 3; j >= a[i].second; j--) if (front[j] <= rear[j]) t = min(t, q[j][rear[j]]); if (t == 0x7fffffff) for (; j; j--) if (front[j] <= rear[j]) { t = q[j][front[j]]; break; } if (t == 0x7fffffff) break; f[i] = f[t]; if (a[t].second >= a[i].second) { if (a[i].second == 1) f[i].first += a[t].first - a[i].first; else if (a[i].second == 2) f[i].second += a[t].first - a[i].first; } else { if (a[i].second == 2) { f[i].second += s; f[i].first -= s - a[t].first + a[i].first; } else if (a[i].second == 3) { if (a[t].second == 2) f[i].second -= s - a[t].first + a[i].first; else f[i].first -= s - a[t].first + a[i].first; } } q[a[i].second][++rear[a[i].second]] = i; } for (i = 1; i <= m; i++) printf("%d %d\n", ans[i].first, ans[i].second); }
10
#include <bits/stdc++.h> using namespace std; const int INF = 1034567891; long long L[200050]; long long sum[200050]; long long T[200050]; vector<long long> Va; int main() { int N, i, j; long long R; scanf("%d %lld", &N, &R); for (i = 1; i <= N; i++) { scanf("%lld", L + i); sum[i] = sum[i - 1] + L[i]; } for (i = 1; i <= N; i++) scanf("%lld", T + i); for (i = 1; i <= N; i++) { if (L[i] > T[i]) return !printf("-1\n"); T[i] = max(0ll, 2 * L[i] - T[i]); } long long st = 0, ans = 0; long long curt = 0; for (i = 1; i <= N; i++) { st = max(st, sum[i - 1]); long long d = min(L[i], st - sum[i - 1]); T[i] = max(0ll, T[i] - d); if (T[i] == 0) { curt += 2 * L[i] - d; continue; } curt += d; curt += (L[i] - T[i] - d) * 2; long long c = (T[i] + R - 1) / R; ans += c; st = sum[i] + c * R - T[i]; for (j = 0; j < c && Va.size() < 100000; j++) Va.push_back(curt + R * j); curt += T[i]; } printf("%lld\n", ans); if (ans <= 100000) { for (auto it : Va) printf("%lld ", it); printf("\n"); } return 0; }
7
#include <bits/stdc++.h> int main() { int a, b, c; int s, m, x, y, p, q; scanf("%d%d%d", &a, &b, &c) == 3; s = a + b + c; m = a * b * c; x = (a + b) * c; y = a * (b + c); p = (a * b) + c; q = a + (b * c); if (s >= m && s >= x && s >= y && s >= p && s >= q) { printf("%d", s); } else if (m >= s && m >= x && m >= y && m >= p && m >= q) { printf("%d", m); } else if (x >= s && x >= m && x >= y && x >= p && x >= q) { printf("%d", x); } else if (y >= s && y >= m && y >= x && y >= p && y >= q) { printf("%d", y); } else if (p >= s && p >= m && p >= x && p >= y && p >= q) { printf("%d", p); } else if (q >= s && q >= m && q >= x && q >= y && q >= p) { printf("%d", s); } }
1
#include <bits/stdc++.h> #pragma warning(disable : 4996) const long long MOD = 1000000007; using namespace std; char str[55][55]; int main(int argc, char* argv[]) { int n; scanf("%d", &n); int i, j; for (i = 0; i < n; i++) { scanf("%s", str[i]); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (str[i][j] == '.') { if (j == 0 || j == n - 1 || i >= n - 2) { printf("NO\n"); return 0; } if (str[i + 1][j - 1] == '#' || str[i + 1][j] == '#' || str[i + 1][j + 1] == '#' || str[i + 2][j] == '#') { printf("NO\n"); return 0; } str[i + 1][j - 1] = str[i + 1][j] = str[i + 1][j + 1] = str[i + 2][j] = '#'; } } } printf("YES\n"); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } string to_string(char c) { return "'" + string(1, c) + "'"; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ": " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = false; string r = "{"; for (auto x : v) { if (f) r += ", "; r += to_string(x); f = true; } return r += "}"; } template <typename A> string to_string(vector<vector<A>> v) { string r; for (auto x : v) r += "\n" + to_string(x); return r; } int Nerr; template <typename A> string to_string(A *p) { return to_string(vector<A>(p, p + Nerr)); } void err(istream_iterator<string>) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " =: " << to_string(a) << "; "; err(++it, args...); } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } int const MOD = 1e9 + 7; long long const INF = 1e18 + 42; const int N = 1e6 + 42, K = 26; int to[N][K], lk[N], qu[N], len[N], vis[N]; string a[N]; vector<int> g[N]; pair<int, int> dfs(int u) { vis[u] = 1; int x = 1, y = 0; for (int v : g[u]) if (!vis[v]) { auto [cx, cy] = dfs(v); x += cy; y += cx; } return {max(x, y), y}; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int tt; cin >> tt; while (tt--) { int n; cin >> n; int L = 10; for (int i = 1; i <= n; ++i) { cin >> a[i]; L += int((a[i]).size()); } for (int i = 0; i < L; ++i) { g[i].clear(); vis[i] = 0; lk[i] = len[i] = 0; for (int j = 0; j < K; ++j) to[i][j] = 0; } int ctr = 1; for (int i = 1; i <= n; ++i) { int u = 0; for (char &c : a[i]) { c -= 'a'; int &v = to[u][c]; if (!v) v = ctr++; len[v] = 1 + len[u]; u = v; } } int fp = 0, bp = 0; qu[fp++] = 0; while (bp < fp) { int u = qu[bp++]; int w = lk[u]; if (w && len[u] == 1 + len[w]) { g[u].push_back(w); g[w].push_back(u); 42; ; } for (int i = 0; i < K; ++i) { int &v = to[u][i]; if (v) { lk[v] = u ? to[w][i] : 0; qu[fp++] = v; } else { v = to[w][i]; } } } int ans = 0; for (int i = 1; i < ctr; ++i) { if (!vis[i]) { ans += dfs(i).first; } } cout << ans << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; constexpr int Maxn = 100005, Maxs = Maxn * 22; int n, m; vector<int> g[Maxn]; int latest[Maxn]; int root, indexed; int par[Maxn], sz[Maxn], son[Maxn], dep[Maxn]; int top[Maxn], ind[Maxn], rind[Maxn]; void dfs1(int u, int depth) { sz[u] = 1; dep[u] = depth; son[u] = 0; for (auto &v : g[u]) { dfs1(v, depth + 1); sz[u] += sz[v]; if (sz[v] > sz[son[u]]) { son[u] = v; } } } void dfs2(int u, int topv) { top[u] = topv; ++indexed; ind[u] = indexed, rind[indexed] = u; if (son[u]) dfs2(son[u], topv); for (auto &v : g[u]) { if (v == son[u]) continue; dfs2(v, v); } } void start() { memset(sz, 0, sizeof(sz)); memset(son, 0, sizeof(son)); memset(dep, 0, sizeof(dep)); memset(top, 0, sizeof(top)); memset(ind, 0, sizeof(ind)); memset(rind, 0, sizeof(rind)); indexed = 0; dfs1(root, 1); dfs2(root, root); } struct node { int ls, rs, sum; } tr[Maxs]; int tot; inline int clone(int x) { tr[++tot] = tr[x]; return tot; } inline void pushup(int p) { tr[p].sum = tr[tr[p].ls].sum + tr[tr[p].rs].sum; } void build(int &p, int l, int r) { assert(!p); p = ++tot; if (l == r) { tr[p].sum = 0; return; } int mid = (l + r) >> 1; build(tr[p].ls, l, mid); build(tr[p].rs, mid + 1, r); pushup(p); } void modify(int &p, int l, int r, int pos) { p = clone(p); if (l == r) { tr[p].sum = 1; return; } int mid = (l + r) >> 1; if (pos <= mid) modify(tr[p].ls, l, mid, pos); else modify(tr[p].rs, mid + 1, r, pos); pushup(p); } int query(int pl, int pr, int l, int r, int L, int R) { if (!pr || L > r || l > R) return 0; if (L <= l && r <= R) return tr[pr].sum - tr[pl].sum; int left = query(tr[pl].ls, tr[pr].ls, l, (l + r) >> 1, L, R); int right = query(tr[pl].rs, tr[pr].rs, ((l + r) >> 1) + 1, r, L, R); return left + right; } inline int qry(int pl, int pr, int l, int r) { if (l > r) return 0; return (r - l + 1) - query(pl, pr, 1, n, l, r); } int llf(int pl, int pr, int l, int r, int k) { assert(k <= r - l + 1); if (l == r) return l; int prl = tr[tr[pr].ls].sum; int pll = tr[tr[pl].ls].sum; int val = prl - pll; int mid = (l + r) >> 1; val = (mid - l + 1) - val; if (val >= k) return llf(tr[pl].ls, tr[pr].ls, l, mid, k); else return llf(tr[pl].rs, tr[pr].rs, mid + 1, r, k - val); } inline int lfind(int pl, int pr, int L, int R, int k) { return llf(pl, pr, 1, n, k + qry(pl, pr, 1, L - 1)); } int rt[Maxn]; int qtree(int u, int v, int tl, int tr, int k) { int sum = 0; static int down[100]; int tp = 0; down[tp++] = v; while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) { down[tp++] = top[v]; v = par[top[v]]; } else { int cnt = qry(rt[tl], rt[tr], ind[top[u]], ind[u]); sum += cnt; if (sum >= k) return lfind(rt[tl], rt[tr], ind[top[u]], ind[u], sum - k + 1); u = par[top[u]]; } } if (dep[u] > dep[v]) { int cnt = qry(rt[tl], rt[tr], ind[v], ind[u]); sum += cnt; if (tp != 1) { if (sum >= k) return lfind(rt[tl], rt[tr], ind[v], ind[u], sum - k + 1); } else { if (sum > k || (sum == k && latest[v] > tl)) return lfind(rt[tl], rt[tr], ind[v], ind[u], sum - k + 1); } } else down[tp++] = u; for (int i = tp - 1; i > 1; --i) { int cnt = qry(rt[tl], rt[tr], ind[down[i]], ind[par[down[i - 1]]]); sum += cnt; if (sum >= k) return lfind(rt[tl], rt[tr], ind[down[i]], ind[par[down[i - 1]]], k + cnt - sum); } if (tp > 1) { int cnt = qry(rt[tl], rt[tr], ind[down[1]], ind[down[0]]); sum += cnt; if (sum > k || (sum == k && latest[down[0]] > tl)) return lfind(rt[tl], rt[tr], ind[down[1]], ind[down[0]], k + cnt - sum); } return -1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", par + i); if (par[i] == 0) root = i; else g[par[i]].push_back(i); } start(); tot = 0; build(rt[0], 1, n); scanf("%d", &m); for (int i = 1; i <= m; ++i) { rt[i] = clone(rt[i - 1]); int op, a, b, c, d; scanf("%d%d", &op, &a); if (op == 1) { modify(rt[i], 1, n, ind[a]); latest[a] = i; } else { scanf("%d%d%d", &b, &c, &d); int ans = qtree(a, b, d, i, c + 1 - (latest[a] > d)); if (ans == -1) puts("-1"); else printf("%d\n", rind[ans] == b ? -1 : rind[ans]); } } return 0; }
10
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 1, 0, -1, -1, -1, 1, 1}; int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; int n; bool in_range(int x) { return x >= 0 && x < n; } int main() { ios::sync_with_stdio(false); string a, b; cin >> a >> b; sort(a.begin(), a.end()); sort(b.begin(), b.end()); n = a.size(); int ia = 0, ja = n / 2 + n % 2; ja--; int ib = ja + 1, jb = n - 1; int i = 0, j = n - 1; string res(n, '.'); for (int k = 0; k < n; k++) { if (k % 2 == 0) { if (k == n - 1 || a[ia] < b[jb]) { res[i++] = a[ia++]; } else res[j--] = a[ja--]; } else { if (k == n - 1 || b[jb] > a[ia]) res[i++] = b[jb--]; else res[j--] = b[ib++]; } } cout << res; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = (int)(INT_MAX - 100); const int N = (int)(0); const long long mod = (int)(1e+9 + 7); int main() { int n; cin >> n; int p = 0; vector<int> v(n - 1), v2(n - 1); int num; for (int i = 0; i < n; i++) { scanf("%d", &num); if (num == 0) continue; v[p++] = num; } p = 0; for (int i = 0; i < n; i++) { scanf("%d", &num); if (num == 0) continue; v2[p++] = num; } p = 0; for (int i = 0; i < n - 1; i++) { if (v[i] == v2[0]) break; p++; } for (int i = 0; i < n - 1; i++) { if (v[(p + i) % (n - 1)] != v2[i]) return puts("NO"), 0; } puts("YES"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; struct Edge { int u, v, w; } E[N]; int n, m; int fa[N], vis[N]; template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); } template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } inline bool cmp(Edge a, Edge b) { return a.w > b.w; } inline int getroot(int x) { if (fa[x] == x) return x; else return fa[x] = getroot(fa[x]); } int main() { read(n); read(m); for (int i = 1; i <= m; ++i) { int u, v, w; read(u); read(v); read(w); E[i] = (Edge){u, v, w}; } sort(E + 1, E + m + 1, cmp); for (int i = 1; i <= n; ++i) fa[i] = i; long long ans = 0; for (int i = 1; i <= m; ++i) { int bu = getroot(E[i].u), bv = getroot(E[i].v); if (vis[bu] && vis[bv]) continue; if (bu == bv && !vis[bu]) { vis[bu] = 1; ans += (long long)E[i].w; continue; } else if (bu == bv) continue; ans += (long long)E[i].w; if (vis[bu]) fa[bv] = bu; else fa[bu] = bv; } printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long i, ii, n, m, x, ans, sum = 0; cin >> n; long long a[n + 5]; for (i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } sort(a + 1, a + n + 1); cin >> m; long long b[m + 5]; for (i = 1; i <= m; i++) { cin >> b[i]; } for (ii = 1; ii <= m; ii++) { ans = sum - a[n - (b[ii] - 1)]; cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, p; cin >> n >> p; long long ans = -1; for (long long k = 1; k <= 31; k++) { long long x = n - (k * p); long long find_bits = __builtin_popcount(x); if ((find_bits <= k) && (x >= k)) { ans = k; break; } } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const double PI = atan2(0, -1); const int MAXL = 1e5 + 5; const int P = 239, MOD = 1e9 + 7; int n, k; char buf[MAXL]; map<pair<int, int>, vector<int>> have; int main() { scanf("%d%d", &k, &n); for (int i = 0; i < k; ++i) { int cost; scanf("%s%d", buf, &cost); int h = 0, rh = 0; for (int j = 0; j < n; ++j) { h = ((long long)h * P + buf[j]) % MOD; rh = ((long long)rh * P + buf[n - 1 - j]) % MOD; } have[{h, rh}].push_back(cost); } for (auto& p : have) { sort(begin(p.second), end(p.second)); } long long ans = 0; int best = 0; for (auto& p : have) { if (p.first.first == p.first.second) { int last = 0; while (((int)(p.second).size()) >= 2 && p.second[((int)(p.second).size()) - 2] + p.second.back() > 0) { ans += p.second.back(); p.second.pop_back(); ans += p.second.back(); last = min(last, p.second.back()); p.second.pop_back(); } if (((int)(p.second).size())) { best = max(best, p.second.back()); } best = max(best, -last); } else { pair<int, int> other = {p.first.second, p.first.first}; auto& v = have[other]; while (((int)(p.second).size()) && ((int)(v).size()) && p.second.back() + v.back() > 0) { ans += p.second.back(); p.second.pop_back(); ans += v.back(); v.pop_back(); } } } ans += best; cout << ans << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; static void solve() { int n; cin >> n; int arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i]; int j = 2, c = 0, i = 1, k = n; while (j < n) { for (i = 1; i < j; i++) { if (arr[j] > arr[i]) { c++; break; } } for (k = n; k > j; k--) { if (arr[j] > arr[k]) { c++; break; } } if (c == 2) { cout << "YES\n" << i << " " << j << " " << k << "\n"; return; } c = 0; j++; } cout << "NO\n"; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t; cin >> t; while (t-- > 0) { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; long long dp[2][200500], arr[200500], n; long long solve(bool flip, int idx) { if (idx < 0 || idx >= n) return 0; long long& ret = dp[flip][idx]; if (~ret) return ret; ret = -(1ll << 60); return ret = arr[idx] + (flip ? solve(0, idx - arr[idx]) : solve(1, idx + arr[idx])); } int main() { memset(dp, -1, sizeof(dp)); scanf("%I64d", &n); for (int i = 1; i < n; i++) scanf("%I64d", &arr[i]); long long ans; for (int i = 1; i < n; i++) { arr[0] = i; ans = i + solve(1, i); if (ans < 0) puts("-1"); else cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int tree[400005][6], arr[100005], lazy[400005]; int powers[100005][6]; void build(int i, int L, int R) { if (L == R) { tree[i][0] = arr[L]; for (int j = 1; j <= 5; j++) tree[i][j] = (tree[i][j - 1] * 1LL * (L + 1)) % 1000000007; lazy[i] = -1; return; } build(i * 2 + 1, L, (L + R) / 2); build(i * 2 + 2, (L + R) / 2 + 1, R); for (int j = 0; j <= 5; j++) tree[i][j] = (tree[i * 2 + 1][j] + tree[i * 2 + 2][j]) % 1000000007; lazy[i] = -1; } void update(int i, int L, int R, int x, int y, int val) { if (lazy[i] != -1) { for (int j = 0; j <= 5; j++) { int temp = powers[R][j] - powers[L - 1][j]; temp %= 1000000007; temp += 1000000007; temp %= 1000000007; tree[i][j] = (lazy[i] * 1LL * temp) % 1000000007; } if (L != R) { lazy[i * 2 + 1] = lazy[i]; lazy[i * 2 + 2] = lazy[i]; } lazy[i] = -1; } if (L >= x && R <= y) { lazy[i] = val; for (int j = 0; j <= 5; j++) { int temp = powers[R][j] - powers[L - 1][j]; temp %= 1000000007; temp += 1000000007; temp %= 1000000007; tree[i][j] = (lazy[i] * 1LL * temp) % 1000000007; } if (L != R) { lazy[i * 2 + 1] = lazy[i]; lazy[i * 2 + 2] = lazy[i]; } lazy[i] = -1; return; } if (L > y || R < x) return; update(i * 2 + 1, L, (L + R) / 2, x, y, val); update(i * 2 + 2, (L + R) / 2 + 1, R, x, y, val); for (int j = 0; j <= 5; j++) tree[i][j] = (tree[i * 2 + 1][j] + tree[i * 2 + 2][j]) % 1000000007; } int query(int i, int L, int R, int x, int y, int k) { if (lazy[i] != -1) { for (int j = 0; j <= 5; j++) { int temp = powers[R][j] - powers[L - 1][j]; temp %= 1000000007; temp += 1000000007; temp %= 1000000007; tree[i][j] = (lazy[i] * 1LL * temp) % 1000000007; } if (L != R) { lazy[i * 2 + 1] = lazy[i]; lazy[i * 2 + 2] = lazy[i]; } lazy[i] = -1; } if (L >= x && R <= y) return tree[i][k]; if (L > y || R < x) return 0; return (query(i * 2 + 1, L, (L + R) / 2, x, y, k) + query(i * 2 + 2, (L + R) / 2 + 1, R, x, y, k)) % 1000000007; } int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 0; i <= 6; i++) powers[0][i] = 1; for (int i = 2; i <= n + 1; i++) { int tmp = 1; powers[i - 1][0] = (powers[i - 2][0] + tmp) % 1000000007; tmp = (tmp * 1LL * i) % 1000000007; powers[i - 1][1] = (powers[i - 2][1] + tmp) % 1000000007; tmp = (tmp * 1LL * i) % 1000000007; powers[i - 1][2] = (powers[i - 2][2] + tmp) % 1000000007; tmp = (tmp * 1LL * i) % 1000000007; powers[i - 1][3] = (powers[i - 2][3] + tmp) % 1000000007; tmp = (tmp * 1LL * i) % 1000000007; powers[i - 1][4] = (powers[i - 2][4] + tmp) % 1000000007; tmp = (tmp * 1LL * i) % 1000000007; powers[i - 1][5] = (powers[i - 2][5] + tmp) % 1000000007; } build(0, 1, n); string in; int x, y, z; while (m--) { cin >> in >> x >> y >> z; if (in == "=") update(0, 1, n, x, y, z); else { long long res, pw = x, A, B, C, D, E, F; if (z == 0) { res = query(0, 1, n, x, y, 0); } else if (z == 1) { A = query(0, 1, n, x, y, 1); B = (query(0, 1, n, x, y, 0) * 1LL * pw) % 1000000007; res = (A - B) % 1000000007; res += 1000000007; res %= 1000000007; } else if (z == 2) { A = query(0, 1, n, x, y, 2); B = (query(0, 1, n, x, y, 1) * 2LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; C = (query(0, 1, n, x, y, 0) * 1LL * pw) % 1000000007; res = (A - B + C) % 1000000007; res += 1000000007; res %= 1000000007; } else if (z == 3) { A = query(0, 1, n, x, y, 3); B = (query(0, 1, n, x, y, 2) * 3LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; C = (query(0, 1, n, x, y, 1) * 3LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; D = (query(0, 1, n, x, y, 0) * 1LL * pw) % 1000000007; res = (A - B + C) % 1000000007; res += 1000000007; res %= 1000000007; res = (res - D) % 1000000007; res += 1000000007; res %= 1000000007; } else if (z == 4) { A = query(0, 1, n, x, y, 4); B = (query(0, 1, n, x, y, 3) * 4LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; C = (query(0, 1, n, x, y, 2) * 6LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; D = (query(0, 1, n, x, y, 1) * 4LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; E = (query(0, 1, n, x, y, 0) * 1LL * pw) % 1000000007; res = (A - B + C) % 1000000007; res += 1000000007; res %= 1000000007; res = (res - D + E) % 1000000007; res += 1000000007; res %= 1000000007; } else { A = query(0, 1, n, x, y, 5); B = (query(0, 1, n, x, y, 4) * 5LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; C = (query(0, 1, n, x, y, 3) * 1LL * pw) % 1000000007; C = (C * 10LL) % 1000000007; pw = (x * 1LL * pw) % 1000000007; D = (query(0, 1, n, x, y, 2) * 1LL * pw) % 1000000007; D = (D * 10LL) % 1000000007; pw = (x * 1LL * pw) % 1000000007; E = (query(0, 1, n, x, y, 1) * 5LL * pw) % 1000000007; pw = (x * 1LL * pw) % 1000000007; F = (query(0, 1, n, x, y, 0) * 1LL * pw) % 1000000007; res = (A - B + C) % 1000000007; res += 1000000007; res %= 1000000007; res = (res - D + E) % 1000000007; res += 1000000007; res %= 1000000007; res = (res - F) % 1000000007; res += 1000000007; res %= 1000000007; } cout << res << "\n"; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); string s; cin >> s; int n = s.size(); long long ans = 0; long long p2[200]; p2[0] = 1; for (int i = 1; i < 200; ++i) p2[i] = p2[i - 1] * 2 % 1000000007; for (int i = 0; i < n; ++i) if (s[i] == '1') ans = (ans + p2[2 * (n - 1 - i)] * p2[i]) % 1000000007; cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> pr; int pairs(int x, int y) { for (pair<int, int> p : pr) { if (p.first != x && p.second != x && p.first != y && p.second != y) return 0; } return 1; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; pr.push_back(make_pair(a, b)); } for (int i : {pr[0].first, pr[0].second}) { if (pairs(i, 0) == 1) { cout << "YES"; exit(0); } for (int j = 1; j < m; j++) { if (pr[j].first != i && pr[j].second != i) { if (pairs(i, pr[j].first) == 1 || pairs(i, pr[j].second) == 1) { cout << "YES"; exit(0); } break; } } } cout << "NO"; return 0; }
3
#include <bits/stdc++.h> int n; int cnt = -1; int first[600007], next_[600007 << 1], to[600007 << 1]; bool delet[600007 << 1]; int cur[600007]; int color[600007]; void add(int a, int b) { ++cnt; next_[cnt] = first[a]; first[a] = cnt; to[cnt] = b; } void add_e(int a, int b) { add(a, b); add(b, a); } void dfs(int x) { for (int &i = first[x]; i != -1; i = next_[i]) { if (delet[i]) continue; int v = to[i]; cur[x] = i; if (!color[(i >> 1) + 1]) { delet[i] = true; delet[i ^ 1] = true; color[(i >> 1) + 1] = (x < 200007) + 1; dfs(v); } } } int d[600007]; int main() { memset(first, -1, sizeof(first)); memset(next_, -1, sizeof(next_)); scanf("%d", &n); for (int i = 1; i <= n; ++i) { int a, b; scanf("%d%d", &a, &b); ++d[a]; ++d[b + 200007]; add_e(a, b + 200007); } for (int i = 1; i <= 200007; ++i) { if (d[i] & 1) add_e(i, 200007 + 200007 + 1), ++d[200007 + 200007 + 1]; } for (int i = 200007 + 1; i <= 200007 + 200007; ++i) { if (d[i] & 1) add_e(0, i), ++d[0]; } if (d[0] & 1) add_e(0, 200007 + 200007 + 1); for (int i = 1; i <= 200007 + 200007; ++i) dfs(i); for (int i = 1; i <= n; ++i) { if (color[i] == 1) putchar('r'); else putchar('b'); } return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > k4 = {{1, 2}, {3, 4}, {1, 3}, {2, 4}, {1, 4}, {2, 3}}; vector<int> bip = {2, 1, 4, 3, 4, 3, 2, 1, 3, 4, 1, 2, 1, 2, 3, 4}; vector<pair<int, int> > k5 = {{4, 0}, {3, 4}, {3, 0}, {2, 0}, {1, 2}, {1, 0}, {1, 3}, {2, 4}, {1, 4}, {2, 3}}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; if (n == 1) { cout << "YES\n"; return 0; } if (n % 4 == 2 || n % 4 == 3) { cout << "NO\n"; return 0; } if (n % 4 == 0) { cout << "YES\n"; for (int m = 0; m < n; m += 4) { for (auto p : k4) cout << m + p.first << " " << m + p.second << '\n'; } for (int p = 0; p < n; p += 4) { for (int q = p + 4; q < n; q += 4) { for (int i = 0; i < 16; i++) { cout << p + (i % 4) + 1 << " " << q + bip[i] << '\n'; } } } } if (n % 4 == 1) { cout << "YES\n"; for (int m = 0; m < n - 1; m += 4) { for (auto p : k5) { int a = p.first; int b = p.second; a += m; if (b == 0) b = n; else b += m; cout << a << " " << b << '\n'; } } for (int p = 0; p < n - 1; p += 4) { for (int q = p + 4; q < n - 1; q += 4) { for (int i = 0; i < 16; i++) { cout << p + (i % 4) + 1 << " " << q + bip[i] << '\n'; } } } } }
11
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long max(long long a, long long b) { if (a > b) return a; else return b; } long long min(long long a, long long b) { if (a < b) return a; else return b; } void vecdisp(vector<long long> v) { for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; } void arrdisp(long long a[], long long n) { for (long long i = 0; i < n; i++) cout << a[i] << " "; } void sarrdisp(string s[], long long n) { for (long long i = 0; i < n; i++) cout << s[i] << "\n"; } void twodarrdisp(long long *a, long long r, long long c) { for (long long i = 0; i < r; i++) { for (long long j = 0; j < c; j++) cout << (*((a + i * c) + j)) << " "; cout << "\n"; } } void setdisp(set<long long> s) { for (auto it = s.begin(); it != s.end(); ++it) cout << " " << *it; } long long nod(long long a) { long long c = 0; set<long long> s; while (a > 0) { ++c; s.insert(a % 10); a /= 10; } if (s.size() == c) return 1; else return 0; } long long sod(long long a) { long long c = 0; while (a > 0) { c += a % 10; a /= 10; } return c; } bool isComposite(long long n) { if (n <= 1) return false; if (n <= 3) return false; if (n % 2 == 0 || n % 3 == 0) return true; for (long long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return true; } return false; } long long countDivisors(long long n) { long long cnt = 0; for (long long i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) cnt++; else cnt += 2; } } return cnt; } long long isPalindrome(string s) { long long l = s.size(); long long flag = 0; for (long long i = 0; i < l; i++) { if (s[i] != s[l - i - 1]) { flag = 1; break; } } if (flag) return 0; else return 1; } void solve() { long long n, x; cin >> n; long long a[N], b[N]; for (long long i = 1; i <= (n / 2); i++) cin >> b[i]; a[1] = 0; a[n] = b[1]; for (long long i = 2; i <= n / 2; i++) { long long prev = a[i - 1]; prev = max(prev, b[i] - a[n - i + 2]); a[i] = prev; a[n - i + 1] = b[i] - prev; } for (long long i = 1; i <= n; i++) cout << a[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t = 1; if (!t) cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int i, x1, x2, ct1 = 0, ct2 = 0, arr[n]; int *ch1, *ch2; ch1 = (int *)malloc(sizeof(int)); ch2 = (int *)malloc(sizeof(int)); scanf("%1d", &arr[0]); x1 = arr[0]; for (i = 1; i < n; i++) { scanf("%1d", &arr[i]); if (arr[i] != arr[i - 1]) { ct1++; ch1 = (int *)realloc(ch1, ct1 * sizeof(int)); ch1[ct1 - 1] = i; x1 = arr[i]; } } int arr2[n]; scanf("%1d", &arr2[0]); x2 = arr2[0]; for (i = 1; i < n; i++) { scanf("%1d", &arr2[i]); if (arr2[i] != arr2[i - 1]) { ct2++; ch2 = (int *)realloc(ch2, ct2 * sizeof(int)); ch2[ct2 - 1] = i; x2 = arr2[i]; } } if (x1 == arr2[n - 1]) printf("%d ", ct1 + ct2); else printf("%d ", ct1 + ct2 + 1); for (i = 0; i < ct1; i++) printf("%d ", ch1[i]); if (x1 != arr2[n - 1]) printf("%d ", n); for (i = ct2 - 1; i >= 0; i--) printf("%d ", ch2[i]); printf("\n"); } }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 1e6 + 10, INF = 0x3f3f3f3f; int N, R, ans; char buff[maxn], res[maxn]; void solve(int, int); int main() { while (~scanf("%d%d", &N, &R)) { ans = INF; for (int i = 1; i <= R; i++) { solve(i, R); solve(R, i); } if (ans == INF) puts("IMPOSSIBLE"); else printf("%d\n%s\n", ans, res); } return 0; } void solve(int top, int bot) { for (int i = N - 1; i > 0; i--) { if (top == bot) return; if (top > bot) { top -= bot; buff[i] = 'T'; } else { bot -= top; buff[i] = 'B'; } } if (top != 1 || bot != 1) return; buff[0] = 'T'; int cnt = 0; for (int i = 1; i < N; i++) if (buff[i - 1] == buff[i]) cnt++; if (cnt < ans) { ans = cnt; memcpy(res, buff, sizeof(buff[0]) * N); res[N] = 0; } }
6
#include <bits/stdc++.h> using namespace std; int n, x[123456], q, m; int main() { cin >> n; for (int i = 0; i < n; i++) { scanf(" %d", x + i); } sort(x, x + n); cin >> q; for (int i = 0; i < q; i++) { scanf(" %d", &m); printf("%d\n", upper_bound(x, x + n, m) - x); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; struct edge { int u; edge* next; edge(int u = 0, edge* next = NULL) : u(u), next(next) {} void insert(int u) { if (this->next == NULL) this->next = new edge(u, NULL); else this->next = new edge(u, this->next); } } e[1000006]; int siz[1000006], v[1000006], tot, num[1000006]; int ans[1000006], t; void dfs(int u) { siz[u] = v[u], num[u] = 1; for (edge* n = e[u].next; n != NULL; n = n->next) { int v = n->u; dfs(v); if (siz[v] != tot / 3) siz[u] += siz[v]; num[u] += num[v]; } if (siz[u] == tot / 3 && t != 2) ans[t++] = u; } int main() { cin >> n; int root; for (int i(1); i <= n; ++i) { static int u, w; scanf("%d%d", &u, &w); tot += w; v[i] = w; if (u == 0) root = i; e[u].insert(i); } if (tot % 3 || tot == 0) { puts("-1"); return 0; } dfs(root); if (t >= 2 && num[ans[0]] + num[ans[1]] != n) printf("%d %d", ans[0], ans[1]); else puts("-1"); }
6
#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; void solve() { int xa, xb, xf, ya, yb, yf; cin >> xa >> ya; cin >> xb >> yb; cin >> xf >> yf; int arr_x[3] = { xa, xb, xf }; sort(arr_x, arr_x + 3); int arr_y[3] = { ya, yb, yf }; sort(arr_y, arr_y + 3); if ((arr_x[0] == arr_x[2] && arr_y[1] == yf) || (arr_y[0] == arr_y[2] && arr_x[1] == xf)) { cout << abs(xa - xb) + abs(ya - yb) + 2 << '\n'; } else if (xa == xb) { cout << abs(ya - yb) << '\n'; } else if (ya == yb) { cout << abs(xa - xb) << '\n'; } else { cout << abs(xa - xb) + abs(ya - yb) << '\n'; } } int main() { setlocale(0, ""); int t; cin >> t; while (t--) { solve(); } }
0
#include <bits/stdc++.h> #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; const long double EPS = 0.001; const long double EPS2 = 1e-6; const long double PI = acos(-1); int mod = (int)998244353; const int MOD7 = 1000000007; const int MOD9 = 1000000009; const int a228 = 18; const int inf = 1e9; const long long kekmod = 1791791791; const long long bestmod = 1148822869; signed main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL)); int n; cin >> n; vector<int> b(n); for (long long i = 0; i < b.size(); ++i) cin >> b[i]; ; vector<int> a(n); a[0] = b[0]; int mx = a[0]; for (int i = 1; i < n; ++i) { a[i] = b[i] + mx; mx = max(mx, a[i]); } for (int i : a) cout << i << ' '; }
0