solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, m, p, q, r, i, j, k, a[200005], b[2][200005] = {0}; ios::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } b[0][0] = b[1][0] = b[0][n + 1] = b[1][n + 1] = 0; for (i = 1, j = 0; i <= n; i++) { if (a[i] == 0) { b[0][i] = j; } else { j++; } } for (i = n, j = 0; i > 0; i--) { if (a[i] == 1) { b[1][i] = j; } else { j++; } } for (i = 1, j = 0; i <= n; i++) { j += b[0][i]; } k = j; for (i = 1, j = 0; i <= n; i++) { j += b[1][i]; } k = min(k, j); cout << k; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 100; const long long MOD = 1e9 + 7; const long long INF = 1e18; long long cost[MAXN]; bool block[MAXN]; long long le[MAXN]; int main(int argc, char const *argv[]) { long long n, m, p; while (cin >> n >> m >> p) { memset(block, 0, sizeof block); for (long long i = 0; i < m; ++i) { long long tem; scanf("%lld", &tem); block[tem] = 1; } for (long long i = 1; i <= p; ++i) scanf("%lld", &cost[i]); if (block[0]) { puts("-1"); continue; } long long lb = 0, cur = 0; for (long long i = 0; i < n; ++i) { if (block[i]) { cur++; lb = max(lb, cur); le[i] = le[i - 1]; } else { cur = 0; le[i] = i; } } long long ans = INF; for (long long i = lb + 1; i <= p; ++i) { long long cans = 0; for (long long j = 0; j < n; j += i) { if (block[j]) { j = le[j] - i; continue; } cans += cost[i]; } ans = min(ans, cans); } if (ans == INF) { puts("-1"); } else { printf("%lld\n", ans); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0, f = 1; char ch = getchar(); while ((ch != '-') && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') ch = getchar(), f = -1; while (ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar(); return sum * f; } int T; int h, g; int w[2100000]; bool Can[2100000]; bool per[2100000]; int tt; int n, m, mc; void out() { for (int i = 1; i <= tt; i++) cout << 1 << " "; cout << '\n'; } int ans[2100000], tot; int suan(int s) { return s > n ? 0 : w[s]; } void upd(int now) { int ll = suan(now << 1); int rr = suan(now << 1 | 1); if (!ll && !rr) { w[now] = 0; if (now <= m) Can[now] = per[now] = 1; return; } if (ll > rr) { w[now] = ll; upd(now << 1); } else { w[now] = rr; upd(now << 1 | 1); } if (now > m) return; ll = suan(now << 1), rr = suan(now << 1 | 1); if (!ll && !rr) Can[now] = per[now] = 1; else { if (ll > rr) Can[now] = Can[now << 1]; else Can[now] = Can[now << 1 | 1]; if (now <= mc) per[now] = per[now << 1] & per[now << 1 | 1]; } } int main() { T = read(); while (T--) { tot = 0; int mmin = 2e9; h = read(), g = read(); n = (1 << h) - 1; m = (1 << g) - 1; mc = (1 << (g - 1)) - 1; for (int i = 1; i <= n; i++) w[i] = read(), mmin = min(mmin, w[i]); for (int i = 1; i <= n; i++) Can[i] = per[i] = 0; tt = n - m; if (g == 1) { cout << mmin << '\n'; out(); continue; } while (tt--) { int now = 1; while (Can[now]) { int lson = now << 1; int rson = now << 1 | 1; if (suan(lson) > suan(rson)) { if (per[lson]) now = rson; else now = lson; } else { if (per[rson]) now = lson; else now = rson; } } ans[++tot] = now; upd(now); if (now > m) continue; while (now) { now >>= 1; int ll = suan(now << 1), rr = suan(now << 1 | 1); if (ll > rr) Can[now] = Can[now << 1]; else Can[now] = Can[now << 1 | 1]; if (now <= mc) per[now] = per[now << 1] & per[now << 1 | 1]; } } long long ANS = 0; for (int i = 1; i <= n; i++) if (w[i]) ANS += w[i]; cout << ANS << '\n'; for (int i = 1; i <= tot; i++) cout << ans[i] << " "; cout << '\n'; } return 0; }
5
#include<bits/stdc++.h> using namespace std; int main(){ string a , b; cin>>a>>b; string s = a+b; int n = stoi(s); if( sqrt(n) - floor(sqrt(n)) == 0 ) cout<<"Yes"; else cout<<"No"; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 100; int tree[maxn << 2]; int a[maxn], p[maxn]; void push(int id) { tree[id] = tree[id << 1] + tree[id << 1 | 1]; } void update(int ind, int x, int l, int r, int id) { if (l == r) { tree[id] = x; return; } int mid = (l + r) >> 1; if (ind <= mid) update(ind, x, l, mid, id << 1); else update(ind, x, mid + 1, r, id << 1 | 1); push(id); } int query(int L, int R, int l, int r, int id) { if (L <= l && r <= R) return tree[id]; int mid = (l + r) >> 1; int res = 0; if (L <= mid) res += query(L, R, l, mid, id << 1); if (mid < R) res += query(L, R, mid + 1, r, id << 1 | 1); return res; } int main() { int n, q; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; p[0] = 0, p[n + 1] = n + 1; for (int i = 2; i <= n; i++) if (p[i] < p[i - 1]) update(i, 1, 1, n, 1); scanf("%d", &q); int op, x, y; while (q--) { scanf("%d%d%d", &op, &x, &y); if (op == 1) printf("%d\n", query(x + 1, y, 1, n, 1) + 1); else { int xx = a[x], yy = a[y]; swap(a[x], a[y]); swap(p[xx], p[yy]); update(xx, 0, 1, n, 1); update(xx + 1, 0, 1, n, 1); update(yy, 0, 1, n, 1); update(yy + 1, 0, 1, n, 1); if (p[xx] < p[xx - 1]) update(xx, 1, 1, n, 1); if (p[xx + 1] < p[xx]) update(xx + 1, 1, 1, n, 1); if (p[yy] < p[yy - 1]) update(yy, 1, 1, n, 1); if (p[yy + 1] < p[yy]) update(yy + 1, 1, 1, n, 1); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const int SZ = 1e6 + 10; const int mod = 1e9 + 7; const double PI = acos(-1); const double eps = 1e-7; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > '9' || a < '0') { if (a == '-') flag = 1; a = getchar(); } while (a <= '9' && a >= '0') { n = n * 10 + a - '0', a = getchar(); } if (flag) n = -n; return n; } int edges1[15][15][17010]; vector<int> nodes[17010]; vector<pair<int, int> > G[22]; bool vis[17010]; int f[17010], g[15][15][17010]; int to[15][17010], n, m, trans[17010]; void print(int S) { for (int i = 0; i < n; i++) if (S >> i & 1) printf("%d,", i + 1); } void dfs(int S) { if (vis[S]) return; vis[S] = 1; int ans = 1e9, e1 = 0, e2 = 0; for (int S1 = (S - 1) & S; S1 > 0; S1 = (S1 - 1) & S) { int S2 = S ^ S1; dfs(S1); for (int i = 0; i < n; i++) { if ((S2 >> i & 1) == 0) continue; if (!to[i][S1]) continue; for (int j = 0; j < n; j++) { if ((S2 >> j & 1) == 0) continue; if (!to[j][S1]) continue; if (!g[i][j][S2]) continue; int tmp = f[S1] + g[i][j][S2] + 2; if (tmp < ans) { ans = tmp; trans[S] = S1; e1 = i; e2 = j; } } } } for (int i = 0; i < n; i++) { if (S >> i & 1) { int S1 = S ^ (1 << i); int t1 = 0, t2 = 0; for (pair<int, int> p : G[i]) { int v = p.first; if (S1 >> v & 1) { if (!t1) t1 = p.second; else t2 = p.second; } } if (t2 == 0) continue; int tmp = f[S1] + 2; if (tmp < ans) ans = tmp, e1 = i, e2 = i, trans[S] = S1; } } f[S] = ans; nodes[S].push_back(e1); nodes[S].push_back(e2); } int a[SZ], b[SZ]; int pre[15][15][17010]; int main() { n = read(), m = read(); for (int i = 1; i <= m; i++) { int x = read() - 1, y = read() - 1; g[x][y][(1 << x) | (1 << y)] = 1; g[y][x][(1 << x) | (1 << y)] = 1; edges1[x][y][(1 << x) | (1 << y)] = i; edges1[y][x][(1 << x) | (1 << y)] = i; G[x].push_back(make_pair(y, i)); G[y].push_back(make_pair(x, i)); a[i] = x; b[i] = y; } vis[0] = 1; for (int i = 0; i < n; i++) { f[1 << i] = 0, vis[1 << i] = 1; for (int S = 0; S < (1 << n); S++) { for (pair<int, int> p : G[i]) { if (S >> p.first & 1) { to[i][S] = p.second; break; } } } } for (int S = 0; S < (1 << n); S++) { for (int i = 0; i < n; i++) { if ((S >> i & 1) == 0) continue; for (int j = 0; j < n; j++) { if ((S >> j & 1) == 0) continue; if (!g[i][j][S]) continue; for (pair<int, int> p : G[j]) { int x = p.first; if (S >> x & 1) continue; if (x == i) continue; if (!g[i][x][S ^ (1 << x)]) { g[i][x][S ^ (1 << x)] = g[i][j][S] + 1; pre[i][x][S ^ (1 << x)] = j; edges1[i][x][S ^ (1 << x)] = p.second; } } } } } dfs((1 << n) - 1); printf("%d\n", f[(1 << n) - 1]); int S = (1 << n) - 1; while (__builtin_popcount(S) > 1) { int S2 = S ^ trans[S], S1 = trans[S]; int x = nodes[S][0]; int y = nodes[S][1]; if (x != y) { printf("%d %d\n", a[to[x][S1]] + 1, b[to[x][S1]] + 1); printf("%d %d\n", a[to[y][S1]] + 1, b[to[y][S1]] + 1); while (__builtin_popcount(S2) >= 2) { int v = edges1[x][y][S2]; printf("%d %d\n", a[v] + 1, b[v] + 1); int p = pre[x][y][S2]; S2 ^= 1 << y; y = p; } S = S1; } else { int t = 0; for (pair<int, int> p : G[x]) { if (S1 >> p.first & 1) { printf("%d %d\n", a[p.second] + 1, b[p.second] + 1); if (++t == 2) break; } } S = S1; } } }
6
#include <bits/stdc++.h> using namespace std; const int _ = 2e5 + 7, P = 998244353; int poww(long long a, int b) { int tms = 1; while (b) { if (b & 1) tms = tms * a % P; a = a * a % P; b >>= 1; } return tms; } vector<int> qry; int N, K, tms = 1, fac[_], ifac[_]; int main() { ios::sync_with_stdio(0); cin >> N >> K; int x; cin >> x; qry.push_back(x); fac[0] = 1; for (int i = 1; i <= N; ++i) fac[i] = 1ll * fac[i - 1] * i % P; ifac[N] = poww(fac[N], P - 2); for (int i = N - 1; ~i; --i) ifac[i] = ifac[i + 1] * (i + 1ll) % P; for (int i = 2; i <= N; ++i) { int t; cin >> t; if (qry.back() == t) tms = 1ll * tms * K % P; else qry.push_back(t); } if (qry.size() == 1) { cout << 0; return 0; } while (qry.front() == qry.back()) { tms = 1ll * tms * K % P; qry.pop_back(); } int lft = 0; for (int i = 0; 2 * i <= qry.size(); ++i) { lft = (lft + 1ll * fac[qry.size()] * ifac[i] % P * ifac[i] % P * ifac[qry.size() - 2 * i] % P * poww(K - 2, qry.size() - 2 * i)) % P; } cout << 1ll * tms * (poww(K, qry.size()) - lft + P) % P * (P + 1) / 2 % P; return 0; }
4
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); struct pt { double x, y; pt(){}; pt(double _x, double _y) : x(_x), y(_y){}; pt operator+(pt const& p) { return pt(x + p.x, y + p.y); } pt operator-(pt const& p) { return pt(x - p.x, y - p.y); } pt operator*(pt const& p) { return pt(x * p.x, y * p.y); } pt operator/(pt const& p) { return pt(x / p.x, y / p.y); } pt operator~() { return pt(y, x); } friend pt operator*(double val, pt const& p) { return pt(val * p.x, val * p.y); } double dot(pt const& p) { return x * p.x + y * p.y; } }; void findCircle(pt p1, pt p2, pt p3, pt& center, double& radius) { pt vt12 = p1 - p2; pt vt13 = p1 - p3; double d31 = p3.dot(p3) - p1.dot(p1); double d12 = p1.dot(p1) - p2.dot(p2); center = 0.5 * ~(d31 * vt12 + d12 * vt13) / (~vt13 * vt12 - ~vt12 * vt13); radius = hypot(center.x - p1.x, center.y - p1.y); } double alpha(pt vt1, pt vt2) { double norm1 = hypot(vt1.x, vt1.y); double norm2 = hypot(vt2.x, vt2.y); return acos(vt1.dot(vt2) / (norm1 * norm2)); } bool isEqual(double n1, double n2) { return abs(n1 - n2) / n2 < 1e-6; } bool isDivisible(double ang1, double ang2, double div) { double ang3 = 2 * PI - ang1 - ang2; double aux = ang1 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; aux = ang2 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; aux = ang3 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; return true; } int main() { pt p1, p2, p3, cent; int i, sides; double ang1, ang2, ang3, alp, area, radius; cin >> p1.x >> p1.y; cin >> p2.x >> p2.y; cin >> p3.x >> p3.y; findCircle(p1, p2, p3, cent, radius); ang1 = alpha(p1 - cent, p2 - cent); ang2 = alpha(p3 - cent, p1 - cent); ang3 = alpha(p2 - cent, p3 - cent); if (ang1 > ang2) swap(ang1, ang2); if (ang2 > ang3) swap(ang2, ang3); for (i = 3; i <= 100; i++) { alp = PI - (i - 2) * PI / i; if (isDivisible(ang1, ang2, alp)) { sides = i; break; } } alp = (sides - 2) * PI / sides; area = radius * radius * sin(alp) * sides / 2; cout << setprecision(8) << fixed << area << endl; }
3
#include <bits/stdc++.h> using namespace std; int x[55], y[55], cnt1[1005], cnt2[1005]; int main() { int n, i, ans, sum1, sum2; bool flag = false; ans = sum1 = sum2 = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x[i]); cnt1[x[i]]++; sum1 += x[i]; } for (i = 0; i < n; i++) { scanf("%d", &y[i]); cnt2[y[i]]++; sum2 += y[i]; } if (sum1 >= sum2) { printf("Yes\n"); return 0; } printf("No\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; void solve() { int n; string a, b; cin >> n >> a >> b; int l = 0, r = n - 1; int lv = 1, rv = -1; int cnt = 0; vector<int> ans; for (int i = n - 1; i >= 0; i--) { if ((a[r] - '0' + cnt) % 2 != b[i] - '0') { if (a[l] != a[r]) { ans.push_back(1); } ans.push_back(i + 1); l += lv; swap(l, r); swap(lv, rv); cnt++; } else { r += rv; } } cout << ans.size() << "\n"; for (auto i : ans) cout << i << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename t> using V = vector<t>; template <typename t> void print(ostream& os, const t& a) { os << a << '\n'; } template <typename t, typename... A> void print(ostream& os, const t& a, A&&... b) { os << a << ' '; print(os, b...); } constexpr int INF = 1e9 + 1; constexpr int MaxRes = 1e9; struct Point { int x, y; Point(int x = -INF, int y = -INF) : x(x), y(y) {} }; struct Ev { int x, lo, hi, val; Ev(int x, int lo, int hi, int val = 1) : x(x), lo(lo), hi(hi), val(val) {} }; inline bool operator<(const Ev& mn, const Ev& wc) { return mn.x == wc.x ? mn.val > wc.val : mn.x < wc.x; } struct Node { int val, v; Node() : val(0), v(0) {} }; int n, m, k, rozm = 1; V<Ev> evs; V<int> low; V<Node> tree; V<Point> ign; inline void upd(int v) { if (tree[v].val > 0) tree[v].v = INF; else { if (v >= rozm) tree[v].v = v - rozm; else tree[v].v = min(tree[v << 1].v, tree[(v << 1) + 1].v); } } inline void insert(int lo, int hi, int co) { lo += rozm; hi += rozm; tree[lo].val += co; if (lo != hi) tree[hi].val += co; upd(lo); upd(hi); while ((lo >> 1) != (hi >> 1)) { if ((lo & 1) == 0) { tree[lo + 1].val += co; upd(lo + 1); } if ((hi & 1) == 1) { tree[hi - 1].val += co; upd(hi - 1); } upd(lo); upd(hi); lo >>= 1; hi >>= 1; } upd(lo); upd(hi); for (lo >>= 1; lo >= 1; lo >>= 1) upd(lo); } inline void process(const Ev& ev) { int lo = lower_bound(begin(low), end(low), ev.lo) - begin(low); int hi = lower_bound(begin(low), end(low), ev.hi) - begin(low); insert(lo, hi, ev.val); } inline pair<bool, Point> process() { low.clear(); tree.clear(); rozm = 1; low.push_back(1); low.push_back(m); for (auto& ev : evs) { low.push_back(ev.lo); low.push_back(ev.hi); } sort(begin(low), end(low)); low.resize(unique(begin(low), end(low)) - begin(low)); int siz = low.size(); for (int i = 0; i < siz - 1; ++i) if (low[i + 1] - low[i] > 1) low.push_back(low[i] + 1); sort(begin(low), end(low)); while (rozm < low.size()) rozm <<= 1; tree.resize(rozm << 1); for (auto& n : tree) n = Node(); for (int i = rozm; i < rozm << 1; ++i) tree[i].v = i - rozm; for (int i = rozm - 1; i >= 1; --i) upd(i); int pop = 1; int x = INT_MAX, y = INT_MAX; for (auto& ev : evs) { if (ev.x != pop) { if (tree[1].v < low.size()) { x = min(x, pop); y = min(y, low[tree[1].v]); } pop = ev.x; } process(ev); } if (tree[1].v < low.size()) { x = min(x, pop); y = min(y, low[tree[1].v]); } if (y == INT_MAX) return {true, Point()}; else return {false, Point(x, y)}; } inline void append_events(const Point& a, int v) { int lo = max(a.y - v, 1); int hi = min((ll)a.y + v, (ll)m); int d = max(a.x - v, 1); int u = min((ll)a.x + v + 1, (ll)n + 1); evs.emplace_back(d, lo, hi); if (u <= n) evs.emplace_back(u, lo, hi, -1); } inline bool check(int v) { ; evs.clear(); for (auto& p : ign) append_events(p, v); sort(begin(evs), end(evs)); ; bool res; Point q; tie(res, q) = process(); if (res == true) { ; return true; }; ; q.x += v; q.y += v; append_events(q, v); sort(begin(evs), end(evs)); ; tie(res, q) = process(); ; return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; ign.resize(k); for (auto& p : ign) cin >> p.x >> p.y; int lo = -1; for (int b = MaxRes + 1; b >= 1; b >>= 1) while (lo + b <= MaxRes and check(lo + b) == false) lo += b; print(cout, lo + 1); }
5
#include <bits/stdc++.h> using namespace std; const int N = 300006; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<bool> can_modify(n + 1, true); vector<pair<long double, int>> a[2]; for (int i = 1; i <= n; i += 1) { long double x; cin >> x; if (x == trunc(x)) can_modify[i] = false; if (x < 0.0) { a[0].push_back({x, i}); } if (x > 0.0) { a[1].push_back({x, i}); } } long long sum = 0; for (auto &it : a[0]) { it.first = trunc(it.first); sum += trunc(it.first); } for (auto &it : a[1]) { it.first = trunc(it.first); sum += trunc(it.first); } if (sum > 0) { for (auto &it : a[0]) { if (can_modify[it.second] && sum > 0) { sum--; it.first--; } } } else { for (auto &it : a[1]) { if (can_modify[it.second] && sum < 0) { sum++; it.first++; } } } vector<long double> ans(n + 1); for (auto it : a[0]) ans[it.second] = it.first; for (auto it : a[1]) ans[it.second] = it.first; for (int i = 1; i <= n; i++) if (ans[i] == 0) ans[i] = 0; for (int i = 1; i <= n; i++) cout << ans[i] << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MN = 101234; long double fac[MN] = {}, ans = 0; long double chs(int x, int y) { return fac[y] - fac[x] - fac[y - x]; } int main() { for (int i = 1; i < MN; i++) fac[i] = fac[i - 1] + log(i); int n, m, k, good; cin >> n >> m >> k; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) if ((good = i * n + j * n - i * j) <= k) ans += exp(chs(i, n) + chs(j, n) + chs(k - good, m - good) - chs(k, m)); cout << setprecision(60) << min((long double)1e99, ans); }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 50; constexpr ll INF = 1000000000000000002LL; namespace dsu { int f[MAXN], sz[MAXN]; void build(int n) { fill(sz, sz + n, 1); for (int i = (int)0; i < (int)n; ++i) f[i] = i; } int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } bool join(int x, int y) { if ((x = find(x)) == (y = find(y))) return false; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; f[y] = x; return true; } bool connected(int i, int j) { return find(i) == find(j); } } // namespace dsu ll memo[MAXN], f[MAXN]; int res[MAXN]; bitset<MAXN> used; ll add(ll a, ll b) { if (a + b >= INF) return INF; return a + b; } ll mul(ll a, ll b) { long double ans = (long double)(a) * (long double)(b); if (ans >= INF) return INF; return a * b; } ll dp(int len) { ll& ans = memo[len]; if (~ans) { assert(ans >= 0); return ans; } if (len == 0) return ans = 1; ans = 0; for (int i = 1; i <= len; ++i) ans = add(ans, mul(dp(len - i), f[max(0, i - 2)])); return ans; } void rebuild(int len, ll k, int pos, int n) { if (len == 0) { assert(pos == n); return; } ll ans = 0; for (int i = 1; i <= len; ++i) { ll ol = ans; ans = add(ans, mul(dp(len - i), f[max(0, i - 2)])); if (k <= ans) { k -= ol; ll j = k / dp(len - i); while (mul(j, dp(len - i)) < k) ++j; k -= mul(j - 1, dp(len - i)); res[pos] = i + pos; dsu::build(MAXN); used.reset(); for (int posin = 2; posin <= i; ++posin) { int put = -1; ll ac = 0, olac; for (int num = 1; num < i; ++num) { if (dsu::connected(num, posin) || used[num]) continue; olac = ac; ac = add(ac, f[max(0, i - posin - 1)]); if (j <= ac) { put = num; break; } } assert(put != -1); res[pos + posin - 1] = put + pos; used[put] = true; dsu::join(put, posin); j -= olac; } rebuild(len - i, k, i + pos, n); return; } } assert(false); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); f[0] = 1; for (int i = (int)1; i < (int)MAXN; ++i) { if (f[i - 1] == INF) { f[i] = f[i - 1]; } else { f[i] = min(INF, 1LL * i * f[i - 1]); } } memset(memo, -1, sizeof memo); int tc; cin >> tc; while (tc--) { int n; ll k; cin >> n >> k; if (k <= dp(n)) { rebuild(n, k, 0, n); for (int i = (int)0; i < (int)n; ++i) cout << res[i] << ' '; } else { cout << -1; } cout << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) int formula(string &s, int P, int Q, int R, int &i) { if(s[i] == '-') { i++; return 2-formula(s, P, Q, R, i); } else if(s[i] == '(') { int X, Y; i++; X = formula(s, P, Q, R, i); if(s[i] == '*') { i++; Y = formula(s, P, Q, R, i); i++; if(X == 0) return 0; if(X == 1) { if(Y == 0) return 0; else return 1; } else { return Y; } } else { i++; Y = formula(s, P, Q, R, i); i++; if(X == 0) return Y; if(X == 1) { if(Y == 0) return 1; else return Y; } else { return 2; } } } else { int ret; if(s[i] >= '0' && s[i] <= '2') ret = s[i]-'0'; else if(s[i] == 'P') ret = P; else if(s[i] == 'Q') ret = Q; else if(s[i] == 'R') ret = R; i++; return ret; } } int main(void){ string s; while(cin >> s && s != ".") { int cnt = 0; rep(i, 3) rep(j, 3) { rep(k, 3) { int index = 0; if(formula(s, i, j, k, index) == 2) cnt++; } } cout << cnt << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int k, r, min; cin >> k >> r; for (min = 1; min <= 10; min++) { if (k * min % 10 == 0 || k * min % 10 == r) break; } cout << min; }
1
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts("-1"); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { long long z = y; while (x % y != 0) { z = x % y; x = y; y = z; } return z; } int x[3], y[3], r[3], d[3]; int dis(int i, int p, int q) { int a = abs(p - x[i]), b = abs(q - y[i]); return a * a + b * b; } int p2(int x) { return x * x; } int cal2(int i, int j) { int D = p2(x[i] - x[j]) + p2(y[i] - y[j]); if (D >= p2(r[i] + r[j])) return 3; else if (p2(r[i] - r[j]) >= D) return 3; return 4; } struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} long double abs() const { return hypot(x, y); } long double arg() const { return atan2(y, x); } Point operator*(double o) const { return Point(x * o, y * o); } Point operator/(double o) const { return Point(x / o, y / o); } Point operator+(const Point &o) const { return Point(x + o.x, y + o.y); } Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); } bool operator<(const Point &o) const { return x < o.x - 1e-9 || (x < o.x + 1e-9 && y < o.y - 1e-9); } friend bool operator==(const Point &r1, const Point &r2) { return r1.x == r2.x && r1.y == r2.y; } Point scale(double o) const { return *this * (o / abs()); } Point rotY() const { return Point(-y, x); } Point rotX() const { return Point(y, -x); } double cross(Point b) const { return x * b.y - b.x * y; } double dot(Point b) const { return x * b.x + y * b.y; } long double disToSeg(Point &a, Point &b) { return fabs(((*this) - a).cross(b - a) / (b - a).abs()); } pair<long long, long long> getVec() { if (x == 0) return {0, y > 0 ? 1 : -1}; if (y == 0) return {x < 0 ? -1 : 1, 0}; long long gg = gcd1(::abs(x), ::abs(y)); long long px = x / gg, py = y / gg; return {px, py}; } void readin() { scanf("%lf%lf", &x, &y); } }; struct Circle { Point o; double r; Circle() {} Circle(Point _o, double _r) : o(_o), r(_r) {} vector<Point> getCrossPoint(Circle &c2) const { double d = (o - c2.o).abs(); if (d > r + c2.r + 1e-9 || d < fabs(r - c2.r) - 1e-9) return vector<Point>(); double dt = (r * r - c2.r * c2.r) / d, d1 = (d + dt) / 2; Point dir = (c2.o - o) / d, pcrs = o + dir * d1; dt = sqrt(max(0.0, r * r - d1 * d1)); dir = dir.rotY(); return vector<Point>{pcrs + dir * dt, pcrs - dir * dt}; } } p[5]; void fmain(int ID) { scanf("%d", &n); for (int(i) = 0; (i) < (int)(n); (i)++) { scanf("%d%d%d", x + i, y + i, r + i); p[i].o = Point(x[i], y[i]); p[i].r = r[i]; } if (n == 1) { puts("2"); return; } else if (n == 2) { printf("%d\n", cal2(0, 1)); return; } for (int(i) = 0; (i) < (int)(3); (i)++) { int a = (i + 1) % 3, b = (i + 2) % 3; int d1 = dis(a, x[i], y[i]); int d2 = dis(b, x[i], y[i]); bool f1 = d1 > p2(r[i] + r[a]) || d1 < p2(r[i] - r[a]); bool f2 = d2 > p2(r[i] + r[b]) || d2 < p2(r[i] - r[b]); if (f1 && f2) { printf("%d\n", cal2(a, b) + 1); return; } } int E = 0, V = 0; set<Point> tt, sp[3]; for (int(i) = 0; (i) < (int)(3); (i)++) for (int j = (i + 1); j < 3; j++) { vector<Point> tmp = p[i].getCrossPoint(p[j]); for (Point z : tmp) { tt.insert(z); sp[i].insert(z); sp[j].insert(z); } } V = tt.size(); for (int(i) = 0; (i) < (int)(3); (i)++) E += sp[i].size(); printf("%d\n", E - V + 2); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int NUM = 500 + 10; int n, first; int color[NUM], numAll = 0; int size[NUM], come[NUM]; vector<int> ne[NUM], w[NUM]; long long dist[NUM]; int f[NUM][NUM][NUM / 2]; int lab[NUM], tl[NUM][NUM]; vector<int> zishu[NUM]; void update(int& a, int b) { if (a == -1 || b < a) a = b; } int tmp[2][NUM]; void work(int num, int now, int last, int k, int nl) { int o; for ((o) = (0); (o) <= (num); ++(o)) if (tmp[last][o] != -1) update(tmp[now][o + k], tmp[last][o] + nl); } void dfs(int u, int fa) { int i, j, k; size[u] = 1; vector<int> son; for ((i) = (0); (i) != ((int)ne[u].size()); ++(i)) { int v = ne[u][i]; if (v == fa) continue; dist[v] = dist[u] + w[u][i]; dfs(v, u); size[u] += size[v]; son.push_back(v); come[v] = w[u][i]; for ((j) = (0); (j) != ((int)zishu[v].size()); ++(j)) { zishu[u].push_back(zishu[v][j]); lab[zishu[v][j]] = v; } } int Other = n + 1; size[Other] = 1; dist[Other] = dist[u]; lab[Other] = Other; for ((i) = (0); (i) != (2); ++(i)) for ((j) = (0); (j) != (2); ++(j)) f[Other][i][j] = -first - 2; if (color[u] == 1) f[Other][1][1] = first; f[Other][1][color[u] == 0] = first; f[Other][0][0] = -1; vector<pair<int, int> > Val; for ((i) = (0); (i) <= (size[u] - 1); ++(i)) { int v = (i < size[u] - 1 ? zishu[u][i] : Other); long long l = dist[v] - dist[u]; if (l > first) continue; Val.push_back(make_pair(l - first - 1, v * 2)); Val.push_back(make_pair(-l - 1, v * 2 + 1)); } sort(Val.begin(), Val.end()); zishu[u].push_back(u); for ((i) = (0); (i) != ((int)son.size()); ++(i)) { int v = son[i]; for ((j) = (0); (j) <= (size[v]); ++(j)) { tl[v][j] = min(size[v], n / 2); while (tl[v][j] && f[v][j][tl[v][j] - 1] >= 0) --tl[v][j]; } } for (i = Val.size() - 1; i >= 0; --i) { int w = Val[i].first, v; int p = Val[i].second / 2; int flag = Val[i].second % 2; for ((j) = (0); (j) != ((int)son.size()); ++(j)) { v = son[j]; for ((k) = (0); (k) <= (size[v]); ++(k)) for (int& l = tl[v][k]; l && f[v][k][l - 1] - come[v] >= w;) --l; } int now = 0, last = 1, num = 0; tmp[now][0] = 0; for ((j) = (0); (j) != ((int)son.size()); ++(j)) { int v = son[j]; if (v == lab[p] && flag == 0) continue; swap(now, last); for ((k) = (0); (k) <= (num + size[v]); ++(k)) tmp[now][k] = -1; for ((k) = (0); (k) <= (size[v]); ++(k)) { int l = tl[v][k]; if (f[v][k][l] < 0 && f[v][k][l] - come[v] < w) continue; work(num, now, last, k, l); } num += size[v]; } int nw = dist[p] - dist[u]; if (flag == 1) nw = -nw - 1; else { nw = first - nw; swap(now, last); v = lab[p]; for ((k) = (0); (k) <= (num + size[lab[p]]); ++(k)) tmp[now][k] = -1; int up = min(n / 2, size[v]); for ((k) = (0); (k) <= (size[v]); ++(k)) { int l = 0, r = up; while (l < r) { int mid = (l + r) / 2; if (f[v][k][mid] - come[v] >= nw) r = mid; else l = mid + 1; } if (l == up + 1 || f[v][k][l] - come[v] < nw) continue; work(num, now, last, k, l); } num += size[v]; } for ((j) = (0); (j) <= (num); ++(j)) { int a = tmp[now][j]; if (a != -1 && nw > f[u][j][a]) f[u][j][a] = nw; } } int up = min(n / 2, size[u]); for ((j) = (0); (j) <= (size[u]); ++(j)) for ((k) = (1); (k) <= (up); ++(k)) f[u][j][k] = max(f[u][j][k], f[u][j][k - 1]); } int main() { int i, j, k; scanf("%d%d", &n, &first); for ((i) = (1); (i) <= (n); ++(i)) { scanf("%d", &color[i]); numAll += color[i]; } for ((i) = (0); (i) != (NUM); ++(i)) for ((j) = (0); (j) != (NUM); ++(j)) for ((k) = (0); (k) != (NUM / 2); ++(k)) f[i][j][k] = -first - 2; for ((i) = (1); (i) <= (n - 1); ++(i)) { int a, b, c; scanf("%d%d%d", &a, &b, &c); ne[a].push_back(b); w[a].push_back(c); ne[b].push_back(a); w[b].push_back(c); } dfs(1, 0); int ans = -1; for (i = 0; i <= n / 2; ++i) if (f[1][numAll][i] >= 0) { ans = i; break; } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") long long i, j, k, l, n, m, ans = 1; long long a[100009]; vector<long long> p, res; long long query(long long t, long long x) { long long res = 0; if (t == 1) { cout << 'A' << ' ' << x << '\n'; cout.flush(); cin >> res; } if (t == 2) { cout << 'B' << ' ' << x << '\n'; cout.flush(); cin >> res; } if (t == 3) { cout << 'C' << ' ' << x << '\n'; cout.flush(); exit(0); } return res; } int main() { cin >> n; p.push_back(0); for (i = 2; i <= n; i++) { if (a[i] == 0) { for (j = i * i; j <= n; j += i) a[j] = 1; p.push_back(i); } } m = p.size() - 1; long long flag = 0, l = 0, r = 0; long long bl = sqrt(m) + 1; memset(a, 0, sizeof(a)); for (i = 1; i <= m; i++) { k = query(2, p[i]); long long cnt = 0; for (j = p[i]; j <= n; j += p[i]) { cnt += a[j] == 0; a[j] = 1; } if (cnt != k) { res.push_back(p[i]); } if (flag) continue; if (i % bl == 0 || i == m) { k = query(1, 1); cnt = 0; for (j = 1; j <= n; j++) { cnt += a[j] == 0; } if (cnt != k) { flag = 1; l = ((i - 1) / bl) * bl + 1; r = i; } } } if (l != 0) { for (i = l; i <= r; i++) { if (query(1, p[i]) > 0) { res.push_back(p[i]); break; } } } for (auto to : res) { long long num = to; while (num * to <= n) { num *= to; if (query(1, num) == 0) { num /= to; break; } } ans *= num; } query(3, ans); exit(0); }
5
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230; long long n, m; bool check(long long x) { long long i = (x * x + x) / 2; long long j = (n - m); return i >= j; } bool cmp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b) { if (a.first.first == b.first.first) return a.first.second > b.first.second; return a.first.first < b.first.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, i, x, y; cin >> n; vector<pair<pair<int, int>, int>> v; for (i = 0; i < n; i++) { cin >> x >> y; v.push_back({{x, y}, i + 1}); } sort((v).begin(), (v).end(), cmp); int g = 0; int first = 1; for (i = 1; i < v.size(); i++) { if (v[i].first.second <= v[g].first.second) { cout << v[i].second << " " << v[g].second; first = 0; break; } else { g = i; } } if (first) cout << "-1 -1"; return 0; }
3
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <string> int main() { int n; std::cin >> n; std::vector<std::tuple<int, int, std::string, long long, std::string>> tp(0); int v, w; long long d; std::string t, s; for (int i = 0; i < n; i++) { std::cin >> v >> w >> t >> d >> s; auto tuple = std::make_tuple(v, w, t, d, s); tp.push_back(tuple); } std::sort(tp.begin(), tp.end()); for (auto &x : tp) { std::cout << std::get<0>(x) << " " << std::get<1>(x) << " " << std::get<2>(x) << " " << std::get<3>(x) << " " << std::get<4>(x) << std::endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; pair<long long int, long long int> dxy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const long long MAXN = 1e5 + 10, MOD = 1e9 + 7, INF = ((1ll << 60) - 1) * 2 + 1; long long int n, m; long long int t[4 * MAXN]; long long int a[4 * MAXN]; long long int arr[MAXN]; long long int d[MAXN]; void push(long long int v) { t[v] += a[v]; if (v * 2 < 4 * MAXN) a[v * 2] += a[v]; if (v * 2 + 1 < 4 * MAXN) a[v * 2 + 1] += a[v]; a[v] = 0; } void ad(long long int v, long long int l0, long long int r0, long long int l, long long int r, long long int val) { if (r <= l) return; if (l0 == l && r0 == r) { a[v] += val; return; } long long int mid = (l0 + r0) >> 1; ad(v * 2, l0, mid, l, min(mid, r), val); ad(v * 2 + 1, mid, r0, max(mid, l), r, val); } long long int gt(long long int v, long long int l0, long long int r0, long long int id) { push(v); if (l0 + 1 == r0) return t[v]; long long int mid = (l0 + r0) >> 1; if (id < mid) return gt(v * 2, l0, mid, id); return gt(v * 2 + 1, mid, r0, id); } void build(long long int v, long long int l0, long long int r0) { if (l0 + 1 == r0) { t[v] = arr[l0]; return; } long long int mid = (l0 + r0) >> 1; build(v * 2, l0, mid); build(v * 2 + 1, mid, r0); } void solve() { cin >> n; for (long long int i = 0; i < (n); ++i) cin >> arr[i]; build(1, 0, n); long long int sumd = 0; for (long long int i = 1; i <= (n - 1); ++i) { d[i] = max(0ll, arr[i] - arr[i - 1]); sumd += d[i]; } long long int sm = arr[0] + sumd; if (sm >= 0) cout << (sm + 1) / 2 << "\n"; else cout << sm / 2 << "\n"; cin >> m; while (m--) { long long int l, r, val; cin >> l >> r >> val; --l; ad(1, 0, n, l, r, val); if (l > 0) { sumd -= d[l]; d[l] = max(0ll, gt(1, 0, n, l) - gt(1, 0, n, l - 1)); sumd += d[l]; } else arr[0] = gt(1, 0, n, 0); sumd -= d[r]; d[r] = max(0ll, gt(1, 0, n, r) - gt(1, 0, n, r - 1)); sumd += d[r]; long long int sm = arr[0] + sumd; if (sm >= 0) cout << (sm + 1) / 2 << "\n"; else cout << sm / 2 << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); }
4
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<pair<int,int>> a(n),b(n); for(int i=0;i<n;i++){ int k,l; cin >> k >> l ; a[i]={l,k}; } for(int i=0;i<n;i++){ int k,l; cin >> k >> l ; b[i]={k,l}; } sort(a.begin(),a.end()); reverse(a.begin(),a.end()); sort(b.begin(),b.end()); int count=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i].second<b[j].first && a[i].first<b[j].second) {count++; b[j]={INT_MIN,INT_MIN}; break; } } } cout << count << endl; }
0
#include <iostream> #include <algorithm> #include <string> #include <vector> #define frs first #define scn second #define phb push_back #define ppb pop_back #define mkp make_pair using namespace std; typedef pair< int, int > Pr; typedef pair< int, Pr > Trp; int N, M; string txt[10], ptr[10]; void read(); void soups_on(); vector< int > yee_haw(vector< Trp >, string [], int); bool not_dot(const char &); int main() { ios::sync_with_stdio(false); while (cin >> N >> M, N + M) read(), soups_on(); return 0; } void read() { for (int i = 0; i < N; ++i) cin >> txt[i]; for (int i = 0; i < M; ++i) cin >> ptr[i]; } void soups_on() { vector< int > p, q, t; vector< Trp > tmp, res; for (int i = 0; i < N; ++i) p.phb(int(find_if(txt[i].begin(), txt[i].end(), not_dot) - txt[i].begin())); for (int i = 1; i <= 20; ++i) for (int j = 1; j <= 20; ++j) for (int k = 1; k <= 20; ++k) { tmp.phb(mkp(i, mkp(j, k))); t = yee_haw(tmp, txt, N); bool is_valid = true; for (int c = 0; c < N && is_valid; ++c) if (p[c] != t[c]) is_valid = false; if (is_valid) res.phb(mkp(i, mkp(j, k))); tmp.ppb(); } q = yee_haw(res, ptr, M); for (int i = 0; i < M; ++i) { if (i > 0) cout << " "; cout << q[i]; } cout << "\n"; } vector< int > yee_haw(vector< Trp > t, string p[], int k) { vector< int > ret; int c[3], f; fill(&c[0], &c[3], 0); for (int i = 0; i < k; ++i) { vector< int > pv; for (int j = 0; j < int(t.size()); ++j) { f = c[0] * t[j].frs + c[1] * t[j].scn.frs + c[2] * t[j].scn.scn; pv.phb(f); } stable_sort(pv.begin(), pv.end()); pv.erase(unique(pv.begin(), pv.end()), pv.end()); if (int(pv.size()) > 1) ret.phb(-1); else ret.phb(pv[0]); c[0] += count(p[i].begin(), p[i].end(), '('); c[0] -= count(p[i].begin(), p[i].end(), ')'); c[1] += count(p[i].begin(), p[i].end(), '['); c[1] -= count(p[i].begin(), p[i].end(), ']'); c[2] += count(p[i].begin(), p[i].end(), '{'); c[2] -= count(p[i].begin(), p[i].end(), '}'); } return ret; } bool not_dot(const char &rc) { return rc != '.'; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9 + 5; const long long INF64 = 1e18; string to_string(const string &s) { return '"' + s + '"'; } string to_string(bool b) { return b ? "true" : "false"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename T> string to_string(const vector<T> &v) { string ret = "<"; for (auto it : v) ret += to_string(it) + ", "; ret.pop_back(); ret.pop_back(); ret += ">"; return ret; } void debug_print() { cerr << endl; } template <typename T, typename... U> void debug_print(T t, U... u) { cerr << " " << to_string(t); debug_print(u...); } const int N = 1e5 + 5; string s; int n; long long powmod(long long a, long long b) { long long ret = 1ll; while (b) { if (b & 1) ret = ret * a % MOD; b = b / 2; a = (a * a) % MOD; } return ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int a = 0, c = 0, g = 0, t = 0; int mx = -1; int cnt = 0; cin >> n >> s; for (auto it : s) { if (it == 'A') { a++; if (a > mx) { mx = a; cnt = 1; } else if (a == mx) cnt++; } if (it == 'C') { c++; if (c > mx) { mx = c; cnt = 1; } else if (c == mx) cnt++; } if (it == 'G') { g++; if (g > mx) { mx = g; cnt = 1; } else if (g == mx) cnt++; } if (it == 'T') { t++; if (t > mx) { mx = t; cnt = 1; } else if (t == mx) cnt++; } } cout << powmod(cnt, n) << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, a[3005]; bool bad[3005]; set<pair<long long int, int>> s; int get_l() { int an = n + 1; int cnt = 0; int cur = 0; int j = 1; while (bad[j]) j++; for (int i = n; i >= j; i--) { if (bad[i]) cnt--; else cnt++; if (cnt >= cur) an = i, cur = cnt; } return an; } int get_cnt(int l) { int an = 0; for (int i = l; i <= n; i++) if (bad[i]) an--; else an++; return an; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int cur = 0; long long int ans = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; cur += (a[i] + 1e9 - i); s.insert(make_pair(a[i] + 1e9 - i, i)); } int l; long long int d = 0; ans = cur; while (l <= n) { l = get_l(); int cnt = get_cnt(l); while (s.size() && s.begin()->second < l) s.erase(s.begin()); if (s.size() == 0) break; long long int p = s.begin()->first; cur -= (p - d) * cnt; d = p; while (s.size() && s.begin()->first == d) { bad[s.begin()->second] = 1; s.erase(s.begin()); } ans = min(ans, cur); } cout << ans << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> g[N]; vector<int> p; vector<long long> a; vector<long long> s; deque<int> cur; bool f = true; void dfs(int v, int k) { if (k % 2 == 0) { long long mn = 1e9; int v1 = cur[int(cur.size()) - 1]; for (int i : g[v]) { mn = min(mn, s[i] - s[v1]); } if (mn < 0) f = false; if (g[v].size() == 0) mn = 0; a[v] = mn; } else if (v != 0) { int v1 = cur[int(cur.size()) - 1]; int v2 = cur[int(cur.size()) - 2]; a[v] = s[v] - s[v2] - a[v1]; } cur.push_back(v); for (int i : g[v]) { dfs(i, k + 1); } cur.pop_back(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; p.resize(n); for (int i = 1; i < n; i++) { cin >> p[i]; p[i]--; g[p[i]].push_back(i); } s.resize(n); a.resize(n, 0); for (int i = 0; i < n; i++) cin >> s[i]; a[0] = s[0]; dfs(0, 1); if (!f) { cout << -1; return 0; } long long sum = 0; for (int i = 0; i < n; i++) sum += a[i]; cout << sum; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, i; vector<pair<int, int> > v; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &x, &y); v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); x = v[0].second; y = 0; for (i = 1; i < n; i++) { if (v[i].second < x) y++; else x = v[i].second; } cout << y << endl; }
3
#include<bits/stdc++.h> using namespace std; int main() { float a,b,c; cin >>a>>b>>c; cout << (a*b)/2 << endl; }
0
#include <bits/stdc++.h> using namespace std; long long i, j, n, k, k1, k2, k3, m, fldgjdflgjhrthrl, fldggfhfghjdflgjl, fldgjdflgrtyrtyjl, ffgfldgjdflgjl, h, x, y; string s1, s2, s3, s; long long dp[3][505][505]; char c[505][505]; int main() { cin >> n >> m; for (i = 0; i < n; i++) for (j = 0; j < m; j++) cin >> c[i][j]; long long pos = (n + m - 2) / 2; if (c[0][0] == c[n - 1][m - 1]) dp[0][0][n - 1] = 1; for (int i = 0; i < pos; i++) { for (j = 0; j < n; j++) for (k = 0; k < n; k++) { dp[(i + 1) & 1][j][k] = 0; while (dp[i & 1][j][k] >= 1000000007) dp[i & 1][j][k] -= 1000000007; } for (int first_row = 0; first_row < n; first_row++) for (int last_row = 0; last_row < n; last_row++) { long long first_col = i - first_row; long long last_col = m - 1 - (i - (n - 1 - last_row)); j = first_row; k = last_row; if (first_col < m - 1 && last_col > 0 && c[first_row][first_col + 1] == c[last_row][last_col - 1]) dp[(i + 1) & 1][j][k] = (dp[(i + 1) & 1][j][k] + dp[i & 1][j][k]); if (first_col < m - 1 && last_row > 0 && c[first_row][first_col + 1] == c[last_row - 1][last_col]) dp[(i + 1) & 1][j][k - 1] = (dp[(i + 1) & 1][j][k - 1] + dp[i & 1][j][k]); if (first_row < n - 1 && last_col > 0 && c[first_row + 1][first_col] == c[last_row][last_col - 1]) dp[(i + 1) & 1][j + 1][k] = (dp[(i + 1) & 1][j + 1][k] + dp[i & 1][j][k]); if (first_row < n - 1 && last_row > 0 && c[first_row + 1][first_col] == c[last_row - 1][last_col]) dp[(i + 1) & 1][j + 1][k - 1] = (dp[(i + 1) & 1][j + 1][k - 1] + dp[i & 1][j][k]); } } long long ans = 0; if ((n + m) % 2 == 0) { for (i = 0; i < n; i++) ans += dp[pos & 1][i][i]; ans %= 1000000007; cout << ans << endl; } else { for (i = 0; i < n; i++) { long long first_col = pos - i; if (c[i][first_col] == c[i][first_col + 1]) ans += dp[pos & 1][i][i]; } for (i = 0; i < n - 1; i++) { long long first_col = pos - i; if (c[i][first_col] == c[i + 1][first_col]) ans += dp[pos & 1][i][i + 1]; } ans %= 1000000007; cout << ans << endl; } return 0; }
5
#include <cstdio> #include <iostream> #include <string> #include <vector> #include <sstream> #include <map> #include <set> #include <queue> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <typeinfo> #include <numeric> #include <functional> #include <unordered_map> #include <bitset> using namespace std; using ll = long long; using ull = unsigned long long; const ll INF = 1e16; const ll MOD = 1e9 + 7; #define REP(i, n) for(ll i = 0; i < n; i++) template <typename T> class dijkstra { private: using T_nao = pair<T, pair<T, T>>; struct edge{ long long to; T cost; }; long long vertex; // 頂点数 vector<vector<edge>> list; //隣接リスト vector<vector<T>> distance; // 距離 public: dijkstra(long long n, T infinity = 1e16); // nは頂点の数、infinityは初期化に使う値 void add_edge(long long v1, long long v2, T cost); // 頂点v1から頂点v2に辺を張る void run(long long s, long long t); // sは開始地点。 T get_distance(long long v); // 頂点vへの距離を返す }; template <typename T> dijkstra<T>::dijkstra(long long n, T infinity){ vertex = n; list.resize(n); distance = vector<vector<T>>(vertex, vector<T>(2, infinity)); } template <typename T> void dijkstra<T>::add_edge(long long v1, long long v2, T cost){ list[v1].push_back({ v2, cost }); } template <typename T> void dijkstra<T>::run(long long s, long long t){ priority_queue<T_nao, vector<T_nao>, greater<T_nao>> que; que.push({0, {s, 1}}); while(!que.empty()){ T_nao p = que.top(); que.pop(); T v = p.second.first; T w = p.second.second; if(distance[v][w] < p.first) continue; distance[v][w] = p.first; if(v == t) return; for(auto e : list[v]){ que.push({p.first + e.cost, {e.to, w}}); if(w == 0)continue; for(auto ee : list[e.to]){ que.push({p.first, {ee.to, 0}}); } } } } template <typename T> T dijkstra<T>::get_distance(long long v){ return min(distance[v][0], distance[v][1]); } int main() { ll n, m; cin >> n >> m; while(n != 0 || m != 0){ dijkstra<ll> dik(n); REP(i, m){ ll a, b, c; cin >> a >> b >> c; a--;b--; dik.add_edge(a, b, c); dik.add_edge(b, a, c); } dik.run(0, n - 1); cout << dik.get_distance(n - 1) << endl; cin >> n >> m; } }
0
#include <iostream> #include <string> #include <set> using namespace std; int main(){ int n; cin>>n; for(int i=0;i<n;i++){ string s; cin>>s; int len=s.size(); set<string> se; se.insert(s); for(int j=1;j<len;j++){ string s1,s2; s1=s.substr(0,j); s2=s.substr(j,len-j); int l1=s1.size(),l2=s2.size(); string s1r=s1,s2r=s2; for(int k=0;k<l1;k++){ s1r[k]=s1[l1-k-1]; } for(int k=0;k<l2;k++){ s2r[k]=s2[l2-k-1]; } se.insert(s1+s2r); se.insert(s1r+s2); se.insert(s1r+s2r); se.insert(s2+s1); se.insert(s2r+s1); se.insert(s2+s1r); se.insert(s2r+s1r); } cout<<se.size()<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template<class S, class T> istream& operator>>(istream& is, pair<S,T>& p){ return is >> p.FF >> p.SS; } template<class S, class T> ostream& operator<<(ostream& os, const pair<S,T>& p){ return os << p.FF << " " << p.SS; } template<class T> void maxi(T& x, T y){ if(x < y) x = y; } template<class T> void mini(T& x, T y){ if(x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; int H, N; int init[20][2][2]; int blk[8][2][2]; void fall(vector<VVI>& fd, vector<VVI>& bl){ int y; for(y=H+6;y>0;--y){ bool done = false; REP(dy,2) REP(d,2) REP(x,2){ if(bl[dy][d][x] && fd[y+dy-1][d][x]) done = true; } if(done) break; } bool f = true; REP(d,2) REP(x,2) if(bl[0][d][x]) f = false; if(f && y == 0){ --y; } REP(dy,2) REP(d,2) REP(x,2){ if(bl[dy][d][x]) fd[y+dy][d][x] = 1; } } int del(vector<VVI>& fd){ int res = 0; for(int y=19;y>=0;--y){ bool fill = true; REP(d,2) REP(x,2) if(!fd[y][d][x]) fill = false; if(fill){ ++res; FOR(ty,y,19) REP(d,2) REP(x,2) fd[ty][d][x] = fd[ty+1][d][x]; REP(d,2) REP(x,2) fd[19][d][x] = 0; } } return res; } int check(const vector<PII>& ps){ vector<VVI> fd(20, VVI(2, VI(2))); REP(y,20) REP(d,2) REP(x,2) fd[y][d][x] = init[y][d][x]; int res = 0; REP(i,N){ vector<VVI> vblk(2, VVI(2, VI(2))); REP(y,2) REP(d,2) REP(x,2) if(blk[i*2+y][d][x]){ vblk[y][d+ps[i].FF][x+ps[i].SS] = 1; } fall(fd, vblk); res += del(fd); } return res; } int dfs(int i, vector<PII>& ps){ if(i == N) return check(ps); int res = 0; FOR(dd,-1,2) FOR(dx,-1,2){ bool ok = true; REP(y,2) REP(d,2) REP(x,2){ if(blk[i*2+y][d][x]){ int td = d + dd, tx = x + dx; if(td < 0 || 2 <= td || tx < 0 || 2 <= tx) ok = false; } } if(!ok) continue; ps.EB(dd, dx); maxi(res, dfs(i+1, ps)); ps.pop_back(); } return res; } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); while(cin>>H>>N,H){ fill((int*)init, (int*)init+20*2*2,0); fill((int*)blk, (int*)blk+8*2*2,0); REP(y,H) REP(d,2) REP(x,2){ char c; cin >> c; init[y][d][x] = c == '#'; } REP(i,N*2) REP(d,2) REP(x,2){ char c; cin >> c; blk[i][d][x] = c == '#'; } vector<PII> ps; cout << dfs(0,ps) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1 << 28; long long gcd(long long m, long long n) { long long r; while (n != 0) { r = m % n; m = n; n = r; } return m; } long long expmod(long long a, long long b) { long long x = 1, y = a; while (b > 0) { if (b & 1) x = (x * y) % 1000000007; y = (y * y) % 1000000007; b >>= 1; } return (x) % 1000000007; } long long inv(long long n) { return expmod(n, 1000000007 - 2); } int main() { int n; cin >> n; int a = 0, b = 0; int sum = 0; for (int i = 0; i < n; i++) { int temp; cin >> temp; if (temp == 100) a++; else b++; } b = b % 2; if (b == 1) { if (a <= 1) { cout << "NO" << endl; return 0; } a = a - 2; } if (a % 2 == 1) { cout << "NO" << endl; } else cout << "YES" << endl; return 0; }
1
#include <bits/stdc++.h> int main() { int i, j, n, x = 0; scanf("%d", &n); char str[n][6]; for (i = 0; i < n; i++) { scanf("%s", str[i]); } for (i = 0; i < n; i++) { for (j = 0; j < 5; j++) { if (str[i][0] == 'O' && str[i][1] == 'O') { x++; str[i][0] = '+'; str[i][1] = '+'; } else if (str[i][3] == 'O' && str[i][4] == 'O') { x++; str[i][3] = '+'; str[i][4] = '+'; } if (x > 0) { break; } } if (x > 0) { break; } } if (x > 0) { printf("YES\n"); for (i = 0; i < n; i++) { printf("%s\n", str[i]); } } else printf("NO\n"); }
1
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; cout << (a % 2); }
1
#include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define pii pair<int,int> #define sz size() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define vi vector<int> #define vii vector<pair<int,int>> void solve(){ int n,ll,rr; cin>>n>>ll>>rr; vector<int>a(n); for(int i=0;i<n;i++)cin>>a[i]; sort(all(a)); int ans=0; for(int i=0;i<n-1;i++){ int l=i+1; int r=n-1; if(a[i]>rr)break; int k1=ll-a[i]; int k2=rr-a[i]; int idx=lower_bound(a.begin()+i+1,a.end(),k1)-a.begin(); int idx1=upper_bound(a.begin()+i+1,a.end(),k2)-a.begin(); //cout<<idx<<" "<<idx1<<endl; ans+=idx1-idx; } cout<<ans<<endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; //T=1; cin>>T; while(T--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 5005; map<int, int> M; int n, v[N], b, d[N], sol; int value(int x) { int val = 0, auxn = x; while (auxn % 2 == 0) auxn /= 2, val += M[2] + 1; for (int i = 3; i * i <= x && auxn != 1; i += 2) while (auxn % i == 0) auxn /= i, val += M[i] + 1; if (auxn != 1) val += M[auxn] + 1; return val; } int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { cin >> n >> b; for (int i = 0; i < n; ++i) cin >> v[i]; while (b--) { int x; cin >> x; M[x] = -2; } d[0] = v[0]; for (int i = 1; i < n; ++i) d[i] = gcd(d[i - 1], v[i]); int g = 1; for (int i = n - 1; i >= 0; --i) { if (value(d[i] / g) <= 0) g = d[i]; sol += value(v[i] / g); } cout << sol; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXM = 1000005; const int MOD = 1000000007; const int MAMOD = 998244353; const int INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1.0); const double EPS = 1e-8; int main() { double n, r, x, R; scanf("%lf%lf", &n, &r); x = sin(PI / n); R = (r / (1 - x)) - r; printf("%.9lf\n", R); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MOD = 12345; const int STD_SIZE = 130; int msize; struct Matrix { int mat[STD_SIZE][STD_SIZE]; Matrix() { memset(mat, 0, sizeof mat); } inline Matrix operator*(const Matrix& b) { Matrix ret; for (int i = 0; i < msize; i++) for (int j = 0; j < msize; j++) for (int k = 0; k < msize; k++) ret.mat[i][j] = (ret.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD; return ret; } }; map<char, vector<int> > mp; int lvl[30], prd[30]; bool vis[STD_SIZE], nvalid[STD_SIZE]; int main() { long long N; int C; cin >> N; scanf("%d", &C); for (int i = 1; i <= C; i++) { char buf[10]; int val; scanf("%s%d", buf, &val); mp[buf[0]].push_back(val); } for (char chr = 'A'; chr <= 'Z'; chr++) { vector<int>& tmp = mp[chr]; sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end()); lvl[chr - 'A' + 1] = 1; for (int i = 0; i < (int)tmp.size(); i++) lvl[chr - 'A' + 1] = lvl[chr - 'A' + 1] * tmp[i]; } prd[0] = lvl[0] = 1; for (int i = 1; i <= 26; i++) prd[i] = prd[i - 1] * lvl[i]; msize = prd[26]; Matrix unit, mfac; for (int i = 0; i < msize; i++) unit.mat[i][i] = 1; for (int cur = 0; cur < msize; cur++) { for (int nxt = 0; nxt < 26; nxt++) { if (mp['A' + nxt].size() == 0) continue; int cbit = cur / prd[nxt] % lvl[nxt + 1]; int nbit = (cbit + 1) % lvl[nxt + 1]; int delta = (nbit - cbit) * prd[nxt]; mfac.mat[cur][cur + delta]++; } } while (N) { if (N & 1) unit = unit * mfac; mfac = mfac * mfac; N >>= 1; } int ans = 0; for (int chr = 0; chr < 26; chr++) { vector<int>& tmp = mp['A' + chr]; if (tmp.size() == 0) continue; memset(vis, 0, sizeof vis); for (int i = 0; i < (int)tmp.size(); i++) for (int j = 0; j < lvl[1 + chr]; j += tmp[i]) vis[j] = true; for (int i = 0; i < msize; i++) { int cbit = i / prd[chr] % lvl[chr + 1]; if (!vis[cbit]) nvalid[i] = true; } } for (int i = 0; i < msize; i++) { if (nvalid[i]) continue; ans = (ans + unit.mat[0][i]); } cout << ans % MOD << '\n'; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double PI = acos(-1.0); const int MAXN = 1e6 + 5; vector<long long int> lucky; void init(long long int num) { if (num > 1e10) return; if (num) lucky.push_back(num); init(num * 10 + 4); init(num * 10 + 7); } void cp() { long long int l, r; cin >> l >> r; long long int left = l; long long int sum = 0; int ind = lower_bound(lucky.begin(), lucky.end(), left) - begin(lucky); for (int i = ind; i < ((int)lucky.size()); i++) { if (!sum) sum += (min(r, lucky[i]) - left + 1) * lucky[i]; else sum += (min(r, lucky[i]) - left) * lucky[i]; left = min(r, lucky[i]); } cout << sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; init(0); sort(lucky.begin(), lucky.end()); while (t--) { cp(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int i, n, p, q, sum = 0, w, a[109], d[109] = {0}, b[109], c[109], j, sum1 = 0, sum2 = 0; cin >> n >> w; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 0) { sum = sum + (a[i] / 2); b[i] = sum1 + (a[i] / 2); } else { sum = sum + ((a[i] / 2) + 1); b[i] = sum1 + ((a[i] / 2) + 1); } c[i] = sum2 + a[i]; } if (sum <= w) { sort(c, c + n, greater<int>()); p = w - sum; for (j = 0; j < n; j++) { for (i = 0; i < n; i++) { if (c[j] == a[i] && d[i] != (-1)) { q = c[j] - b[i]; if (p >= q) { b[i] = b[i] + q; p = p - q; d[i] = -1; break; } else if (p < q) { b[i] = b[i] + p; p = 0; d[i] = -1; break; } } } if (p == 0) { break; } } for (i = 0; i < n; i++) { cout << b[i] << " "; } } else cout << "-1"; }
3
#include <bits/stdc++.h> int main() { int n, p; int x[600] = {}; int s = -1; int i, j, k; scanf("%d%d", &p, &n); for (i = 1; i <= n; i++) { scanf("%d", &j); if (x[j % p] == 0) { x[j % p] = i; } else { s = i; break; } } printf("%d\n", s); return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { static char c; static int f; for (c = getchar(), f = 1; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -f; for (x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + (c & 15); x *= f; } template <typename T> void write(T x) { static char q[65]; int cnt = 0; if (x < 0) putchar('-'), x = -x; q[++cnt] = x % 10, x /= 10; while (x) q[++cnt] = x % 10, x /= 10; while (cnt) putchar(q[cnt--] + '0'); } const int maxn = 305; struct Edge { int v; long long w; int nt; Edge(int v = 0, long long w = 0, int nt = 0) : v(v), w(w), nt(nt) {} } e[maxn * maxn * 2 + maxn * 4]; int hd[maxn * 2], num = 1; void qwq(int u, int v, long long w) { e[++num] = Edge(v, w, hd[u]), hd[u] = num; } void qvq(int u, int v, long long w) { qwq(u, v, w); qwq(v, u, 0); } int S, T, cur[maxn * 2], q[maxn * 2], dis[maxn * 2]; int bfs(void) { memset(dis, 0, sizeof dis); int fro = 0, bac = 0; dis[q[bac++] = S] = 1; while (fro < bac) { int u = q[fro++]; for (int i = hd[u]; i; i = e[i].nt) { int v = e[i].v; long long w = e[i].w; if (w && !dis[v]) dis[q[bac++] = v] = dis[u] + 1; } } return dis[T] > 0; } long long dfs(int u, long long ep) { if (u == T) return ep; long long re = 0; for (int& i = cur[u]; i; i = e[i].nt) { int v = e[i].v; long long w = e[i].w; if (w && dis[v] == dis[u] + 1) { long long tmp = dfs(v, min(ep, w)); re += tmp; ep -= tmp; e[i].w -= tmp; e[i ^ 1].w += tmp; if (!ep) break; } } return re; } long long solve(void) { long long re = 0; while (bfs()) { memcpy(cur, hd, sizeof hd); re += dfs(S, 1000000000000000ll); } return re; } int main() { int n; read(n); for (int i = 1; i <= n; ++i) { int k; read(k); for (int j = 1; j <= k; ++j) { int s; read(s); qvq(i, s + n, 1000000000000000ll); } } S = n * 2 + 1, T = S + 1; long long ans = 0; for (int i = 1; i <= n; ++i) { long long w; read(w); w = 1000000000ll - w; ans += w; qvq(S, i, w); } for (int i = 1; i <= n; ++i) qvq(i + n, T, 1000000000ll); write(solve() - ans), putchar('\n'); return 0; }
5
#include <bits/stdc++.h> using namespace std; int L[200010], R[200010]; int len[200010]; int s[200010]; int inter(int u, int v) { return max(0, min(R[u], R[v]) - max(L[u], L[v]) + 1); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> L[i]; for (int i = 1; i <= n; i++) cin >> R[i]; L[0] = R[0] = 0; for (int i = 0; i <= n; i++) len[i] = R[i] - L[i] + 1; for (int i = 1; i <= n; i++) { s[i] = ((long long)len[i] * len[i - 1] - inter(i, i - 1)) % 1000000007 * power((long long)len[i] * len[i - 1] % 1000000007, 1000000007 - 2, 1000000007) % 1000000007; } long long ans = 0; for (int i = 1; i <= n; i++) ans += s[i]; ans %= 1000000007; ans = ans * ans % 1000000007; for (int i = 1; i <= n; i++) { ans += s[i]; ans -= (long long)s[i] * s[i] % 1000000007; ans %= 1000000007; } ans = ans % 1000000007 + 1000000007; ans %= 1000000007; for (int i = 1; i <= n - 1; i++) { ans -= (long long)2 * s[i] * s[i + 1] % 1000000007; long long tot = (long long)len[i - 1] * len[i] % 1000000007 * len[i + 1] % 1000000007; tot -= (long long)inter(i - 1, i) * len[i + 1] % 1000000007 + (long long)inter(i, i + 1) * len[i - 1] % 1000000007; tot += max(0, min(min(R[i - 1], R[i]), R[i + 1]) - max(max(L[i - 1], L[i + 1]), L[i]) + 1); tot = tot % 1000000007 * power((long long)len[i - 1] * len[i] % 1000000007 * len[i + 1] % 1000000007, 1000000007 - 2, 1000000007) % 1000000007; if (tot < 0) tot += 1000000007; ans += 2 * tot; ans %= 1000000007; } ans %= 1000000007; if (ans < 0) ans += 1000000007; cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; struct compare { bool operator()(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2) const { if (p1.first.first / 1000 != p2.first.first / 1000) return p1.first.first / 1000 < p2.first.first / 1000; else return (p1.first.first / 1000) % 2 ? p1.first.second < p2.first.second : p1.first.second > p2.first.second; } }; int N; vector<pair<pair<int, int>, int>> P; int main() { cin >> N; for (int i = 1; i <= N; i++) { int x, y; scanf("%d %d", &x, &y); P.push_back({{x, y}, i}); } sort(P.begin(), P.end(), compare()); for (int i = 0; i < N; i++) printf("%d ", P[i].second); }
3
#include <bits/stdc++.h> using namespace std; signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(NULL); long long t; cin >> t; for (long long g = 0; g < t; g++) { long long n, k, d; cin >> n >> k >> d; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long Min = 1e9; map<long long, long long> m; long long col = 0; for (long long i = 0; i < d; i++) { if (m[a[i]] == 0) { col++; } m[a[i]]++; } long long down = 0; Min = col; for (long long i = d; i < n; i++) { if (m[a[down]] == 1) { col--; } m[a[down]]--; if (m[a[i]] == 0) col++; m[a[i]]++; Min = min(Min, col); down++; } cout << Min << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int N,Z=0; cin>>N; vector<int> p(N); for(int i=0;i<N;i++){ cin>>p.at(i); } vector<int> q(N); for(int i=N-1;i>=0;i--){ int A=0; for(int j=i*2+1;j<N;j+=i+1){ A+=q.at(j); } if((A+p.at(i))%2==1){ q.at(i)=1; Z++; } } cout<<Z<<endl; for(int k=0;k<N;k++){ if(q.at(k)==1){ cout<<k+1<<endl; } } }
0
#include <bits/stdc++.h> using namespace std; int a, n, d, t, v; double ans[100001]; double gets_ans() { double t1 = v * 1.0 / a; if (0.5 * a * t1 * t1 <= d) return t + t1 + (d - 0.5 * a * t1 * t1) / v; return sqrt(2.0 * d / a) + t; } int main() { cin >> n >> a >> d; for (int i = 0; i < n; i++) { cin >> t >> v; ans[i] = gets_ans(); if (i != 0 && ans[i] < ans[i - 1]) ans[i] = ans[i - 1]; printf("%.10lf\n", ans[i]); } }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 112345, CEM = 112; vector<int> grafo[MAXN]; int match_a[MAXN], match_b[MAXN], dist_a[MAXN], dist_b[MAXN], fila[MAXN], A, B; bool mrk_a[MAXN], mrk_b[MAXN]; bool BFS() { int ini = 1, fim = 0; for (int i = 1; i <= A; i++) dist_a[i] = 0; for (int i = 1; i <= B; i++) dist_b[i] = 0; for (int i = 1; i <= A; i++) { if (!match_a[i]) fila[++fim] = i; } bool flag = false; while (ini <= fim) { int v = fila[ini]; int tam = grafo[v].size(); for (int i = 0; i < tam; i++) { int w = grafo[v][i]; if (dist_b[w]) continue; dist_b[w] = dist_a[v] + 1; if (!match_b[w]) flag = true; else { dist_a[match_b[w]] = dist_b[w] + 1; fila[++fim] = match_b[w]; } } ini++; } return flag; } bool DFS(int v) { int tam = grafo[v].size(); for (int i = 0; i < tam; i++) { int w = grafo[v][i]; if (dist_b[w] != dist_a[v] + 1) continue; dist_b[w] = 0; if (!match_b[w] || DFS(match_b[w])) { match_b[w] = v; match_a[v] = w; return true; } } return false; } int matching() { int resp = 0; while (BFS()) { for (int i = 1; i <= A; i++) { if (!match_a[i] && DFS(i)) resp++; } } return resp; } const int MAXI = 112345; int primo[MAXI], qtd_primos; bool p_mrk[MAXI]; int N, M, a[CEM], par[2][CEM], s[CEM], e[CEM]; vector<pair<int, int> > fatores[CEM]; void crivo() { for (int i = 2; i < MAXI; i++) p_mrk[i] = true; primo[++qtd_primos] = 2; for (int i = 3; i < MAXI; i += 2) { if (p_mrk[i]) { primo[++qtd_primos] = i; long long aux = i; aux *= i; if (aux > (long long)MAXI) continue; for (int k = i * i; k < MAXI; k += i) p_mrk[k] = false; } } } void fatora(int &a, int idx, int x) { s[idx] = a; for (int i = 1; i <= qtd_primos; i++) { int cnt = 0; while (x % primo[i] == 0) { cnt++; x /= primo[i]; } if (cnt > 0) fatores[idx].push_back(make_pair(primo[i], cnt)); a += cnt; if (x == 1) break; } if (x > 1) { fatores[idx].push_back(make_pair(x, 1)); a++; } } void build_graph() { crivo(); for (int i = 1; i <= N; i++) { if (i % 2 == 1) fatora(A, i, a[i]); else fatora(B, i, a[i]); } for (int i = 1; i <= M; i++) { int a = par[0][i], b = par[1][i]; if (a % 2 == 0) swap(a, b); int pos_a = s[a], pos_b = s[b], t1 = fatores[a].size(), t2 = fatores[b].size(); int esq = 0, dir = 0; while (esq < t1 && dir < t2) { if (fatores[a][esq].first < fatores[b][dir].first) { pos_a += fatores[a][esq].second; esq++; } else if (fatores[a][esq].first > fatores[b][dir].first) { pos_b += fatores[b][dir].second; dir++; } else { for (int k = pos_a; k < pos_a + fatores[a][esq].second; k++) { for (int j = pos_b; j < pos_b + fatores[b][dir].second; j++) { grafo[k + 1].push_back(j + 1); } } pos_a += fatores[a][esq].second; pos_b += fatores[b][dir].second; esq++; dir++; } } } } int main() { cin >> N >> M; for (int i = 1; i <= N; i++) cin >> a[i]; for (int i = 1; i <= M; i++) cin >> par[0][i] >> par[1][i]; build_graph(); printf("%d\n", matching()); return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; template<class T> using V = vector<T>; template<class T> using VV = V<V<T>>; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() #define dump(x) cerr << #x << " = " << (x) << endl constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; } template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; } template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os<<"("<<p.first<<","<<p.second<<")"; return os; } template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { os<<"{"; rep(i, v.size()) { if (i) os<<","; os<<v[i]; } os<<"}"; return os; } const int maxn = 100010; const int INF = TEN(9); V<int> g[maxn]; int r; int dfs(int v, int p, int l) { V<int> vec; for (int to : g[v]) if (to != p) { int res = dfs(to, v, l); if (res == -1 || res > l) { return -1; } vec.pb(res); } sort(ALL(vec)); int sz = vec.size(); if (sz % 2 == 0) { { //all int nl = 0, nr = sz - 1; bool f = 1; rep(i, sz / 2) { if (vec[nl] + vec[nr] > l) { f = 0; } ++nl; --nr; } if (f) { return 1; } } vec.pop_back(); --sz; } //sz : odd auto valid = [&](int p) { int nl = 0, nr = sz - 1; bool f = 1; rep(i, sz / 2) { if (nl == p) ++nl; if (nr == p) --nr; if (vec[nl] + vec[nr] > l) { f = 0; } ++nl; --nr; } return f; }; int lo = 0, hi = sz - 1; if (valid(lo)) { hi = 0; } if (!valid(hi)) { return -1; } while (hi - lo > 1) { int m = (lo + hi) / 2; if (valid(m)) hi = m; else lo = m; } return vec[hi] + 1; } bool ok(int l) { int res = dfs(r, -1, l); return (res != -1); } int main() { int N; cin >> N; rep(i, N-1) { int a, b; cin >> a >> b; --a; --b; g[a].pb(b); g[b].pb(a); } rep(i, N) if (g[i].size() == 1) { r = i; break; } int A = 0; rep(i, N) if (g[i].size() % 2 == 1) ++A; int lo = 0, hi = N; while (hi - lo > 1) { int m = (lo + hi) / 2; if (ok(m)) { hi = m; } else { lo = m; } } printf("%d %d\n", A / 2, hi); return 0; }
0
#include <bits/stdc++.h> using namespace std; using dbl = long double; using Pi = pair<int, int>; const dbl eps = 1e-15; #define lt(a, b) ((a)-(b) < -eps) #define eq(a, b) (fabs((a)-(b)) < eps) int W, H; char mas[505][505]; Pi S, G; vector<Pi> Bs; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; bool in(int y, int x) { return 0<=y&&y<H&&0<=x&&x<W; } vector<vector<dbl> > bfs(const vector<Pi>& s) { //cout<<"!!!!!"<<endl; queue<Pi> que; vector<vector<dbl> > dist(H, vector<dbl>(W, -1)); for(Pi p : s) { que.emplace(p); dist[p.first][p.second] = 0; } while(!que.empty()) { int y, x; tie(y, x) = que.front(); que.pop(); //cout<<mas[y][x]<<" "<<y<<" "<<x<<endl; for(int i = 0; i < 4; ++i) { int ny = y+dy[i], nx = x+dx[i]; if(!in(ny, nx) || mas[ny][nx] == '#' || mas[ny][nx] == '*') continue; if(dist[ny][nx] == -1) { dist[ny][nx] = dist[y][x]+1; que.emplace(ny, nx); } } } return dist; } vector<vector<dbl> > db; vector<vector<dbl> > dg; bool check(dbl mb) { dbl sum = 0; int num = 0; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { if(mas[i][j] == '.') { if(dg[i][j] == -1 && db[i][j] == -1) continue;//assert(false); else if(dg[i][j] == -1) sum += db[i][j]+mb; else if(db[i][j] == -1) sum += dg[i][j]; else sum += min(dg[i][j], db[i][j]+mb); ++num; } } } assert(num > 0); sum /= num; return sum >= mb; } int main() { cin >> W >> H; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { cin >> mas[i][j]; if(mas[i][j] == 's') S = Pi(i, j), mas[i][j] = '.'; else if(mas[i][j] == 'g') G = Pi(i, j); else if(mas[i][j] == '*') Bs.emplace_back(i, j); } } db = bfs(Bs); dg = bfs({G}); dbl ans = dg[S.first][S.second]; //cout<<ans<<endl; /* bool flag = false; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { if(mas[i][j] == '.' && dg[i][j] == -1 && db[i][j] == -1) flag = true; } } */ if(ans == -1) ans = DBL_MAX/2; else if(db[S.first][S.second] == -1) { //else if(flag) { cout << fixed << setprecision(12) << ans << endl; return 0; } dbl lb = 0, ub = 1e20; for(int i = 0; i < 200; ++i) { dbl mb = (lb+ub)/2; if(check(mb)) lb = mb; else ub = mb; } ans = min(ans, db[S.first][S.second]+lb); cout << fixed << setprecision(12) << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; string x, y; int a, b, c, d, all, all1; int main() { cin >> x >> y; a = (x[0] - '0') * 10 + (x[1] - '0'); b = (x[3] - '0') * 10 + (x[4] - '0'); c = (y[0] - '0') * 10 + (y[1] - '0'); d = (y[3] - '0') * 10 + (y[4] - '0'); all = a * 60 + b; all1 = c * 60 + d; if (all < all1) all += 24 * 60 - all1; else all = all - all1; if (all / 60 > 0) { if (all / 60 < 10) cout << 0 << all / 60 << ":"; else cout << all / 60 << ":"; all = all % 60; } else cout << "00:"; if (all % 60 > 0) { if (all % 60 < 10) cout << 0 << all % 60; else cout << all % 60; } else cout << "00"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N=2e5; int n,cnt,hd[N],v[N],nxt[N],w[N]; long long d[N]; void dfs(int u,int fa){ for(int i=hd[u];i;i=nxt[i]){ if(v[i]!=fa){ d[v[i]]=d[u]+w[i]; dfs(v[i],u); } } } int main(){ cin>>n; for(int i=1,x,y,z;i<n;i++){ cin>>x>>y>>z; v[++cnt]=y; nxt[cnt]=hd[x]; w[cnt]=z; hd[x]=cnt; v[++cnt]=x; nxt[cnt]=hd[y]; w[cnt]=z; hd[y]=cnt; } dfs(1,0); for(int i=1;i<=n;i++){ int op=d[i]&1; cout<<op<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string in; int n; cin >> in >> n; string q[n]; for (int i = 0; i < n; i++) { cin >> q[i]; } long long unsigned now[10]; long long unsigned len[10]; for (int i = 0; i < 10; i++) { now[i] = i; len[i] = 10; } for (int i = n - 1; i >= 0; i--) { string& qu = q[i]; int target = qu[0] - '0'; if (qu.length() == 3) { now[target] = 0; len[target] = 0; } long long unsigned val = 0; long long unsigned po = 1; for (int j = qu.length() - 1; j > 2; j--) { int d = qu[j] - '0'; val += po * now[d]; val %= 1000000007; po *= len[d]; po %= 1000000007; } now[target] = val; len[target] = po; } long long unsigned ans = 0; long long unsigned passed = 1; for (int i = in.length() - 1; i >= 0; i--) { int d = in[i] - '0'; ans += passed * now[d]; passed *= len[d]; ans %= 1000000007; passed %= 1000000007; } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int a, a1, a2, a3, b1, b2, b3, n, d, k, b; int main() { cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> n; a = a1 + a2 + a3; b = b1 + b2 + b3; d = (a + 4) / 5; k = (b + 9) / 10; if (d + k <= n) cout << "YES"; else cout << "NO"; return 0; }
1
/* ??¨????????¨DP. Node???????????????????????? BidirectionalTreeDP<Node> treedp(G); ??¨????????°?????? get(v)??§v????????¨??????dp??????get(v,p)??§v????????¨??????p??????????????£?????????Node??????????????? pic.png?????§ ?????§normalize????????§G?????????????????£???????????¨?????¨??? */ #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";} template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;} namespace Normalize{ template<class E> void dfs(vector<vector<E>>& G,vector<int>& ord, int v,int p=-1){ ord.pb(v); int K = G[v].size(); rep(i,K){ if(G[v][i].to==p){ rotate(G[v].begin()+i,G[v].begin()+i+1,G[v].end()); K--; break; } } rep(i,K){ dfs(G,ord,G[v][i].to,v); } } template<class E> vector<int> normalize_and_gettopord(vector<vector<E>>& G, int r=0){ vector<int> ord; dfs(G,ord,r); return ord; } } /* Node + ???????????????????????????????????§??????(?¶????????????????????????????(?????????up????¶??????´????????????????????§??¨???)) */ template<class N> struct BidirectionalTreeDP{ vector<N> dp; //dp[v] <- u1,u2,... p????????£???v?????? vector<N> up; //up[v] <- v's brothers + pp v????????£???p?????? vector<N> rp; //dp[r] <- r????????¨????????????????????? vector<int> par; template<class E> BidirectionalTreeDP(vector<vector<E>>& G, int r=0){ int V = G.size(); dp.assign(V,N()); up.assign(V,N()); rp.assign(V,N()); par.assign(V,0); vector<int> ord = Normalize::normalize_and_gettopord<E>(G,r); rep(t,V){ int v = ord[t]; if(v==r) par[v]=-1; else par[v]=G[v].back().to; } for(int t=V-1;t>=0;t--){ //dfs int v = ord[t]; dp[v] = N(); int K = G[v].size() - (v!=r); rep(i,K){ const E& e = G[v][i]; int u = e.to; dp[v] = dp[v] + dp[u].append_edge(v,e); } dp[v].finalize(v); } rep(t,V){ //ufs int v = ord[t]; int K = G[v].size() - (v!=r); vector<N> ls(K+1),rs(K+1); rep(i,K){ ls[i+1] = ls[i] + dp[G[v][i].to].append_edge(v,G[v][i]); rs[K-1-i] = dp[G[v][K-1-i].to].append_edge(v,G[v][K-1-i]) + rs[K-i]; } rep(i,K){ const E& e = G[v][i]; int u = e.to; up[u] = ls[i] + rs[i+1]; if(v!=r) up[u] = up[u] + up[v].append_edge(v,G[v].back()); up[u].finalize(v); } rp[v] = ls[K]; if(v!=r) rp[v] = rp[v] + up[v].append_edge(v,G[v].back()); rp[v].finalize(v); } } N get(int v,int p=-1){ //p????????£???v?????? if(p==-1) return rp[v]; if(par[v]==p) return dp[v]; return up[p]; } }; struct Node{ //p????????£???v???????????¨?????¨????????´???(??±?????????????????????) int dia; array<int,2> rd; Node(){ dia=0; rd[0]=rd[1]=0; } /* ???????????¨????£? e=(p -> this)?????????????????????????????? */ template<class E> Node append_edge(int p,const E& e) const { Node n; n.rd[0] = rd[0] + e.dist; n.rd[1] = 0; n.dia = dia; return n; } Node operator+(const Node& r) const { Node n; vector<int> vc; rep(t,2) vc.pb(rd[t]),vc.pb(r.rd[t]); sort(all(vc),greater<int>()); rep(t,2) n.rd[t]=vc[t]; n.dia = max(dia,r.dia); return n; } void finalize(int r){ chmax(dia,rd[0]+rd[1]); } }; struct Edge{ int to; int dist; }; int main(){ int N; cin>>N; vector<vector<Edge>> G(N); rep(i,N-1){ int x,d; cin>>x>>d; int y = i+1; G[x].pb({y,d}); G[y].pb({x,d}); } BidirectionalTreeDP<Node> treedp(G); int ans = 0; rep(v,N){ for(const Edge& e:G[v]){ int u = e.to; chmax(ans,treedp.get(v,u).dia+e.dist); } } cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; long long int a[5003], n, m, k; long long int dp[5003][5003], sum[5003]; long long int func(long long int in, long long int baki) { long long int &num = dp[in][baki]; if (num != -1ll) return num; num = 0; if (in >= n) return 0; if (baki) { long long int ind = in + m - 1; if (ind < n) { long long int s = 0; if (in == 0) s = sum[ind]; else s = sum[ind] - sum[in - 1]; num = s + func(ind + 1, baki - 1); } } num = max(num, func(in + 1, baki)); return num; } int main() { ios::sync_with_stdio(0); cin.tie(0); memset(dp, -1ll, sizeof(dp)); cin >> n >> m >> k; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; } cout << func(0, k) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return (N | (1 << pos)); } int fx[] = {-1, 0, 1, 0}; int fy[] = {0, 1, 0, -1}; inline long long BigMod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return R; } int ara[104][104]; int mark[104]; int row[104]; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ara[i][j]; } } for (int j = 1; j <= m; j++) { vector<int> G[104]; for (int i = 1; i <= n; i++) { if (row[i]) continue; if (mark[ara[i][j]]) { row[i] = j; continue; } G[ara[i][j]].push_back(i); } for (int i = 1; i <= k; i++) { if (G[i].size() <= 1) continue; for (int x = 0; x < G[i].size(); x++) { row[G[i][x]] = j; } mark[i] = 1; } } for (int i = 1; i <= n; i++) cout << row[i] << "\n"; }
2
#include<iostream> using namespace std; int main(){ int N, M; cin >> N >> M; cout << N*(N-1)/2 + M*(M-1)/2; }
0
#include<bits/stdc++.h> using namespace std; #define ll long long map<int,ll>mp; int main() { int n; cin>>n; ll ans=0; for(int i=1,x;i<=n;i++) { cin>>x; ans=ans+mp[i-x]; mp[i+x]++; } cout<<ans<<"\n"; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, b, c; cin >> n >> a >> b >> c; long long ans = 0; if (a <= b - c) { cout << (n / a) << "\n"; ; return 0; } if (b <= n) { n -= b; ans += n / (b - c); n -= (n / (b - c)) * (b - c); n += c; ans++; } ans += n / a; cout << (ans) << "\n"; ; }
1
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; int x, s = 0; for (int i = 0; i < n; i++) { cin >> x; s += x; } int c = 0; for (int i = 1; i <= 5; i++) { if ((s + i) % (n + 1) != 1) c++; } cout << c << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 11; int n, a[N], ma; int main(){ cin>>n; for(int i = 1;i <= n; i++){ scanf("%d", &a[i]); ma = max(ma, a[i]); } int dl = 0; for(int i = 1;i <= n; i++){ if(ma - a[i] > 1){ puts("No"); return 0; } else if(ma - a[i])dl++; } if(dl == 0){ if(ma == n - 1 || ma * 2 <= n)puts("Yes"); else puts("No"); } else{ if(n - dl < 2 * (ma - dl) || ma <= dl) puts("No"); else puts("Yes"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long t = 0, n = 0; cin >> t; while (t--) { cin >> n; if (n == 1 || n == 2) cout << "1" << endl; else if (n == 3 || n == 4) cout << "2" << endl; else { if (n % 2 == 0) cout << n / 2 << endl; else cout << n / 2 + 1 << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, m, n, t, max = 0; long long a[50003]; long long total, ans = 0; scanf("%d", &n); total = 0; for (i = 1; i <= n; i++) { scanf("%I64d", &a[i]); total += a[i]; } total /= n; for (i = 1; i <= n - 1; i++) if (a[i] < total) { ans += total - a[i]; a[i + 1] -= (total - a[i]); } else if (a[i] > total) { ans += a[i] - total; a[i + 1] += a[i] - total; } printf("%I64d\n", ans); }
2
#include <bits/stdc++.h> using namespace std; void solve(); int main() { solve(); } int d[1000]; int s[1000]; set<pair<pair<int, int>, int>> t, b; set<pair<pair<int, int>, int>>* other(set<pair<pair<int, int>, int>>* x) { return (x == &t) ? &b : &t; } namespace std { template <> struct hash<set<pair<pair<int, int>, int>>::iterator> { size_t operator()(set<pair<pair<int, int>, int>>::iterator t) const { return t->first.first * 10000 + t->first.second * 100 + t->second; } }; } // namespace std void solve() { int l, r, n; cin >> l >> r >> n; for (int i = 0; i < n; ++i) { int v, ll, rr; char a; cin >> v >> a >> ll >> rr; pair<pair<int, int>, int> z = make_pair(make_pair(ll, rr), v); if (a == 'T') t.insert(z); else b.insert(z); } int ans = 0; for (int cnt = 1; cnt <= n; ++cnt) { unordered_set<set<pair<pair<int, int>, int>>::iterator> v; int pr = r; if (cnt % 2 == 1) pr = 100 - pr; int realFinish = pr - cnt * 100; int h = l - realFinish; set<pair<pair<int, int>, int>>* cur = &b; int sum = 0; bool ok = true; for (int i = 0; i < cnt; ++i) { int curH = l + i * 100; double s = curH * 100000 / (double)h; set<pair<pair<int, int>, int>>::iterator it = cur->upper_bound(make_pair(make_pair(s, 10000000), 10000000)); if (it == cur->begin()) { ok = false; break; } --it; if (it->first.second < s) { ok = false; break; } v.insert(it); sum += it->second; cur = other(cur); } if (ok && v.size() == cnt) { ans = max(ans, sum); } } for (int cnt = 1; cnt <= n; ++cnt) { unordered_set<set<pair<pair<int, int>, int>>::iterator> v; int pr = r; if (cnt % 2 == 1) pr = 100 - pr; int realFinish = pr + cnt * 100; int h = realFinish - l; set<pair<pair<int, int>, int>>* cur = &t; int sum = 0; bool ok = true; for (int i = 0; i < cnt; ++i) { int curH = 100 - l + i * 100; double s = curH * 100000 / (double)h; set<pair<pair<int, int>, int>>::iterator it = cur->upper_bound(make_pair(make_pair(s, 10000000), 10000000)); if (it == cur->begin()) { ok = false; break; } --it; if (it->first.second < s) { ok = false; break; } v.insert(it); sum += it->second; cur = other(cur); } if (ok && v.size() == cnt) { ans = max(ans, sum); } } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; int powermod(int base, int pw, int modulo) { int ans = 1; while (pw) { if (pw & 1) { ans = (1LL * base * ans) % modulo; } base = (1LL * base * base) % modulo; pw >>= 1; } return ans; } inline void debug(long double n) { cout << n << ' '; cout << "\n"; } inline void debug(string s, long double n) { cout << s << ": "; cout << n << ' '; cout << "\n"; } inline void debug(string s) { cout << s << " "; cout << "\n"; } void print_array(int* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(unsigned int* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(long long int* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(unsigned long long int* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(float* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(double* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } void print_array(long double* a, int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); int n; double avg = 0; int ans = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; avg += a[i]; } avg = avg / n; avg = 4.5 - avg; if (avg > 0) { sort(a, a + n); avg = avg * (double)n; int i = 0; while (avg > 1e-6) { avg = avg - (5 - a[i]); i++; ans++; } } cout << ans << "\n"; return 0; }
2
#include <bits/stdc++.h> int main() { int n, m = 0, t = 0, x, y = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); if (x <= y * 2) t++; else t = 1; m = m > t ? m : t; y = x; } printf("%d", m); }
2
#include <bits/stdc++.h> using namespace std; void fail() { puts("-1"), exit(0); } int O_A[200011], A_B[200011], A_O[200011], B_A[200011]; int tot, fi[200011]; struct edge { int nx, to; } e[200011 * 4]; void ps(int x, int y) { e[++tot] = (edge){fi[x], y}; fi[x] = tot; } int ft[200011], dep[200011]; void dfs_base(int x, int fa) { ft[x] = fa; for (int i = fi[x], y; (y = e[i].to); i = e[i].nx) if (y != fa) dep[y] = dep[x] + 1, dfs_base(y, x); return; } int C[200011], n; bool check() { for (int i = 1; i <= n; i++) C[i] = A_B[i]; int now = B_A[1]; while (ft[now]) swap(C[now], C[ft[now]]), now = ft[now]; for (int i = 1; i <= n; i++) { if (C[i] != i) return 0; } return 1; } vector<int> cir[2]; int Cnt = 0; void Chain(int x) { if (Cnt > 1 || (Cnt == 1 && ft[x] != ft[cir[0][0]])) fail(); int t; do { t = 0; for (int i = fi[x], y; (y = e[i].to); i = e[i].nx) if (C[y] != y && y != ft[x]) { if (!t) t = y; else fail(); } cir[Cnt].push_back(x); x = t; } while (x); ++Cnt; } void dfs_sd(int x, int fa) { for (int i = fi[x], y; (y = e[i].to); i = e[i].nx) if (y != fa) { if (C[y] == y) dfs_sd(y, x); else Chain(y); } } int len, Cir_P[200011], P[200011]; int getdis(int x, int y) { int dd = 0; while (x != y) { if (dep[x] < dep[y]) swap(x, y); x = ft[x]; ++dd; } return dd; } void work() { for (int i = 1; i <= n; i++) Cir_P[i] = -1; for (int i = 0; i < (int)cir[0].size(); i++) { P[len] = C[cir[0][i]]; Cir_P[cir[0][i]] = len; ++len; } int ru = cir[0].back(), rv = ft[cir[0].front()]; int tp = rv; if (cir[1].size()) { rv = cir[1].back(); for (int i = cir[1].size() - 1; i >= 0; i--) { P[len] = C[cir[1][i]]; Cir_P[cir[1][i]] = len; ++len; } } ru = A_O[ru]; rv = A_O[rv]; if (ru > rv) swap(ru, rv); long long dis = Cir_P[P[0]]; for (int i = 1; i < len; i++) { if ((Cir_P[P[i]] - i + len) % len != dis) fail(); } for (int i = 1; i <= n; i++) { if (C[i] != i && Cir_P[i] == -1) fail(); } int tmp = B_A[1]; while (Cir_P[tmp] == -1 && tmp != 1) tmp = ft[tmp]; printf("%d %d ", ru, rv); if (tmp == 1) { dis = min(dis, len - dis) * (len + 1); printf("%lld", dis + getdis(B_A[1], tp) + dep[tp]); return; } if (Cir_P[tmp] >= (int)cir[0].size()) { dis = min((dis - 1) * (len + 1) + (Cir_P[tmp] + 1), (len - dis) * (len + 1) + (len - Cir_P[tmp])); } else { dis = min(dis * (len + 1) + (Cir_P[tmp] + 1), (len - dis - 1) * (len + 1) + (len - Cir_P[tmp])); } printf("%lld", dis + getdis(B_A[1], tmp) + dep[tp]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &O_A[i]), ++O_A[i], A_O[O_A[i]] = i; for (int i = 1; i <= n; i++) scanf("%d", &A_B[O_A[i]]), ++A_B[O_A[i]]; for (int i = 1, x, y; i < n; i++) { scanf("%d%d", &x, &y); x = O_A[x]; y = O_A[y]; ps(x, y); ps(y, x); } for (int i = 1; i <= n; i++) B_A[A_B[i]] = i; dfs_base(1, 0); if (check()) { printf("%d %d", 0, dep[B_A[1]]); return 0; } dfs_sd(1, 0); work(); }
6
#include <bits/stdc++.h> using namespace std; bool cmp(pair<string, int> p1, pair<string, int> p2) { if (p1.second > p2.second) return true; return false; } int main() { int n = 0, m = 0; cin >> n >> m; vector<int> a(n); for (int &i : a) cin >> i; sort(a.begin(), a.end()); vector<pair<string, int>> v; for (int i = 0; i < m; ++i) { string second; cin >> second; bool fl = false; int j = 0; for (j = 0; j < (int)v.size(); j++) { if (v[j].first == second) { fl = true; break; } } if (fl) { v[j].second += 1; } else v.push_back({second, 1}); } int l = 0, h = 0, j = 0; sort(v.begin(), v.end(), cmp); for (auto i : v) { l += a[j++] * i.second; } j = n - 1; for (auto i : v) { h += a[j--] * i.second; } cout << l << " " << h; }
3
#include <iostream> using namespace std; int main() { int n; while(cin >> n, n) { int id, dis; id = -1, dis = -1; for(int i = 0; i < n; i++) { int p; int d1, d2; cin >> p >> d1 >> d2; if(dis < d1+d2) { id = p; dis = d1+d2; } } cout << id << " " << dis << endl; } }
0
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& os, set<T>& s) { for (auto it = s.begin(); it != s.end(); ++it) { cout << *it << ","; } cout << endl; return os; } int main() { string s; cin >> s; for (int i = 0; i < s.size(); ++i) { set<char> _s; if (i > 0) { _s.insert(s[i - 1]); } _s.insert(s[i]); if (i < s.size() - 1) { _s.insert(s[i + 1]); } if (_s.find('A') != _s.end() && _s.find('B') != _s.end() && _s.find('C') != _s.end()) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y; cin >> n >> x >> y; x--; y--; if ((x == n / 2 && y == n / 2) || (x + 1 == n / 2 && y == n / 2) || (x == n / 2 && y + 1 == n / 2) || (x + 1 == n / 2 && y + 1 == n / 2)) cout << "NO"; else cout << "YES"; return 0; }
2
#include <bits/stdc++.h> const long long oo = 2 * 1000 * 1000 * 1000; const int _cnt = 1000 * 1000; const int _p = 1000 * 1000 * 1000 + 7; int o(int x) { return x % _p; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } using namespace std; void file_put() { freopen("filename.in", "r", stdin); freopen("filename.out", "w", stdout); } int n, m, l[205], a[205]; set<int> S; int get_num() { for (int i = 1; i <= n; i++) if (S.find(i) == S.end()) return i; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &l[i]); for (int i = 1; i <= m - 1; i++) if (!a[l[i]]) a[l[i]] = (l[i + 1] - l[i] + n - 1) % n + 1; else if (a[l[i]] != (l[i + 1] - l[i] + n - 1) % n + 1) return 0 * printf("-1\n"); for (int i = 1; i <= n; i++) if (a[i]) S.insert(a[i]); for (int i = 1; i <= n; i++) { if (a[i]) continue; a[i] = get_num(); S.insert(a[i]); } for (int i = 1; i <= n; i++) S.insert(a[i]); if (S.size() < n) printf("-1\n"); else for (int i = 1; i <= n; i++) printf("%d ", a[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; double dp[304][304][304]; double expected(int three,int two,int one){ int zero = n - three-two-one; double& ret = dp[three][two][one]; if(ret != 0.0) return ret; if(zero == n) return 0.0; if(three>0) ret += (expected(three-1,two+1,one)+1)*three; if(two>0) ret += (expected(three,two-1,one+1)+1)*two; if(one>0) ret += (expected(three,two,one-1)+1)*one; ret = ret/(one+two+three)+1.0*(n-one-two-three)/(one+two+three); return ret; } int main() { cin>>n; int a[3] = {0,}, k; for(int i = 0 ; i <n;i++){ cin>>k; a[k-1]++; } memset(dp,0,sizeof(dp)); cout<<fixed; cout.precision(10); cout<<expected(a[2],a[1],a[0]); return 0; }
0
#include <bits/stdc++.h> const long long N = 1e5 + 5; const long long K = 105; using namespace std; vector<long long> adj[N]; vector<long long> val[K]; vector<long long> dist[N]; signed main() { std ::ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m, k, second; cin >> n >> m >> k >> second; for (long long i = 1; i <= n; i++) { long long x; cin >> x; val[x].push_back(i); } for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (long long i = 1; i <= k; i++) { queue<long long> q; long long prev_level = 0; long long cur_level = 0; vector<bool> vis(n + 1); for (long long j = 0; j < val[i].size(); j++) { prev_level++; q.push(val[i][j]); vis[val[i][j]] = true; } long long cur_min = 0; while (!q.empty()) { long long p = q.front(); q.pop(); prev_level--; dist[p].push_back(cur_min); for (long long e : adj[p]) { if (vis[e]) continue; cur_level++; vis[e] = true; q.push(e); } if (prev_level == 0) { prev_level = cur_level; cur_level = 0; cur_min++; } } } for (long long i = 1; i <= n; i++) sort((dist[i]).begin(), (dist[i]).end()); for (long long i = 1; i <= n; i++) { long long ans = 0; for (long long j = 0; j < second; j++) ans += dist[i][j]; cout << ans << ' '; } }
1
#include <stdio.h> #include <algorithm> #include <utility> #include <map> #include <deque> #include <vector> using namespace std; typedef pair<int,int> pi; #define F first #define S second #define PB push_back const int N=2e5+10; struct ant{ int x; int y; bool face; void in(){ char c[5]; scanf("%d%d",&x,&y); scanf("%s",c); face=c[0]=='E'; return ; } int pos(){return x+y;} bool black(){return (x+y)%2;} }; int main(){ vector<pi> ans,temp; vector<int> emp,l[2],r[2]; map<int,vector<int>>m; deque<int>dq[2]; int n,w,h,p[N]; ant a[N]; scanf("%d%d%d",&w,&h,&n); for(int i=1;i<=n;i++){ a[i].in(); if(m.find(a[i].pos())==m.end())m.insert({a[i].pos(),emp}); m[a[i].pos()].PB(i); } for(pair<int,vector<int>> i:m){ l[0].clear(); l[1].clear(); r[0].clear(); r[1].clear(); temp.clear(); for(int j:i.S)temp.PB({a[j].x,j}); sort(temp.begin(),temp.end()); reverse(temp.begin(),temp.end()); if(a[temp.front().S].black()){ for(pi j:temp){ if(a[j.S].face){ if(dq[(j.F+1)%2].empty())p[j.S]=j.S; else{ p[j.S]=dq[(j.F+1)%2].front(); dq[(j.F+1)%2].pop_front(); dq[(j.F+1)%2].PB(j.S); } } else { l[j.F%2].PB(j.S); dq[j.F%2].PB(j.S); } } } else{ for(pi j:temp){ if(a[j.S].face){ if(dq[j.F%2].empty())p[j.S]=j.S; else{ p[j.S]=dq[j.F%2].front(); dq[j.F%2].pop_front(); dq[j.F%2].PB(j.S); } } else { l[j.F%2].PB(j.S); dq[j.F%2].PB(j.S); } } } while(!dq[0].empty()){ r[0].PB(dq[0].front()); dq[0].pop_front(); } while(!dq[1].empty()){ r[1].PB(dq[1].front()); dq[1].pop_front(); } for(int j=0;j<r[0].size();j++)p[l[0][j]]=r[0][j]; for(int j=0;j<r[1].size();j++)p[l[1][j]]=r[1][j]; } for(int i=1;i<=n;i++)if(a[i].face)ans.PB({w-a[i].x,p[i]}); else ans.PB({h-a[i].y,p[i]}); sort(ans.begin(),ans.end()); for(pi i:ans)printf("%d\n",i.S); }
0
#include <bits/stdc++.h> using namespace std; int n, k; int dx[222]; int dy[222]; string s; int main() { ios_base::sync_with_stdio(false); cin >> n; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'U') { dx[i + 1] = dx[i] + 1; dy[i + 1] = dy[i]; } if (s[i] == 'D') { dx[i + 1] = dx[i] - 1; dy[i + 1] = dy[i]; } if (s[i] == 'L') { dy[i + 1] = dy[i] + 1; dx[i + 1] = dx[i]; } if (s[i] == 'R') { dy[i + 1] = dy[i] - 1; dx[i + 1] = dx[i]; } } int ans = 0; for (int i = 0; i < s.size(); i++) { for (int j = i + 1; j <= s.size(); j++) { if (dx[i] == dx[j] && dy[i] == dy[j]) ans++; } } cout << ans << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; }; bool operator<(point A, point B) { return make_pair(A.x, A.y) < make_pair(B.x, B.y); } bool operator==(point A, point B) { return make_pair(A.x, A.y) == make_pair(B.x, B.y); } struct Vector { long long x, y; Vector(point a, point b) { x = b.x - a.x; y = b.y - a.y; } }; Vector neg(Vector x) { x.x = -x.x; x.y = -x.y; return x; } point add(point p, Vector a) { p.x += a.x; p.y += a.y; return p; } long long operator^(Vector a, Vector b) { return abs(a.x * b.y - a.y * b.x); } vector<point> p; long long res(int x, int y, int z) { return Vector(p[x], p[y]) ^ Vector(p[x], p[z]); } int n; long long s; int main() { scanf("%d %I64d", &n, &s); p = vector<point>(n); for (int i = 0; i < n; i++) { scanf("%I64d %I64d", &p[i].x, &p[i].y); } sort(p.begin(), p.end()); p.resize(unique(p.begin(), p.end()) - p.begin()); if ((int)p.size() < 3) { while ((int)p.size() < 3) { p.push_back(p[0]); } for (int i = 0; i < 3; i++) { printf("%I64d %I64d\n", p[i].x, p[i].y); } return 0; } random_shuffle(p.begin(), p.end()); int a = 0, b = 1, c = 2; while (1) { bool better = false; for (int i = 0; i < n; i++) { if (res(a, b, c) < res(a, b, i)) { better = true; c = i; } if (res(a, b, c) < res(a, i, c)) { better = true; b = i; } if (res(a, b, c) < res(i, b, c)) { better = true; a = i; } } if (!better) { break; } } point r1, r2, r3; r1 = add(p[a], Vector(p[b], p[c])); r2 = add(p[a], neg(Vector(p[b], p[c]))); r3 = add(p[b], Vector(p[a], p[c])); printf("%I64d %I64d\n", r1.x, r1.y); printf("%I64d %I64d\n", r2.x, r2.y); printf("%I64d %I64d\n", r3.x, r3.y); return 0; }
5
#include <bits/stdc++.h> using namespace std; int h[111]; int main() { string s; cin >> s; int j = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { h[j] = 1; j++; } else if (s[i] == '2') { h[j] = 2; j++; } else if (s[i] == '3') { h[j] = 3; j++; } else { continue; } } sort(h, h + j); int p = 0; for (int i = 0; i < j; i++) { if (p) { cout << "+"; } p++; cout << h[i]; } }
1
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (n%2 == 0 && s.substr(0, n/2) == s.substr(n/2)) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; map<string, int> mp; int main() { int n, k; cin >> n >> k; string st[n + 1]; for (int i = 0; i < n; i++) { cin >> st[i]; mp[st[i]]++; } string st2 = "EST"; int ct = 0; set<string> se; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { string st1, s; vector<string> vs; for (int a = 0; a < k; a++) { if (st[i][a] == st[j][a]) { st1.push_back(st[i][a]); } else { for (int b = 0; b < 3; b++) { if (st[i][a] != st2[b] and st[j][a] != st2[b]) { st1.push_back(st2[b]); break; } } } } if (mp[st1] > 0) { vs.push_back(st[i]); vs.push_back(st[j]); vs.push_back(st1); sort(vs.begin(), vs.end()); s = vs[0] + vs[1] + vs[2]; int l = se.size(); se.insert(s); if (se.size() > l) { ct++; } } } } cout << ct << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int ar[1000006], stk[1000006], t; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &ar[i]); int m; scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); ar[x - 1] *= -1; } for (int i = n - 1; i >= 0; i--) { if (ar[i] > 0) { if (t == 0 || abs(ar[stk[t - 1]]) != abs(ar[i])) stk[t++] = i, ar[i] *= -1; else t--; } else stk[t++] = i; } if (t) { puts("NO"); return 0; } puts("YES"); for (int i = 0; i < n; i++) printf("%d%c", ar[i], i == n - 1 ? '\n' : ' '); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y) { long long res = 1LL; x = x % 1000000007; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res % 1000000007; } long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; } long long a[55][55]; long long dx[] = {1, -1, 0, 0}; long long n, m; long long dy[] = {0, 0, 1, -1}; long long vis[55][55], con = 0, sz = 0; long long ok(long long x, long long y) { return x >= 0 && y >= 0 && x < n && y < m && vis[x][y] == 0 && a[x][y] == 1; } void dfs(long long x, long long y) { vis[x][y] = 1; long long i; con++; for (i = 0; i < 4; i++) { long long nx, ny; nx = x + dx[i]; ny = y + dy[i]; if (ok(nx, ny)) dfs(nx, ny); } } void go() { long long i, j; con = 0; memset(vis, 0, sizeof(vis)); for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i][j] == 1) { dfs(i, j); return; } } void solve() { string s; cin >> n >> m; long long i, j; for (i = 0; i < n; i++) { string s; cin >> s; for (j = 0; j < m; j++) { if (s[j] == '#') { a[i][j] = 1; sz++; } } } if (sz < 3) { cout << "-1\n"; return; } for (i = 0; i < n; i++) for (j = 0; j < m; j++) { if (a[i][j]) { a[i][j] = 0; go(); a[i][j] = 1; if (con == sz - 1) continue; else { cout << "1\n"; return; } } } cout << "2\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } }
3
#include <iostream> using namespace std; long long gcd(long long a, long long b){ if(a<b) swap(a,b); if(b==0) return a; return gcd(a%b,b); } int main(){ long long a,b; cin >> a >> b; if(b%a==0){ cout << -1 << endl; }else{ long long i; for(i=0;i<=a - 1;i++){ long long j = i + b; if(j/a + j%a<1 + i){ cout << j << endl; return 0; } } cout << -1 << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int maxm = 1e5 + 233; const int CDD = 360; int n, m, q, g[11][maxm], p[maxm << 2], id[maxm << 2]; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } struct Node { int val, L[11], R[11], siz; } * ch[maxm << 2], *emp; inline int unite(int x, int y) { int a = find(x), b = find(y); if (a == b) return 0; p[a] = b; return 1; } Node *perx, *pery; inline Node* merge(Node* x, Node* y, int pos) { if (x->val == -1) return y; if (y->val == -1) return x; Node* z = new Node; perx->val = x->val; perx->siz = x->siz; for (int i = 1; i <= n; i++) perx->L[i] = x->L[i], perx->R[i] = x->R[i]; pery->val = y->val; pery->siz = y->siz; for (int i = 1; i <= n; i++) pery->L[i] = y->L[i], pery->R[i] = y->R[i]; z->val = x->val + y->val; for (int i = 1; i <= n; i++) y->L[i] += x->siz, y->R[i] += x->siz; for (int i = 1; i <= n; i++) { int buf = y->L[i]; p[buf] = buf; buf = y->R[i]; p[buf] = buf; buf = x->L[i]; p[buf] = buf; buf = x->R[i]; p[buf] = buf; } for (int i = 1; i <= n; i++) if (g[i][pos] == g[i][pos + 1]) { z->val -= unite(x->R[i], y->L[i]); } for (int i = 1; i <= n; i++) { z->L[i] = find(x->L[i]); z->R[i] = find(y->R[i]); id[z->L[i]] = -1; id[z->R[i]] = -1; } int cnt = 0; for (int i = 1; i <= n; i++) { if (id[z->L[i]] == -1) id[z->L[i]] = ++cnt; if (id[z->R[i]] == -1) id[z->R[i]] = ++cnt; z->L[i] = id[z->L[i]]; z->R[i] = id[z->R[i]]; } z->siz = cnt; x->val = perx->val; x->siz = perx->siz; for (int i = 1; i <= n; i++) x->L[i] = perx->L[i], x->R[i] = perx->R[i]; y->val = pery->val; y->siz = pery->siz; for (int i = 1; i <= n; i++) y->L[i] = pery->L[i], y->R[i] = pery->R[i]; return z; } void build(int l, int r, int cur) { if (l == r) { ch[cur] = new Node; int& x = ch[cur]->val; x = 0; for (int i = 1; i <= n; i++) { if (i == 1 || g[i][l] != g[i - 1][l]) x++; ch[cur]->L[i] = ch[cur]->R[i] = x; } ch[cur]->siz = x; return; } int mid = (l + r) >> 1, lc = cur << 1, rc = lc | 1; build(l, mid, lc); build(mid + 1, r, rc); ch[cur] = merge(ch[lc], ch[rc], mid); return; } Node* query(int l, int r, int cur, int x, int y) { if (r < x || l > y) return emp; if (x <= l && r <= y) { return ch[cur]; } int mid = (l + r) >> 1, lc = cur << 1, rc = lc | 1; return merge(query(l, mid, lc, x, y), query(mid + 1, r, rc, x, y), mid); } int main() { perx = new Node; pery = new Node; emp = new Node; emp->val = -1; cin >> n >> m >> q; int siz = (m + CDD - 1) / CDD; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &g[i][j]); build(1, m, 1); for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", query(1, m, 1, l, r)->val); } return 0; }
5
#include<iostream> using namespace std; typedef long long ll; int main(){ int n; cin >> n; ll a[n], b[n]; for(int i = 0; i < n; i++) cin >> a[i] >> b[i]; ll ans = 0; for(int i = n-1; i >= 0; i--){ a[i] += ans; ans += (a[i]+b[i]-1)/b[i]*b[i] - a[i]; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> int main() { int n, m, x; scanf("%d%d%d", &n, &m, &x); x--; int tmp; if (n < m) { tmp = m; m = n; n = tmp; } n -= 2 * x; m -= 2 * x; int total = 0; if (m > 0) if (m > 1) total = n + m - 2; else total = 1 + (n - 1) / 2; printf("%d\n", total); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int main() { long long x; cin >> x; if (x <= 4) { cout << x * (x - 1) / 2 << endl; return 0; } long long y = x * 2 - 1; long long z = y; int num = 0; long long ans = 0; while (y) { y /= 10; num++; } if (z == 9 || z == 99 || z == 999 || z == 9999 || z == 99999 || z == 999999 || z == 9999999 || z == 99999999 || z == 999999999) { cout << "1" << endl; return 0; } if (z < 9) { puts("0"); return 0; } num--; long long u = 0; int uu = num; long long ab = 1; while (uu--) { u = u * 10 + 9; ab *= 10; } for (long long i = u; i <= z; i += ab) { if (i > x) ans += (x - (i - x) + 1) / 2; else ans += i / 2; } cout << ans << endl; return 0; }
4
#include<bits/stdc++.h> using namespace std; string S;int n; int main(){ cin>>n>>S;int maxn=0,t=0; for(int i=0;i<S.size();i++){ if(S[i]=='I')t++;else t--; maxn=max(maxn,t); } cout<<maxn<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, n, m, s, d, step = 0, pos = 0; bool flg = true; cin >> n >> m >> s >> d; vector<int> p(n); vector<int> plan(3 * n); for (i = 0; i < n; i++) cin >> p[i]; for (i = 0; i < 2 * n; i++) plan[i] = 0; sort(p.begin(), p.end()); i = 0; while (i < n) { plan[step] = p[i] - 1 - pos; if (plan[step] < s) { cout << "IMPOSSIBLE" << endl; return 0; } pos += plan[step]; step++; i++; while (i < n && p[i] - p[i - 1] < s + 2) i++; plan[step] = p[i - 1] + 1 - pos; if (plan[step] > d) { cout << "IMPOSSIBLE" << endl; return 0; } pos += plan[step]; step++; } if (pos < m) plan[step] = m - pos; i = 0; while (plan[i] > 0) { if (i % 2 == 1 && plan[i] > d) flg = false; i++; } if (flg) { i = 0; while (i < 3 * n && plan[i] > 0) { if (i % 2 == 0) cout << "RUN " << plan[i] << endl; else cout << "JUMP " << plan[i] << endl; i++; } } else { cout << "IMPOSSIBLE" << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 5; long long p[maxn]; int n; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } bool cmp(long long a, long long b) { return a > b; } long long x, y, a, b, k, temp; bool check(int n) { long long ans = 0; int num1 = n / temp; int num2 = n / a - num1; int num3 = n / b - num1; int xx = x, yy = y; if (yy > xx) { swap(num2, num3); swap(xx, yy); } for (int i = 0; i < n; i++) { if (num1) { ans = ans + p[i] * (xx + yy); num1--; continue; } if (num2) { ans += p[i] * xx; num2--; continue; } if (num3) { ans += p[i] * yy; num3--; continue; } } if (ans >= k) { return true; } return false; } int main() { int q; cin >> q; while (q--) { memset(p, 0, sizeof p); cin >> n; for (int i = 0; i < n; i++) { cin >> p[i]; p[i] /= 100ll; } cin >> x >> a; cin >> y >> b; cin >> k; temp = a * b / gcd(a, b); sort(p, p + n, cmp); int l = 0, r = n; int ans = -1; while (l <= r) { int mid = l + r >> 1; if (check(mid)) { ans = mid, r = mid - 1; } else { l = mid + 1; } } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long i, j, n, x, y, k, b; long long a[12345678]; int main() { cin >> n >> x >> y; i = 1; j = 1; while (i <= x || j <= y) { if (i * y < j * x) { ++k; a[k] = 1; ++i; } else if (i * y > j * x) { ++k; a[k] = 2; ++j; } else { ++k; a[k] = 3; ++k; a[k] = 3; ++i; ++j; } } for (i = 0; i < n; ++i) { cin >> b; b = b % (x + y); if (a[b] == 1) cout << "Vanya" << endl; else if (a[b] == 2) cout << "Vova" << endl; else cout << "Both" << endl; } }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { int t; cin >> t; int c = 0; if (t) { if (i - k > t) { if (i - 2 * k > t) c += k; else c += i - t - k - 1; c += min(n, i + k) - i; c++; } else { c += max(min(n, i + k) - (t + k), 0); } } else { c += min(i - 1, k) + min(n, i + k) - i + 1; } cout << (a[i] = c + a[t]) << ' '; } }
2
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using P = pair<int,int>; int main(){ int n; cin >> n; P a[n]; int ans = 0; rep(i,n) cin >> a[i].first >> a[i].second; sort(a,a+n); vector<int> l(n), r(n); l[0] = a[0].second, r[n-1] = a[n-1].second; for(int i = 1; i < n; i++) { l[i] = min(l[i-1],a[i].second); r[n-i-1] = min(r[n-i],a[n-i-1].second); } rep(i,n) { int d = a[i].second - a[i].first + 1; if(i == 0) ans = max(ans,d+r[1]-a[n-1].first+1); else if(i == n-1) ans = max(ans,d+l[n-2]-a[n-2].first+1); else ans = max(ans,d+max(0,min(r[i+1],l[i-1])-a[n-1].first+1)); } rep(i,n-1) ans = max(ans,max(l[i]-a[i].first+1,0)+max(r[i+1]-a[n-1].first+1,0)); cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int pw(long long int a, long long int b, long long int mod) { if (!b) return 1; if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod; return pw(a * a % mod, b / 2, mod) % mod; } const long long int MAXN = 1e5 + 10; const long long int INF = 8e18; const long long int MOD = 1e9 + 7; long long int n, q, seg[MAXN << 2][3], lazy[MAXN << 2][2]; void build(int id, int l, int r) { if (r - l == 1) { seg[id][1] = seg[id][2] = r; return; } int mid = (l + r) >> 1; build(2 * id, l, mid); build(2 * id + 1, mid, r); seg[id][1] = min(seg[2 * id][1], seg[2 * id + 1][1]); seg[id][2] = max(seg[2 * id][2], seg[2 * id + 1][2]); } void shift(int id, int l, int r) { if (lazy[id][0] == 0) return; seg[id][0] += lazy[id][1] * (r - l); seg[id][1] = seg[id][2] = lazy[id][0]; if (r - l > 1) { lazy[2 * id][1] += lazy[id][1]; lazy[2 * id + 1][1] += lazy[id][1]; lazy[2 * id][0] = lazy[2 * id + 1][0] = lazy[id][0]; } lazy[id][0] = lazy[id][1] = 0; } void update(int id, int l, int r, int ql, int qr, long long int x) { shift(id, l, r); if (qr <= l || r <= ql) return; if (ql <= l && r <= qr && seg[id][1] == seg[id][2]) { lazy[id][0] = x; lazy[id][1] = abs(x - seg[id][1]); return shift(id, l, r); } int mid = (l + r) >> 1; update(2 * id, l, mid, ql, qr, x); update(2 * id + 1, mid, r, ql, qr, x); seg[id][0] = seg[2 * id][0] + seg[2 * id + 1][0]; seg[id][1] = min(seg[2 * id][1], seg[2 * id + 1][1]); seg[id][2] = max(seg[2 * id][2], seg[2 * id + 1][2]); } long long int get(int id, int l, int r, int ql, int qr) { shift(id, l, r); if (qr <= l || r <= ql) return 0; if (ql <= l && r <= qr) return seg[id][0]; int mid = (l + r) >> 1; return get(2 * id, l, mid, ql, qr) + get(2 * id + 1, mid, r, ql, qr); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> q; build(1, 0, n); while (q--) { int type, l, r, x; cin >> type >> l >> r; if (type == 1) { cin >> x; update(1, 0, n, l - 1, r, x); } else { cout << get(1, 0, n, l - 1, r) << '\n'; } } return 0; }
3
#include <bits/stdc++.h> #include <algorithm> #include <cstdio> #include <cstdlib> #include <exception> #include <map> #include <memory> #include <queue> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; #define mp(x, y) make_pair(x, y) const int N = 1e5 + 5, bs = 320, mod = 1e9 + 7; int n, q, b[N], c[N]; int t[N << 2], lz[N << 2], bit[N]; vector<int> v[550]; bool needLoad[550]; void add(int x, int v) { while (x <= n) bit[x] += v, x += x & -x; } int query(int x) { int ans = 0, sum = 0; for (int i = 1 << 16; i; i >>= 1) if ((ans | i) <= n && sum + bit[ans | i] < x) ans |= i, sum += bit[ans]; return ans + 1; } void load(int x) { int l = max(1, x * bs), r = min(n, (x + 1) * bs - 1); v[x].clear(); for (int i = l; i <= r; i++) { v[x].push_back(query(b[i])); add(v[x].back(), 1); } for (int i = v[x].size() - 1; i >= 0; i--) add(v[x][i], -1); sort(v[x].begin(), v[x].end()); needLoad[x] = false; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &b[i]), b[i] = i - b[i]; for (int i = 1; i <= n; i++) add(i, 1); for (int i = 0; i <= n / bs; i++) needLoad[i] = true; scanf("%d", &q); while (q--) { int op, x, y; scanf("%d%d", &op, &x); if (op == 1) { scanf("%d", &y); b[x] = x - y; needLoad[x / bs] = true; } else { int rkx = b[x]; int las = min(n, (x / bs + 1) * bs - 1); for (int i = x + 1; i <= las; i++) if (b[i] <= rkx) rkx++; if (las + 1 <= n) { las = (las + 1) / bs; for (int i = las; i <= n / bs; i++) { if (needLoad[i]) load(i); int p = upper_bound(v[i].begin(), v[i].end(), rkx) - v[i].begin(); rkx += p; } } printf("%d\n", rkx); } } }
4
#include <bits/stdc++.h> using namespace std; const int N = 60; const int L = 5e5 + 10; const int INF = 1e9 + 10; int n, d; int c[N]; bool f[L]; int dp[L]; int mx; int main() { scanf("%d%d", &n, &d); for (int i = 0; i < n; ++i) { scanf("%d", c + i); } sort(c, c + n); f[0] = 1; for (int i = 0; i < n; ++i) { for (int j = L - 1; j >= 0; --j) { if (f[j]) f[j + c[i]] = 1; } } fill(dp, dp + L, INF); dp[0] = 0; int j = 0; for (int i = 0; i < L; ++i) { if (f[i]) { if (dp[i] >= INF) { break; } for (; j - i <= d && j < L; ++j) { if (f[j]) { mx = j; dp[j] = min(dp[j], dp[i] + 1); } } } } printf("%d %d\n", mx, dp[mx]); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const long long sz = 1e2 + 1; long long A[sz + 1]; void solve() { long long n, k; cin >> n >> k; long long flag = 0; long long cur; bool allSame = true; set<long long> s; for (long long i = 0; i < n; i++) { cin >> A[i]; s.insert(A[i]); if (flag == 0) { flag++; cur = A[i]; } if (A[i] != cur) allSame = false; } if (n == 1 || allSame) { cout << "1" << "\n"; } else { if (k == 1) { cout << "-1" << "\n"; } else { long long temp = s.size(); cout << 1 + ceil((double)(temp - k) / (k - 1)) << "\n"; } } return; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } return 0; }
2