solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 100010; const int mod[2] = {1000000009, 1000000007}; const int base[2] = {1000000007, 998244353}; pair<int, int> mult[maxn]; struct HSH { pair<int, int> hsh; HSH(pair<int, int> hsh = {0, 0}) : hsh(hsh) {} HSH operator+(const HSH &rhs) const { HSH res({hsh.first + rhs.hsh.first, hsh.second + rhs.hsh.second}); if (res.hsh.first >= mod[0]) res.hsh.first -= mod[0]; if (res.hsh.second >= mod[1]) res.hsh.second -= mod[1]; return res; } HSH operator*(const pair<int, int> c) const { return HSH({1LL * hsh.first * c.first % mod[0], 1LL * hsh.second * c.second % mod[1]}); } bool operator==(const HSH &rhs) const { return hsh == rhs.hsh; } bool operator!=(const HSH &rhs) const { return hsh != rhs.hsh; } } NUL; struct TNode { int l, r, sz; HSH key, hsh; TNode(int l = 0, int r = 0, int sz = 0) : l(l), r(r), sz(sz) {} } T[8000000]; int stp; int newnode(int v = 0) { ++stp; T[stp].l = T[stp].r = 0; T[stp].sz = 1; T[stp].hsh = T[stp].key = HSH({v, v}); return stp; } void Tpull(int cur) { T[cur].sz = T[T[cur].l].sz + T[T[cur].r].sz + 1; T[cur].hsh = T[T[cur].l].hsh * mult[T[T[cur].r].sz + 1] + T[cur].key * mult[T[T[cur].r].sz] + T[T[cur].r].hsh; } int merge(int l, int r) { if (!l || !r) return l ? l : r; int res = newnode(); if (rng() % (T[l].sz + T[r].sz) < T[l].sz) T[res] = T[l], T[res].r = merge(T[l].r, r); else T[res] = T[r], T[res].l = merge(l, T[r].l); Tpull(res); return res; } void split(int rt, int &l, int &r, int k) { if (rt == 0) return l = r = 0, void(); int res = newnode(); T[res] = T[rt]; if (T[T[rt].l].sz >= k) split(T[rt].l, l, T[res].l, k), r = res; else split(T[rt].r, T[res].r, r, k - 1 - T[T[rt].l].sz), l = res; Tpull(res); } int a[maxn]; struct SEGTREE { int cl[maxn << 2], op[maxn << 2]; bool bad[maxn << 2]; void pull(int idx) { if (bad[idx << 1] || bad[idx << 1 | 1]) return bad[idx] = true, void(); if (T[op[idx << 1]].sz >= T[cl[idx << 1 | 1]].sz) { int l, r; split(op[idx << 1], l, r, T[op[idx << 1]].sz - T[cl[idx << 1 | 1]].sz); if (T[cl[idx << 1 | 1]].hsh != T[r].hsh) bad[idx] = true; else { cl[idx] = cl[idx << 1]; op[idx] = merge(l, op[idx << 1 | 1]); bad[idx] = false; } } else { int l, r; split(cl[idx << 1 | 1], l, r, T[cl[idx << 1 | 1]].sz - T[op[idx << 1]].sz); if (T[op[idx << 1]].hsh != T[r].hsh) bad[idx] = true; else { cl[idx] = merge(l, cl[idx << 1]); op[idx] = op[idx << 1 | 1]; bad[idx] = false; } } } void build(int idx, int l, int r) { if (l == r) { cl[idx] = op[idx] = 0; if (a[l] > 0) op[idx] = newnode(a[l]); else cl[idx] = newnode(-a[l]); return; } int mid = (l + r) >> 1; build(idx << 1, l, mid), build(idx << 1 | 1, mid + 1, r); pull(idx); } void modify(int idx, int l, int r, int pos, int val) { if (l == r) { cl[idx] = op[idx] = 0; if (val > 0) op[idx] = newnode(val); else cl[idx] = newnode(-val); return; } int mid = (l + r) >> 1; if (pos <= mid) modify(idx << 1, l, mid, pos, val); else modify(idx << 1 | 1, mid + 1, r, pos, val); pull(idx); } bool query(int idx, int l, int r, int L, int R, int &CL, int &OP) { if (L <= l && r <= R) { return CL = cl[idx], OP = op[idx], bad[idx]; } if (L > r || l > R) return CL = OP = 0, false; int mid = (l + r) >> 1; int lCL, rCL, lOP, rOP; bool faill = query(idx << 1, l, mid, L, R, lCL, lOP); bool failr = query(idx << 1 | 1, mid + 1, r, L, R, rCL, rOP); if (faill || failr) return true; if (T[lOP].sz >= T[rCL].sz) { int l, r; split(lOP, l, r, T[lOP].sz - T[rCL].sz); if (T[rCL].hsh != T[r].hsh) return true; else { CL = lCL; OP = merge(l, rOP); return false; } } else { int l, r; split(rCL, l, r, T[rCL].sz - T[lOP].sz); if (T[lOP].hsh != T[r].hsh) return true; else { CL = merge(l, lCL); OP = rOP; return false; } } } } ST; int main() { ios_base::sync_with_stdio(0); cin.tie(0); mult[0] = {1, 1}; for (int i = 1; i < maxn; i++) mult[i] = {1LL * mult[i - 1].first * base[0] % mod[0], 1LL * mult[i - 1].second * base[1] % mod[1]}; int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; ST.build(1, 1, n); int q; cin >> q; while (q--) { int op, l, r; cin >> op >> l >> r; if (op == 1) ST.modify(1, 1, n, l, r), a[l] = r; else { int cursz = stp; int CL, OP; bool fail = ST.query(1, 1, n, l, r, CL, OP); if (fail || T[CL].hsh != NUL || T[OP].hsh != NUL) cout << "No\n"; else cout << "Yes\n"; stp = cursz; } if (stp >= 7e6) stp = 0, ST.build(1, 1, n); } }
25
#include <bits/stdc++.h> using namespace std; double sin_deg(double x) { return sin((x * acos(-1.0)) / 180.0); } double cos_deg(double x) { return cos((x * acos(-1.0)) / 180.0); } double tan_deg(double x) { return tan((x * acos(-1.0)) / 180.0); } double asin_deg(double x) { return (asin(x) * 180) / acos(-1.0); } double acos_deg(double x) { return (acos(x) * 180) / acos(-1.0); } double atan_deg(double x) { return (atan(x) * 180) / acos(-1.0); } long long int _xor(long long int a, long long int b) { return a ^ b; } long long int _and(long long int a, long long int b) { return a & b; } long long int _or(long long int a, long long int b) { return a | b; } long long int _not(long long int a) { return ~a; } int main() { long long int b, c = 0, d, e, f, g, h, i, j, k, l1, l2, l3, t, a, m, n, p, q, r, s, u, v, x, y, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0; long long int cnt = 0, ans = 0; string str; scanf("%lld", &n); scanf("%lld", &m); for (i = 0; i < n; i++) { if (i % 2 == 0) str += '0'; else str += '1'; } cout << str; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; unordered_map<int, int> cnt; int main() { int x, ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &x), cnt[x]++, ans = max(ans, cnt[x]); printf("%d", ans); }
4
#include <bits/stdc++.h> using namespace std; struct node_class { int x, y, k; node_class(int xx, int yy, int kk) { x = xx; y = yy; k = kk; } node_class(void) {} } d[200000], ss; bool operator<(const node_class a, const node_class b) { return (a.x < b.x); } multiset<node_class> a; multiset<node_class>::iterator iter; int n, k, m; int nt[200000], px[200000], p[200000], v[200000]; void dfs(int x) { v[x] = 1; int j = p[x]; while (j) { if (!v[px[j]]) { cout << x << ' ' << px[j] << endl; v[px[j]] = 1; dfs(px[j]); } j = nt[j]; } } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> d[i].x; d[i].y = i; d[i].k = 0; } int exists = 0; for (int i = 1; i <= n; i++) if (d[i].x == 0) { exists = 1; break; } if (!exists) { cout << -1 << endl; return 0; } sort(d + 1, d + n + 1); a.insert(node_class(d[1].x, d[1].y, d[1].k)); for (int i = 2; i <= n; i++) { int bb = 0; while ((iter = a.find(node_class(d[i].x - 1, d[i].y, d[i].k))) != a.end()) { if (iter->k < k) { bb = 1; ss.x = iter->x; ss.y = iter->y; ss.k = iter->k + 1; a.erase(iter); a.insert(ss); m++; if (p[d[i].y]) { px[m] = ss.y; nt[m] = p[d[i].y]; p[d[i].y] = m; } else { px[m] = ss.y; p[d[i].y] = m; } m++; if (p[ss.y]) { px[m] = d[i].y; nt[m] = p[ss.y]; p[ss.y] = m; } else { px[m] = d[i].y; p[ss.y] = m; } break; } else { a.erase(iter); } } if (!bb) { cout << "-1"; return 0; } a.insert(node_class(d[i].x, d[i].y, 1)); } cout << n - 1 << endl; dfs(1); return 0; }
10
#include <bits/stdc++.h> using namespace std; struct ST { double key, mul, add; } a[400003]; int n, m; int s[100003]; int ls(int x) { return x << 1; } int rs(int x) { return x << 1 | 1; } void push_up(int x) { a[x].key = a[ls(x)].key + a[rs(x)].key; } void build(int x, int l, int r) { a[x].mul = 1; a[x].add = 0; if (l == r) { a[x].key = s[l]; return; } int mid = (l + r) >> 1; build(ls(x), l, mid); build(rs(x), mid + 1, r); push_up(x); } void push_down(int x, int l, int r) { int mid = (l + r) >> 1; a[ls(x)].key = a[ls(x)].key * a[x].mul + a[x].add * (mid - l + 1); a[rs(x)].key = a[rs(x)].key * a[x].mul + a[x].add * (r - mid); a[ls(x)].mul *= a[x].mul; a[rs(x)].mul *= a[x].mul; a[ls(x)].add = a[ls(x)].add * a[x].mul + a[x].add; a[rs(x)].add = a[rs(x)].add * a[x].mul + a[x].add; a[x].mul = 1; a[x].add = 0; } void modify1(int x, int l, int r, int nl, int nr, double k) { if (nl <= l && nr >= r) { a[x].key *= k; a[x].mul *= k; a[x].add *= k; return; } push_down(x, l, r); int mid = (l + r) >> 1; if (nl <= mid) modify1(ls(x), l, mid, nl, nr, k); if (nr > mid) modify1(rs(x), mid + 1, r, nl, nr, k); push_up(x); } void modify2(int x, int l, int r, int nl, int nr, double k) { if (nl <= l && nr >= r) { a[x].key += (r - l + 1) * k; a[x].add += k; return; } push_down(x, l, r); int mid = (l + r) >> 1; if (nl <= mid) modify2(ls(x), l, mid, nl, nr, k); if (nr > mid) modify2(rs(x), mid + 1, r, nl, nr, k); push_up(x); } double query(int x, int l, int r, int nl, int nr) { if (nl <= l && nr >= r) return a[x].key; push_down(x, l, r); int mid = (l + r) >> 1; double res = 0; if (nl <= mid) res = query(ls(x), l, mid, nl, nr); if (nr > mid) res += query(rs(x), mid + 1, r, nl, nr); return res; } void modify(int l, int r, int l1, int r1) { int len = r - l + 1, len1 = r1 - l1 + 1; double x = query(1, 1, n, l, r) / len, x1 = query(1, 1, n, l1, r1) / len1; modify1(1, 1, n, l, r, (double)(len - 1) / len); modify1(1, 1, n, l1, r1, (double)(len1 - 1) / len1); modify2(1, 1, n, l, r, x1 / len); modify2(1, 1, n, l1, r1, x / len1); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &s[i]); build(1, 1, n); while (m--) { int opt, l, r; scanf("%d%d%d", &opt, &l, &r); if (opt == 1) { int l1, r1; scanf("%d%d", &l1, &r1); modify(l, r, l1, r1); } else printf("%.7lf\n", query(1, 1, n, l, r)); } return 0; }
15
#include <bits/stdc++.h> using namespace std; string second = "What are you doing at the end of the world? Are you busy? Will you save " "us?"; string s_1 = "What are you doing while sending \""; string s_2 = "\"? Are you busy? Will you send \""; string s_3 = "\"?"; unsigned long long d[1000016][6]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); d[0][0] = 75; for (unsigned long long i = 1; i <= 100000; i++) { d[i][1] = s_1.size(); d[i][2] = d[i - 1][0]; d[i][3] = s_2.size(); d[i][4] = d[i - 1][0]; d[i][5] = s_3.size(); d[i][0] = d[i][1] + d[i][2] + d[i][3] + d[i][4] + d[i][5]; } unsigned long long q; cin >> q; while (q--) { unsigned long long n, k; cin >> n >> k; if (d[n][0] < k) { cout << "."; continue; } while (1) { unsigned long long sum = 0; if (n == 0) { cout << second[k - 1]; break; } if (k <= sum + d[n][1]) { cout << s_1[k - 1]; break; } sum += d[n][1]; if (k <= sum + d[n][2] && k < sum + d[n][2] + d[n][3]) { n = n - 1; k = k - sum; continue; } sum += d[n][2]; if (k <= d[n][3] + sum && k < d[n][3] + sum + d[n][4]) { cout << s_2[k - sum - 1]; break; } sum += d[n][3]; if (k <= sum + d[n][4] && k < sum + d[n][5] + d[n][4]) { n = n - 1; k = k - sum; continue; } sum += d[n][4]; cout << s_3[k - sum - 1]; break; } } return 0; }
9
#include <bits/stdc++.h> template <typename T> struct xgcd_s { T m, n, gcd; }; template <typename T> xgcd_s<T> xgcd(T a, T b) { T m[] = {1, 0}, n[] = {0, 1}, q[] = {0, 0}; for (int i = 0;; i ^= 1) { m[i] -= m[i ^ 1] * q[i]; n[i] -= n[i ^ 1] * q[i]; if (!b) return xgcd_s<T>{m[i], n[i], a}; q[i] = a / b; a %= b; std::swap(a, b); } } template <unsigned M()> struct _intmod { private: struct _p { unsigned x; }; _intmod(_p x) { n = x.x; } public: unsigned n; _intmod() { n = 0; } _intmod(int x) { n = x < 0 ? M() + x % (int)M() : x % M(); } _intmod(unsigned x) { n = x % M(); } _intmod(long x) { n = unsigned(x < 0 ? M() + x % (long)M() : x % M()); } _intmod(unsigned long x) { n = x % M(); } _intmod(long long x) { n = unsigned(x < 0 ? M() + x % (long long)M() : x % M()); } _intmod(unsigned long long x) { n = x % M(); } _intmod operator+(_intmod b) const { return {n + b.n}; } _intmod operator*(_intmod b) const { return {_p{unsigned((unsigned long long)n * b.n % M())}}; } _intmod operator-(_intmod b) const { return {_p{n >= b.n ? n - b.n : M() + n - b.n}}; } _intmod operator-() const { return {_p{n ? M() - n : 0U}}; } _intmod& operator+=(_intmod b) { return (*this = *this + b); } _intmod& operator-=(_intmod b) { return (*this = *this - b); } _intmod& operator/=(_intmod b) { return (*this = *this / b); } _intmod& operator*=(_intmod b) { return (*this = *this * b); } static _intmod mulinv(_intmod n) { xgcd_s<signed> x = xgcd<signed>(n.n, M()); assert(x.gcd == 1); return x.m; } _intmod operator/(_intmod b) const { return *this * mulinv(b); } bool operator==(_intmod b) const { return n == b.n; } bool operator!=(_intmod b) const { return n != b.n; } explicit operator int() const { return n; } }; template <unsigned M()> std::ostream& operator<<(std::ostream& o, _intmod<M> n) { return o << n.n; } template <unsigned M()> std::istream& operator>>(std::istream& i, _intmod<M>& n) { long long x; i >> x; n = x; return i; } template <unsigned M> unsigned intmod_unsignedconst() { static_assert(M > 0 && M < (1U << 31), "modulus bad"); return M; } template <unsigned* M> unsigned intmod_unsignedptr() { return *M; } template <unsigned M> using intmod = _intmod<intmod_unsignedconst<M> >; template <unsigned* M> using intmodv = _intmod<intmod_unsignedptr<M> >; static_assert(std::is_trivially_copyable<intmod<3> >::value, ""); template <typename T, typename X> T bpow(T b, X x) { T r = 1, p = b; while (x) { if (x & 1) r = r * p; if (x >>= 1) p = p * p; } return r; } using I = intmod<1000000007>; using XI = intmod<1000000007 - 1>; struct expr { I m; XI xm, len; }; I ten(XI x) { return bpow(I(10), x.n); } expr operator+(expr a, expr b) { expr o; o.len = a.len + b.len; o.m = a.m * ten(b.len) + b.m; return o; } expr rangeSameLen(expr a, expr b) { I L = ten(a.len); expr o; if (L == 1) { o.m = (b.m * (b.m + 1) - (a.m - 1) * a.m) / 2; } else { I ilm1 = I::mulinv(L - 1); I Lpow = bpow(L, int(b.xm - a.xm + 1)); o.m = ((Lpow - L) * ilm1 - b.m + a.m * Lpow) * ilm1; } o.len = a.len * (b.xm - a.xm + 1); return o; } expr operator-(expr a, expr b) { expr o{}; while (a.len.n < b.len.n) { I m10 = ten(a.len.n); XI xm10 = bpow(XI(10), a.len.n); o = o + rangeSameLen(a, expr{m10 - 1, xm10 - 1, a.len.n}); ++a.len.n; a.m = m10; a.xm = xm10; } return o + rangeSameLen(a, b); } expr operator*(expr a, expr b) { I x = ten(b.len); expr o; I xsum = x == 1 ? a.m : (I(1) - bpow(x, a.xm.n)) / (I(1) - x); o.m = b.m * xsum; o.len = a.xm * b.len; return o; } expr parseNumber(const char*& p) { expr o{}; while (*p >= '0' && *p <= '9') { int d = *p++ - '0'; o.m = o.m * 10 + d; o.xm = o.xm * 10 + d; ++o.len.n; } return o; } expr parseExpression(const char*& p); expr parseTerm(const char*& p) { expr a = parseNumber(p); if (*p == '-') { ++p; expr b = parseNumber(p); return a - b; } if (*p == '(') { ++p; expr b = parseExpression(p); assert(*p == ')'); ++p; return a * b; } return a; } expr parseExpression(const char*& p) { expr a = parseTerm(p); if (*p == '+') { ++p; expr b = parseExpression(p); return a + b; } return a; } using namespace std; int main(int argc, char** argv) { if (argc == 2) freopen(argv[1], "r", stdin); ios_base::sync_with_stdio(0); string s; cin >> s; const char* p = s.data(); expr ans = parseExpression(p); assert(p == s.data() + s.size()); cout << ans.m; return 0; }
26
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int C = 26; void add(int &x, int y) { x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x) { while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % MOD; b >>= 1; a = 1ll * a * a % MOD; } return ret; } const int MAXN = 2e5 + 10; const int SQ = 1000; int n, q; vector<int> adj[2][MAXN]; int type[MAXN], uu[MAXN], vv[MAXN]; set<pair<int, int> > exists; bool mark[MAXN]; int inc(int x) { return x + 1 == n ? 0 : x + 1; } int comp[MAXN], gg; void dfsComp(int v) { if (comp[v]) return; comp[v] = gg; for (int u : adj[0][v]) dfsComp(u); } pair<int, int> getInOrder(pair<int, int> p) { return p.first < p.second ? p : make_pair(p.second, p.first); } void build(int l, int r) { for (int v = 0; v <= n; v++) adj[0][v].clear(), adj[1][v].clear(); memset(mark, 0, sizeof(mark)); set<pair<int, int> > temp; for (int i = l; i < r; i++) mark[uu[i]] = mark[vv[i]] = mark[inc(uu[i])] = mark[inc(vv[i])] = true, temp.insert(getInOrder({uu[i], vv[i]})), temp.insert(getInOrder({inc(uu[i]), inc(vv[i])})); for (auto x : exists) if (!temp.count(x)) adj[0][x.first].push_back(x.second), adj[0][x.second].push_back(x.first); gg = 0; memset(comp, 0, sizeof(comp)); for (int v = 0; v < n; v++) if (mark[v] && !comp[v]) { gg++; dfsComp(v); } for (int v = 0; v < n; v++) comp[v]--; for (auto x : exists) if (temp.count(x) && comp[x.first] != comp[x.second]) adj[1][comp[x.first]].push_back(comp[x.second]), adj[1][comp[x.second]].push_back(comp[x.first]); } bool vis[MAXN]; bool dfs(int v, int dest) { if (vis[v]) return false; vis[v] = true; if (v == dest) return true; for (int u : adj[1][v]) if (!vis[u] && dfs(u, dest)) return true; return false; } void removeEdge(int v, int u) { adj[1][v].erase(find(adj[1][v].begin(), adj[1][v].end(), u)); } void solve() { cin >> n >> q; for (int i = 0; i < q; i++) cin >> type[i] >> uu[i] >> vv[i], uu[i]--, vv[i]--; int last = 0; for (int i = 0; i < q; i++) { if (i % SQ == 0) build(i, min(i + SQ, q)); if (last) uu[i] = inc(uu[i]), vv[i] = inc(vv[i]); if (uu[i] > vv[i]) swap(uu[i], vv[i]); if (type[i] == 1) { if (exists.count({uu[i], vv[i]})) { exists.erase({uu[i], vv[i]}); if (comp[uu[i]] != comp[vv[i]]) { removeEdge(comp[uu[i]], comp[vv[i]]); removeEdge(comp[vv[i]], comp[uu[i]]); } } else { exists.insert({uu[i], vv[i]}); if (comp[uu[i]] != comp[vv[i]]) { adj[1][comp[uu[i]]].push_back(comp[vv[i]]); adj[1][comp[vv[i]]].push_back(comp[uu[i]]); } } } else { fill(vis, vis + gg, 0); if (dfs(comp[uu[i]], comp[vv[i]])) { cout << "1"; last = 1; } else { cout << "0"; last = 0; } } } cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int te = 1; for (int w = 1; w <= te; w++) { solve(); } return 0; }
18
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } int n, m; vector<pair<long long, int> > dv; long long calc(int idx, long long num) { if (idx == dv.size()) { long long res = 0; while (num < n) { res += n - num; num *= 2; } return res; } long long res = 0; for (int i = 0; i <= dv[idx].second; i++) { res += calc(idx + 1, num); num *= dv[idx].first; } return res; } int main() { cin >> m >> n; int x = 0, t; for (int i = 0; i < m; i++) { cin >> t; t--; x = gcd(x, t); } while (x % 2 == 0) x /= 2; for (int i = 2; i * i <= x; i++) { int cnt = 0; while (x % i == 0) { cnt++; x /= i; } if (cnt > 0) dv.push_back(pair<long long, int>(i, cnt)); } if (x > 1) dv.push_back(pair<long long, int>(x, 1)); long long res = calc(0, 1); cout << res << endl; return 0; }
14
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long a, b, c, aa, bb, cc; cin >> a >> b >> c; aa = a / 3; bb = b / 2; cc = c / 2; long long ans = min(aa, min(bb, cc)), mx = 0; a -= ans * 3, b -= ans * 2, c -= ans * 2; for (long long i = 0; i < 7; i++) { long long ta = a, tb = b, tc = c, j; for (j = i; j < i + 7; j++) { if (j % 7 == 0 || j % 7 == 1 || j % 7 == 4) ta--; else if (j % 7 == 2 || j % 7 == 6) tb--; else tc--; if (ta < 0 || tb < 0 || tc < 0) { j--; break; } } mx = max(mx, j - i + 1); } cout << ans * 7 + mx << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 18; int N; int p[MAXN]; int data[MAXN]; int find(int u) { return (data[u] < 0) ? u : data[u] = find(data[u]); } void join(int u, int v) { u = find(u); v = find(v); if (u != v) { data[u] += data[v]; data[v] = u; } } int main() { ios_base::sync_with_stdio(0); memset(data, -1, sizeof data); cin >> N; for (int i = 0; i < N; ++i) { cin >> p[i]; p[i]--; } string S; cin >> S; for (int i = 0; i < (int)S.size(); ++i) { if (S[i] == '1') join(i, i + 1); } for (int i = 0; i < N; ++i) { if (find(p[i]) != find(i)) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int head[N], ver[N << 1], Next[N << 1], total; int d[N], f[N][3], len; vector<int> seq, stl, str; inline void add(int x, int y) { ver[++total] = y; Next[total] = head[x]; head[x] = total; } inline void addv(int x, int v) { int pos = -1; for (int i = 0; i <= 2; ++i) { if (v >= f[x][i]) { pos = i; break; } } if (pos == -1) return; for (int i = 2; i >= pos + 1; --i) f[x][i] = f[x][i - 1]; f[x][pos] = v; } void dfs1(int x, int fa, int dep, int b) { d[x] = 0; if (x == b) len = dep; for (int i = head[x]; i; i = Next[i]) { int y = ver[i]; if (y == fa) continue; dfs1(y, x, dep + 1, b); addv(x, d[y] + 1); d[x] = max(d[x], d[y] + 1); } } bool IsExistKeyPoint(int x, int fa) { if (f[x][2] >= len) return true; for (int i = head[x]; i; i = Next[i]) { int y = ver[i]; if (y == fa) continue; addv(y, f[x][d[y] + 1 == f[x][0]] + 1); if (IsExistKeyPoint(y, x)) return true; } return false; } bool dfs3(int x, int fa, int b) { bool flag = (x == b); d[x] = 0; for (int i = head[x]; i; i = Next[i]) { int y = ver[i]; if (y == fa) continue; if (dfs3(y, x, b)) flag = true; else d[x] = max(d[x], d[y] + 1); } if (flag) seq.push_back(x); return flag; } int main() { int T, n, a, b; scanf("%d", &T); while (T--) { scanf("%d", &n); scanf("%d", &a); scanf("%d", &b); memset(head, 0, sizeof(int) * (n + 1)); for (int i = 1; i <= n; ++i) for (int j = 0; j <= 2; ++j) f[i][j] = 0; total = 0, seq.clear(), stl.clear(), str.clear(); for (int i = 1; i <= n - 1; ++i) { int u, v; scanf("%d", &u); scanf("%d", &v); add(u, v); add(v, u); } dfs1(a, 0, 0, b); if (!IsExistKeyPoint(a, 0)) { puts("NO"); continue; } dfs3(a, 0, b); for (int i = 0; i <= len; ++i) { stl.push_back(stl.empty() || d[seq[i]] - i > stl.back() ? d[seq[i]] - i : stl.back()); str.push_back(str.empty() || d[seq[len - i]] - i > str.back() ? d[seq[len - i]] - i : str.back()); } reverse(str.begin(), str.end()); int l = 0, r = len; while (l < r) { if (l < str[r]) l = str[r]; else if (len - r < stl[l]) r = len - stl[l]; else break; } puts(l >= r ? "YES" : "NO"); } return 0; }
22
#include <bits/stdc++.h> using namespace std; int a[100000]; int main() { string s; cin >> s; for (int i = 1; i < s.size(); i++) { if (s[i] == s[i - 1]) a[i - 1]++; } for (int i = 0; i < s.size() - 1; i++) { a[i + 1] += a[i]; } int n; cin >> n; while (n--) { int x, y; cin >> x >> y; cout << (y - 2 >= 0 ? a[y - 2] : 0) - (x - 2 >= 0 ? a[x - 2] : 0) << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 1 << 19; struct Node { int l, r; pair<int, int> val; int toAdd; Node() : l(-1), r(-1), val(make_pair(N, 1)), toAdd(0) {} Node(int _l, int _r) : l(_l), r(_r), val(make_pair(N, _r - _l)), toAdd(0) {} void add(int x) { toAdd += x; val.first += x; } }; Node tree[2 * N + 3]; void build() { for (int i = 0; i < N; i++) tree[N + i] = Node(i, i + 1); for (int i = N - 1; i > 0; i--) tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r); } void push(int v) { if (v >= N) throw; for (int u = 2 * v; u < 2 * v + 2; u++) { tree[u].add(tree[v].toAdd); } tree[v].toAdd = 0; } pair<int, int> merge(pair<int, int> v, pair<int, int> u) { pair<int, int> res; res.first = min(v.first, u.first); res.second = 0; if (res.first == v.first) res.second += v.second; if (res.first == u.first) res.second += u.second; return res; } void update(int v) { if (v >= N) throw; tree[v].val = merge(tree[2 * v].val, tree[2 * v + 1].val); } void addOnSegm(int v, int l, int r, int x) { if (l <= tree[v].l && tree[v].r <= r) { tree[v].add(x); return; } if (l >= tree[v].r || tree[v].l >= r) return; push(v); for (int u = 2 * v; u < 2 * v + 2; u++) addOnSegm(u, l, r, x); update(v); } pair<int, int> getVal(int v, int l, int r) { if (l <= tree[v].l && tree[v].r <= r) return tree[v].val; if (l >= tree[v].r || tree[v].l >= r) return make_pair(N, 0); push(v); return merge(getVal(2 * v, l, r), getVal(2 * v + 1, l, r)); } int n; int a[N + 5]; int lst[N + 5]; int st[N + 5][2]; int stSz; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < N; i++) lst[i] = -1; build(); st[stSz][0] = N; st[stSz][1] = -1; stSz++; long long ans = 0; int L = 0; for (int r = 1; r <= n; r++) { int x = a[r - 1]; L = max(L, lst[x] + 1); lst[x] = r - 1; int p = r - 1; while (st[stSz - 1][0] < x) { stSz--; addOnSegm(1, st[stSz][1], p, -st[stSz][0]); p = st[stSz][1]; } addOnSegm(1, p, r, x); st[stSz][0] = x; st[stSz][1] = p; stSz++; addOnSegm(1, r - 1, r, r - 1 - N); pair<int, int> val = getVal(1, L, r); if (val.first == r) ans += val.second; } printf("%lld\n", ans); return 0; }
17
#include <bits/stdc++.h> const int maxn = 2e5 + 5; int n, lent; char s[maxn], t[maxn]; struct ACAM { int ch[maxn][26], tag[maxn], fail[maxn], cnt[maxn], tot = 1; int match[maxn]; void insert(char *s) { int now = 1, len = strlen(s); for (int i = 0; i < len; ++i) { int sid = s[i] - 'a'; if (!ch[now][sid]) ch[now][sid] = ++tot; now = ch[now][sid]; } ++tag[now]; } void build_fail() { std ::queue<int> q; fail[1] = 1; for (int i = 0; i < 26; ++i) if (ch[1][i]) fail[ch[1][i]] = 1, q.push(ch[1][i]); else ch[1][i] = 1; while (!q.empty()) { int u = q.front(); q.pop(); int f = fail[u]; for (int i = 0; i < 26; ++i) { int &v = ch[u][i]; if (v) fail[v] = ch[f][i], q.push(v); else v = ch[f][i]; } cnt[u] = tag[u] + cnt[fail[u]]; } } void Match(char *t) { int now = 1, len = strlen(t); for (int i = 0; i < len; ++i) { now = ch[now][t[i] - 'a']; match[i] = cnt[now]; } } } F, G; int main() { scanf("%s", t); lent = strlen(t); scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%s", s), F.insert(s); std ::reverse(s, s + strlen(s)); G.insert(s); } F.build_fail(), G.build_fail(); F.Match(t); std ::reverse(t, t + lent); G.Match(t); long long ans = 0; for (int i = 0; i < lent - 1; ++i) ans = ans + (long long)F.match[i] * G.match[lent - i - 2]; printf("%lld", ans); return 0; }
16
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int d[maxn], b[maxn]; int main() { int n, a; cin >> n; memset(d, 0, sizeof(d)); int maxv = 0; for (int i = 0; i < n; i++) { scanf("%d ", &a), scanf("%d", &b[a]); if (a > maxv) maxv = a; } if (b[0] > 0) d[0] = 1; int ans = 0; for (int i = 1; i <= maxv + 1; i++) { if (b[i] == 0) d[i] = d[i - 1]; else { if (b[i] > i) d[i] = 1; else d[i] = d[i - b[i] - 1] + 1; } if (d[i] > ans) ans = d[i]; } cout << n - ans << endl; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k, m, i, j, cost = 0; cin >> n >> k; char a[n]; map<char, long long> make_pair; priority_queue<long long> pq; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) make_pair[a[i]]++; map<char, long long>::iterator itr; for (itr = make_pair.begin(); itr != make_pair.end(); itr++) { pq.push(itr->second); } while (!pq.empty() && k > 0) { long long t, taken; t = pq.top(); if (t >= k) { taken = k; k = 0; } else { taken = t; k -= t; pq.pop(); } cost += taken * taken; } cout << cost << endl; }
5
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; long long danger, n, m, x, nb, curans, myprev; string s; long long dp[1000001]; long long f(long long n) { long long ans = 1; while (n) { if (n % 10) ans *= (n % 10); n /= 10; } return ans; } vector<long long> val[100]; long long g(long long n) { if (n < 10) return n; if (dp[n] != -1) return dp[n]; return g(f(n)); } void go() { for (int i = 1; i <= 1e6; i++) dp[i] = -1; for (int i = 1; i <= 1e6; i++) dp[i] = g(i), val[dp[i]].push_back(i); } void solve() { go(); long long Q; cin >> Q; while (Q--) { long long l, r, k; cin >> l >> r >> k; long long low = lower_bound((val[k]).begin(), (val[k]).end(), l) - val[k].begin(); long long high = upper_bound((val[k]).begin(), (val[k]).end(), r) - val[k].begin(); cout << high - low << '\n'; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int n; int p[MAXN], ones[MAXN << 1]; int main() { while (~scanf("%d", &n)) { long long sum = 0, mn; int mn_idx = 0, pos = 0, neg = 0; memset(ones, 0, sizeof(ones)); for (int i = 1; i <= n; ++i) { scanf("%d", p + i); sum += abs(p[i] - i); if (p[i] > i) ++pos; else ++neg; if (p[i] >= i) ++ones[p[i] - i]; } mn = sum; for (int i = 1; i <= n; ++i) { sum = sum - pos + neg - 1; sum = sum - abs(p[n - i + 1] - n) + p[n - i + 1] - 1; if (p[n - i + 1] != 1) { ++pos; --neg; } pos -= ones[i]; neg += ones[i]; ++ones[p[n - i + 1] - 1 + i]; if (sum < mn) { mn = sum; mn_idx = i; } } printf("%I64d %d\n", mn, mn_idx); } }
11
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000007; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<map<int, int> > s(n); vector<vector<int> > a(n, vector<int>(m, 0)); int mind = 0; int mx = 0; for (int i = 0; i < int(n); ++i) { for (int j = 0; j < int(m); ++j) { cin >> a[i][j]; s[i][a[i][j]] = j; } if (s[i].size() > mx) { mx = s[i].size(); mind = i; } } int sum = 0; vector<int> ans(n, 0); for (int i = 0; i < int(n); ++i) { if (i != mind) { int s = sum ^ a[i][0]; for (int j = 0; j < int(m); ++j) { if (sum ^ a[i][j] > s) { s = sum ^ a[i][j]; ans[i] = j; } } sum = s; } } for (int i = 0; i < int(m); ++i) { if ((sum ^ a[mind][i]) != 0) { cout << "TAK\n"; ans[mind] = i; for (int j = 0; j < int(n); ++j) cout << ans[j] + 1 << ' '; return 0; } } cout << "NIE"; }
8
#include <bits/stdc++.h> using namespace std; int main() { int i, j, q, r, t; int w, h; cin >> w >> h; long long c = 0; for (i = 0; i <= w; i++) { for (j = 0; j <= h; j++) { c += min(i - 0, w - i) * min(j - 0, h - j); } } cout << c; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int oo = (int)1e9; const long long OO = (long long)4e18; const double PI = acos(-1.0); const int N = (int)1e6 + 9; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { int t; scanf("%d", &t); while (t--) { int le, v, l, r; scanf("%d%d%d%d", &le, &v, &l, &r); l--; int cannot = r / v - l / v; int stat = le / v; printf("%d\n", stat - cannot); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, k, A, B; long long a[100005]; long long Cont(long long l, long long r) { return upper_bound(a, a + k, r) - lower_bound(a, a + k, l + 1); } long long Try(long long l, long long r) { long long tt = Cont(l, r); if (tt == 0) return A; if (l + 1 == r) return B * tt * (r - l); return min(Try(l, (r + l) / 2) + Try((r + l) / 2, r), B * tt * (r - l)); } int main() { cin >> n >> k >> A >> B; for (int i = 0; i < k; i++) cin >> a[i]; sort(a, a + k); long long p = 1; while (n--) p *= 2; n = p; cout << Try(0, n) << '\n'; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 533333, EE = 100000000, GG = 1000000000, ima = 2147483647; int eq(double a, double b) { return a + 0.00000000001 >= b && b + 0.00000000001 >= a; } template <typename T> void _ma(T &a, T b) { if (a < b) a = b; } template <typename T> void _mi(T &a, T b) { if (a > b) a = b; } namespace io { void _(int &k) { char c; int e = 1; k = 0; while ((c = getchar()) > '9' || c < '0') if (c == '-') e = -1; k = c - '0'; while ((c = getchar()) <= '9' && c >= '0') { k *= 10; k += c - '0'; } k *= e; } void _(long long &k) { char c; int e = 1; k = 0; while ((c = getchar()) > '9' || c < '0') if (c == '-') e = -1; k = c - '0'; while ((c = getchar()) <= '9' && c >= '0') { k *= 10; k += c - '0'; } k *= e; } void _(int &a, int &b) { _(a); _(b); } void _(int &a, int &b, int &c, int &d, int &e) { _(a); _(b); _(c); _(d); _(e); } void _(int &a, int &b, int &c, int &d) { _(a); _(b); _(c); _(d); } void _(int &a, int &b, int &c) { _(a); _(b); _(c); } void _(long long &a, long long &b) { _(a); _(b); } void _(long long &a, long long &b, long long &c, long long &d, long long &e) { _(a); _(b); _(c); _(d); _(e); } void _(long long &a, long long &b, long long &c) { _(a); _(b); _(c); } void _(long long &a, long long &b, long long &c, long long &d) { _(a); _(b); _(c); _(d); } void _c(char &c) { while ((c = getchar()) == ' ' || c == '\n') ; } void _p0(int k) { if (k >= 10) _p0(k / 10); putchar(k % 10 + '0'); } void _p0(long long k) { if (k >= 10) _p0(k / 10); putchar(k % 10 + '0'); } void _p(int k) { if (k < 0) { putchar('-'); _p0(-k); } else _p0(k); } void _p(long long k) { if (k < 0) { putchar('-'); _p0(-k); } else _p0(k); } void __p(int k) { _p(k); putchar(' '); } void _pn(int k) { _p(k); putchar('\n'); } void __p(long long k) { _p(k); putchar(' '); } void _pn(long long k) { _p(k); putchar('\n'); } void _p(int a, int b) { __p(a); _pn(b); } void _p(int a, int b, int c) { __p(a); __p(b); _pn(c); } void _p(int a, int b, int c, int d) { __p(a); __p(b); __p(c); _pn(d); } void _p(long long a, long long b) { __p(a); _pn(b); } void _p(long long a, long long b, long long c) { __p(a); __p(b); _pn(c); } void _p(long long a, long long b, long long c, long long d) { __p(a); __p(b); __p(c); _pn(d); } void op(long long *a, int n) { int i; n--; for (i = 1; i <= n; i++) __p(a[i]); _pn(a[n + 1]); } void op(int *a, int n) { int i; n--; for (i = 1; i <= n; i++) __p(a[i]); _pn(a[n + 1]); } } // namespace io namespace graph { int fr[N], nx[N], t[N], w[N], j2; void me(int s1, int t1) { nx[++j2] = fr[s1]; fr[s1] = j2; t[j2] = t1; } void me(int s1, int t1, int w1) { nx[++j2] = fr[s1]; fr[s1] = j2; t[j2] = t1; w[j2] = w1; } void me1(int s1, int t1) { me(s1, t1); me(t1, s1); } void me1(int s1, int t1, int w1) { me(s1, t1, w1); me(t1, s1, w1); } } // namespace graph namespace ufs { int fa[N]; void _init(int n) { int i; for (i = 1; i <= n; i++) fa[i] = i; } int fi(int k) { return fa[k] = fa[k] == k ? k : fi(fa[k]); } void hb(int a, int b) { a = fi(a); b = fi(b); fa[a] = b; } } // namespace ufs int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void fop(const char *s) { char c[256], d[256]; memset(c, 0, sizeof(c)); memset(d, 0, sizeof(d)); strcpy(c, s); strcpy(d, s); freopen(strcat(c, ".in"), "r", stdin); freopen(strcat(d, ".out"), "w", stdout); } void fcl() { fclose(stdin); fclose(stdout); } using namespace ufs; using namespace io; int n, m, a[N], an, f[N]; char s[N]; int main() { int i, j, a1, a2; _(n); for (i = 1; i <= n; i++) _(a[i]); sort(a + 1, a + n + 1); op(a, n); }
1
#include <bits/stdc++.h> using namespace std; int a[200005 << 1], res[200005]; vector<int> E[200005]; int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); } int main() { int n, i, k, j; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; i++) E[gcd(i, n)].push_back(i); for (i = 1; i < n; i++) { if (!E[i].size()) continue; memset(res, 0, sizeof(res)); for (k = 1; k <= n * 2; k++) res[k % i] = max(res[k % i], a[k]); int t = 0, cnt = 0; for (k = 1; k <= n * 2; k++) { if (a[k] >= res[k % i]) { cnt++; if (t < E[i].size() && cnt >= E[i][t]) t++; if (k > n) ans += t; } else t = 0, cnt = 0; } } cout << ans << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 100010; vector<int> vc[N]; int dp[350]; int a[N], b; int n, m, k, s, e; void run() { int ans = 0; memset(dp, 0x3f, sizeof(dp)); dp[0] = -1; vector<int>::iterator it; scanf("%d%d%d%d", &n, &m, &s, &e); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < m; ++i) scanf("%d", &b), vc[b].push_back(i); for (int i = 0; i < n; ++i) for (int j = s / e; j >= 1; --j) { it = upper_bound(vc[a[i]].begin(), vc[a[i]].end(), dp[j - 1]); if (it == vc[a[i]].end()) continue; dp[j] = min(*it, dp[j]); if (i + 2 + dp[j] + j * e <= s) ans = max(ans, j); } printf("%d\n", ans); } int main() { run(); return 0; }
15
#include <bits/stdc++.h> struct SEG { int l, r, v; }; struct LIST { int prev, next; SEG seg; } list[1000000]; int num; void initList() { num = 2; list[1].next = 0; list[0].prev = 1; list[0].prev = list[1].next = -1; } void set(int x, int l, int r, int v) { list[x].seg.l = l; list[x].seg.r = r; list[x].seg.v = v; } void insert(int c, int b) { int a = list[c].prev; list[a].next = b; list[c].prev = b; list[b].prev = a; list[b].next = c; } long long treeSum[1000000], markSum[1000000]; void initSum(int pos, int l, int r) { treeSum[pos] = markSum[pos] = 0; if (l == r) return; int mid = (l + r) / 2; initSum(2 * pos, l, mid); initSum(2 * pos + 1, mid + 1, r); } void spreadSum(int pos, int l, int r) { int mid = (l + r) / 2; markSum[2 * pos] += markSum[pos]; markSum[2 * pos + 1] += markSum[pos]; treeSum[2 * pos] += markSum[pos] * (mid - l + 1); treeSum[2 * pos + 1] += markSum[pos] * (r - mid); markSum[pos] = 0; } void updateSum(int pos, int l, int r, int dl, int dr, int v) { if (l > r) { puts("errorzzz"); exit(0); } if (dl <= l && r <= dr) { treeSum[pos] += (long long)(r - l + 1) * v; markSum[pos] += v; return; } int mid = (l + r) / 2; if (markSum[pos]) spreadSum(pos, l, r); if (dl <= mid) updateSum(2 * pos, l, mid, dl, dr, v); if (dr > mid) updateSum(2 * pos + 1, mid + 1, r, dl, dr, v); treeSum[pos] = treeSum[2 * pos] + treeSum[2 * pos + 1]; } long long querySum(int pos, int l, int r, int dl, int dr) { if (l > r) { puts("errorzzz"); exit(0); } if (dl <= l && r <= dr) return treeSum[pos]; int mid = (l + r) / 2; if (markSum[pos]) spreadSum(pos, l, r); long long sum = 0; if (dl <= mid) sum += querySum(2 * pos, l, mid, dl, dr); if (dr > mid) sum += querySum(2 * pos + 1, mid + 1, r, dl, dr); treeSum[pos] = treeSum[2 * pos] + treeSum[2 * pos + 1]; return sum; } int treePos[1000000], markPos[1000000]; void spreadPos(int pos) { treePos[2 * pos] = treePos[2 * pos + 1] = markPos[2 * pos] = markPos[2 * pos + 1] = markPos[pos]; markPos[pos] = 0; } void initPos(int pos, int l, int r) { markPos[pos] = 0; if (l == r) { treePos[pos] = l + 1; return; } int mid = (l + r) / 2; initPos(2 * pos, l, mid); initPos(2 * pos + 1, mid + 1, r); } void updatePos(int pos, int l, int r, int dl, int dr, int v) { if (l > r) { puts("errorzz"); exit(0); } if (dl <= l && r <= dr) { markPos[pos] = v; treePos[pos] = v; return; } int mid = (l + r) / 2; if (markPos[pos]) spreadPos(pos); if (dl <= mid) updatePos(2 * pos, l, mid, dl, dr, v); if (dr > mid) updatePos(2 * pos + 1, mid + 1, r, dl, dr, v); } int queryPos(int pos, int l, int r, int x) { if (l > r) { puts("errorzz"); exit(0); } if (l == r) return treePos[pos]; int mid = (l + r) / 2; if (markPos[pos]) spreadPos(pos); if (x <= mid) return queryPos(2 * pos, l, mid, x); else return queryPos(2 * pos + 1, mid + 1, r, x); } int abs(int x) { return (x < 0) ? -x : x; } void print(SEG x) {} int main() { int n, m; scanf("%d %d", &n, &m); initList(); initSum(1, 1, n); initPos(1, 1, n); for (int i = 1; i <= n; i++) { set(num, i, i, i); insert(0, num); num++; } int p, l, r, v; for (int i = 0; i < m; i++) { scanf("%d", &p); if (p == 1) { scanf("%d %d %d", &l, &r, &v); int lpos = queryPos(1, 1, n, l), rpos = queryPos(1, 1, n, r); print(list[lpos].seg), print(list[rpos].seg); int A = list[lpos].prev; int C = list[rpos].next; list[A].next = C; list[C].prev = A; int pos = lpos; bool flag = false; while (1) { if (list[pos].seg.l <= l && r <= list[pos].seg.r) { if (l != list[pos].seg.l) { set(num, list[pos].seg.l, l - 1, list[pos].seg.v); updatePos(1, 1, n, list[pos].seg.l, l - 1, num); insert(C, num); num++; } set(num, l, r, v); updatePos(1, 1, n, l, r, num); updateSum(1, 1, n, l, r, abs(v - list[pos].seg.v)); insert(C, num); num++; flag = true; if (r != list[pos].seg.r) { set(num, r + 1, list[pos].seg.r, list[pos].seg.v); updatePos(1, 1, n, r + 1, list[pos].seg.r, num); insert(C, num); num++; } } else if (list[pos].seg.l < l) { set(num, list[pos].seg.l, l - 1, list[pos].seg.v); updatePos(1, 1, n, list[pos].seg.l, l - 1, num); insert(C, num); num++; if (num == 1000000) { puts("error"); exit(0); } updateSum(1, 1, n, l, list[pos].seg.r, abs(v - list[pos].seg.v)); } else if (list[pos].seg.r > r) { set(num, l, r, v); updatePos(1, 1, n, l, r, num); insert(C, num); num++; if (num == 1000000) { puts("error"); exit(0); } flag = true; set(num, r + 1, list[pos].seg.r, list[pos].seg.v); updatePos(1, 1, n, r + 1, list[pos].seg.r, num); insert(C, num); num++; if (num == 1000000) { puts("error"); exit(0); } updateSum(1, 1, n, list[pos].seg.l, r, abs(v - list[pos].seg.v)); } else { updateSum(1, 1, n, list[pos].seg.l, list[pos].seg.r, abs(v - list[pos].seg.v)); } if (pos == rpos) break; pos = list[pos].next; } if (!flag) { set(num, l, r, v); updatePos(1, 1, n, l, r, num); insert(C, num); num++; if (num == 1000000) { puts("error"); exit(0); } } } else { scanf("%d %d", &l, &r); printf("%I64d\n", querySum(1, 1, n, l, r)); } } }
16
#include <bits/stdc++.h> using namespace std; const int size = 5007; long long a[size], diff[size]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; long long s = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) diff[abs(a[i] - a[j])]++, s++; long double ans = 0; long long sum[size]; sum[0] = diff[0]; for (int i = 1; i < size; ++i) sum[i] = sum[i - 1] + diff[i]; for (int i = 0; i < size; ++i) if (diff[i] > 0) for (int j = 0; j < size; ++j) if (diff[j] > 0) ans += diff[i] * diff[j] * (sum[size - 1] - sum[min(size - 1, i + j)]); ans /= (1.0 * s); ans /= (1.0 * s); ans /= (1.0 * s); cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 5010; const int MAXM = 100010; const int INF = ~0U >> 2; struct node { int a, b, w; } e[MAXM]; struct Enode { int to, next; } ee[MAXM * 2]; int n, m, k, box[MAXN], size; int len[MAXN][MAXN]; int r[MAXN], rc; int p[MAXN]; int dp[MAXN][3]; bool used[MAXN]; map<pair<int, int>, int> id; bool operator<(const node &x, const node &y) { return x.w < y.w; } int find_set(int x) { if (p[x] != x) p[x] = find_set(p[x]); return p[x]; } void init() { scanf("%d%d%d", &n, &m, &k); rc = size = 0; memset(box, -1, sizeof(box)); for (int i = 0, a, b, w; i < m; ++i) { scanf("%d%d%d", &a, &b, &w); e[i].a = a, e[i].b = b, e[i].w = w; len[a][b] = len[b][a] = w; id[make_pair(a, b)] = id[make_pair(b, a)] = i + 1; if (a == 1) r[rc++] = b; if (b == 1) r[rc++] = a; } } void add(int from, int to) { ee[size].to = to; ee[size].next = box[from]; box[from] = size++; ee[size].to = from; ee[size].next = box[to]; box[to] = size++; } void del(int from, int to) { if (box[from] == -1) return; if (ee[box[from]].to == to) { box[from] = ee[box[from]].next; return; } for (int i = box[from]; ~i; i = ee[i].next) { if (ee[ee[i].next].to == to) { ee[i].next = ee[ee[i].next].next; break; } } } int MDMST() { sort(e, e + m); for (int i = 1; i <= n; ++i) p[i] = i; for (int i = 0; i < m; ++i) { if (e[i].a == 1 || e[i].b == 1) continue; int a = find_set(e[i].a), b = find_set(e[i].b); if (a != b) p[a] = b, add(e[i].a, e[i].b); } int s = 0; for (int i = 2; i <= n; ++i) if (p[i] == i) s++; for (int i = 0; i < m; ++i) { if (e[i].a != 1 && e[i].b != 1) continue; int a = find_set(e[i].a), b = find_set(e[i].b); if (a != b) p[a] = b, add(e[i].a, e[i].b); } memset(used, false, sizeof(used)); for (int i = box[1]; ~i; i = ee[i].next) used[ee[i].to] = true; return s; } void DP(int x, int father) { for (int i = box[x]; ~i; i = ee[i].next) if (ee[i].to != father) { if (len[x][ee[i].to] > dp[x][0]) { dp[ee[i].to][0] = len[x][ee[i].to]; dp[ee[i].to][1] = x; dp[ee[i].to][2] = ee[i].to; } else { dp[ee[i].to][0] = dp[x][0]; dp[ee[i].to][1] = dp[x][1]; dp[ee[i].to][2] = dp[x][2]; } DP(ee[i].to, x); } } void inc_deg() { memset(dp, -1, sizeof(dp)); for (int i = box[1]; ~i; i = ee[i].next) DP(ee[i].to, 1); int id, mn = INF, i1, i2; for (int i = 0; i < rc; ++i) if (!used[r[i]] && len[1][r[i]] - dp[r[i]][0] < mn) { mn = len[1][r[i]] - dp[r[i]][0]; id = r[i]; i1 = dp[r[i]][1], i2 = dp[r[i]][2]; } add(1, id); used[id] = true; del(i1, i2), del(i2, i1); } void KDMST() { int M = MDMST(); if (k < M || k > rc) { puts("-1"); return; } for (int i = 0; i < k - M; ++i) inc_deg(); printf("%d\n", n - 1); for (int i = 1; i <= n; ++i) for (int j = box[i]; ~j; j = ee[j].next) if (i < ee[j].to) printf("%d ", id[make_pair(i, ee[j].to)]); printf("\n"); } int main() { init(); KDMST(); return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { int n, i; long long s[1200] = {0}; long long d[1200] = {0}; long long m[1200] = {0}; cin >> n; for (i = 0; i < n; i++) { cin >> s[i] >> d[i]; } m[0] = s[0]; long long sum = 0; for (i = 1; i < n; i++) { long long j = s[i]; while (j <= m[i - 1]) { j += d[i]; } m[i] = j; } cout << m[n - 1]; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6e1; long long r1, r2, r3, d; vector<long long> a; long long Memo[N][2]; int n; long long Calc(int i, bool CameBefore) { if (i == n) return -1ll * d; long long &Re = Memo[i][CameBefore]; if (Re + 1ll) return Re; Re = 0x7fffffffffffffff; if (CameBefore) { if (i == n - 1) Re = min(Re, Calc(i + 1, 0) + 1ll * r1 * a[i] + r3); else Re = min(Re, Calc(i + 1, 0) + 1ll * r1 * a[i] + r3 + d); Re = min(Re, Calc(i + 1, 0) + 1ll * r2 + r1 + d); Re = min(Re, Calc(i + 1, 0) + 1ll * r1 * a[i] + 2ll * r1 + d); } else { Re = min(Re, Calc(i + 1, 0) + 1ll * r1 * a[i] + r3 + d); Re = min(Re, Calc(i + 1, 1) + 1ll * r2 + r1 + 3 * d); Re = min(Re, Calc(i + 1, 1) + 1ll * r1 * a[i] + 2ll * r1 + 3ll * d); } return Re; } int main() { scanf("%d%lld%lld%lld%lld", &n, &r1, &r2, &r3, &d); a.resize(n); for (auto &i : a) scanf("%lld", &i); memset(Memo, -1, sizeof Memo); long long Ans = Calc(0, false); printf("%lld", Ans); return 0; }
15
#include <bits/stdc++.h> using namespace std; inline int read() { char c; int s = 0, t = 1; while (!isdigit(c = getchar())) if (c == '-') t = -1; do { s = s * 10 + c - '0'; } while (isdigit(c = getchar())); return s * t; } inline long long readl() { char c; long long s = 0; int t = 1; while (!isdigit(c = getchar())) if (c == '-') t = -1; do { s = s * 10 + c - '0'; } while (isdigit(c = getchar())); return s * t; } int n, m; int main() { int a, b, c, d; a = read(), b = read(), c = read(), d = read(); for (int i = c; i <= c * 2; i++) if (d <= i && d * 2 >= i) { if (d <= 2 * a && 2 * d >= 2 * a) continue; if (d <= 2 * b && 2 * d >= 2 * b) continue; printf("%d\n", 2 * a); printf("%d\n", 2 * b); printf("%d\n", i); return 0; } printf("%d\n", -1); }
5
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; const int INF = 1e5; int Rx = INF, Lx = -INF, Uy = INF, Dy = -INF; while (n--) { int x, y, l, u, r, d; cin >> x >> y >> l >> u >> r >> d; if (!l) Lx = max(Lx, x); if (!r) Rx = min(Rx, x); if (!u) Uy = min(Uy, y); if (!d) Dy = max(Dy, y); } if (Rx < Lx || Uy < Dy) cout << 0 << '\n'; else cout << 1 << ' ' << Lx << ' ' << Uy << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { int n, m, i; scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { int l, r; scanf("%d%d", &l, &r); a[l] += 1; a[r + 1] -= 1; } for (i = 1; i <= n; i++) { a[i] += a[i - 1]; if (a[i] != 1) { printf("%d %d", i, a[i]); return 0; } } printf("OK"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, a[10] = {0}; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) for (int j = 2; j <= s[i] - 48; j++) if (j == 4) a[2] += 2; else if (j == 6) { a[3]++; a[2]++; } else if (j == 8) a[2] += 3; else if (j == 9) a[3] += 2; else a[j]++; while (a[2] > 0) { if (a[7] > 0) { a[2] -= 4; a[3] -= 2; a[5]--; a[7]--; cout << 7; } else if (a[5] > 0) { a[2] -= 3; a[3]--; a[5]--; cout << 5; } else if (a[3] > 0) { a[2]--; a[3]--; cout << 3; } else { a[2]--; cout << 2; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 220000; struct Char { long long s; char c; Char(int _s = 0, char _c = 0) { s = _s; c = _c; } bool operator==(const Char tt) const { return s == tt.s && c == tt.c; } bool operator>=(const Char tt) const { return s >= tt.s && c == tt.c; } } s[N], t[N]; int n, m; int get(Char *s, int n) { int x; char ch; scanf("%d-%c", &x, &ch); s[0] = Char(x, ch); int cc = 0; for (int i = 1; i < n; i++) { scanf("%d-%c", &x, &ch); if (ch == s[cc].c) s[cc].s += x; else s[++cc] = Char(x, ch); } return cc; } int Next[N]; void get_Next(Char *s, int n) { Next[0] = -1; for (int i = 1, j = 0; i <= n;) if (j == -1 || s[i] == s[j]) Next[++i] = ++j; else j = Next[j]; } long long ind() { if (!m) { long long cc = 0; for (int i = 0; i <= n; i++) if (s[i].c == t[0].c && s[i].s >= t[0].s) cc += s[i].s - t[0].s + 1; return cc; } if (m == 1) { long long cc = 0; for (int i = 1; i <= n; i++) if (s[i - 1] >= t[0] && s[i] >= t[1]) cc++; return cc; } int i = 1, j = 1; long long cc = 0; while (i < n) if (j == 0 || s[i] == t[j]) { i++; j++; if (j == m) { if (s[i - m] >= t[j - m] && s[i] >= t[j]) cc++; j = Next[j]; } } else j = Next[j]; return cc; } int main() { while (~scanf("%d%d", &n, &m)) { n = get(s, n); m = get(t, m); get_Next(t, m); cout << ind() << endl; } return 0; }
13
#include <bits/stdc++.h> using namespace std; char s[505]; int main() { long long k, a, b; cin >> k >> a >> b; if ((a % k > 0 && b < k) || (b % k > 0 && a < k)) puts("-1"); else cout << a / k + b / k << endl; }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; const long long INF = 2e9, INFLL = 1e18, MOD = 1e9 + 7; void dfs(long long nod, vector<long long> &ar, vector<long long> &c, vector<long long> &ch, vector<long long> &M, vector<vector<long long>> &g) { c[nod] = ar[nod]; if (g[nod].size() == 0) ch[nod] = 1, M[nod] = ar[nod]; for (auto i : g[nod]) { dfs(i, ar, c, ch, M, g); c[nod] += c[i]; ch[nod] += ch[i]; M[nod] = max(M[nod], M[i]); } M[nod] = max(M[nod], (c[nod] + ch[nod] - 1) / ch[nod]); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; vector<vector<long long>> g(n); for (long long i = 1; i < n; i++) { long long a; cin >> a; g[a - 1].push_back(i); } vector<long long> ar(n); for (auto &i : ar) cin >> i; vector<long long> c(n), ch(n), M(n); dfs(0, ar, c, ch, M, g); cout << M[0]; return 0; }
11
#include <bits/stdc++.h> using namespace std; const int M = 1e6 + 15; const int Q = 1e9 + 7; int a[M]; vector<int> b; int main() { srand(time(NULL)); cin.tie(0); ios_base::sync_with_stdio(0); int n, s; cin >> n >> s; s--; for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 0; if (a[s]) { a[s] = 0, ans++; } int fr = 0; for (int i = 0; i < n; i++) { if (i != s && a[i] == 0) ans++, fr++; else b.push_back(a[i]); } sort(b.begin(), b.end()); int l = 1, r = (int)b.size() - 1; while (l <= r) { int k = max(0, b[l] - b[l - 1] - 1); while (l <= r && k) { if (fr) { fr--; } else { ans++; r--; } k--; } l++; } cout << ans << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; unsigned long long a, inv, nr1, nr2; int fr[11]; string b; vector<int> res; int main() { cin >> a >> b; while (a != 0) { nr1++; fr[a % 10]++; a /= 10; } if (b.size() > nr1) { for (int i = 9; i >= 0; i--) for (int j = 1; j <= fr[i]; j++) cout << i; return 0; } for (auto it : b) { int c = it - '0'; if (fr[c] == 0) { int ok = 0; for (int i = c - 1; i >= 0 + (res.size() == 0); i--) if (fr[i] != 0) { res.push_back(i); fr[i]--; ok = 1; break; } if (ok) { for (auto it : res) cout << it; for (int i = 9; i >= 0; i--) for (int j = 1; j <= fr[i]; j++) cout << i; return 0; } else { while (true) { for (int i = res.back() - 1; i >= 0; i--) { if (fr[i] != 0) { fr[res.back()]++; res.back() = i; fr[i]--; ok = 1; break; } } if (ok) break; fr[res.back()]++; res.pop_back(); } for (auto it : res) cout << it; for (int i = 9; i >= 0; i--) for (int j = 1; j <= fr[i]; j++) cout << i; return 0; } } else { res.push_back(c); fr[c]--; } inv /= 10; } for (auto it : res) cout << it; for (int i = 9; i >= 0; i--) for (int j = 1; j <= fr[i]; j++) cout << i; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5 + 123, inf = 1e9, mod = 1e9 + 7, LOG = 62; const long long INF = 1e18; long long dp[100][100]; void add(long long &x, long long y) { x += y; if (x >= mod) x -= mod; } long long rofl(long long x) { x >>= 1; x++; long long a = (x >> 1); long long b = ((x + 1) >> 1); a %= mod; b %= mod; return a * (a + 1) / 2 % mod + b * (b + 1) / 2 % mod; } int main() { int n; scanf("%d", &n); while (n--) { long long x; scanf("%lld", &x); printf("%lld\n", rofl(x) % mod); } }
16
#include <bits/stdc++.h> using namespace std; const long long kMod = 1000000007; int N, M, K; int C[15][15]; int f[15][15], g[15][15]; long long res; void read(); void soups_on(); void dfs(int, int, int); void add_answer(); inline bool is_valid(int x, int y, int i) { int t = f[x - 1][y] | f[x][y - 1]; return bool((t >> i & 1) ^ 1); } int main() { cin >> N >> M >> K; if (N + M - 1 > K) cout << "0\n"; else { read(); soups_on(); } return 0; } void read() { for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) cin >> C[i][j]; } void soups_on() { dfs(1, 1, 1); cout << res << "\n"; } void dfs(int x, int y, int lim) { if (x == N + 1) { add_answer(); return; } for (int i = 1; i <= lim; ++i) if (is_valid(x, y, i)) { f[x][y] = f[x - 1][y] | f[x][y - 1] | (1 << i); g[x][y] = i; dfs(y == M ? x + 1 : x, y == M ? 1 : y + 1, min(K, max(lim, i + 1))); } } void add_answer() { int mx[15], my[15], used_clr, fixed_clr; fill(&mx[0], &mx[15], 0); fill(&my[0], &my[15], 0); used_clr = 0; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) { used_clr = max(used_clr, g[i][j]); if (C[i][j]) { if (mx[C[i][j]] && mx[C[i][j]] != g[i][j]) return; if (my[g[i][j]] && my[g[i][j]] != C[i][j]) return; mx[C[i][j]] = g[i][j], my[g[i][j]] = C[i][j]; } } fixed_clr = count_if(mx, mx + 15, [](int i) { return i != 0; }); long long t = 1; for (int i = 0; i < used_clr - fixed_clr; ++i) t = t * (K - fixed_clr - i) % kMod; res = (res + t) % kMod; }
19
#include <bits/stdc++.h> using std::cerr; using std::endl; const int N = 2e5 + 233; int n, q, fa[N], cnt[N][26], sum[N], len[N], f[N][26], ban, id[N]; char C[N]; std::vector<int> G[N], T[N]; void dfs1(int x) { if (G[x].empty()) { id[x] = x; return; } for (int y : G[x]) { dfs1(y); len[x] = std::max(len[x], len[y] + 1); } for (int y : G[x]) if (len[x] != len[y] + 1) { while (q--) std::cout << "Fou" << '\n'; exit(0); } if (x != 1 && G[x].size() == 1) id[x] = id[G[x][0]]; else id[x] = x; } void dfs2(int x) { for (int y : T[x]) { fa[y] = x; dfs2(y); } } inline void update(int x, int c, int v) { cnt[x][c] += v; for (x = fa[x]; x; x = fa[x]) { ban -= sum[x] > len[x]; sum[x] -= f[x][c]; f[x][c] = 0; for (int y : T[x]) { f[x][c] = std::max(f[x][c], f[y][c] + cnt[y][c]); } sum[x] += f[x][c]; ban += sum[x] > len[x]; } } int main() { std::ios::sync_with_stdio(0); std::cin >> n >> q; for (int i = 2, x; i <= n; ++i) { std::cin >> x >> C[i]; G[x].push_back(i); } dfs1(1); for (int i = 1; i <= n; ++i) if (id[i] == i) { for (int y : G[i]) T[i].push_back(id[y]); } dfs2(1); for (int i = 1; i <= n; ++i) if (C[i] != '?') update(id[i], C[i] - 'a', 1); while (q--) { int x; std::cin >> x; if (C[x] != '?') update(id[x], C[x] - 'a', -1); std::cin >> C[x]; if (C[x] != '?') update(id[x], C[x] - 'a', 1); if (ban) std::cout << "Fou" << '\n'; else { std::cout << "Shi" << ' '; long long ans = 0; for (int i = 0; i < 26; ++i) { ans += (i + 1) * (len[1] - sum[1] + f[1][i]); } std::cout << ans << '\n'; } } return 0; }
22
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") #pragma warning(default : 4) using namespace std; int n, m, s1[1 << 21], s2[1 << 21], r, p2[1 << 21]; long long q[1 << 21]; int main() { memset(p2, -1, sizeof(p2)); scanf("%d %d", &n, &m); for (int i = 0; i < (int)(n); ++i) { scanf("%d", s1 + i); s1[n + i] = s1[i]; } for (int i = 0; i < (int)(m); ++i) { scanf("%d", s2 + i); s2[m + i] = s2[i]; p2[s2[i]] = i; } int ql, qr; ql = qr = 0; for (int i = 0; i < (int)(2 * n); ++i) { int &p = p2[s1[i]]; if (p == -1) { ql = qr = 0; continue; } q[qr] = q[qr - 1] / m * m + p; if (qr) while (q[qr] < q[qr - 1]) q[qr] += m; if (q[qr++] - q[ql] > m) ql++; r = max(r, qr - ql); } printf("%d\n", min(r, n)); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; int N, perm[110000], invperm[110000], pos[110000]; pair<int, int> nums[110000], tmp[110000]; bool lucky[110000], mark[110000]; bool islucky(int n) { while (n) { int d = n % 10; n /= 10; if (d != 4 && d != 7) return false; } return true; } void mark_cycle(int n) { mark[n] = true; int p = perm[n]; while (p != n) { mark[p] = true; p = perm[p]; } } int main() { scanf("%d", &N); int st = -1; for (int i = (0); i < (N); i++) { scanf("%d", &nums[i].first); nums[i].second = i; lucky[i] = islucky(nums[i].first); if (lucky[i]) st = i; } if (st == -1) { bool sorted = true; for (int i = (0); i < (N - 1); i++) if (nums[i].first > nums[i + 1].first) sorted = false; if (sorted) { cout << 0 << endl; } else { cout << -1 << endl; } } else { for (int i = (0); i < (N); i++) tmp[i] = nums[i]; vector<pair<int, int> > swaps; sort(nums, nums + N); for (int i = (0); i < (N); i++) { perm[nums[i].second] = i; invperm[i] = nums[i].second; } memset(mark, 0, sizeof(mark)); mark_cycle(st); for (int i = (0); i < (N); i++) { if (mark[i]) continue; mark_cycle(i); swaps.push_back(pair<int, int>(st + 1, i + 1)); swap(tmp[st], tmp[i]); st = i; } for (int i = (0); i < (N); i++) pos[tmp[i].second] = i; while (perm[tmp[st].second] != st) { int p = pos[invperm[st]]; swaps.push_back(pair<int, int>(p + 1, st + 1)); swap(tmp[p], tmp[st]); swap(pos[tmp[p].second], pos[tmp[st].second]); st = p; } printf("%d\n", int((swaps).size())); for (int i = (0); i < (int((swaps).size())); i++) printf("%d %d\n", swaps[i].first, swaps[i].second); } return 0; }
12
#include <bits/stdc++.h> using namespace std; bool vi[100010]; int ocu[100010]; int check(int n, bool ty) { if (ty) { for (int i = 2; i * i <= n; ++i) if (n % i == 0) { if (ocu[i] != 0) return ocu[i]; if (ocu[n / i] != 0) return ocu[n / i]; } if (ocu[n] != 0) return ocu[n]; } for (int i = 2; i * i <= n; ++i) if (n % i == 0) { if (ty) ocu[i] = n; else ocu[i] = 0; if (ty) ocu[n / i] = n; else ocu[n / i] = 0; } if (ty) ocu[n] = n; else ocu[n] = 0; vi[n] = ty; return -1; } int main() { int n, m, tmp; char str[10]; while (scanf("%d%d", &n, &m) == 2) { memset(vi, 0, sizeof(vi)); memset(ocu, 0, sizeof(ocu)); for (int i = 0; i < m; ++i) { scanf("%s%d", str, &tmp); if (str[0] == '+') { if (vi[tmp]) printf("Already on\n"); else { tmp = check(tmp, 1); if (tmp == -1) printf("Success\n"); else printf("Conflict with %d\n", tmp); } } else if (!vi[tmp]) printf("Already off\n"); else check(tmp, 0), printf("Success\n"); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long a[100005]; bool comp(long long a, long long b) { return a > b; } int main() { int t; cin >> t; while (t--) { memset(a, 0, sizeof(a)); int ta; long long tb; scanf("%d %lld", &ta, &tb); for (int i = 1; i <= ta; i++) { scanf("%lld", &a[i]); a[i] -= tb; } sort(a + 1, a + ta + 1, comp); long long k = 0; for (int i = 1; i <= ta; i++) { a[i] += a[i - 1]; if (a[i] >= 0) k = i; } cout << k << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 50; int fa[maxn]; string s[maxn]; vector<int> v[maxn]; int ans[maxn]; int in[maxn]; int vis[maxn]; int fin(int f) { return f == fa[f] ? f : fa[f] = fin(fa[f]); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n + m; ++i) fa[i] = i; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < m; ++j) { if (s[i][j] == '=') { int fi = fin(i), fj = fin(j + n); if (fi != fj) { fa[fj] = fi; } } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] == '>') { v[fin(n + j)].push_back(fin(i)); ++in[fin(i)]; } else if (s[i][j] == '<') { v[fin(i)].push_back(fin(n + j)); ++in[fin(n + j)]; } } } queue<int> q; for (int i = 0; i < n + m; ++i) { if (!in[fin(i)]) { ans[fin(i)] = 1; q.push(fin(i)); } } while (!q.empty()) { int tmp = q.front(); q.pop(); if (vis[tmp]) { continue; } vis[tmp] = 1; for (auto i : v[tmp]) { ans[fin(i)] = max(ans[(fin(i))], ans[fin(tmp)] + 1); --in[fin(i)]; if (!in[fin(i)]) q.push(fin(i)); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] == '=') { if (ans[fin(i)] != ans[fin(n + j)]) { cout << "No" << endl; return 0; } } if (s[i][j] == '<') { if (ans[fin(i)] >= ans[fin(j + n)]) { cout << "No" << endl; return 0; } } if (s[i][j] == '>') { if (ans[fin(i)] <= ans[fin(j + n)]) { cout << "No" << endl; return 0; } } } } cout << "Yes" << endl; for (int i = 0; i < n; ++i) { cout << ans[fin(i)] << " "; } cout << endl; for (int i = 0; i < m; ++i) { cout << ans[fin(i + n)] << " "; } cout << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; long long n, a[10001], cnt[10001], dp[10001]; signed main() { cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; cnt[i] = a[i]; } sort(cnt + 1, cnt + n + 1); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { dp[j] += abs(a[i] - cnt[j]); if (j > 1) { dp[j] = min(dp[j], dp[j - 1]); } } } cout << dp[n]; }
14
#include <bits/stdc++.h> using namespace std; long long int n, m, i, j, k; long long int sum = 0; long long int b_search(long long int b[], long long int lo, long long int hi, long long int x, long long int pos) { if (lo > hi) { return pos; } long long int mid = lo + (hi - lo) / 2; if (b[mid] >= x) { pos = mid; return b_search(b, lo, mid - 1, x, pos); } else { return b_search(b, mid + 1, hi, x, pos); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long int sum = 0; long long int pre[n]; for (i = 0; i < n; i++) { sum = sum + a[i]; pre[i] = sum; } cin >> m; while (m--) { long long int x; cin >> x; cout << b_search(pre, 0, n - 1, x, (n - 1) / 2) + 1 << endl; } }
4
#include <bits/stdc++.h> bool local = false; using namespace std; template <class T> string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> void print(T v) { cout << ((int)(v).size()) << "\n"; for (auto x : v) cout << x << ' '; cout << "\n"; }; template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> x) { return o << x.first << ' ' << x.second; } template <class T1, class T2> istream &operator>>(istream &o, pair<T1, T2> &x) { return o >> x.first >> x.second; } template <class T1, class T2> pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) { a.first += b.first; a.second += b.second; return a; } template <class T1, class T2> pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) { a.first -= b.first; a.second -= b.second; return a; } template <class T1, class T2> void operator+=(pair<T1, T2> &a, pair<T1, T2> b) { a.first += b.first; a.second += b.second; } template <class T1, class T2> void operator-=(pair<T1, T2> &a, pair<T1, T2> b) { a.first -= b.first; a.second -= b.second; } int nint() { int x; cin >> x; return x; } double getTime() { return clock() / (double)CLOCKS_PER_SEC; }; mt19937 rnd(0); int rand(int n) { return rnd() % n; } int rand(int l, int r) { return rnd() % (r - l + 1) + l; } const int mod = 1000000000 + 7; void initIO() { if (local) { freopen("input.txt", "r", stdin); srand((int)time(0)); rnd.seed((int)time(0)); } else { { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }; } } void solve(); int main() { initIO(); int tc = 1; for (int tt = 0; tt < (tc); tt++) { solve(); } if (local) cout << "\n" << "time = " << getTime() << "\n"; } int n; int m; void solve() { cin >> n; int s = 0; for (int i = 0; i < (n); i++) { int x; cin >> x; s += x; } int ans = 0; for (int i = 1; i <= 5; i++) ans += (s + i) % (n + 1) != 1; cout << ans; }
2
#include <bits/stdc++.h> using namespace std; struct HASH { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } static uint64_t splitmix64(const pair<long long, long long>& p) { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); long long x = p.first + FIXED_RANDOM, y = p.second + FIXED_RANDOM; x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; y += 0x9e3779b97f4a7c15; y = (y ^ (y >> 30)) * 0xbf58476d1ce4e5b9; y = (y ^ (y >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31) ^ y ^ (y >> 31); } size_t operator()(const pair<long long, long long>& p) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(p); } }; const int dx4[4] = {-1, 0, 1, 0}, dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int n, k; string str; map<string, long long> mp; long long returnnc2(long long val) { return (val * (val - 1LL)) / 2LL; } void solve() { int m, a, b, c; cin >> n; long long onebitset = 0, zero = 0; for (int i = 0; i < (int)n; i++) { cin >> str; int hash1[26] = {0}; for (int j = 0; j < (int)str.size(); j++) { hash1[str[j] - 'a']++; } string ans = ""; int cnt = 0; for (int j = 0; j < (int)26; j++) { hash1[j] = (hash1[j] & 1); if (hash1[j]) cnt++; ans += (hash1[j] + '0'); } if (cnt == 1) onebitset++; if (cnt == 0) zero++; mp[ans]++; } long long res = 0; for (auto i : mp) { res += (returnnc2(i.second)); string str = i.first; vector<int> vec; for (int i = 0; i < (int)26; i++) { if (str[i] == '1') vec.push_back(i); } for (int j = 0; j < (int)int((vec).size()); j++) { str[vec[j]] = '0'; res += ((i.second) * mp[str]); str[vec[j]] = '1'; } } cout << res << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int tc = 1; while (tc--) { solve(); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); if (((k % 3) != 0) && (n % 3 == 0)) printf("Bob\n"); else if ((k % 3) != 0 && (n % 3 != 0)) printf("Alice\n"); else { n = n % (k + 1); if ((n % 3 != 0) || n == k) printf("Alice\n"); else printf("Bob\n"); } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { short int n; int l; cin >> n >> l; vector<int> lanterns(n); for (int i = 0; i < n; i++) cin >> lanterns[i]; set<int> sett(lanterns.begin(), lanterns.end()); priority_queue<int> pq; int tempset = 0; short int aa = 0; for (set<int>::iterator it = sett.begin(); it != sett.end(); it++) { aa++; if (it == sett.begin() && *it != 0) pq.push(*it * 2); if (aa == sett.size() && *it != l) pq.push((l - *it) * 2); pq.push(*it - tempset); tempset = *it; } float resp = (float)pq.top() / 2; printf("%.10f\n", resp); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long inv(long long i) { if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod; } long long mod_mul(long long a, long long b) { a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod; } long long mod_add(long long a, long long b) { a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long ceil_div(long long a, long long b) { return a % b == 0 ? a / b : a / b + 1; } long long pwr(long long a, long long b) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long mx = 100000; vector<long long> mobius(1 + mx, 0); vector<bool> isprime(1 + mx, true); long long n, m; vector<long long> l, r; void sieve() { isprime[1] = false; mobius[1] = 1; vector<long long> primes; long long i, j; for (i = 2; i <= mx; i++) { if (isprime[i]) { primes.push_back(i); mobius[i] = -1; } for (j = 0; j < primes.size() and i * primes[j] <= mx; j++) { isprime[i * primes[j]] = false; if (i % primes[j] == 0) { mobius[i * primes[j]] = 0; break; } else { mobius[i * primes[j]] = mobius[i] * mobius[primes[j]]; } } } } long long solve(long long d) { long long M; M = m / d; vector<long long> dp(M + 1, 0); long long i, j; vector<long long> psum(M + 1, 0); dp[0] = 1; for (i = 0; i <= M; i++) psum[i] = 1; for (i = 1; i <= n; i++) { long long left, right; left = ceil_div(l[i], d); right = r[i] / d; if (left > right) return 0LL; for (j = 0; j <= M; j++) { dp[j] = ((j - left >= 0 ? psum[j - left] : 0LL) + mod - (j - right > 0 ? psum[j - right - 1] : 0LL)) % mod; } for (j = 0; j <= M; j++) { psum[j] = (j == 0 ? dp[0] : mod_add(dp[j], psum[j - 1])); } } long long ans = 0; for (i = 1; i <= M; i++) { ans = mod_add(ans, dp[i]); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t, i, j, ans, temp, sum; string sans; t = 1; while (t--) { sans = "NO"; ans = temp = sum = 0; cin >> n >> m; l.resize(n + 1); r.resize(n + 1); for (i = 1; i <= n; i++) { cin >> l[i] >> r[i]; } vector<long long> f(m + 1), g(m + 1); for (i = 1; i <= m; i++) { g[i] = solve(i); } for (i = m; i >= 1; i--) { f[i] = g[i] % mod; for (j = 2 * i; j <= m; j += i) { f[i] = (f[i] - f[j] + mod) % mod; } } ans = f[1]; cout << ans << "\n"; } return 0; }
14
#include <bits/stdc++.h> const int N = 20010; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const double e = 2.718281828459046; using namespace std; int g[110][110]; int w[110]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", w + i + 1); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); g[a][b] = g[b][a] = 1; } int ret = inf; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) for (int k = 1; k <= n; k++) { if (g[i][j] && g[i][k] && g[k][j] && ret > w[i] + w[j] + w[k]) ret = w[i] + w[j] + w[k]; } if (ret == inf) puts("-1"); else printf("%d\n", ret); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, f[1000010], c1, c2; int main() { scanf("%d", &n); for (int i = 1, x; i <= n; ++i) { scanf("%d", &x); if (x == 1) ++c1; else ++c2; } f[0] = f[1] = 1; for (int i = 2; i <= c1; ++i) { f[i] = ((long long)f[i - 1] + (long long)(i - 1) * f[i - 2]) % mod; } long long ans = f[c1]; for (int i = 1; i <= c2; ++i) { ans = ans * (c1 + i) % mod; } printf("%d\n", ans); }
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 100; const int INF = 1e9 + 10; int A[MAXN], x[MAXN]; int main() { int n; cin >> n; int mn = INF; int mx = -INF; for (int i = 0; i < n; i++) { cin >> x[i]; mn = min(mn, x[i]); mx = max(mx, x[i]); } if (mx - mn < 2) { cout << n << endl; for (int i = 0; i < n; i++) cout << x[i] << ' '; return 0; } int sum = 0; for (int i = 0; i < n; i++) { x[i] -= mn; sum += x[i]; A[x[i]]++; } int ans = INF, res = 0; for (int i = 0; i < min(n, sum) + 1; i++) { int cnt2 = (sum - i) / 2; if ((sum - i) % 2) continue; int cnt1 = n - i - cnt2; if (cnt1 < 0 || cnt2 < 0) continue; if (ans > (min(A[0], cnt1) + min(A[1], i) + min(A[2], cnt2))) { ans = (min(A[0], cnt1) + min(A[1], i) + min(A[2], cnt2)); res = i; } } cout << ans << endl; for (int i = 0; i < res; i++) cout << 1 + mn << ' '; for (int i = 0; i < (sum - res) / 2; i++) cout << 2 + mn << ' '; for (int i = 0; i < (n - res - (sum - res) / 2); i++) cout << mn << ' '; cout << endl; return 0; }
9
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<string> words; long long int n, m; cin >> n >> m; string mid = ""; for (long long int i = 0; i < n; ++i) { string s; cin >> s; string r = s; reverse(r.begin(), r.end()); if (r == s) { mid = r; } else { words.push_back(s); } } set<long long int> used; string ans = mid; for (long long int i = 0; i < words.size(); ++i) { if (used.count(i)) continue; for (long long int j = 0; j < words.size(); ++j) { if (i == j || used.count(j)) continue; string r = words[j]; reverse(r.begin(), r.end()); if (r == words[i]) { used.insert(i); used.insert(j); ans = words[i] + ans + words[j]; } } } cout << ans.size() << endl; cout << ans << endl; }
3
#include <bits/stdc++.h> using namespace std; inline int gi() { register int data = 0, w = 1; register char ch = 0; while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); return w * data; } void chkmax(int &x, int y) { if (x < y) x = y; } const int MAX_N = 3e5 + 5; struct Cloud { int l, r, c; } cld[MAX_N]; bool operator<(const Cloud &l, const Cloud &r) { return l.c < r.c; } struct Query { int id, t; } q[MAX_N]; bool operator<(const Query &l, const Query &r) { return l.t < r.t; } struct Node { int t, op, id; } a[MAX_N << 1]; int tot = 0; bool operator<(const Node &l, const Node &r) { return l.t < r.t; } int N, M, C, single[MAX_N], opt[MAX_N], ans[MAX_N]; map<int, int> cross[MAX_N]; set<int> s; set<int>::iterator ite; int Free, Top; int mx[MAX_N << 2]; void modify(int o, int l, int r, int pos, int v) { if (l == r) return (void)(mx[o] = v); int mid = (l + r) >> 1; if (pos <= mid) modify((o << 1), l, mid, pos, v); else modify((o << 1 | 1), mid + 1, r, pos, v); mx[o] = max(mx[(o << 1)], mx[(o << 1 | 1)]); } int find(int o, int l, int r) { if (l == r) return l; int mid = (l + r) >> 1; if (mx[(o << 1)] > mx[(o << 1 | 1)]) return find((o << 1), l, mid); else return find((o << 1 | 1), mid + 1, r); } int query(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return find(o, l, r); int mid = (l + r) >> 1, res = 0; if (ql <= mid) res = query((o << 1), l, mid, ql, qr); if (qr > mid) { int tmp = query((o << 1 | 1), mid + 1, r, ql, qr); if (single[tmp] > single[res]) res = tmp; } return res; } int sum(int x, int y) { if (x > y) swap(x, y); return single[x] + single[y] + cross[x][y]; } void solve() { int now = 0, pos = 1; for (int i = 1; i <= tot; i++) { int dlt = a[i].t - now; now = a[i].t; if (!s.size()) Free += dlt; else if (s.size() == 1) { ite = s.upper_bound(0); int x = *ite; single[x] += dlt; modify(1, 1, N, x, single[x]); opt[x] += dlt; int rem = C - cld[x].c; if (rem >= 0) { int val = single[x]; if (rem >= cld[1].c) { int l = 1, r = N, res = 1; while (l <= r) { int mid = (l + r) >> 1; if (cld[mid].c <= rem) res = mid, l = mid + 1; else r = mid - 1; } int ql = 1, qr = res; if (x == qr) --qr; if (x < qr) { ql = x + 1; if (ql <= qr) chkmax(val, sum(x, query(1, 1, N, ql, qr))); ql = 1, qr = x - 1; } if (ql <= qr) chkmax(val, sum(x, query(1, 1, N, ql, qr))); } chkmax(opt[x], val); chkmax(Top, opt[x]); } } else if (s.size() == 2) { ite = s.upper_bound(0); int x = *ite; ++ite; int y = *ite; if (cross[x].count(y) > 0) cross[x][y] += dlt; else cross[x][y] = dlt; if (cld[x].c + cld[y].c <= C) { chkmax(opt[x], sum(x, y)); chkmax(opt[y], sum(x, y)); chkmax(Top, opt[x]); } } while (pos <= M && Top + Free >= q[pos].t) ans[q[pos].id] = now - (Top + Free - q[pos].t), ++pos; if (pos > M) break; if (a[i].op == 1) s.insert(a[i].id); else s.erase(a[i].id); } } int main() { N = gi(), C = gi(); for (int i = 1; i <= N; i++) cld[i].l = gi(), cld[i].r = gi(), cld[i].c = gi(); sort(&cld[1], &cld[N + 1]); for (int i = 1; i <= N; i++) { a[++tot] = (Node){cld[i].l, 1, i}; a[++tot] = (Node){cld[i].r, -1, i}; } sort(&a[1], &a[tot + 1]); a[++tot] = (Node){(int)(2e9 + 7), 1, N + 1}; M = gi(); for (int i = 1; i <= M; i++) q[i].id = i, q[i].t = gi(); sort(&q[1], &q[M + 1]); solve(); for (int i = 1; i <= M; i++) printf("%d\n", ans[i]); return 0; }
26
#include <bits/stdc++.h> using namespace std; const int N = 1100; const long long mod = 1000000007LL; int n; int p[N]; long long sum[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; } for (int i = 1; i <= n; i++) { for (int j = p[i]; j < i; j++) { sum[i] = (sum[i] + sum[j]) % mod; } sum[i] = (sum[i] + 2) % mod; } long long res = 0; for (int i = 1; i <= n; i++) { res = (res + sum[i]) % mod; } cout << res; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 200000, M = 200000; int n, m, k, i, j, ii, jj, a[N]; signed long long int ans; vector<int> vec[M]; vector<pair<int, int> > v; int main() { cin >> n >> m; k = n / m; for (int i = 0; i < n; i += 1) cin >> a[i]; for (int i = 0; i < n; i += 1) vec[a[i] % m].push_back(i); for (int i = 0; i < 2 * m; i += 1) { ii = i % m; while (vec[ii].size() > k) { j = vec[ii].back(); vec[ii].pop_back(); v.push_back({j, ii}); } while (vec[ii].size() < k && v.size()) { j = v.back().first; jj = v.back().second; v.pop_back(); vec[ii].push_back(j); a[j] += (ii > jj ? ii - jj : m + ii - jj); ans += (ii > jj ? ii - jj : m + ii - jj); } } cout << ans << '\n'; for (int i = 0; i < n; i += 1) cout << a[i] << ' '; cout << '\n'; return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, m, ans; vector<int> sz; map<int, int> ch; struct DSU { vector<int> p; DSU(int n) : p() { p.resize(n); iota(p.begin(), p.end(), 0); } int root(int x) { return p[x] == x ? x : p[x] = root(p[x]); } void join(int x, int y) { p[root(y)] = root(x); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; DSU dsu(n + 1); sz.resize(n + 1); while (m--) { int x, y; cin >> x >> y; dsu.join(x, y); } cin >> m; while (m--) { int x, y; cin >> x >> y; x = dsu.root(x); y = dsu.root(y); if (x == y) ch[x] = 1; } for (int i = 1; i <= n; i++) sz[dsu.root(i)]++; for (int i = 1; i <= n; i++) { if (ch[i] == 1) sz[i] = 0; } for (int i = 1; i <= n; i++) { if (dsu.root(i) == i) ans = max(ans, sz[i]); } cout << ans; }
7
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace std { template <> struct hash<pair<int, int>> { size_t operator()(const pair<int, int> &x) const { long long P = 38923, Q = 109797901; return (size_t)((x.first * P + x.second) % Q); } }; }; // namespace std template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; template <typename T> void print(T t) { cout << t << endl; } template <typename T, typename... Args> void print(T t, Args... args) { cout << t << " "; print(args...); } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int _ = min((int)0, (int)t); _ < max((int)0, (int)t); _ += 1) { int n; cin >> n; vector<vector<int>> pieces; vector<set<int>> cake(n); queue<int> q; for (int i = min((int)0, (int)n - 2); i < max((int)0, (int)n - 2); i += 1) { int a, b, c; cin >> a >> b >> c; a--; b--; c--; pieces.push_back({a, b, c}); cake[a].insert(i); cake[b].insert(i); cake[c].insert(i); } for (int i = min((int)0, (int)n); i < max((int)0, (int)n); i += 1) { if ((int)(cake[i]).size() == 1) q.push(i); } vector<int> eat_order; while ((int)(q).size()) { int a = q.front(); q.pop(); if (!(int)(cake[a]).size()) continue; int p = *cake[a].begin(); for (auto &x : pieces[p]) { cake[x].erase(p); if ((int)(cake[x]).size() == 1) q.push(x); } eat_order.push_back(p); } int last = eat_order[(int)(eat_order).size() - 1]; vector<int> lp = pieces[last]; vector<int> l(n, -1), r(n, -1); l[lp[0]] = lp[2]; l[lp[1]] = lp[0]; l[lp[2]] = lp[1]; r[lp[0]] = lp[1]; r[lp[1]] = lp[2]; r[lp[2]] = lp[0]; for (int i = (int)(eat_order).size() - 2; i >= 0; i--) { vector<int> p = pieces[eat_order[i]]; int x, y, z; if (l[p[0]] == -1) { y = p[0]; x = p[1]; z = p[2]; if (l[x] == z) swap(x, z); } else if (l[p[1]] == -1) { y = p[1]; x = p[0]; z = p[2]; if (l[x] == z) swap(x, z); } else { y = p[2]; x = p[1]; z = p[0]; if (l[x] == z) swap(x, z); } r[x] = y; l[y] = x; r[y] = z; l[z] = y; } for (int i = r[0]; i != 0; i = r[i]) cout << (i + 1) << " "; cout << "1\n"; for (auto &x : eat_order) cout << (x + 1) << " "; cout << "\n"; } return 0; }
16
#include <bits/stdc++.h> const int N = 100010; struct node { int l, r; double sum, add, mul; } a[N << 2]; int n, q; int read() { int d = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') d = d * 10 + c - 48, c = getchar(); return d * f; } void pushup(int i) { a[i].sum = a[i << 1].sum + a[i << 1 | 1].sum; } void pushdown(int i) { int i1 = i << 1, i2 = i1 | 1; a[i1].sum = a[i1].sum * a[i].mul + a[i].add * (a[i1].r - a[i1].l + 1); a[i1].mul *= a[i].mul; a[i1].add = a[i1].add * a[i].mul + a[i].add; a[i2].sum = a[i2].sum * a[i].mul + a[i].add * (a[i2].r - a[i2].l + 1); a[i2].mul *= a[i].mul; a[i2].add = a[i2].add * a[i].mul + a[i].add; a[i].add = 0; a[i].mul = 1; } void build(int i, int l, int r) { a[i].l = l; a[i].r = r; a[i].mul = 1; if (l == r) { a[i].sum = read(); return; } int mid = (l + r) >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); pushup(i); } double query(int i, int l, int r) { if (l <= a[i].l && a[i].r <= r) return a[i].sum; pushdown(i); int mid = (a[i].l + a[i].r) >> 1; if (r <= mid) return query(i << 1, l, r); else if (l > mid) return query(i << 1 | 1, l, r); else return query(i << 1, l, mid) + query(i << 1 | 1, mid + 1, r); } void Mul(int i, int l, int r, double x) { if (l <= a[i].l && a[i].r <= r) { a[i].sum *= x; a[i].add *= x; a[i].mul *= x; return; } pushdown(i); int mid = (a[i].l + a[i].r) >> 1; if (r <= mid) Mul(i << 1, l, r, x); else if (l > mid) Mul(i << 1 | 1, l, r, x); else Mul(i << 1, l, mid, x), Mul(i << 1 | 1, mid + 1, r, x); pushup(i); } void Add(int i, int l, int r, double x) { if (l <= a[i].l && a[i].r <= r) { a[i].sum += x * (a[i].r - a[i].l + 1); a[i].add += x; return; } pushdown(i); int mid = (a[i].l + a[i].r) >> 1; if (r <= mid) Add(i << 1, l, r, x); else if (l > mid) Add(i << 1 | 1, l, r, x); else Add(i << 1, l, mid, x), Add(i << 1 | 1, mid + 1, r, x); pushup(i); } int main() { n = read(); q = read(); build(1, 1, n); while (q--) { int op = read(), l = read(), r = read(); if (op == 1) { int L = read(), R = read(), len1 = r - l + 1, len2 = R - L + 1; double sum1 = query(1, l, r), sum2 = query(1, L, R); Mul(1, l, r, (len1 - 1) * 1.0 / len1); Mul(1, L, R, (len2 - 1) * 1.0 / len2); Add(1, l, r, 1.0 / len1 * sum2 / len2); Add(1, L, R, 1.0 / len2 * sum1 / len1); } else printf("%.6lf\n", query(1, l, r)); } return 0; }
15
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long power(long long a, long long n) { a %= 1000000007; if (n == 1) return a; if (n == 0) return 1; if (n % 2) return (a * (power((a * a) % 1000000007, n / 2) % 1000000007)) % 1000000007; return power((a * a) % 1000000007, n / 2) % 1000000007; } const long long inf = (long long)1e18; long long inverse(long long x) { return power(x, 1000000007 - 2) % 1000000007; } void solve() { long long n, d; cin >> n >> d; if (d > (n * (n - 1)) / 2) { cout << "NO\n"; return; } vector<long long> par(n + 5), h(n + 5), cc(n + 5); long long cur = (n * (n - 1)) / 2; for (long long i = 2; i < n + 1; ++i) par[i] = i - 1, h[i] = i - 1, cc[i - 1] = 1; set<pair<long long, long long> > pq; pq.insert({-n + 1, n}); while (cur > d) { long long x = (*pq.begin()).second, height = -1 * (*pq.begin()).first; pq.erase(pq.begin()); if (height > h[x]) continue; long long mxi = -1; for (long long i = 1; i < n + 1; ++i) { if (cc[i] < 2 && h[i] < h[x] && cur + h[i] + 1 - h[x] >= d) { if (mxi == -1 || h[mxi] > h[i]) { mxi = i; } } } if (mxi == -1) break; cur += h[mxi] - h[x] + 1; cc[par[x]]--; pq.insert({-h[par[x]], par[x]}); par[x] = mxi; cc[mxi]++; h[x] = h[mxi] + 1; } if (cur != d) { cout << "NO\n"; return; } cout << "YES\n"; for (long long i = 2; i < n + 1; ++i) cout << par[i] << " "; cout << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long T = 1; cin >> T; for (long long t = 1; t < T + 1; ++t) { solve(); } return 0; }
14
#include <bits/stdc++.h> using namespace std; const int N = 5010, P = 37, M = 1e9 + 207; int h[N], p[N]; int hs(int l, int r) { int prv = ((l ? h[l - 1] : 0) * 1LL * p[r - l + 1]) % M; return (h[r] + M - prv) % M; } unordered_map<int, int> all; int f[N]; char can[N][N]; int main() { p[0] = 1; all.reserve(5e5); all.max_load_factor(228); for (int i = 1; i < N; ++i) p[i] = (p[i - 1] * 1LL * P) % M; int n, a, b; cin >> n >> a >> b; string s; cin >> s; for (int i = 0; i < n; ++i) h[i] = ((i ? h[i - 1] : 0) * 1LL * P + s[i]) % M; for (int i = 0; i < n; ++i) f[i] = (i + 1) * a; f[0] = a; for (int len = 1; len <= n; ++len) { for (int i = 0; i + len <= n; ++i) { int H = hs(i, i + len - 1); auto it = all.find(H); if (it == all.end()) all[H] = i + len - 1; else { int x = it->second; if (i > x) can[i][i + len - 1] = 1; } } all.clear(); } for (int i = 0; i < n - 1; ++i) { f[i + 1] = min(f[i + 1], f[i] + a); int l = 1, r = n - i; while (l < r) { int mid = (l + r) >> 1; if (can[i + 1][i + mid]) l = mid + 1; else r = mid; } for (int j = i + 1; j < i + l; ++j) f[j] = min(f[j], f[i] + b); } cout << f[n - 1]; }
13
#include <bits/stdc++.h> using namespace std; const long long INF = 9223372036854775807LL; struct Point { long long x, y; Point() {} Point(const long long _x, const long long _y) : x(_x), y(_y) {} }; struct CmpX { bool operator()(const Point &a, const Point &b) const { return a.x != b.x ? a.x < b.x : a.y < b.y; } }; struct CmpY { bool operator()(const Point &a, const Point &b) const { return a.y != b.y ? a.y < b.y : a.x < b.x; } }; multiset<Point, CmpX> XM; multiset<Point, CmpY> YM; long long ans; void UpdateAns() { auto itx = XM.end(); itx--; long long x = (*itx).x - (*XM.begin()).x; x = (x + 1LL) / 2LL; if (x == 0LL) x++; auto ity = YM.end(); ity--; long long y = (*ity).y - (*YM.begin()).y; y = (y + 1LL) / 2LL; if (y == 0LL) y++; if (ans > x * y) ans = x * y; } long long N, K; void UpdateAns(const long long l, const long long r, const long long u, const long long d) { vector<Point> tmp; for (int i = 0; i < l; i++) { auto it = XM.begin(); Point p = *it; XM.erase(XM.find(p)), YM.erase(YM.find(p)); tmp.push_back(p); } for (int i = 0; i < r; i++) { auto it = XM.end(); it--; Point p = *it; XM.erase(XM.find(p)), YM.erase(YM.find(p)); tmp.push_back(p); } for (int i = 0; i < u; i++) { auto it = YM.begin(); Point p = *it; XM.erase(XM.find(p)), YM.erase(YM.find(p)); tmp.push_back(p); } for (int i = 0; i < d; i++) { auto it = YM.end(); it--; Point p = *it; XM.erase(XM.find(p)), YM.erase(YM.find(p)); tmp.push_back(p); } UpdateAns(); for (int i = 0; i < tmp.size(); i++) { const Point &p = tmp[i]; XM.insert(p), YM.insert(p); } assert(XM.size() == N && YM.size() == N); } int main() { scanf("%I64d%I64d", &N, &K); for (int i = 0; i < N; i++) { static long long x1, y1, x2, y2; scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2); XM.insert(Point(x1 + x2, y1 + y2)); YM.insert(Point(x1 + x2, y1 + y2)); } ans = INF; UpdateAns(); for (int l = 0; l <= K; l++) { for (int r = 0; l + r <= K; r++) { for (int u = 0; l + r + u <= K; u++) { for (int d = 0; l + r + u + d <= K; d++) { UpdateAns(l, r, u, d); } } } } printf("%I64d\n", ans); return 0; }
15
#include <bits/stdc++.h> typedef struct link { char element; struct link* next; } Link; typedef struct stack { Link* top; int size; } Stack; Link* create_link(char it, Link* nextval) { Link* n = (Link*)malloc(sizeof(Link)); n->element = it; n->next = nextval; return n; } Stack* create_stack() { Stack* s = (Stack*)malloc(sizeof(Stack)); s->top = NULL; s->size = 0; return s; } void push(Stack* s, char it) { s->top = create_link(it, s->top); s->size++; } void pop(Stack* s) { if (s->top == NULL) return; s->top = s->top->next; s->size--; } char topValue(Stack* s) { if (s->top == NULL) return -1; return s->top->element; } int main() { char braket; int count = 0; Stack* atual; int open = 0, close = 0; char retornado; int tick = 0; atual = create_stack(); scanf("%c", &braket); if (braket == '}' || braket == ']' || braket == '>' || braket == ')') printf("Impossible"); else { while (braket != '\n') { if (braket == '{' || braket == '[' || braket == '<' || braket == '(') { push(atual, braket); open++; } else { close++; retornado = topValue(atual); if (retornado != '[' && retornado != '{' && retornado != '(' && retornado != '<') close = close + 352; if ((retornado == '{' && braket == '}') || (retornado == '[' && braket == ']') || (retornado == '<' && braket == '>') || (retornado == '(' && braket == ')')) pop(atual); else { count++; pop(atual); } } scanf("%c", &braket); } if (open != close) printf("Impossible"); else printf("%d", count); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int a[101], b[101] = {0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, c = 0; cin >> n; ; for (int i = 1; i <= n; i++) cin >> a[i]; ; if (n == 1) { cout << "NO" << endl; return 0; } sort(a + 1, a + n + 1); int mn = a[1]; for (int i = 1; i <= n; i++) if (a[i] == mn) a[i] = 101; sort(a + 1, a + n + 1); int ch = 0; for (int i = 1; i <= n; i++) if (a[i] == 101) ch++; if (ch == n) { cout << "NO" << endl; return 0; } cout << a[1] << endl; ; return 0; ; }
0
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; long long n, m, t, k; string s; long long a[500005]; vector<long long> ans; long long lower(int num) { int l = 0, r = ans.size() - 1; while (l < r - 1) { int mid = (l + r) / 2; if (ans[mid] > num) { r = mid; } else { l = mid; } } return ans[l]; } int pos[500005]; long long md(long long a) { a %= n; while (a >= n) a -= n; while (a < 0) a += n; return a; } int main() { ios::sync_with_stdio(0); cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; pos[i] = 0; } int num = 0; for (int i = 0; i < n; i++) { int p = md(i + a[i]); if (pos[p] == 0) { num++; } pos[p] = 1; } if (num == n) { cout << "YES\n"; } else { cout << "NO\n"; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; char a[4], b[4]; char x[4]; int main() { for (int i = 0; i < 4; i++) scanf(" %c", a + i); for (int i = 0; i < 4; i++) scanf(" %c", b + i); swap(a[2], a[3]); swap(b[2], b[3]); for (int i = 0; i < 4; i++) x[i] = a[i]; for (int i = 0; i < 4; i++) { if (a[i] == 'A') { for (int j = 0; j < 4; j++) a[j] = x[(i + j) % 4]; break; } } for (int i = 0; i < 4; i++) x[i] = b[i]; for (int i = 0; i < 4; i++) { if (b[i] == 'A') { for (int j = 0; j < 4; j++) b[j] = x[(i + j) % 4]; break; } } int i = 1; if (a[i] == 'X') i++; int j = 1; if (b[j] == 'X') j++; if (a[i] == b[j]) printf("YES\n"); else printf("NO\n"); }
4
#include <bits/stdc++.h> using namespace std; int Sig(double a) { return a < -1e-8 ? -1 : (a > 1e-8); } double sx, sy; struct Point { double x, y; double angle; Point() {} Point(double x0, double y0) : x(x0), y(y0) {} void in() { scanf("%lf %lf", &x, &y); angle = atan2(y - sy, x - sx); } void out() { printf("%.4f %.4f\n", x + sx, y + sy); } double operator*(Point pt) { return x * pt.y - y * pt.x; } Point operator-(Point pt) { return Point(x - pt.x, y - pt.y); } Point operator+(Point pt) { return Point(x + pt.x, y + pt.y); } bool operator<(const Point &p) const { return angle < p.angle; } } p[10]; set<Point> hull; set<Point>::iterator ll; Point lefthand(Point o) { if (hull.count(o)) return o; ll = hull.lower_bound(o); if (ll != hull.begin()) return *(--ll); else return *(--hull.end()); } Point righthand(Point o) { ll = hull.upper_bound(o); if (ll == hull.end()) return *hull.begin(); else return *ll; } bool Is_Inside(Point o) { Point l = lefthand(o); Point r = righthand(o); return Sig((r - l) * (o - l)) >= 0; } void addPoint(Point o) { if (Is_Inside(o)) return; while (true) { Point r1 = righthand(o); hull.erase(r1); Point r2 = righthand(o); if (Sig((r1 - o) * (r2 - r1)) > 0) { hull.insert(r1); break; } } while (true) { Point l1 = lefthand(o); hull.erase(l1); Point l2 = lefthand(o); if (Sig((l1 - o) * (l2 - l1)) < 0) { hull.insert(l1); break; } } hull.insert(o); } double rands[10]; void init() { srand(time(NULL)); for (int i = 0; i < 3; i++) rands[i] = 1.0 * (rand() % 1000000) / 1000000.0; } int main() { init(); int n, flag; scanf("%d", &n); sx = sy = 0; double sum = 0; for (int i = 0; i < 3; i++) { scanf("%d", &flag); p[i].in(); sum += rands[i]; } for (int i = 0; i < 3; i++) sx += p[i].x * rands[i], sy += p[i].y * rands[i]; sx /= sum; sy /= sum; for (int i = 0; i < 3; i++) p[i].angle = atan2(p[i].y - sy, p[i].x - sx); hull.clear(); for (int i = 0; i < 3; i++) hull.insert(p[i]); Point o; for (int i = 3; i < n; i++) { scanf("%d", &flag); o.in(); if (flag == 1) addPoint(o); else { if (Is_Inside(o)) printf("YES\n"); else printf("NO\n"); } } return 0; }
19
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; pair<int, int> arr[n]; for (int i = 0; i < n; i++) { int a; cin >> a; arr[i] = {a, i}; } sort(arr, arr + n); set<int> st[k + 1]; vector<int> ans(n, 0); int last = 1; for (int i = 0; i < n; i++) { if (st[last].find(arr[i].first) == st[last].end()) { st[last].insert(arr[i].first); ans[arr[i].second] = last; last = (last + 1) % k; if (last == 0) { last = k; } } } vector<bool> vis(k + 1, 0); for (int i = 0; i < n; i++) { if (st[ans[i]].size() > st[k].size() && !vis[ans[i]]) { vis[ans[i]] = 1; ans[i] = 0; } } for (int i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (auto& x : a) cin >> x; bool check = 0; for (int i = 0; i < n - 1; i++) if (a[i] != a[i + 1]) check = 1; if (check == 0) cout << "NO" << '\n'; else { cout << "YES" << '\n'; int fst = 0; while (a[0] == a[fst]) fst++; for (int i = 1; i < n; i++) { if (a[i] != a[0]) { cout << 1 << " " << i + 1 << '\n'; } else { if (i != fst) { cout << fst + 1 << " " << i + 1 << '\n'; } } } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; inline bool equal(double _x, double _y) { return _x > _y - 1e-9 && _x < _y + 1e-9; } map<string, string> mp; int n; string str; int main() { scanf("%d", &n); mp["purple"] = "Power"; mp["green"] = "Time"; mp["blue"] = "Space"; mp["orange"] = "Soul"; mp["red"] = "Reality"; mp["yellow"] = "Mind"; for (int i = 0; i < n; i++) { cin >> str; mp.erase(str); } printf("%d\n", ((int)mp.size())); for (auto i : mp) { cout << i.second << '\n'; } }
0
#include <bits/stdc++.h> using namespace std; double rf, re, rs, df, de, maxi, dp[2][20]; int pf[22], pe[22], ps[22], f, e, s; double memoize(int cho, int dis) { if (dp[cho][dis] != -1) return dp[cho][dis]; double ans = 0; pair<double, double> sta = make_pair(-1 * rs, rs); pair<double, double> endi; if (cho == 0) endi = make_pair(dis - rf, dis + rf); else endi = make_pair(dis - re, dis + re); if (endi.first <= sta.first && endi.second >= sta.second) ans = (2 * rs); else if (endi.first >= sta.first && endi.second <= sta.second) ans = (endi.second - endi.first); else if (endi.first >= sta.first && endi.first <= sta.second && endi.second >= sta.second) ans = (sta.second - endi.first); else if (sta.first >= endi.first && sta.first <= endi.second && sta.second >= endi.second) ans = (endi.second - sta.first); return dp[cho][dis] = ans; } void calc() { double ans = 0, fire = 0, elec = 0; for (int i = int(1); i <= int(s); i++) { for (int j = int(1); j <= int(f); j++) { fire += memoize(0, abs(pf[j] - ps[i])); } for (int j = int(1); j <= int(e); j++) { elec += memoize(1, abs(pe[j] - ps[i])); } } fire += f * 2 * rf; elec += e * 2 * re; ans = fire * df + de * elec; maxi = max(maxi, ans); } void recurse(int nf, int ne, int ns, int poi) { if (nf == 0 && ne == 0 && ns == 0) { calc(); return; } if (nf - 2 >= 0) { pf[nf] = pf[nf - 1] = poi; recurse(nf - 2, ne, ns, poi + 1); } if (ne - 2 >= 0) { pe[ne] = pe[ne - 1] = poi; recurse(nf, ne - 2, ns, poi + 1); } if (ns - 2 >= 0) { ps[ns] = ps[ns - 1] = poi; recurse(nf, ne, ns - 2, poi + 1); } if (nf - 1 >= 0 && ne - 1 >= 0) { pf[nf] = pe[ne] = poi; recurse(nf - 1, ne - 1, ns, poi + 1); } if (nf - 1 >= 0 && ns - 1 >= 0) { pf[nf] = ps[ns] = poi; recurse(nf - 1, ne, ns - 1, poi + 1); } if (ne - 1 >= 0 && ns - 1 >= 0) { pe[ne] = ps[ns] = poi; recurse(nf, ne - 1, ns - 1, poi + 1); } if (ne == 1 && ns == 0 && nf == 0) { pe[ne] = poi; recurse(nf, ne - 1, ns, poi + 1); } if (ns == 1 && ne == 0 && nf == 0) { ps[ns] = poi; recurse(nf, ne, ns - 1, poi + 1); } if (nf == 1 && ns == 0 && ne == 0) { pf[nf] = poi; recurse(nf - 1, ne, ns, poi + 1); } } int main() { maxi = 0; int nf, ne, ns; scanf("%d", &f); scanf("%d", &e); scanf("%d", &s); scanf("%lf%lf%lf", &rf, &re, &rs); rf = sqrt(rf * rf - 1); re = sqrt(re * re - 1); rs = sqrt(rs * rs - 1); scanf("%lf%lf", &df, &de); for (int i = int(0); i <= int(1); i++) for (int j = int(0); j <= int(19); j++) dp[i][j] = -1; recurse(f, e, s, 0); printf("%0.8lf\n", maxi); return 0; }
18
#include <bits/stdc++.h> using namespace std; array<vector<int>, 2001> adj; bool vis[2001]; bool like[2001][2001]; vector<int> group; void dfs(int f) { vis[f] = true; group.push_back(f); for (auto to : adj[f]) if (like[f][to] && !vis[to]) dfs(to); } int main() { adj.fill({}); memset(like, true, sizeof like); int n, k, m, i, j; cin >> n >> k; while (k--) { cin >> i >> j; adj[i].push_back(j); adj[j].push_back(i); } cin >> m; while (m--) { cin >> i >> j; adj[i].push_back(j); adj[j].push_back(i); like[i][j] = like[j][i] = false; } int mx = 0; for (int i = 1; i <= n; i++) { group.clear(); memset(vis, false, sizeof vis); dfs(i); bool b = true; for (auto f1 : group) for (auto f2 : group) if (f1 != f2 && !like[f1][f2]) { b = false; break; } if (b) mx = max(mx, int(group.size())); } cout << mx; return 0; }
7
#include <bits/stdc++.h> using namespace std; int nums[101]; int n, k; int main() { int i; scanf("%d %d", &n, &k); for (i = 1; i <= n; i++) { scanf("%d", &nums[i]); } sort(nums + 1, nums + 1 + n); reverse(nums + 1, nums + 1 + n); printf("%d\n", nums[k]); return 0; }
1
#include <bits/stdc++.h> const long long int MOD = 1e9 + 7; const long long int INF = 1011111111; const long long int LLINF = 1000111000111000111LL; const long double EPS = 1e-10; const long double PI = 3.14159265358979323; using namespace std; long long int power(long long int x, long long int n) { if (n == 0) return 1; else if (n % 2 == 0) return power((x * x), n / 2); else return x * power((x * x), (n - 1) / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, i, j, k; string s; cin >> n; cin >> s; char ch[200505]; memset(ch, '.', sizeof ch); long long int id = 0; ch[0] = s[0]; long long int ans = 0; for (i = 1; i < n; i++) { if (id % 2 == 0 && s[i] == ch[id]) { ans++; } else { ch[++id] = s[i]; } } if (id % 2 == 0) { ch[id] = '.'; ans++; } cout << ans << '\n'; for (i = 0; i <= id + 10; i++) { if (ch[i] != '.') cout << ch[i]; } }
5
#include <bits/stdc++.h> using namespace std; /* Utilities */ #define fr(i, n) for (int i = 0; i < n; i++) #define forin(i, j, k, in) for (int i = j; i < k; i += in) #define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++) #define in(a, b, c) b <= a &&a <= c #define clr(x) memset(x, 0, sizeof(x)) #define endln "\n" #define pb push_back #define ll long long #define ul unsigned long /* Constants */ #define INF (int)1e9 #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 void solve() { int n, x = 0, y, z; cin >> n; fr(i, n) { cin >> y; z = x&(~y); cout << z << " "; x = y^z; } cout << endln; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) solve(); }
5
#include <bits/stdc++.h> using ll = long long; using ull = unsigned long long; using ld = long double; using PLL = std::pair<ll, ll>; template <class T> using prique = std::priority_queue<T, std::vector<T>, std::greater<T>>; template <class T> class infinity { public: static constexpr T value = std::numeric_limits<T>::max() / 2; static constexpr T mvalue = std::numeric_limits<T>::min() / 2; static constexpr T max = std::numeric_limits<T>::max(); static constexpr T min = std::numeric_limits<T>::min(); }; template <class T> constexpr T infinity<T>::value; template <class T> constexpr T infinity<T>::mvalue; template <class T> constexpr T infinity<T>::max; template <class T> constexpr T infinity<T>::min; constexpr ll inf = infinity<ll>::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; template <class T, class U> std::ostream& operator<<(std::ostream& ost, const std::pair<T, U>& p) { return ost << p.first << ' ' << p.second; } template <class T, class U> std::istream& operator>>(std::istream& ist, std::pair<T, U>& p) { return ist >> p.first >> p.second; } template <class Container, typename std::enable_if<!std::is_same< Container, std::string>::value>::type* = nullptr> auto operator<<(std::ostream& ost, const Container& cont) -> decltype(cont.begin(), cont.end(), ost) { for (auto itr = cont.begin(); itr != cont.end(); ++itr) { if (itr != cont.begin()) ost << ' '; ost << *itr; } return ost; } template <class Container, typename std::enable_if<!std::is_same< Container, std::string>::value>::type* = nullptr> auto operator>>(std::istream& ist, Container& cont) -> decltype(cont.begin(), cont.end(), ist) { for (auto itr = cont.begin(); itr != cont.end(); ++itr) ist >> *itr; return ist; } template <class T, class U> inline constexpr bool chmin(T& a, const U& b) noexcept { return a > b ? a = b, true : false; } template <class T, class U> inline constexpr bool chmax(T& a, const U& b) noexcept { return a < b ? a = b, true : false; } inline ll gcd(ll a, ll b) noexcept { while (b) { const ll c = a; a = b; b = c % b; } return a; } inline ll lcm(ll a, ll b) noexcept { return a / gcd(a, b) * b; } inline bool is_prime(ll N) noexcept { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector<ll> prime_factor(ll N) noexcept { std::vector<ll> res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline ll my_pow(ll a, ll b) noexcept { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline ll mod_pow(ll a, ll b, ll mod) noexcept { a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } PLL extGCD(ll a, ll b) noexcept { if (b == 0) return PLL{1, 0}; PLL p = extGCD(b, a % b); std::swap(p.first, p.second); p.second -= p.first * (a / b); if (p.first < 0) { p.first += b; p.second -= a; } return p; } ll mod_inv(ll a, ll mod) noexcept { const PLL p = extGCD(a, mod); assert(p.first * a + p.second * mod == 1); return p.first; } PLL ChineseRemainder(ll b1, ll m1, ll b2, ll m2) noexcept { const PLL p = extGCD(m1, m2); const ll g = p.first * m1 + p.second * m2; const ll l = m1 / g * m2; if ((b2 - b1) % g != 0) return PLL{-1, -1}; const ll x = (b2 - b1) / g * p.first % (m2 / g); return {(x * m1 + b1 + l) % l, l}; } PLL ChineseRemainders(const std::vector<ll>& b, const std::vector<ll>& m) noexcept { PLL res{0, 1}; for (ll i = 0; i < (ll)(b.size()); ++i) { res = ChineseRemainder(res.first, res.second, b[i], m[i]); if (res.first == -1) return res; } return res; } template <class F> class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward<F>(f_)) {} template <class... Args> constexpr auto operator()(Args&&... args) const -> decltype(f(*this, std::forward<Args>(args)...)) { return f(*this, std::forward<Args>(args)...); } }; template <class F> inline constexpr RecLambda<F> rec_lambda(F&& f) { return RecLambda<F>(std::forward<F>(f)); } template <class T, class Arg> constexpr std::vector<T> make_vec(int n, Arg&& arg) { return std::vector<T>(n, arg); } template <class T, class... Args> constexpr auto make_vec(int n, Args&&... args) -> std::vector<decltype(make_vec<T>(args...))> { return std::vector<decltype(make_vec<T>(args...))>( n, make_vec<T>(std::forward<Args>(args)...)); } inline int popcnt(ull x) { x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } template <class T> class presser : public std::vector<T> { private: using Base = std::vector<T>; public: using Base::Base; presser(const std::vector<T>& vec) : Base(vec) {} void push(const std::vector<T>& vec) { int n = this->size(); this->resize(n + vec.size()); std::copy((vec).begin(), (vec).end(), this->begin() + n); } int build() { std::sort(this->begin(), this->end()); this->erase(std::unique(this->begin(), this->end()), this->end()); return this->size(); } int get_index(const T& val) const { return static_cast<int>(std::lower_bound(this->begin(), this->end(), val) - this->begin()); } std::vector<int> pressed(const std::vector<T>& vec) const { std::vector<int> res(vec.size()); for (ll i = 0; i < (ll)(vec.size()); ++i) res[i] = this->get_index(vec[i]); return res; } void press(std::vector<T>& vec) const { static_assert(std::is_integral<T>::value, "cannot convert from int type"); for (ll i = 0; i < (ll)(vec.size()); ++i) vec[i] = this->get_index(vec[i]); } }; using namespace std; int main() { int q; cin >> q; for (ll REP_COUNTER_0 = 0; REP_COUNTER_0 < (ll)(q); ++REP_COUNTER_0) { int n; cin >> n; for (ll i = 0; i < (ll)(n); ++i) cout << i + 2 << " \n"[i == n - 1]; } }
0
#include <bits/stdc++.h> using namespace std; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { c_p_c(); long long p, n; vector<long long> v; cin >> n; for (long long i = 0; i < n; i++) { cin >> p; v.push_back(p); } cout << count(v.begin(), v.end(), 1) << "\n"; vector<long long> vect; for (long long i = 0; i < n; i++) { if (v[i] == 1 && i != 0) { cout << vect.size() << " "; vect.clear(); } vect.push_back(v[i]); } cout << vect.size(); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int a[int(1e5 + 10)], t = 0, k; long long int fun(int v1, int v2, int p1, int p2, int it, int f, int i) { long long int t1 = 0; if (v1 <= it) { if (v2 > a[i - 1]) { if (p2 >= f) { if (p1 < a[i + k]) { t1 = (long long int)(min(v2, it) - max(a[i - 1] + 1, v1) + 1) * (long long int)(min(a[i + k] - 1, p2) - max(f, p1) + 1); } } } } return t1; } int main() { int prv = 1; a[++t] = 4; a[++t] = 7; for (int i = 2; i <= 9; ++i) { int f = t; for (int j = prv; j <= f; ++j) { a[++t] = a[j] * 10 + 4; a[++t] = a[j] * 10 + 7; } prv = f + 1; } a[t + 1] = 2e9; int p1, p2, v1, v2; cin >> p1 >> p2 >> v1 >> v2 >> k; long long int ans = 0; a[0] = 0; for (int i = 1; i <= t - k + 1; i++) { int it = a[i], f = a[i + k - 1]; long long int t1, t2; t1 = fun(p1, p2, v1, v2, it, f, i); t2 = fun(v1, v2, p1, p2, it, f, i); ans += t1 + t2; if (k == 1 && t1 && t2) ans--; } double fans = ans, g = p2 - p1 + 1; g *= v2 - v1 + 1; fans /= g; printf("%.10lf\n", fans); return 0; }
11
#include <bits/stdc++.h> using namespace std; long long solve() { long long n; cin >> n; if (n % 2) { cout << -1; return 0; } long long k = n, i, j; while (n--) { if (n % 2) { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { if (min(min(i, k - 1 - i), min(j, k - 1 - j)) % 2) cout << "b"; else cout << "w"; } cout << "\n"; } } else { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { if (min(min(i, k - 1 - i), min(j, k - 1 - j)) % 2) cout << "w"; else cout << "b"; } cout << "\n"; } } cout << "\n"; } } signed main() { long long t = 1; while (t--) solve(); }
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int i = 1; int res = 0; while (i < s.length()) { if (s[i] != '1') res += s[i] - '0'; else { res += 10; i++; } i++; } res += 1; cout << res; return 0; }
11
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { x = 0; int fl = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fl = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } x *= fl; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } const int N = 100005; int n, mxdeg, deg[N]; vector<int> edge[N]; vector<pair<int, int> > ans; void dfs(int x, int f, int t) { ans.emplace_back(make_pair(x, t)); if (t == mxdeg) { t = mxdeg - deg[x]; ans.emplace_back(make_pair(x, t)); } for (auto to : edge[x]) { if (to == f) continue; dfs(to, x, t + 1); t++; if (ans.back().second != t - 1) ans.emplace_back(make_pair(to, t - 1)); ans.emplace_back(make_pair(x, t)); if (f && t == mxdeg) { t = mxdeg - deg[x]; ans.emplace_back(make_pair(x, t)); } } } int main() { read(n); if (n == 1) { puts("1"); puts("1 0"); return 0; } for (int i = 1; i < n; i++) { int u, v; read(u, v); edge[u].emplace_back(v), edge[v].emplace_back(u); deg[u]++, deg[v]++; mxdeg = max(mxdeg, deg[u]); mxdeg = max(mxdeg, deg[v]); } dfs(1, 0, 0); printf("%d\n", (int)ans.size()); for (auto k : ans) printf("%d %d\n", k.first, k.second); return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; s.push_back('#'); bool ab = false, ba = false; for (int i = 0; i < s.length() - 2; ++i) { if (s.substr(i, 2) == "AB") { for (int j = i + 2; j < s.size() - 2; ++j) if (s.substr(j, 2) == "BA") { cout << "YES"; return 0; } break; } } for (int i = 0; i < s.length() - 2; ++i) { if (s.substr(i, 2) == "BA") { for (int j = i + 2; j < s.size() - 2; ++j) if (s.substr(j, 2) == "AB") { cout << "YES"; return 0; } break; } } cout << "NO"; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n; long long k, a[110], d; long long func(long long val) { long long hold = 0; for (int i = 0; i < n; i++) { hold += (long long)ceil((double)a[i] / (double)val); } hold = k / hold; return hold; } int main() { scanf("%d%lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%lld", a + i); k += a[i]; } long long ans = 1; for (long long i = 1; i <= k; i++) { d = k / i; if (func(d) >= d) ans = max(ans, d); i = k / d; } printf("%lld\n", ans); return 0; }
15
#include <bits/stdc++.h> using namespace std; const long long UNDEF = -1; const long long INF = 1e18; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } static char stdinBuffer[1024]; static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); static const char *stdinPos = stdinDataEnd; void readAhead(size_t amount) { size_t remaining = stdinDataEnd - stdinPos; if (remaining < amount) { memmove(stdinBuffer, stdinPos, remaining); size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin); stdinPos = stdinBuffer; stdinDataEnd = stdinBuffer + remaining + sz; if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; } } int readi() { readAhead(16); int x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; } char readc() { readAhead(16); while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; char ans = *stdinPos; ++stdinPos; return ans; } const int mn = 1e5 + 4; bitset<mn> a; bitset<mn> known; bitset<mn> leader; char vc[mn]; int vx[mn]; int b[mn]; vector<pair<int, char> > x2i[mn]; int query(int l, int r) { return b[r] - b[l - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n = readi(), m = readi(); for (int i = 1; i <= m; i++) { vc[i] = readc(), vx[i] = readi(); } for (int i = 1; i <= m; i++) { char c = vc[i]; int x = vx[i]; if (!known[x]) { known[x] = 1; if (c == '+') a[x] = 0; else a[x] = 1; } } for (int i = 1; i <= m; i++) { char c = vc[i]; int x = vx[i]; x2i[x].push_back(make_pair(i, c)); } int live = a.count(); for (int i = 1; i <= m; i++) { b[i] = live ? 1 : 0; char c = vc[i]; if (c == '+') { live++; } else { live--; } } b[m + 1] = live ? 1 : 0; for (int i = 1; i <= m + 1; i++) b[i] += b[i - 1]; for (int x = 1; x <= n; x++) { if (!known[x]) { leader[x] = 1; continue; } leader[x] = 1; int last = 0; for (auto &w : x2i[x]) { if (w.second == '+') { int l = last + 1, r = w.first; if (query(l, r)) { leader[x] = 0; } last = -1; } else { last = w.first; } } if (known[x] && last != -1) { int l = last + 1, r = m + 1; if (query(l, r)) { leader[x] = 0; } } } printf("%d\n", (int)leader.count()); for (int x = 1; x <= n; x++) { if (leader[x]) printf("%d ", x); } if (leader.count() > 0) printf("\n"); }
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long double ans = 0; long double check(vector<long long>& mas, long double& x) { long long n = mas.size(); long double mx1 = mas[0] - x, sum = 0, min_sum = 0; for (long long r = 0; r < n; r++) { sum += mas[r] - x; mx1 = max(mx1, sum - min_sum); min_sum = min(min_sum, sum); } long double mx2 = mas[0] - x; sum = 0; min_sum = 0; for (long long r = 0; r < n; r++) { sum += mas[r] - x; mx2 = min(mx2, sum - min_sum); min_sum = max(min_sum, sum); } return max(abs(mx1), abs(mx2)); } void solve() { long long n; cin >> n; vector<long long> mas(n); for (long long i = 0; i < n; i++) { cin >> mas[i]; } long double l = -10001, r = 10001; for (long long i = 0; i < 100; i++) { long double lm = (2 * l + r) / 3; long double rm = (l + 2 * r) / 3; long double a = check(mas, lm), b = check(mas, rm); if (a > b) { l = lm; ans = a; } else { r = rm; ans = b; } } cout << fixed << setprecision(20) << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { solve(); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { long double n, r, x, y, z, len, s; cin >> n >> r; x = acos(-1.0) / (2 * n); y = acos(-1.0) / n; z = acos(-1.0) - x - y; len = r * sin(x) / sin(z); s = len * r * sin(y) * n; cout << fixed << setprecision(8) << s << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; int c = (n / 3) * 2; if (n % 3) c += 1; cout << c; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int i, a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); if (n == 1) cout << a[0]; if (n == 3) cout << a[1]; if (n == 5) cout << a[2]; }
0
#include <bits/stdc++.h> using namespace std; int i, j, n, m, l, r, t; int cnt[200][200], cnt2[200][200]; bool used[200][200], used2[200][200]; char a[200][200]; int x[200 * 200], y[200 * 200]; int u, v, u2, v2, tu, tv; bool first; bool valid(int u, int v) { if (u > 0 && v > 0 && u <= n && v <= n) return true; return false; } void bfs(int u, int v) { l = r = 1; int u2, v2; x[l] = u; used[u][v] = true; y[l] = v; cnt[u][v] = 1; while (l <= r) { u = x[l]; v = y[l]; if (!used[u - 2][v + 2] && valid(u - 2, v + 2)) { tu = u - 2; tv = v + 2; r++; x[r] = tu; y[r] = tv; used[tu][tv] = true; cnt[tu][tv] = cnt[u][v] + 1; } if (!used[u + 2][v + 2] && valid(u + 2, v + 2)) { tu = u + 2; tv = v + 2; r++; x[r] = tu; y[r] = tv; cnt[tu][tv] = cnt[u][v] + 1; used[tu][tv] = true; } if (!used[u - 2][v - 2] && valid(u - 2, v - 2)) { tu = u - 2; tv = v - 2; r++; x[r] = tu; y[r] = tv; cnt[tu][tv] = cnt[u][v] + 1; used[tu][tv] = true; } if (!used[u + 2][v - 2] && valid(u + 2, v - 2)) { tu = u + 2; tv = v - 2; r++; x[r] = tu; y[r] = tv; cnt[tu][tv] = cnt[u][v] + 1; used[tu][tv] = true; } l++; } } void bfs2(int u, int v) { l = r = 1; x[l] = u; y[l] = v; used2[u][v] = true; cnt2[u][v] = 1; while (l <= r) { u = x[l]; v = y[l]; if (!used2[u - 2][v + 2] && valid(u - 2, v + 2)) { tu = u - 2; tv = v + 2; r++; x[r] = tu; y[r] = tv; cnt2[tu][tv] = cnt2[u][v] + 1; used2[tu][tv] = true; } if (!used2[u + 2][v + 2] && valid(u + 2, v + 2)) { tu = u + 2; tv = v + 2; r++; x[r] = tu; y[r] = tv; cnt2[tu][tv] = cnt2[u][v] + 1; used2[tu][tv] = true; } if (!used2[u - 2][v - 2] && valid(u - 2, v - 2)) { tu = u - 2; tv = v - 2; r++; x[r] = tu; y[r] = tv; cnt2[tu][tv] = cnt2[u][v] + 1; used2[tu][tv] = true; } if (!used2[u + 2][v - 2] && valid(u + 2, v - 2)) { tu = u + 2; tv = v - 2; r++; x[r] = tu; y[r] = tv; cnt2[tu][tv] = cnt2[u][v] + 1; used2[tu][tv] = true; } l++; } } void cl() { for (int i = 1; i <= n; i++) a[0][i] = '#', a[n + 1][i] = '#', a[i][0] = '#', a[i][n + 1] = '#'; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { cnt[i][j] = cnt2[i][j] = 0; used[i][j] = used2[i][j] = false; } } int main() { scanf("%d\n", &t); while (t--) { n = 8; first = false; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { scanf("%c", &a[i][j]); if (a[i][j] == 'K' && !first) { u = i; v = j; first = true; } else if (first && a[i][j] == 'K') u2 = i, v2 = j; } scanf("\n"); } scanf("\n"); cl(); bfs(u, v); bfs2(u2, v2); string ans = "NO"; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (a[i][j] != '#' && cnt[i][j] && cnt2[i][j] && cnt[i][j] && cnt[i][j] % 2 == cnt2[i][j] % 2) ans = "YES"; cout << ans << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, x, y; cin >> n >> x >> y; long long white = x + y - 2; long long black = 2 * n - x - y; if (white <= black) cout << "White"; else cout << "Black"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long a, b, x, y, c = 0; cin >> x >> y >> a >> b; int g = (x * y) / gcd(x, y); cout << b / g - (a - 1) / g << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long INF = 999999999999999; long long MOD = 1000000007; void solve() { long long ans = 0; vector<long long> a(3); cin >> a[0] >> a[1] >> a[2]; sort(a.begin(), a.end()); set<long long> usd; for (long long i = 0; i < 11; i++) { for (int i1 = 0; i1 <= 1; i1++) for (int i2 = 0; i2 <= 1; i2++) for (int i3 = 0; i3 <= 1; i3++) if (i1 || i2 || i3) if (i1 <= a[0] && i2 <= a[1] && i3 <= a[2]) if (!usd.count(i1 * 121 + i2 * 11 + i3)) { usd.insert(i1 * 121 + i2 * 11 + i3); a[0] -= i1; a[1] -= i2; a[2] -= i3; ans++; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long q = 1; cin >> q; while (q--) { solve(); } }
1
#include <bits/stdc++.h> using namespace std; const long long logN = 20; const long long N = 100005; const long long M = 1000000007; const long long dx[4] = {1, 0, -1, 0}; const long long dy[4] = {0, 1, 0, -1}; long long vis[1005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.length(); char B[n]; bool f = 1; long long A[27] = {0}; A[26] = M; long long flag = 0; for (long long i = 0; i < n; i++) A[s[i] - 'a']++, B[i] = '0'; for (long long i = 0; i < 26; i++) { if (A[i] > A[flag]) flag = i; } for (long long i = 2; i < n; i++) { if ((2 * i) <= n and !vis[i]) { for (long long j = i; j <= n; j += i) vis[j] = 1; } } long long cur = 0; for (long long i = 0; i < n; i++) { if (vis[i + 1]) { B[i] = flag + 'a'; A[flag]--; if (A[flag] < 0) f = 0; } else { while (A[cur] <= 0 or cur == flag) cur++; if (cur == 26) cur = flag; B[i] = cur + 'a'; A[cur]--; } } if (!f) cout << "NO" << "\n"; else { cout << "YES" << "\n"; for (long long i = 0; i < n; i++) cout << B[i]; cout << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2020, P = 1e9 + 7, B = 19260817; int n, m, ans, p[N], g[2][2][N], h[N], f[2][N][N], a[2][N]; char s[2][N], t[N]; int hs(int l, int r) { return (h[r] - 1ll * h[l - 1] * p[r - l + 1] % P + P) % P; } int gs(int l, int r, int k, int b) { if (b == 0) return (g[0][k][r] - 1ll * g[0][k][l - 1] * p[r - l + 1] % P + P) % P; if (b == 1) return (g[1][k][l] - 1ll * g[1][k][r + 1] * p[r - l + 1] % P + P) % P; } int cal(int l, int r, int k, int b) { return (1ll * gs(l, r, k, !b) * p[r - l + 1] % P + gs(l, r, !k, b)) % P; } void work() { reverse(t + 1, t + m + 1); for (int i = 1; i <= m; i++) h[i] = (1ll * h[i - 1] * B % P + t[i]) % P; f[0][0][0] = f[1][0][0] = 1; for (int i = 1; i <= n - 1; i++) { for (int j = 0; j <= m; j++) for (int k = 0; k <= 1; k++) a[k][j] = j ? f[k][i - 1][j - 1] * (s[k][i] == t[j]) : 0; f[0][i][0] = f[1][i][0] = 1; for (int j = 1; j <= m; j++) for (int k = 0; k <= 1; k++) { f[k][i][j] = j ? (a[!k][j - 1] * (s[k][i] == t[j]) + a[k][j]) % P : 0; if (j / 2 > 1 && j % 2 == 0 && i >= j / 2) f[k][i][j] += (cal(i - j / 2 + 1, i, !k, 0) == h[j]); if (j < m && (m - j) % 2 == 0 && n - i >= (m - j) / 2) (ans += f[k][i][j] * (cal(i + 1, i + (m - j) / 2, k, 1) == hs(j + 1, m))) %= P; } for (int k = 0; k <= 1; k++) (ans += f[k][i][m - 1] * (s[k][i + 1] == t[m])) %= P; } } int main() { scanf("%s%s%s", s[0] + 1, s[1] + 1, t + 1); n = strlen(s[0] + 1); m = strlen(t + 1); if (m == 1) { for (int i = 1; i <= n; i++) for (int k = 0; k <= 1; k++) ans += s[k][i] == t[1]; cout << ans << '\n'; return 0; } p[0] = 1; for (int i = 1; i <= max(n, m); i++) p[i] = 1ll * p[i - 1] * B % P; for (int i = 1; i <= n; i++) for (int k = 0; k <= 1; k++) g[0][k][i] = (1ll * g[0][k][i - 1] * B % P + s[k][i]) % P; for (int i = n; i >= 1; i--) for (int k = 0; k <= 1; k++) g[1][k][i] = (1ll * g[1][k][i + 1] * B % P + s[k][i]) % P; work(); work(); if (m % 2 == 0) for (int i = 1; i <= n - m / 2 + 1; i++) { int j = i + m / 2 - 1; for (int k = 0; k <= 1; k++) { ans += cal(i, j, k, 0) == h[m]; if (m > 2) ans += cal(i, j, k, 1) == h[m]; } } cout << ans % P << '\n'; }
24