solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; if (a.length() != b.length()) { cout << "No"; return 0; } for (int i = 0; i < a.length(); i++) { if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') { if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') continue; cout << "No"; return 0; } if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') { cout << "No"; return 0; } } cout << "Yes"; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, x[200000], s = 0, a = 0; cin >> n; for (i = 0; i < n; i++) { cin >> x[i]; s = s + x[i]; } for (i = 0; i < n; i++) { a = a + x[i]; if (a >= s / 2 && s % 2 == 0) break; else if (s % 2 != 0 && a >= s / 2 + 1) break; } cout << i + 1; }
5
#include <bits/stdc++.h> using namespace std; const int sz = 4e5; long long a[sz], n; long long curinv[32], flipinv[32]; void foo(vector<int> v, int b) { if (b < 0 || v.empty()) return; vector<int> zvec, ovec; int z = 0, o = 0; for (int k : v) { if ((k >> b) & 1) { o++; flipinv[b] += z; ovec.push_back(k); } else { z++; curinv[b] += o; zvec.push_back(k); } } foo(zvec, b - 1); foo(ovec, b - 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; vector<int> v; for (int i = 0; i < n; i++) { cin >> a[i]; v.push_back(a[i]); } foo(v, 31); long long mynum = 0, totinv = 0; for (int b = 0; b < 32; b++) { if (curinv[b] <= flipinv[b]) totinv += curinv[b]; else { totinv += flipinv[b]; mynum |= (1 << b); } } cout << totinv << " " << mynum << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; const int maxn = 100006; int n; vector<int> e[maxn]; int c[maxn]; bool check(int x, int p, int o) { if (p != 0 && c[x] != c[p]) return false; for (auto i : e[x]) if (i != p && i != o) if (!check(i, x, o)) return false; return true; } bool check_point(int o) { for (auto i : e[o]) if (!check(i, 0, o)) return false; return true; } void judge() { for (int i = 1; i <= n; ++i) for (auto j : e[i]) if (c[i] != c[j]) { if (check_point(i)) { cout << "YES\n" << i << endl; return; } if (check_point(j)) { cout << "YES\n" << j << endl; return; } cout << "NO\n"; return; } cout << "YES\n1\n"; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) e[i].clear(); for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } for (int i = 1; i <= n; ++i) cin >> c[i]; judge(); }
8
#include <bits/stdc++.h> using namespace std; const int Maxm = 500005; int m, k, n, s; int a[Maxm]; int lft[Maxm], tot; bool mark[Maxm]; vector<int> res; void Add(int x) { if (lft[x] == 1) tot--; lft[x]--; } void Rem(int x) { if (lft[x] == 0) tot++; lft[x]++; } int main() { scanf("%d %d %d %d", &m, &k, &n, &s); for (int i = 1; i <= m; i++) scanf("%d", &a[i]); for (int i = 1; i <= s; i++) { int b; scanf("%d", &b); lft[b]++; } for (int i = 0; i < Maxm; i++) if (lft[i] > 0) tot++; int r = 0; for (int i = 1; i + k - 1 <= m; i++) { while (r <= m && (tot > 0 || r - i < k)) { Add(a[r]); r++; } int rem = (i - 1) % k + r - i - k; if (tot == 0 && rem <= m - k * n) { int rem = r - i - k; for (int j = i; j < r; j++) if (lft[a[j]] < 0 && rem > 0) { lft[a[j]]++; rem--; } else mark[j] = true; int nd = n - 1; for (int j = 1; j + k <= i && nd > 0; j += k) { nd--; for (int z = j; z < j + k; z++) mark[z] = true; } for (int j = m; j - k >= r - 1 && nd > 0; j -= k) { nd--; for (int z = j; z > j - k; z--) mark[z] = true; } for (int j = 1; j <= m; j++) if (!mark[j]) res.push_back(j); printf("%d\n", int(res.size())); for (int j = 0; j < res.size(); j++) printf("%d%c", res[j], j + 1 < res.size() ? ' ' : '\n'); return 0; } Rem(a[i]); } printf("-1\n"); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int test, cas = 0; int n; cin >> n; int ar[n + 5]; for (long long i = 1; i <= n; i++) { cin >> ar[i]; }; bool ck = 0; for (long long i = 1; i <= n; i++) { if (ar[i] % 2 == 0) { cout << ar[i] / 2 << endl; } else if (ck == 0) { if (ar[i] > 0) cout << ar[i] / 2 + 1 << endl; else { cout << ar[i] / 2 << endl; } ck = 1; } else { if (ar[i] > 0) cout << ar[i] / 2 << endl; else cout << ar[i] / 2 - 1 << endl; ck = 0; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long n, m; cin >> n >> m; long long total = 0; for (long long i = 1; i <= n; i++) { long long k = (m + i % 5) / 5; total += k; } cout << total; }
3
#include <bits/stdc++.h> int main() { int i, j = 1, k = 1, n, m, a[1000] = {0}, b[1000] = {0}, c, d, e, f; scanf("%d %d", &n, &m); for (i = 1; i <= m; i++) { if (i % 2 != 0) { a[k] = i; k++; } else { b[j] = i; j++; } } c = n + 1; d = n + 1; k = 1; j = 1; for (i = 1; i <= 2 * n; i++) { if (i % 2 != 0) { if (a[c] != 0) { printf("%d %d ", a[c], a[k]); k++; c++; } else if (a[k] != 0) { printf("%d ", a[k]); k++; } } else { if (b[d] != 0) { printf("%d %d ", b[d], b[j]); j++; d++; } else if (b[j] != 0) { printf("%d ", b[j]); j++; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int read() { register int s = 0, t = 0; register char ch = getchar(); while (ch > '9' || ch < '0') t |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return t ? -s : s; } int c[11], d[11], g[200001 << 2][7], f[200001 << 2][7], now, num, add[200001 << 2]; int a[200001], cnt, cntx, w[11], h[11], n, m, p, siz[200001 << 2], md[200001 << 2]; void pushdown(int k, int l, int r) { int mid = l + r >> 1; if (md[k]) { add[k << 1] = 0; add[k << 1 | 1] = 0; md[k << 1] = md[k]; md[k << 1 | 1] = md[k]; siz[k << 1] = 1; g[k << 1][1] = md[k]; f[k << 1][1] = mid - l + 1; siz[k << 1 | 1] = 1; g[k << 1 | 1][1] = md[k]; f[k << 1 | 1][1] = r - mid; } else { if (md[k << 1]) md[k << 1] += add[k]; if (md[k << 1 | 1]) md[k << 1 | 1] += add[k]; for (int i = 1; i <= siz[k << 1]; i++) g[k << 1][i] += add[k]; for (int i = 1; i <= siz[k << 1 | 1]; i++) g[k << 1 | 1][i] += add[k]; if (!md[k << 1]) add[k << 1] += add[k]; if (!md[k << 1 | 1]) add[k << 1 | 1] += add[k]; } add[k] = 0; md[k] = 0; } void merge(int x, int y) { for (int i = 1; i <= siz[x]; i++) c[i] = 0, d[i] = 0; for (int i = 1; i <= siz[x]; i++) c[i] = g[x][i], d[i] = f[x][i]; bool flag; cnt = siz[x]; cntx = 0; for (int i = 1; i <= siz[y]; i++) { flag = false; for (int j = 1; j <= cnt; j++) { if (c[j] == g[y][i]) d[j] += f[y][i], flag = true; } if (flag == true) continue; now = 1; for (int j = 1; j <= cnt; j++) if (d[now] >= d[j]) now = j; if (cnt < num) { cnt++; c[cnt] = g[y][i]; d[cnt] = f[y][i]; continue; } if (d[now] <= f[y][i]) { int t = d[now]; c[now] = g[y][i]; d[now] = f[y][i]; for (int j = 1; j <= cnt; j++) d[j] -= t; } else for (int j = 1; j <= cnt; j++) d[j] -= f[y][i]; } for (int i = 1; i <= cnt; i++) w[++cntx] = c[i], h[cntx] = d[i]; } void build(int k, int l, int r) { int mid = l + r >> 1; if (l == r) { g[k][1] = a[l]; f[k][1] = 1; siz[k] = 1; return; } build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); merge(k << 1, k << 1 | 1); siz[k] = cntx; for (int i = 1; i <= cntx; i++) g[k][i] = w[i], f[k][i] = h[i]; } void query(int k, int l, int r, int L, int R) { if (L <= l && r <= R) { merge(0, k); siz[0] = cntx; for (int i = 1; i <= cntx; i++) g[0][i] = w[i], f[0][i] = h[i]; return; } int mid = l + r >> 1; pushdown(k, l, r); if (L <= mid) query(k << 1, l, mid, L, R); if (R > mid) query(k << 1 | 1, mid + 1, r, L, R); merge(k << 1, k << 1 | 1); siz[k] = cntx; for (int i = 1; i <= cntx; i++) g[k][i] = w[i], f[k][i] = h[i]; } void modify(int k, int l, int r, int L, int R, int x) { if (L <= l && r <= R) { add[k] = 0; md[k] = x; siz[k] = 1; g[k][1] = x; f[k][1] = r - l + 1; return; } int mid = l + r >> 1; pushdown(k, l, r); if (L <= mid) modify(k << 1, l, mid, L, R, x); if (R > mid) modify(k << 1 | 1, mid + 1, r, L, R, x); merge(k << 1, k << 1 | 1); siz[k] = cntx; for (int i = 1; i <= cntx; i++) g[k][i] = w[i], f[k][i] = h[i]; } void change(int k, int l, int r, int L, int R) { if (L <= l && r <= R) { if (md[k]) md[k] += 1; else add[k] += 1; for (int i = 1; i <= siz[k]; i++) g[k][i]++; return; } int mid = l + r >> 1; pushdown(k, l, r); if (L <= mid) change(k << 1, l, mid, L, R); if (R > mid) change(k << 1 | 1, mid + 1, r, L, R); merge(k << 1, k << 1 | 1); siz[k] = cntx; for (int i = 1; i <= cntx; i++) g[k][i] = w[i], f[k][i] = h[i]; } int main() { n = read(); m = read(); p = read(); num = 100 / p; for (int i = 1; i <= n; i++) a[i] = read(); build(1, 1, n); for (int i = 1; i <= m; i++) { int opt = read(), l = read(), r = read(); siz[0] = 0; if (opt == 2) { query(1, 1, n, l, r); printf("%d ", siz[0]); for (int j = 1; j <= siz[0]; j++) printf("%d ", g[0][j]); puts(""); } if (opt == 1) { int x = read(); modify(1, 1, n, l, r, x); } } return 0; }
24
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int main() { int n; cin >> n; string s; for (int i = 0; i < n; ++i) { if (i % 4 < 2) { s += 'a'; } else { s += 'b'; } } cout << s << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int l[n], r[n]; int L = 0, R = 0; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; L += l[i], R += r[i]; } int ans = abs(L - R); int col = 0; for (int i = 0; i < n; i++) { int tL = L, tR = R; tL = tL - l[i] + r[i]; tR = tR - r[i] + l[i]; if (ans < abs(tL - tR)) { ans = max(ans, abs(tL - tR)); col = i + 1; } } cout << col; return 0; }
3
#include <bits/stdc++.h> using namespace std; map<string, int> MP; int go(int t, string s) { if (MP.find(s) != MP.end()) { return MP[s]; } if (s.size() == 2) { return MP[s] = (s[0] - '0') * 2 + (s[1] - '0'); } bool lose = true; bool vis[4] = {0}; for (int i = (0); i < (int)(s.size()); i++) { string s2 = s; s2.erase(s2.begin() + i); int ret = go(1 - t, s2); vis[ret] = 1; } if (t == 0) { int i = 0; while (!vis[i]) ++i; return MP[s] = i; } if (t == 1) { int i = 3; while (!vis[i]) --i; return MP[s] = i; } } int stupid(string s) { MP.clear(); int res = go(0, s); return res; } set<string> glo; void exhaust(string &s, int idx) { if (idx == s.size()) { int ans = stupid(s); string bin = ""; bin += '0' + (ans / 2); bin += '0' + (ans % 2); glo.insert(bin); return; } if (s[idx] != '?') return exhaust(s, idx + 1); s[idx] = '0'; exhaust(s, idx + 1); s[idx] = '1'; exhaust(s, idx + 1); s[idx] = '?'; } set<string> doit(string s) { glo.clear(); exhaust(s, 0); return glo; } int main() { string s; while (cin >> s) { int n = s.size(); int c0 = 0, c1 = 0, gao = 0; int last0 = n, last1 = n, lastgao = n; for (int i = (0); i < (int)(n); i++) { if (s[i] == '0') c0++, last0 = i; else if (s[i] == '1') c1++, last1 = i; else gao++, lastgao = i; } set<string> ans; if (gao == 0) { if (c0 + n % 2 > c1) ans.insert("00"); else if (c1 > c0 + n % 2) ans.insert("11"); else if (last0 < last1) ans.insert("01"); else ans.insert("10"); } else { if (c0 + n % 2 + gao < c1) ans.insert("11"); else if (c1 + gao < c0 + n % 2) ans.insert("00"); else { if (c0 + n % 2 + gao > c1) ans.insert("00"); if (c1 + gao > c0 + n % 2) ans.insert("11"); if (c0 + n % 2 + gao >= c1) { string t = s; int _c0 = c0, _c1 = c1; int _last0 = n, _last1 = n; for (int i = (0); i < (int)(n); i++) { if (s[i] == '?') { if (_c0 < n / 2) s[i] = '0', ++_c0; else s[i] = '1', ++_c1; } if (s[i] == '0') _last0 = i; else if (s[i] == '1') _last1 = i; } if (_c0 + n % 2 == _c1 && _last0 < _last1) ans.insert("01"); s = t; } if (c1 + gao >= c0 + n % 2) { string t = s; int _c0 = c0, _c1 = c1; int _last0 = n, _last1 = n; for (int i = (0); i < (int)(n); i++) { if (s[i] == '?') { if (_c1 < n / 2 + n % 2) s[i] = '1', ++_c1; else s[i] = '0', ++_c0; } if (s[i] == '0') _last0 = i; else if (s[i] == '1') _last1 = i; } if (_c0 + n % 2 == _c1 && _last1 < _last0) ans.insert("10"); s = t; } } } for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); it++) cout << *it << endl; } return 0; }
11
#include <bits/stdc++.h> using namespace std; struct data { int siz; int old; int par; }; vector<data> t; void remake(int v) { t[v] = {1, 0, v}; } void make(int v) { t.push_back({1, 0, v}); } int fin(int v) { if (v == t[v].par) return v; else { t[v].par = fin(t[v].par); return t[v].par; } } void uni(int x, int y, int z) { int xx = fin(x); int yy = fin(y); if (t[xx].par != t[yy].par) { if (t[xx].siz > t[yy].siz) swap(xx, yy); t[xx].par = yy; if (t[xx].siz == t[yy].siz) t[yy].siz++; t[yy].old = z; } } vector<pair<int, pair<int, int> > > vec; int ans = 0; int a, b, c, n, m; vector<int> q; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < 2 * n; i++) make(i); for (int i = 0; i < m; i++) { cin >> a >> b >> c; vec.push_back(make_pair(c, make_pair(a - 1, b - 1))); } sort(vec.begin(), vec.end()); for (int l = 0, r = 0; r < m; l = r) { while (r < m && vec[l].first == vec[r].first) r++; for (int i = l; i < r; i++) { int v1 = vec[i].second.first, v2 = vec[i].second.second, cost = vec[i].first; if (fin(v1) == fin(v2)) continue; if (fin(n + fin(v1)) != fin(n + fin(v2))) { q.push_back(fin(n + fin(v1))); q.push_back(fin(n + fin(v2))); uni(n + fin(v1), n + fin(v2), 1); } else ans++; } while (q.size() != 0) { remake(q[q.size() - 1]); q.pop_back(); } for (int i = l; i < r; i++) { int v1 = vec[i].second.first, v2 = vec[i].second.second, cost = vec[i].first; if (fin(v1) != fin(v2)) { uni(v1, v2, cost); } } } cout << ans; }
13
#include <bits/stdc++.h> using namespace std; int C[1001][1001]; int MOD = (int)1e9 + 7; int COMBO(int n, int r) { if (n < 0 || r < 0) return 0; if (n < r) return 0; if (C[n][r] != -1) return C[n][r]; return C[n][r] = (COMBO(n - 1, r - 1) + COMBO(n - 1, r)) % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; scanf("%d", &n); int m; scanf("%d", &m); int k; scanf("%d", &k); n--; m--; if (2 * k > n || 2 * k > m) { cout << 0 << endl; } else { memset(C, -1, sizeof(C)); C[0][0] = 1; cout << 1ULL * COMBO(n, 2 * k) * COMBO(m, 2 * k) % MOD << endl; } return 0; }
12
#include <bits/stdc++.h> using namespace std; string s, t; int sc[100005]; int bc[100005]; int ca[100005], cb[100005]; ; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s >> t; int cnt = 0; for (int i = 0; i < s.size(); i++) { sc[i + 1] = sc[i]; if (s[i] == 'A') cnt++; else cnt = 0, sc[i + 1]++; ca[i + 1] = cnt; } cnt = 0; for (int i = 0; i < t.size(); i++) { bc[i + 1] = bc[i]; if (t[i] == 'A') cnt++; else cnt = 0, bc[i + 1]++; cb[i + 1] = cnt; } int q; cin >> q; while (q--) { int a, b, c, d; cin >> a >> b >> c >> d; int k = (bc[d] - bc[c - 1]) - (sc[b] - sc[a - 1]); int k1 = min(ca[b], b - a + 1); int k2 = min(cb[d], d - c + 1); if (k < 0) cout << '0'; else { if (k1 < k2) cout << '0'; else { if (k == 0) cout << ((k1 - k2) % 3 == 0); else { if (k % 2 == 0 && k1 - ((sc[b] - sc[a - 1]) == 0) >= k2) cout << '1'; else cout << '0'; } } } } cout << '\n'; return 0; }
17
#include <bits/stdc++.h> using namespace std; const int N = 500009; const int mod = 1000000007; long long int n, m, x, y; vector<long long int> v[N]; vector<bool> vis; bool is; void pre() { vis.assign(N, 0); } void dfs(long long int ver, long long int par) { vis[ver] = 1; for (auto &it : v[ver]) { if (it == par) continue; if (vis[it] == 1) { is = 1; continue; } dfs(it, ver); } } int32_t main() { pre(); cin >> n >> m; for (long long int i = 0; i < m; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } long long int ans = 0; for (long long int i = 1; i <= n; i++) { if (vis[i]) continue; is = 0; dfs(i, -1); ans++; if (is) ans--; } cout << ans << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; struct Pt { double x, y; double Dist(Pt p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); } double Len() const { return sqrt(x * x + y * y); } void Norm() { double l = Len(); if (!l) return; x /= l; y /= l; } void Mul(double k) { x *= k; y *= k; } void operator+=(Pt p) { x += p.x; y += p.y; } }; struct Circle { double r; Pt O; bool Contains(Pt p) { return p.Dist(O) < r; } }; int main() { double R, a, b, c, d; cin >> R >> a >> b >> c >> d; Pt O{a, b}, p{c, d}; Circle circ{R, O}; if (!circ.Contains(p)) { cout << setprecision(10) << circ.O.x << " " << circ.O.y << " " << circ.r << endl; return 0; } Pt q{c - a, d - b}; double dist = q.Len() + R; if (!q.Len()) q = {0, 1}; else { q.Norm(); q.Mul(-1.0); } q.Mul(R); q += O; Pt m{(c + q.x) / 2.0, (d + q.y) / 2.0}; cout << setprecision(10) << m.x << " " << m.y << " " << dist / 2.0 << endl; return 0; }
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const long long md = 1e9 + 7; int main() { iostream::sync_with_stdio(0); cin.tie(0), cout.tie(0); vector<int> cn(5, 0); string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s.substr(i, min((int)s.length() - i, 5)) == "Danil") cn[0]++; if (s.substr(i, min((int)s.length() - i, 4)) == "Olya") cn[1]++; if (s.substr(i, min((int)s.length() - i, 5)) == "Slava") cn[2]++; if (s.substr(i, min((int)s.length() - i, 3)) == "Ann") cn[3]++; if (s.substr(i, min((int)s.length() - i, 6)) == "Nikita") cn[4]++; } bool ch = 1; int C = 0; for (int i = 0; i < 5; i++) { if (cn[i] > 1) ch = 0; if (cn[i]) C++; } if (ch && C == 1) cout << "YES\n"; else cout << "NO\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a[101], b[101], s = 0, c[101], l = 0; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = i + 1; } for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; t = b[i]; b[i] = b[j]; b[j] = t; } } } for (int i = 0; i < n; i++) { if (s < m) { s += a[i]; c[l] = b[i]; l++; } } if (s > m) l--; cout << l << endl; for (int i = 0; i < l - 1; i++) { for (int j = i + 1; j < l; j++) { if (c[i] > c[j]) { int t = c[i]; c[i] = c[j]; c[j] = t; } } } for (int i = 0; i < l; i++) cout << c[i] << " "; }
2
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; int main() { long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; long long d1[n], d2[n], d3[n], d4[n], d5[n], d6[n]; memset(d1, 0, sizeof(d1)); memset(d2, 0, sizeof(d2)); memset(d3, 0, sizeof(d3)); memset(d4, 0, sizeof(d4)); memset(d5, 0, sizeof(d5)); memset(d6, 0, sizeof(d6)); for (int i = 0; i < n; i++) { if (i == 0) { if (arr[i] == 4) d1[0] = 1; else d1[0] = 0; } else { d1[i] += d1[i - 1] + (arr[i] == 4); } } for (int i = 0; i < n; i++) { if (i == 0) { d2[0] = 0; } else { d2[i] += min(d1[i], d2[i - 1] + (arr[i] == 8)); } } for (int i = 0; i < n; i++) { if (i == 0) { d3[0] = 0; } else { d3[i] += min(d2[i], d3[i - 1] + (arr[i] == 15)); } } for (int i = 0; i < n; i++) { if (i == 0) { d4[0] = 0; } else { d4[i] += min(d3[i], d4[i - 1] + (arr[i] == 16)); } } for (int i = 0; i < n; i++) { if (i == 0) { d5[0] = 0; } else { d5[i] += min(d4[i], d5[i - 1] + (arr[i] == 23)); } } for (int i = 0; i < n; i++) { if (i == 0) { d6[0] = 0; } else { d6[i] += min(d5[i], d6[i - 1] + (arr[i] == 42)); } } cout << n - 6 * d6[n - 1] << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { float hh, mm, h, d, c, n; cin >> hh >> mm; cin >> h >> d >> c >> n; float xmin = 0; if (hh < 20) { xmin = 60 - mm + (20 - hh - 1) * 60; } float xh = xmin * d + h; float xnr = ceil((xh / n)); float nr = ceil((h / n)); float xval = c - c * .2; float ans = min(xnr * xval, (nr * c)); printf("%f", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int ans = -1; int mi = 2 * 1e9; void rec(string s, int level) { int l = s.size(); if (l == 0) return; if (s[0] != '0') { int tonum = stoi(s); double square = sqrt(tonum); int square_int = (int)square; if (square_int == square) { if (level < mi) { mi = level; ans = level; } return; } } for (int i = 0; i < l; i++) { string part = s.substr(0, i) + s.substr(i + 1); rec(part, level + 1); } } int main() { int n; cin >> n; string s = to_string(n); rec(s, 0); cout << ans; }
6
#include <bits/stdc++.h> using namespace std; const int N = 31; const int OFFSET = 150; const int MOD = 1e9 + 7; int n, t[N]; bool f[N][310][310][3][3], g[310][310]; int ans; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> t[i]; for (int i = 1; i <= t[1]; ++i) g[OFFSET][OFFSET + i] = 1; f[1][OFFSET][OFFSET + t[1]][1][2] = 1; for (int i = 2; i <= n; ++i) { for (int x = 0; x < 310; ++x) for (int y = 0; y < 310; ++y) for (int sx = 0; sx <= 2; ++sx) for (int sy = 0; sy <= 2; ++sy) { if (!f[i - 1][x][y][sx][sy]) continue; int nx, ny; for (int add = 1; add <= t[i]; ++add) { if (sx != 1 && sy != 1) { nx = x + (sx - 1) * add; ny = y; if (add == t[i]) f[i][nx][ny][sx][1] = 1; g[nx][ny] = 1; ny = y + (sy - 1) * add; nx = x; if (add == t[i]) f[i][nx][ny][1][sy] = 1; g[nx][ny] = 1; } else if (sx == 1) { nx = x + add; ny = y + (sy - 1) * add; if (add == t[i]) f[i][nx][ny][2][sy] = 1; g[nx][ny] = 1; nx = x - add; ny = y + (sy - 1) * add; if (add == t[i]) f[i][nx][ny][0][sy] = 1; g[nx][ny] = 1; } else { ny = y + add; nx = x + (sx - 1) * add; if (add == t[i]) f[i][nx][ny][sx][2] = 1; g[nx][ny] = 1; ny = y - add; nx = x + (sx - 1) * add; if (add == t[i]) f[i][nx][ny][sx][0] = 1; g[nx][ny] = 1; } } } } for (int j = 0; j < 310; ++j) for (int k = 0; k < 310; ++k) ans += g[j][k]; cout << ans << endl; }
11
#include <bits/stdc++.h> using namespace std; vector<long long int> bit(400005, 0), pre(400005, 0); map<long long int, long long int> mp; void upd(int x) { while (x < 400005) { bit[x]++; x += x & -x; } } int get(int x) { int ans = 0; while (x > 0) { ans += bit[x]; x -= x & -x; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t, n, m, q, i, j, k, x, y, z; long long int ans = 0; cin >> n >> k; mp[0] = 1; mp[-k] = 1; for (i = 1; i <= n; i++) { cin >> x; pre[i] = pre[i - 1] + x; mp[pre[i]] = 1; mp[pre[i] - k] = 1; } long long int ind = 0; for (auto it = mp.begin(); it != mp.end(); it++) it->second = ++ind; upd(mp[0]); for (i = 1; i <= n; i++) { ans += i - get(mp[pre[i] - k]); upd(mp[pre[i]]); } cout << ans; }
10
#include <bits/stdc++.h> int main() { int n, i; scanf("%d", &n); if (n <= 2) { printf("1 \n1"); } else if (n == 3) { printf("2 \n1 3"); } else if (n == 4) { printf("4 \n3 1 4 2"); } else { printf("%d\n", n); for (i = 1; i <= n; i = i + 2) { printf("%d\t", i); } for (i = 2; i <= n; i = i + 2) { printf("%d\t", i); } } }
3
#include <bits/stdc++.h> using namespace std; const int NN = 555; const int mod = 1e9 + 7; int a[NN][NN]; int dp[NN][NN][2]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) cin >> a[i][j]; } for (int i = 1; i <= n; i++) dp[i][i][0] = 1; for (int len = 1; len <= n; len++) { for (int i = 1; i <= n; i++) { int j = i + len; if (j > n) break; for (int k = i + 1; k < j; k++) { if (a[i][k]) { dp[i][j][0] = (dp[i][j][0] + 1LL * dp[i][k][1] * dp[k][j][0] % mod) % mod; dp[i][j][1] = (dp[i][j][1] + 1LL * dp[i][k][1] * dp[k][j][1] % mod) % mod; } } dp[i][j][1] = (dp[i][j][1] + dp[i + 1][j][0]) % mod; if (a[i][j]) dp[i][j][0] = (dp[i][j][0] + dp[i][j][1]) % mod; } } cout << dp[1][n][0] << endl; return 0; }
17
#include <bits/stdc++.h> using namespace std; int main() { int n; int a = 0, b = 0; cin >> n; int arr[10]; for (int i = 0; i < 10; i++) arr[i] = 0; for (int i = 0; i < n; i++) { cin >> a >> b; if (a * b == 2) arr[0]++, arr[1]++, arr[2]++; if (a * b == 3) arr[0]++, arr[6]++, arr[9]++; if (a * b == 4) arr[1]++, arr[7]++, arr[9]++; if (a * b == 5) arr[2]++, arr[6]++, arr[7]++; if (a * b == 6) arr[0]++, arr[3]++, arr[4]++; if (a * b == 8) arr[1]++, arr[3]++, arr[8]++; if (a * b == 10) arr[2]++, arr[4]++, arr[8]++; if (a * b == 12) arr[3]++, arr[5]++, arr[9]++; if (a * b == 15) arr[4]++, arr[5]++, arr[6]++; if (a * b == 20) arr[5]++, arr[7]++, arr[8]++; } for (int i = 0; i < 10; i++) { if (arr[i] == 3 || arr[i] == 0) { cout << "WIN"; return 0; } } cout << "FAIL"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long MN = 200; long long d[MN], h[MN]; void solve() { long long n, k; cin >> n >> k; for (long long i = 0; i < n; ++i) cin >> d[i] >> h[i]; long long imax1 = 0, imax2 = 0; for (long long i = 0; i < n; ++i) { if (d[i] > d[imax1]) imax1 = i; if (d[i] - h[i] > d[imax2] - h[imax2]) imax2 = i; } if (k - d[imax1] <= 0) cout << 1 << "\n"; else if (d[imax2] - h[imax2] <= 0) cout << -1 << "\n"; else cout << (k - d[imax1] - 1) / (d[imax2] - h[imax2]) + 2 << "\n"; } int32_t main() { long long t; cin >> t; while (t--) solve(); }
8
#include <bits/stdc++.h> using namespace std; class seg_max_index { public: int n, size; vector<long long> arr; vector<int> hai; long long inf; seg_max_index(int size) : n(size / 2), inf(-(1LL << 60)) { arr.resize(n); hai.resize(n * 2); fill(arr.begin(), arr.end(), inf); this->size = size; for (int i = n; i < size; i++) hai[i] = i - n; } void update(int i, long long v) { arr[i] = v; i += n; while (i > 1) { i /= 2; hai[i] = arr[hai[2 * i]] > arr[hai[2 * i + 1]] ? hai[2 * i] : hai[2 * i + 1]; } } int range_max_index(int left, int right) { if (left > right) return -1; int res = left; left += n, right += n; for (; left <= right; left = (left + 1) >> 1, right = (right - 1) >> 1) { if (left & 1) { if (arr[res] < arr[hai[left]]) res = hai[left]; } if ((right & 1) == 0) { if (arr[res] < arr[hai[right]]) res = hai[right]; } } return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n - 1); for (int i = 0; i < n - 1; i++) { cin >> a[i]; a[i]--; } vector<long long> dp(n - 1); seg_max_index seg(1 << 20); for (int i = 0; i < n - 1; i++) seg.update(i, a[i]); dp[n - 2] = 1; long long res = 1; for (int i = n - 3; i >= 0; i--) { int ind = seg.range_max_index(i + 1, a[i]); dp[i] = a[i] - i + dp[ind] - (a[i] - ind) + (n - a[i] - 1); res += dp[i]; } cout << res << "\n"; return 0; }
15
#include <bits/stdc++.h> using namespace std; __inline bool nextInt(int &val) { char ch; int sgn = 1; while ((ch = getchar()) != EOF) { if (ch == '-') sgn = -1; if (ch >= '0' && ch <= '9') break; } if (ch == EOF) return false; val = (int)(ch - '0'); while (true) { ch = getchar(); if (ch >= '0' && ch <= '9') { val = 10 * val + (int)(ch - '0'); } else break; } val *= sgn; return true; } __inline bool nextString(string &s) { char ch; while ((ch = getchar()) != EOF) { if (ch >= 33 && ch <= 126) break; } if (ch == EOF) return false; s = string(1, ch); while (true) { ch = getchar(); if (ch >= 33 && ch <= 126) { s = s + string(1, ch); } else break; } return true; } int n, sub[111888], a[111888], c[111888]; long long coef[111888], INF = 1000000001LL, INF2 = 1000000001LL * 10001LL; vector<int> e[111888]; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { long long d = gcd(a, b); long long tmp = a / d * b; if (tmp >= INF) tmp = INF; return tmp; } void dfs(int u, int pu) { c[u] = 0; if (pu != -1 && ((int)(e[u]).size()) == 1) { sub[u] = 1; coef[u] = 1; } else { long long d = -1; for (int ie = 0; ie < (((int)(e[u]).size())); ie++) { int v = e[u][ie]; if (v != pu) { dfs(v, u); c[u]++; if (d == -1) d = coef[v]; else d = gcd(d, coef[v]); sub[u] += sub[v]; } } long long prod = 1LL; for (int ie = 0; ie < (((int)(e[u]).size())); ie++) { int v = e[u][ie]; if (v != pu) { prod = lcm(prod, coef[v]); if (prod >= INF) break; } } coef[u] = c[u] * prod; if (coef[u] >= INF) coef[u] = INF; } } void dfs2(int u, int pu) { if (pu != -1) if (coef[pu] < INF) coef[u] = coef[pu] / c[pu]; else coef[u] = INF; for (int ie = 0; ie < (((int)(e[u]).size())); ie++) { int v = e[u][ie]; if (v != pu) dfs2(v, u); } } int main() { ios_base::sync_with_stdio(false); scanf("%d", &n); for (int i = 0; i < (n); i++) scanf("%d", a + i); for (int i = 0; i < (n - 1); i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; e[u].push_back(v); e[v].push_back(u); } dfs(0, -1); dfs2(0, -1); long long ret = 0, k = 0; if (coef[0] < INF) { long long low = 0, high = 1000000000LL, mid; while (low <= high) { mid = low + (high - low) / 2; bool ok = true; for (int u = 1; u < n && ok; u++) if (c[u] == 0 && mid * coef[u] > a[u]) { ok = false; } if (ok) { k = ((k) > (mid) ? (k) : (mid)); low = mid + 1; } else high = mid - 1; } } for (int i = 0; i < (n); i++) if (c[i] == 0) ret += a[i] - coef[i] * k; cout << ret << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } const int maxn = 1e5 + 5; struct tree { int c[2], siz; } t[maxn * 40]; struct edge { int next, to; } e[maxn * 2]; struct node { int l, r; node() {} node(int L, int R) { l = L, r = R; } } zc[114][2]; struct quest { int u, v, k, y; } w[maxn]; int rt[maxn], top[maxn], siz[maxn], f[maxn], son[maxn], p[maxn], d[maxn], pos, n, m, cnt, tot, h[maxn], ti[maxn], fp[maxn], zcnt[2]; void addedge(int x, int y) { e[++cnt].next = h[x]; e[cnt].to = y; h[x] = cnt; } void add(int &id, int o, int l, int r, int v) { id = ++cnt, t[id] = t[o], t[id].siz++; if (l == r) return; int mid = l + r >> 1; v <= mid ? add(t[id].c[0], t[o].c[0], l, mid, v) : add(t[id].c[1], t[o].c[1], mid + 1, r, v); } int ask(int id, int o, int l, int r, int L, int R) { if (l >= L && r <= R) return t[id].siz - t[o].siz; int mid = l + r >> 1, sum = 0; if (L <= mid) sum += ask(t[id].c[0], t[o].c[0], l, mid, L, R); if (R > mid) sum += ask(t[id].c[1], t[o].c[1], mid + 1, r, L, R); return sum; } void dfs1(int u, int fa) { d[u] = d[fa] + 1, siz[u] = 1, f[u] = fa; for (register int i = h[u]; i; i = e[i].next) { int j = e[i].to; if (j == fa) continue; dfs1(j, u); siz[u] += siz[j]; if (siz[j] > siz[son[u]]) son[u] = j; } } void dfs2(int u, int tp) { top[u] = tp, p[u] = ++pos, rt[pos] = rt[pos - 1], fp[pos] = u; add(rt[pos], rt[pos], 0, m, ti[u]); if (son[u]) dfs2(son[u], tp); for (register int i = h[u]; i; i = e[i].next) { int j = e[i].to; if (j == f[u] || j == son[u]) continue; dfs2(j, j); } } int query(int u, int v, int y, int k, int w) { bool pd = 0; int sum = 0, yv = v; zcnt[0] = zcnt[1] = 0; if (ti[u] < y || ti[u] > w) k++; while (top[u] != top[v]) { if (d[top[u]] < d[top[v]]) swap(u, v), pd ^= 1; zc[++zcnt[pd]][pd] = node(p[top[u]], p[u]); u = f[top[u]]; } if (d[u] > d[v]) swap(u, v), pd ^= 1; zc[++zcnt[!pd]][!pd] = node(p[u], p[v]); for (register int i = 1; i <= zcnt[0]; i++) { int L = zc[i][0].l, R = zc[i][0].r, v = ask(rt[R], rt[L - 1], 0, m, 0, y - 1) + ask(rt[R], rt[L - 1], 0, m, w + 1, m); if (k > v) { k -= v; continue; } int l = L, r = R, ans = 0; while (l <= r) { int mid = l + r >> 1; if (ask(rt[R], rt[mid - 1], 0, m, 0, y - 1) + ask(rt[R], rt[mid - 1], 0, m, w + 1, m) >= k) ans = mid, l = mid + 1; else r = mid - 1; } return fp[ans] == yv ? -1 : fp[ans]; } for (register int i = zcnt[1]; i; i--) { int L = zc[i][1].l, R = zc[i][1].r, v = ask(rt[R], rt[L - 1], 0, m, 0, y - 1) + ask(rt[R], rt[L - 1], 0, m, w + 1, m); if (k > v) { k -= v; continue; } int l = L, r = R, ans = 0; while (l <= r) { int mid = l + r >> 1; if (ask(rt[mid], rt[L - 1], 0, m, 0, y - 1) + ask(rt[mid], rt[L - 1], 0, m, w + 1, m) >= k) ans = mid, r = mid - 1; else l = mid + 1; } return fp[ans] == yv ? -1 : fp[ans]; } return -1; } int main() { int op, x; n = read(); for (register int i = 1; i <= n; i++) { x = read(); addedge(x, i), addedge(i, x); } m = read(); for (register int i = 1; i <= m; i++) { op = read(), x = read(); if (op == 1) ti[x] = i; else w[i].u = x, w[i].v = read(), w[i].k = read(), w[i].y = read() + 1; } dfs1(1, 0), dfs2(1, 1); for (register int i = 1; i <= m; i++) { if (!w[i].u) continue; printf("%d\n", query(w[i].u, w[i].v, w[i].y, w[i].k, i)); } return 0; }
21
#include <bits/stdc++.h> using namespace std; inline long long getint() { long long _x = 0, _tmp = 1; char _tc = getchar(); while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar(); if (_tc == '-') _tc = getchar(), _tmp = -1; while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar(); return _x * _tmp; } inline long long add(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x + _y; if (_ >= _mod) _ -= _mod; return _; } inline long long sub(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x - _y; if (_ < 0) _ += _mod; return _; } inline long long mul(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x * _y; if (_ >= _mod) _ %= _mod; return _; } long long mypow(long long _a, long long _x, long long _mod) { if (_x == 0) return 1ll; long long _tmp = mypow(_a, _x / 2, _mod); _tmp = mul(_tmp, _tmp, _mod); if (_x & 1) _tmp = mul(_tmp, _a, _mod); return _tmp; } long long mymul(long long _a, long long _x, long long _mod) { if (_x == 0) return 0ll; long long _tmp = mymul(_a, _x / 2, _mod); _tmp = add(_tmp, _tmp, _mod); if (_x & 1) _tmp = add(_tmp, _a, _mod); return _tmp; } inline bool equal(double _x, double _y) { return _x > _y - 1e-9 && _x < _y + 1e-9; } int __ = 1, _cs; void build() {} int n, m, q, x[202020], y[202020]; vector<pair<int, int> > qry; int ql[202020], qr[202020], qs[202020], qt[202020]; bool ans[202020]; int mn[1010][1010]; inline void add(int idx) { mn[x[idx]][y[idx]] = mn[y[idx]][x[idx]] = idx; for (int i = 1; i <= n; i++) { mn[x[idx]][i] = min(mn[x[idx]][i], mn[y[idx]][i]); mn[y[idx]][i] = min(mn[y[idx]][i], mn[x[idx]][i]); } } void init() { n = getint(); m = getint(); q = getint(); for (int i = 1; i <= m; i++) { x[i] = getint(); y[i] = getint(); } for (int i = 0; i < q; i++) { ql[i] = getint(); qr[i] = getint(); qs[i] = getint(); qt[i] = getint(); qry.push_back({ql[i], i}); if (qs[i] == qt[i]) ans[i] = true; } sort((qry).begin(), (qry).end()); reverse((qry).begin(), (qry).end()); } void solve() { int midx = m; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) mn[i][j] = 202020; for (size_t _ = 0; _ < qry.size(); _++) { int i = qry[_].second; while (midx >= 1 && midx >= ql[i]) add(midx--); if (mn[qs[i]][qt[i]] <= qr[i]) ans[i] = true; } for (int i = 0; i < q; i++) puts(ans[i] ? "Yes" : "No"); } int main() { build(); while (__--) { init(); solve(); } }
20
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:36777216") using namespace std; string __file__() { string res = "<stdin>"; int r = int((res).size()) - 1; while (res[r] != '.') --r; int l = r - 1; while (res[l] != '\\') --l; ++l; return res.substr(l, r - l); } void Exec(string a, string b, string c) { if (b.empty()) b = __file__(); string cmd = a + ' ' + b + '.' + c; system(cmd.c_str()); } void Ruby(string file = "") { Exec("ruby", file, "rb"); } void Python(string file = "") { Exec("python", file, "py"); } void Haskell(string file = "") { Exec("runghc", file, "hs"); } void Pascal(string file = "") { Exec("pascal", file, "pas"); } void Ocaml(string file = "") { Exec("ocaml", file, "ml"); } template <class T> inline T &RD(T &); template <class T> inline void OT(const T &); inline long long RD() { long long x; return RD(x); } inline double &RF(double &); inline double RF() { double x; return RF(x); } inline char *RS(char *s); inline char &RC(char &c); inline char RC(); inline char &RC(char &c) { scanf(" %c", &c); return c; } inline char RC() { char c; return RC(c); } template <class T> inline T &RDD(T &x) { char c; for (c = getchar(); c < '-'; c = getchar()) ; if (c == '-') { x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c; } else { x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; } return x; } inline long long RDD() { long long x; return RDD(x); } template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); return x0; } template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); return x0; } template <class T0, class T1, class T2, class T3> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); return x0; } template <class T0, class T1, class T2, class T3, class T4> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0; } template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } inline char &RC(char &a, char &b) { RC(a), RC(b); return a; } inline char &RC(char &a, char &b, char &c) { RC(a), RC(b), RC(c); return a; } inline char &RC(char &a, char &b, char &c, char &d) { RC(a), RC(b), RC(c), RC(d); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e) { RC(a), RC(b), RC(c), RC(d), RC(e); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a; } inline double &RF(double &a, double &b) { RF(a), RF(b); return a; } inline double &RF(double &a, double &b, double &c) { RF(a), RF(b), RF(c); return a; } inline double &RF(double &a, double &b, double &c, double &d) { RF(a), RF(b), RF(c), RF(d); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e) { RF(a), RF(b), RF(c), RF(d), RF(e); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e, double &f) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e, double &f, double &g) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a; } inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } template <class T0, class T1> inline void RDD(T0 &a, T1 &b) { RDD(a), RDD(b); } template <class T0, class T1, class T2> inline void RDD(T0 &a, T1 &b, T2 &c) { RDD(a), RDD(b), RDD(c); } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T> inline void CLR(T &A) { A.clear(); } template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) { RST(A0), RST(A1); } template <class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2) { RST(A0), RST(A1), RST(A2); } template <class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { RST(A0), RST(A1), RST(A2), RST(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); } template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) { FLC(A0, x), FLC(A1, x); } template <class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x); } template <class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x); } template <class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x); } template <class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) { while (!Q.empty()) Q.pop(); } template <class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) { while (!Q.empty()) Q.pop(); } template <class T> inline void CLR(stack<T> &S) { while (!S.empty()) S.pop(); } template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) { CLR(A0), CLR(A1); } template <class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2) { CLR(A0), CLR(A1), CLR(A2); } template <class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { CLR(A0), CLR(A1), CLR(A2), CLR(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); } template <class T> inline void CLR(T &A, int n) { for (int i = 0; i < int(n); ++i) CLR(A[i]); } template <class T> inline bool EPT(T &a) { return a.empty(); } template <class T> inline T &SRT(T &A) { sort(A.begin(), A.end()); return A; } template <class T> inline T &RVS(T &A) { reverse(A.begin(), A.end()); return A; } template <class T> inline T &UNQ(T &A) { A.resize(unique(SRT(A).begin(), SRT(A).end()) - A.begin()); return A; } template <class T, class C> inline T &SRT(T &A, C B) { sort(A.begin(), A.end(), B); return A; } const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const long long INFF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const double OO = 1e20; const double PI = acos(-1.0); const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; template <class T> inline void checkMin(T &a, const T b) { if (b < a) a = b; } template <class T> inline void checkMax(T &a, const T b) { if (a < b) a = b; } template <class T> inline void checkMin(T &a, T &b, const T x) { checkMin(a, x), checkMin(b, x); } template <class T> inline void checkMax(T &a, T &b, const T x) { checkMax(a, x), checkMax(b, x); } template <class T, class C> inline void checkMin(T &a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkMax(T &a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; } inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; } inline int sgn(double x, double y) { return sgn(x - y); } inline double cos(double a, double b, double c) { return (sqr(a) + sqr(b) - sqr(c)) / (2 * a * b); } inline double cot(double x) { return 1. / tan(x); }; inline double sec(double x) { return 1. / cos(x); }; inline double csc(double x) { return 1. / sin(x); }; namespace CG { struct Po; struct Line; struct Seg; struct Triangle; struct Circle; struct Po { double x, y; Po(double x = 0, double y = 0) : x(x), y(y) {} inline void input() { RF(x, y); } inline friend istream &operator>>(istream &in, Po &p) { return in >> p.x >> p.y; } inline friend ostream &operator<<(ostream &out, Po p) { return out << "(" << p.x << ", " << p.y << ")"; } inline bool operator==(const Po &r) const { return !sgn(x - r.x) && !sgn(y - r.y); }; inline bool operator!=(const Po &r) const { return sgn(x - r.x) || sgn(y - r.y); } inline bool operator<(const Po &r) const { return sgn(x, r.x) < 0 || (!sgn(x, r.x) && sgn(y, r.y) < 0); } inline bool operator<=(const Po &r) const { return *this < r || *this == r; } inline bool operator>(const Po &r) const { return !(*this <= r); } inline bool operator>=(const Po &r) const { return !(*this < r); } inline Po operator-() const { return Po(-x, -y); } inline Po &operator+=(const Po &r) { x += r.x, y += r.y; return *this; } inline Po &operator-=(const Po &r) { x -= r.x, y -= r.y; return *this; } inline Po &operator*=(double k) { x *= k, y *= k; return *this; } inline Po &operator/=(double k) { x /= k, y /= k; return *this; } inline Po operator+(const Po &r) const { return Po(x + r.x, y + r.y); } inline Po operator-(const Po &r) const { return Po(x - r.x, y - r.y); } inline Po operator*(double k) const { return Po(x * k, y * k); } inline Po operator/(double k) const { return Po(x / k, y / k); } inline double operator*(const Po &) const; inline double operator^(const Po &) const; inline Po operator*(const Line &) const; inline double length2() const { return sqr(x) + sqr(y); } inline double length() const { return sqrt(length2()); } inline Po unit() const { return *this / length(); } inline bool dgt() const { return !sgn(x) && !sgn(y); } inline double atan() const { return atan2(y, x); } inline Po lt90() { sym(), x = -x; return *this; } inline Po rt90() { sym(), y = -y; return *this; } inline Po sym() { swap(x, y); return *this; } inline Po symm() { x = -x, y = -y; return *this; } inline Po rotate(double a, const Po &o = Po()) { x -= o.x, y -= o.y; return *this = Po(x * cos(a) - y * sin(a), y * cos(a) + x * sin(a)) + o; } inline Po symmetry(const Line &); }; inline Po sym(Po p) { p.sym(); return p; } inline Po symm(Po p) { p.symm(); return p; } inline Po lt90(Po p) { p.lt90(); return p; } inline Po rt90(Po p) { p.rt90(); return p; } inline Po rotate(Po p, double alpha, const Po &o = Po()) { return p.rotate(alpha, o); } inline Po operator*(double k, Po a) { return a * k; } inline double dot(const double &x1, const double &y1, const double &x2, const double &y2) { return x1 * x2 + y1 * y2; } inline double dot(const Po &a, const Po &b) { return dot(a.x, a.y, b.x, b.y); } inline double dot(const Po &p0, const Po &p1, const Po &p2) { return dot(p1 - p0, p2 - p0); } inline double det(const double &x1, const double &y1, const double &x2, const double &y2) { return x1 * y2 - x2 * y1; } inline double det(const Po &a, const Po &b) { return det(a.x, a.y, b.x, b.y); } inline double det(const Po &p0, const Po &p1, const Po &p2) { return det(p1 - p0, p2 - p0); } template <class T1, class T2> inline int dett(const T1 &x, const T2 &y) { return sgn(det(x, y)); } template <class T1, class T2> inline int dott(const T1 &x, const T2 &y) { return sgn(dot(x, y)); } template <class T1, class T2, class T3> inline int dett(const T1 &x, const T2 &y, const T3 &z) { return sgn(det(x, y, z)); } template <class T1, class T2, class T3> inline int dott(const T1 &x, const T2 &y, const T3 &z) { return sgn(dot(x, y, z)); } template <class T1, class T2, class T3, class T4> inline int dett(const T1 &x, const T2 &y, const T3 &z, const T4 &w) { return sgn(det(x, y, z, w)); } template <class T1, class T2, class T3, class T4> inline int dott(const T1 &x, const T2 &y, const T3 &z, const T4 &w) { return sgn(dot(x, y, z, w)); } inline double dist2(const double &x, const double &y) { return sqr(x) + sqr(y); } inline double dist2(const double &x, const double &y, const double &z) { return sqr(x) + sqr(y) + sqr(z); } inline double dist2(const Po &a, const Po &b) { return dist2(a.x - b.x, a.y - b.y); } template <class T1, class T2> inline double dist(const T1 &x, const T2 &y) { return sqrt(dist2(x, y)); } template <class T1, class T2, class T3> inline double dist(const T1 &x, const T2 &y, const T3 &z) { return sqrt(dist2(x, y, z)); } double Po::operator*(const Po &r) const { return dot(*this, r); } double Po::operator^(const Po &r) const { return det(*this, r); } struct Line { Po a, b; Line(const Po &a = Po(), const Po &b = Po()) : a(a), b(b) {} Line(double x0, double y0, double x1, double y1) : a(Po(x0, y0)), b(Po(x1, y1)) {} Line(const Line &l) : a(l.a), b(l.b) {} Line(const Po &a, double alpha) : a(a), b(a + Po(cos(alpha), sin(alpha))) {} Line(double A, double B, double C) { C = -C; if (!::sgn(A)) a = Po(0, C / B), b = Po(1, C / B); else if (!::sgn(B)) a = Po(C / A, 0), b = Po(C / A, 1); else a = Po(0, C / B), b = Po(1, (C - A) / B); } void input() { a.input(), b.input(); } inline friend istream &operator>>(istream &in, Line &p) { return in >> p.a >> p.b; } inline friend ostream &operator<<(ostream &out, Line p) { return out << p.a << "-" << p.b; } inline Line operator+(const Po &x) const { return Line(a + x, b + x); } inline Line operator-(const Po &x) const { return Line(a - x, b - x); } inline Line operator*(double k) const { return Line(a * k, b * k); } inline Po operator*(const Line &) const; inline Po operator*(const Po &) const; inline double length2() const { return (b - a).length2(); } inline double length() const { return (b - a).length(); } inline bool dgt() const { return (b - a).dgt(); } inline int sgn(const Po &p) const { return dett(a, b, p); } inline int sgn(const Line &) const; inline bool same_sgn(const Po &p1, const Po &p2) const { return sgn(p1) == sgn(p2); } inline void getEquation(double &K, double &B) const { K = ::sgn(a.x, b.x) ? (b.y - a.y) / (b.x - a.x) : OO; B = a.y - K * a.x; } inline void getEquation(double &A, double &B, double &C) const { A = a.y - b.y, B = b.x - a.x, C = det(a, b); } inline void getIntersect(const Line &, Po &) const; inline Po foot(const Po &) const; }; inline double dot(const Line &l1, const Line &l2) { return dot(l1.b - l1.a, l2.b - l2.a); } inline double det(const Line &l1, const Line &l2) { return det(l1.b - l1.a, l2.b - l2.a); } inline int Line::sgn(const Line &l) const { return dett(*this, l); } inline Po intersect(const Line &l1, const Line &l2) { return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1)); } inline Po Line::operator*(const Line &l) const { return intersect(*this, l); } inline Po Line::foot(const Po &p) const { return *this * Line(p, p + lt90(b - a)); } inline Po Line::operator*(const Po &p) const { return *this * p; } inline Po Po::operator*(const Line &l) const { return l * *this; } inline void Line::getIntersect(const Line &l, Po &p) const { p = *this * l; } inline Po Po::symmetry(const Line &l) { Po o = l * *this; return *this = o + o - *this; } inline Po symmetry(Po p, const Line &l) { return p.symmetry(l); } struct Seg : public Line { Seg(const Po &a = Po(), const Po &b = Po()) : Line(a, b) {} Seg(double x0, double y0, double x1, double y1) : Line(x0, y0, x1, y1) {} Seg(const Line &l) : Line(l) {} Seg(const Po &a, double alpha) : Line(a, alpha) {} Seg(double A, double B, double C) : Line(A, B, C) {} inline int sgn(const Po &p) const; inline bool qrt(const Seg &l) const; inline int sgn(const Seg &l) const; }; inline bool Seg::qrt(const Seg &l) const { return min(a.x, b.x) <= max(l.a.x, l.b.x) && min(l.a.x, l.b.x) <= max(a.x, b.x) && min(a.y, b.y) <= max(l.a.y, l.b.y) && min(l.a.y, l.b.y) <= max(a.y, b.y); } inline int Seg::sgn(const Seg &l) const { if (!qrt(l)) return -1; int d1 = dett(a, b, l.a), d2 = dett(a, b, l.b), d3 = dett(l.a, l.b, a), d4 = dett(l.a, l.b, b); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 1; return ((!d1 && dott(l.a - a, l.a - b) <= 0) || (!d2 && dott(l.b - a, l.b - b) <= 0) || (!d3 && dott(a - l.a, a - l.b) <= 0) || (!d4 && dott(b - l.a, b - l.b) <= 0)) ? 0 : -1; } inline int Seg::sgn(const Po &p) const { if (dett(p, a, b)) return -1; if (p == a || p == b) return 0; return (::sgn(a.x, p.x) * ::sgn(b.x, p.x) <= 0 && ::sgn(a.y, p.y) * ::sgn(b.y, p.y) <= 0) ? 1 : -1; } inline double dist2(const Line &l, const Po &p) { Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length2(); } inline double dist2(const Line &l1, const Line &l2) { return dett(l1, l2) ? 0 : dist2(l1, l2.a); } inline double dist2(const Seg &l, const Po &p) { Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b; if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist2(Line(l), p); else return min(v1.length2(), v2.length2()); } inline double dist2(const Seg &l1, const Line &l2) { Po v0 = l2.b - l2.a, v1 = l1.a - l2.a, v2 = l1.b - l2.a; double c1 = det(v0, v1), c2 = det(v0, v2); return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length2(); } inline double dist2(const Seg &l1, const Seg &l2) { if (l1.sgn(l2)) return 0; else return min(dist2(l2, l1.a), dist2(l2, l1.b), dist2(l1, l2.a), dist2(l1, l2.b)); } const int Disjoint = -2, Exscribe = -1, Cross = 0, Inscribe = 1, Contain = 2; Po getX3(const Po &a, const Po &b, const Po &c) { Po v0 = b - a, v1 = c - a; double s0 = v0.length2(), s1 = v1.length2(), d = det(v0, v1); return Po(s0 * v1.y - s1 * v0.y, s1 * v0.x - s0 * v1.x) / d + a; } Po getX4(const Po &a, const Po &b, const Po &c) { return Line(a, Line(b, c) * a) * Line(b, Line(a, c) * b); } struct Circle { Po o; double r; Circle(const Po &o = Po(), double r = 0) : o(o), r(r) {} Circle(const Po &a, const Po &b) { o = (a + b) / 2, r = dist(a, b) / 2; } Circle(const Po &a, const Po &b, const Po &c) { o = getX3(a, b, c), r = dist(o, a); } void input() { RF(o.x, o.y, r); } void output() { printf("%.2f %.2f %.2f\n", o.x, o.y, r); } bool operator<(const Circle &c) const { return r < c.r; } inline int sgn(const Po &p) const { return ::sgn(sqr(r), dist2(o, p)); } inline int sgn(const Line &l) const { return ::sgn(sqr(r), dist2(l, o)); } inline int sgn(const Circle &c) const { double d = dist2(o, c.o); if (::sgn(sqr(r + c.r), d) < 0) return Disjoint; if (!::sgn(sqr(r + c.r), d)) return Exscribe; if (!::sgn(sqr(r - c.r), d)) return Inscribe; if (::sgn(sqr(r - c.r), d) > 0) return Contain; return Cross; } inline double s() { return PI * sqr(r); } inline double p() { return 2 * PI * r; } inline Po operator^(const Circle &c) const { return Po(det(Po(o.x, r), Po(c.o.x, c.r)), det(Po(o.y, r), Po(c.o.y, c.r))) / (c.r - r); } inline void getIntersect(const Line &l, Po &p0, Po &p1) const { Po m = l.foot(o), d = (l.b - l.a).unit() * sqrt(sqr(r) - dist2(l, o)); p0 = m + d, p1 = m - d; } inline void getTangency(const Po &p, Po &p0, Po &p1) const { double d = dist(o, p), a = acos(r / d); Po op = (p - o).unit() * r; p0 = o + rotate(op, a), p1 = o + rotate(op, -a); } inline void getTangency(const Circle &c, Po &p0, Po &p1, Po &p2, Po &p3) const { if (!::sgn(r, c.r)) { Po d = (o - c.o).rt90().unit() * r; p0 = o + d, p1 = o - d, p2 = c.o + d, p3 = c.o - d; } else { Po p = (*this) ^ c; getTangency(p, p0, p1), c.getTangency(p, p2, p3); } } inline double arc(const Po &a, const Po &b) { double alpha = acos(cos(dist(a, o), dist(b, o), dist(a, b))); if (det(o, a, b) < 0) alpha = 2 * PI - alpha; return alpha * r; } }; struct Triangle { Po A, B, C; double a, b, c; double alpha, beta, theta; double r, R; double S, P; Po I, G, O, H; void init() { a = dist(B, C), b = dist(a, c), c = dist(a, b); alpha = acos(cos(b, c, a)), beta = acos(cos(a, c, b)), theta = acos(cos(a, b, c)); P = a + b + c; double s = P / 2; S = sqrt(s * (s - a) * (s - b) * (s - c)); R = (a * b * c) / (4 * S), r = 2 * S / P; I = Po(a * A.x + b * B.x + c * C.x, a * A.y + b * B.y + c * C.y) / P; G = (A + B + C) / 3; O = getX3(A, B, C), H = getX4(A, B, C); assert(!sgn(dist(I, O), sqrt(R * (R - 2 * r)))); assert(!sgn(dist(H, G), dist(O, H) * 2 / 3)); } void input() { A.input(), B.input(), C.input(); init(); } }; double getArea(vector<Po> &P) { double S = 0; for (int i = 0; i < int(int((P).size())); ++i) S += det(P[i], P[i + 1]); return S; } vector<Po> getConvexHull(vector<Po> &P) { int n = int((P).size()); if (n <= 3) return P; SRT(P); vector<Po> C; C.resize(n + 9); int nn = -1; for (int i = 0; i < int(n); ++i) { while (nn > 0 && dett(C[nn - 1], C[nn], P[i]) <= 0) --nn; C[++nn] = P[i]; } int _nn = nn; for (int i = int(n - 1 - 1); i >= int(0); --i) { while (nn > _nn && dett(C[nn - 1], C[nn], P[i]) <= 0) --nn; C[++nn] = P[i]; } C.resize(nn + 1); return C; } Circle getMinimalCoverCircle(vector<Po> &P) { random_shuffle(P.begin(), P.end()); int n = int((P).size()); Circle C(P[0]); for (int i = int(1); i < int(n); ++i) if (!~C.sgn(P[i])) { C = Circle(P[i]); for (int j = 0; j < int(i); ++j) if (!~C.sgn(P[j])) { C = Circle(P[i], P[j]); for (int k = 0; k < int(j); ++k) if (!~C.sgn(P[k])) { C = Circle(P[i], P[j], P[k]); } } } return C; } struct Polygon { vector<Po> P; void input(); }; bool equal(const pair<double, double> &lhs, const Seg &rhs) { double k, b; rhs.getEquation(k, b); return k == lhs.first && b == lhs.second; } double getUnion(vector<Polygon> &P, vector<Seg> &S) { vector<pair<double, double> > L; for (__typeof(S.begin()) Si = S.begin(); Si != S.end(); ++Si) { double k, b; Si->getEquation(k, b); L.push_back(make_pair(k, b)); } UNQ(L); double res = 0; for (__typeof(L.begin()) Li = L.begin(); Li != L.end(); ++Li) { vector<pair<double, int> > I; Line l0(0, Li->second, 1, Li->first + Li->second); for (__typeof(P.begin()) Pi = P.begin(); Pi != P.end(); ++Pi) { int i; for (i = int(1); i < int(int((Pi->P).size())); ++i) if (equal(*Li, Seg(Pi->P[i - 1], Pi->P[i]))) break; if (i != int((Pi->P).size())) continue; vector<Po> cut; for (i = int(1); i < int(int((Pi->P).size())); ++i) { Seg l1(Pi->P[i - 1], Pi->P[i]); if (!dett(l0, l1)) continue; Po p = l0 * l1; if (dott(p, l1.a, l1.b) <= 0) cut.push_back(p); } UNQ(cut); if (int((cut).size()) == 2) { I.push_back(make_pair(cut[0].x, 1)); I.push_back(make_pair(cut[1].x, -1)); } } for (__typeof(S.begin()) Si = S.begin(); Si != S.end(); ++Si) if (equal(*Li, *Si)) { I.push_back(make_pair(min(Si->a.x, Si->b.x), 2)); I.push_back(make_pair(max(Si->a.x, Si->b.x), -2)); } } return res; } double getUnion(vector<Polygon> &P) { vector<Seg> up, down; for (__typeof(P.begin()) it = P.begin(); it != P.end(); ++it) { for (int i = int(1); i < int(int((it->P).size())); ++i) { Seg s(it->P[i - 1], it->P[i]); int t = sgn(s.a.x, s.b.x); if (t > 0) up.push_back(s); else if (t < 0) down.push_back(s); } } return getUnion(P, up) - getUnion(P, down); } } // namespace CG using namespace CG; template <class T> inline T &RD(T &x) { char c; for (c = getchar(); c < '0'; c = getchar()) ; x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x; } inline double &RF(double &x) { scanf("%lf", &x); return x; } inline char *RS(char *s) { scanf("%s", s); return s; } long long last_ans; int Case; template <class T> inline void OT(const T &x) { cout << x << endl; } namespace acm { const int N = 510; const int M = 6; const double eps = EPS; int dcmp(double x) { if (x < -eps) return -1; else return x > eps; } struct poly { int n; vector<Po> cp; void get() { n = 4; cp.resize(4); for (int i = 0; i < (int)(n); ++i) cp[i].input(); cp = getConvexHull(cp); } } ply[N]; int n; pair<Po, Po> seg1[N * M], seg2[N * M]; Po lin[N * M]; Po getline(Po u, Po v) { double k; if (dcmp(u.x - v.x) == 0) k = OO; else k = (u.y - v.y) / (u.x - v.x); return Po(k, u.y - k * u.x); } bool getcut(Po lin, Po a, Po b, Po &cp) { double t = lin.x * (a.x - b.x) - (a.y - b.y); if (dcmp(t) == 0) return false; double x = ((a ^ b) - (a.x - b.x) * lin.y) / t; cp = Po(x, lin.x * x + lin.y); return true; } double calc(pair<Po, Po> seg[], int m) { int ln = 0; double ans = 0; Po A, B, cp; for (int i = 0; i < (int)(m); ++i) lin[ln++] = getline(seg[i].first, seg[i].second); sort(lin, lin + ln); ln = unique(lin, lin + ln) - lin; for (int i = 0; i < (int)(ln); ++i) { vector<pair<double, int> > mark; Line l0(0, lin[i].y, 1, lin[i].x + lin[i].y); for (int j = 0; j < (int)(n); ++j) { bool touch = 0; for (int k = 0; k < (int)(ply[j].n); ++k) if (lin[i] == getline(ply[j].cp[k], ply[j].cp[k + 1])) { touch = 1; break; } if (touch) continue; vector<Po> cut; for (int k = 0; k < (int)(ply[j].n); ++k) { Seg l1(ply[j].cp[k], ply[j].cp[k + 1]); if (!dett(l0, l1)) continue; Po p = l0 * l1; if (dott(p, l1.a, l1.b) <= 0) cut.push_back(p); } UNQ(cut); if (((int)cut.size()) == 2) { mark.push_back(make_pair(cut[0].x, 0)); mark.push_back(make_pair(cut[1].x, 1)); } } for (int j = 0; j < (int)(m); ++j) if (lin[i] == getline(seg[j].first, seg[j].second)) { double mn = min(seg[j].first.x, seg[j].second.x); double mx = max(seg[j].first.x, seg[j].second.x); mark.push_back(make_pair(mn, 2)); mark.push_back(make_pair(mx, 3)); } sort(mark.begin(), mark.end()); double last = mark[0].first; int in = 0, ct = 0; for (int j = 0; j < (int)(((int)mark.size())); ++j) { double y0 = lin[i].x * last + lin[i].y; double y1 = lin[i].x * mark[j].first + lin[i].y; if (!in && ct) ans += (y0 + y1) * (mark[j].first - last) / 2; last = mark[j].first; if (mark[j].second == 0) in++; if (mark[j].second == 1) in--; if (mark[j].second == 2) ct++; if (mark[j].second == 3) ct--; } } return ans; } double PolyUnion(poly ply[], int n) { int n1, n2, tot = n; n1 = n2 = 0; for (int i = 0; i < (int)(n); ++i) for (int j = 0; j < (int)(ply[i].n); ++j) { Po A = ply[i].cp[j]; Po B = ply[i].cp[j + 1]; if (dcmp(A.x - B.x) > 0) seg1[n1++] = make_pair(A, B); if (dcmp(A.x - B.x) < 0) seg2[n2++] = make_pair(A, B); } return calc(seg1, n1) - calc(seg2, n2); } void solve() { for (int i = 0; i < (int)(n); ++i) { ply[i].get(); } double area = PolyUnion(ply, n); double tot = 0; for (int i = 0; i < (int)(n); ++i) for (int j = 0; j < (int)(ply[i].n); ++j) tot += det(ply[i].cp[j], ply[i].cp[j + 1]); tot /= 2; printf("%.10f\n", tot / area); } void icpc() { while (~scanf("%d", &n)) { solve(); } } } // namespace acm int main() { acm::icpc(); return 0; }
19
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long n) { long long s = 1; while (n) { if (n & 1) s = s * a % 1000000007; a = a * a % 1000000007; n >>= 1; } return s; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long c, i, j, k, l, m, t, x, y, n, z; string s; cin >> n >> x; long long a1[n]; priority_queue<long long, vector<long long>, greater<long long> > pq; for (i = 0; i < n; i++) cin >> a1[i]; long long sum = 0; for (i = 0; i < n; i++) sum = sum + a1[i]; z = 0; map<long long, long long> mp; for (i = 0; i < n; i++) mp[sum - a1[i]]++; while (mp.begin()->second % x == 0) { mp[mp.begin()->first + 1] += mp.begin()->second / x; mp.erase(mp.begin()); } long long b = mp.begin()->first; cout << power(x, min(sum, b)) << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, c[N], pc[N]; long long res[N]; vector<int> E[N]; map<int, int> S[N]; map<int, long long> cnt[N]; void dfs(int cur, int pre) { S[cur].insert(make_pair(c[cur], 1)); cnt[cur].insert(make_pair(1, c[cur])); for (int j = (0); j <= (E[cur].size() - 1); j++) { int to = E[cur][j]; if (to == pre) continue; dfs(to, cur); if (S[pc[to]].size() > S[pc[cur]].size()) swap(pc[to], pc[cur]); for (__typeof(S[pc[to]].begin()) it = S[pc[to]].begin(); it != S[pc[to]].end(); it++) { cnt[pc[cur]][S[pc[cur]][it->first]] -= it->first; S[pc[cur]][it->first] += it->second; cnt[pc[cur]][S[pc[cur]][it->first]] += it->first; } } res[cur] = cnt[pc[cur]].rbegin()->second; } int main() { scanf("%d", &n); for (int i = 1; i <= (n); i++) scanf("%d", &c[i]); for (int i = 1; i <= (n - 1); i++) { int u, v; scanf("%d%d", &u, &v); E[u].push_back(v); E[v].push_back(u); } for (int i = 1; i <= (n); i++) pc[i] = i; dfs(1, 0); for (int i = 1; i <= (n); i++) printf("%I64d ", res[i]); printf("\n"); return 0; }
15
#include <bits/stdc++.h> using namespace std; int w[2 * 100010], n, a[100010]; struct point { int x, y; point() {} point(int a, int b) { x = a, y = b; } void sc() { scanf("%d%d", &x, &y); } void pr() { printf("%d %d\n", x, y); } bool eq(point b) { return x == b.x && y == b.y; } point add() { return point(x + 1, y + 1); } } p[100010], ans[100010], temp[100010], mp[2 * 100010]; int cmp(point a, point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool solve() { memset(w, 0, sizeof(w)); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { p[i].sc(); int t = p[i].y - p[i].x; w[100010 + t]++; } sort(p, p + n, cmp); point start = point(-1, -1); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { w[100010 + a[i]]--; if (w[100010 + a[i]] < 0) return false; mp[a[i] + 100010] = start; } for (int i = 0; i < n; i++) { if (mp[a[i] + 100010].eq(start)) { if (a[i] == 0) ans[i] = point(0, 0); else if (a[i] < 0) { if (mp[a[i] + 100010 + 1].eq(start)) return false; ans[i] = point(-a[i], 0); } else { if (mp[a[i] + 100010 - 1].eq(start)) return false; ans[i] = point(0, a[i]); } mp[a[i] + 100010] = ans[i]; } else { ans[i] = mp[a[i] + 100010].add(); if (mp[a[i] + 100010 + 1].x != ans[i].x - 1) return false; if (mp[a[i] + 100010 - 1].y != ans[i].y - 1) return false; mp[a[i] + 100010] = ans[i]; } temp[i] = ans[i]; } sort(temp, temp + n, cmp); for (int i = 0; i < n; i++) if (!p[i].eq(temp[i])) return false; return true; } int main() { while (~scanf("%d", &n)) { if (!solve()) printf("NO\n"); else { printf("YES\n"); for (int i = 0; i < n; i++) ans[i].pr(); } } return 0; }
9
#include <bits/stdc++.h> using namespace std; bool check(long long int *a, long long int n, long long int l, long long int x) { for (int i = 0; i < n; i++) if (binary_search(a, a + n, a[i] - x) | binary_search(a, a + n, a[i] + x)) return true; return false; } void func(long long int *a, long long int n, long long int l, long long int x, long long int y) { bool flag1 = false, flag2 = false; flag1 = check(a, n, l, x); flag2 = check(a, n, l, y); if (!flag1 && !flag2) { bool flag = false; int pos; for (int i = 0; i < n; i++) { long long int z = a[i] + y; if (z <= l) if (binary_search(a, a + n, z + x) | binary_search(a, a + n, z - x)) { flag = true; pos = z; break; } z = a[i] - y; if (z >= 0) if (binary_search(a, a + n, z + x) | binary_search(a, a + n, z - x)) { flag = true; pos = z; break; } } if (!flag) for (int i = 0; i < n; i++) { long long int z = a[i] + x; if (z <= l) if (binary_search(a, a + n, z + y) | binary_search(a, a + n, z - y)) { flag = true; pos = z; break; } z = a[i] - x; if (z >= 0) if (binary_search(a, a + n, z + y) | binary_search(a, a + n, z - y)) { flag = true; pos = z; break; } } if (!flag) cout << 2 << endl << x << endl << y << endl; else cout << 1 << endl << pos << endl; } else if (!flag1) cout << 1 << endl << x << endl; else if (!flag2) cout << 1 << endl << y << endl; else cout << 0 << endl; } int main() { ios_base::sync_with_stdio(false); long long int n, l, x, y; cin >> n >> l >> x >> y; long long int a[n + 1]; for (long long int i = 0; i < n; i++) cin >> a[i]; func(a, n, l, x, y); return 0; }
9
#include <bits/stdc++.h> using namespace std; pair<int, int> a[2005]; pair<int, int> b[2005]; int maxv[1000005]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { scanf("%d", &a[i].first); scanf("%d", &a[i].second); } for (int i = 0; i < m; i++) { scanf("%d", &b[i].first); scanf("%d", &b[i].second); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i].first <= b[j].first && a[i].second <= b[j].second) { int id = b[j].first - a[i].first; maxv[id] = max(maxv[id], b[j].second - a[i].second + 1); } } } int ans = 1 << 30; int Max = 0; for (int i = 1e6; i >= 0; i--) { Max = max(Max, maxv[i]); ans = min(ans, Max + i); } cout << ans; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2; cout << l1 << ' ' << ((l1 != r2) ? r2 : l2) << endl; } }
0
#include <bits/stdc++.h> using namespace std; vector<pair<char, int> > as, bs; int N; vector<pair<int, int> > solve(int initmoveb, int initmovea) { vector<pair<int, int> > ans; vector<pair<char, int> > na, nb; int bcnt = 0, acnt = 0; for (int i = 0; i < initmoveb; ++i) { na.push_back(bs[i]); bcnt += bs[i].second; } for (int i = 0; i < initmovea; ++i) { nb.push_back(as[i]); acnt += as[i].second; } for (int i = initmoveb; i < (int)bs.size(); ++i) { if (!nb.empty() && nb.back().first == bs[i].first) nb.back().second += bs[i].second; else nb.push_back(bs[i]); } for (int i = initmovea; i < (int)as.size(); ++i) { if (!na.empty() && na.back().first == as[i].first) na.back().second += as[i].second; else na.push_back(as[i]); } ans.emplace_back(bcnt, acnt); while (!nb.empty() && nb.back().first == 'b') nb.pop_back(); while (!na.empty() && na.back().first == 'a') na.pop_back(); while (na.size() != nb.size()) { if (na.size() < nb.size()) { char targ = (na.size() % 2) ? 'a' : 'b'; na.insert(na.begin(), make_pair(targ, 0)); } else { char targ = (nb.size() % 2) ? 'b' : 'a'; nb.insert(nb.begin(), make_pair(targ, 0)); } } int aswap = 0, bswap = 0; for (auto x : na) aswap += x.second; for (auto x : nb) bswap += x.second; bool swapped = false; for (int i = (int)na.size() - 1; i >= 0; --i) { ans.emplace_back(bswap, aswap); if (swapped) { swap(aswap, bswap); bswap -= nb[i].second; aswap -= na[i].second; } else { bswap -= nb[i].second; aswap -= na[i].second; swap(aswap, bswap); } swapped = !swapped; } return ans; } vector<pair<int, int> > solve(string s1, string s2) { as.clear(); bs.clear(); string s; s = s1 + 'b'; char now = s[0]; int cnt = 1; for (int i = 1; i < (int)s.size(); ++i) { if (now != s[i]) { bs.emplace_back(now, cnt); now = s[i]; cnt = 0; } ++cnt; } s = s2 + 'a'; now = s[0]; cnt = 1; for (int i = 1; i < (int)s.size(); ++i) { if (now != s[i]) { as.emplace_back(now, cnt); now = s[i]; cnt = 0; } ++cnt; } if ((int)as.size() == 0 && (int)bs.size() == 0) { vector<pair<int, int> > ans; return ans; } int amove, bmove; bmove = (int)bs.size() % 2; amove = (int)as.size() % 2; if (!bmove && (int)bs.size() != 0) bmove += 2; if (!amove && (int)as.size() != 0) amove += 2; while (amove <= (int)as.size() && bmove <= (int)bs.size()) { int bexp = amove + (int)bs.size() - bmove - 1; int aexp = bmove + (int)as.size() - amove - 1; if (abs(bexp - aexp) <= 2) { return solve(bmove, amove); } else if (aexp > bexp) { amove += 2; } else { bmove += 2; } } assert(false); } int main() { string s1, s2; cin >> s1 >> s2; auto ans1 = solve(s1, s2); auto ans2 = solve(s2, s1); if (ans1.size() <= ans2.size()) { printf("%d\n", (int)ans1.size()); for (auto x : ans1) printf("%d %d\n", x.first, x.second); } else { printf("%d\n", (int)ans2.size()); for (auto x : ans2) printf("%d %d\n", x.second, x.first); } }
20
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O2") #pragma GCC optimization("unrolled-loops") using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long base = 1e9 + 7; const long long oo = 6e3 + 1; const long long gh = 1e3 + 3; const long long inf = 1e15 + 5; long long n, m, k, b; long long nextt[oo]; long long res[oo]; long long a[oo]; void input() { cin >> n; } long long sum = 0; const long long maxoo = 1e7 + 8e6 + 6e3 + 6; bool isp[maxoo]; void cre() { long long n = maxoo; long long j; isp[1] = 1; for (long long i = 2; i <= sqrt(n); i++) { if (isp[i] == 0) { j = i * i; while (j <= n) { isp[j] = 1; j += i; } } } } void solve() { cre(); long long t1, t2; if (n == 2) { for (long long i = 1; i <= n; i++) cout << 1 << " "; exit(0); } t1 = n + 1; t2 = n; if (n % 2) t1 /= 2; else t2 /= 2; long long necsum = 0; for (long long i = 1; i <= t1 * t2; i++) { long long j = t1 * t2 - i; if (!isp[i] && !isp[j]) { necsum = i; break; } } if (necsum == 0) { a[3] = 2; for (long long i = 1; i <= t1 * t2 - 3; i++) { long long j = t1 * t2 - 3 - i; if (!isp[i] && !isp[j]) { necsum = i; break; } } } for (long long i = n;; i--) { necsum -= i; if (necsum > 0) a[i] = 1; else { necsum += i; if (necsum != 3) a[necsum] = 1; else a[1] = a[2] = 1; break; } } } void ouput() { for (long long i = 1; i <= n; i++) cout << a[i] + 1 << " "; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (ifstream("test.txt")) { freopen("test.txt", "r", stdin); } else if (ifstream("" ".inp")) { freopen( "" ".inp", "r", stdin); freopen( "" ".out", "w", stdout); } long long t = 1; while (t--) { input(); solve(); ouput(); } }
14
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int N = 1005; long long f[N][N], F[N][N], binom[N][N], fac[N]; void prepro() { f[0][0] = 1; for (int n = (1); n <= (1001); ++n) for (int k = (1); k <= (50); ++k) { for (int i = k; i <= n; i += k) f[n][k] = (f[n - i][k - 1] + f[n][k]) % mod; } binom[0][0] = 1; for (int n = (1); n <= (1001); ++n) for (int k = (0); k <= (n); ++k) { if (k == 0 || k == n) binom[n][k] = 1; else binom[n][k] = (binom[n - 1][k] + binom[n - 1][k - 1]) % mod; } fac[0] = 1; for (int n = (1); n <= (1001); ++n) fac[n] = (fac[n - 1] * n) % mod; F[0][0] = 1; for (int n = (0); n <= (1000); ++n) for (int k = (1); k <= (50); ++k) F[n][k] = ((f[n][k] + f[n][k - 1]) * fac[k]) % mod; } long long BINOM(int a, int b) { if (a < b) return 0; if (b < 0) return 0; return binom[a][b]; } long long jebaj() { int n, k; cin >> n >> k; if (k > 45) return 0; long long wyn = 0; for (int a = (0); a <= ((n + 1) - 1); ++a) { wyn = (wyn + F[a][k] * BINOM(n - a, k)) % mod; } return wyn; } int main() { prepro(); ios_base::sync_with_stdio(0); int t; cin >> t; for (int i = (0); i <= ((t)-1); ++i) { cout << jebaj() << "\n"; } }
15
#include <bits/stdc++.h> using namespace std; int n, m, now, i, j, k, cnt; int l[105], r[105], d[205]; double ans[105], f[105][105]; double len, ll, rr, p1, p2, p3; void calc(int L, int R) { int i, j, k; cnt = 1; for (i = 0; i <= n; ++i) for (j = 0; j <= n; ++j) f[i][j] = 0; f[0][0] = 1; for (i = 1; i <= n; ++i) if (i != now) { if (r[i] <= L) { ++cnt; continue; } if (l[i] >= R) continue; len = r[i] - l[i]; p1 = (L - l[i]) / len; p2 = (R - L) / len; p3 = (r[i] - R) / len; for (j = i; j >= 0; --j) for (k = i - j; k >= 0; --k) { f[j + 1][k] += f[j][k] * p1; f[j][k + 1] += f[j][k] * p2; f[j][k] *= p3; } } for (i = 0; i + cnt <= n; ++i) { for (j = n - i - cnt; j >= 0; --j) ans[i + cnt] += f[i][j] * (R - L) / (j + 1), ans[i + j + 1 + cnt] -= f[i][j] * (R - L) / (j + 1); } } int main() { scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d%d", &l[i], &r[i]); d[++m] = l[i]; d[++m] = r[i]; } sort(d + 1, d + m + 1); for (i = 2, k = 1; i <= m; ++i) if (d[i] != d[i - 1]) { ++k; if (k != i) d[k] = d[i]; } m = k; for (i = 1; i <= n; ++i) { now = i; for (j = 1; j <= n; ++j) ans[j] = 0; for (j = 1; j < m; ++j) if (l[i] <= d[j] && r[i] >= d[j + 1]) calc(d[j], d[j + 1]); for (j = 1; j <= n; ++j) ans[j] += ans[j - 1]; for (j = 1; j <= n; ++j) printf("%.6lf ", ans[j] / (r[i] - l[i])); printf("\n"); } }
22
#include <bits/stdc++.h> using namespace std; int p[311111]; int x[311111]; long long s[311111]; int n; int k; long long mn; int ind; long long now = 0; bool comp(int i, int j) { return x[i] < x[j]; } long long labs(long long a) { if (a < 0) a = -a; } int main() { int i, j; cin >> n; for (i = 1; i <= n; ++i) { cin >> x[i]; p[i] = i; } sort(p + 1, p + n + 1, comp); for (i = 1; i <= n; ++i) { s[i] = s[i - 1] + x[p[i]]; } cin >> k; for (int _i = 1; _i <= k; ++_i) { i = p[_i]; now += 1ll * x[i] * (_i - 1) - s[_i - 1]; } mn = now; ind = k; for (int _i = k + 1; _i <= n; _i++) { i = p[_i]; now -= (s[_i - 1] - s[_i - k]) - 1ll * x[p[_i - k]] * (k - 1); now += 1ll * x[i] * (k - 1) - (s[_i - 1] - s[_i - k]); if (now < mn) { mn = now; ind = _i; } } for (i = ind - k + 1; i <= ind; ++i) cout << p[i] << ' '; return 0; }
12
#include <bits/stdc++.h> using namespace std; int a, b, c, n; int main() { int tmp = 0; for (; cin >> a;) n += a * tmp++; cout << n << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int m, n, a[maxn]; long long s[maxn], p[maxn]; pair<long long, long long> Q[maxn]; double K[maxn]; double getk(pair<long long, long long> A) { return 1.0 * A.second / A.first; } double getk(pair<long long, long long> A, pair<long long, long long> B) { return getk(make_pair(B.first - A.first, B.second - A.second)); } void add(pair<long long, long long> x) { while (m > 1 && getk(Q[m], x) > getk(Q[m - 1], Q[m])) --m; Q[++m] = x; if (m > 1) K[m - 1] = -getk(Q[m - 1], Q[m]); } inline long long getv(pair<long long, long long> l, long long x) { return -l.first * x + l.second; } long long query(long long x) { if (m == 1) return getv(Q[m], x); int r = lower_bound(K + 1, K + m, -x) - K; return getv(Q[r], x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); s[i] = s[i - 1] + a[i]; p[i] = p[i - 1] + 1LL * a[i] * i; } long long ans = 0; for (int i = 1; i <= n; ++i) { add(make_pair(i, (i - 1) * s[i - 1] - p[i - 1])); ans = max(ans, s[i] + p[i] + query(s[i])); } printf("%I64d\n", ans); }
24
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, c; string s, t; bool ff; while (cin >> s) { c = -1; for (i = 0; i < s.size(); i++) { if (s[i] == '<') { ff = true; for (j = i + 1; j < s.size(); j++) { if (s[j] == '>') break; if (s[j] == '/') ff = false; } t = s.substr(i, j - i + 1); i = j; if (ff) c++; for (j = 0; j < c; j++) { cout << " "; } cout << t << endl; if (!ff) c--; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; bool iscons(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == '-' || ch == '?') { return false; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; int n = s.length(); if (n < 3) { cout << s; return 0; } string ansstr = ""; char prev2 = s.at(0); char prev = s.at(1); ansstr += prev2; ansstr += prev; for (int i = 2; i < n; i++) { char curr = s.at(i); if (iscons(prev2) && iscons(prev) && iscons(curr)) { if (prev2 != prev || prev != curr || prev2 != curr) { ansstr += ' '; prev2 = '-'; } } else { } ansstr += curr; if (prev2 != '-') { prev2 = prev; } else { prev2 = '?'; } prev = curr; } cout << ansstr; return 0; }
7
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); long long tc = 1; while (tc--) { long long k, d; cin >> k >> d; if (d == 0) if (k > 1) { cout << "No solution"; continue; } cout << d; for (long long i = 0; i < k - 1; i++) cout << 0; } return 0; }
3
#include <bits/stdc++.h> using namespace std; char str[100005]; int flag[100005]; void print(int len) { for (int i = 0; i < len / 2; i++) { printf("4"); } for (int j = len / 2; j < len; j++) { printf("7"); } printf("\n"); } int main() { scanf("%s", str); int len = strlen(str); memset(flag, 0, sizeof(flag)); if (len % 2) { print(len + 1); } else { if ((str[0] - '0') > 7) { print(len + 2); } else { int cnt = 0, x = 0, y = 0; for (int i = len - 1; i >= 0; i--) { if ((str[i] - '0' + cnt) > 7) { if (i == 0) { print(len + 2); return 0; } cnt = 1; str[i] = '4'; } else if ((str[i] - '0' + cnt) > 4 && (str[i] - '0' + cnt) < 7) { cnt = 0; str[i] = '7'; flag[i + 1] = 1; } else if ((str[i] - '0' + cnt) < 4) { cnt = 0; str[i] = '4'; flag[i + 1] = 1; } else if ((str[i] - '0' + cnt) == 4) { str[i] = '4'; if (cnt) { flag[i + 1] = 1; } cnt = 0; } else { str[i] = '7'; if (cnt) { flag[i + 1] = 1; } cnt = 0; } } int pos = -1; for (int i = 0; i < len; i++) { if (flag[i]) { pos = i; break; } } if (pos != -1) for (int i = pos; i < len; i++) { str[i] = '4'; } for (int i = 0; i < len; i++) { if (str[i] == '4') x++; else y++; } if (x == y) printf("%s\n", str); else if (x > y) { int ans = x - y, flag = 0; for (int i = len - 1; i >= 0; i--) { if (ans == 0) { printf("%s\n", str); break; } else { if (str[i] == '4') { str[i] = '7'; ans -= 2; } } } } else { int ans = y - x; for (int i = len - 1; i >= 0; i--) { int p = 0; if ((str[i] - '0') == 4) { for (int j = 0; j < i; j++) { if (str[j] == '4') p++; } if (((len - 1 - i) + p) >= len / 2) { str[i] = '7'; ans += 2; for (int j = i + 1; j < len; j++) { if (ans == 0) { printf("%s\n", str); return 0; } if (str[j] == '7') { str[j] = '4'; ans -= 2; } } printf("%s\n", str); return 0; } } } print(len + 2); } } } }
10
#include <bits/stdc++.h> using namespace std; int n; int dp[100003]; int ans[100003]; int mark[100003]; void f(int X) { ans[X] = -1; for (int i = 2; (i * (i + 1)) / 2 <= X; i++) if ((X - (i * (i + 1)) / 2) % i == 0) { int bas = (X - (i * (i + 1)) / 2) / i + 1; int st = dp[bas + i - 1] ^ dp[bas - 1]; mark[st] = X; if (!st && ans[X] == -1) ans[X] = i; } for (int i = 0;; i++) { if (mark[i] == X) continue; dp[X] = dp[X - 1] ^ i; return; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) f(i); printf("%d\n", ans[n]); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int X = 30; long long L, R, memo[X][4][2]; int T, len; string s; long long dfs(int cur, int M, int f) { if (M > 3) return 0; if (cur == len) return 1; if (memo[cur][M][f] != -1) return memo[cur][M][f]; int v; if (f) v = s[cur] - '0'; else v = 9; long long ans = 0; for (int i = 0; i <= v; i++) { if (i == 0 && f == 1 && i == v) ans += dfs(cur + 1, M, 1); else if (i == 0) ans += dfs(cur + 1, M, 0); else if (f == 1 && i == v) ans += dfs(cur + 1, M + 1, 1); else ans += dfs(cur + 1, M + 1, 0); } memo[cur][M][f] = ans; return ans; } long long solve(long long x) { s = to_string(x); len = s.length(); memset(memo, -1, sizeof(memo)); return dfs(0, 0, 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> T; while (T-- > 0) { cin >> L >> R; cout << solve(R) - solve(L - 1) << "\n"; } }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int maxSum = 0; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) maxSum++; if (maxSum > k) { int y = 0; for (int i = 0; i < n; i++) { cout << "0 " << y++ << endl; } } else { cout << "no solution"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; struct TR { int sum, l0, r0, nmin; }; int n, q, a[maxn], maxa; set<int> pos[maxn]; int nmax[4 * maxn]; void nmax_xg(int k, int l, int r, int x, int z) { if (l == r) { nmax[k] += z; return; } int t = k << 1, mid = (l + r) >> 1; if (x <= mid) nmax_xg(t, l, mid, x, z); else nmax_xg(t + 1, mid + 1, r, x, z); nmax[k] = max(nmax[t], nmax[t + 1]); } int nmax_cx(int k, int l, int r, int x, int y) { if (x > y) return 0; if (x <= l && r <= y) return nmax[k]; int t = k << 1, mid = (l + r) >> 1, re = 0; if (x <= mid) re = max(re, nmax_cx(t, l, mid, x, y)); if (mid < y) re = max(re, nmax_cx(t + 1, mid + 1, r, x, y)); return re; } TR tr[4 * maxn]; int bz[4 * maxn]; void update(int k, int t) { tr[t].nmin += bz[k], tr[t + 1].nmin += bz[k]; bz[t] += bz[k], bz[t + 1] += bz[k]; bz[k] = 0; } TR merge(const TR &a, const TR &b) { if (a.nmin < b.nmin) return a; else if (a.nmin > b.nmin) return b; else return (TR){a.sum + b.sum + nmax_cx(1, 1, n, a.r0 + 1, b.l0), a.l0, b.r0, a.nmin}; } void tr_js(int k, int l, int r) { if (l == r) { tr[k].l0 = tr[k].r0 = l; return; } int t = k << 1, mid = (l + r) >> 1; tr_js(t, l, mid), tr_js(t + 1, mid + 1, r); tr[k] = merge(tr[t], tr[t + 1]); } void tr_xg_sing(int k, int l, int r, int x, int z) { if (l == r) { nmax_xg(1, 1, n, x, z); return; } int t = k << 1, mid = (l + r) >> 1; update(k, t); if (x <= mid) tr_xg_sing(t, l, mid, x, z); else tr_xg_sing(t + 1, mid + 1, r, x, z); tr[k] = merge(tr[t], tr[t + 1]); } void tr_xg_q(int k, int l, int r, int x, int y, int z) { if (x > y) return; if (x <= l && r <= y) { tr[k].nmin += z; bz[k] += z; return; } int t = k << 1, mid = (l + r) >> 1; update(k, t); if (x <= mid) tr_xg_q(t, l, mid, x, y, z); if (mid < y) tr_xg_q(t + 1, mid + 1, r, x, y, z); tr[k] = merge(tr[t], tr[t + 1]); } void modify(int x, int sig) { if (pos[x].empty()) return; tr_xg_sing(1, 0, n, *pos[x].begin(), sig * pos[x].size()); set<int>::iterator it = pos[x].end(); it--; tr_xg_q(1, 0, n, *pos[x].begin(), *it - 1, sig); } void calc_ans() { int ans = n - tr[1].sum - nmax_cx(1, 1, n, 1, tr[1].l0) - nmax_cx(1, 1, n, tr[1].r0 + 1, n); printf("%d\n", ans); } int main() { scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); maxa = max(maxa, a[i]); pos[a[i]].insert(i); } tr_js(1, 0, n); for (int i = 1; i <= maxa; i++) modify(i, 1); calc_ans(); while (q--) { int x, y; scanf("%d %d", &x, &y); modify(a[x], -1); pos[a[x]].erase(x); modify(a[x], 1); modify(y, -1); pos[y].insert(x); modify(y, 1); a[x] = y; calc_ans(); } }
24
#include <bits/stdc++.h> using namespace std; int main() { long long l, r, i; while (~scanf("%I64d%I64d", &l, &r)) { if ((r - l + 1) % 2 == 0) { printf("YES\n"); for (i = l; i <= r; i += 2) printf("%I64d %I64d\n", i, i + 1); } else printf("NO\n"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { char s[25]; char t[25]; scanf("%s", s); scanf("%s", t); int len = strlen(s); int res = 0; for (int i = 0; i < len; i += 2) { if (s[i] == '[') { if (t[i] == '(') { res++; } else if (t[i] == '8') { res--; } } else if (s[i] == '(') { if (t[i] == '[') { res--; } else if (t[i] == '8') { res++; } } else if (s[i] == '8') { if (t[i] == '(') { res--; } else if (t[i] == '[') { res++; } } } if (res > 0) { puts("TEAM 1 WINS"); } else if (res < 0) { puts("TEAM 2 WINS"); } else { puts("TIE"); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a; cin >> b; string s; cin >> s; vector<int> v(a); for (int i = 0; i < a; i++) { v[i] = int(s[i]) - '0'; } if (a == 1 && b == 1) { cout << 0; return 0; } if (v[0] != 1 && b != 0) { v[0] = 1; b--; } int t = 1; while (b > 0 && t < a) { if (v[t] == 0) { t++; continue; } else { v[t] = 0; b--; t++; } } for (int i = 0; i < a; i++) { cout << v[i]; } }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; const ll MAX_N = 1e5 + 1; ll MOD = 1e9 + 7; const ll INF = 1e9; template <typename T> void print(T &&t) { cout << t << "\n"; } template <typename T, typename... Args> void print(T &&t, Args &&...args) { cout << t << " "; print(forward<Args>(args)...); } int GCD(int a, int b) { while (b) { a %= b; swap(a, b); } return a; } int GCD_extended(int a, int b, int &x, int &y) { x = 1, y = 0; int x1 = 0, y1 = 1, a1 = a, b1 = b; while (b1) { int q = a1 / b1; tie(x, x1) = make_tuple(x1, x - q * x1); tie(y, y1) = make_tuple(y1, y - q * y1); tie(a1, b1) = make_tuple(b1, a1 - q * b1); } return a1; } int LCM(int a, int b) { return (a * b) / GCD(a, b); } ll modpow(ll x, int n, ll m) { ll res = 1; while (n > 0) { if (n & 1) res = (res * x) % m; x = (x * x) % m; n >>= 1; } return res; } ll modinv(ll x, ll m = MOD) { return modpow(x, m - 2, m); } ll cei(ll a, ll b) { ll val = (a / b) + ((a % b) != 0); return val; } void solve() { ll n; cin >> n; ll ans = modpow(2, n, MOD - 1); ans = (ans - 2 + MOD - 1) % (MOD - 1); ans = modpow(4, ans, MOD); ans = (ans * 6) % MOD; cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll tc = 1; for (ll t = 1; t <= tc; t++) { solve(); } }
5
#include <bits/stdc++.h> #define mod (int)(1e9+7) using namespace std; int n, t, q, x, i, j; long long sum = 1; int b[110], c[110], sp[110], f[10204], mp[1200000], sc[110], s[10204]; int main() { cin >> n; for (i = 1; i <= n; i ++ ) cin >> c[i], sum = 1ll * sum * (c[i] + 1) % mod, sc[i] = sc[i - 1] + c[i]; for (int i = 1; i < n; i ++ ) cin >> b[i], sp[i + 1] = sp[i] + b[i]; for (int i = 2; i <= n; i ++ ) sp[i] += sp[i - 1]; cin >> q; memset(mp, -1, sizeof mp); while (q -- ) { cin >> x; if (mp[x + 500000] != -1) { cout << mp[x + 500000] << endl; continue; } for (i = 1; i <= n; i ++ ) if (x * i + sp[i] > 0) break; if (i > n) { cout << sum << endl; continue; } for (i = 1; i <= n; i ++ ) if (x * i + sp[i] > sc[i]) break; if (i <= n) { puts("0"); continue; } int l = 0; memset(f, 0, sizeof f); s[0] = f[0] = 1; for (i = 1; i <= n; i ++ ) { int L = l; l = max(l, x * i + sp[i]); for (j = sc[i - 1] + 1; j <= sc[i]; j ++ ) s[j] = s[j - 1]; for (j = sc[i]; j >= l; j -- ) f[j] = (s[j] - (j - c[i] - 1 >= L ? s[j - c[i] - 1] : 0)) % mod; if (l) s[l - 1] = 0; for (j = l; j <= sc[i]; j ++ ) s[j] = ((j ? s[j - 1] : 0) + f[j]) % mod; } mp[x + 500000] = (s[sc[n]] % mod + mod) % mod; cout << mp[x + 500000] << endl; } return 0; }
21
#include <bits/stdc++.h> const int maxn = 200005; using namespace std; int l[maxn + 5], r[maxn + 5]; int main() { int id, lnum = 0, rnum = 0, num = 0, q; char c; scanf("%d", &q); memset(l, -1, sizeof(l)); memset(r, -1, sizeof(r)); for (int i = 0; i < q; i++) { getchar(); num++; scanf("%c %d", &c, &id); if (c == 'L') l[id] = ++lnum; else if (c == 'R') r[id] = ++rnum; else { num--; if (l[id] == -1) { int use = rnum - r[id]; printf("%d\n", min(use, num - use - 1)); } else { int use = lnum - l[id]; printf("%d\n", min(use, num - use - 1)); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; class Edge { public: int to, next; long long cost; Edge(int t, int n, long long c) : to(t), next(n), cost(c) {} Edge() : to(-1), next(-1), cost(0) {} }; void add_edge(int u, int v, long long w); void add_edge_DAG(int u, int v, long long w); long long get_limit(long long w); long long get_sum(long long w); void get_scc(); void tarjan(int u, stack<int> &s); long long solve(int s); const long long NAGINF = 0xbfbfbfbf; const int MAXN = 1e6 + 5, MAXM = 1e6 + 5; Edge edges[MAXM]; int head[MAXN]; int esize; Edge edges_DAG[MAXM]; int head_DAG[MAXN]; int esize_DAG; int dfn[MAXN], low[MAXN], belong[MAXN], dfcnt, scc; int degree[2][MAXN]; long long sum[MAXN], dist[MAXN]; int n; int main() { int m, s; while (scanf("%d %d", &n, &m) != EOF) { memset(head, -1, sizeof(head)); esize = 0; memset(dfn, -1, sizeof(dfn)); memset(low, -1, sizeof(low)); memset(belong, -1, sizeof(belong)); dfcnt = 0; memset(head_DAG, -1, sizeof(head_DAG)); esize_DAG = 0; memset(degree, 0, sizeof(degree)); memset(sum, 0, sizeof(sum)); int x, y; long long w; while (m--) { scanf("%d %d %lld", &x, &y, &w); add_edge(x, y, w); } scanf("%d", &s); get_scc(); for (int u = 1; u <= n; u++) for (int i = head[u]; i != -1; i = edges[i].next) { int v = edges[i].to; if (belong[u] == belong[v]) { sum[belong[u]] += get_limit(edges[i].cost); } } for (int u = 1; u <= n; u++) for (int i = head[u]; i != -1; i = edges[i].next) { int v = edges[i].to; if (belong[u] != belong[v]) { add_edge_DAG(belong[u], belong[v], edges[i].cost + sum[belong[v]]); } } long long res = solve(belong[s]); printf("%lld\n", res); } return 0; } void add_edge(int u, int v, long long w) { edges[esize].to = v; edges[esize].next = head[u]; edges[esize].cost = w; head[u] = esize; esize++; } void add_edge_DAG(int u, int v, long long w) { edges_DAG[esize_DAG].to = v; edges_DAG[esize_DAG].next = head_DAG[u]; edges_DAG[esize_DAG].cost = w; head_DAG[u] = esize_DAG; esize_DAG++; } long long get_limit(long long cost) { long long left = 1, right = 1e5; long long ans = 0; while (left <= right) { long long mid = (left + right) / 2; long long sum = (1 + mid) * mid / 2; if (sum < cost) { ans = mid; left = mid + 1; } else { right = mid - 1; } } return (cost + ans * cost - get_sum(ans)); } inline long long get_sum(long long n) { long long sum = n * (n + 1) * (n + 2) / 6; return sum; } void get_scc() { scc = 0; stack<int> s; for (int i = 1; i <= n; i++) { if (dfn[i] == -1) { tarjan(i, s); } } for (int u = 1; u <= n; u++) for (int i = head[u]; i != -1; i = edges[i].next) { int v = edges[i].to; if (belong[u] != belong[v]) { degree[0][belong[v]]++; degree[1][belong[u]]++; } } } void tarjan(int u, stack<int> &s) { dfn[u] = low[u] = ++dfcnt; s.push(u); for (int i = head[u]; i != -1; i = edges[i].next) { int v = edges[i].to; if (dfn[v] == -1) { tarjan(v, s); low[u] = min(low[u], low[v]); } else if (belong[v] == -1) { low[u] = min(low[u], dfn[v]); } } if (dfn[u] == low[u]) { scc++; while (!s.empty()) { int v = s.top(); s.pop(); belong[v] = scc; if (u == v) break; } } } long long solve(int s) { memset(dist, NAGINF, sizeof(dist)); dist[s] = sum[s]; queue<int> q; for (int i = 1; i <= scc; i++) { if (degree[0][i] == 0) q.push(i); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head_DAG[u]; i != -1; i = edges_DAG[i].next) { int v = edges_DAG[i].to; if (dist[u] != NAGINF) { dist[v] = max(dist[v], dist[u] + edges_DAG[i].cost); } if (--degree[0][v] == 0) q.push(v); } } long long res = 0; for (int i = 1; i <= scc; i++) { res = max(res, dist[i]); } return res; }
13
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 7; const int N = 1e8 + 7; const int INF = 1000000000000000001; const int MOD = 1000000009; const double eps = 1e-2; long long po(long long x, long long y) { if (y == 0) { return 1; } long long sq = po(x, y / 2); sq *= sq; sq = sq; if (y % 2 != 0) { sq *= x; sq = sq; } return sq; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); priority_queue<int> pq; priority_queue<int> pq1; int n, m, x, sumB = 0, sums = 0; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x; pq.push(x); pq1.push(-x); } for (int i = 0; i < n; i++) { sumB += pq.top(); pq.push(pq.top() - 1); pq.pop(); } for (int i = 0; i < n; i++) { sums -= pq1.top(); int t = pq1.top() + 1; pq1.pop(); pq1.push(t); if (pq1.top() == 0) { pq1.pop(); } } cout << sumB << ' ' << sums << endl; }
3
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, pair<int, int> > > > ad; vector<vector<int> > fre(40); int v[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; int sz[12] = {6, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2}; long long int pro(long long int a, long long int b, long long int M) { if (b == 0 || a == 0) return 0; long long int d = a * b; if (d % a == 0 && d % b == 0 && d > 0) return d % M; else if (b % 2 == 0) return pro((a + a) % M, b / 2, M); else if (b % 2 == 1) return (a + pro((a + a) % M, b / 2, M)) % M; } long long int bit(long long int a, long long int b, long long int M) { long long int x = 1; a %= M; while (b > 0) { if (b & 1) x = pro(x, a, M); b = b >> 1; a = pro(a, a, M); } return x; } long long int join(long long int r1, long long int r2, long long int a1, long long int a2, long long int p1, long long int p2) { long long int b1 = bit(r2, p1 - 1, r1); long long int b2 = bit(r1, p2 - 1, r2); long long int y = pro(pro(a1, b1, r1 * r2), r2, r1 * r2) + pro(pro(a2, b2, r1 * r2), r1, r1 * r2); return y % (r1 * r2); } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int ans(int k) { int n, m = 0, l, i = 0, j, s = ad[k].size(), z; long long int lcm, mod, phi, a, b, g, p, q, r, y; while (i < s) { j = i; while (j < s - 1 && ad[k][j + 1].first - ad[k][j].first == 1) j++; for (z = 0; z < 12; z++) { for (n = 1; n < sz[z]; n++) fre[v[z]][n] = 0; } l = 0, n = 0; lcm = 1, mod = 0, phi = 1; while (i <= j) { a = ad[k][i].second.second, r = ad[k][i].second.first; g = gcd(lcm, a); if (mod % g == r % g) { b = a, q = a, l++; for (z = 0; z < 12; z++) { if (b % v[z] == 0) { p = 0, q -= (q / v[z]); while (b % v[z] == 0) p++, b /= v[z]; fre[v[z]][p]++; } } y = join(lcm / g, a / g, (mod - mod % g) / g, (r - r % g) / g, phi, q), b = a; for (z = 0; z < 12; z++) { if (b % v[z] == 0) { p = 0, q = 1; while (b % v[z] == 0) p++, q *= v[z], b /= v[z]; if (lcm % q > 0) { if (lcm % v[z] > 0) q /= v[z], lcm *= v[z], phi *= (v[z] - 1); else if (lcm % v[z] == 0) q /= gcd(lcm, q); while (q > 1) q /= v[z], lcm *= v[z], phi *= v[z]; } } } mod = r % g + y * g, mod %= lcm, n = max(n, l), i++; } else { a = ad[k][i - l].second.second, b = a, l--; for (z = 0; z < 12; z++) { if (b % v[z] == 0) { p = 0, q = v[z]; while (b % v[z] == 0) p++, b /= v[z], q *= v[z]; fre[v[z]][p]--; if (fre[v[z]][p] == 0 && lcm % q > 0) { r = 1; while (fre[v[z]][p] == 0) r *= v[z], p--, lcm /= v[z]; if (p > 0) phi /= r; else if (p == 0) phi /= (r - (r / v[z])); } } } mod %= lcm; } } i = j + 1, m = max(m, n); } return m; } int main() { int n, m, i, j, k, x, s; scanf("%d", &n), scanf("%d", &m); ad.resize(m + 1); for (i = 0; i < 12; i++) fre[v[i]].resize(sz[i], 0); for (i = 0; i < 12; i++) fre[v[i]][0] = 1; for (i = 0; i < n; i++) { scanf("%d", &k); for (j = 0; j < k; j++) scanf("%d", &x), ad[x].push_back(make_pair(i, make_pair(j, k))); } for (i = 1; i <= m; i++) printf("%d\n", ans(i)); }
20
#include <bits/stdc++.h> int cuberoot(int x) { int fr = 0, la = 1001, mid, p; while (la - fr > 0) { mid = (fr + la) / 2; p = mid * mid * mid; if (p == x) return mid; else if (p > x) la = mid; else fr = mid + 1; } if (fr * fr * fr == x) return fr; else return -1; } int main() { int n, a, b, p, q, r, i, t; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &a, &b); p = a; q = b; r = p % q; while (r > 0) { p = q; q = r; r = p % q; } a = a / q; b = b / q; if (q % a != 0) { printf("No\n"); } else { q = q / a; if (q % b != 0) { printf("No\n"); } else { q = q / b; if (cuberoot(q) == -1) { printf("No\n"); } else printf("Yes\n"); } } } }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; int main() { int t; cin >> t; while (t--) { ll n, x, y; cin >> n >> x >> y; ll sum = x + y; if (x > y) swap(x, y); ll mx = min(n, x + y - 1); ll mn = max(1LL, min(n, x + y - n + 1)); cout << mn << " " << mx << endl; } }
9
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); const int N = 1e4 + 7; int MOD; bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return (x != 1); } int dp[N]; vector<pair<int, int> > e[N]; void add(int x) { for (int i = N - x; i >= 0; i--) { dp[i + x] += dp[i]; if (dp[i + x] >= MOD) dp[i + x] -= MOD; } } void del(int x) { for (int i = x; i < N; i++) { dp[i] -= dp[i - x]; if (dp[i] < 0) dp[i] += MOD; } } int main() { ios::sync_with_stdio(0); cin.tie(0); MOD = 5e8 + rnd() % (int)5e8; while (!prime(MOD)) MOD--; int n, q; cin >> n >> q; dp[0] = 1; vector<bool> ans(n + 1); for (int i = 0; i < q; i++) { int l, r, x; cin >> l >> r >> x; l--, r--; e[l].push_back({x, 1}); e[r + 1].push_back({x, -1}); } for (int i = 0; i < n; i++) { for (auto c : e[i]) { if (c.second == 1) { add(c.first); } else { del(c.first); } } for (int j = 1; j <= n; j++) { if (dp[j]) { ans[j] = true; } } } vector<int> res; for (int i = 1; i <= n; i++) { if (ans[i]) { res.push_back(i); } } cout << res.size() << '\n'; for (int x : res) { cout << x << ' '; } cout << '\n'; }
14
#include <bits/stdc++.h> using namespace std; const int maxn = 101111; struct node { int u, v, w; }; vector<node> out; int n, m, dis[maxn], val[maxn], pre[maxn], cant[maxn]; bool vis[maxn]; vector<pair<int, int> > G[maxn], tmp; map<pair<int, int>, int> mp; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; G[x].push_back(make_pair(y, z)); G[y].push_back(make_pair(x, z)); if (x > y) swap(x, y); mp[make_pair(x, y)] = z; } memset(dis, 0x3f3f3f3f, sizeof dis); queue<int> q; while (!q.empty()) q.pop(); q.push(1); dis[1] = 0; vis[1] = true; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].first, w = G[u][i].second; if (dis[v] > dis[u] + 1) { pre[v] = u; dis[v] = dis[u] + 1; val[v] = val[u] + w; if (!vis[v]) { vis[v] = true; q.push(v); } } else if (dis[v] == dis[u] + 1) { if (val[v] < val[u] + w) { val[v] = val[u] + w; pre[v] = u; } } } } int u = n; while (u != 1) { int aa = u, bb = pre[u]; if (aa > bb) swap(aa, bb); if (mp[make_pair(aa, bb)] == 0) { if (u < pre[u]) out.push_back((node){u, pre[u], 1}); else out.push_back((node){pre[u], u, 1}); } cant[u] = pre[u]; u = pre[u]; } for (int i = 1; i <= n; i++) { for (int j = 0; j < G[i].size(); j++) { int v = G[i][j].first; if (cant[i] != v && cant[v] != i) { if (G[i][j].second == 1) { if (i < v) tmp.push_back(make_pair(i, v)); else tmp.push_back(make_pair(v, i)); } } } } sort(tmp.begin(), tmp.end()); for (int i = 0; i < tmp.size(); i++) { int x = tmp[i].first, y = tmp[i].second; if (i == 0) out.push_back((node){x, y, 0}); else { if (x != tmp[i - 1].first || y != tmp[i - 1].second) { out.push_back((node){x, y, 0}); } } } cout << out.size() << endl; for (int i = 0; i < out.size(); i++) { cout << out[i].u << " " << out[i].v << " " << out[i].w << endl; } return 0; }
13
#include <bits/stdc++.h> using namespace std; vector<int> adj[105]; int n, k; struct State { int node, disUp, disDown; bool operator<(const State &other) const { if (node != other.node) return node < other.node; if (disUp != other.disUp) return disUp < other.disUp; return disDown < other.disDown; }; }; map<State, long long> cache; long long rec(int node, int parent, int disUp, int disDown) { if (disUp > k && disDown > k) return 0; State s; s.node = node; s.disUp = disUp; s.disDown = disDown; if (cache.count(s)) return cache[s]; long long waysSmaller = 1, waysLarger = 1; int newDisUp = min(disUp + 1, disDown + 1); if (newDisUp > k + 1) newDisUp = k + 1; bool any = 0; for (auto to : adj[node]) { if (to == parent) continue; any = 1; long long sum = 0; for (int i = (disDown); i < int(2 * k + 4); ++i) { sum += rec(to, node, newDisUp, i); } sum %= 1000000007; waysSmaller *= sum; waysSmaller %= 1000000007; if (disDown) { sum += rec(to, node, newDisUp, disDown - 1); } else { waysSmaller = 0; } sum %= 1000000007; waysLarger *= sum; waysLarger %= 1000000007; } if (!any) return (disDown == 0 || disDown == k + 1); long long ret = waysLarger - waysSmaller; ret %= 1000000007; if (ret < 0) ret += 1000000007; cache[s] = ret; return ret; } int main() { scanf("%d%d", &n, &k); for (int i = (0); i < int(n - 1); ++i) { int u, v; scanf("%d%d", &u, &v); --u; --v; adj[u].push_back(v); adj[v].push_back(u); } long long ans = 0; for (int disDown = (0); disDown < int(k + 1); ++disDown) { ans += rec(0, -1, k + 1, disDown); ans %= 1000000007; } cout << ans << endl; }
17
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 1; int dp[N][N]; int acc[N]; int ht[N]; int nxt[N][N]; int n; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> acc[i + 1]; acc[i + 1] += acc[i]; } for (int i = 0; i < n; ++i) { int curr = i; for (int j = i; j >= 0; --j) { while (curr <= n && 2 * acc[i + 1] - acc[j] > acc[curr]) ++curr; nxt[i][j] = curr - 1; } } for (int i = n - 2; i >= 0; --i) for (int j = i; j >= 0; --j) { int &res = dp[i][j]; res = 1 + dp[i + 1][j]; if (nxt[i][j] < n) res = min(res, nxt[i][j] - i - 1 + dp[nxt[i][j]][i + 1]); } cout << dp[0][0] << endl; }
13
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000; bool used[1100]; string S, St; int n; map<char, int> M; vector<int> prime(int n) { vector<int> Ans; vector<bool> p; p.assign(n + 1, true); p[1] = false; for (int i = 2; i <= n; ++i) { if (p[i]) for (int j = i + i; j <= n; j += i) p[j] = false; } for (int i = 0; i <= n; ++i) if (p[i]) Ans.push_back(i); return Ans; } void load() { cin >> S; St = S; n = S.size(); } void algo() { vector<int> P = prime(n), Pos; int c = 0; for (size_t i = 1; i < P.size(); ++i) { if (P[i] * 2 <= n) { for (int j = P[i], d = 1; j <= n; j = P[i] * d, d++) if (!used[j]) Pos.push_back(j), used[j] = true, c++; } } for (size_t i = 0; i < S.size(); ++i) M[S[i]]++; string A; A.assign(n, '#'); bool yes = false; for (int i = 'a'; i <= 'z'; ++i) { if (M[i] >= c) { yes = true; for (size_t j = 0; j < Pos.size(); ++j) A[Pos[j] - 1] = char(i), M[i]--; break; } } if (!yes) { cout << "NO" << endl; exit(0); } int k = 0; for (int i = 'a'; i <= 'z'; ++i) { while (M[i] > 0) { while (A[k] != '#') k++; A[k] = char(i), M[i]--; } } cout << "YES" << endl; cout << A << endl; } void write() {} int main() { load(); algo(); write(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[100010], b[100010]; int main() { int n, sum, k; while (~scanf("%d%d", &n, &k)) { sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); for (int i = 0; i < n && k; i++) { for (int j = n - 1 - i; j >= 0 && k; j--) { cout << i + 1 << " "; for (int q = n - 1; q > n - 1 - i; q--) { cout << a[q] << " "; } cout << a[j]; cout << endl; k--; } } } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long int ans(int num) { long long int sum = 0; for (int i = 0; i <= 5; i++) { if ((num >> i) % 2 == 0) { sum++; } } return sum; } long long int power(long long int sum) { long long int ans = 1, res = 3; while (sum) { if (sum % 2) ans *= res; res = ((res % 1000000007) * (res % 1000000007)) % 1000000007; sum >>= 1; ans %= 1000000007; } return ans; } int main() { string s; cin >> s; long long int sum = 0; int num = 0; for (int i = 0; i < s.length(); i++) { if (s[i] >= '0' && s[i] <= '9') num = s[i] - '0'; if (s[i] >= 'A' && s[i] <= 'Z') num = s[i] - 'A' + 10; if (s[i] >= 'a' && s[i] <= 'z') num = s[i] - 'a' + 36; if (s[i] == '_') num = 63; if (s[i] == '-') num = 62; sum += ans(num); } sum = power(sum); cout << sum; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n; vector<int> note; vector<pair<int, int> > maxNote; vector<pair<int, int> > minNote; bool Work() { maxNote.resize(n); minNote.resize(n); maxNote[0] = make_pair(1, 1); minNote[0] = make_pair(1, 1); if (note[0] > 1) return false; for (int i = 1; i < n; ++i) { if (maxNote[i - 1].second < 2) maxNote[i] = make_pair(maxNote[i - 1].first, maxNote[i - 1].second + 1); else maxNote[i] = make_pair(maxNote[i - 1].first + 1, 1); if (minNote[i - 1].second < 5) minNote[i] = make_pair(minNote[i - 1].first, minNote[i - 1].second + 1); else minNote[i] = make_pair(minNote[i - 1].first + 1, 1); if (note[i]) { if (note[i] > maxNote[i].first || note[i] < minNote[i].first) return false; if (note[i] < maxNote[i].first) maxNote[i] = make_pair(note[i], 5); if (note[i] > minNote[i].first) minNote[i] = make_pair(note[i], 1); } } if (maxNote[n - 1].second == 1) maxNote[n - 1] = maxNote[n - 2]; if (note[n - 1] > maxNote[n - 1].first) return false; return true; } int main() { while (cin >> n) { note.resize(n); for (int i = 0; i < n; ++i) cin >> note[i]; bool ans = Work(); if (!ans) { cout << -1 << endl; continue; } note[n - 1] = maxNote[n - 1].first; int len = 1; for (int i = n - 2; i >= 0; --i) { if (maxNote[i].first >= note[i + 1]) { note[i] = note[i + 1]; ++len; if (len > 5) { --note[i]; len = 1; } } else { note[i] = maxNote[i].first; len = 1; } } cout << note[n - 1] << endl; for (int i = 0; i < n; ++i) cout << note[i] << " "; cout << endl; } return 0; }
17
#include <bits/stdc++.h> using namespace std; const long long Maxn = 1e6 + 10; const long long Mod = 1e9 + 7; const long long Max = 1e3 + 1; const long long Inf = 1e9 + 1; char s[Max] = {0}, t[Maxn] = {0}; int main() { cin >> s >> t; int end = -1, start = -1, p = 0; int sl = strlen(s); int tl = strlen(t); for (int i = 0; i < tl; i++) { if (t[i] == s[p]) p++; if (p == sl) { end = i; break; } } p = sl - 1; for (int i = tl - 1; i >= 0; i--) { if (t[i] == s[p]) p--; if (p == -1) { start = i; break; } } if (start != -1 && end != -1) cout << (max(start - end, 0)); else cout << 0; return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long n, m, k, t; long long dx[] = {-1, 0, 0, 1}; long long dy[] = {0, -1, 1, 0}; long long f[1005][1005]; bool vis[1005][1005]; long long dis[1005][1005]; bool check(long long i, long long j) { for (long long d = 0; d <= 3; d++) { long long x = i + dx[d], y = j + dy[d]; if (f[i][j] == f[x][y]) return 1; } return 0; } void multisource_bfs() { queue<pair<long long, long long>> q; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= m; j++) { if (check(i, j)) { q.push({i, j}); vis[i][j] = 1; } } while (!q.empty()) { pair<long long, long long> fr = q.front(); q.pop(); for (long long i = 0; i <= 3; i++) { long long x = fr.first + dx[i], y = fr.second + dy[i]; if (x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y]) { dis[x][y] = 1 + dis[fr.first][fr.second]; vis[x][y] = 1; q.push({x, y}); } } } } int32_t main() { std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); ; cin >> n >> m >> t; memset(f, -1, sizeof(f)); for (long long i = 1; i <= n; i++) { string s; cin >> s; for (long long j = 0; j < m; j++) f[i][j + 1] = (s[j] == '0') ? 0 : 1; } multisource_bfs(); while (t--) { long long i, j, p; cin >> i >> j >> p; long long itr = dis[i][j]; if (vis[i][j]) cout << (f[i][j] ^ (max(0ll, p - itr) & 1)) << endl; else cout << f[i][j] << endl; } }
12
#include <bits/stdc++.h> using namespace std; int main() { int n, plus, minus, A, B, C, D, E; string x, name; cin >> n; int array[n]; int sum, largest = -100000; for (int i = 0; i < n; i++) { cin >> x >> plus >> minus >> A >> B >> C >> D >> E; sum = (plus * 100) - (minus * 50) + A + B + C + D + E; if (sum > largest) { largest = sum; name = x; } } cout << name << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long r, g, b, ans, minn, maxn; scanf("%I64d%I64d", &r, &g); scanf("%I64d", &b); minn = 2e9 + 5; maxn = max(r, g); maxn = max(maxn, b); int flag = 0; if (r == maxn) { if (maxn >= 2 * (g + b)) { flag = 1; ans = g + b; } } if (g == maxn) { if (maxn >= 2 * (b + r)) { flag = 1; ans = b + r; } } if (b == maxn) { if (maxn >= 2 * (g + r)) { flag = 1; ans = g + r; } } if (flag != 1) ans = (r + g + b) / 3; printf("%I64d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 100 + 10; int main() { int n; cin >> n; vector<string> dict(n); for (int i = 0; i < n; i++) cin >> dict[i]; string s; bool mark[N] = {0}; cin >> s; for (int i = 0; i < (int)(s).size(); i++) { for (int j = 0; j < (int)(dict).size(); j++) { bool found = true; for (int k = 0; k < (int)(dict[j]).size() && found; k++) if (s[i + k] != dict[j][k] && abs(s[i + k] - dict[j][k]) != 'a' - 'A') found = false; if (found) for (int k = 0; k < (int)(dict[j]).size(); k++) mark[i + k] = true; } } char c; cin >> c; for (int i = 0; i < (int)(s).size(); i++) { if (!mark[i]) continue; if (s[i] >= 'a') { if (s[i] == c && s[i] == 'a') s[i] = 'b'; else if (s[i] == c) s[i] = 'a'; else s[i] = c; } else { if (s[i] == c - ('a' - 'A') && s[i] == 'A') s[i] = 'B'; else if (s[i] == c - ('a' - 'A')) s[i] = 'A'; else s[i] = c - ('a' - 'A'); } } cout << s; return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const long long mod = 998244353; const double eps = 1e-6; const double PI = acos(-1); const double R = 0.57721566490153286060651209; inline long long read() { long long res = 0; bool zf = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') zf = 1; else res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + c - '0'; if (zf) return -res; return res; } const int N = 1e5 + 10; vector<pair<int, int> > a(N); vector<int> g[N]; int siz[N], son[N], dep[N], fa[N]; void dfs1(int u, int par) { dep[u] = dep[fa[u] = par] + (siz[u] = 1); for (auto v : g[u]) { if (v == par) continue; dfs1(v, u); siz[u] += siz[v]; if (!son[u] || siz[v] > siz[son[u]]) son[u] = v; } } int top[N], dfn[N], nodeof[N], tim; void dfs2(int u, int topf) { nodeof[dfn[u] = ++tim] = u; top[u] = topf; if (!son[u]) return; dfs2(son[u], topf); for (auto v : g[u]) { if (v == fa[u] || v == son[u]) continue; dfs2(v, v); } } struct Tree { int l, r; int sum, tag; } t[N << 2]; void push_up(int u) { t[u].sum = t[u << 1].sum + t[u << 1 | 1].sum; } void push_down(int u) { if (t[u].tag == -1) return; t[u << 1].sum = (t[u << 1].r - t[u << 1].l + 1) * t[u].tag; t[u << 1 | 1].sum = (t[u << 1 | 1].r - t[u << 1 | 1].l + 1) * t[u].tag; t[u << 1].tag = t[u].tag; t[u << 1 | 1].tag = t[u].tag; t[u].tag = -1; } void build(int u, int l, int r) { t[u].l = l; t[u].r = r; t[u].tag = -1; if (l == r) { t[u].sum = 1; return; } int m = l + r >> 1; build(u << 1, l, m); build(u << 1 | 1, m + 1, r); push_up(u); } void modify(int u, int p, int z) { if (z && t[u].sum == t[u].r - t[u].l + 1) return; if (t[u].l == t[u].r) { t[u].sum = z; return; } if (p <= (t[u].l + t[u].r) / 2) modify(u << 1, p, z); else modify(u << 1 | 1, p, z); push_up(u); } long long ans; bool query(int u, int ql, int qr) { if (ql <= t[u].l && t[u].r <= qr) { if (t[u].sum == t[u].r - t[u].l + 1) { ans += t[u].sum; return 1; } return 0; } if (ql <= (t[u].l + t[u].r) / 2) if (!query(u << 1, ql, qr)) return 0; if (qr > (t[u].l + t[u].r) / 2) if (!query(u << 1 | 1, ql, qr)) return 0; return 1; } void modify_chain(int x, int z) { int u = a[x].first, v = a[x].second; if (v == fa[u]) modify(1, dfn[u], z); else modify(1, dfn[v], z); } int query_chain(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); if (!query(1, dfn[top[x]], dfn[x])) return 0; x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (dfn[x] + 1 <= dfn[y]) { if (!query(1, dfn[x] + 1, dfn[y])) return 0; } return 1; } void solve() { int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; a[i] = pair<int, int>{u, v}; g[u].push_back(v); g[v].push_back(u); } dfs1(1, 0); dfs2(1, 1); build(1, 1, n); int m; cin >> m; for (int i = 1; i <= m; i++) { int opt; cin >> opt; if (opt == 1) { int x; cin >> x; modify_chain(x, 1); } else if (opt == 2) { int x; cin >> x; modify_chain(x, 0); } else if (opt == 3) { int u, v; cin >> u >> v; ans = 0; if (query_chain(u, v)) cout << ans << "\n"; else cout << -1 << "\n"; } } } signed main() { ios_base::sync_with_stdio(false); solve(); return 0; }
13
#include <bits/stdc++.h> using namespace std; const int Mod = 777777777; const long long Mod2 = (long long)Mod * Mod; int n, k; inline int add(int a, int b) { a += b; if (a >= Mod) return a - Mod; return a; } inline int mul(int a, int b) { return (int)(((long long)a * b) % Mod); } int choose[1005][1005]; const int MAX = 300; int memo[MAX][MAX][MAX]; int dp(int left, int big, int pos) { if (big == 0) return left == 0 && pos == 0; if (memo[left][big][pos] != -1) return memo[left][big][pos]; long long rr = 0; for (int i = (int)(0); i < (int)(pos + 1); ++i) if (i != big) { rr += (long long)choose[pos][i] * dp(left, big - 1, pos - i); if (rr >= Mod2) rr -= Mod2; } if (left >= big) rr += (long long)choose[left][big] * dp(left - big, big - 1, pos); return memo[left][big][pos] = (int)(rr % Mod); } int main() { ios_base::sync_with_stdio(false); memset(memo, -1, sizeof memo); choose[0][0] = 1; for (int i = (int)(1); i < (int)(1000); ++i) for (int j = (int)(0); j < (int)(1000); ++j) { if (j == 0) choose[i][j] = 1; else choose[i][j] = add(choose[i - 1][j - 1], choose[i - 1][j]); } cin >> n >> k; cout << mul(choose[n][k], dp(n - k, n, k)) << endl; return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> v, color; bool flag = false; for (int i = 0; i < n; i++) { int x; cin >> x; x--; v.push_back(x); } int sum = 0; for (int i = 0; i < m; i++) { int x; cin >> x; sum += x; color.push_back(x); } for (int i = 0; i < n; i++) if (i + sum - 1 < n) { vector<int> cnt(m, 0); for (int j = 0; j < sum; j++) cnt[v[i + j]]++; if (cnt == color) flag = true; } if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int x; scanf("%d", &x); if (x == 3) { printf("5"); } else { for (int k = 0;; ++k) { int v = 2 * k * k + 2 * k + 1; if (v >= x) { printf("%d", 2 * k + 1); break; } } } return 0; }
9
#include <bits/stdc++.h> int main() { int n, t; std::cin >> n >> t; int arr[n - 1]; int i = 0; for (; i < n - 1; ++i) std::cin >> arr[i]; i = 1; while (i < t) { i += arr[i - 1]; } std::cout << (i == t ? "YES" : "NO") << std::endl; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &output, const vector<T> &v) { if (v.empty()) return output; for (int i = 0; i < v.size() - 1; i++) output << v[i] << " "; output << v.back(); return output; } template <typename T> istream &operator>>(istream &input, vector<T> &v) { for (auto &i : v) cin >> i; return input; } long long int gcd(const long long int a, const long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(const long long int a, const long long int b) { return (a * b) / gcd(a, b); } const int N = 1e5 + 100, mod = 1e9 + 7; vector<int> g[N]; long long int a[N]; long long int ans = 0; void dfs(int node, int par, unordered_map<long long int, long long int> &m) { unordered_map<long long int, long long int> cur; for (auto &i : m) cur[gcd(i.first, a[node])] += i.second; cur[a[node]]++; for (auto &i : cur) { ans += (long long int)i.first * i.second % mod; if (ans >= mod) ans -= mod; } for (auto &i : g[node]) { if (i != par) dfs(i, node, cur); } return; } void __sol() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i + 1]; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; g[x].emplace_back(y); g[y].emplace_back(x); } unordered_map<long long int, long long int> m; dfs(1, 0, m); cout << ans; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tc = 1; while (tc--) __sol(); return 0; }
12
#include <bits/stdc++.h> using namespace std; int n, a[1000], mas[1000], st, fin; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int cur = 0; for (int i = 0; i < n / 2 + n % 2; i++) { mas[cur] = a[i]; cur += 2; } cur = 1; for (int i = n / 2 + n % 2; i < n; i++) { mas[cur] = a[i]; cur += 2; } for (int i = 0; i < n - 1; i++) if (!(i % 2)) { if (mas[i] > mas[i + 1]) { printf("Impossible\n"); return 0; } } else if (mas[i] < mas[i + 1]) { printf("Impossible\n"); return 0; } for (int i = 0; i < n; i++) printf("%d ", mas[i]); printf("\n"); }
2
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, a, b, c; int main() { std::cin.sync_with_stdio(false); std::cin.tie(0); cin >> n >> m >> k >> a >> b >> c; if (a >= n && a + b >= n + m && a + b + c >= n + m + k) { cout << "YES"; } else { cout << "NO"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); long long gcd(long long a, long long b) { while (b) { a %= b; a ^= b, b ^= a, a ^= b; } return a; } int main() { long long a, b; scanf("%I64d%I64d", &a, &b); if (gcd(a, b) > 1) puts("Impossible"); else { while (a && b) { if (a > b) { printf("%I64dA", a / b - (b == 1)); a %= b; } else { printf("%I64dB", b / a - (a == 1)); b %= a; } } } return 0; }
16
#include <bits/stdc++.h> int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main() { int n; int a[100005]; int a1[100005]; int i; int cou = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); a1[i] = a[i]; } qsort(a1, n, sizeof(int), cmp); for (i = 0; i < n; i++) { if (a[i] != a1[i]) { cou++; if (cou >= 3) { printf("NO\n"); return 0; } } } printf("YES\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define oo 1000000010 const int N = 300010; int n , arr[N]; vector< int > g[N]; int main(){ scanf("%d",&n); for(int i = 0 ;i < n;i++){ scanf("%d",&arr[i]); } for(int i = n - 1;i >= 0;i--){ g[arr[i]].push_back(i); } int last1 = -1, last2 = -1; int ans = 0; for(int i = 0;i < n;i++){ g[arr[i]].pop_back(); if(arr[i] == last1 || arr[i] == last2) continue; ans++; if(last1 == -1) last1 = arr[i]; else if(last2 == -1) last2 = arr[i]; else{ if((int)g[last1].size() == 0) last1 = arr[i]; else if((int)g[last2].size() == 0) last2 = arr[i]; else{ if(g[last1].back() < g[last2].back()) last2 = arr[i]; else last1 = arr[i]; } } } cout << ans << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:16777216") #pragma warning(disable : 4786) int cnt[1000002]; int getValue(char a[]) { int ret = 0; for (int i = 0; a[i]; i++) { ret = ret * 2 + (a[i] - 48); } return ret; } int getPattern(char a[]) { char bin[100]; long long len = 0; ; for (int i = 0; a[i]; i++) { bin[len++] = (a[i] - 48) % 2 + 48; } bin[len] = 0; return getValue(bin); } int main() { int t, x; scanf("%d", &t); while (t--) { char cmd[10], val[100]; scanf("%s %s", cmd, val); if (strcmp(cmd, "+") == 0) { x = getPattern(val); cnt[x]++; } else if (strcmp(cmd, "-") == 0) { x = getPattern(val); cnt[x]--; } else { x = getValue(val); printf("%d\n", cnt[x]); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, k; int arr[505][505]; int main() { scanf("%i%i", &n, &k); arr[1][1] = 1; int curr = 1; int sum = 0; if (k == 1) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { arr[i][j] = curr; curr++; if (j == k) { sum += arr[i][j]; } } } } else { for (int i = 1; i <= n; i++) { for (int j = 1; j < k; j++) { arr[i][j] = curr; curr++; } } for (int i = 1; i <= n; i++) { for (int j = k; j <= n; j++) { arr[i][j] = curr; curr++; if (j == k) { sum += arr[i][j]; } } } } printf("%i\n", sum); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { printf("%i ", arr[i][j]); } printf("\n"); } }
5
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long pow_q(long long n, long long q) { long long tmp = 1; while (q > 0) { if (q & 1) { tmp *= n; tmp %= mod; } n *= n; n %= mod; q >>= 1; } return tmp; } int main() { long long n; scanf("%lld", &n); long long a1, a2 = 2, tmp; bool even = 0; while (n--) { scanf("%lld", &tmp); a2 = pow_q(a2, tmp); if (tmp % 2 == 0) even = 1; } a2 = a2 * pow_q(2, mod - 2) % mod; if (even) a1 = (a2 + 1) * pow_q(3, mod - 2) % mod; else a1 = (a2 - 1) * pow_q(3, mod - 2) % mod; printf("%lld/%lld\n", a1, a2); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int MOD = 1e9 + 7; int n, ans, ret; int tot[N], p10[N], p2[N], t4[N], t7[N], t47[N]; int a[N]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void dfs(int x, int y, int o) { if (x == n + 1) return; if (!o) { int bt = n - x + 1; int nret = ret; add(ret, 1ll * (2ll * t47[bt - 1] % MOD + MOD - t4[bt - 1] - MOD - t7[bt - 1]) % MOD * y % MOD * p10[bt] % MOD); add(ret, tot[bt - 1]); add(ret, 1ll * (p2[bt] + MOD - 1) % MOD * y % MOD * y % MOD * p10[bt] % MOD * p10[bt] % MOD); return; } if (a[x] == 4) { dfs(x + 1, (10ll * y + 4) % MOD, o); } else { dfs(x + 1, (10ll * y + 4) % MOD, 0); int lft = 1ll * ((10ll * y + 4) % MOD * p10[n - x] % MOD + ((x == n) ? 0 : t7[n - x - 1])) % MOD; int rht = 1ll * ((10ll * y + 7) % MOD * p10[n - x] % MOD + ((x == n) ? 0 : t4[n - x - 1])) % MOD; add(ret, 1ll * lft * rht % MOD); dfs(x + 1, (10ll * y + 7) % MOD, o); } } char s[N]; int solve() { for (int i = 1; i <= n; i++) a[i] = s[i] - '0'; ret = 0; dfs(1, 0, 1); return ret; } int main() { p10[0] = p2[0] = 1; t4[0] = 4; t7[0] = 7; for (int i = 1; i < N; i++) p10[i] = 10ll * p10[i - 1] % MOD; for (int i = 1; i < N; i++) p2[i] = 2 * p2[i - 1] % MOD; for (int i = 1; i < N; i++) t4[i] = (4ll * p10[i] % MOD + t4[i - 1]) % MOD; for (int i = 1; i < N; i++) t7[i] = (7ll * p10[i] % MOD + t7[i - 1]) % MOD; for (int i = 0; i < N; i++) t47[i] = 1ll * (1ll * t4[i] + t7[i]) % MOD * p2[i] % MOD; tot[0] = 28; for (int i = 1; i < N; i++) { add(tot[i], 2ll * tot[i - 1] % MOD); add(tot[i], 65ll * p10[i] % MOD * p10[i] % MOD * ((p2[i] + MOD - 1) % MOD) % MOD); add(tot[i], 11ll * (1ll * (t4[i - 1] + t7[i - 1]) % MOD * p2[i] % MOD + MOD - t4[i - 1] + MOD - t7[i - 1]) % MOD * p10[i] % MOD); add(tot[i], 1ll * (4ll * p10[i] % MOD + t7[i - 1]) % MOD * ((7ll * p10[i] % MOD + t4[i - 1]) % MOD) % MOD); } scanf("%s", s + 1); n = strlen(s + 1); ans = MOD - solve(); scanf("%s", s + 1); add(ans, solve()); printf("%d\n", ans); return 0; }
20
#include <bits/stdc++.h> using namespace std; long long n, k, ans = 1e18; signed main() { scanf("%lld%lld", &n, &k); for (long long i = 1; i <= n; i++) { if (n % i == 0) { long long j = n / i; long long x = k * i + j; if ((x % k) * (x / k) == n) ans = min(ans, x); } } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int n, m, ara[10010], low[10010][1010], g, r, ind, rem, vrem, res; deque<pair<long long int, long long int> > q; int main() { cin >> n >> m; for (int i = 0; i < m; i++) cin >> ara[i]; sort(ara, ara + m); cin >> g >> r; for (int i = 0; i <= m; i++) { for (int j = 0; j <= g; j++) { low[i][j] = -1; } } q.push_back({0, 0}); low[0][0] = 0; while (!q.empty()) { ind = q.front().first; rem = q.front().second; q.pop_front(); for (int i = -1; i < 2; i++) { if (i == 0) continue; if (0 <= ind + i && ind + i < m) { vrem = rem + abs(ara[ind] - ara[i + ind]); if (vrem <= g && (low[ind + i][vrem % g] == -1 || low[ind][rem] + (vrem == g) < low[ind + i][vrem % g])) { low[ind + i][vrem % g] = low[ind][rem] + (vrem == g); if (vrem == g) q.push_back({ind + i, vrem % g}); else q.push_front({ind + i, vrem % g}); } } } } res = -1; for (int i = 0; i < g; i++) { if (low[m - 1][i] == -1) continue; if (res == -1) res = (g + r) * low[m - 1][i] + i - (i == 0) * r; else res = min(res, (g + r) * low[m - 1][i] + i); } cout << res << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { long long int m; cin >> m; long long int arr[m]; for (int j = 0; j < m; j++) { cin >> arr[j]; } sort(arr, arr + m); long long int ans = 0; for (int j = 0, h = m - 1; h > j; h--, j++) { ans += ((arr[j] + arr[h]) * (arr[j] + arr[h])); } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll MOD = 998244353; int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } else { return -1; } } ll power(ll x, ll y, int p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } const int N = 200000; int cnt[N][5]; void solve() { int n; cin >> n; for (int i = (0); i < (n); i++) { string s; cin >> s; for (int j = (0); j < (5); j++) { cnt[i][j] = 0; } for (int j = (0); j < (s.size()); j++) { cnt[i][int(s[j] - 'a')]++; } } int ans = 0; for (int i = (0); i < (5); i++) { int curr = 0; priority_queue<int> q; for (int j = (0); j < (n); j++) { q.push(2 * cnt[j][i] - accumulate(cnt[j], cnt[j] + 5, 0)); } int x = 0; while (!q.empty()) { if (x + q.top() > 0) { x += q.top(); curr++; q.pop(); } else break; } ans = max(ans, curr); } cout << ans << endl; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(0); int n, i, j, k, cnt; long double p[] = {500, 1000, 1500, 2000, 2500}; long double m[10], w[10], s, u, sum = 0; for (i = 0; i <= 4; i++) cin >> m[i]; for (i = 0; i <= 4; i++) cin >> w[i]; cin >> s >> u; for (i = 0; i <= 4; i++) { long double a = 0.3 * p[i]; long double b = ((1.0 - (m[i] / 250.0)) * p[i]) - 50.0 * w[i]; if (a > b) sum += a; else sum += b; } sum += (s * 100.00); sum -= (u * 50.00); cout << sum; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, px, py; double dist(long long px, long long py, long long x1, long long y1, long long x2, long long y2) { double d2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); if (d2 == 0.0) return ((x1 - px) * (x1 - px) + (y1 - py) * (y1 - py)); double t = ((double)((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1))) / d2; if (t < 0.0) return ((px - x1) * (px - x1) + (py - y1) * (py - y1)); else if (t > 1.0) return ((px - x2) * (px - x2) + (py - y2) * (py - y2)); double nx = x1 + t * (x2 - x1); double ny = y1 + t * (y2 - y1); return ((nx - px) * (nx - px) + (ny - py) * (ny - py)); } int main() { const double PI = atan(1.0) * 4; cin >> n >> px >> py; vector<long long> d(n); vector<double> d2(n); vector<double> cs(0), xs(n), ys(n); double x1, x2, y1, y2; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; xs[i] = x; ys[i] = y; d[i] = (x - px) * (x - px) + (y - py) * (y - py); } for (int i = 0; i < n; i++) { x1 = xs[i]; x2 = xs[(i + 1) % n]; y1 = ys[i]; y2 = ys[(i + 1) % n]; double dd = dist(px, py, x1, y1, x2, y2); d2[i] = dd; } double maxd = max_element(d.begin(), d.end()) - d.begin(); double mind = min_element(d2.begin(), d2.end()) - d2.begin(); double res; res = PI * (d[maxd] - d2[mind]); cout.precision(20); cout << res; }
11
#include <bits/stdc++.h> using namespace std; const long long maxn = 5000 * 1000 + 5, base = 737, mod = 1000 * 1000 * 1000 + 7; long long dp[maxn]; bool p[maxn]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long x = 0, y = 0, b = 1; for (long long i = 0; i < (long long)(s).size(); i++) { x *= base; x += s[i] - '0'; y += b * (s[i] - '0'); b *= base; y %= mod; x %= mod; b %= mod; if (x == y) p[i] = true; } long long ans = 1; dp[0] = 1; for (long long i = 1; i < (long long)(s).size(); i++) { if (p[i]) dp[i] = dp[(i + 1) / 2 - 1] + 1; ans += dp[i]; } cout << ans << endl; return false; }
14