func_code_string stringlengths 59 71.4k |
|---|
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000 + 5; const int N = 2e5 + 5; const long long oo = 1e18 + 5; const long long mod = 998244353; int a[N], b[N]; int mul(int a, int b) { return 1ll * a * b % mod; } void solve() { int n, k; cin >> n >> k; vector<int> pos(n + 2); vector<int> lv(n + 2); for (int i = 1; i <= n; ++i) { int x; cin >> x; a[i] = x; pos[x] = i; lv[i] = 0; } for (int i = 1; i <= k; ++i) { int x; cin >> x; b[i] = x; lv[pos[x]] = i; } lv[0] = lv[n + 1] = INF; int ans = 1; for (int i = 1; i <= n; ++i) { if (lv[i]) { if (lv[i] > lv[i - 1] && lv[i] > lv[i + 1]) ans = mul(ans, 2); else { if (lv[i] <= lv[i - 1] && lv[i] <= lv[i + 1]) { cout << 0 << n ; return; } } } } cout << ans << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { vector<string> s(9); for (long long int i = 0; i < 9; i++) { cin >> s[i]; } s[0][0] = s[0][1]; s[1][3] = s[1][4]; s[2][6] = s[2][7]; s[3][1] = s[3][2]; s[4][4] = s[4][5]; s[5][7] = s[5][8]; s[6][2] = s[6][1]; s[7][5] = s[7][4]; s[8][8] = s[8][7]; for (long long int i = 0; i < 9; i++) { cout << s[i] << endl; } } } |
#include <bits/stdc++.h> using namespace std; struct Node { int l, r; pair<int, int> v; int mid() { return (l + r) >> 1; } }; struct HNode { int m, p; long long x, y, r, d; bool operator<(const HNode &cur) const { return d < cur.d; } }; Node tree[4 * 250005]; HNode hook[250005]; int n; vector<int> has; void push_up(int pos) { tree[pos].v = min(tree[pos << 1].v, tree[pos << 1 | 1].v); } void build(int pos, int l, int r) { tree[pos].l = l; tree[pos].r = r; if (l == r) { tree[pos].v = make_pair(hook[l].m, l); return; } int mid = tree[pos].mid(); build(pos << 1, l, mid); build(pos << 1 | 1, mid + 1, r); push_up(pos); } void update(int pos, int idx, int mass) { if (tree[pos].l == tree[pos].r) { tree[pos].v.first = mass; return; } int mid = tree[pos].mid(); if (idx <= mid) update(pos << 1, idx, mass); else update(pos << 1 | 1, idx, mass); push_up(pos); } pair<int, int> query(int pos, int l, int r) { if (l <= tree[pos].l && tree[pos].r <= r) return tree[pos].v; int mid = tree[pos].mid(); pair<int, int> ret = make_pair(0x3fffffff, 0x3fffffff); if (l <= mid) ret = min(ret, query(pos << 1, l, r)); if (r > mid) ret = min(ret, query(pos << 1 | 1, l, r)); return ret; } int bifind(long long v) { int l = 1, r = n, mid; if (hook[r].d <= v) return r; if (hook[l].d > v) return -1; while (l + 1 < r) { mid = (l + r) >> 1; if (hook[mid].d <= v) l = mid; else r = mid; } return l; } int main() { int i, j, k; while (scanf( %I64d%I64d%d%I64d%d , &hook[0].x, &hook[0].y, &hook[0].p, &hook[0].r, &n) != EOF) { for (i = 1; i <= n; i++) scanf( %I64d%I64d%d%d%I64d , &hook[i].x, &hook[i].y, &hook[i].m, &hook[i].p, &hook[i].r); for (i = 0; i <= n; i++) { hook[i].r *= hook[i].r; hook[i].d = (hook[0].x - hook[i].x) * (hook[0].x - hook[i].x) + (hook[0].y - hook[i].y) * (hook[0].y - hook[i].y); } sort(hook + 1, hook + 1 + n); has.clear(); has.push_back(0); build(1, 1, n); for (i = 0; i < has.size(); i++) { int rs = bifind(hook[has[i]].r); while (rs != -1) { pair<int, int> cur = query(1, 1, rs); if (cur.first <= hook[has[i]].p) { update(1, cur.second, 0x3fffffff); has.push_back(cur.second); } else break; } } printf( %d n , has.size() - 1); } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( -O3 ) #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) using namespace std; long long n, m, k, i, ost, x, y, ans, a[200010], f[200010], ost1, sum1; vector<pair<long long, long long>> v3, v1, v2; pair<long long, long long> sum; multiset<long long> st, st1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (i = 1; i <= n; i++) cin >> a[i]; cin >> x; for (i = 0; i < x; i++) { cin >> y; f[y]++; } cin >> x; for (i = 0; i < x; i++) { cin >> y; f[y] += 2; } for (i = 1; i <= n; i++) { if (f[i] == 1) v1.push_back(make_pair(a[i], i)); if (f[i] == 2) v2.push_back(make_pair(a[i], i)); if (f[i] == 3) v3.push_back(make_pair(a[i], i)); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); x = v3.size(); x = min(x, k); y = k - x; if (min((long long)v1.size(), (long long)v2.size()) < y or m < x + y * 2) { cout << -1 << endl; return 0; } for (i = 0; i < x; i++) { sum.first += v3[i].first; a[v3[i].second] = 1e18; } for (i = 0; i < y; i++) { sum.first += v1[i].first; a[v1[i].second] = 1e18; sum.first += v2[i].first; a[v2[i].second] = 1e18; } sort(a + 1, a + n + 1); ost = m - x - 2 * y; for (i = 1; i <= n; i++) { if (i <= ost) st1.insert(a[i]), sum.second += a[i]; else st.insert(a[i]); } ans = sum.first + sum.second; while (x) { if (min((long long)v1.size(), (long long)v2.size()) <= y or y > k or x + (y + 1) * 2 - 1 > m) break; if (ost > 0) ost--; x--; sum.first -= v3[x].first; sum.first += v1[y].first + v2[y].first; st.insert(v3[x].first); auto it = st.find(v1[y].first); if (it != st.end()) st.erase(it); else { it = st1.find(v1[y].first); st1.erase(it); sum.second -= v1[y].first; } it = st.find(v2[y].first); if (it != st.end()) st.erase(it); else { it = st1.find(v2[y].first); st1.erase(it); sum.second -= v2[y].first; } y++; ost1 = st1.size(), sum1 = sum.second; it = st1.end(); while (ost1 > ost) { ost1--; it--; sum1 -= *it; } for (auto it = st.begin(); it != st.end(); it++) { ost1++; if (ost1 > ost) break; sum1 += *it; } ans = min(ans, sum.first + sum1); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<vector<int> > ad; vector<int> cam(0); vector<bool> vis; bool k = false; void dfs(const int &m) { vis[m] = true; if (m == ad.size() - 1) { k = true; } else for (int i = 0; i < ad[m].size(); i++) { if (!vis[ad[m][i]]) dfs(ad[m][i]); if (k) break; } if (k) { cam.push_back(m); } } int main() { int n; cin >> n; ad.resize(n + 1, vector<int>(0)); vis.resize(n + 1, false); for (int i = int(2); i < int(n + 1); i += int(1)) { int x; cin >> x; ad[i].push_back(x); ad[x].push_back(i); } dfs(1); for (int i = cam.size() - 1; i >= 0; i--) cout << cam[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N]; long long sum[N]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , a + i); sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; long long ans_sum = 0; int ans_len = 0, pos = 1, len = 1; for (int i = 1; i <= n; i++) { int l = 0, r = min(i - 1, n - i); while (l < r) { int ml = (2 * l + r) / 3, mr = (2 * r + l + 2) / 3; long long suml = sum[i] - sum[i - ml - 1] + sum[n] - sum[n - ml]; long long sumr = sum[i] - sum[i - mr - 1] + sum[n] - sum[n - mr]; if (suml * (2 * mr + 1) < sumr * (2 * ml + 1)) l = ml + 1; else r = mr - 1; } long long tmp_sum = sum[i] - sum[i - l - 1] + sum[n] - sum[n - l] - (l * 2 + 1LL) * a[i]; if (tmp_sum * len > ans_sum * (2 * l + 1)) ans_sum = tmp_sum, ans_len = l, pos = i, len = 2 * l + 1; } printf( %d n , ans_len * 2 + 1); for (int i = pos - ans_len; i <= pos; i++) printf( %d , a[i]); for (int i = n - ans_len + 1; i <= n; i++) printf( %d , a[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int N, p; scanf( %d%d , &N, &p); int i; p--; string S; cin >> S; int sz = S.size(), add = 0, flag = 0, first, last; for (i = 0; i < sz / 2; i++) { char c1 = S[i], c2 = S[sz - 1 - i]; if (c1 != c2) { if (flag == 0) { first = i; flag = 1; } last = i; } add += min(abs(c1 - c2), abs(26 - abs(c1 - c2))); } if (p > sz / 2) { p = sz - 1 - p; } if (flag == 0) { printf( 0 ); return 0; } if (p != sz / 2) { if (last == first) { printf( %d , abs(p - last) + add); return 0; } if (last < p) { printf( %d , abs(p - first) + add); return 0; } if (first > p) { printf( %d , abs(p - last) + add); return 0; } printf( %d , min(2 * (p - first) + (last - p), 2 * (last - p) + (p - first)) + add); return 0; } if (p == sz / 2) { if (sz % 2) { printf( %d , p - first + add); return 0; } else { p--; printf( %d , p - first + add); return 0; } } } |
#include <bits/stdc++.h> int main() { int J, M; float QJ, QM; int i; scanf( %d , &J); scanf( %d , &M); i = 1; while (1) { J = J - i; if (J < 0) { printf( Vladik n ); break; } i++; M = M - i; if (M < 0) { printf( Valera n ); break; } i++; } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[3001]; void swap(int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } int main() { int n; cin >> n; ; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<pair<int, int> > x; for (int i = 0; i < n; i++) { int min = a[i]; int sw = i; for (int j = i + 1; j < n; j++) { if (a[j] < min) { min = a[j]; sw = j; } } if (sw != i) { x.push_back(make_pair(i, sw)); swap(i, sw); } } cout << x.size() << endl; for (vector<pair<int, int> >::iterator i = x.begin(); i != x.end(); i++) { cout << (*i).first << << (*i).second << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; const int INF = 1e7; struct info { pair<int, int> x = {-INF, -1}; }; struct segtree { int n; vector<info> t; segtree(int n) { this->n = n; t.resize(n * 4); } void Merge(info &node, info &l, info &r) { node.x = max(l.x, r.x); } void update(int node, int l, int r, int i, int j, int add) { if (l > j || r < i) { return; } if (l >= i && r <= j) { t[node].x = {add, i}; return; } int mid = (l + r) / 2; update(node * 2, l, mid, i, j, add); update(node * 2 + 1, mid + 1, r, i, j, add); Merge(t[node], t[node * 2], t[node * 2 + 1]); } void update(int l, int add) { update(1, 0, n - 1, l, l, add); } info query(int node, int l, int r, int i, int j) { if (l > j || r < i) { info x; return x; } if (l >= i && r <= j) { return t[node]; } int mid = (l + r) / 2; info x = query(node * 2, l, mid, i, j); info y = query(node * 2 + 1, mid + 1, r, i, j); info ret; Merge(ret, x, y); return ret; } pair<int, int> query(int l, int r) { info ret = query(1, 0, n - 1, l, r); return ret.x; } }; int n; int a[N]; int b[N]; int dist[N][2]; vector<int> adj[N]; bool vis[N][2]; int p[N][2]; int go[N][2]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; segtree seg(n + 1); for (int i = 0; i <= n; i++) { dist[i][0] = INF; dist[i][1] = INF; seg.update(i, INF); } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } priority_queue<pair<int, pair<int, int> > > pq; pq.push({0, {n, 0}}); dist[n][0] = 0; dist[n][1] = 0; seg.update(n, 0); while (!pq.empty()) { pair<int, pair<int, int> > t = pq.top(); pq.pop(); int v = t.second.first; int state = t.second.second; if (vis[v][state]) continue; vis[v][state] = 1; if (state == 1) { int koi = v + b[v]; if (dist[koi][0] > dist[v][1]) { dist[koi][0] = dist[v][1]; go[koi][0] = v; pq.push({-dist[koi][0], {koi, 0}}); } continue; } int l = v - a[v]; int r = v; while (true) { pair<int, int> x = seg.query(l, r); if (x.first <= dist[v][0] + 1) { break; } int koi = x.second; dist[koi][1] = dist[v][0] + 1; go[koi][1] = v; seg.update(koi, dist[koi][1]); pq.push({-dist[koi][1], {koi, 1}}); } } if (dist[0][0] >= INF) { cout << -1 n ; return 0; } cout << dist[0][0] << n ; pair<int, int> now = {0, 0}; vector<int> ans; while (1) { if (now.second == 1) { ans.push_back(now.first); } if (now.first == n) { break; } now.first = go[now.first][now.second]; now.second ^= 1; }; reverse(ans.begin(), ans.end()); for (int i : ans) cout << i << ; cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int Distance(int x1, int y1, int x2, int y2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } int main() { int n, m, x1, y1, x2, y2, x3, y3, x4, y4, flag, dis1, dis2; while (~scanf( %d%d , &n, &m)) { flag = 0; if (m > n) { swap(n, m), flag = 1; } if (m == 0) { x1 = 1, x2 = n; x3 = 0; x4 = n - 1; y1 = y2 = y3 = y4 = 0; } else { dis1 = Distance(0, 0, n, m) * 2 + Distance(0, 0, n, 0); dis2 = Distance(0, 0, n, m - 1) * 2 + Distance(0, 0, n, m); if (dis1 > dis2) { x1 = n; y1 = m; x2 = 0; y2 = 0; x3 = n; y3 = 0; x4 = 0; y4 = m; } else { x1 = 0; y1 = 1; x2 = n; y2 = m; x3 = 0; y3 = 0; x4 = n; y4 = m - 1; } } if (flag) { swap(x1, y1); swap(x2, y2); swap(x3, y3); swap(x4, y4); } printf( %d %d n%d %d n%d %d n%d %d n , x1, y1, x2, y2, x3, y3, x4, y4); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[111], f[(1 << (23))], x[33]; int main() { while (cin >> n) { for (int i = 0, _a = (n); i < _a; i++) cin >> a[i]; if (n == 1) { cout << 1 << endl; continue; } int res = n + 1; for (int S = 0, _a = ((1 << (n))); S < _a; S++) f[S] = n + 1; f[1] = 1; for (int S = (1), _b = ((1 << (n)) - 2); S <= _b; S++) { int last = -1; for (int i = (n - 1), _b = (0); i >= _b; i--) if ((S & (1 << (i)))) { last = i; break; } int cur = f[S]; if (last == n - 1) { res = min(res, cur); continue; } int next = last + 1; bool ok = false; int nx = 0; for (int i = 0, _a = (n); i < _a; i++) if ((S & (1 << (i)))) x[++nx] = a[i]; sort(x + 1, x + nx + 1); int j = nx, i = 1; while (i <= j) { if (x[i] + x[j] == a[next]) { ok = true; break; } else if (x[i] + x[j] > a[next]) { --j; } else ++i; } if (!ok) continue; int S2 = S | (1 << (next)); f[S2] = min(f[S2], max(cur, __builtin_popcount(S2))); for (int i = 0, _a = (n); i < _a; i++) if ((S & (1 << (i)))) { S2 = S - (1 << (i)) + (1 << (next)); f[S2] = min(f[S2], max(cur, __builtin_popcount(S2))); } } if (res > n) cout << -1 << endl; else cout << res << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; cin >> a >> b; long long ans = 0; while (true) { if (!a || !b) { cout << ans; return 0; } if (a == b || (a % b == 0)) { cout << (a / b + ans); return 0; } if (a > b) { long long tmp = a / b; a -= tmp * b; ans += tmp; } else { long long tmp = b / a; b -= tmp * a; ans += tmp; } } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> bool ckmax(T& x, T y) { return x < y ? x = y, 1 : 0; } template <class T> bool ckmin(T& x, T y) { return x > y ? x = y, 1 : 0; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = 0; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - 0 , ch = getchar(); return f ? x : -x; } inline char rdopt() { char ch = getchar(); while (!isalpha(ch)) ch = getchar(); return ch; } const int N = 1 << 20; int n, m, t1, t2, nq, pre[N]; long long tr[N], ans[N]; vector<int> e1[N], e2[N], u2[N]; vector<pair<int, int> > u1[N], q[N]; struct dsu { int F[N], siz[N]; inline void init(const int& n = N - 1) { for (int i = 1, iend = n; i <= iend; ++i) F[i] = i, siz[i] = 1; } inline int anc(int x) { return x == F[x] ? x : F[x] = anc(F[x]); } } d1, d2; inline void add(int x, long long d) { for (int i = x; i <= m; i += i & -i) tr[i] += d; } inline long long ask(int x) { long long res = 0; for (int i = x; i > 0; i -= i & -i) res += tr[i]; return res; } inline int query(int tim) { int res = 0, lim = ask(tim); for (int i = 19; i >= 0; --i) if (res + (1 << i) <= m && tr[res + (1 << i)] < lim) res += 1 << i, lim -= tr[res]; return res; } void dfs2(int u) { for (int i : u2[u]) add(i, 1); for (pair<int, int> i : q[u]) pre[i.second] = query(i.first); for (int v : e2[u]) dfs2(v); for (int i : u2[u]) add(i, -1); } void dfs1(int u) { for (pair<int, int> i : u1[u]) add(i.first, i.second); for (pair<int, int> i : q[u]) ans[i.second] = ask(i.first) - ask(pre[i.second]); for (int v : e1[u]) dfs1(v); for (pair<int, int> i : u1[u]) add(i.first, -i.second); } signed main() { n = read(), m = read(); d1.init(n), d2.init(n), t1 = t2 = n; for (int i = 1; i <= m; ++i) { char op = rdopt(); if (op == U ) { int x = read(), y = read(), fx = d1.anc(x), fy = d1.anc(y); ++t1, d1.F[fx] = d1.F[fy] = d1.F[t1] = t1, d1.siz[t1] = d1.siz[fx] + d1.siz[fy]; e1[t1].push_back(fx), e1[t1].push_back(fy); } else if (op == M ) { int x = read(), y = read(), fx = d2.anc(x), fy = d2.anc(y); ++t2, d2.F[fx] = d2.F[fy] = d2.F[t2] = t2; e2[t2].push_back(fx), e2[t2].push_back(fy); } else if (op == A ) { int x = read(), fx = d1.anc(x); u1[fx].push_back(make_pair(i, d1.siz[fx])); } else if (op == Z ) { int x = read(), fx = d2.anc(x); u2[fx].push_back(i); } else if (op == Q ) { int x = read(); q[x].push_back(make_pair(i, ++nq)); } } for (int i = 1, iend = t2; i <= iend; ++i) if (d2.F[i] == i) dfs2(i); for (int i = 1, iend = t1; i <= iend; ++i) if (d1.F[i] == i) dfs1(i); for (int i = 1; i <= nq; ++i) printf( %lld n , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; int n, m, k, a[N]; bool mark[N]; int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> a[i], mark[a[i]] = 1; deque<int> que; int ans = 0; for (int i = 1; i < N; i++) { while (que.size() && que.front() <= i - m) que.pop_front(); if (mark[i]) que.push_back(i); if (que.size() >= k) while (que.size() >= k) que.pop_back(), ans++; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; template <typename T> void read(T &x) { x = 0; char op = getchar(); int F = 1; while (!isdigit(op)) { if (op == - ) F *= -1; op = getchar(); } while (isdigit(op)) { x = (x << 1) + (x << 3) + op - 0 ; op = getchar(); } x *= F; } template <typename T, typename... Args> void read(T &x, Args &...args) { read(x); read(args...); } template <typename T1, typename T2> void ckmax(T1 &x, T2 y) { if (x < y) x = y; } template <typename T1, typename T2> void ckmin(T1 &x, T2 y) { if (x > y) x = y; } int a[1005]; int n, m, c; void work() { int rec; cin >> rec; if (2 * rec <= c) { for (int i = 1; i <= n; i++) { if (a[i] > rec || !a[i]) { a[i] = rec; cout << i << n ; break; } } } else { for (int i = n; i >= 1; i--) { if (a[i] < rec || !a[i]) { a[i] = rec; cout << i << n ; break; } } } fflush(stdout); } int main() { cin >> n >> m >> c; while (m--) work(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 1e5 + 100; long long a[maxn]; map<long long, long long> p[maxn]; vector<int> G[maxn]; long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } long long ans = 0; void dfs(int v, int fa) { for (auto it : p[fa]) { long long t = gcd(a[v], it.first); p[v][t] += it.second; ans = (ans + t % mod * it.second % mod) % mod; } p[v][a[v]] += 1; ans = (ans + a[v]) % mod; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i]; if (to == fa) continue; dfs(to, v); } } int main() { std::ios::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i++) cin >> a[i]; for (int i = 1; i < N; i++) { int x; int y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } dfs(1, 0); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string str[n]; for (int i = 0; i < n; i++) cin >> str[i]; int brr[m]; for (int i = 0; i < m; i++) cin >> brr[i]; int count = 0; for (int i = 0; i < m; i++) { int arr[5] = {0}; for (int j = 0; j < n; j++) { arr[str[j][i] - A ]++; } int k = *max_element(arr, arr + 5); count += k * brr[i]; } cout << count; } |
#include <bits/stdc++.h> using namespace std; int main() { double n, a, d; cin >> n >> a >> d; vector<int> t; vector<int> v; double prev = 0; int bit = 1; for (int i = 0; i < n; i++) { double t1, v1; cin >> t1 >> v1; double t2 = 0, t3 = 0, tot = 0; double dis = 0; t2 = (double)(v1) / double(a); dis = a * (t2) * (t2); dis = (float)dis / (float)2; if (dis >= d) { double velo = 2 * a * d; velo = sqrt(velo); t2 = velo / a; tot = t2 + (double)t1; } else { dis = d - dis; t3 = (double)dis / (double)v1; tot = (double)t2 + t3 + (double)t1; } if (tot > prev || bit == 1) { cout << setprecision(12) << tot << endl; prev = tot; bit = 0; } else { cout << setprecision(12) << prev << endl; bit = 0; } } } |
#include <bits/stdc++.h> using namespace std; class Mod { public: int mod; Mod(int mod_ = 1e9 + 7) : mod(mod_) {} int add(int a, int b) { return (a + b) % mod; } int mul(long long a, int b) { return (a * b) % mod; } int pow(long long a, int x) { int ans = 1; while (x) { if (x & 1) { ans = (ans * a) % mod; } x >>= 1; a = (a * a) % mod; } return ans; } }; class IO { public: static void read(int &val) { scanf( %d , &val); } static void read(long long &val) { scanf( %lld , &val); } static void read(char *val) { scanf( %s , val); } static void read(string &val) { const int BUFFER_SIZE = 1e5 + 5; static char buffer[BUFFER_SIZE]; scanf( %s , buffer); val = buffer; } static void read_n(vector<int> &val, int n) { val.resize(n); for (int i = 0; i < n; i++) { scanf( %d , &val[i]); } } static void write(int val) { printf( %d n , val); } static void write(long long val) { printf( %lld n , val); } static void write(const char *val) { printf( %s n , val); } static void write(const string &val) { printf( %s n , val.c_str()); } static void write(const vector<int> &val) { for (int i = 0; i < val.size(); i++) { if (i > 0) { putchar( ); } printf( %d , val[i]); } putchar( n ); } }; long long gcd_ex(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long ans = gcd_ex(b, a % b, y, x); y -= (a / b) * x; return ans; } class Solver { long long x; stringstream ans; int cnt_ans = 0; void print(long long x, long long a) { long long cur = 0; while (a) { if (a & 1) { if (cur > 0) { ans << cur << + << x << endl; cnt_ans++; } cur += x; } a >>= 1; ans << x << + << x << endl; cnt_ans++; x <<= 1; } } public: void read() { IO::read(x); } void solve() { long long e = 1; while ((e << 1) <= x) { e <<= 1; } print(x, e); ans << e * x << ^ << x << endl; cnt_ans++; long long y = ((e * x) ^ x); long long a, b; gcd_ex(x, y, a, b); if (a < 0) { swap(a, b); swap(x, y); } b = -b; if (!(a & 1)) { a += y; b += x; } print(x, a); print(y, b); ans << a * x << ^ << b * y << endl; cnt_ans++; IO::write(cnt_ans); IO::write(ans.str()); } }; void init() { ios::sync_with_stdio(false); } int main() { init(); int t = 1; while (t--) { Solver solver; solver.read(); solver.solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; map<int, int> m[N]; set<pair<int, int> > s[N]; vector<int> g[N]; int n, c[N]; long long ans[N]; void dfs(int u, int p) { m[u][c[u]] = 1; s[u].insert(pair<int, int>(1, c[u])); ans[u] = c[u]; for (int v : g[u]) { if (v == p) continue; dfs(v, u); if (m[u].size() < m[v].size()) { m[u].swap(m[v]); s[u].swap(s[v]); ans[u] = ans[v]; } } for (int v : g[u]) { if (v == p) continue; for (auto x : m[v]) { s[u].erase(pair<int, int>(m[u][x.first], x.first)); m[u][x.first] += x.second; if (s[u].empty() || m[u][x.first] > (*--s[u].end()).first) ans[u] = x.first; else if (m[u][x.first] == (*--s[u].end()).first) ans[u] += x.first; s[u].insert(pair<int, int>(m[u][x.first], x.first)); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> c[i]; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs(1, 0); for (int i = 1; i <= n; ++i) cout << ans[i] << ; } |
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; long long INF = 1e18; int n, s; vector<int> ans; long long ansV = 0; long long dis[N]; int edge[N], val[N]; vector<pair<int, int> > adj[N]; void solve() { for (int i = 1; i <= n; i++) dis[i] = INF; priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > pq; dis[s] = 0; pq.push({dis[s], s}); while (!pq.empty()) { int u = pq.top().second; long long Cost = pq.top().first; pq.pop(); if (Cost > dis[u]) continue; if (u != s) { ans.push_back(edge[u]); ansV += val[edge[u]]; } for (auto P : adj[u]) { int v = P.first, idx = P.second; if (Cost + val[idx] < dis[v]) { dis[v] = Cost + val[idx]; edge[v] = idx; pq.push({dis[v], v}); } else if (Cost + val[idx] == dis[v] && val[idx] < val[edge[v]]) edge[v] = idx; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v >> val[i]; adj[u].push_back({v, i}); adj[v].push_back({u, i}); } cin >> s; solve(); sort(ans.begin(), ans.end()); cout << ansV << endl; for (auto v : ans) cout << v + 1 << ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long double PI = acosl(-1.0); const double EPS = 1e-12; void showBit(int no, int N) { for (int i = N - 1; i >= 0; i--) if ((no >> i) & 1) printf( 1 ); else printf( 0 ); printf( n ); } int binToDec(string s) { int ln = s.length(); int sum = 0; for (int i = ln - 1, k = 0; i >= 0; i--, k++) sum = sum + (s[i] - 0 ) * (1 << k); return sum; } void combination(long long com[][1005]) { com[0][0] = 1; int sz = 1000; for (int i = 1; i <= sz; i++) { com[i][0] = 1; for (int j = 1; j <= i; j++) { com[i][j] = com[i - 1][j] + com[i - 1][j - 1]; com[i][j] = com[i][j] % 1000000007; } } } int find_par(int cn) {} int gcd(int u, int v) { if (v == 0) return u; return gcd(v, u % v); } void insert_ind(int cs, int ci, int s, int e) { if (s == e) { return; } int mid = (s + e) / 2; if (ci <= mid) insert_ind(2 * cs, ci, s, mid); else insert_ind(2 * cs + 1, ci, mid + 1, e); } void update_ind(int cs, int ci, int s, int e, int val) { if (s == e) { return; } int mid = (s + e) / 2; if (ci <= mid) update_ind(2 * cs, ci, s, mid, val); else update_ind(2 * cs + 1, ci, mid + 1, e, val); } void update_seg(int cs, int ds, int de, int s, int e, int val) { if (ds == s && de == e) { return; } int mid = (s + e) / 2; if (ds <= mid) update_seg(2 * cs, ds, min(mid, de), s, mid, val); if (de > mid) update_seg(2 * cs + 1, max(mid + 1, ds), de, mid + 1, e, val); } void pattern_match(char pat[], const int len, int pi[]) { pi[1] = 0; for (int i = 2; i <= len; i++) { int st = i - 1; while (st >= 0 && pat[pi[st] + 1] != pat[i]) st = pi[st]; if (st == 0) pi[i] = 0; else pi[i] = pi[st] + 1; } } void pattern_match(char pat[], int pi[], char tar[], const int len1, int gi[]) { if (pat[0] == tar[0]) gi[0] = 1; for (int i = 1; i < len1; i++) { int st = gi[i - 1]; while (pat[st] != tar[i]) { if (st == 0) { st = -1; break; } st = pi[st - 1]; } if (st == -1) gi[i] = 0; else gi[i] = st + 1; } } struct myArray { int mat[10][10]; }; myArray matrix_mul(myArray mat1, myArray mat2, const int sz) { int tmp[sz][sz]; memset((tmp), 0, sizeof((tmp))); for (int r = 0; r < sz; r++) for (int c = 0; c < sz; c++) for (int cr = 0; cr < sz; cr++) tmp[r][c] += mat1.mat[r][cr] * mat2.mat[c][cr]; myArray res; for (int r = 0; r < sz; r++) for (int c = 0; c < sz; c++) res.mat[r][c] = tmp[r][c]; return res; } myArray unit_matrix(const int sz) { myArray ma; memset((ma.mat), 0, sizeof((ma.mat))); for (int i = 0; i < sz; i++) ma.mat[i][i] = 1; return ma; } myArray matrix_expo(int p, myArray mat, const int sz) { if (p == 0) { return unit_matrix(sz); } if (p & 1) return matrix_mul(mat, matrix_expo(p - 1, mat, sz), sz); myArray tmp = matrix_expo(p >> 1, mat, sz); return matrix_mul(tmp, tmp, sz); } int X[] = {0, 0, 1, -1}; int Y[] = {1, -1, 0, 0}; map<string, int> msi; void cal_state(string s, int tran[][30]) { int ln = s.length(); string s1 = s; for (int i = 0; i < ln; i++) { msi[s1] = ln - i; s1 = s1.substr(0, ln - i - 1); } s1 = ; for (int i = 0; i <= ln; i++) { string ts = s1; for (int j = 0; j < 26; j++) { s1 = ts + (char)( a + j); int tln = i + 1; while (msi[s1] == 0) { s1 = s1.substr(1); tln--; if (tln == 0) break; } if (tln == 0) s1 = ; tran[i][j] = msi[s1]; } if (i < ln) s1 = s.substr(0, i + 1); } } string s, b; int tran[105][30]; int n, k; bool col[105]; bool dp[105][105][105]; int c[105][105][105], ps[105][105][105]; int prp[105][105][105]; void print(int ln, int cs, int rp) { if (ln == 0) return; print(ln - 1, ps[ln][cs][rp], prp[ln][cs][rp]); printf( %c , a + c[ln][cs][rp]); } int main() { cin >> n >> k; cin >> s; cal_state(s, tran); cin >> b; int ln = b.length(); int cnt = 0; for (int i = (0); i < (ln); ++i) if (b[i] == 1 ) col[i] = true, cnt++; dp[0][0][0] = true; ln = s.length(); for (int i = 0; i < n; i++) for (int j = 0; j <= n; j++) for (int rp = 0; rp <= cnt; rp++) if (dp[i][j][rp]) { for (int ne = 0; ne < k; ne++) { int ns = tran[j][ne]; if (ns == ln) { if (col[i - ln + 1] && rp + 1 <= cnt) { dp[i + 1][ns][rp + 1] = true; c[i + 1][ns][rp + 1] = ne; ps[i + 1][ns][rp + 1] = j; prp[i + 1][ns][rp + 1] = rp; } } else { dp[i + 1][ns][rp] = true; c[i + 1][ns][rp] = ne; ps[i + 1][ns][rp] = j; prp[i + 1][ns][rp] = rp; } } } for (int i = 0; i <= n; i++) if (dp[n][i][cnt] == true) { print(n, i, cnt); printf( n ); return 0; } printf( No solution n ); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > result; void move(pair<int, int> &from, pair<int, int> &to) { from.first -= to.first; to.first *= 2; result.push_back(make_pair(to.second, from.second)); } void removeone(pair<int, int> list[3]) { sort(list, list + 3); if (list[0].first == 0) { swap(list[0], list[2]); return; } int q = list[1].first / list[0].first; for (int x = 1; q; x <<= 1) if (q & x) { move(list[1], list[0]); q -= x; } else move(list[2], list[0]); removeone(list); } int main(int argc, char **argv) { int n; vector<pair<int, int> > nums; scanf( %d , &n); for (int i = 0; i < n; i++) { int v; scanf( %d , &v); if (v) nums.push_back(make_pair(v, i)); } if (nums.size() < 2) { printf( -1 n ); return 0; } result.clear(); while (nums.size() > 2) { removeone(&nums[nums.size() - 3]); nums.pop_back(); } printf( %d n , (int)result.size()); for (int i = 0; i < (int)result.size(); i++) printf( %d %d n , result[i].first + 1, result[i].second + 1); return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) long long x, y, z, a, b, c, d, n, m, mod = 1000000007; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string s; bool w = true; cin >> n >> m >> s; a = s.find( G ); b = s.find( T ); if (a > b && (a - b) % m == 0) { for (int i = a; i > b; i -= m) { if (s[i] == # ) { w = false; break; } } } else if (a < b && (b - a) % m == 0) { for (int i = a; i < b; i += m) { if (s[i] == # ) { w = false; break; } } } if (w == false || abs(b - a) % m != 0) cout << NO ; else cout << YES ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); string a, b; int k; cin >> a >> b >> k; int n = a.length(), l = 0; long long gd = 0, bd = 0; int f = 0; if (a.length() != b.length()) { cout << 0; return 0; } for (int i = 0; i < n; i++) { int j; for (j = 0; j < n; j++) if (a[(i + j) % n] != a[j]) break; if (j == n) gd++; else bd++; for (j = 0; j < n; j++) if (a[(i + j) % n] != b[j]) break; if (j == n) f = 1; } if (f == 0) { cout << 0; return 0; } vector<int> dp(2, 0), dp1(2, 0); if (a == b) dp[0] = 1; else dp1[0] = 1; long long mod = 1e9 + 7; for (int i = 1; i <= k; i++) { long long x = dp[1 - i % 2] * (gd - 1) + dp1[1 - i % 2] * gd; x %= mod; dp[i % 2] = x; x = dp[1 - i % 2] * bd + dp1[1 - i % 2] * (bd - 1); x %= mod; dp1[i % 2] = x; } cout << dp[k % 2] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void slove() { int n, m, k; cin >> n >> m >> k; int ls = 0; if (n % 2) { n--, ls += m / 2; } if (m % 2) m--; int flag = 1; if (ls > k) flag = 0; ls += (n * m) / 2; if (ls < k) flag = 0; if (ls % 2 != k % 2) flag = 0; if (flag) puts( YES ); else puts( NO ); } int main() { int T; scanf( %d , &T); while (T--) { slove(); } return 0; } |
#include <bits/stdc++.h> template <class T, class F, class C> auto binary_search(T l, T r, F func, C cmp) -> T; auto division(int64_t n, int64_t d) -> int64_t { return n / d; } int main() { int64_t n, k, m, d; std::cin >> n >> k >> m >> d; int64_t answer = 0; for (int64_t q_k = 1; q_k <= d; ++q_k) { int64_t x_max = binary_search( int64_t(1), m + 1, [n, q_k, k](int64_t x) { return (division(n, x) - 1) / k + 1 >= q_k; }, [](int64_t l, int64_t r) { return l + 1 >= r; }); if ((division(n, x_max) - 1) / k + 1 != q_k) { continue; } answer = std::max(answer, x_max * q_k); } std::cout << answer << n ; return 0; } template <class T, class F, class C> auto binary_search(T l, T r, F func, C cmp) -> T { bool dir = func(l); for (auto mid = (l + r) / 2; !cmp(l, r); mid = (l + r) / 2) { (func(mid) == dir ? l : r) = mid; } return l; } |
#include <algorithm> #include <cmath> #include <vector> #include <iostream> using namespace std; void solve() { int n, totalCost, prev, x, cost; totalCost = 0; cin >> n; if (n <= 1) { cout << 0 << n ; return; } cin >> prev; int m, M; for (int i = 1; i < n; ++i) { cin >> x; if (x < prev) { m = x; M = prev; } else { m = prev; M = x; } if (m * 2 < M) { cost = int(ceil(log2(float(M) / float(m)))) - 1; // cout << -> << m << << M << << cost << n ; totalCost += cost; } prev = x; } cout << totalCost << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; for (int t = 0; t < T; ++t) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; char s[105]; int main() { int n, nr = 0; gets(s); n = strlen(s); int x = 0; for (int i = 0; i < n; i++) x = (x * 2 + s[i] - 0 ) % mod; for (int i = 1; i < n; i++) x = (x * 2) % mod; printf( %d n , x); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, b, mod; cin >> n >> m >> b >> mod; int dp[502][502]; int bugs[n + 2]; for (int i = 0; i < n; i++) cin >> bugs[i]; memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int k = 0; k < n; k++) { for (int j = 0; j <= b - bugs[k]; j++) { for (int i = 0; i < m; i++) { dp[i + 1][j + bugs[k]] += dp[i][j]; dp[i + 1][j + bugs[k]] %= mod; } } } int ans = 0; for (int i = 0; i <= b; i++) { ans += dp[m][i]; ans %= mod; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; mt19937_64 rang( chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; void solve() { int sum, limit; cin >> sum >> limit; vector<int> res; for (int i = limit; i >= 1; i--) { int lowbit = i ^ (i & (i - 1)); if (sum >= lowbit) res.push_back(i), sum -= lowbit; if (sum == 0) break; } if (sum != 0) cout << -1; else { cout << res.size() << n ; for (auto x : res) cout << x << ; } } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = (int)4e6 + 11; const long long inf = 1e9 + 7; int n, m, p[1 << 20], can[1 << 20]; int find_parent(int x) { if (x == p[x]) { return x; } return p[x] = find_parent(p[x]); } int main() { scanf( %d %d , &n, &m); for (int i = 0; i < 1 + n; ++i) { p[i] = i; } memset(can, -1, sizeof(can)); for (int i = 0; i < m; ++i) { int l, r, x; scanf( %d %d %d , &l, &r, &x); --x; l = find_parent(l - 1); while (l < r) { if (l != x) { can[l] = x; p[l] = 1 + l; } l = find_parent(1 + l); } } for (int i = 0; i < n; ++i) { printf( %d , can[i] + 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 110, alpha = 26; int n, m; int dp[maxn][maxn][alpha]; bool mark[maxn]; vector<int> ts; vector<pair<int, int> > G[maxn]; void dfs(int a) { mark[a] = 1; for (pair<int, int> p : G[a]) { int b = p.first; if (!mark[b]) { dfs(b); } } ts.push_back(a); } int32_t main() { cin >> n >> m; for (int i = int(0); i < int(m); i++) { int a, b; char c; cin >> a >> b >> c; a--, b--; G[a].push_back({b, c - a }); } for (int i = int(0); i < int(n); i++) { if (!mark[i]) { dfs(i); } } for (int size = int(0); size < int(2 * n - 1); size++) { for (int i = int(0); i < int(min(size + 1, n)); i++) { int j = size - i; if (j >= n) continue; int a = ts[i], b = ts[j]; for (int x = int(0); x < int(alpha); x++) { for (pair<int, int> p : G[a]) { int ap = p.first, c = p.second; if (c >= x) dp[a][b][x] = max(dp[a][b][x], 1 - dp[b][ap][c]); } } } } for (int i = int(0); i < int(n); i++) { for (int j = int(0); j < int(n); j++) { cout << (dp[i][j][0] ? A : B ); } cout << endl; } } |
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { int n, i, f; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); f = 0; for (i = 1; i < n; i++) { if (a[i] != a[0]) { f = 1; break; } } if (f == 1) cout << a[i]; else cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 110; string s[maxn]; int cur[maxn], c[maxn]; int n, m, ans = 0; void dfs(int x) { if (x == n) { int flag = 1; for (int i = 1; i <= m; i++) if (cur[i] - c[i]) flag = 0; ans += flag; return; } for (char v = 0 ; v <= 1 ; v++) { int can = 1; for (int i = 1; i <= m; i++) { cur[i] += (v == s[i][x]); if (cur[i] > c[i]) can = 0; } if (can) dfs(x + 1); for (int i = 1; i <= m; i++) cur[i] -= (v == s[i][x]); } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) cin >> s[i] >> c[i]; memset(cur, 0, sizeof(cur)); dfs(0); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct Hint { int up, cnt; bool operator<(Hint o) const { return up < o.up; } }; int n, b, q; Hint h[10013]; int has[10013][5], take[10013]; int main() { scanf( %d%d%d , &n, &b, &q); for (int i = 1; i <= q; i++) { scanf( %d%d , &h[i].up, &h[i].cnt); } h[++q] = {b, n}; sort(h, h + q + 1); for (int i = 1; i <= q; i++) { for (int j = h[i - 1].up + 1; j <= h[i].up; j++) has[i][j % 5] += 1; take[i] = h[i].cnt - h[i - 1].cnt; if (0 > take[i] || take[i] > h[i].up - h[i - 1].up) return printf( unfair n ), 0; } for (int i = 1; i < 32; i++) { int need = n / 5 * __builtin_popcount(i); int can = 0; for (int k = 1; k <= q; k++) { int cur = 0; for (int j = 0; j < 5; j++) if (i & (1 << j)) { cur += has[k][j]; } can += min(cur, take[k]); } if (need > can) return printf( unfair n ), 0; } printf( fair n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 60; map<double, int> p; bitset<N * N> g[N * N + 5]; int n, m; double ya[N + 5], yb[N + 5]; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %lf , &ya[i]); for (int i = 1; i <= m; i++) scanf( %lf , &yb[i]); int tot = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { double now = (ya[i] + yb[j]) / 2; if (p.find(now) == p.end()) tot++, p[now] = tot; g[p[now]].set(i, 1); g[p[now]].set(j + n, 1); } } int ans = -1; for (int i = 1; i <= tot; i++) { for (int j = i; j <= tot; j++) { bitset<N * N> s; s = g[i] | g[j]; ans = max(ans, (int)s.count()); } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; int ans; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int a[1111] = {}, cnt[1111] = {}; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { if (cnt[i] == 0) { ans++; for (int j = i + 1; j <= n; j++) { if (a[j] % a[i] == 0) { cnt[j] = 1; } } } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k; cin >> n; vector<long long> dp(n + 3, 0); vector<long long> v; long long sum = 0; for (int j = 0; j < n; j++) { cin >> k; if (j % 2 == 0) { sum = sum + k; } v.push_back(k); } dp[0] = 0; long long ans = 0; long long z = 0; if (n > 1) { dp[1] = max(z, v[1] - v[0]); ans = max(ans, dp[1]); } k = 2; for (int j = 1; j < n - 1; j++) { if (j % 2 == 0) { dp[k] = max(z, dp[k - 2] + (v[j + 1] - v[j])); } else { dp[k] = max(z, dp[k - 2] + (v[j] - v[j + 1])); } ans = max(ans, dp[k]); k++; } cout << sum + ans << endl; } } |
#include <stdio.h> int main() { int t; scanf( %d , &t); while (t--) { int x, i; scanf( %d , &x); for (i = 1; i * (i + 1) / 2 < x; i++); if (i * (i + 1) / 2 - x != 1) { printf( %d n , i); } else { printf( %d n , i + 1); } } } |
#include <bits/stdc++.h> template <typename Y> inline bool updmin(Y &a, Y b) { if (a > b) { a = b; return 1; } return 0; } template <typename Y> inline bool updmax(Y &a, Y b) { if (a < b) { a = b; return 1; } return 0; } template <typename Y> inline Y abs(Y a) { if (a < 0) a = -a; return a; } template <typename Y> inline Y sqr(Y a) { return a * a; } int read() { int w = 1, q = 0, ch = ; for (; ch < 0 || ch > 9 ; ch = getchar()) if (ch == - ) w = -1; for (; ch >= 0 && ch <= 9 ; ch = getchar()) q = q * 10 + ch - 48; return q * w; } inline void FileIO() { freopen( .in , r , stdin); freopen( .out , w , stdout); } int n, m, h; const int N = 100020; std::vector<int> G[N], s; int tim = 0, cnt = 0; int low[N], dfn[N], vis[N], bel[N]; void tarjan(int p) { vis[p] = 1; s.push_back(p); low[p] = dfn[p] = ++tim; for (int t : G[p]) { if (!dfn[t]) { tarjan(t); updmin(low[p], low[t]); } else if (vis[t]) { updmin(low[p], dfn[t]); } } if (low[p] == dfn[p]) { ++cnt; while (s.back() != p) { int cur = s.back(); s.pop_back(); vis[cur] = 0; bel[cur] = cnt; } bel[p] = cnt; vis[p] = 0; s.pop_back(); } } int u[N], size[N], out[N]; int main() { n = read(); m = read(); h = read(); for (int i = 1; i <= n; i++) { u[i] = read(); } for (int i = 1; i <= m; i++) { int x = read(), y = read(); if ((u[x] + 1) % h == u[y]) { G[x].push_back(y); } if ((u[y] + 1) % h == u[x]) { G[y].push_back(x); } } for (int i = 1; i <= n; i++) { if (!dfn[i]) { tarjan(i); } } for (int i = 1; i <= n; i++) { size[bel[i]]++; for (int j : G[i]) { if (bel[i] != bel[j]) { out[bel[i]]++; } } } int ans = n + 1, pos = 0; for (int i = 1; i <= cnt; i++) { if (!out[i] && updmin(ans, size[i])) { pos = i; } } printf( %d n , ans); for (int i = 1; i <= n; i++) { if (bel[i] == pos) { printf( %d , i); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); string r; int i = 0; while (i < n) { if (n - i >= 3) { if (s.substr(i, 3) == WUB ) { if (r.size() && r.back() != ) r += ; i += 3; } else r += s[i++]; } else { r += s[i++]; } } cout << r << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long ara[12]; int main() { long long n, i, j, k; for (i = 0; i < 4; i++) { cin >> k; ara[i] = k; } if (ara[0] == 0 && ara[3] == 0 && ara[2] > 0) { printf( 0 n ); } else { if (ara[0] == ara[3]) printf( 1 n ); else printf( 0 n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool isvowel(char c) { return c == a || c == e || c == i || c == o || c == u ; } int main() { char arr[110]; scanf( %s , arr); int len = strlen(arr); bool check = true; for (int i = 0; i < len; i++) { if (isvowel(arr[i])) { } else if (arr[i] == n ) { } else { if (i == len - 1 || !isvowel(arr[i + 1])) check = false; } } puts(check ? YES : NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int x) { long long int res = 1; while (x > 0) { if (x & 1) { res *= a; } a *= a; x = x / 2; } return res; } int main() { long long int n; scanf( %lld , &n); long long int ans = 0; ans = power(4, n - 2) * 6; if (n > 3) { ans += (n - 3) * 4 * 9 * power(4, n - 4); } printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; const int A = 1e5 + 11; const int B = 1e6 + 11; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; inline int read() { char c = getchar(); int x = 0, f = 1; for (; !isdigit(c); c = getchar()) if (c == - ) f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return x * f; } struct edgexx { int v, nxt, w; } edge[B]; struct edgex { int v, nxt; } edge_[B]; struct node { long long d; int id; bool operator<(const node &a) const { return d > a.d; } }; struct xxs { int u, v; long long w; bool operator<(const xxs &e) const { return w < e.w; } } e[B]; int n, cnt, js, k, c, t, head[B], m, q, head2[B], fa[B], u_fa[B]; long long dis[B], wnd[B]; int sz[B], son[B], top[B], dep[B]; priority_queue<node> pq; void add1(int u, int v, int w) { edge[++cnt].nxt = head[u]; edge[cnt].v = v; edge[cnt].w = w; head[u] = cnt; } void add2(int u, int v) { edge_[++js].nxt = head2[u]; edge_[js].v = v; head2[u] = js; } int find(int x) { if (x == u_fa[x]) return x; return u_fa[x] = find(u_fa[x]); } void dfs1(int u, int f) { sz[u] = 1; dep[u] = dep[fa[u] = f] + 1; for (int i = head2[u]; i; i = edge_[i].nxt) { int v = edge_[i].v; if (v == f) continue; dfs1(v, u); sz[u] += sz[v]; if (sz[v] > sz[son[u]]) son[u] = v; } } void dfs2(int u, int topf) { top[u] = topf; if (son[u]) dfs2(son[u], topf); for (int i = head2[u]; i; i = edge_[i].nxt) { int v = edge_[i].v; if (v == fa[u] || v == son[u]) continue; dfs2(v, v); } } int lca(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); u = fa[top[u]]; } return dep[u] < dep[v] ? u : v; } int main() { cin >> n >> m >> k >> q; for (int i = 1; i <= m; i++) { int x = read(), y = read(), z = read(); add1(x, y, z); add1(y, x, z); e[i] = (xxs){x, y, z}; } memset(dis, 0x3f, sizeof(dis)); for (int i = 1; i <= k; i++) pq.push((node){dis[i] = 0, i}); while (!pq.empty()) { long long d = pq.top().d; int u = pq.top().id; pq.pop(); if (d != dis[u]) continue; for (int i = head[u]; i; i = edge[i].nxt) { int v = edge[i].v; if (dis[v] > d + edge[i].w) pq.push((node){dis[v] = d + edge[i].w, v}); } } for (int i = 1; i <= m; i++) e[i].w += dis[e[i].u] + dis[e[i].v]; sort(e + 1, e + 1 + m); for (int i = 1; i <= 2 * n; i++) u_fa[i] = i; cnt = n; for (int i = 1; i <= m; i++) { int u = e[i].u, v = e[i].v; long long w = e[i].w; u = find(u); v = find(v); if (u == v) continue; u_fa[u] = u_fa[v] = ++cnt; wnd[cnt] = w; add2(cnt, u); add2(cnt, v); } dfs1(cnt, 0); dfs2(cnt, cnt); while (q--) { int u = read(), v = read(); printf( %lld n , wnd[lca(u, v)]); } return 0; } |
#include <bits/stdc++.h> using namespace std; map<int, int> mp; vector<int> object, ans; map<int, int> counter; int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } int main() { int n, num; scanf( %d , &n); for (int i = 0; i < n * n; i++) { scanf( %d , &num); object.push_back(num); } sort(object.begin(), object.end()); for (int i = object.size() - 1; i >= 0; i--) { if (counter[object[i]]) { counter[object[i]]--; continue; } for (int j = 0; j < ans.size(); j++) { int temp = gcd(ans[j], object[i]); counter[temp] += 2; } ans.push_back(object[i]); } for (int i = 0; i < n; i++) { if (i) printf( ); printf( %d , ans[i]); } printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; vector<long long> g[100005]; long long vis[MAXN], s[MAXN], gc[MAXN]; long long n; void dfs(long long v) { vis[v] = 1; s[v] = 1; for (auto i : g[v]) { if (!vis[i]) { dfs(i); s[v] += s[i]; gc[v] = max(gc[v], s[i]); } } gc[v] = max(gc[v], n - s[v]); } int pa[MAXN], sz[MAXN]; long long find_pa(long long pos) { if (pa[pos] != pos) pa[pos] = find_pa(pa[pos]); return pa[pos]; } void join(int a, int b) { int x = find_pa(a), y = find_pa(b); if (x != y) { if (sz[x] >= sz[y]) sz[x] += sz[y], pa[y] = x; else sz[y] += sz[x], pa[x] = y; } } void dsu_init(long long n) { for (long long i = 0, ggdem = n; i < ggdem; ++i) pa[i] = i, sz[i] = 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n; for (long long i = 0, ggdem = n - 1; i < ggdem; ++i) { long long u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0); long long mini = n; for (long long i = 0, ggdem = n; i < ggdem; ++i) { mini = min(mini, gc[i]); } vector<long long> v; for (long long i = 0, ggdem = n; i < ggdem; ++i) { if (gc[i] == mini) v.push_back(i); } assert(((int)v.size()) <= 2); sort(v.begin(), v.end()); if (((int)v.size()) == 1) { for (long long k = 0, ggdem = 2; k < ggdem; ++k) cout << 1 << << g[0][0] + 1 << n ; } else { assert(mini == n / 2 && n % 2 == 0); dsu_init(n); for (long long i = 0, ggdem = n; i < ggdem; ++i) { for (auto j : g[i]) { if (min(i, j) != v[0] || max(i, j) != v[1]) { join(i, j); } } } long long br = 0; for (long long i = 0, ggdem = n; i < ggdem; ++i) { if (((int)g[i].size()) == 1 && i != v[0] && find_pa(i) != find_pa(v[1])) { cout << i + 1 << << g[i][0] + 1 << n ; cout << i + 1 << << v[1] + 1 << n ; br++; break; } } assert(br); } for (long long i = 0, ggdem = n; i < ggdem; ++i) g[i].clear(); for (long long i = 0, ggdem = n; i < ggdem; ++i) vis[i] = 0, s[i] = 0, gc[i] = 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[1000005]; queue<long long int> q; vector<long long int> v; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int i, j, k, n, m, mid; cin >> k; string s; cin >> s; n = s.size(); int co = 0; long long int sum = 0; if (k == 0) { for (i = 0; i < n; i++) { if (s[i] == 0 ) { co++; } else co = 0; sum += (co); } cout << sum; return 0; } for (i = 0; i < n; i++) { if (s[i] == 1 ) { a[i] = co; co = 0; v.push_back(i); } else co++; } long long int first = 0; long long int l = v.size(); for (i = k - 1; i < l; i++) { long long int temp1 = a[v[first]] + 1; long long int temp2; if (i + 1 < l) temp2 = v[i + 1] - v[i]; else temp2 = n - v[i]; sum += temp1 * temp2; first++; } cout << sum; return 0; } |
#include <bits/stdc++.h> using namespace std; inline int readint() { int x = 0; bool f = 0; char c = getchar(); while (!isdigit(c) && c != - ) c = getchar(); if (c == - ) { f = 1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - 0 ; c = getchar(); } return f ? -x : x; } const int maxn = 4e5 + 5; int n, m, c[maxn]; vector<int> g[maxn]; int fa[maxn]; void dfs(int u) { for (int i = 0; i < (int)g[u].size(); i++) { int v = g[u][i]; if (v == fa[u]) continue; fa[v] = u; dfs(v); } } struct lct { int fa[maxn], ch[maxn][2], s[maxn], s2[maxn]; long long ss[maxn]; void init() { for (int i = 1; i <= n; i++) s[i] = 1; } void pushup(int x) { s[x] = s2[x] + 1; if (ch[x][0]) s[x] += s[ch[x][0]]; if (ch[x][1]) s[x] += s[ch[x][1]]; } bool isroot(int x) { return !fa[x] || (ch[fa[x]][0] != x && ch[fa[x]][1] != x); } void rotate(int x) { int y = fa[x], z = fa[fa[x]]; if (!isroot(y)) ch[z][ch[z][1] == y] = x; bool d = ch[y][1] == x; int k = ch[x][!d]; if (k) fa[k] = y; ch[y][d] = k; ch[x][!d] = y; fa[y] = x; fa[x] = z; pushup(y); pushup(x); } void splay(int x) { while (!isroot(x)) { int y = fa[x], z = fa[fa[x]]; if (!isroot(y)) { if ((ch[y][1] == x) ^ (ch[z][1] == y)) rotate(x); else rotate(y); } rotate(x); } } void access(int x) { int y = 0; while (x) { splay(x); if (ch[x][1]) { s2[x] += s[ch[x][1]]; ss[x] += 1ll * s[ch[x][1]] * s[ch[x][1]]; } ch[x][1] = y; if (y) { s2[x] -= s[y]; ss[x] -= 1ll * s[y] * s[y]; } y = x; x = fa[x]; } } int findroot(int x) { access(x); splay(x); while (ch[x][0]) x = ch[x][0]; splay(x); return x; } void link(int x, int y) { access(y); splay(y); splay(x); fa[x] = y; s2[y] += s[x]; ss[y] += 1ll * s[x] * s[x]; pushup(y); } void cut(int x, int y) { access(y); splay(y); splay(x); fa[x] = 0; s2[y] -= s[x]; ss[y] -= 1ll * s[x] * s[x]; pushup(y); } } t; vector<pair<int, int> > q[maxn]; bool c2[maxn]; long long query(int u) { int x = t.findroot(u); if (c2[x]) return 1ll * t.s[t.ch[x][1]] * t.s[t.ch[x][1]]; else return 1ll * t.s[x] * t.s[x]; } long long modify(int u) { c2[u] ^= 1; if (c2[u]) { if (u == 1) { t.access(1); return t.ss[1] - 1ll * t.s[1] * t.s[1]; } else { long long res = -query(u); t.cut(u, fa[u]); t.access(u); res += t.ss[u]; if (!c2[fa[u]]) res += query(fa[u]); return res; } } else { if (u == 1) { t.access(1); return 1ll * t.s[1] * t.s[1] - t.ss[1]; } else { t.access(u); long long res = -t.ss[u]; if (!c2[fa[u]]) res -= query(fa[u]); t.link(u, fa[u]); res += query(u); return res; } } } long long ans[maxn]; void solve(int c) { for (int i = 0; i < (int)q[c].size(); i++) ans[q[c][i].first] += modify(q[c][i].second); for (int i = (int)q[c].size() - 1; i >= 0; i--) modify(q[c][i].second); } int main() { n = readint(); m = readint(); for (int i = 1; i <= n; i++) c[i] = readint(); for (int i = 1; i < n; i++) { int u, v; u = readint(); v = readint(); g[u].push_back(v); g[v].push_back(u); } dfs(1); t.init(); for (int i = 2; i <= n; i++) t.link(i, fa[i]); for (int i = 1; i <= n; i++) q[c[i]].push_back(pair<int, int>(0, i)); for (int i = 1; i <= m; i++) { int u, x; u = readint(); x = readint(); q[c[u]].push_back(pair<int, int>(i, u)); q[c[u] = x].push_back(pair<int, int>(i, u)); } for (int i = 1; i <= n; i++) solve(i); for (int i = 1; i <= m; i++) ans[i] += ans[i - 1]; for (int i = 0; i <= m; i++) printf( %lld n , -ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, m; long long a[100000], b[100000]; long long val(long long p) { long long ret = 0; for (long long i = 0; i < n; ++i) ret += max((long long)0, p - a[i]); for (long long i = m - 1; i >= 0; --i) ret += max((long long)0, b[i] - p); return ret; } int32_t main() { cin >> n >> m; for (long long i = 0; i < n; ++i) cin >> a[i]; for (long long i = 0; i < m; ++i) cin >> b[i]; sort(a, a + n), sort(b, b + m); long long l = 0, r = 1e9 + 1, m1, m2, v1, v2; long long ans = LLONG_MAX; while (l <= r) { m1 = l + (r - l) / 3; m2 = l + 2 * (r - l) / 3; v1 = val(m1), v2 = val(m2); if (v1 <= v2) { ans = min(v1, ans); r = m2 - 1; } else { ans = min(v2, ans); l = m1 + 1; } } cout << ans; } |
#include <cstdlib> #include <cstdarg> #include <cassert> #include <cctype> // tolower #include <ctime> #include <cmath> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <stdexcept> #include <map> #include <tuple> #include <unordered_map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <string> #include <limits> #include <utility> #include <memory> #include <numeric> #include <iterator> #include <algorithm> #include <functional> /* * g++ -g -std=c++11 -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp} * * TODO: * C++ dataframe * stl11 -> c++11 standard template lib in c++98 * overload >> for map and set, using (insert) iterator * chmod:: consider an algorithm stable to int64 overflow * shortest path algorithm * shortest path in a tree * maximum network flow * partial idx/iter sort * a prime number generator which traverses prime numbers w/ ++ * a divisor generator which traverses divisors efficiently * Apply1st ?! * Apply2nd and bind2nd ?! * count_if ( a.begin(), a.end(), a.second < x ) * Arbitrary-precision arithmetic / Big Integer / Fraction - rational num * tuple class --> cplusplus.com/reference/tuple * get<1>, get<2>, bind2nd ( foo ( get<2> pair ), val ) * isin( const T & val, first, last ) * fuinction composition in c++ * blogea.bureau14.fr/index.php/2012/11/function-composition-in-c11/ * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition/ * TimeWrapper -- accumulate time of a function call * stackoverflow.com/questions/879408 * hash map -- possible hash value & obj % some_big_prime [ b272 ] * lower level can be a simple map to resolve hash collisions * add explicit everywhere necessary * bloom filters * heap -> how to update values in place / increase-key or decrease-key ... IterHeap ?! * median maintaince --> max_heap / min_heap * prim s min spaning tree alg. O ( m log n ) using heap contianing V - X vertices * kruskal algorithm minimum cost spanning tree with union find data structure * unique_ptr * hufman codes * simple arithmatic tests * longest common subsequence using seq. alignment type algorithm * longest common substring ( consequative subsequeance ) * Graham scan; en.wikipedia.org/wiki/Graham_scan * problem/120/F -- how to extend in order to calculate all-pair distances in a tree * compile time prime number calculator using templates * swith to type_cast < T1, T2 > for val2str & str2val * overload plus<> for two vectors?! * overloading operator << for tuple: * stackoverflow.com/questions/9247723 * bitmask class with iterator protocol * ??? segment tree +++ interval tree * ??? an lru cache: a heap which maintains last access, an a map which maintains key:value * ??? in lazy-union how to query size of a connected component( 437/D ) */ /* * @recepies * ---------------------------------------------- * odd / even * transform ( x.begin(), x.end(), x.begin(), bind2nd( modulus<int>(), 2 )); * count_if ( x.begin(), x.end(), bind2nd( modulus < int > (), 2)); * Apply2nd * max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) * sort ( a.begin(), a.end(), Apply2nd < char, int , greater< int > > ) * count_if ( m.begin(), m.end(), Apply2nd < string, int, modulus < int > > ) * accumulate ( m.begin(), m.end(), 0.0, Apply2nd < int, double, plus < double > > ) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * abs_diff * adjacent_difference ( a.begin(), a.end(), adj_diff.begin( ), abs_diff < int > ( ) ) * accumulate ( a.begin(), a.end(), 0, abs_diff < int > ( ) ) * erase * a.erase ( remove_if ( a.begin( ), a.end( ), bind2nd ( less < int >( ), 0 ) ), a.end( ) ) * a.erase ( remove ( a.begin( ), a.end( ), b.begin( ), b.end( ) ), a.end ( ) ) * binding * bind2nd ( mem_fun_ref (& ClassName::m_func ), arg ) // binds the argument of the object * iterator generators * generate_n ( back_inserter ( a ), n, rand ); // calls push_back * generate_n ( inserter( a, a.begin( ) + 5 ), 10, RandInt( 0 , 100 ) ) // calls insert * copy ( foo.begin( ), foo.end( ), insert_iterator < list < double > > ( bar, bar.begin( ) + 5 )) * copy ( a.begin( ), a.end( ), ostream_iterator < double > ( cout, , )) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length)); */ /* * @good read * ---------------------------------------------- * [ partial ] template specialization * cprogramming.com/tutorial/template_specialization.html * function composition * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition */ /* * @prob set * ---------------------------------------------- * purification --> c330 */ /* * @limits * ---------------------------------------------- * int 31 2.14e+09 * long int 31 2.14e+09 * unsigned 32 4.29e+09 * long unsigned 32 4.29e+09 * size_t 32 4.29e+09 * long long int 63 9.22e+18 * long long unsigned 64 1.84e+19 */ /* * issues * ---------------------------------------------- * stackoverflow.com/questions/10281809 * mem_fun -> func_obj ( pointer to instance, origanal argument ) * bind1st ( mem_fun ( & ClassName::m_func ), this ) // binds obj of the class * bind1st takes const T & as the first argument */ /* * typedef / define * ---------------------------------------------- */ typedef long long int int64; typedef unsigned long long int uint64; #ifndef M_PI #define M_PI 3.14159265358979323846L #endif #define DOUBLE_INF numeric_limits< double >::infinity() #define DOUBLE_NAN numeric_limits< double >::quiet_NaN() #define __STR__(a) #a #define STR(a) __STR__(a) #define ASSERT(expr, msg) if (!( expr )) throw runtime_error(__FILE__ : STR(__LINE__) - msg); #define DECLARE( X ) typedef shared_ptr < X > X ## _shared_ptr; typedef const shared_ptr < X > X ## _const_shared_ptr; #ifdef BUG #define DEBUG(var) { cout << #var << : << (var) << endl; } #define COND_DEBUG(expr, var) if (expr) { cout << #var << : << (var) << endl; } #define EXPECT(expr) if ( ! (expr) ) cerr << Assertion << #expr failed at << __FILE__ << : << __LINE__ << endl; #else #define DEBUG(var) #define COND_DEBUG(expr, var) #define EXPECT(expr) #endif #define DBG(v) copy( v.begin(), v.end(), ostream_iterator < typeof( *v.begin() )> ( cout, ) ) #if __cplusplus < 201100 #define move( var ) var #endif /* * http://rootdirectory.de/wiki/SSTR() * usage: * SSTR( x^2: << x*x ) */ #define SSTR( val ) dynamic_cast< ostringstream & >( ostringstream() << dec << val ).str() using namespace std; /* https://www.quora.com/C++-programming-language/What-are-some-cool-C++-tricks */ // template <typename T, size_t N> // char (&ArraySizeHelper(T (&array)[N]))[N]; // #define arraysize(array) (sizeof(ArraySizeHelper(array))) /* * forward decleration * ---------------------------------------------- */ class ScopeTimer; /* * functional utils * ---------------------------------------------- */ template < typename T > struct abs_diff : binary_function < T, T, T > { typedef T value_type; inline value_type operator( ) ( const value_type & x, const value_type & y ) const { return abs( x - y ); } }; // template < class InputIterator, class T > // class isin : public binary_function < InputIterator, InputIterator, bool > // { // public: // typedef T value_type; // // isin ( const InputIterator & first, const InputIterator & last ): // m_first ( first ), m_last ( last ) { } // // bool operator ( ) ( const value_type & val ) const // { // return find ( m_first, m_last, val ) != m_last; // } // private: // const InputIterator m_first, m_last; // } template < typename value_type, typename cont_type > class isin : public unary_function < value_type, bool > { public: isin( const cont_type & vals ): m_vals ( vals ) { }; bool operator ( ) ( const value_type & x ) const { return find ( m_vals.begin( ), m_vals.end( ), x ) != m_vals.end( ); } private: const cont_type m_vals; }; /* * max_element, min_element, count_if ... on the 2nd element * eg: max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) */ template < class T1, class T2, class BinaryOperation > class Apply2nd : binary_function < typename std::pair < T1, T2 >, typename std::pair < T1, T2 >, typename BinaryOperation::result_type > { public: typedef T1 first_type; typedef T2 second_type; typedef typename BinaryOperation::result_type result_type; typedef typename std::pair < first_type, second_type > value_type; inline result_type operator( ) ( const value_type & x, const value_type & y ) const { return binary_op ( x.second , y.second ); } private: BinaryOperation binary_op; }; /* * algo utils * ---------------------------------------------- */ /** * count the number of inversions in a permutation; i.e. how many * times two adjacent elements need to be swaped to sort the list; * 3 5 2 4 1 --> 7 */ template < class InputIterator > typename iterator_traits<InputIterator>::difference_type count_inv ( InputIterator first, InputIterator last ) { typedef typename iterator_traits<InputIterator>::difference_type difference_type; typedef typename iterator_traits<InputIterator>::value_type value_type; list < value_type > l; /* list of sorted values */ difference_type cnt = 0; for ( difference_type n = 0; first != last; ++first, ++n ) { /* count how many elements larger than *first appear before *first */ typename list < value_type >::iterator iter = l.begin( ); cnt += n; for ( ; iter != l.end( ) && * iter <= * first; ++ iter, -- cnt ) ; l.insert( iter, * first ); } return cnt; } template < class ForwardIterator, class T > inline void fill_inc_seq ( ForwardIterator first, ForwardIterator last, T val ) { for ( ; first != last; ++first, ++val ) * first = val; } template <class ForwardIterator, class InputIterator > ForwardIterator remove ( ForwardIterator first, ForwardIterator last, InputIterator begin, InputIterator end ) { ForwardIterator result = first; for ( ; first != last; ++ first ) if ( find ( begin, end, *first ) == end ) { *result = *first; ++result; } return result; } /* stackoverflow.com/questions/1577475 */ template < class RAIter, class Compare > class ArgSortComp { public: ArgSortComp ( const RAIter & first, Compare comp ): m_first ( first ), m_comp( comp ) { } inline bool operator() ( const size_t & i, const size_t & j ) const { return m_comp ( m_first[ i ] , m_first[ j ] ); } private: const RAIter & m_first; const Compare m_comp; }; /*! * usage: * vector < size_t > idx; * argsort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template < class RAIter, class Compare=less< typename RAIter::value_type > > void argsort( const RAIter & first, const RAIter & last, vector < size_t > & idx, Compare comp = Compare()) // less< typename RAIter::value_type >() ) { const size_t n = last - first; idx.resize ( n ); for ( size_t j = 0; j < n; ++ j ) idx[ j ] = j ; stable_sort( idx.begin(), idx.end(), ArgSortComp< RAIter, Compare >( first, comp )); } template < class RAIter, class Compare > class IterSortComp { public: IterSortComp ( Compare comp ): m_comp ( comp ) { } inline bool operator( ) ( const RAIter & i, const RAIter & j ) const { return m_comp ( * i, * j ); } private: const Compare m_comp; }; /*! * usage: * vector < list < Type >::const_iterator > idx; * itersort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template <class INIter, class RAIter, class Compare> void itersort ( INIter first, INIter last, vector < RAIter > & idx, Compare comp ) { /* alternatively: stackoverflow.com/questions/4307271 */ idx.resize ( distance ( first, last ) ); for ( typename vector < RAIter >::iterator j = idx.begin( ); first != last; ++ j, ++ first ) * j = first; sort ( idx.begin( ), idx.end( ), IterSortComp< RAIter, Compare > (comp ) ); } /* * string utils * ---------------------------------------------- */ inline void erase ( string & str, const char & ch ) { binder2nd < equal_to < char > > isch ( equal_to< char >(), ch ); string::iterator iter = remove_if ( str.begin(), str.end(), isch ); str.erase ( iter, str.end() ); } inline void erase ( string & str, const string & chrs ) { isin < char, string > isin_chrs ( chrs ); string::iterator iter = remove_if ( str.begin(), str.end(), isin_chrs ); str.erase ( iter, str.end() ); } template < typename value_type> inline string val2str ( const value_type & x ) { ostringstream sout ( ios_base::out ); sout << x; return sout.str(); } template < typename value_type> inline value_type str2val ( const string & str ) { istringstream iss ( str, ios_base::in ); value_type val; iss >> val; return val; } vector< string > tokenize( const string & str, const char & sep ) { /*! * outputs empty tokens and assumes str does not start with sep * corner cases: * empty string, one char string, * string starting/ending with sep, all sep, end with two sep */ vector < string > res; string::const_iterator follow = str.begin(), lead = str.begin(); while ( true ) { while ( lead != str.end() && * lead != sep ) ++ lead; res.push_back ( string( follow, lead ) ); if ( lead != str.end () ) follow = 1 + lead ++ ; else break; } return res; } /*! * chunk a string into strings of size [ at most ] k */ void chunk ( const string::const_iterator first, const string::const_iterator last, const size_t k, const bool right_to_left, list < string > & str_list ) { str_list.clear( ); if ( right_to_left ) /* chunk from the end of the string */ for ( string::const_iterator i, j = last; j != first; j = i ) { i = first + k < j ? j - k : first; str_list.push_back ( string ( i, j ) ); } else /* chunk from the begining of the string */ for ( string::const_iterator i = first, j; i != last; i = j ) { j = i + k < last ? i + k : last; str_list.push_back ( string ( i, j ) ); } } /*! * next lexicographically smallest string * within char set a..z */ string & operator++( string & s ) { /* find the first char from right less than z */ string::reverse_iterator j = find_if( s.rbegin( ), s.rend( ), bind2nd( less < char > ( ), z )); if ( j != s.rend( )) { ++ *j; fill( s.rbegin( ), j, a ); } else s.assign( s.length( ) + 1, a ); return s; } /*! if a starts with b */ inline bool starts_with( const string & a, const string & b ) { return !( a.size() < b.size() || mismatch( begin(b), end(b), begin(a) ).first != end(b) ); } /*! prefix tree */ auto get_prefix_tree( const vector< string > & word ) -> vector< map< char, size_t > > { typedef map< char, size_t > node; /* empty string as the first element */ vector< node > tree(1); for( const auto & w: word ) { /* start with empty string as the root node */ size_t root = 0; for( const auto & a : w ) { auto & ref = tree[ root ][ a ]; if ( ref == 0 ) /* new prefix, add to the end of tree */ { ref = tree.size(); tree.push_back( node() ); } root = ref; } } return tree; } /*! * getline ( cin, str ) * requires ctrl-D * cin >> str; does not pass after space char */ /* * number utils * ---------------------------------------------- */ class BigInteger { #if ULONG_MAX <= 1 << 32 typedef long long unsigned val_type; #else typedef long unsigned val_type; #endif const static int WSIZE = 32; const static val_type BASE = 1LL << WSIZE; public: private: list < val_type > val; /* val[ 0 ] is most significant */ bool pos; /* true if sign is positive */ }; /** * greatest common divisor - Euclid s alg. */ template < typename value_type > inline value_type gcd ( value_type a, value_type b ) { return ! b ? a : gcd( b, a % b ); } /** * prime factorization */ template < class T > void prime_factors( T n, map < T, size_t > & fac ) { for ( T k = 2; n > 1; ++ k ) if ( ! ( n % k ) ) { size_t & ref = fac[ k ]; while ( ! ( n % k ) ) { ++ ref; n /= k; } } } /* abs diff - safe for unsigned types */ template < class T > inline T absdiff( T a, T b ) { return a < b ? b - a : a - b; } namespace { template < class T > pair < T, T > __extgcd ( const T & x0, const T & y0, const T & x1, const T & y1, const T & r0, const T & r1 ) { const T q = r0 / r1; const T r2 = r0 % r1; if ( ! ( r1 % r2 ) ) return make_pair < T, T > ( x0 - q * x1, y0 - q * y1 ); const T x2 = x0 - q * x1; const T y2 = y0 - q * y1; return __extgcd ( x1, y1, x2, y2, r1, r2 ); } } /** * extended euclidean algorithm: a x + b y = gcd( a, b) * en.wikipedia.org/wiki/Extended_Euclidean_algorithm */ template < class value_type > inline pair < value_type, value_type > extgcd ( value_type a, value_type b ) { return a % b ? __extgcd < value_type > ( 1, 0, 0, 1, a, b ) : make_pair < value_type, value_type > ( 0, 1 ); } /** * modular multiplicative inverse * en.wikipedia.org/wiki/Modular_multiplicative_inverse */ template < class value_type > inline value_type modinv ( value_type a, value_type m ) { const pair < value_type, value_type > coef ( extgcd( a, m ) ); /* a needs to be coprime to the modulus, or the inverse won t exist */ if ( a * coef.first + m * coef.second != 1 ) throw runtime_error ( val2str( a ) + is not coprime to + val2str( m )); /* return a pos num between 1 & m-1 */ return ( m + coef.first % m ) % m; } inline bool isnan ( const double & a ) { return ! ( a == a ); } template < typename value_type > inline value_type mini ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = min ( res, val ); } va_end( vl ); return res; } template < typename value_type > inline value_type maxi ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = max ( res, val ); } va_end( vl ); return res; } // XXX look this up how is this implemented template < class T > inline int sign ( const T & x ) { if ( x == T() ) return 0; else if ( x < T() ) return -1; else return 1; } /* * change moduluos from n to m */ string chmod ( string num, const unsigned n, const unsigned m ) { const char * digit = 0123456789abcdefghijklmnopqrstuvwxyz ; transform ( num.begin(), num.end(), num.begin(), ::tolower ); isin < char, string > is_alpha_num ( digit ); assert ( find_if ( num.begin( ), num.end( ), not1 ( is_alpha_num ) ) == num.end( )); unsigned long long int val ( 0 ); if ( n == 10U ) { istringstream iss ( num, ios_base::in ); iss >> val; } else for ( string::const_iterator iter = num.begin( ); iter != num.end( ); ++ iter ) val = val * n + ( a <= *iter ? *iter - a + 10U : *iter - 0 ); if ( m == 10U ) { ostringstream sout ( ios_base::out ); sout << val; return sout.str ( ); } else { string res; for ( ; val ; val /= m ) res.push_back( digit [ val % m ] ); return res.length( ) ? string( res.rbegin( ), res.rend( )) : 0 ; } } template < class value_type > /* a^n mod m */ value_type powmod ( value_type a, const value_type & n, const value_type & m ) { if ( a == 1 || ! n ) return m != 1 ? 1 : 0; value_type res = 1; for ( value_type k = 1; k <= n; a = a * a % m, k = k << 1 ) if ( k & n ) res = ( res * a ) % m; return res; } template < class T > inline T atan(const T & x, const T & y) { return atan( y / x ) + ( x < 0 ) * M_PI; } /* * Fermat pseudoprime test * www.math.umbc.edu/~campbell/Computers/Python/numbthy.py * NOTE: since return type is bool, and powmod may break for ints, * the argument is always casted to long long */ inline bool is_pseudo_prime ( const long long & a ) { /* all the primes less than 1000 ( 168 primes )*/ const long long p [ ] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73, 79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157, 163,167,173,179,181,191,193,197,199,211,223,227,229,233,239, 241,251,257,263,269,271,277,281,283,293,307,311,313,317,331, 337,347,349,353,359,367,373,379,383,389,397,401,409,419,421, 431,433,439,443,449,457,461,463,467,479,487,491,499,503,509, 521,523,541,547,557,563,569,571,577,587,593,599,601,607,613, 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709, 719,727,733,739,743,751,757,761,769,773,787,797,809,811,821, 823,827,829,839,853,857,859,863,877,881,883,887,907,911,919, 929,937,941,947,953,967,971,977,983,991,997 }; const size_t n = sizeof( p ) / sizeof ( p[ 0 ] ); if ( a < p[ n - 1 ] + 1) return binary_search ( p, p + n , a ); if ( find_if ( p, p + n, not1 ( bind1st ( modulus< long long >( ), a ))) != p + n ) return false; const size_t k = a < 9006401LL ? 3 : a < 10403641LL ? 4 : a < 42702661LL ? 5 : a < 1112103541LL ? 6 : 7; for ( size_t j = 0; j < k; ++ j ) if ( powmod ( p[ j ], a - 1, a ) != 1 ) return false; return true; } /* * returns a sorted vector of all primes less than or equal to n * maximum adj diff of all primes less than 1e5 is 72 ( 114 for 1e6 ) */ template < typename value_type > vector < value_type > get_primes ( const value_type n ) { #ifdef BUG ScopeTimer scope_timer ( vector < value_type > get_primes ( const value_type n ) ); #endif // typedef typename vector < value_type >::iterator iterator; vector < value_type > primes; for ( value_type k = 2 ; k <= n; ++ k ) if ( is_pseudo_prime ( k ) ) { // FIXME this is very stupid // const value_type sqrt_k = 1 + static_cast < value_type > ( sqrt ( k + 1 ) ); // iterator iend = upper_bound ( primes.begin( ), primes.end( ), sqrt_k ); // if ( find_if ( primes.begin( ), iend, not1 ( bind1st ( modulus< value_type >( ), k ) ) ) != iend ) // continue; primes.push_back ( k ); } return primes; } template < class T > inline vector < pair < T, size_t > > get_prime_fact( T a ) { vector< pair < T, size_t > >fac; for ( T k = 2; a > 1; ++ k ) if ( ! ( a % k ) ) // no need to check if k is prime { size_t m = 0; for ( ; ! ( a % k ) ; ++m, a/= k ) ; fac.push_back ( pair < T, size_t > ( k, m ) ); } return fac; } template < class T > /* prime factorization */ vector < pair < T, size_t > > get_prime_fac( T a ) { vector < pair < T, size_t > > fac; size_t k = 0; while (!(a % 2)) { a /= 2; ++ k; } if ( k != 0 ) fac.push_back( make_pair(2, k)); k = 0; while (!(a % 3)) { a /= 3; ++ k; } if ( k != 0 ) fac.push_back( make_pair(3, k)); for ( T j = 5; j * j < a + 1 && a > 1 && j < a + 1; j += 4 ) { size_t k = 0; while (!(a % j)) { a /= j; ++ k; } if ( k != 0 ) fac.push_back( make_pair(j, k)); j += 2; k = 0; while (!(a % j)) { a /= j; ++ k; } if ( k != 0 ) fac.push_back( make_pair(j, k)); } if ( a > 1 ) fac.push_back( make_pair(a, 1)); return fac; } template < class T > /* generates all divisors from vector of prime factorization */ void gen_div( T a, vector < T > & divs, typename vector < pair < T, size_t > >::const_iterator iter, typename vector < pair < T, size_t > >::const_iterator last ) { if ( iter != last ) { auto next = iter + 1; const auto k = iter->second + 1; const auto prime = iter->first; for ( size_t j = 0; j < k; ++ j ) { gen_div(a, divs, next, last); a *= prime; } } else divs.push_back( a ); } template < class T > T n_choose_k ( T n, T k ) { if ( k > n ) return 0; const T lb = min ( k, n - k ) + 1; const T ub = n - lb + 1; T res = 1, j = 2; while ( n > ub && j < lb) { res *= n--; while ( j < lb and ! (res % j) ) res /= j++; } while ( n > ub ) res *= n--; return res; } /** * median calculator, using two heaps */ template < class InputIter > inline pair < typename InputIter::value_type, typename InputIter::value_type > median ( InputIter first, InputIter last ) { typedef typename InputIter::value_type value_type; typedef pair< value_type, value_type > result_type; /* * max_heap: * - the lower half of the elements * - the biggest of such elements is on the top */ vector < value_type > max_heap, min_heap; /* * comp argument to heap algorithm should provide * strict weak ordering ; in particular * not2 ( less < value_type > ) * does not have such a strict weak ordering; */ less < value_type > max_heap_comp; greater < value_type > min_heap_comp; if ( first == last ) /* corner case: empty vector */ throw runtime_error ( median of an empty vector is undefined! ); InputIter iter = first; max_heap.push_back ( * iter ); for ( ++iter ; iter != last; ++ iter ) if ( * iter < max_heap.front() ) { max_heap.push_back ( * iter ); push_heap ( max_heap.begin(), max_heap.end(), max_heap_comp ); if ( min_heap.size() + 1 < max_heap.size() ) { /* max_heap has got too large */ min_heap.push_back( max_heap.front() ); push_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); pop_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); max_heap.pop_back(); } } else { min_heap.push_back ( * iter ); push_heap ( min_heap.begin(), min_heap.end(), min_heap_comp ); if ( max_heap.size() + 1 < min_heap.size() ) { /* min_heap has got too large */ max_heap.push_back( min_heap.front() ); push_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); pop_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); min_heap.pop_back(); } } DEBUG( max_heap ); DEBUG( min_heap ); return min_heap.empty( ) /* corner case: ++first = last */ ? result_type ( *first, *first ) : result_type ( max_heap.size() < min_heap.size() ? min_heap.front() : max_heap.front(), min_heap.size() < max_heap.size() ? max_heap.front() : min_heap.front() ); } /* * geometry util * ---------------------------------------------- */ struct xyPoint { double x, y; xyPoint( const double & a = .0, const double & b = .0 ): x ( a ), y( b ) { }; }; struct xyCircle { xyPoint center; double radius; }; ostream & operator<< ( ostream & out, const xyPoint & p ) { out << ( << p.x << , << p.y << ) ; return out; } istream & operator>> ( istream & ist, xyPoint & p ) { ist >> p.x >> p.y; return ist; } ostream & operator<< ( ostream & out, const xyCircle & o ) { out << {( << o.center.x << , << o.center.y << ) << o.radius << } ; return out; } istream & operator>> ( istream & ist, xyCircle & o ) { ist >> o.center.x >> o.center.y >> o.radius; return ist; } inline double cartesian_dist ( const xyPoint & a, const xyPoint & b ) { const double d = a.x - b.x; const double e = a.y - b.y; return sqrt ( d * d + e * e ); } class xyLine { public: xyLine ( const xyPoint & , const xyPoint & ); xyLine ( const double slope, const double intercept ); /* * signed orthogonal distance; the sign is useful * to compare which side of the line the point is */ inline double orth_dist ( const xyPoint & ) const; private: double m_slope; double m_intercept; double m_normfac; /* normalization factor for orth_dist calc */ bool m_vertical; /* if the line is verticcal */ double m_xcross; /* x axis cross point for vertical line */ }; xyLine::xyLine ( const xyPoint & a, const xyPoint & b ) { if ( a.x == b.x ) /* vertical line */ { m_vertical = true; m_xcross = a.x; m_intercept = DOUBLE_NAN; m_slope = DOUBLE_INF; m_normfac = DOUBLE_NAN; } else { m_vertical = false; m_xcross = DOUBLE_NAN; m_slope = ( b.y - a.y ) / ( b.x - a.x ); m_intercept = a.y - m_slope * a.x; m_normfac = sqrt ( m_slope * m_slope + 1.0 ); } } xyLine::xyLine ( const double slope, const double intercept ): m_slope ( slope ), m_intercept ( intercept ) { m_vertical = false; m_xcross = DOUBLE_NAN; m_normfac = sqrt ( m_slope * m_slope + 1.0 ); } double xyLine::orth_dist ( const xyPoint & o ) const /* signed orthogonal distance */ { if ( m_vertical ) return o.x - m_xcross; else return ( m_slope * o.x - o.y + m_intercept ) / m_normfac; } inline double triangle_area ( const xyPoint & a, const xyPoint & b, const xyPoint & c ) { const xyLine l ( a, b ); const double h = abs ( l.orth_dist ( c ) ); const double e = cartesian_dist ( a, b ); return h * e; } /* * operator<< overrides * ---------------------------------------------- */ namespace { /* helper function to output containers */ template < typename T > ostream & __output ( ostream & out, const T & a ) { typedef typename T::const_iterator const_iterator; out << { ; // does not work for pair value type // copy ( a.begin( ), a.end( ), ostream_iterator < typename T::value_type > ( cout, , )); for ( const_iterator iter = a.begin(); iter != a.end(); ++ iter ) out << ( iter != a.begin( ) ? , : ) << *iter ; return out << } ; } } template < typename key_type, typename value_type > ostream & operator<< ( ostream & out, const pair < key_type, value_type > & p) { out << ( << p.first << , << p.second << ) ; return out; } template < typename T0, typename T1, typename T2 > ostream & operator<< ( ostream & out, const tuple < T0, T1, T2 > & t ) { out << ( << get<0>(t) << , << get<1>(t) << , << get<2>(t) << ) ; return out; } template < typename T0, typename T1 > ostream & operator<< ( ostream & out, const tuple < T0, T1 > & t ) { out << ( << get<0>(t) << , << get<1>(t) << ) ; return out; } template < typename key_type, typename value_type, typename comp > ostream & operator<< ( ostream & out, const map < key_type, value_type, comp > & m ) { return __output ( out, m ); } template < typename value_type, typename comp > ostream & operator<< ( ostream & out, const set < value_type, comp > & s ) { return __output ( out, s ); } template < typename value_type > ostream & operator<< ( ostream & out, const vector < value_type > & a ) { return __output ( out, a ); } template < typename value_type > ostream & operator<< ( ostream & out, const list < value_type > & a ) { return __output ( out, a ); } template < typename value_type > ostream & operator<< ( ostream & out, const vector < vector < value_type > > & a ) { typedef typename vector < vector < value_type > >::const_iterator const_iterator; for ( const_iterator iter = a.begin( ); iter != a.end( ); ++ iter ) out << n << *iter ; return out; } /* * operator>> overrides * ---------------------------------------------- */ template < typename key_type, typename value_type > istream & operator>> ( istream & in, pair < key_type, value_type > & p) { in >> p.first >> p.second; return in; } template < typename T0, typename T1, typename T2 > istream & operator>> ( istream & fin, tuple < T0, T1, T2 > & t ) { fin >> get<0>(t) >> get<1>(t) >> get<2>(t); return fin; } template < typename value_type > istream & operator>> ( istream & in, vector < value_type > & a ) { typedef typename vector < value_type >::iterator iterator; if ( ! a.size( ) ) { size_t n; in >> n; a.resize( n ); } for ( iterator iter = a.begin(); iter != a.end(); ++ iter ) in >> * iter; return in; } /* * readin quick utilities * ---------------------------------------------- */ // template < typename value_type > // inline void readin ( vector < value_type > & a, size_t n = 0, istream & in = cin ) // { // // if ( ! n ) cin >> n; // if ( ! n ) in >> n ; // a.resize ( n ); // // cin >> a; // in >> a; // } // XXX consider removing // template < typename key_type, typename value_type > // inline void readin (vector < pair < key_type , value_type > > & a, size_t n = 0 ) // { // if ( !n ) cin >> n; // a.resize( n ); // cin >> a; // } /* * pair utility * ---------------------------------------------- */ /* * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ); * stackoverflow.com/questions/18640152 */ template < typename T1, typename T2 > inline pair < T1, T2 > operator+ ( const pair < T1, T2 > & a, const pair < T1, T2 > & b ) { return make_pair < T1, T2 > ( a.first + b.first, a.second + b.second ); } template < typename T1, typename T2 > inline pair < T1, T2 > operator- ( const pair < T1, T2 > & a, const pair < T1, T2 > & b ) { return make_pair < T1, T2 > ( a.first - b.first, a.second - b.second ); } // template < class T1, class T2, class BinaryOperation > // class Apply2nd : binary_function < typename pair < T1, T2 >, // typename pair < T1, T2 >, // typename BinaryOperation::result_type > namespace { /*! * helper template to do the work */ template < size_t J, class T1, class T2 > struct Get; template < class T1, class T2 > struct Get < 0, T1, T2 > { typedef typename pair < T1, T2 >::first_type result_type; static result_type & elm ( pair < T1, T2 > & pr ) { return pr.first; } static const result_type & elm ( const pair < T1, T2 > & pr ) { return pr.first; } }; template < class T1, class T2 > struct Get < 1, T1, T2 > { typedef typename pair < T1, T2 >::second_type result_type; static result_type & elm ( pair < T1, T2 > & pr ) { return pr.second; } static const result_type & elm ( const pair < T1, T2 > & pr ) { return pr.second; } }; } template < size_t J, class T1, class T2 > typename Get< J, T1, T2 >::result_type & get ( pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } template < size_t J, class T1, class T2 > const typename Get< J, T1, T2 >::result_type & get ( const pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } /* * graph utils * ---------------------------------------------- */ /* maximum in sub-tree distance */ void get_indist( const size_t root, const vector < vector < size_t > > & tree, vector < tuple< size_t, size_t > > & indist ) { const auto NIL = numeric_limits< size_t >::max(); size_t a = 0, b = NIL; for ( const auto u: tree[ root ] ) { get_indist( u, tree, indist ); if ( a < 1 + get< 0 >( indist[ u ] ) ) { b = a; a = 1 + get< 0 >( indist[ u ] ); } else b = b != NIL ? max( b, 1 + get< 0 >( indist[ u ] )) : 1 + get< 0 >( indist[ u ] ); } indist[ root ] = make_tuple(a, b); } /* maximum out of subtree distance */ void get_outdist( const size_t root, const vector< vector< size_t > > & tree, const vector< tuple< size_t, size_t > > & indist, vector < size_t > & outdist ) { const auto NIL = numeric_limits< size_t >::max(); for( const auto u: tree[ root ] ) { if ( 1 + get< 0 >( indist[ u ] ) == get< 0 >( indist[ root ] ) ) outdist[ u ] = get< 1 >( indist[ root ] ) != NIL ? 1 + max( get< 1 >( indist[ root ] ), outdist[ root ] ) : 1 + outdist[ root ]; else outdist[ u ] = 1 + max( get< 0 >( indist[ root ] ), outdist[ root ] ); get_outdist( u, tree, indist, outdist ); } } /* * Dijkstra :: single-source shortest path problem for * a graph with non-negative edge path costs, producing * a shortest path tree * en.wikipedia.org/wiki/Dijkstra s_algorithm */ template < typename DistType > void Dijekstra ( const size_t & source, const vector < list < size_t > > & adj, // adjacency list const vector < vector < DistType > > & edge_len, // pair-wise distance for adjacent nodes vector < DistType > & dist, // distance from the source vector < size_t > prev ) // previous node in the shortest path tree { // TODO } // TODO http://en.wikipedia.org/wiki/Shortest_path_problem // TODO Graph class, Weighted graph, ... /* * maximum cardinality matching in a bipartite graph * G = G1 ∪ G2 ∪ {NIL} * where G1 and G2 are partition of graph and NIL is a special null vertex * https://en.wikipedia.org/wiki/Hopcroft-Karp_algorithm */ class HopcroftKarp { public: HopcroftKarp ( const vector < list < size_t > > & adj, const vector < bool > & tag ); size_t get_npair ( ) { return npair; }; map < size_t, size_t > get_map ( ); private: bool mf_breadth_first_search ( ); // breadth first search from unpaired nodes in G1 bool mf_depth_first_search ( const size_t v ); // dfs w/ toggeling augmenting paths const vector < list < size_t > > & m_adj; // adjacency list for each node const vector < bool > & m_tag; // binary tag distinguishing partitions size_t npair; const size_t NIL; // special null vertex const size_t INF; // practically infinity distance vector < size_t > m_g1; // set of nodes with tag = true vector < size_t > m_dist; // dist from unpaired vertices in G1 vector < size_t > m_pair; }; map < size_t, size_t > HopcroftKarp::get_map ( ) { map < size_t, size_t > m; for ( size_t j = 0; j < m_pair.size( ); ++ j ) if ( m_pair[ j ] != NIL && m_tag[ j ]) m[ j ] = m_pair[ j ]; return m; } HopcroftKarp::HopcroftKarp ( const vector < list < size_t > > & adj, const vector < bool > & tag ): m_adj ( adj ), m_tag ( tag ), npair ( 0 ), NIL ( adj.size( )), INF ( adj.size( ) + 1 ), m_dist ( vector < size_t > ( adj.size( ) + 1, INF)), m_pair ( vector < size_t > ( adj.size( ), NIL )) // initially everything is paired with nil { assert ( m_adj.size() == m_tag.size() ); for ( size_t j = 0; j < tag.size( ); ++ j ) if ( tag[ j ] ) m_g1.push_back ( j ); while ( mf_breadth_first_search ( ) ) for ( vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++ v ) if ( m_pair[ *v ] == NIL && mf_depth_first_search ( *v ) ) ++ npair; } bool HopcroftKarp::mf_breadth_first_search( ) { /* only nodes from g1 are queued */ queue < size_t > bfs_queue; /* initialize queue with all unpaired nodes from g1 */ for ( vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++v ) if ( m_pair[ *v ] == NIL ) { m_dist[ *v ] = 0; bfs_queue.push ( *v ); } else m_dist[ *v ] = INF; m_dist[ NIL ] = INF; /* find all the shortest augmenting paths to node nil */ while ( ! bfs_queue.empty() ) { const size_t v = bfs_queue.front( ); bfs_queue.pop ( ); if ( m_dist[ v ] < m_dist[ NIL ] ) for ( list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ * u ] ] == INF ) { m_dist[ m_pair[ * u ] ] = m_dist[ v ] + 1; bfs_queue.push ( m_pair[ * u ] ); } } return m_dist[ NIL ] != INF; } bool HopcroftKarp::mf_depth_first_search( const size_t v ) { if ( v == NIL ) return true; else { for ( list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ *u ] ] == m_dist[ v ] + 1 && mf_depth_first_search( m_pair[ *u ] )) { /* * there is an augmenting path to nil from m_pair[ *u ] * and hence there is an augmenting path from v to u and * u to to nil; therefore v and u can be paired together */ m_pair [ *u ] = v; m_pair [ v ] = *u; return true; } m_dist[ v ] = INF; return false; } } /** * lazy all pairs shortest path in a tree with only one BFS * test case: book of evil * codeforces.com/problemset/problem/337/D */ class All_Pairs_Tree_Shortest_Path { public: All_Pairs_Tree_Shortest_Path( const vector< list < size_t > > & adj ): n( adj.size( ) ), depth( vector < size_t > ( n, All_Pairs_Tree_Shortest_Path::INF ) ), parent( vector < size_t > ( n ) ), dist( vector < vector < unsigned short > > ( n ) ) { /* perform bfs from root node 0 and assign depth to each node */ /* XXX probably would be worth to set the root as node with highest degree */ queue< size_t > bfs_queue; bfs_queue.push( 0 ); depth[ 0 ] = 0; parent[ 0 ] = 0; while ( ! bfs_queue.empty( ) ) { const size_t u = bfs_queue.front( ); bfs_queue.pop( ); for ( list< size_t >::const_iterator j = adj[ u ].begin( ); j != adj[ u ].end( ); ++ j ) if ( depth[ u ] + 1 < depth[ *j ] ) { depth[ *j ] = depth[ u ] + 1; parent[ *j ] = u; bfs_queue.push( *j ); } } /* adjust pair-wise distance to zero along the diagonal */ for ( size_t j = 1; j < n; ++ j ) dist[ j ].resize( j, All_Pairs_Tree_Shortest_Path::INF ); } /* interface object function as to lazily look-up distances */ size_t operator( )( const size_t u, const size_t v ) { if ( u == v ) return 0; else if ( u < v) return (*this)( v, u ); else if ( dist[ u ][ v ] == All_Pairs_Tree_Shortest_Path::INF ) { if ( depth[ u ] < depth[ v ] ) /* u is in a lower level than v */ dist[ u ][ v ] = 1 + (*this)( u, parent[ v ]); else if ( depth[ v ] < depth[ u ] ) /* v is in a lower level than u */ dist[ u ][ v ] = 1 + (*this)( parent[ u ], v ); else /* u and v are at the same depth */ dist[ u ][ v ] = 2 + (*this)( parent[ u ], parent[ v ] ); } return dist[ u ][ v ]; } /* TODO populate; a method which populates pair-wise distances * and returns the matrix */ private: /* * constant infinity value for initializing distances * even though this is private it will be assigned outside of the class */ static const unsigned short INF; const size_t n; /* numbert of nodes in the tree */ vector < size_t > depth; /* distance to root node 0 */ vector < size_t > parent; /* parent of each node with root node 0 */ vector < vector < unsigned short > > dist; /* pair-wise shortest path distance */ }; const unsigned short All_Pairs_Tree_Shortest_Path::INF = numeric_limits< unsigned short >::max( ); /* * data-structure utility * ---------------------------------------------- */ template < class T, class Comp = less< T > > class Heap /* less< T > --> max-heap */ { typedef T value_type; typedef typename vector < value_type >::size_type size_type; public: /* * stackoverflow.com/questions/10387751 * possible work-around: a memebr pointer to m_val * TODO static/friend heapify ( val, & heap ) XXX O( n ) ?! * TODO implement insert iterator */ Heap(): m_val( vector < value_type >() ), m_comp( Comp() ) {} Heap( const Comp & comp ): m_val( vector< value_type >() ), m_comp( comp ){ } template < class InputIter > Heap ( InputIter first, InputIter last ): m_val ( vector < value_type > ( ) ), m_comp( Comp( ) ) { for ( ; first != last ; ++ first ) m_val.push_back ( * first ); make_heap( m_val.begin( ), m_val.end( ), m_comp ); } /*! * to avoid destroying heap property, front( ) * should always return a const reference */ inline const value_type & front( ) const { return m_val.front( ); } inline bool empty( ) const { return m_val.empty( ); } inline size_type size( ) const { return m_val.size( ); } inline void push( const value_type & a ) { m_val.push_back( a ); push_heap( m_val.begin( ), m_val.end( ), m_comp ); } inline void pop( ) { pop_heap ( m_val.begin( ), m_val.end( ), m_comp ); m_val.pop_back( ); } // inline void swap( Heap< T, Comp> & other ) { m_val.swap( other.m_val ) }; // void sort( ) { sort_heap ( m_val.begin( ), m_val.end( ), m_comp ); } // template < class X, class Y > // friend ostream & operator<<( ostream & out, const Heap < X, Y> & heap ); private: vector < value_type > m_val; const Comp m_comp; }; /* * boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm */ #if 1 < 0 template < class Type > class shared_ptr { typedef Type value_type; public: explicit shared_ptr ( value_type * p = NULL ) : ptr ( p ), count ( new size_t ( 1U ) ) { } shared_ptr ( const shared_ptr < value_type > & sp ): ptr ( sp.ptr ), count ( sp.count ) { ++ * count; } ~ shared_ptr ( ) { release( ); } bool operator== ( const shared_ptr < value_type > & sp ) { return ptr == sp.ptr; } bool operator!= ( const shared_ptr < value_type > & sp ) { return ptr != sp.ptr; } shared_ptr < value_type > & operator= ( const shared_ptr < value_type > & sp ) { if ( this != & sp && ptr != sp.ptr ) { release( ); ptr = sp.ptr; count = sp.count; ++ * count; } return * this; } value_type * operator-> ( ) { return ptr ; } value_type & operator* ( ) { return *ptr ; } const value_type * operator-> ( ) const { return ptr ; } const value_type & operator* ( ) const { return *ptr; } void swap ( shared_ptr < value_type > & sp ) { if ( this != &sp && ptr != sp.ptr ) { swap ( ptr, sp.ptr ); swap ( count, sp.count ); } } private: void release ( ) { /* stackoverflow.com/questions/615355 */ -- * count; if ( ! * count ) { delete count; delete ptr; count = NULL; ptr = NULL; } } value_type * ptr; size_t * count; }; #endif /*! * union find data structure with * - lazy unions * - union by rank * - path compression */ class UnionFind { public: UnionFind( const size_t n ): parent ( vector < size_t > ( n ) ), /* initialize each node as its own */ rank ( vector < size_t > ( n, 0 )) /* parent and set all the ranks to 0 */ { for ( size_t j = 0; j < n; ++ j ) parent[ j ] = j ; } inline size_t find( const size_t s ) { /* * perform path compresion and add shortcut * if parent[ s ] is not a root node */ const size_t p = parent[ s ]; return parent[ p ] == p ? p : parent[ s ] = find( p ) ; } inline void lazy_union ( size_t i, size_t j ) { /* unions should be done on root nodes */ i = find( i ); j = find( j ); if ( i != j ) { if ( rank [ i ] < rank[ j ] ) parent[ i ] = j; else { parent[ j ] = i; rank[ i ] += rank[ i ] == rank[ j ]; } } } private: vector < size_t > parent; vector < size_t > rank; }; // TODO XXX // template < class NumType > // unsigned num_hash_func ( const NumType & a ) // { // // XXX what will happen in case of overflow? // return static_cast < unsigned > ( a % 9973 ) % 9973 ; // } /* * XXX: HashMap: map< Key, T > data [ 9973 ] * data [ num_hash_func ( key ) ][ key ] */ /* * testing util * ---------------------------------------------- */ // TODO add a preprocessor which automatically includes the funciton name, or __line__ // and disables if not in debug mode /* prints the life length of the object when it goes out of scope */ class ScopeTimer { public: ScopeTimer ( const string & msg = ): tic ( clock ( )), m_msg( msg ) { }; ~ ScopeTimer ( ) { const clock_t toc = clock(); const uint64 dt = 1000L * ( toc - tic ) / CLOCKS_PER_SEC; const uint64 mil = dt % 1000L; const uint64 sec = ( dt / 1000L ) % 60L; const uint64 min = ( dt / 60000L ) % 60L; const uint64 hrs = ( dt / 3600000L ); cout << n << m_msg << n telapsed time: ; if ( hrs ) cout << hrs << hrs, ; if ( min ) cout << min << min, ; if ( sec ) cout << sec << sec, ; cout << mil << mil-sec n ; } private: typedef unsigned long long int uint64; const clock_t tic; const string m_msg; }; class RandInt { public: RandInt ( int a = 0, int b = 100 ): m ( a ), f ( static_cast < double > ( b - a ) / RAND_MAX ) { } inline int operator() ( ) { return m + ceil ( f * rand( ) ); } private: const int m; const double f; }; class RandDouble { public: RandDouble ( double a = 0.0, double b = 1.0 ): m ( a ), f ( ( b - a ) / RAND_MAX ) { } inline double operator() ( ) { return m + f * rand( ); } private: const double m, f; }; class Noisy { public: Noisy ( string str ): msg ( str ) { cout << Noisy ( << msg << ) t@ << this << endl; } ~Noisy ( ) { cout << ~Noisy ( << msg << ) t@ << this << endl; } void beep ( ) { cout << beep ( << msg << ) t@ << this << endl; } void beep ( ) const { cout << const beep ( << msg << ) t@ << this << endl; } private: const string msg; }; DECLARE ( Noisy ); /* * ---------------------------------------------- * ---------------------------------------------- */ /* * -- @@@ ------------------------------------------------- */ inline int minchar( const size_t j, const vector< int > & a ) { const auto n = a.size(); int k = 0; while( ( j + 1 < n && a[ j + 1 ] == k ) || ( j + 2 < n && a[ j + 2 ] == k )) ++ k; return k; } void probA() { size_t n; int k; cin >> n >> k; string str; cin >> str; vector< int > a( n ); transform(begin(str), end(str), a.rbegin(), bind(minus<char>(), placeholders::_1, a )); // first letter that can increase size_t j = 0; // find_if(begin(a), end(a), bind(less<int>(), placeholders::_1, k)) - begin(a); bool flag = false; while( j < n && ! flag ) { ++ a[ j ]; while( ( j + 1 < n && a[ j ] == a[ j + 1 ]) || ( j + 2 < n && a[ j ] == a[ j + 2 ] )) ++ a[ j ]; if( k < 1 + a[ j ] ) { ++ j; continue; } bool fail = false; for( size_t i = j; 0 < i && !fail; -- i ) { a[ i - 1 ] = minchar(i - 1, a); fail = k < 1 + a[ i - 1 ]; } flag = ! fail; } if( !flag ) cout << NO ; else { for( auto iter = a.rbegin(); iter != a.rend(); ++ iter ) cout << (char)( a + *iter); } } int main ( const int argc, char * argv [ ]) { probA(); // cout << setprecision( 10 ) return EXIT_SUCCESS; } /** * mislav.uniqpath.com/2011/12/vim-revisited/ * set encoding=utf-8 * %s/ (. {60,70 } ) / 1 r/gc * %s/ / /gc * %s/10 ([0-9] {1,2 } )/10^ 1/gc */ |
#include <bits/stdc++.h> using namespace std; int v[112345]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &v[i]); sort(v, v + n); int res = 1; for (int i = 0; i < n; i++) { if (v[i] >= res) { v[i] = res; res++; } } printf( %d n , res); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long a[maxn], b[maxn]; int main() { int m, n; long long sum1 = 0, sum2 = 0, max1 = 0, max2 = 0, t1, t2; scanf( %d%d , &m, &n); for (int i = 1; i <= m; i++) { scanf( %lld , &a[i]); sum1 += a[i]; if (a[i] > max1) { t1 = i; max1 = a[i]; } } for (int i = 1; i <= n; i++) { scanf( %lld , &b[i]); sum2 += b[i]; if (b[i] > max2) { t2 = i; max2 = b[i]; } } long long ans1 = sum1 * n, ans2 = sum2 * m; for (int i = 1; i <= m; i++) { if (i != t1 && a[i] < sum2) ans2 = ans2 - sum2 + a[i]; } for (int i = 1; i <= n; i++) { if (i != t2 && b[i] < sum1) ans1 = ans1 - sum1 + b[i]; } printf( %lld n , min(ans1, ans2)); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; string st; int call() { int s = 0, r = -1, neg = 0; for (int i = 0; st[i]; i++) { if (st[i] == ( ) s++; else s--; if (s <= 1) r = i; if (s < 0) neg = 1; } int ans = 0; if (s == 2 && !neg) { s = 0; for (int i = 0; st[i]; i++) { if (st[i] == ( ) s++; else s--; if (i > r) { if (st[i] == ( ) { if (s - 2 >= 0) ans++; } } } } return ans; } int main() { cin >> n >> st; int ans = call(); string y; for (int i = st.size() - 1; i >= 0; i--) { if (st[i] == ) ) y += ( ; else y += ) ; } st = y; ans += call(); cout << ans; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, m, far[MAXN], a[MAXN]; struct LCT { int fa[MAXN], ch[MAXN][2]; int s[MAXN], xs[MAXN]; long long xs2[MAXN], sum[MAXN], ans[MAXN], xans[MAXN]; bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } int get(int x) { return ch[fa[x]][1] == x; } void up(int x) { s[x] = s[ch[x][0]] + s[ch[x][1]] + xs[x] + 1; sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + 1ll * a[x] * (xs[x] + 1); ans[x] = ans[ch[x][0]] + ans[ch[x][1]] + xans[x] + a[x] * (1ll * (xs[x] + 1) * (xs[x] + 1) - xs2[x]) + 2ll * a[x] * (xs[x] + 1) * s[ch[x][1]] + 2ll * sum[ch[x][0]] * (s[x] - s[ch[x][0]]); } void rotate(int x) { int f1 = fa[x], f2 = fa[f1], id = get(x); ch[f1][id] = ch[x][id ^ 1]; fa[ch[f1][id]] = f1; if (!isroot(f1)) ch[f2][get(f1)] = x; fa[x] = f2; ch[x][id ^ 1] = f1; fa[f1] = x; up(f1); } void splay(int x) { while (!isroot(x)) { int f1 = fa[x]; if (!isroot(f1)) { if (get(x) == get(f1)) rotate(f1); else rotate(x); } rotate(x); } up(x); } void access(int x) { for (int y = 0; x; y = x, x = fa[x]) { splay(x); xs[x] += s[ch[x][1]] - s[y]; xs2[x] += 1ll * s[ch[x][1]] * s[ch[x][1]] - 1ll * s[y] * s[y]; xans[x] += ans[ch[x][1]] - ans[y]; ch[x][1] = y; up(x); } } void link(int x, int y) { access(x), splay(x); access(y), splay(y); fa[x] = y; xs[y] += s[x]; xans[y] += ans[x]; xs2[y] += 1ll * s[x] * s[x]; up(y); } void cut(int x, int y) { access(y), splay(y); splay(x); fa[x] = 0; xs[y] -= s[x]; xans[y] -= ans[x]; xs2[y] -= 1ll * s[x] * s[x]; up(y); } bool is_anc(int x, int y) { access(y); splay(y), splay(x); return !isroot(y); } long long ask() { splay(1); return ans[1]; } void modify(int x, int v) { access(x), splay(x); a[x] = v, up(x); } } tr; char op[2]; int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) scanf( %d , &far[i]); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= n; i++) tr.s[i] = 1; for (int i = 2; i <= n; i++) tr.link(i, far[i]); printf( %.10lf n , (double)tr.ask() / n / n); scanf( %d , &m); while (m--) { int x, y; scanf( %s%d%d , op, &x, &y); if (op[0] == P ) { if (tr.is_anc(x, y)) swap(x, y); tr.cut(x, far[x]); far[x] = y; tr.link(x, far[x]); } else tr.modify(x, y); printf( %.10lf n , (double)tr.ask() / n / n); } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[200010], b[200010]; int n, m; inline bool isprime(int x) { for (int i = 2; i * i <= x; i++) if (x % i == 0) return 0; return 1; } unsigned long long hashseed; unsigned long long h1[200010], h2[200010], t[200010]; inline void prehash() { for (int i = 1; i < n; i++) h1[i] = h1[i - 1] * hashseed + (unsigned long long)b[i]; for (int i = n - 1; i; i--) h2[i] = h2[i + 1] * hashseed + (unsigned long long)b[i]; t[0] = 1; for (int i = 1; i <= n; i++) t[i] = t[i - 1] * hashseed; } inline unsigned long long q1(int l, int r) { return h1[r] - h1[l - 1] * t[r - l + 1]; } inline unsigned long long q2(int l, int r) { return h2[l] - h2[r + 1] * t[r - l + 1]; } inline bool check(int l, int r) { return q1(l, r) == q2(l, r); } set<int> ans; int main() { srand(time(NULL)); hashseed = rand() * rand(); while (!isprime(hashseed)) hashseed++; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , a + i); for (int i = 1; i < n; i++) b[i] = a[i + 1] - a[i]; prehash(); bool flag; for (int i = 1; i <= n; i++) { flag = 1; if (i != 1 && !check(1, i - 1)) flag = 0; if (i != n) { if (a[1] + a[i] + m != a[i + 1] + a[n]) flag = 0; if (i != n - 1 && !check(i + 1, n - 1)) flag = 0; } if (flag) ans.insert((a[1] + a[i]) % m); } cout << ans.size() << endl; for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) printf( %d , *it); } |
#include <iostream> #include <algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<cstdlib> #include<vector> #include<iomanip> #include<ctime> #include<set> #include<map> #include<queue> #include<stack> #include<bitset> #define sqr(x) ((x)*(x)) #define fz1(i,n) for ((i)=1;(i)<=(n);(i)++) #define fd1(i,n) for ((i)=(n);(i)>=1;(i)--) #define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++) #define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--) #define fz0k(i,n) for ((i)=0;(i)<(n);(i)++) #define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--) #define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++) #define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--) #define fzin fz1(i,n) #define fzim fz1(i,m) #define fzjn fz1(j,n) #define fzjm fz1(j,m) #define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr) #define rdst(st,len){static char ss[len];scanf( %s ,ss);(st)=ss;} #define incm(x,y) {x=((x)+(y))%mod;} #define spln(i,n) (i==n? n : ) #define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}} #define fi first #define se second #define mk make_pair using namespace std; inline void read(int &x) { char c;int f=1; while(!isdigit(c=getchar()))if(c== - )f=-1; x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15); x*=f; } int n,m,q,i,j,cur; vector<unsigned long long> all; unsigned long long a[200005],op[200005],b[200005]; struct bit { int b[400005]; unsigned long long a[400005]; void add(int x,int y) { unsigned long long t=y*all[x-1]; while(x<=m){ a[x]+=t; b[x]+=y; x+=(x&-x); } } unsigned long long query1(int x) { unsigned long long s=0; while(x){ s+=a[x]; x-=(x&-x); } return s; } int query0(int x) { int s=0; while(x){ s+=b[x]; x-=(x&-x); } return s; } int kth(int x) { int s=0,i; for(i=18;i>=0;i--){ if(s+(1<<i)>m) continue; if(b[s+(1<<i)]<x){ x-=b[s+(1<<i)]; s+=(1<<i); } } return s+1; } }tr1; struct nd { unsigned long long s1,s2; int sz; }; struct seg { nd s[1600005]; nd merge(nd x,nd y) { nd z; z.sz=x.sz+y.sz; z.s1=x.s1+y.s1; z.s2=x.s2+y.s2+y.s1*(unsigned long long)x.sz; return z; } void pushup(int x) { s[x]=merge(s[x+x],s[x+x+1]); } void update(int x,int l,int r,int y,int c) { if(l==r){ s[x].s1+=((unsigned long long)c)*all[l-1]; s[x].s2+=((unsigned long long)c)*all[l-1]; s[x].sz+=c; return; } int mid=(l+r)/2; if(y<=mid)update(x+x,l,mid,y,c); else update(x+x+1,mid+1,r,y,c); pushup(x); } nd query(int x,int l,int r,int ql,int qr) { if(ql<=l&&r<=qr) return s[x]; int mid=(l+r)/2; if(qr<=mid) return query(x+x,l,mid,ql,qr); if(ql>mid) return query(x+x+1,mid+1,r,ql,qr); return merge(query(x+x,l,mid,ql,qr),query(x+x+1,mid+1,r,ql,qr)); } }tr2; void ins(int x) { cur++; tr1.add(x,1); tr2.update(1,1,m,x,1); } void del(int x) { cur--; tr1.add(x,-1); tr2.update(1,1,m,x,-1); } unsigned long long gsum1(int r) { if(r==0) return 0; if(r==cur) return tr1.query1(m); int t=tr1.kth(r); unsigned long long s1=tr1.query1(t),s0=tr1.query0(t); return s1-(s0-r)*all[t-1]; } unsigned long long gsum1(int l,int r) { return gsum1(r)-gsum1(l-1); } unsigned long long c2(unsigned long long x) { unsigned long long y=x+1; if(x%2==0)x/=2;else y/=2; return x*y; } unsigned long long gsuml(int len) { if(len==0) return 0; int t=tr1.kth(len); nd s=tr2.query(1,1,m,1,t); return s.s2-(c2(s.sz)-c2(len))*all[t-1]-(s.s1-(s.sz-len)*all[t-1])*(len+1); } unsigned long long gsumr(int len) { if(len==0) return 0; int t=tr1.kth(cur-len); nd s=tr2.query(1,1,m,t,m); unsigned long long res=s.s2-(c2(s.sz-len))*all[t-1]-(s.s1-(s.sz-len)*all[t-1])*(s.sz-len); return res; } void query() { if(cur<=1){ puts( 0 ); return; } unsigned long long ans=tr2.s[1].s2*2; int s=0,l=0,r=cur/2+1,mid; while(l<r){ mid=(l+r)/2; unsigned long long sl=gsum1(1,mid+1); unsigned long long sr=gsum1(cur-mid+1,cur); if(sl>=sr){ s=mid; l=mid+1; } else r=mid; } ans+=gsuml(s+1); ans+=gsumr(s); s=cur,l=cur/2,r=cur-1,mid; while(l<r){ mid=(l+r+1)/2; unsigned long long sl=gsum1(1,mid+1); unsigned long long sr=gsum1(cur-mid+1,cur); if(sl>=sr){ s=mid; r=mid-1; } else l=mid; } ans-=gsuml(s); ans-=gsumr(s-1); ans-=tr2.s[1].s1*(cur+1); printf( %I64u n ,ans); } int main() { scanf( %d%d ,&n,&q); fz1(i,n){ scanf( %I64u ,&a[i]); all.push_back(a[i]); } fz1(i,q){ scanf( %d%I64u ,&op[i],&b[i]); all.push_back(b[i]); } sort(all.begin(),all.end()); all.resize(unique(all.begin(),all.end())-all.begin()); m=all.size(); fz1(i,n){ ins(upper_bound(all.begin(),all.end(),a[i])-all.begin()); } query(); fz1(i,q){ if(op[i]==1) ins(upper_bound(all.begin(),all.end(),b[i])-all.begin()); else del(upper_bound(all.begin(),all.end(),b[i])-all.begin()); query(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long int T, n; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> T; while (T--) { cin >> n; set<long long int> chk_div; vector<long long int> g; for (long long int i = 0, u; i < n; i++) cin >> u, g.push_back(u); sort(g.begin(), g.end()); long long int p = (g[0] * g[n - 1]); for (long long int i = 2; i * i <= p; i++) if (!(p % i)) chk_div.insert(i), chk_div.insert(p / i); set<long long int> div(g.begin(), g.end()); (chk_div == div) ? (cout << p << n ) : (cout << -1 n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 15 + 5; int deg[MAXN], s[MAXN]; queue<int> q; int main() { int n, sum = 0, u, v; cin >> n; while (!q.empty()) q.pop(); for (int i = 0; i < n; i++) { cin >> deg[i] >> s[i]; if (deg[i] == 1) q.push(i); sum += deg[i]; } cout << sum / 2 << endl; while (!q.empty()) { u = q.front(); q.pop(); if (deg[u] == 0) continue; deg[u] = 0; v = s[u]; s[v] ^= u; deg[v]--; if (deg[v] == 1) q.push(v); cout << u << << v << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { vector<long> vt; long i = 0, n, temp, count = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> temp; vt.push_back(temp); } sort(vt.begin(), vt.end()); while (i < n) { if (vt[i] <= count) { count++; } else { temp = count; bool check = true; for (int j = i; j < n; j++) { if (vt[j] <= temp) { check = true; temp++; i = j; break; } else { check = false; temp++; } } if (check == false) break; else count = temp; } i++; } cout << count << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long h[1000005]; long long dp[1000005][2]; const int mod = 1e9 + 7; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %I64d , &h[i]), h[i]--; for (int i = 1; i <= n; i++) { dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] * min(h[i], h[i - 1]) + h[i]) % mod; dp[i][1] = ((min(h[i + 1], h[i])) + min(h[i + 1], min(h[i], h[i - 1])) * dp[i - 1][1]) % mod; } printf( %I64d n , dp[n][0]); return 0; } |
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> p1, pair<int, int> p2) { return (p1.first < p2.first) || (p1.first == p2.first && p1.second > p2.second); } int main() { bool b[200000]; int t[200000]; int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> t[i]; b[i] = true; } int answer = 1; for (int i = 2; i <= n; ++i) { if (t[i] == 0) { ++answer; continue; } if (b[t[i]]) b[t[i]] = false; else ++answer; } cout << answer; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; int scan() { return getchar(); } template <class T> void scan(T a) { cin >> a; } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(long double &a) { cin >> a; } void scan(char a[]) { scanf( %s , a); } void scan(string &a) { cin >> a; } template <class T> void scan(vector<T> &); template <class T, size_t size> void scan(array<T, size> &); template <class T, class L> void scan(pair<T, L> &); template <class T, size_t size> void scan(T (&)[size]); template <class T> void scan(vector<T> &a) { for (auto &i : a) scan(i); } template <class T> void scan(deque<T> &a) { for (auto &i : a) scan(i); } template <class T, size_t size> void scan(array<T, size> &a) { for (auto &i : a) scan(i); } template <class T, class L> void scan(pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T, size_t size> void scan(T (&a)[size]) { for (auto &i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } string stin() { string s; cin >> s; return s; } template <class T, class S> inline bool chmax(T &a, S b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class S> inline bool chmin(T &a, S b) { if (a > b) { a = b; return 1; } return 0; } vector<int> iota(int n) { vector<int> a(n); iota(begin(a), end(a), 0); return a; } template <class T> void UNIQUE(vector<T> &x) { sort(begin(x), end(x)); x.erase(unique(begin(x), end(x)), x.end()); } int in() { int x; cin >> x; return x; } long long lin() { unsigned long long x; cin >> x; return x; } void print() { putchar( ); } void print(bool a) { cout << a; } void print(int a) { cout << a; } void print(long long a) { cout << a; } void print(char a) { cout << a; } void print(string &a) { cout << a; } void print(double a) { cout << a; } template <class T> void print(const vector<T> &); template <class T, size_t size> void print(const array<T, size> &); template <class T, class L> void print(const pair<T, L> &p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } cout << endl; } template <class T> void print(const deque<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } } template <class T, size_t size> void print(const array<T, size> &a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } } template <class T, class L> void print(const pair<T, L> &p) { cout << ( ; print(p.first); cout << , ; print(p.second); cout << ) ; } template <class T> void print(set<T> &x) { for (auto e : x) print(e), cout << ; cout << endl; } template <class T> void print(multiset<T> &x) { for (auto e : x) print(e), cout << ; cout << endl; } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { cout << ; print(*i); } } template <class T> void print(const T &a) { cout << a; } int out() { putchar( n ); return 0; } template <class T> int out(const T &t) { print(t); putchar( n ); return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); putchar( ); out(tail...); return 0; } long long gcd(long long a, long long b) { while (b) { long long c = b; b = a % b; a = c; } return a; } long long lcm(long long a, long long b) { if (!a || !b) return 0; return a * b / gcd(a, b); } vector<pair<long long, long long>> factor(long long x) { vector<pair<long long, long long>> ans; for (long long i = 2; i * i <= x; i++) if (x % i == 0) { ans.push_back({i, 1}); while ((x /= i) % i == 0) ans.back().second++; } if (x != 1) ans.push_back({x, 1}); return ans; } template <class T> vector<T> divisor(T x) { vector<T> ans; for (T i = 1; i * i <= x; i++) if (x % i == 0) { ans.push_back(i); if (i * i != x) ans.push_back(x / i); } return ans; } template <typename T> void zip(vector<T> &x) { vector<T> y = x; sort(begin(y), end(y)); for (int i = 0; i < x.size(); ++i) { x[i] = distance((y).begin(), lower_bound(begin(y), end(y), (x[i]))); } } int popcount(long long x) { return __builtin_popcountll(x); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); } long long rndll(long long n) { return uniform_int_distribution<long long>(0, n - 1)(rng); } template <typename T> void shuffle(vector<T> &v) { for (long long i = v.size() - 1; i >= 1; --i) { swap(v[i], v[rnd(i)]); } } vector<string> YES{ NO , YES }; vector<string> Yes{ No , Yes }; vector<string> yes{ no , yes }; template <class... T> void err(const T &...) {} template <typename T> struct edge { int from, to; T cost; int id; edge(int to, T cost) : from(-1), to(to), cost(cost) {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; struct Graph : vector<vector<int>> { using vector<vector<int>>::vector; Graph(int n, int m) : vector(n) { read(m); } inline void add(int a, int b, bool directed = false) { (*this)[a].emplace_back(b); if (!directed) (*this)[b].emplace_back(a); } void read(int n = -1, int offset = 1, bool directed = false) { if (n == -1) n = this->size() - 1; int a, b; while (n--) { cin >> a >> b; Graph::add(a - offset, b - offset, directed); } } }; template <typename T> struct WeightedGraph : vector<Edges<T>> { using vector<Edges<T>>::vector; inline void add(int a, int b, T c, bool directed = false) { (*this)[a].emplace_back(b, c); if (!directed) (*this)[b].emplace_back(a, c); } void read(int n = -1, int offset = 1) { if (n == -1) n = this->size() - 1; int a, b; T c; while (n--) { cin >> a >> b >> c; WeightedGraph::add(a - offset, b - offset, c); } } }; struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); } } setup_io; int main() { int n; IN(n); vector<int> a(n); IN(a); vector<vector<vector<bool>>> dp( n + 1, vector<vector<bool>>(100, vector<bool>(3000))); vector<vector<vector<pair<int, int>>>> pre( n + 1, vector<vector<pair<int, int>>>(100, vector<pair<int, int>>(3000))); dp[0][0][0] = 1; vector<int> tr(100); for (long long i = 1; i <= 98; ++i) tr[i + 1] = tr[i] + i; sort(begin(a), end(a)); for (long long i = 0; i < n; ++i) { for (long long j = 0; j < a[i] * 2 + 2; ++j) { for (long long k = 0; k < 3000; ++k) { if (!dp[i][j][k]) continue; int jj = j + 1, kk = k + a[i]; while (kk >= tr[jj]) { dp[i + 1][jj][kk] = true; pre[i + 1][jj][kk] = pair<int, int>(j, k); jj++, kk += a[i]; } } } } int m = 0; vector<int> v; for (long long i = 1; i <= 64; ++i) { if (dp[n][i][tr[i]]) { m = i; int j = i, k = tr[i]; int t = n; while (t > 0) { auto [jj, kk] = pre[t][j][k]; for (long long _ = 0; _ < j - jj; ++_) v.emplace_back(a[t - 1]); t--, j = jj, k = kk; } break; } } if (!m) { cout << =( << n ; return 0; } reverse(begin(v), end(v)); cout << m << n ; vector<vector<int>> g(m, vector<int>(m, -1)); auto id = iota(m); for (long long i = m - 1; i >= 0; --i) { for (long long j = 0; j < v[id[i]]; ++j) g[id[i]][id[j]] = 1, g[id[j]][id[i]] = 0; for (long long j = v[id[i]]; j <= i - 1; ++j) g[id[i]][id[j]] = 0, g[id[j]][id[i]] = 1, v[id[j]]--; sort(id.begin(), id.begin() + i, [&](int a, int b) { return v[a] < v[b]; }); } for (long long i = 0; i < m; ++i) g[i][i] = 0; for (long long i = 0; i < m; ++i) { for (long long j = 0; j < m; ++j) { cout << g[i][j]; } cout << n ; } } |
#include <bits/stdc++.h> using namespace std; FILE* stream; const long long P = 998244353; long long mpow(long long n, int deg) { if (deg == 0) { return 1; } if (deg % 2 == 0) { return mpow((n * n) % P, deg / 2) % P; } return (mpow(n, deg - 1) * n) % P; } signed main() { ios::sync_with_stdio(0); cin.tie(0); long long w, h; cin >> w >> h; cout << mpow(2, w + h) % P << endl; } |
#include <bits/stdc++.h> using namespace std; int n; map<int, int> a; bool found; int b[5][5], sum, cols[5]; void backTrack(int x, int y, int sumRow, int sumDia1, int sumDia2) { if (x == n) { if (sumDia1 != sum || sumDia2 != sum) return; for (int i = 0; i < n; i++) if (cols[i] != sum) return; found = true; return; } for (typeof(a.begin()) it = a.begin(); it != a.end(); it++) if (it->second > 0) { it->second--; sumRow += it->first; if (x == y) sumDia1 += it->first; if (x + y == n - 1) sumDia2 += it->first; cols[y] += it->first; int xx = x, yy = y + 1; if (yy == n) yy = 0, xx++; b[x][y] = it->first; if (yy == 0) { if (sumRow == sum) backTrack(xx, yy, 0, sumDia1, sumDia2); } else backTrack(xx, yy, sumRow, sumDia1, sumDia2); it->second++; sumRow -= it->first; if (x == y) sumDia1 -= it->first; if (x + y == n - 1) sumDia2 -= it->first; cols[y] -= it->first; if (found) return; } } int main() { sum = 0; scanf( %d , &n); for (int i = 0, val; i < n * n; i++) { scanf( %d , &val); a[val]++; sum += val; } sum /= n; found = false; memset(cols, 0, sizeof(cols)); backTrack(0, 0, 0, 0, 0); printf( %d n , sum); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j) putchar( ); printf( %d , b[i][j]); } putchar( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; double PI = 3.141592653589793; const double EPS = 1e-9; const int N = 1e6 + 5; const long long mod = 1e9 + 7; const int oo = 1e9 + 9; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int n, m; long long row[1005], col[1005]; int main() { scanf( %d%d , &n, &m); for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= m - 1; j++) { int x; scanf( %d , &x); row[i] += x; col[j] += x; } } long long min_row = 1e18; int ans_row; for (int i = 0; i <= n; i++) { long long curr = 0; for (int j = i, x = 2; j < n; j++, x += 4) { curr += x * x * row[j]; } for (int j = i - 1, x = 2; j >= 0; j--, x += 4) { curr += x * x * row[j]; } if (curr < min_row) { ans_row = i; min_row = curr; } } long long min_col = 1e18; int ans_col; for (int i = 0; i <= m; i++) { long long curr = 0; for (int j = i, y = 2; j < m; j++, y += 4) { curr += (long long)y * y * col[j]; } for (int j = i - 1, y = 2; j >= 0; j--, y += 4) { curr += (long long)y * y * col[j]; } if (curr < min_col) { ans_col = i; min_col = curr; } } printf( %lld n%d %d , min_col + min_row, ans_row, ans_col); } |
#include <bits/stdc++.h> using namespace std; long long input() { long long x = 0, f = 0; char ch = getchar(); while (ch < 0 || ch > 9 ) f |= ch == - , ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); return f ? -x : x; } const int N = 107; string s[N]; map<string, int> p; int vis[N]; string res; int main() { int n = input(), m = input(); for (int i = 1; i <= n; i++) { cin >> s[i]; if (p[s[i]]) res += s[i], vis[i] = vis[p[s[i]]] = 1; else reverse(s[i].begin(), s[i].end()), p[s[i]] = i; } string ser = res; reverse(ser.begin(), ser.end()); for (int i = 1; i <= n; i++) { if (vis[i]) continue; int flag = 1; for (int j = 0; j < m / 2; j++) { if (s[i][j] != s[i][m - j - 1]) { flag = 0; break; } } if (flag == 1) { res += s[i]; break; } } res += ser; cout << res.length() << endl; cout << res << endl; } |
#include <bits/stdc++.h> using namespace std; inline long long read() { long long 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 n; long long w[22]; bool used[(1 << 20) + 10]; int st[22], num, end_, maxn; bool col[22]; long long A[(1 << 20) + 10], B[(1 << 20) + 10], cntA, cntB; long long AA, BB, AA_, BB_; void dfs(int a, long long cha, int ty, int allodd, int alleven) { if (a == end_) { if (!ty) A[++cntA] = cha; else B[++cntB] = cha; if (!ty) { if (allodd) AA = cha; if (alleven) AA_ = cha; } else { if (allodd) BB = cha; if (alleven) BB_ = cha; } return; } col[a] = 0, dfs(a + 1, cha - w[st[a]], ty, 0, alleven); col[a] = 1, dfs(a + 1, cha + w[st[a]], ty, allodd, 0); } bool chk(int sta) { num = 0; long long sum = 0; for (int i = 1; i <= n; i++) if (sta & (1 << (i - 1))) st[++num] = i, sum += w[i]; if (num == 1 || (sum & 1) != ((num - 1) & 1)) return 0; int mid = num >> 1; cntA = cntB = 0; AA = AA_ = BB = BB_ = 2e18; end_ = mid + 1, dfs(1, 0, 0, 1, 1); end_ = num + 1, dfs(mid + 1, 0, 1, 1, 1); sort(A + 1, A + 1 + cntA); sort(B + 1, B + 1 + cntB); int pB = cntB, pA = cntA; if (abs(AA + BB_) <= num - 1 || abs(AA_ + BB) <= num - 1) return 1; for (int i = 1; i <= cntA; i++) { if (A[i] == AA_ || A[i] == AA) continue; long long l = 1 - num - A[i], r = num - 1 - A[i]; while (pB && B[pB] > r) pB--; if (!pB) return 0; if (B[pB] >= l) return 1; } for (int i = 1; i <= cntB; i++) { if (B[i] == BB_ || B[i] == BB) continue; long long l = 1 - num - B[i], r = num - 1 - B[i]; while (pA && A[pA] > r) pA--; if (!pA) return 0; if (A[pA] >= l) return 1; } return 0; } int f[(1 << 20) + 10 + 10]; int s[22]; int main() { n = read(); int q = 0; for (int i = 1; i <= n; i++) { w[i] = read(); if (w[i]) w[++q] = w[i]; } n = q; maxn = (1 << n) - 1; for (int i = 1; i <= maxn; i++) { used[i] = chk(i); if (used[i]) f[i] = 1; } for (int i = 1; i <= maxn; i++) if (used[i]) { int g = maxn ^ i; for (int j = g; j; j = (j - 1) & g) { f[i + j] = max(f[i + j], f[j] + f[i]); } } cout << n - f[maxn]; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return + s + ; } string to_string(char s) { return string(1, s); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool f = 1; string r = { ; for (const auto& x : v) { if (!f) r += , ; f = 0; r += to_string(x); } return r + } ; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << << to_string(H); debug_out(T...); } int main() { ios::sync_with_stdio(0); cin.tie(0); int v; cin >> v; while (v--) { int n = 250; vector<int> a(n); double m = 0; for (auto i = (0); i <= (n - 1); ++i) cin >> a[i], m += a[i]; m /= n; double var = 0; for (auto i = (0); i <= (n - 1); ++i) var += (a[i] - m) * (a[i] - m); var /= n; if (abs(var - m) > abs(var - (m * m + m) / 3)) { int mx = *max_element((a).begin(), (a).end()); cout << mx / 2 << n ; } else { cout << (int)round(m) << n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int find(int u[], int i) { if (u[i] == i) { return i; } return (u[i] = find(u, u[i])); } int main() { int n, m; cin >> n >> m; if (n != m + 1) { cout << no << endl; return 0; } int ufs[n]; for (int i = 0; i < n; i++) { ufs[i] = i; } int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; int c = find(ufs, a - 1); int d = find(ufs, b - 1); if (c == d) { cout << no << endl; return 0; } int m = min(c, d); ufs[c] = m; ufs[d] = m; } for (int i = 0; i < n; i++) { if (find(ufs, i) != 0) { cout << no << endl; return 0; } } cout << yes << endl; } |
#include <bits/stdc++.h> #include<cmath> using namespace std; int main(){ long long t; cin>>t; while(t--){ int a,b,n; long long ans=0; cin>>a>>b>>n; int ar[a+1],br[b+1],arr[2][n]; memset(ar,0,sizeof(ar)); memset(br,0,sizeof(br)); for(int i=0;i<n;i++){ cin>>arr[0][i]; ar[arr[0][i]]++; } for(int i=0;i<n;i++){ cin>>arr[1][i]; br[arr[1][i]]++; } for(int i=0;i<n-1;i++){ ans+=n-i-1 -ar[arr[0][i]]+1 -br[arr[1][i]]+1; ar[arr[0][i]]--; br[arr[1][i]]--; } cout<<ans<<endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, ex[3]; char st[210]; int main() { scanf( %d , &n); scanf( %s , st + 1); for (int i = 1; i <= (n); i++) { if (st[i] == R ) ex[0]++; if (st[i] == G ) ex[1]++; if (st[i] == B ) ex[2]++; } if ((ex[0] > 0) + (ex[1] > 0) + (ex[2] > 0) == 3) printf( BGR n ); else if ((ex[0] > 0) + (ex[1] > 0) + (ex[2] > 0) == 1) { if (ex[0]) printf( R n ); if (ex[1]) printf( G n ); if (ex[2]) printf( B n ); } else { if (ex[0] == 0) { if (ex[1] == 1 && ex[2] == 1) printf( R n ); if (ex[1] > 1 && ex[2] > 1) printf( BGR n ); if (ex[1] == 1 && ex[2] > 1) printf( GR n ); if (ex[1] > 1 && ex[2] == 1) printf( BR n ); } if (ex[1] == 0) { if (ex[0] == 1 && ex[2] == 1) printf( G n ); if (ex[0] > 1 && ex[2] > 1) printf( BGR n ); if (ex[0] == 1 && ex[2] > 1) printf( GR n ); if (ex[0] > 1 && ex[2] == 1) printf( BG n ); } if (ex[2] == 0) { if (ex[1] == 1 && ex[0] == 1) printf( B n ); if (ex[1] > 1 && ex[0] > 1) printf( BGR n ); if (ex[1] == 1 && ex[0] > 1) printf( BG n ); if (ex[1] > 1 && ex[0] == 1) printf( BR n ); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 100100 * 3; const int INF = 1E9; const int base = 1E9 + 7; void no() { cout << no n ; exit(0); } int a[maxn]; pair<int, int> b[maxn]; char ans[3030][3030]; int n, m; void validate() { vector<int> draws; n = m; for (int i = 0; i < n; i++) if (a[i] & 1) draws.push_back(i); memset(ans, 0, sizeof(ans)); for (int i = 0; i < draws.size(); i += 2) { int x, y; x = draws[i], y = draws[i + 1]; a[x]--, a[y]--; ans[x][y] = ans[y][x] = D ; } for (int i = 0; i < n; i++) b[i] = make_pair(a[i], i); for (int i = 0; i < n; i++) ans[i][i] = X ; for (int i = 0; i < n; i++) { sort(b + i, b + n); reverse(b + i, b + n); int g = b[i].second; for (int j = n - 1; j > i; j--) if (ans[g][b[j].second] == 0) { char w = W , l = L ; if (b[i].first > 0) { ans[g][b[j].second] = w, ans[b[j].second][g] = l; b[i].first -= 2; } else { ans[g][b[j].second] = l, ans[b[j].second][g] = w; b[j].first -= 2; } } if (b[i].first != 0) no(); } puts( yes ); for (int i = 0; i < n; i++) puts(ans[i]); } int solve() { cin >> m >> n; for (int i = 0; i < n; i++) cin >> a[i]; int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; if (sum > m * (m - 1)) no(); if (sum + (m - n) * a[n - 1] < m * (m - 1)) no(); while (sum < m * (m - 1)) for (int i = n; sum < m * (m - 1) && i < m; i++) a[i]++, sum++; validate(); } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; const long long mod = 1e9 + 7; long long num[N]; long long a[N]; long long b[N]; long long ans[N]; signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long T = 1; cin >> T; while (T--) { long long n; cin >> n; for (long long i = 1; i <= n; i++) cin >> num[i]; a[1] = num[1] - 1, b[1] = 1; for (long long i = 2; i <= n; i++) { a[i] = min(a[i - 1], num[i] - b[i - 1]); b[i] = num[i] - a[i]; } bool flag = true; long long ma = *min_element(a + 1, a + 1 + n); long long mb = *min_element(b + 1, b + n + 1); if (ma + mb < 0) flag = false; cout << (flag ? YES n : NO n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b, c, d; scanf( %lld %lld %lld %lld , &a, &b, &c, &d); printf( %lld %lld %lld n , b, c, c); } int main() { int t; scanf( %d , &t); while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; long long a[300005], m[300005], t[300005], ans, sum, n, x, qwq; int main() { scanf( %lld , &n); for (long long i = 1; i <= n; i++) { scanf( %lld%lld , &a[i], &m[i]); x = m[i]; while (x) { x >>= 1ll; t[i]++; } sum += a[i]; } if (sum < 0) for (long long i = 1; i <= n; i++) a[i] *= -1ll; for (long long i = 0; i <= 62; i++) { qwq = 0ll; for (long long j = 1; j <= n; j++) if (m[j] & (1ll << i) && t[j] - 1ll == i) qwq += a[j]; if (qwq > 0) { for (long long j = 1; j <= n; j++) if (m[j] & (1ll << i)) a[j] *= -1ll; ans += (1ll << i); } } printf( %lld , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long ans[maxn << 2]; int main() { long long n, k, m; scanf( %lld%lld , &n, &k); m = (n * 6 - 1) * k; printf( %lld n , m); for (int i = 1; i <= n; i++) { long long tmp = 6 * (i - 1); printf( %lld %lld %lld %lld n , (tmp + 1LL) * k, (tmp + 2LL) * k, (tmp + 3LL) * k, (tmp + 5LL) * k); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool isPrime(int x) { if (x == 1) return false; for (long long i = 2; i <= sqrt(x); i += 1) if (x % i == 0) return false; return true; } void print(int a[], int n) { for (long long i = 0; i < n; i += 1) cout << a[i] << ; } void iarr(int *a, int n) { for (long long i = 0; i < n; i += 1) cin >> a[i]; } long long power(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = ((res % 1000000007) * (a % 1000000007)) % 1000000007; a = ((a % 1000000007) * (a % 1000000007)) % 1000000007; b >>= 1; } return res; } long long numFactors(long long x) { long long cnt = 0; for (long long i = 1; i <= sqrt(x); i += 1) { if (x % i == 0) { if (x / i == i) cnt += 1; else cnt += 2; } } return cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed; cout.precision(10); ; int n; cin >> n; int a[n]; iarr(a, n); long long cnt = 0; while (true) { int ind = -1, mx = a[0]; for (long long i = 1; i < n; i += 1) { if (a[i] >= mx) { ind = i; mx = a[i]; } } if (ind != -1) { a[ind]--; a[0]++; cnt++; } else if (ind == -1) { break; } } cout << cnt; return 0; } |
#include <bits/stdc++.h> using namespace std; unsigned long long int power(unsigned long long int a, unsigned long long int b, unsigned long long int p) { unsigned long long int res = 1; a = a % p; while (b > 0) { if (b & 1) { res = (res * a) % p; b--; } a = (a * a) % p; b >>= 1; } return res; } unsigned long long int gcd(unsigned long long int a, unsigned long long int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); unsigned long long int n, p, k; cin >> n >> p >> k; vector<unsigned long long int> v(n); for (auto &i : v) cin >> i; map<unsigned long long int, unsigned long long int> cnt; for (unsigned long long int i = 0; i < n; i++) { unsigned long long int cur = (power(v[i], 4, p) - (k * v[i]) % p + p) % p; cnt[cur]++; } unsigned long long int ans = 0; for (auto i : cnt) ans += (i.second * (i.second - 1)) / 2; cout << ans << n ; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937_64 gen(gen_seed); const int A = 500; long long d[A + 1][A + 1]; long long fac[A + 1]; long long ifac[A + 1]; long long cfac[A + 1][A + 1]; long long cfac2[A + 1][A + 1]; long long gcd(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long invmod(long long a, long long modulo) { long long x, y; gcd(a, modulo, x, y); return (x % modulo + modulo) % modulo; } int main() { fac[0] = 1; for (long long i = 1; i <= A; i++) fac[i] = (fac[i - 1] * i) % mod; for (int i = 0; i < A + 1; i++) ifac[i] = invmod(fac[i], mod); d[0][0] = 1; for (int i = 0; i < A + 1; i++) for (int j = 0; j < i + 1; j++) cfac[i][j] = fac[i] * ifac[i - j] % mod; for (int i = 0; i < A + 1; i++) for (int j = 0; j < i + 1; j++) cfac2[i][j] = cfac[i][j] * (j + 1) % mod; for (int b = 0; b < A + 1; b++) for (int a = 0; a < A + 1; a++) { if (a == 0) { if (b > 0) d[0][b] = (d[0][b - 1] * b) % mod; continue; } for (int tb = 0; tb < b + 1; tb++) { d[a][b] += d[a - 1][b - tb] * cfac[b][tb] % mod; } if (a > 1) { long long cand = 0; for (int tb = 0; tb < b + 1; tb++) { cand += d[a - 2][b - tb] * cfac2[b][tb] % mod; } d[a][b] += cand * (a - 1); } d[a][b] %= mod; } int n; scanf( %d , &n); int a = 0; int b = 0; for (int i = 0; i < n; i++) { int x; scanf( %d , &x); if (x == 1) a++; else b++; } cout << d[a][b]; } |
#include <bits/stdc++.h> using namespace std; const int NMAX = 20, LMAX = 400000, INF = ~0u >> 1; int K, L = 1, f1[2 * LMAX], f2[LMAX], f3[LMAX]; long long A, B; int gcd(int x, int y) { if (!y) return x; return gcd(y, x % y); } int main() { int i, j; scanf( %I64d %I64d %d , &B, &A, &K); for (i = 2, L = 2; i <= K; i += 1) L = i * L / gcd(L, i); f1[0] = f2[0] = f3[0] = 0; if (B - A <= (long long)2 * L) { for (i = 1; A + i - 1 != B; i += 1) { f1[i] = f1[i - 1] + 1; for (j = 2; j <= K; j += 1) { if (i >= (A + i) % j) f1[i] = min(f1[i], f1[i - (A + i) % j] + 1); } } printf( %d n , f1[i - 1]); } else { for (i = 1; i <= L && (A + i - 1) % L; i += 1) { f1[i] = f1[i - 1] + 1; for (j = 2; j <= K; j += 1) { if ((long long)i >= (A + i) % j) f1[i] = min(f1[i], f1[i - (A + i) % j] + 1); } } for (i = 1; i <= L; i += 1) { f2[i] = f2[i - 1] + 1; for (j = 2; j <= K; j += 1) { if (i >= i % j) f2[i] = min(f2[i], f2[i - i % j] + 1); } } for (i = 1; i <= L && (i - 1) != B % L; i += 1) { f3[i] = f3[i - 1] + 1; for (j = 2; j <= K; j += 1) { if (i >= i % j) f3[i] = min(f3[i], f3[i - i % j] + 1); } } printf( %I64d n , f1[(A + L - 1) / L * L - A] + (long long)f2[L] * (B / L - (A + L - 1) / L) + f3[i - 1]); } exit(0); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k, ans = 0, t; cin >> t; while (t--) { cin >> n >> k; ans = 0; while (n > 0) { if (n % k == 0) { n /= k; ans++; } else { ans += n % k; n -= n % k; } } cout << ans << n ; } } |
#include <bits/stdc++.h> long long p(long long n, long long k) { long long d = 1; while (n) { if (n & 1) d = (d * k) % 1000000007; k = (k * k) % 1000000007; n >>= 1; } return d; } int main() { long long n, l; scanf( %I64d , &n); l = p(n, 2); printf( %I64d , ((l + l * l) / 2) % 1000000007); return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> using VV = vector<vector<T>>; template <class T> inline bool SMIN(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool SMAX(T &l, const T &r) { return l > r ? l = r, 1 : 0; } const string yes = YES , no = NO ; int X, Y, Z, T1, T2, T3; string solve() { long long s = abs(X - Y) * T1; long long e = abs(Z - X) * T2 + abs(X - Y) * T2 + 3LL * T3; return !(s < e) ? yes : no; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> X >> Y >> Z >> T1 >> T2 >> T3; cout << solve() << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k, i, j, mas[50077], fix[50077], dom, a[50077]; int main() { cin >> n; for (k = 1; k <= n; k++) cin >> a[k]; for (k = 1; k <= n; k++) { dom = a[k]; for (j = 1; j <= n; j++) mas[a[j]] = 0; for (i = k; i <= n; i++) { mas[a[i]]++; if (mas[a[i]] > mas[dom]) dom = a[i]; else if (a[i] < dom && mas[a[i]] == mas[dom]) dom = a[i]; fix[dom]++; } } for (k = 1; k <= n; k++) cout << fix[k] << ; } |
#include <bits/stdc++.h> const double pi = (double)(2.0 * acos(0.0)); const int inf = 1 << 30; const double eps = 1E-9; const double e = exp(1.0); const int sz = 100000 + 5; const int mod = 1000000000 + 7; using namespace std; int main() { double x, y, z, res, v; string st = ; scanf( %lf %lf %lf , &x, &y, &z); if (x < 1.0 + eps && y < 1.0 + eps && z < 1.0 + eps) { res = 1e18; v = z * log(y) + log(log(1.0 / x)); if (res > v + eps) res = v, st = x^y^z ; v = y * log(z) + log(log(1.0 / x)); if (res > v + eps) res = v, st = x^z^y ; v = log(y) + log(z) + log(log(1.0 / x)); if (res > v + eps) res = v, st = (x^y)^z ; v = z * log(x) + log(log(1.0 / y)); if (res > v + eps) res = v, st = y^x^z ; v = x * log(z) + log(log(1.0 / y)); if (res > v + eps) res = v, st = y^z^x ; v = log(x) + log(z) + log(log(1.0 / y)); if (res > v + eps) res = v, st = (y^x)^z ; v = y * log(x) + log(log(1.0 / z)); if (res > v + eps) res = v, st = z^x^y ; v = x * log(y) + log(log(1.0 / z)); if (res > v + eps) res = v, st = z^y^x ; v = log(x) + log(y) + log(log(1.0 / z)); if (res > v + eps) res = v, st = (z^x)^y ; cout << st << endl; } else { res = -1e18; if (x > 1.0 + eps) { v = z * log(y) + log(log(x)); if (res + eps < v) res = v, st = x^y^z ; v = y * log(z) + log(log(x)); if (res + eps < v) res = v, st = x^z^y ; v = log(y) + log(z) + log(log(x)); if (res + eps < v) res = v, st = (x^y)^z ; } if (y > 1.0 + eps) { v = z * log(x) + log(log(y)); if (res + eps < v) res = v, st = y^x^z ; v = x * log(z) + log(log(y)); if (res + eps < v) res = v, st = y^z^x ; v = log(x) + log(z) + log(log(y)); if (res + eps < v) res = v, st = (y^x)^z ; } if (z > 1.0 + eps) { v = y * log(x) + log(log(z)); if (res + eps < v) res = v, st = z^x^y ; v = x * log(y) + log(log(z)); if (res + eps < v) res = v, st = z^y^x ; v = log(x) + log(y) + log(log(z)); if (res + eps < v) res = v, st = (z^x)^y ; } cout << st << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int ans = 0; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 1 ) ans += 10; else if (s[i] == A ) ans += 1; else ans += s[i] - 0 ; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005], difs, f, s; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; i++) if (a[i] != b[i]) difs++; if (difs > 2) { cout << NO ; return 0; } else { cout << YES ; return 0; } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,no-stack-protector ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) using namespace std; const int maxn = 3e5 + 5; const int maaxn = 1e7 + 2; const int minn = 22; const long long mod = 1e9 + 7; const long double pi = 3.141592653589793238462643383279; long long binpow(long long a, long long p) { if (p == 0) return 0; if (p % 2 == 1) return (a + binpow(a, p - 1)) % mod; long long b = binpow(a, p / 2); return (b + b) % mod; } int a[maxn]; vector<int> dv[maxn]; vector<int> hop[maaxn]; int cnt[maaxn]; bool sh(int &c, int &b) { return a[c] < a[b]; } void solve() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = 2; j * j <= a[i]; j++) { if (a[i] % j == 0) { dv[i].push_back(j); cnt[j]++; hop[j].push_back(i); if (j * j != a[i]) dv[i].push_back(a[i] / j), cnt[a[i] / j]++, hop[a[i] / j].push_back(i); } } } for (int i = 2; i < maaxn; i++) { if (cnt[i] >= k) { for (int j = 0; j < k; j++) { cout << (hop[i][j] + 1) << ; } cout << endl; return; } } int ma = 0; int p = 0; for (int i = 2; i < maaxn; i++) { if (hop[i].size() > ma) { ma = hop[i].size(); p = i; } } vector<int> answ; unordered_set<int> tv; for (int i = 0; i < hop[p].size(); i++) tv.insert(hop[p][i]); for (int i = 0; i < n; i++) { if (tv.count(i) == 0) answ.push_back(i); } sort(answ.begin(), answ.end(), sh); for (int i = 0; i < k; i++) cout << answ[i] + 1 << ; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; t = 1; while (t--) { solve(); } cin >> t; return 0; } |
#include <bits/stdc++.h> using namespace std; const int p = 1e9 + 7; struct Matrix { int n, m; long long a[105][105]; Matrix(int x, int y) { n = x, m = y; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) a[i][j] = 0; } } void init() { for (int i = 1; i <= min(n, m); i++) a[i][i] = 1; } void print() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) cout << a[i][j] << ; cout << endl; } cout << endl; } }; Matrix operator*(Matrix a, Matrix b) { Matrix c = Matrix(a.n, b.m); for (int i = 1; i <= a.n; i++) { for (int j = 1; j <= b.m; j++) { for (int k = 1; k <= a.m; k++) { c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j] % p) % p; } } } return c; } Matrix qpow(Matrix a, long long b) { Matrix res = Matrix(a.n, a.m); res.init(); while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long n, m, k; int main() { cin >> n >> m >> k; Matrix f = Matrix(m, m); for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) f.a[i][j] = 1; } string s; int x, y, z; while (k--) { cin >> s; if (s[0] <= Z && s[0] >= A ) x = s[0] - A + 27; else x = s[0] - a + 1; if (s[1] <= Z && s[1] >= A ) y = s[1] - A + 27; else y = s[1] - a + 1; f.a[x][y] &= 0; } Matrix ans = Matrix(1, m); for (int i = 1; i <= m; i++) ans.a[1][i] = 1; f = qpow(f, n - 1); ans = ans * f; long long res = 0; for (int i = 1; i <= m; i++) res += ans.a[1][i], res %= p; cout << res << endl; return 0; } |
#include <bits/stdc++.h> int main() { int n, a[5001]; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= n; i++) if (a[a[a[i]]] == i) { printf( YES ); return 0; } printf( NO ); return 0; } |
#include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; void solve() { int n; cin >> n; int adad[n]; map<int, int> cnt; for (int i = 0; i < n; i++) { cin >> adad[i]; cnt[adad[i]]++; } int mini; mini = *min_element(adad, adad + n); cout << n - cnt[mini] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; inline void writeln2() { cout << n ; } inline void writeln() { cout << n ; } inline void readln() {} template <typename T> inline void read(T&); template <typename T> inline void priws(T); template <typename T> inline void print(T); void err(vector<string>::iterator it) { ++it; } template <typename Head, typename... Tail> inline void readln(Head& head, Tail&... tail) { read(head); readln(tail...); } template <typename Head, typename... Tail> inline void writeln2(Head head, Tail... tail) { print(head); writeln2(tail...); } template <typename Head, typename... Tail> inline void writeln(Head head, Tail... tail) { priws(head); writeln2(tail...); } template <typename T> inline void writeln_range(T f, T s) { if (f != s) for (auto i = f; i != s; ++i) writeln(*i); } template <typename Head, typename... Tail> inline void err(vector<string>::iterator it, Head head, Tail... tail) { writeln((*it).substr((*it)[0] == ), = , head); err(++it, tail...); } vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } void printAnsAndExit(long long ans) { writeln(ans); exit(0); } vector<vector<int>> g; vector<bool> used; int cc = 0; int dfs(int u) { used[u] = true; int ok = 0; for (int i = 0; i < (int)(g[u].size()); ++i) if (!used[g[u][i]]) ok += dfs(g[u][i]); if (ok >= 3) cc += 2, ok = 0; else { if (ok == 2) cc += 2, ok = 0; else { if (ok == 1) cc++; ok = 1; } } return ok; } void run() { int n, x, y; readln(n, x, y); vector<pair<int, int>> a(n - 1); readln(a); vector<int> deg(n + 1, 0); g.resize(n); used.resize(n); for (int i = 0; i < (int)(n - 1); ++i) deg[a[i].first - 1]++, deg[a[i].second - 1]++, g[a[i].first - 1].push_back(a[i].second - 1), g[a[i].second - 1].push_back(a[i].first - 1); if (x > y) { for (int i = 0; i < (int)(deg.size()); ++i) if (deg[i] == n - 1) printAnsAndExit(x + (n - 2) * 1ll * y); printAnsAndExit((n - 1) * 1ll * y); } dfs(0); writeln(cc * 1ll * x + (n - cc - 1) * 1ll * y); } int main() { ios_base::sync_with_stdio(false); run(); return 0; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& _a); template <typename T1, typename T2> inline istream& operator>>(istream& is, pair<T1, T2>& _a) { return is >> _a.first >> _a.second; } template <typename T1, typename T2> inline ostream& operator<<(ostream& os, pair<T1, T2>& _a) { return os << _a.first << << _a.second; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& _a) { if (_a.size()) os << _a[0]; else os << n ; for (int i = 1; i < _a.size(); ++i) os << n [is_fundamental<T>::value] << _a[i]; return os; } template <typename T> inline istream& operator>>(istream& is, vector<T>& _a) { if (_a.size() == 0) { int _n; is >> _n; _a.resize(_n); } for (int i = 0; i < _a.size(); ++i) is >> _a[i]; return is; } template <typename T> inline void print(T _a) { cout << << _a; } template <typename T> inline void priws(T _a) { cout << _a; } template <typename T> inline void read(T& _a) { cin >> _a; } |
#include <bits/stdc++.h> using namespace std; set<int> marc[100010]; int input(void) { int x; scanf( %d , &x); return x; } int main() { int n = input(); for (int i = 0; i < (n); i++) { int x = input(), k = input(); if (x == 0) { marc[k].insert(0); } else { if (marc[k].find(x - 1) != marc[k].end()) { marc[k].insert(x); } else { cout << NO n ; return 0; } } } cout << YES n ; return 0; } |
#include <bits/stdc++.h> using namespace std; struct Time { long long deno, num; inline long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } inline void simplify() { long long g = gcd(abs(deno), abs(num)); deno /= g; num /= g; if (deno < 0) { deno *= -1; num *= -1; } } Time() {} Time(long long n, long long d) : num(n), deno(d) { simplify(); } inline bool operator<(const Time &t) const { return num * t.deno < t.num * deno; } inline bool operator!=(const Time &t) const { return num * t.deno != t.num * deno; } }; pair<Time, Time> s[100010]; pair<Time, int> ds[100010]; int d[100010]; struct BIT { int f[100010]; BIT() { fill(f, f + 100010, 0); } inline int lowbit(int x) { return (x & -x); } inline void add(int p, int v) { for (++p; p < 100010; p += lowbit(p)) f[p] += v; } inline int sum(int p) { int ret = 0; for (++p; p; p -= lowbit(p)) ret += f[p]; return ret; } inline int sum(int l, int r) { return sum(r) - sum(l - 1); } } bit; int n, w; long long v, x; int main() { cin >> n >> w; for (int i = 0; i < n; i++) { cin >> x >> v; s[i] = make_pair(Time(-x, v - w), Time(-x, v + w)); } for (int i = 0; i < n; i++) s[i].second.num *= -1; sort(s, s + n); for (int i = 0; i < n; i++) s[i].second.num *= -1; for (int i = 0; i < n; i++) ds[i] = make_pair(s[i].second, i); sort(ds, ds + n); for (int i = 0, rk = -1; i < n; i++) { if (!i || ds[i].first != ds[i - 1].first) rk++; d[ds[i].second] = rk; } long long ans = 0; for (int i = 0; i < n; i++) { ans += bit.sum(d[i], 100010 - 1); bit.add(d[i], 1); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int par1[10008]; int par2[10008]; vector<pair<int, int>> result; int find1(int r) { if (par1[r] == r) return r; else return par1[r] = find1(par1[r]); } int find2(int r) { if (par2[r] == r) return r; else return par2[r] = find2(par2[r]); } int main() { int i, j, t, n, m1, m2, p, q, res = 0, uu, vv, uu2, vv2; cin >> n >> m1 >> m2; for (i = 1; i <= n; i++) { par1[i] = i; par2[i] = i; } for (i = 0; i < m1; i++) { cin >> p >> q; uu = find1(p); vv = find1(q); par1[uu] = vv; } for (i = 0; i < m2; i++) { cin >> p >> q; uu2 = find2(p); vv2 = find2(q); par2[uu2] = vv2; } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { uu = find1(i); vv = find1(j); uu2 = find2(i); vv2 = find2(j); if ((uu != vv) && (uu2 != vv2)) { res++; par1[uu] = vv; par2[uu2] = vv2; result.push_back({i, j}); } } } cout << res << endl; for (i = 0; i < result.size(); i++) { cout << result[i].first << << result[i].second << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inf = 1e17 + 10; const int N = 1e4 + 10; const long long mod = 998244353; map<string, int> ml; int c[N], vis[N], a[N], t, n, m, x, y, k, dp[N][105]; char s[N]; int ex, ey, cnt; long long dist[N]; vector<pair<int, int> > v; map<int, int> mp; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]]++; } for (auto p : mp) v.push_back(p); if (v.size() == 2) return cout << n << endl, 0; dp[0][0] = 1; for (auto p : v) { int cost = p.first; int k = p.second; for (int i = N; i >= 0; i--) for (int j = 1; j <= n; j++) for (int t = 1; t <= k && t <= j && t * cost <= i; t++) dp[i][j] += dp[i - t * cost][j - t]; } int ans = 1; for (auto p : v) { int cost = p.first; int k = p.second; for (int i = 1; i <= k; i++) { if (dp[i * cost][i] == 1) ans = max(ans, i); } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<pair<int, int> > a; vector<int> b; scanf( %d , &n); for (int i = 0; i < n; i++) { int t; scanf( %d , &t); if (t & 1) { int spd = 0; if (t != 5) scanf( %d , &spd); a.push_back({t, spd}); } else { b.push_back(t); } } int res = 0; int spd = 0; vector<int> lim; lim.push_back(1e9); for (pair<int, int> p : a) { if (p.first == 1) { spd = p.second; while (spd > lim.back()) { lim.pop_back(); res++; } } if (p.first == 3) { if (spd <= p.second) lim.push_back(p.second); else res++; } if (p.first == 5) lim.push_back(1e9); } vector<bool> allowed; allowed.push_back(1); for (int p : b) { if (p == 2) { while (!allowed.back()) { allowed.pop_back(); res++; } } if (p == 4) allowed.push_back(1); if (p == 6) allowed.push_back(0); } printf( %d n , res); return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.