func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int Maxn = 3e5 + 10; const int lim = 1000; int prime[Maxn], M[Maxn], H[Maxn], cnt; bool vis[Maxn]; struct node { int v, c, l; } a[Maxn]; int cmp(node A, node B) { return A.l < B.l; } int n, k; int main() { int cnt = 0; vis[0] = vis[1] = 1; for (int i = 2; i <= 3e5; i++) { if (!vis[i]) prime[++cnt] = i; for (int j = 1; j <= cnt && i * prime[j] <= 3e5; j++) { vis[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } scanf( %d %d , &n, &k); for (int i = 1; i <= n; i++) scanf( %d %d %d , &a[i].v, &a[i].c, &a[i].l); sort(a + 1, a + n + 1, cmp); for (int i = 1; i <= n; i++) M[a[i].l] = i; int ans = 0, sum, top; for (int i = 1; i <= n; i++) { if (!M[i]) continue; for (int h = 0; h < lim; h++) { random_shuffle(a + 1, a + M[i] + 1); top = 0, sum = 0; for (int j = 1; j <= M[i]; j++) { bool _ = 1; for (int q = 0; q < top; q++) if (!vis[a[j].c + a[H[q]].c]) { _ = 0; break; } if (_) { H[top++] = j; sum += a[j].v; } } if (sum >= k) return 0 * printf( %d n , i); } } puts( -1 ); }
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; struct TreeNode { int L, R; pair<double, double> val; } tree[maxn << 2]; int a[maxn], b[maxn], n, q; void pushUp(int root) { tree[root].val.first = tree[root << 1].val.first * tree[root << 1 | 1].val.first; tree[root].val.second = tree[root << 1].val.second + tree[root << 1].val.first * tree[root << 1 | 1].val.second; } void build(int L, int R, int root) { tree[root].L = L; tree[root].R = R; if (L == R) { return; } int mid = (L + R) >> 1; build(L, mid, root << 1); build(mid + 1, R, root << 1 | 1); } void update(int pos, pair<double, double> val, int root) { int L = tree[root].L, R = tree[root].R; if (L == R) { tree[root].val = val; return; } int mid = (L + R) >> 1; if (pos <= mid) { update(pos, val, root << 1); } else { update(pos, val, root << 1 | 1); } pushUp(root); } pair<double, double> query(int ql, int qr, int root) { int L = tree[root].L, R = tree[root].R; if (L >= ql && R <= qr) return tree[root].val; int mid = (L + R) >> 1; pair<double, double> a = make_pair(1.0, 0.0), b = make_pair(1.0, 0.0), c; if (ql <= mid) { a = query(ql, qr, root << 1); } if (qr > mid) { b = query(ql, qr, root << 1 | 1); } c.first = a.first * b.first; c.second = a.second + a.first * b.second; return c; } int main() { cin >> n >> q; build(1, n, 1); for (int i = 1; i <= n; i++) { double x, y; cin >> x >> y; double p = x / y; update(i, make_pair((1 - p) / p, (1 - p) / p), 1); } while (q--) { int type; cin >> type; if (type == 1) { int i, x, y; scanf( %d%d%d , &i, &x, &y); double p = double(x) / y; update(i, make_pair((1.0 - p) / p, (1.0 - p) / p), 1); } else { int l, r; scanf( %d%d , &l, &r); pair<double, double> t = query(l, r, 1); double p = t.second; if (p < 1e20) printf( %.6lf n , 1.0 / (1 + p)); else cout << 0.0000000 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, minn = 1000; long long res = 0; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d%d , &a, &b); minn = min(minn, b); res += minn * a; } printf( %d , res); return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; bool mysort(pair<long long, long long> A, pair<long long, long long> B) { return A.second > B.second; } long long moduloMultiplication(long long a, long long b, long long mod) { long long res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; a = (2 * a) % mod; b >>= 1; } return res; } void solve() { long long n, k, i, j; cin >> n >> k; if (k == 0) { if (n % 2 == 0) cout << 0 n ; else cout << 1 n ; } else { if (k <= n) { if (n == 1) cout << n - k << n ; else { if ((n + k) % 2 == 0) { cout << 0 n ; } else { cout << 1 n ; } } } else { cout << k - n << n ; } } } int32_t main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { auto start = high_resolution_clock::now(); solve(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<seconds>(stop - start); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k, a, b, v, nr = 0; cin >> k >> a >> b >> v; while (a > 0) { ++nr; if (b + 1 >= k) { b = b + 1 - k; a = a - k * v; } else { a = a - (b + 1) * v; b = 0; } } cout << nr; return 0; }
#include <bits/stdc++.h> using namespace std; int m; char s[15]; int ans[1005]; bool num[15]; bool dfs(int suml, int sumr, int cot, int pre) { if (cot == m + 1) return true; if (cot & 1) { for (int i = 1; i <= 10; i++) if (num[i] && i != pre && suml + i > sumr) { ans[cot] = i; if (dfs(suml + i, sumr, cot + 1, i)) return true; } } else { for (int i = 1; i <= 10; i++) { if (num[i] && i != pre && sumr + i > suml) { ans[cot] = i; if (dfs(suml, sumr + i, cot + 1, i)) return true; } } } return false; } int main() { scanf( %s , s + 1); for (int i = 1; i <= 10; i++) if (s[i] == 1 ) num[i] = 1; scanf( %d , &m); if (!dfs(0, 0, 1, 0)) printf( NO n ); else { printf( YES n ); for (int i = 1; i <= m; i++) printf( %d , ans[i]); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; inline int qpow(int a, int p) { int ans = 1; while (p) { if (p & 1) ans = (long long)ans * a % MOD; a = (long long)a * a % MOD; p >>= 1; } return ans; } inline int add(const int& x, const int& y) { return x + y >= MOD ? x + y - MOD : x + y; } inline int dec(const int& x, const int& y) { return x < y ? x - y + MOD : x - y; } int rt[2][24], r[262144], l, lim; inline void init() { lim = 1 << l; for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)); } inline void NTT(int* a, int type) { for (int i = 0; i < lim; i++) if (i < r[i]) swap(a[i], a[r[i]]); for (int L = 0; L < l; L++) { int mid = 1 << L, len = mid << 1; long long Wn = rt[type][L + 1]; for (int s = 0; s < lim; s += len) for (int k = 0, w = 1; k < mid; k++, w = w * Wn % MOD) { int x = a[s + k], y = (long long)w * a[s + mid + k] % MOD; a[s + k] = add(x, y), a[s + mid + k] = dec(x, y); } } if (type) { int t = qpow(lim, MOD - 2); for (int i = 0; i < lim; i++) a[i] = (long long)a[i] * t % MOD; } } int fac[262144], finv[262144]; int f[262144], g[262144], t[262144]; void solve(int n) { if (n == 0) return (void)(f[0] = 1); int m = n >> 1; solve(m); for (l = 0; (1 << l) <= (m << 1); l++) ; init(); for (int i = 0; i <= m; i++) t[i] = f[i], f[i] = (long long)f[i] * fac[i] % MOD; g[m] = 1; for (int i = m - 1; i >= 0; i--) g[i] = (long long)g[i + 1] * m % MOD; for (int i = 0; i <= m; i++) g[i] = (long long)g[i] * finv[m - i] % MOD; for (int i = m + 1; i < lim; i++) g[i] = 0; NTT(f, 0), NTT(g, 0); for (int i = 0; i < lim; i++) g[i] = (long long)f[i] * g[i] % MOD; NTT(g, 1); for (int i = 0; i <= m; i++) g[i] = (long long)g[i + m] * finv[i] % MOD; for (int i = m + 1; i < lim; i++) g[i] = 0; for (int i = 0; i <= m; i++) f[i] = t[i]; for (int i = m + 1; i < lim; i++) f[i] = 0; NTT(f, 0), NTT(g, 0); for (int i = 0; i < lim; i++) f[i] = (long long)f[i] * g[i] % MOD; NTT(f, 1); if (n & 1) { for (int i = n; i >= 1; i--) f[i] = ((n - 1ll) * f[i] + f[i - 1]) % MOD; f[0] = (n - 1ll) * f[0] % MOD; } } int main() { rt[0][23] = qpow(3, 119), rt[1][23] = qpow(rt[0][23], MOD - 2); for (int i = 22; i >= 0; i--) { rt[0][i] = (long long)rt[0][i + 1] * rt[0][i + 1] % MOD; rt[1][i] = (long long)rt[1][i + 1] * rt[1][i + 1] % MOD; } int n, a, b; scanf( %d%d%d , &n, &a, &b); --n, --a, --b; if (a < 0 || b < 0 || n < a + b) return puts( 0 ), 0; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % MOD; finv[n] = qpow(fac[n], MOD - 2); for (int i = n - 1; i >= 0; i--) finv[i] = (long long)finv[i + 1] * (i + 1) % MOD; solve(n); int ans = (long long)f[a + b] * fac[a + b] % MOD * finv[a] % MOD * finv[b] % MOD; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; char s[600010]; int a[600010]; int main() { int n; cin >> n >> s + 1; for (int i = n + 1; i < 2 * n; i++) s[i] = s[i - n]; for (int i = 1; i < 2 * n; i++) { a[i] = a[i - 1] + (s[i] == ( ? 1 : -1); } if (a[n] != 0) { cout << 0 << endl; cout << 1 1 << endl; return 0; } int cnt = 0, minn = 0; for (int i = 1; i <= n; i++) { minn = min(minn, a[i]); } int now = 0; for (int i = 1; i <= n; i++) { if (a[i] == minn) cnt++, now = i; } int ans = cnt; int l = 1, r = 1; int nowl = now, tmp = 0; for (int i = now; i <= now + n - 1; i++) { if (a[i] < minn + 2) { if (cnt + tmp > ans) { ans = tmp + cnt; l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1; } nowl = i, tmp = 0; } else if (a[i] == minn + 2) tmp++; } tmp = 0, nowl = now; for (int i = now; i <= now + n; i++) { if (i == now + n || a[i] == minn) { if (tmp > ans) { ans = tmp; l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1; } nowl = i, tmp = 0; } else if (a[i] == minn + 1) tmp++; } cout << ans << endl; cout << l << << r << endl; }
#include <bits/stdc++.h> using namespace std; inline const char nl = n ; inline const long long MOD = 1000000007; inline const long double pi = 3.141592653589793238; struct query { int a, b; } q[105]; bool vis[105]; int n, cnt; void dfs(int x) { vis[x] = 1; for (int i = 1; i <= cnt; i++) { query y = q[i]; if (vis[i]) continue; if ((y.a < q[x].a && q[x].a < y.b) || (y.a < q[x].b && q[x].b < y.b)) dfs(i); } } int main() { cin >> n; while (n--) { int op, x, y; cin >> op >> x >> y; if (op == 1) q[++cnt].a = x, q[cnt].b = y; if (op == 2) { for (int i = 1; i <= 100; i++) vis[i] = 0; dfs(x); if (vis[y]) cout << YES n ; else cout << NO n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; ifstream inp( inp.txt ); ofstream out( out.txt ); const int N = 1e5 + 5; const long long B = 73; int builtin_popcount(int &mask) { int kol = 0; for (int bit = 0; bit < 20; bit++) kol += int(bool(mask & (1 << bit))); return kol; } signed main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a; cin >> a; int up = (1 << 20); vector<int> dp(up, 0); vector<int> masks; for (int i = 0; i < ((int)a.size()); i++) { int mask = 0; for (int j = i; j < min(((int)a.size()), i + 20); j++) { int bit = a[j] - a ; if (mask & (1 << bit)) break; mask |= (1 << bit); masks.push_back(mask); dp[mask] = builtin_popcount(mask); } } for (int mask = 1; mask < up; mask++) for (int bit = 0; bit < 20; bit++) if (mask & (1 << bit)) dp[mask] = max(dp[mask], dp[mask ^ (1 << bit)]); int ans = 0; for (auto &mask : masks) ans = max(ans, builtin_popcount(mask) + dp[mask ^ (up - 1)]); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return + s + ; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } string to_string(vector<bool> v) { bool first = true; string res = { ; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += , ; } first = false; res += to_string(v[i]); } res += } ; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ; for (size_t i = 0; i < N; i++) { res += static_cast<char>( 0 + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto& x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + ) ; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + , + to_string(get<3>(p)) + ) ; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } class dsu { public: vector<int> p; int n; dsu(int _n) : n(_n) { p.resize(n); iota(p.begin(), p.end(), 0); } inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); } inline bool unite(int x, int y) { x = get(x); y = get(y); if (x != y) { p[x] = y; return true; } return false; } }; const int MAX = 100010; int bound[MAX]; int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; assert(1 <= tt && tt <= 1000); int x = 0; while (tt--) { int n; cin >> n; assert(n >= 1); vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); for (int i = 0; i < n - 1; i++) { assert(v[i] < v[i + 1]); } assert(1 <= v[0]); assert(v[n - 1] <= 100000); { int ptr = -1; for (int val = 0; val < MAX; val++) { if (ptr + 1 < n && v[ptr + 1] == val) { ++ptr; } bound[val] = ptr; } } vector<vector<int>> who(1, vector<int>(n, -1)); vector<dsu> d(1, dsu(n + 1)); vector<bool> seen; for (int i = n - 1; i >= 0; i--) { int start = 0; int j = 0; while (start < i) { if (j == (int)who.size()) { who.emplace_back(n, -1); d.emplace_back(n + 1); } int finish = i - 1; int tmp = who[j][i]; if (tmp != -1) { int val = v[i] - (v[tmp] - v[i]); finish = (val <= 0 ? -1 : bound[val]); } if (start <= finish) { int pos = d[j].get(start); while (pos <= finish) { who[j][pos] = i; d[j].unite(pos, pos + 1); pos = d[j].get(pos); } start = finish + 1; } ++j; } int g = 0; while (g < (int)who.size() && who[g][i] != -1) { ++g; } if (g >= (int)seen.size()) { seen.resize(g + 1); } seen[g] = true; } int g = 0; while (g < (int)seen.size() && seen[g]) { ++g; } x ^= g; 42; } cout << (x > 0 ? YES : NO ) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > allModify; class Polygon { private: int n; vector<int> label; vector<set<int> > adj; void splitPolygon(Polygon &a, Polygon &b, int x, int y) const { if (x > y) return (splitPolygon(a, b, y, x)); for (int i = (x), _b = (y); i <= _b; i = i + 1) a.label.push_back(label[i]); for (int i = (0), _b = (x); i <= _b; i = i + 1) b.label.push_back(label[i]); for (int i = (y), _b = (n - 1); i <= _b; i = i + 1) b.label.push_back(label[i]); a.n = a.label.size(); b.n = b.label.size(); a.adj.assign(a.n, set<int>()); b.adj.assign(b.n, set<int>()); for (int i = 0, _n = (n); i < _n; i = i + 1) for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) { int j = *it; Polygon &cur = (x <= i && i <= y && x <= j && j <= y) ? a : b; int idi = lower_bound(cur.label.begin(), cur.label.end(), label[i]) - cur.label.begin(); int idj = lower_bound(cur.label.begin(), cur.label.end(), label[j]) - cur.label.begin(); cur.adj[idi].insert(idj); cur.adj[idj].insert(idi); } int idx, idy; idx = lower_bound(a.label.begin(), a.label.end(), label[x]) - a.label.begin(); idy = lower_bound(a.label.begin(), a.label.end(), label[y]) - a.label.begin(); a.adj[idx].insert(idy); a.adj[idy].insert(idx); idx = lower_bound(b.label.begin(), b.label.end(), label[x]) - b.label.begin(); idy = lower_bound(b.label.begin(), b.label.end(), label[y]) - b.label.begin(); b.adj[idx].insert(idy); b.adj[idy].insert(idx); } int prevInSet(const set<int> &s, set<int>::iterator it) const { if (it == s.begin()) it = s.end(); it--; return (*it); } int nextInSet(const set<int> &s, set<int>::iterator it) const { it++; if (it == s.end()) it = s.begin(); return (*it); } bool haveEdge(int i, int j) const { return (adj[i].find(j) != adj[i].end()); } pair<int, int> tryChange(int i, set<int>::iterator it) const { int x = prevInSet(adj[i], it); int y = nextInSet(adj[i], it); return (make_pair(x, y)); } void fixChange(int u, int v, int x, int y) { adj[u].erase(v); adj[v].erase(u); adj[x].insert(y); adj[y].insert(x); allModify.push_back(make_pair(label[u], label[v])); } void debug(void) const { printf( POLYGON of %d n , n); printf( LABEL: ); for (int i = 0, _n = (n); i < _n; i = i + 1) printf( %d , label[i]); printf( n ); for (int i = 0, _n = (n); i < _n; i = i + 1) { printf( adj of %d: , i); for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) printf( %d , *it); printf( n ); } printf( ___END___ n ); } public: Polygon() { n = 0; } Polygon(int n) { this->n = n; for (int i = 0, _n = (n); i < _n; i = i + 1) label.push_back(i + 1); adj.assign(n, set<int>()); for (int i = 0, _n = (n); i < _n; i = i + 1) { adj[i].insert((i + 1 + n) % n); adj[i].insert((i - 1 + n) % n); } } void input(void) { for (int love = 0, _n = (n - 3); love < _n; love = love + 1) { int x, y; scanf( %d%d , &x, &y); x--; y--; adj[x].insert(y); adj[y].insert(x); } } void modify(const Polygon &goal) { if (n < 4) return; for (int i = 0, _n = (n); i < _n; i = i + 1) for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) if (*it != (i + 1) % n && *it != (i - 1 + n) % n && goal.haveEdge(i, *it)) { int j = *it; Polygon A, B, goalA, goalB; splitPolygon(A, B, i, j); goal.splitPolygon(goalA, goalB, i, j); A.modify(goalA); B.modify(goalB); return; } for (int i = 0, _n = (n); i < _n; i = i + 1) for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) if (*it != (i + 1) % n && *it != (i - 1 + n) % n) { pair<int, int> tmp = tryChange(i, it); if (goal.haveEdge(tmp.first, tmp.second)) { int j = *it; fixChange(i, j, tmp.first, tmp.second); Polygon A, B, goalA, goalB; splitPolygon(A, B, tmp.first, tmp.second); goal.splitPolygon(goalA, goalB, tmp.first, tmp.second); A.modify(goalA); B.modify(goalB); return; } } int chs = -1; for (int i = 0, _n = (n); i < _n; i = i + 1) if (goal.adj[i].size() < 3) if (chs < 0 || adj[i].size() < adj[chs].size()) chs = i; int u = chs; int x = (u - 1 + n) % n; int y = (u + 1 + n) % n; while (adj[u].size() > 2) { for (__typeof((adj[u]).begin()) it = (adj[u]).begin(); it != (adj[u]).end(); it++) if (*it != x && *it != y) { pair<int, int> tmp = tryChange(u, it); fixChange(u, *it, tmp.first, tmp.second); break; } } Polygon A, B, goalA, goalB; splitPolygon(A, B, x, y); goal.splitPolygon(goalA, goalB, x, y); A.modify(goalA); B.modify(goalB); } }; void process(void) { int n; scanf( %d , &n); Polygon src(n), des(n); src.input(); des.input(); src.modify(des); printf( %d n , (int)allModify.size()); for (__typeof((allModify).begin()) it = (allModify).begin(); it != (allModify).end(); it++) printf( %d %d n , it->first, it->second); } int main(void) { process(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 500 + 10; const int pN = 1e6; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void add(long long &x, long long y) { x += y; x %= MOD; } int u, v, n, m; void Solve(int x, int y, int z) { int row = n, cul = m; int nx, ny; for (int i = 1; i <= x; i++) { nx = v; ny = row - u + 1; u = nx; v = ny; swap(row, cul); } for (int i = 1; i <= y; i++) { nx = u; ny = cul - v + 1; u = nx; v = ny; } for (int i = 1; i <= z; i++) { nx = cul - v + 1; ny = u; u = nx; v = ny; swap(row, cul); } cout << u << << v << endl; } int main() { int x, y, z, p; cin >> n >> m >> x >> y >> z >> p; x %= 4; y %= 2; z %= 4; for (int i = 0; i < p; i++) { cin >> u >> v; Solve(x, y, z); } return 0; }
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793238462643383; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int Ceil(long long int a, long long int b) { if (a % b == 0) return a / b; else return a / b + 1; } const int MAX = 400009; const int MOD = 1e9 + 7; const int inf = 1e9 + 10; long long int deg[MAX], vis[MAX], dep[MAX]; vector<long long int> adj[MAX]; void dfs(int i, int d) { vis[i] = 1; dep[i] = d++; for (int j = 0; j < ((int)((adj[i]).size())); ++j) { int u = adj[i][j]; if (!vis[u]) dfs(u, d); } } priority_queue<pair<long long int, pair<long long int, long long int> > > pq; set<pair<long long int, pair<long long int, long long int> > > st; vector<long long int> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < (n); ++i) { int p; cin >> p; p--; if (p == -1) continue; adj[i].push_back(p), adj[p].push_back(i); deg[i]++, deg[p]++; } dfs(0, 1); (memset((vis), 0, sizeof(vis))); for (int i = 0; i < (n); ++i) if (deg[i] % 2 == 0) pq.push((make_pair((dep[i]), ((make_pair((i), (deg[i]))))))); long long int cnt = 0; while (!pq.empty()) { pair<long long int, pair<long long int, long long int> > c = pq.top(); pq.pop(); if (st.find(c) != st.end()) { st.erase(c); continue; } int i = c.second.first; vis[i] = 1; cnt++; ans.push_back(i); for (int j = 0; j < ((int)((adj[i]).size())); ++j) { int u = adj[i][j]; if (vis[u]) continue; deg[u]--; if (deg[u] % 2 == 0) pq.push((make_pair((dep[u]), ((make_pair((u), (deg[u]))))))); else st.insert((make_pair((dep[u]), ((make_pair((u), (deg[u] + 1))))))); } } if (cnt == n) { cout << YES n ; for (auto&(it) : (ans)) cout << it + 1 << n ; } else cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; long n, m, a, b, ar[200][200], ans; long arp[10000], fl, l; void dfs(long v, long par) { arp[v] = 1; l++; for (int i = 1; i <= n; i++) if (ar[v][i] == 1) { if (i == par) continue; if (arp[i] == 1) { fl = 1; return; } dfs(i, v); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; ar[a][b] = ar[b][a] = 1; } for (int i = 1; i <= n; i++) if (arp[i] == 0) { fl = 0; l = 0; dfs(i, -1); if (fl) if (l % 2) ++ans; } n -= ans; if (n % 2) ++ans; cout << ans << endl; cin.get(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; int S[2][500005 * 4], pre[500005], suf[500005]; long long hpr1[3][500005], hpr2[3][500005], hsf1[3][500005], hsf2[3][500005], pww[3][500005]; int n, m, k; long long pr[3] = {79, 103, 211}; bool flag; char a[500005], b[500005]; vector<int> sss[500005], ppp[500005]; int get(int node, int bas, int son, int x, int y, int w) { if (bas > y || son < x) return (w ? -1 : 1900000000); if (bas >= x && son <= y) { if (bas == son) { if (S[w][node]) { flag = true; return bas; } return 1900000000; } if (w) { if (S[w][node * 2 + 1]) return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w); else if (S[w][node * 2]) return get(node * 2, bas, ((bas + son) / 2), x, y, w); else return -1; } else { if (S[w][node * 2]) return get(node * 2, bas, ((bas + son) / 2), x, y, w); else if (S[w][node * 2 + 1]) return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w); else return 1900000000; } } if (w) { int res = get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w); if (flag) return res; return get(node * 2, bas, ((bas + son) / 2), x, y, w); } else { int res = get(node * 2, bas, ((bas + son) / 2), x, y, w); if (flag) return res; return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w); } } void up(int node, int bas, int son, int x, int val, int w) { if (bas > x || son < x) return; if (bas == x && son == x) { S[w][node] = val; return; } up(node * 2, bas, ((bas + son) / 2), x, val, w); up(node * 2 + 1, ((bas + son) / 2) + 1, son, x, val, w); S[w][node] = S[w][node * 2] + S[w][node * 2 + 1]; } void build(int node, int bas, int son, int w) { if (bas == son) { S[w][node] = 1; return; } build(node * 2, bas, ((bas + son) / 2), w); build(node * 2 + 1, ((bas + son) / 2) + 1, son, w); S[w][node] = S[w][node * 2] + S[w][node * 2 + 1]; } pair<pair<long long, long long>, long long> suf2(int bas, int son) { return {{hsf2[0][son] - hsf2[0][bas + 1] * pww[0][bas - son + 1], hsf2[1][son] - hsf2[1][bas + 1] * pww[1][bas - son + 1]}, hsf2[2][son] - hsf2[2][bas + 1] * pww[2][bas - son + 1]}; } pair<pair<long long, long long>, long long> suf1(int bas, int son) { return {{hsf1[0][son] - hsf1[0][bas + 1] * pww[0][bas - son + 1], hsf1[1][son] - hsf1[1][bas + 1] * pww[1][bas - son + 1]}, hsf1[2][son] - hsf1[2][bas + 1] * pww[2][bas - son + 1]}; } pair<pair<long long, long long>, long long> pre2(int bas, int son) { return {{hpr2[0][son] - hpr2[0][bas - 1] * pww[0][son - bas + 1], hpr2[1][son] - hpr2[1][bas - 1] * pww[1][son - bas + 1]}, hpr2[2][son] - hpr2[2][bas - 1] * pww[2][son - bas + 1]}; } pair<pair<long long, long long>, long long> pre1(int bas, int son) { return {{hpr1[0][son] - hpr1[0][bas - 1] * pww[0][son - bas + 1], hpr1[1][son] - hpr1[1][bas - 1] * pww[1][son - bas + 1]}, hpr1[2][son] - hpr1[2][bas - 1] * pww[2][son - bas + 1]}; } void hashsuf() { for (int i = n; i >= 1; i--) { for (int j = 0; j < 3; j++) { hsf1[j][i] = hsf1[j][i + 1] * pr[j] + a[i] - a + 1; } } for (int i = m; i >= 1; i--) { for (int j = 0; j < 3; j++) { hsf2[j][i] = hsf2[j][i + 1] * pr[j] + b[i] - a + 1; } } } void hashpre() { for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { hpr1[j][i] = hpr1[j][i - 1] * pr[j] + a[i] - a + 1; } } for (int i = 1; i <= m; i++) { for (int j = 0; j < 3; j++) { hpr2[j][i] = hpr2[j][i - 1] * pr[j] + b[i] - a + 1; } } } pair<int, int> getsuf(int from, int len) { flag = false; int pos = get(1, 1, n, from + len - 1, n - (k - len), 1); int be = pos - len + 1; int en = pos + (k - len); if (be < from || en > n) return {1900000000, 1900000000}; return {be, en}; } int main() { scanf( %d %d %d , &n, &m, &k); scanf( %s , a + 1); scanf( %s , b + 1); hashpre(); hashsuf(); pww[0][0] = pww[1][0] = pww[2][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) pww[j][i] = pww[j][i - 1] * pr[j]; for (int i = 1; i <= n; i++) { int bas = 1, son = min(m, n - i + 1); while (bas <= son) { if (pre1(i, i + ((bas + son) / 2) - 1) == pre2(1, ((bas + son) / 2))) bas = ((bas + son) / 2) + 1; else son = ((bas + son) / 2) - 1; } pre[i] = son; ppp[son].push_back(i); bas = 1, son = min(m, i); while (bas <= son) { if (suf1(i, i - ((bas + son) / 2) + 1) == suf2(m, m - ((bas + son) / 2) + 1)) bas = ((bas + son) / 2) + 1; else son = ((bas + son) / 2) - 1; } suf[i] = son; sss[son].push_back(i); } for (int i = 1; i <= n; i++) { if (pre[i] == m && m <= k) { int mxen = min(n, i + k - 1); int mnbe = max(1, i - k + 1); if (mxen - 2 * k + 1 >= 1) { printf( Yes n%d %d , mxen - 2 * k + 1, mxen - k + 1); return 0; } if (mnbe + 2 * k - 1 <= n) { printf( Yes n%d %d , mnbe, mnbe + k); return 0; } } } build(1, 1, n, 0); for (int i = 0; i <= min(k, m); i++) { for (int j = 0; j < (int)sss[m - i].size(); j++) up(1, 1, n, sss[m - i][j], 1, 1); flag = false; int pos = get(1, 1, n, k - i + 1, n, 0); for (int j = 0; j < (int)ppp[i].size(); j++) up(1, 1, n, ppp[i][j], 0, 0); if (m - i > k) continue; int en = pos + i - 1; int be = en - k + 1; if (en > n || be < 1) continue; pair<int, int> res = getsuf(en + 1, m - i); if (res.first < en + 1 || res.second > n) continue; printf( Yes n%d %d , be, res.first); return 0; } printf( No ); }
#include <bits/stdc++.h> int read() { int r = 0, t = 1, c = getchar(); while (c < 0 || c > 9 ) { t = c == - ? -1 : 1; c = getchar(); } while (c >= 0 && c <= 9 ) { r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * t; } const int N = 200010; int n; int c1, c2; std::vector<int> e[N]; int dfs(int u, int f) { int c = 0; for (auto v : e[u]) if (v != f) c += dfs(v, u); if (c) { c1++; c2 += c; return 0; } else return 1; } int main() { for (int T = (1), end_T = (read()); T <= end_T; T++) { n = read(); c1 = c2 = 0; for (int i = (1), end_i = (n); i <= end_i; i++) e[i].clear(); for (int i = (1), end_i = (n - 1); i <= end_i; i++) { int u = read(), v = read(); e[u].push_back(v); e[v].push_back(u); } dfs(1, 0); printf( %d n , c2 - (c1 - 1)); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<pair<long long, char> > v; vector<long long> freq(27, 0); string s2(s.length(), 1 ); vector<long long> vis(s.length() + 1, 0); for (long long i = 0; i < s.length(); i++) { freq[s[i] - a ]++; } for (long long i = 0; i < 27; i++) { char c = i + a ; if (freq[i] > 0) v.push_back({freq[i], c}); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); long long n = s.length(); long long cnt = 0; for (long long i = 2; i <= n; i += 2) { vis[i] = 1; cnt++; } for (long long i = 2; i <= n; i += 1) { bool add = false; if (!vis[i]) for (long long j = i; j <= n; j += i) { if (vis[j]) add = true; } if (!add) continue; for (long long j = i; j <= n; j += i) { if (!vis[j]) cnt++; vis[j] = 1; } } if (v[0].first < (cnt)) { cout << NO ; return 0; } long long i; for (i = 2; i <= n; i++) { if (!vis[i]) continue; s2[i - 1] = v[0].second; v[0].first--; vis[i] = 1; } long long k = 0; for (i = 1; i <= n; i++) { if (!vis[i]) { if (v[k].first == 0) { k++; } s2[i - 1] = v[k].second; v[k].first--; } } cout << YES n ; cout << s2; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 405; int a[MAXN], v[MAXN]; int n; void work(int x) { memset(v, 0, sizeof(v)); for (int i = 1; x != 0; i++) { v[i] = x / a[i]; x %= a[i]; } } int work2(int x) { int res = 0; for (int i = 1; x != 0; i++) { res += x / a[i]; x %= a[i]; } return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); int ans = -1; for (int i = 1; i < n; i++) { work(a[i] - 1); for (int j = i + 1; j <= n; j++) { int s = 0, m = 0; for (int k = i; k < j; k++) { s += v[k] * a[k]; m += v[k]; } s += (v[j] + 1) * a[j]; m += (v[j] + 1); if (work2(s) > m) { if (ans == -1 || s < ans) ans = s; } } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef int arr32[5000010]; arr32 x, rk, l, r; int n; long long ans; bool cmp(const int &a, const int &b) { return x[a] < x[b]; } void erase(int x) { r[l[x]] = r[x], l[r[x]] = l[x]; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { scanf( %d , x + i); rk[i] = i, l[i] = i - 1, r[i] = i + 1; } sort(rk + 1, rk + n + 1, cmp); for (int i = 1; i <= n - 2; ++i) { int p = rk[i]; if (l[p] == 0) ans += x[p], erase(p); else if (r[p] == n + 1) ans += x[p], erase(p); else ans += min(x[l[p]], x[r[p]]), erase(p); } cout << ans << endl; }
#include <bits/stdc++.h> int main() { int ss, x, y, flag = 0, kk, flag2 = 1; scanf( %d%d%d , &ss, &x, &y); if (x == 1 || x == ss) flag++; if (y == 1 || y == ss) flag++; if (x >= ss / 2 && x <= ss / 2 + 1 && y >= ss / 2 && y <= ss / 2 + 1) flag2 = 0; if (flag == 0) kk = 9; if (flag == 1) kk = 6; if (flag == 2) kk = 4; printf( %s n , (ss * ss >= kk * 2 && flag2) ? YES : NO ); }
#include <bits/stdc++.h> using namespace std; long long ans, n, k, a, b; int main() { scanf( %I64d %I64d , &n, &k); scanf( %I64d %I64d , &a, &b); long long t = n - 1; t *= a; if (k == 1) { printf( %I64d , t); return 0; } while (n > 1) { if (n % k == 0) ans += min(b, a * (n - (n / k))), n /= k; else { if (n - (n % k) > 0) ans += (a * (n % k)); else ans += (a * ((n % k) - 1)); n -= (n % k); } } ans = min(ans, t); printf( %I64d , ans); }
#include <bits/stdc++.h> using namespace std; const int N = 60 + 5; char a[N][N]; int l[N][N]; int r[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int m, n; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> (a[i] + 1); int f1 = 0, f2 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == A ) f1 = 1; if (a[i][j] == P ) f2 = 1; } } if (!f2) { printf( 0 n ); continue; } if (!f1) { printf( MORTAL n ); continue; } int visl = 0, visb = 0, visj = 0; if (a[1][1] == A || a[1][m] == A || a[n][1] == A || a[n][m] == A ) visj = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (i == 1 || j == 1 || i == n || j == m) { if (a[i][j] == A ) visb = 1; } if (a[i][j] == A ) l[i][j] = l[i][j - 1] + 1; } if (l[i][m] == m) visl = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == A ) r[i][j] = r[i - 1][j] + 1; if (r[n][j] == n) visl = 1; } } int visjl = 0; if (r[n][m] == n || l[n][m] == m || r[n][1] == n || l[1][m] == m) visjl = 1; if (visjl) printf( 1 n ); else if (visj) printf( 2 n ); else if (visl && visb) printf( 2 n ); else if (visb) printf( 3 n ); else printf( 4 n ); memset(l, 0, sizeof l); memset(r, 0, sizeof r); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 2; unsigned long long h[N]; int p[N]; bool ed[N], flg[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count()); unordered_map<unsigned long long, int> mp; int n, m = 0, i, j, k; unsigned long long tot = 0; cin >> n; for (i = 2; i <= n; i++) { if (!ed[i]) p[m++] = i, h[i] = rnd(); for (j = 0; j < m && (k = i * p[j]) <= n; j++) { ed[k] = 1; h[k] = h[i] ^ h[p[j]]; if (i % p[j] == 0) break; } if (1 ^ n - i & 1) tot ^= h[i]; } for (i = 3; i <= n; i++) h[i] ^= h[i - 1]; fill(flg + 1, flg + n + 1, 1); if (tot == 0) goto out; for (i = 1; i <= n; i++) if (tot == h[i]) { flg[i] = 0; goto out; } for (i = 1; i <= n; i++) { auto it = mp.find(tot ^ h[i]); if (it != mp.end()) { flg[i] = flg[it->second] = 0; goto out; } mp[h[i]] = i; } flg[n >> 1] = flg[2] = flg[n] = 0; out: cout << accumulate(flg + 1, flg + n + 1, 0) << n ; for (int i = 1; i <= n; i++) if (flg[i]) cout << i << ; }
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long a, b; cin >> a >> b; while (a != 0 && b != 0) { if (a >= 2 * b) { a = a % (2 * b); } else if (b >= 2 * a) { b = b % (a * 2); } else break; } cout << a << << b << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433; const int mod = 1000000007; const long long inf = 3e18; long long n, m, k; long long dp[5001][5001]; vector<long long> v; long long mkc(long long i, long long j) { if (j == 0 or i >= v.size()) { return 0; } if (dp[i][j] != -1) return dp[i][j]; long long ans = 0; ans = max(mkc(i + 1, j), v[i] + mkc(i + m, j - 1)); return dp[i][j] = ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; memset(dp, -1, sizeof(dp)); cin >> n >> m >> k; vector<long long> a(n), pre(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } pre[0] = a[0]; for (long long i = 1; i < n; i++) { pre[i] = pre[i - 1] + a[i]; } for (long long i = n - 1; i >= m; i--) { pre[i] = pre[i] - pre[i - m]; } for (long long i = m - 1; i < n; i++) { v.push_back(pre[i]); } cout << mkc(0, k); }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long x, y, n; cin >> x >> y >> n; long long rem = n % x; long long ans = n - rem; if (ans + y <= n) { cout << ans + y << n ; } else { ans -= (x - y); cout << ans << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T0, typename T1> bool chkmin(T0 &a, const T1 &b) { return a > b ? a = b, 1 : 0; } template <typename T0, typename T1> bool chkmax(T0 &a, const T1 &b) { return a < b ? a = b, 1 : 0; } const int oo = 0x3f3f3f3f, Max_val = 314000000; const int maxn = 100000, maxm = 100000; int deg[maxn + 5]; pair<pair<int, int>, int> all[maxm + 5]; int tmp[maxm + 5] = {0}; vector<int> yyt[maxn + 5]; bool vis[maxn + 5] = {0}; int Min[maxn + 5]; int Max[maxn + 5]; vector<int> zzj[maxm + 5]; vector<int> owaski[maxn + 5]; int find(const int &first) { if (vis[first]) return -2; if (Max[first]) return Max[first]; vis[first] = 1; Max[first] = -2; for (int i = (0), _end_ = ((int((owaski[first]).size()))); i != _end_; ++i) { const int &second = owaski[first][i]; int tmp = ::tmp[second]; for (int j = (0), _end_ = ((int((zzj[second]).size()))); j != _end_; ++j) { const int &k = zzj[second][j]; int tmp0 = find(k); if (tmp0 == -1) goto yyt; else if (tmp0 == -2) tmp = -2; else if (tmp != -2) chkmin(tmp += tmp0, Max_val); } if (tmp == -2) { Max[first] = -2; vis[first] = 0; return Max[first]; } Max[first] = max(Max[first], tmp); yyt:; } vis[first] = 0; return Max[first]; } int m, n; int main() { memset(Min, oo, sizeof Min); scanf( %d%d , &m, &n); for (int i = (0), _end_ = (m); i != _end_; ++i) { scanf( %d , &all[i].first.first), --all[i].first.first; static int nn; scanf( %d , &nn); all[i].first.second = tmp[i] = 0; owaski[all[i].first.first].push_back(i); for (int j = (0), _end_ = (nn); j != _end_; ++j) { static int first; scanf( %d , &first); if (first == -1) ++all[i].first.second, ++tmp[i]; else --first, ++all[i].second, yyt[first].push_back(i), zzj[i].push_back(first); } } for (int i = (0), _end_ = (m); i != _end_; ++i) if (!all[i].second) chkmin(Min[all[i].first.first], all[i].first.second); priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; for (int i = (0), _end_ = (n); i != _end_; ++i) if (Min[i] < oo) q.push(make_pair(Min[i], i)); while (!q.empty()) { int first = q.top().second; q.pop(); if (vis[first]) continue; vis[first] = 1; for (int i = (0), _end_ = ((int((yyt[first]).size()))); i != _end_; ++i) { const int &second = yyt[first][i]; all[second].first.second = min(all[second].first.second + Min[first], Max_val); if (!--all[second].second && chkmin(Min[all[second].first.first], all[second].first.second)) q.push( make_pair(Min[all[second].first.first], all[second].first.first)); } } memset(vis, 0, sizeof vis); memset(Max, 0, sizeof Max); for (int i = (0), _end_ = (n); i != _end_; ++i) if (Min[i] == oo) Min[i] = Max[i] = -1; for (int i = (0), _end_ = (n); i != _end_; ++i) { find(i); printf( %d %d n , Min[i], Max[i]); } return 0; }
#include <bits/stdc++.h> int INF = 2147483647; double INFD = 2147483647; double PI = 3.14159265359; using namespace std; struct Child { Child(long long v_, long long d_, long long p_) { gone = false; v = v_; d = d_; p = p_; } bool gone; long long v; long long d; long long p; }; int Enter(vector<Child>& kids, int base) { long long v = kids[base].v, deduction = 0; int gone_kid = -1; kids[base].gone = true; base++; while (deduction < v && base < kids.size()) { if (!kids[base].gone) { kids[base].p -= v - deduction; if (kids[base].p < 0 && gone_kid == -1) { gone_kid = base; } deduction++; } base++; } return gone_kid; } void print(vector<Child>& kids) { int i = 0; while (i < kids.size()) { Child c = kids[i]; cout << i + 1 << : << c.v << << c.d << << c.p; if (c.gone) { cout << -------- ; } cout << endl; i++; } } void Deduce(vector<Child>& kids, int base) { long long deduction = 0; int i = base; while (i < kids.size()) { if (!kids[i].gone) { kids[i].p -= deduction; if (kids[i].p < 0) { kids[i].gone = true; deduction += kids[i].d; } } i++; } } int main() { int n = 0; cin >> n; vector<Child> kids(n, Child(0, 0, 0)); vector<int> entered; int i = 0; while (i < n) { cin >> kids[i].v >> kids[i].d >> kids[i].p; i++; } i = 0; while (i < n) { if (!kids[i].gone) { entered.push_back(i); int gone_kid = Enter(kids, i); if (gone_kid != -1) { Deduce(kids, gone_kid); } } i++; } cout << entered.size() << endl; i = 0; while (i < entered.size()) { if (i) { cout << ; } cout << entered[i] + 1; i++; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 55; int res[N][N][N][N]; int tmp[N][N], dat[N][N]; string s[N]; int n, m, q, a, b, c, d; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; for (int i = 1; i <= n; i++) { cin >> s[i]; for (int j = 1; j <= m; j++) dat[i][j] = (s[i][j - 1] == 1 ); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) continue; tmp[i][j] = tmp[i - 1][j] + tmp[i][j - 1] - tmp[i - 1][j - 1] + dat[i][j]; } } for (int xa = 1; xa <= n; xa++) for (int ya = 1; ya <= m; ya++) for (int xb = 1; xb <= n; xb++) for (int yb = 1; yb <= m; yb++) { int sum = tmp[xb][yb] - tmp[xa - 1][yb] - tmp[xb][ya - 1] + tmp[xa - 1][ya - 1]; res[xa][ya][xb][yb] = (sum == 0); } for (int xa = n; xa >= 2; xa--) for (int ya = 1; ya <= m; ya++) for (int xb = xa; xb <= n; xb++) for (int yb = ya; yb <= m; yb++) res[xa - 1][ya][xb][yb] += res[xa][ya][xb][yb]; for (int xa = 1; xa <= n; xa++) for (int ya = m; ya >= 2; ya--) for (int xb = xa; xb <= n; xb++) for (int yb = ya; yb <= m; yb++) res[xa][ya - 1][xb][yb] += res[xa][ya][xb][yb]; for (int xa = 1; xa <= n; xa++) for (int ya = 1; ya <= m; ya++) for (int xb = xa; xb <= n; xb++) for (int yb = ya; yb <= m; yb++) res[xa][ya][xb + 1][yb] += res[xa][ya][xb][yb]; for (int xa = 1; xa <= n; xa++) for (int ya = 1; ya <= m; ya++) for (int xb = xa; xb <= n; xb++) for (int yb = ya; yb <= m; yb++) res[xa][ya][xb][yb + 1] += res[xa][ya][xb][yb]; while (q--) { cin >> a >> b >> c >> d; cout << res[a][b][c][d] << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long t, n, k, a[200005]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> k; for (long long i = 0; i < n; i++) cin >> a[i]; long long Min = k; for (long long i = k; i < n; i++) if (a[i] - a[i - k] < a[Min] - a[Min - k]) Min = i; cout << (a[Min] + a[Min - k]) / 2 << n ; } }
#include <bits/stdc++.h> using namespace std; long long ax, ay, bx, by, cx, cy; void check(long long xx, long long yy) { if (cx == 0 && cy == 0) { if (xx == 0 && yy == 0) { cout << YES ; exit(0); } else return; } if ((cx * (yy)-cy * (xx)) % (cx * cx + cy * cy) != 0 || (cx * (xx) + cy * (yy)) % (cx * cx + cy * cy) != 0) return; cout << YES ; exit(0); } int main() { cin >> ax >> ay >> bx >> by >> cx >> cy; check(bx - ax, by - ay); check(bx - ay, by + ax); check(bx + ax, by + ay); check(bx + ay, by - ax); cout << NO ; }
#include <bits/stdc++.h> using namespace std; const int N = 356438; int from[N], to[N], limL, nextp[N], headp[N], p[N], Lcnt[N], chcnt[N], num, a[N], b[N], sz, c[N]; bool taboo[N], boo[N]; long long limW, W[N], Wcnt[N], ans; inline int lowbit(int x) { return x & (-x); } inline int ask(int x) { int ans = 0; while (x) { ans += c[x]; x -= lowbit(x); } return ans; } inline void add(int x, int y) { while (x < N) { c[x] += y; x += lowbit(x); } } inline void addedge(int x, int y, long long z, int &tot) { from[tot] = x; to[tot] = y; W[tot] = z; nextp[tot] = headp[x]; headp[x] = tot; ++tot; } void dfs(int x) { a[sz] = x; ++sz; chcnt[x] = 1; for (int i = headp[x]; i != -1; i = nextp[i]) { if (taboo[i]) continue; int y = to[i]; if (p[x] != -1 && from[p[x]] == y) continue; p[y] = i; dfs(y); chcnt[x] += chcnt[y]; } } void dfs2(int x, bool b) { boo[x] = b; for (int i = headp[x]; i != -1; i = nextp[i]) { if (taboo[i]) continue; int y = to[i]; if (p[x] != -1 && from[p[x]] == y) continue; p[y] = i; Lcnt[y] = Lcnt[x] + 1; Wcnt[y] = Wcnt[x] + W[i]; dfs2(y, b); } } inline bool cmpb(int x, int y) { if (Wcnt[x] != Wcnt[y]) return Wcnt[x] < Wcnt[y]; if (Lcnt[x] != Lcnt[y]) return Lcnt[x] < Lcnt[y]; return boo[x] < boo[y]; } void work(int root) { p[root] = -1; Lcnt[root] = 0; Wcnt[root] = 0; sz = 0; dfs(root); if (sz == 1) return; int tempsz = 0, tempedge = -1; for (int i = 1; i < sz; ++i) { int x = a[i]; if (min(chcnt[x], sz - chcnt[x]) > tempsz) { tempsz = min(chcnt[x], sz - chcnt[x]); tempedge = p[x]; } } taboo[tempedge] = taboo[tempedge ^ 1] = true; Lcnt[from[tempedge]] = 0; Wcnt[from[tempedge]] = 0; p[from[tempedge]] = -1; dfs2(from[tempedge], true); Lcnt[to[tempedge]] = 0; Wcnt[to[tempedge]] = 0; p[to[tempedge]] = -1; dfs2(to[tempedge], false); for (int i = 0; i < sz; ++i) { int x = a[i]; if (boo[x]) { Lcnt[x] = limL - Lcnt[x] - 1; Wcnt[x] = limW - Wcnt[x] - W[tempedge]; } b[i] = x; } sort(b, b + sz, cmpb); for (int i = 0; i < sz; ++i) { int x = b[i]; if (boo[x]) { if (Lcnt[x] < 0) continue; ans += ask(Lcnt[x] + 1); } else { add(Lcnt[x] + 1, 1); } } for (int i = 0; i < sz; ++i) { int x = b[i]; if (boo[x]) { } else { add(Lcnt[x] + 1, -1); } } work(to[tempedge]); work(from[tempedge]); } int main() { int n; scanf( %d%d%I64d , &n, &limL, &limW); int tot = 0; memset(headp, -1, sizeof(headp)); for (int i = 2; i <= n; ++i) { int j; long long temp; scanf( %d %I64d , &j, &temp); addedge(i, j, temp, tot); addedge(j, i, temp, tot); } work(1); printf( %I64d n , ans); }
#include <bits/stdc++.h> using namespace std; const int K = 7; const int N = 2005; int a[K][N]; vector<int> s[N]; int d[N], n, k, p; void read() { cin >> n >> k; for (int i = 0; i < int(k); ++i) { for (int j = 0; j < int(n); ++j) { cin >> p; --p; if (i == 0) a[i][j] = p; else a[i][p] = j; } } } inline bool can_be_next(const vector<int>& a, const vector<int>& b) { assert(a.size() == b.size()); for (int i = 0; i < int(a.size()); ++i) { if (b[i] <= a[i]) return false; } return true; } void solve() { for (int i = 0; i < int(n); ++i) { s[i] = vector<int>(k - 1); for (int j = 1; j < k; ++j) s[i][j - 1] = a[j][a[0][i]]; } int res = 0; for (int i = 0; i < int(n); ++i) { d[i] = 1; for (int j = 0; j < int(i); ++j) if (can_be_next(s[j], s[i])) d[i] = max(d[j] + 1, d[i]); res = max(res, d[i]); } cout << res << endl; } int main() { read(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long ans = 0; for (int i = 1; i <= n; i++) ans += abs(a[i - 1] - i); cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; using point = pair<int, int>; int D(point a, point b, point c) { return a.first * (b.second - c.second) - a.second * (b.first - c.first) + (b.first * c.second - b.second * c.first); } int main() { int n, d; cin >> n >> d; vector<point> poligon; poligon.push_back(make_pair(0, d)); poligon.push_back(make_pair(d, 0)); poligon.push_back(make_pair(n, n - d)); poligon.push_back(make_pair(n - d, n)); poligon.push_back(poligon[0]); int k; cin >> k; vector<point> points(k); for (auto &[x, y] : points) cin >> x >> y; for (auto each : points) { int s1 = 0, s2 = 0; for (int i = 0; i < poligon.size() - 1; ++i) { int test = D(each, poligon[i], poligon[i + 1]); if (test > 0) s1++; else if (test < 0) s2++; } if (s2 == 0 or s1 == 0) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> prevSmall(vector<long long> &a, long long n) { vector<long long> res(n, -1); for (long long i = 1; i < n; ++i) { long long j = i - 1; while (j >= 0 && a[j] >= a[i]) j = res[j]; res[i] = j; } return res; } vector<long long> nextSmall(vector<long long> &a, long long n) { vector<long long> res(n, n); for (long long i = n - 2; i >= 0; --i) { long long j = i + 1; while (j < n && a[j] > a[i]) j = res[j]; res[i] = j; } return res; } vector<long long> prevLarge(vector<long long> &a, long long n) { vector<long long> res(n, -1); for (long long i = 1; i < n; ++i) { long long j = i - 1; while (j >= 0 && a[j] <= a[i]) j = res[j]; res[i] = j; } return res; } vector<long long> nextLarge(vector<long long> &a, long long n) { vector<long long> res(n, n); for (long long i = n - 2; i >= 0; --i) { long long j = i + 1; while (j < n && a[j] < a[i]) j = res[j]; res[i] = j; } return res; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (n); ++i) cin >> a[i]; long long ans = 0; auto pl = prevLarge(a, n), ps = prevSmall(a, n); auto nl = nextLarge(a, n), ns = nextSmall(a, n); for (long long i = 0; i < (n); ++i) { long long lc = i - pl[i], rc = nl[i] - i; ; ans += lc * rc * a[i]; lc = i - ps[i], rc = ns[i] - i; ; ans -= lc * rc * a[i]; } cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; int arr[int(1e7 + 100)]; void aum(int i) { if (i % 2) { arr[i / 2 + 1] += arr[i]; arr[i / 2] += arr[i]; } else arr[i / 2] += 2 * arr[i]; } int main() { ios_base::sync_with_stdio(0); set<int> conj; memset(arr, 0, sizeof arr); int n, m, l, ans = int(2 * 1e9 + 100), ptr = 0; cin >> n >> m; long long cnt = n, carry = 0, slices = m; for (int i = 0; i < n; ++i) { cin >> l; carry += l; ++arr[l]; } if (carry < m) { cout << -1 n ; return 0; } for (int i = int(1e7 + 100); cnt < m; --i) { if (arr[i]) { aum(i); cnt += arr[i]; arr[i] = 0; } } long long aux = 0; for (int i = int(1e7 + 100); i >= 1; --i) { if (arr[i]) { conj.insert(i); aux += arr[i]; if (aux >= m) { ans = i; break; } } } for (int i = int(1e7 + 100); i >= 1; --i) { if (arr[i]) { int go = (i % 2 == 1 ? 1 : 0); if (i / 2 + go <= *(conj.begin())) { break; } else { aum(i); if (i % 2) conj.insert(i / 2 + 1); conj.insert(i / 2); conj.erase(i); aux += arr[i]; while (1) { int val = (*conj.begin()); if (aux - arr[val] < m) break; aux -= arr[val]; conj.erase(conj.begin()); } } } } cout << max(1, *(conj.begin())) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int n, k; int value, ans; int bads[200010], maxDelay[200010], extraDelay[200010], a[200010], v[200010]; vector<int> T[200010], val[200010], pre[200010], post[200010]; void dfs(int x, int f) { bads[x] = 0; maxDelay[x] = 0; extraDelay[x] = 0; for (int i = 0; i < T[x].size(); ++i) { int y = T[x][i]; if (y != f) { dfs(y, x); bads[x] += bads[y]; if (bads[y] == 0) { maxDelay[x] += maxDelay[y]; } else { extraDelay[x] = max(extraDelay[x], maxDelay[y]); } } } if (a[x] < value) { bads[x]++; maxDelay[x] = 0; } else { maxDelay[x] += extraDelay[x] + 1; } } void dfs(int x, int f, int fBads, int fMaxDelay) { if (a[x] >= value) { int finalVal = maxDelay[x] - extraDelay[x]; finalVal += (fBads == 0 ? fMaxDelay + extraDelay[x] : max(fMaxDelay, extraDelay[x])); ans = max(ans, finalVal); } for (int i = 0; i < T[x].size(); ++i) { int y = T[x][i]; if (y == f) { val[x][i] = (fBads > 0 ? fMaxDelay : 0); } else { val[x][i] = (bads[y] > 0 ? maxDelay[y] : 0); } } int sz = T[x].size(); pre[x][0] = val[x][0]; post[x][sz - 1] = val[x][sz - 1]; for (int i = 1; i < sz; ++i) pre[x][i] = max(val[x][i], pre[x][i - 1]); for (int i = sz - 2; i >= 0; --i) post[x][i] = max(val[x][i], post[x][i + 1]); for (int i = 0; i < T[x].size(); ++i) { int y = T[x][i]; if (y == f) continue; if (a[x] < value) { dfs(y, x, bads[x] - bads[y] + fBads, 0); } else { int newBads = bads[x] - bads[y] + fBads; int newMaxDelay = maxDelay[x] - extraDelay[x] - (bads[y] == 0 ? maxDelay[y] : 0) + (fBads == 0 ? fMaxDelay : 0) + max((i == 0 ? 0 : pre[x][i - 1]), (i == sz - 1 ? 0 : post[x][i + 1])); dfs(y, x, newBads, newMaxDelay); } } } void reset() {} int check(int tval) { reset(); value = tval; ans = 0; dfs(1, 0); dfs(1, 0, 0, 0); return (ans >= k); } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; v[i] = a[i]; } sort(v + 1, v + n + 1); for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; T[a].push_back(b); T[b].push_back(a); } for (int i = 1; i <= n; ++i) { val[i].resize(T[i].size(), 0); pre[i].resize(T[i].size(), 0); post[i].resize(T[i].size(), 0); } int lo = 0, hi = n + 1; while (hi - lo > 1) { int mid = (lo + hi) / 2; if (check(v[mid])) lo = mid; else hi = mid; } cout << v[lo]; }
#include <bits/stdc++.h> using namespace std; bool dbg = 0; void bad(string mes = Impossible ) { cout << mes; exit(0); } template <typename T> string bin(T x, int st = 2) { string ans = ; while (x > 0) { ans += char( 0 + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? 0 : ans; } template <typename T> T input() { T ans = 0, m = 1; char c = ; while (!((c >= 0 && c <= 9 ) || c == - )) { c = getchar(); } if (c == - ) m = -1, c = getchar(); while (c >= 0 && c <= 9 ) { ans = ans * 10 + (c - 0 ), c = getchar(); } return ans * m; } template <typename T> void read(T& a) { a = input<T>(); } template <typename T> void read(T& a, T& b) { read(a), read(b); } template <typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template <typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } void reads(string& second) { string ans = ; char c = n ; while (c == n || c == ) c = getchar(); while (c != n && c != && c) ans += c, c = getchar(); second = ans; } const int inf = 1e9; const double eps = 1e-9; const int maxn = 1e6 + 10, base = 1e9 + 7; const long long llinf = 1e18 + 5; template <typename T> T binpow(T n, T second) { if (second <= 0) return 1LL; if (second % 2 == 0) { T b = binpow(n, second / 2); return (1LL * b * b) % base; } else { return (1LL * binpow(n, second - 1) * n) % base; } } int v[maxn], c[maxn]; int lg[maxn]; int mnSt[maxn][22]; int n, k; void buildMn() { for (int i = 0; i < n; i++) mnSt[i][0] = c[i]; for (int i = 1; i < 22; i++) for (int j = 0; j + (1 << i) <= n; j++) { mnSt[j][i] = min(mnSt[j][i - 1], mnSt[j + (1 << (i - 1))][i - 1]); } } int mxSt[maxn][22]; void buildMx() { for (int i = 0; i < n; i++) mxSt[i][0] = v[i]; for (int i = 1; i < 22; i++) for (int j = 0; j + (1 << i) <= n; j++) { mxSt[j][i] = max(mxSt[j][i - 1], mxSt[j + (1 << (i - 1))][i - 1]); } } int get_min(int l, int r) { int j = lg[r - l + 1]; return min(mnSt[l][j], mnSt[r - (1 << j) + 1][j]); } int get_max(int l, int r) { int j = lg[r - l + 1]; return max(mxSt[l][j], mxSt[r - (1 << j) + 1][j]); } int main() { for (int i = 2; i < maxn; i++) { lg[i] = lg[i / 2] + 1; } read(n, k); for (int i = 0; i < n; i++) read(v[i]), v[i] *= 100; for (int i = 0; i < n; i++) read(c[i]); buildMn(); buildMx(); long double res = 0; vector<int> mda; for (int i = 0; i < n; i++) { int l = i, r = n - 1; while (r - l > 1) { int m = (l + r) >> 1; if (get_min(i, m) < get_max(i, m)) r = m; else l = m; } int kek = -inf; for (int j = max(i, l - 5); j <= min(n - 1, l + 5); j++) { kek = max(kek, min(get_min(i, j), get_max(i, j))); } mda.push_back(kek); } sort((mda).begin(), (mda).end()); long double cf = (long double)n / k; int first = n - 1; for (int i = 0; first >= k - 1; i++, first--) { res += (long double)mda[i] / cf; cf *= (long double)n - i - 1; cf /= (long double)n - k - i; } cout << fixed << setprecision(20) << res; return 0; }
#include <bits/stdc++.h> using namespace std; const double _PI = 3.141592653589793; double n, m, k, ans = 0; signed main() { scanf( %lf %lf %lf , &n, &m, &k); ans += ((n * n * sqrt(3)) / 4) * (sqrt(n * n * 2 / 3)) / 3; ans += (m * m) / 3 * (sqrt(m * m / 2)); double S = (k * k * 5) / (4 * tan(_PI / 5)), r = S / (2.5 * k); ans += S * sqrt(3 * k * k / 4 - r * r) / 3; printf( %0.12lf , ans); }
#include <bits/stdc++.h> using namespace std; int main() { long long b, d, s, mx, sm = 0; cin >> b >> d >> s; mx = max(b, max(s, d)); if (mx != b) b++; if (mx != d) d++; if (mx != s) s++; sm += mx - b; sm += mx - d; sm += mx - s; cout << sm << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, h, x[2010], fall[2][2010]; double p, dp[2010][2010][2][2]; double solve(int l, int r, int fl, int fr) { if (l > r) return 0; if (dp[l][r][fl][fr] != -1) return dp[l][r][fl][fr]; dp[l][r][fl][fr] = 0; double res = 0; int nl = min(r, l + fall[0][l] - 1); if (nl == r) { if (!fr) res += (1 - p) * (x[r] - x[l] + min(h, x[r + 1] - x[r] - h)); else res += (1 - p) * (x[r] - x[l] + min(h, x[r + 1] - x[r])); } else { res += (1 - p) * solve(nl + 1, r, 1, fr); res += (1 - p) * (x[nl] - x[l] + h); } if (fl) res += p * min(h, x[l] - x[l - 1] - h); else res += p * min(h, x[l] - x[l - 1]); res += p * solve(l + 1, r, 0, fr); int nr = max(l, r - fall[1][r] + 1); if (nr == l) { if (fl) res += p * (x[r] - x[l] + min(h, x[l] - x[l - 1] - h)); else res += p * (x[r] - x[l] + min(h, x[l] - x[l - 1])); } else { res += p * solve(l, nr - 1, fl, 0); res += p * (x[r] - x[nr] + h); } if (!fr) res += (1 - p) * min(h, x[r + 1] - x[r] - h); else res += (1 - p) * min(h, x[r + 1] - x[r]); res += (1 - p) * solve(l, r - 1, fl, 1); res /= 2; return dp[l][r][fl][fr] = res; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cin >> n >> h >> p; for (int i = (1); i <= (n); i++) cin >> x[i]; x[++n] = 1000000000; x[++n] = -1000000000; sort(x + 1, x + n + 1); fall[1][1] = 1; for (int i = (2); i <= (n); i++) { if (x[i] - x[i - 1] < h) fall[1][i] = fall[1][i - 1] + 1; else fall[1][i] = 1; } fall[0][n] = 1; for (int i = (n - 1); i >= (1); i--) { if (x[i + 1] - x[i] < h) fall[0][i] = fall[0][i + 1] + 1; else fall[0][i] = 1; } for (int i = (1); i <= (n); i++) for (int j = (1); j <= (n); j++) for (int k = (0); k <= (1); k++) for (int l = (0); l <= (1); l++) dp[i][j][k][l] = -1; cout << fixed << setprecision(9) << solve(2, n - 1, 0, 1); return 0; }
#include <bits/stdc++.h> const int MAX_N = 300000; const int MAX_M = 300000; std::vector<int> types[1 + MAX_N]; std::vector<int> neighbours[1 + MAX_N]; int c; int colours[1 + MAX_M]; bool used[1 + MAX_M]; void dfs(int u = 1, int father = 0) { for (int type : types[u]) if (colours[type] != 0) used[colours[type]] = true; int i = 0, j = 1; while (i < (int)types[u].size()) { if (used[j]) { j++; } else { if (colours[types[u][i]] == 0) { colours[types[u][i]] = j; j++; } i++; } } for (int type : types[u]) used[colours[type]] = false; for (int v : neighbours[u]) if (v != father) { dfs(v, u); } } int main(void) { int n, m; scanf( %d%d , &n, &m); int c = 1; for (int i = 1; i <= n; i++) { int s; scanf( %d , &s); c = std::max(c, s); for (int j = 0; j < s; j++) { int type; scanf( %d , &type); types[i].push_back(type); } } for (int i = 0; i < n - 1; i++) { int u, v; scanf( %d%d , &u, &v); neighbours[u].push_back(v); neighbours[v].push_back(u); } dfs(); for (int i = 1; i <= m; i++) if (colours[i] == 0) colours[i] = 1; printf( %d n , c); for (int i = 1; i <= m; i++) printf( %d , colours[i]); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = (long int)3e5 + 5; long long power(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a, b, prev; cin >> a >> b; long long n = (sqrt(1 + 8 * (a + b)) - 1) / 2; std::vector<long long> d1, d2; for (long long i = n; i > 0; i--) { if (a - i >= 0) { d1.push_back(i); a -= i; } else if (b - i >= 0) { d2.push_back(i); b -= i; } } cout << d1.size() << endl; for (auto i : d1) cout << i << ; cout << endl; cout << d2.size() << endl; for (auto i : d2) cout << i << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int ni() { int a; scanf( %d , &a); return a; } double nf() { double a; scanf( %lf , &a); return a; } char sbuf[100005]; string ns() { scanf( %s , sbuf); return sbuf; } long long nll() { long long a; scanf( %lld , &a); return a; } template <class T> void out(T a, T b) { bool first = true; for (T i = a; i != b; ++i) { if (!first) printf( ); first = false; cout << *i; } printf( n ); } template <class T> void outl(T a, T b) { for (T i = a; i != b; ++i) { cout << *i << n ; } } int n, m; int moo(char c) { if (c <= 9 && c >= 0 ) return c - 0 ; return c - A + 10; } int a[6][6]; bool inside(int a) { return a >= 0 && a < 6; } char ss[1005]; vector<string> ans; void add(char c, int z, int num) { int j; sprintf(ss, %c%d n , c, z + 1); while (num) { if (c == L ) { int t = a[z][0]; for (j = (0); j < (((5))); ++j) a[z][j] = a[z][j + 1]; a[z][5] = t; } if (c == R ) { int t = a[z][5]; for (j = (0); j < (((5))); ++j) a[z][5 - j] = a[z][4 - j]; a[z][0] = t; } if (c == U ) { int t = a[0][z]; for (j = (0); j < (((5))); ++j) a[j][z] = a[j + 1][z]; a[5][z] = t; } if (c == D ) { int t = a[5][z]; for (j = (0); j < (((5))); ++j) a[5 - j][z] = a[4 - j][z]; a[0][z] = t; } ans.push_back(ss); --num; } } int main() { int i, j, k; for (i = (0); i < (((6))); ++i) { string s = ns(); for (j = (0); j < (((6))); ++j) a[i][j] = moo(s[j]); } startOver: int done = 0; for (i = (0); i < (((15))); ++i) { for (j = (0); j < (((15))); ++j) { int y = j; int x = i - j; if (inside(x) && inside(y)) { int xx = -1, yy = -1; int ii, jj; for (ii = (0); ii < ((6)); ++ii) for (jj = (0); jj < ((6)); ++jj) if (a[ii][jj] == y * 6 + x) xx = jj, yy = ii; ; if (x == 5 && y == 0) x = 5; if (xx == x && yy == y) { } else { bool needUp = 0; if (yy == 5 && xx == 5) { if (y == 5) { y = 4; add( U , x, 1); needUp = 1; } if (x < 4) add( L , 5, 4 - x); else if (x > 4) add( R , 5, 1); add( D , x, 5 - y); add( L , 5, 1); add( U , x, 5 - y); if (x != 5) add( R , 5, 5 - x); if (needUp) { add( D , x, 1); y = 5; } } else { if (yy == 5 && xx != 4) { yy = 4; add( U , xx, 1); needUp = 1; } if (xx < 4) add( L , 5, 4 - xx); else if (xx > 4) add( R , 5, 1); add( D , xx, 5 - yy); add( L , 5, 1); add( U , xx, 5 - yy); if (xx != 5) add( R , 5, 5 - xx); if (needUp) { add( D , xx, 1); yy = 5; } needUp = 0; if (y == 5) { y = 4; add( U , x, 1); needUp = 1; } if (x != 5) add( L , 5, 5 - x); add( D , x, 5 - y); add( R , 5, 1); add( U , x, 5 - y); if (x < 4) add( R , 5, 4 - x); else if (x > 4) add( L , 5, 1); if (needUp) { add( D , x, 1); y = 5; } } } if (a[y][x] != y * 6 + x) { printf( %d %d n , x, y); return 0; } ++done; } if (done == 34) { if (a[5][5] != 35) { add( L , 5, 3); goto startOver; } printf( %d n , ans.size()); for (i = (0); i < (((ans.size()))); ++i) printf( %s , ans[i].c_str()); return 0; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int n = 10, dir[4][2] = {{1, 0}, {0, 1}, {1, -1}, {1, 1}}; int main() { char b[n][n]; for (int i = 0; i < 10; i++) for (int j = 0; j < n; j++) scanf( %c , &b[i][j]); bool found = false; for (int i = 0; !found && i < n; i++) for (int j = 0; !found && j < n; j++) for (int k = 0; k < 4; k++) { int p = 0, q = 0; for (int l = 0; l < 5; l++) { int x = i + dir[k][0] * l, y = j + dir[k][1] * l; if (x < 0 || x >= n || y < 0 || y >= n) break; ; char c = b[x][y]; p += (c == X ), q += (c == . ); } if (p == 4 && q == 1) found = true; } if (found) printf( YES n ); else printf( NO n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_ROW = 2000 + 7; const int MAX_COLUMN = 200 + 7; const int BASE = 2807; const int MOD = 1e7 + 7; int n, m; char TableA[MAX_ROW][MAX_COLUMN], TableB[MAX_COLUMN][MAX_ROW]; int HashA[MAX_ROW][MAX_COLUMN], HashB[MAX_COLUMN][MAX_ROW], Pow2807[MAX_ROW], HashFinding[MOD]; int GetHash(const int& L, const int& R, const int H[]) { return (1LL * H[R] - 1LL * H[L - 1] * Pow2807[R - L + 1] + 1LL * MOD * MOD) % MOD; } int main() { ios::sync_with_stdio(false); cout.tie(0), cin.tie(0); Pow2807[0] = 1; for (int i = 1; i < MAX_ROW; ++i) Pow2807[i] = 1LL * Pow2807[i - 1] * BASE % MOD; scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) scanf( %s , TableA[i] + 1); for (int j = 1; j <= m; ++j) scanf( %s , TableB[j] + 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) HashA[i][j] = (1LL * HashA[i][j - 1] * BASE + TableA[i][j]) % MOD; for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j) HashB[i][j] = (1LL * HashB[i][j - 1] * BASE + TableB[i][j]) % MOD; for (int j = 1; j <= (n - m + 1); ++j) { int HashBase = 0; for (int k = j; k <= (j + m - 1); ++k) HashBase = (1LL * HashBase * Pow2807[m] + GetHash(1, m, HashA[k])) % MOD; HashFinding[HashBase] = j; } for (int j = 1; j <= (n - m + 1); ++j) { int HashTest = 0; for (int i = 1; i <= m; ++i) HashTest = (1LL * HashTest * Pow2807[m] + GetHash(j, j + m - 1, HashB[i])) % MOD; if (HashFinding[HashTest]) { cout << HashFinding[HashTest] << << j << n ; return 0; } } cout << Error 404! n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long aa[15]; long long bb[15]; for (int i = 0; i < 14; i++) { scanf( %lld , &aa[i]); bb[i] = aa[i]; } long long maxx = 0; for (int i = 0; i < 14; i++) { if (!aa[i]) continue; long long sum = 0; int c = aa[i] % 14; for (int k = 1; k <= c; k++) { if (i + k < 14) bb[k + i]++; else { bb[(i + k) % 14]++; } } for (int j = 0; j < 14; j++) { if (j != i) bb[j] += aa[i] / 14; else bb[j] = aa[i] / 14; if (bb[j] % 2 == 0) sum += bb[j]; } if (sum > maxx) maxx = sum; for (int j = 0; j < 14; j++) bb[j] = aa[j]; } cout << maxx << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimization( unroll-loops ) const int N = 1e5 + 5; const long long int mod = 1e9 + 7; const long long int Mod = 998244353; const long double Pi = acos(-1); const long long int Inf = 4e18; using namespace std; int n, m; vector<pair<int, int> > e; bool Check(int k) { vector<bool> rem(n + 1, false); vector<int> in[n + 1], out[n + 1], rank(n + 1, -1); for (int i = 0; i <= k; i++) { in[e[i].second].push_back(e[i].first); out[e[i].first].push_back(e[i].second); } int cnt_in = 0, cnt_out = 0, one = -1, last = -1; for (int i = 1; i <= n; i++) { if ((int)in[i].size() == 0) { cnt_in++; one = i; } if ((int)out[i].size() == 0) { cnt_out++; last = i; } } if (cnt_in * cnt_out != 1) return false; rank[1] = one; rank[n] = last; int cur = one; for (int i = 2; i <= n; i++) { int fone = 0, nxt = -1; for (auto u : out[cur]) { int cnt = 0; for (auto t : in[u]) { if (rem[t] == false) cnt++; } if (cnt == 1) { fone++; nxt = u; } } if (fone != 1) return false; else { rem[cur] = true; cur = nxt; rank[i] = cur; } } for (int i = 1; i <= n; i++) { if (rank[i] == -1) return false; } return true; } void TestCase() { cin >> n >> m; for (int i = 1, u, v; i <= m; i++) { cin >> u >> v; e.push_back({u, v}); } int lb = 0, ub = m - 1, ans = m; while (lb <= ub) { int mid = (lb + ub) / 2; if (Check(mid)) { ans = min(ans, mid); ub = mid - 1; } else lb = mid + 1; } ans == m ? cout << -1 : cout << ans + 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int T = 1; while (T--) { TestCase(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); vector<long long int> v; long long int n, i; cin >> n; if (n == 1) { cout << 1; return 0; } for (long long int i = 1; i * i <= n; ++i) { if (n % i == 0) { if (i * i == n) v.push_back(i); else { v.push_back(i); v.push_back(n / i); } } } sort(v.begin(), v.end()); if (v.size() == 2) cout << n; else { long long int flag = 0; if (v.size() == 3) { if (v[1] / v[0] != v[2] / v[1]) { cout << 1; } else cout << v[1]; } else { long long int ans = v[1] / v[0]; for (i = 1; i < v.size() - 1; i++) { if (v[i + 1] != ans * v[i]) { flag = 1; break; } } if (flag == 1) cout << 1; else { cout << v[1]; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; long long qpow(long long a, long long b) { long long res = 1; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } struct graph { int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz; void init() { memset(head, -1, sizeof(head)); } graph() { init(); } void push(int a, int b, int c) { nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++; } int& operator[](const int a) { return to[a]; } }; long long dist[maxn][40], fi[maxn][40], mi[maxn][40]; int to[maxn], w[maxn]; int main() { int n; long long k; cin >> n >> k; for (int i = 0; i < n; ++i) scanf( %d , &to[i]); for (int i = 0; i < n; ++i) { scanf( %d , &w[i]); dist[i][0] = w[i]; mi[i][0] = w[i]; fi[i][0] = to[i]; } for (int i = 1; i <= 39; ++i) { for (int j = 0; j < n; ++j) { dist[j][i] = dist[j][i - 1] + dist[fi[j][i - 1]][i - 1]; fi[j][i] = fi[fi[j][i - 1]][i - 1]; mi[j][i] = min(mi[j][i - 1], mi[fi[j][i - 1]][i - 1]); } } for (int i = 0; i < n; ++i) { long long mix = 1e18; long long ans = 0; long long res = k; int now = i; for (int j = 39; j >= 0; --j) { if (res >= (1ll << j)) { ans += dist[now][j]; mix = min(mix, mi[now][j]); now = fi[now][j]; res -= (1ll << j); } } printf( %lld %lld n , ans, mix); } return 0; }
#pragma GCC target ( avx2 ) #pragma GCC optimize ( O3 ) #pragma GCC optimize ( unroll-loops ) #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef priority_queue<pl, vector<pl>, greater<pl> > pq; template<class T, class S> ostream& operator<<(ostream &o, pair<T, S> p) { return o<< ( <<p.first<< , <<p.second<< ) ; } template<template<class, class...> class T, class... A> typename enable_if<!is_same<T<A...>, string>(), ostream&>::type operator<<(ostream &o, T<A...> V) { o<< [ ; for(auto a:V) o<<a<< , ; return o<< ] ; } // This is tuples, _p is helper template<ll i, class... T> typename enable_if<i==sizeof...(T)>::type _p(ostream& o, tuple<T...> t) {} template<ll i, class... T> typename enable_if<i<sizeof...(T)>::type _p(ostream& o, tuple<T...> t) { _p<i+1>(o << get<i>(t)<< , , t); } template<class... T> ostream& operator<<(ostream& o, tuple<T...> t) { _p<0>(o<< ( , t); return o<< ) ; } #define G(x) ll x; cin >> x; #define Gs(s) string s; cin >> s; #define Gd(x) ld x; cin >> x; #define F(i, l, r) for(ll i = l; i < (r); i++) #define F1(i, l, r) for(ll i = l; i >= (r); i--) #define Fi(i, s) for(auto i = (s).begin(); i != (s).end(); i++) #define L G(_t) F(_t1, 0, _t) #define H(ar, n) vector<ll> ar(n); F(_i, 0, n) cin >> ar[_i]; #define A first #define B second #define PA(ar, n) F(_i, 0, n) cout << (ar)[_i] << ; cout << endl; #define PB push_back #define V vector #define CASE cout << Case # << _t1 + 1 << : #define CC << << #define BE(ar) ar.begin(), ar.end() #define CO(x) cout << (x) << endl; #define D(x) ((x) - 0 ) #define EX(x) {cout << (x) << endl; return 0;} #define S(ar, val) memset(ar, val, sizeof(ar)); ll M = 1000000007; //998244353; ll inv(ll a, ll b){return 1<a ? b - inv(b%a,a)*b/a : 1;} ll gcd(ll x, ll y){if(y == 0) return x; return gcd(y, x % y); } pl neigh[4] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; #define N 0 int main() { ios_base::sync_with_stdio(0); cin.tie(0); L{ G(n) if(n == 2){ cout << 1 n2 1 n1 n2 1 << endl; continue; } if(n == 3){ cout << 5 n2 3 1 n2 n1 3 n3 2 << endl; continue; } ll perm[n]; perm[n-1] = 1; perm[0] = n-1; F1(i, n-2, (n-1)/2+2){ perm[i] = i; } perm[(n-1)/2] = n; perm[(n-1)/2+1]=2; F(i, 1, (n-1)/2){ perm[i] = i+2; } ll x = (n-1)*(n-1); ll c = 1; ll p = n-1; while(c < n-1){ if(c %2) p--; c++; x += p * p; } cout << x << endl; F(i, 0, n){ cout << perm[i] << ; } cout << endl; cout << n - 1 << endl; F1(i, n-1, (n-1)/2+2){ cout << i << << 1 << endl; } F(i, 1, (n-1)/2+2){ cout << i << << n << endl; } } }
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int mod = 1000000007; const int inf = 2012345678; const long long infl = 9012345678901234567; const double pi = 3.1415926535897932384626433832795; const int N = 20; const int M = 100010; char buf[M]; int a[M]; int dp[1 << N][N + 1]; int main() { freopen( input.txt , r , stdin); int n, m; scanf( %d%d , &(n), &(m)); for (int i = 0; i < (n); i++) { scanf( %s , buf); for (int j = 0; j < (m); j++) a[j] = 2 * a[j] + buf[j] - 0 ; } for (int i = 0; i < (m); i++) dp[a[i]][0]++; for (int i = 0; i < (m); i++) for (int j = 0; j < (n); j++) dp[a[i] ^ (1 << j)][1]++; for (int k = 2; k <= n; k++) { for (int i = 0; i < ((1 << n)); i++) { int sum = 0; for (int j = 0; j < (n); j++) { sum += dp[i ^ (1 << j)][k - 1]; } sum -= (n - k + 2) * dp[i][k - 2]; dp[i][k] = sum / k; } } int ans = inf; for (int i = 0; i < ((1 << n)); i++) { int s = 0; for (int k = 0; k <= (n); k++) { s += min(k, n - k) * dp[i][k]; } ans = min(ans, s); } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; char cur = s[0]; int count = 1; for (int i = 1; i < s.length(); i++) { if (cur == s[i]) { count++; if (count == 7) { cout << YES << endl; return 0; } } else { count = 1; cur = s[i]; } } cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int M = 1e9 + 7; const long long int N = 2e5 + 5; const long long int inf = 2e18; long long int mod(long long int x) { return (x % M); } long long int mod_minus(long long int a, long long int b) { long long int ans = (mod(a) - mod(b)); if (ans < 0) ans = mod(ans + M); return ans; } long long int mod_mul(long long int a, long long int b) { return mod(mod(a) * mod(b)); } long long int mod_add(long long int a, long long int b) { return mod(a + b); } long long int power(long long int a, long long int n) { if (n == 0) return 1; else if (n == 1) return a; long long int R = power(a, n / 2) % M; if (n % 2 == 0) { return mod(mod_mul(R, R)); } else { return mod(mod_mul(mod_mul(R, a), mod(R))); } } long long int mod_inv(long long int n) { return power(n, M - 2); } long long int mod_div(long long int a, long long int b) { long long int ans = mod(a); long long int b1 = power(b, M - 2); ans = mod(mod_mul(ans, b1)); return ans; } long long int fact_mod(long long int n) { vector<long long int> fact(n + 1); fact[0] = 1; for (long long int i = 1; i < n + 1; i++) { fact[i] = mod_mul(fact[i - 1], i); } return fact[n]; } long long int nCr_mod(long long int n, long long int r) { if (r == 0 || n == 0) return 1; long long int fac[n + 1]; fac[0] = 1; for (long long int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % M; return (fac[n] * mod_inv(fac[r]) % M * mod_inv(fac[n - r]) % M) % M; } int main() { long long int n; cin >> n; long long int i; queue<pair<long long int, long long int>> q; vector<pair<long long int, long long int>> v(n); for (i = 0; i < n; i++) { long long int x, y; cin >> x >> y; v[i] = {x, y}; if (x == 1) q.push({i, y}); } vector<pair<long long int, long long int>> ans; while (q.empty() == false) { pair<long long int, long long int> temp = q.front(); q.pop(); ans.push_back({temp}); long long int x = temp.second; v[x].first--; v[x].second = temp.first ^ v[x].second; if (v[x].first == 1) q.push({x, v[x].second}); } set<pair<long long int, long long int>> s; for (auto x : ans) { if (!s.count({x.first, x.second}) && !s.count({x.second, x.first})) s.insert({x.first, x.second}); } cout << s.size() << endl; for (auto x : s) cout << x.first << << x.second << endl; }
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; string words[100005]; int type[100005]; int gender[100005]; void reverse(string &c) { for (int i = (0); i < (int((c).size()) / 2); i++) { char tmp = c[i]; c[i] = c[int((c).size()) - i - 1]; c[int((c).size()) - i - 1] = tmp; } } int main() { string line; getline(cin, line); stringstream ss(line); int n = 0; while (true) { string word; ss >> word; if (ss.fail()) break; words[n++] = word; } for (int i = (0); i < (n); i++) reverse(words[i]); for (int i = (0); i < (n); i++) { if (words[i].find( soil ) == 0) { type[i] = 0; gender[i] = 0; continue; }; if (words[i].find( alail ) == 0) { type[i] = 0; gender[i] = 1; continue; }; if (words[i].find( rte ) == 0) { type[i] = 1; gender[i] = 0; continue; }; if (words[i].find( arte ) == 0) { type[i] = 1; gender[i] = 1; continue; }; if (words[i].find( sitini ) == 0) { type[i] = 2; gender[i] = 0; continue; }; if (words[i].find( setini ) == 0) { type[i] = 2; gender[i] = 1; continue; }; gender[i] = -1; } if (n == 1 && gender[0] != -1) { cout << YES n ; return 0; } bool SameGender = true; for (int i = (1); i < (n); i++) if (gender[i] != gender[0]) SameGender = false; if (!SameGender || gender[0] == -1) { cout << NO n ; return 0; } int p1, p2, p3; p1 = 0; while (p1 < n && type[p1] == 0) ++p1; p2 = p1; while (p2 < n && type[p2] == 1) ++p2; p3 = p2; while (p3 < n && type[p3] == 2) ++p3; if (p3 == n && p2 == p1 + 1) cout << YES n ; else cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; int a1[maxn], a2[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, k; cin >> n >> k; long long ans = (n + 1) * n / 2; for (int i = 1; i <= n; i++) a1[i] = i; for (int i = 1; i <= n; i++) a2[i] = i; if (ans > k) { cout << -1 << n ; return 0; } long long up = n / 2; long long now = n - 1; for (int i = 1; i <= up; i++) { if (ans + now <= k) { ans += now; swap(a2[i], a2[i + now]); } else { swap(a2[i], a2[i + k - ans]); ans = k; break; } now -= 2; } cout << ans << n ; for (int i = 1; i <= n; i++) cout << a1[i] << (i == n ? n : ); for (int i = 1; i <= n; i++) cout << a2[i] << (i == n ? n : ); return 0; }
#include <bits/stdc++.h> using namespace std; long long total = 0; struct c { int can; int tru; }; int main() { double a, b, c; scanf( %lf%lf%lf , &a, &b, &c); printf( %.100f n , a * (b) / (b + c)); }
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; using ll = long long; using ld = long double; constexpr int nax = 1000 * 1000 + 105; constexpr int infty = 1000 * 1000 * 1000 + 5; constexpr int mod = 1000 * 1000 * 1000 + 7; int main() { int n; scanf( %d , &n); set<int> s; for (int i = 0; i < n; i++) { int a; scanf( %d , &a); s.insert(a); } printf( %d n , n + 1 - (int)s.size()); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) #pragma GCC target( avx2 ) #pragma GCC optimize( Os ) using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string str; long long i, j, k, x = 0, y = 0, n, m, t, sum = 0, a = 0, b, l, c, d, id; cin >> n; set<long long> s; for (i = 0; i < n; i++) { cin >> a; sum += a; s.insert(a); } x = s.size(); if (x >= 4) cout << -1; else { if (x == 1) cout << 0; else if (x == 2) { a = *next(s.begin(), 0); b = *next(s.begin(), 1); if ((a + b) % 2 != 0) cout << b - a; else cout << (b - a) / 2; } else { a = *next(s.begin(), 0); b = *next(s.begin(), 1); c = *next(s.begin(), 2); t = c - b; if (a + c == b * 2) cout << t; else cout << -1; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int x, i; } a[100020], b[100020], c[100020]; bool f(node a, node b) { return a.x < b.x; } bool g(node a, node b) { return a.i < b.i; } int main() { int i, n, j; cin >> n; cout << YES n ; for (i = 1; i <= n; i++) { cin >> a[i].x, a[i].i = i, a[i].x += 2; } sort(a + 1, a + n + 1, f); for (i = 1; i <= n / 3; i++) b[i].i = c[i].i = a[i].i, b[i].x = i, c[i].x = a[i].x - i; for (i = 1 + n / 3; i <= (n + n) / 3; i++) b[i].i = c[i].i = a[i].i, c[i].x = i, b[i].x = a[i].x - i; for (i = 1 + (n + n) / 3, j = i; i <= n; i++) b[i].i = c[i].i = a[i].i, c[i].x = n - i + 1, b[i].x = a[i].x - c[i].x; sort(b + 1, b + n + 1, g); sort(c + 1, c + n + 1, g); for (i = 1; i <= n; i++) cout << b[i].x - 1 << ; cout << endl; for (i = 1; i <= n; i++) cout << c[i].x - 1 << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> g[1100]; map<long long, bool> visited; long long a, b; map<long long, long long> parent; void dfs(long long v) { visited[v] = 1; if (v > 1e9 || v == b) { return; } if (!visited[2 * v]) { parent[2 * v] = v; dfs(2 * v); } if (!visited[10 * v + 1]) { parent[10 * v + 1] = v; dfs(10 * v + 1); } } decltype(0) main() { ios::sync_with_stdio(0); cin >> a >> b; dfs(a); if (!visited[b]) { cout << NO n ; } else { vector<long long> ans; long long x = b; while (x != parent[a]) { ans.push_back(x); x = parent[x]; } cout << YES n << (long long)(ans.size()) << n ; for (long long i = (long long)(ans.size()) - 1; i >= 0; i--) { cout << ans[i] << ; } cout << n ; } }
#include <bits/stdc++.h> using namespace std; unsigned long long X, Y, sum, exor, l, m; int main() { cin >> sum >> exor; if (sum < exor || sum % 2 != exor % 2) { cout << -1 << endl; return 0; } X = (sum - exor) / 2; Y = sum - X; cout << X << endl << Y << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> ans; int row[5], col[20]; vector<int> v[3]; bool vis[20][11][11][11][11][11][1 << 5]; bool solve(int x, int r1, int r2, int r3, int r4, int r5, int mask) { if (x == m) { if (r1 + r2 + r3 + r4 + r5) return false; return true; } bool &ret = vis[x][r1][r2][r3][r4][r5][mask]; if (ret) return false; ret = true; int a[5]; a[0] = r1, a[1] = r2, a[2] = r3, a[3] = r4, a[4] = r5; int h = col[x]; for (int i = 0; i < v[h].size(); i++) { int b[5] = {0}; int now = v[h][i]; for (int j = 0; j < n; j++) b[j] = a[j]; bool flag = true; for (int j = 0; j < n; j++) { if ((mask & (1 << j)) == 0 && now & (1 << j)) b[j]--; if (b[j] < 0) { flag = false; break; } } if (flag) { if (solve(x + 1, b[0], b[1], b[2], b[3], b[4], now)) { ans.push_back(now); return true; } } } return false; } int main() { scanf( %d , &n); ; scanf( %d , &m); ; for (int i = 0; i < n; i++) scanf( %d , &row[i]); ; for (int i = 0; i < m; i++) scanf( %d , &col[i]); ; for (int i = 0; i < 1 << n; i++) { int c = 0, pre = 0; for (int j = 0; j < n; j++) { if (i & (1 << j) && !pre) { pre = 1; c++; } else pre = bool(i & (1 << j)); } v[c].push_back(i); } solve(0, row[0], row[1], row[2], row[3], row[4], 0); reverse(ans.begin(), ans.end()); for (int i = 0; i < n; i++) { for (int j = 0; j < ans.size(); j++) { if (ans[j] & (1 << i)) cout << * ; else cout << . ; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const unsigned long long base = 2333; const int maxn = 1e2 + 55; const int maxm = 1e4 + 50; const int maxv = 1e6 + 5; const int mod = 51123987; long long Cal(string s, long long p) { long long len = s.size(); long long sum = 0; for (long long i = 0; i < len; i++) { sum = (sum * 10 + s[i] - 0 ) % p; } return sum; } long long phi(long long n) { long long res = n, m = n; for (long long i = 2; i <= sqrt(m); i++) { if (m % i == 0) res = res / i * (i - 1); while (m % i == 0) m /= i; } if (m > 1) res = res / m * (m - 1); return res; } long long Quick_pow(long long x, long long y, long long p) { long long ans = 1, res = x; while (y) { if (y & 1) ans = ans * res % p; res = res * res % p; y >>= 1; } return ans; } int main() { string b, n; long long c; cin >> b >> n >> c; long long ph = phi(c); long long bb = Cal(b, c); long long nn = Cal(n, ph); if (n.size() < 10) { long long z = 0; for (long long i = 0; i < n.size(); i++) z = (z * 10 + n[i] - 0 ); long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, z - 1, c) % c; if (ans == 0) ans = c; printf( %lld n , ans); return 0; } nn = (nn - 1 + ph) % ph + ph; long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, nn, c) % c; if (ans == 0) ans = c; printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m, x, y; cin >> n >> m >> x >> y; char a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int county = 0, count = 0; if (2 * x > y) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == . ) count++; if (j != m - 1) { if (a[i][j] == a[i][j + 1] && a[i][j] == . ) { county++; j++; } } } } cout << (county * y) + (count - county) * x; } else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == . ) count++; } } cout << count * x; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; pair<int, int> a[110]; int n, k; vector<int> ans; void solve() { cin >> n >> k; for (int i = (1); i <= (n); ++i) { int x; cin >> x; a[i].first = x; a[i].second = i; } sort(a + 1, a + 1 + n); int s = 0; for (int i = 1; i <= n; ++i) { s += a[i].first; if (s <= k) ans.push_back(a[i].second); else break; } sort(ans.begin(), ans.end()); cout << ans.size() << endl; { for (int i = (0); i < (ans.size()); ++i) cout << ans[i] << ; cout << endl; }; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000 + 9; const long long MOD = 1000 * 1000 * 1000 + 7; const double EPS = 1e-9; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, -1, 0, 1}; const int N = 505; int n, k; int a[N][N]; int b[N][N]; int s[N][N]; int q; int f[N * N]; int g[N * N]; int ans; int mod; void dfs(int x, int y, int c) { b[x][y] = c; g[c]++; for (int i = 0; i < 4; ++i) { int xx = x + di[i]; int yy = y + dj[i]; if (a[xx][yy] && !b[xx][yy]) { dfs(xx, yy, c); } } } int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { char c; scanf( %c , &c); a[i + 1][j + 1] = c == . ; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (a[i][j] && !b[i][j]) { dfs(i, j, ++q); } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; } } for (int i = 1; i + k - 1 <= n; ++i) { for (int r = 0; r <= q; ++r) { f[r] = 0; } mod = 0; for (int u = i - 1; u <= i + k; ++u) { for (int v = 0; v <= k + 1; ++v) { if ((u == i - 1 || u == i + k) && (v == 0 || v == k + 1)) { continue; } if (f[b[u][v]] == 0) { mod += g[b[u][v]]; } f[b[u][v]]++; } } for (int j = 1; j + k - 1 <= n; ++j) { ans = max(ans, mod + (k * k - (s[i + k - 1][j + k - 1] - s[i - 1][j + k - 1] - s[i + k - 1][j - 1] + s[i - 1][j - 1]))); if (j + k - 1 < n) { for (int u = i - 1; u <= i + k; ++u) { int x = b[u][j - 1 + (u == i - 1 || u == i + k)]; --f[x]; if (f[x] == 0) { mod -= g[x]; } } for (int u = i - 1; u <= i + k; ++u) { int x = b[u][j + k + 1 - (u == i - 1 || u == i + k)]; if (f[x] == 0) { mod += g[x]; } ++f[x]; } } } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using i3 = pair<int, ii>; using li = pair<ll, int>; using lii = pair<ll, ii>; using vi = vector<int>; using vl = vector<ll>; using vli = vector<li>; using vii = vector<ii>; using vi3 = vector<i3>; using vlii = vector<lii>; const int N = 1e5 + 5; const int INF = 1e9 + 7; const double eps = 1e-12, PI = acos(-1); int n, m; char s[N]; int mem[N][5]; int dp(int i, int rem) { if (i == n) { return rem == 0 ? 0 : INF; } int &ret = mem[i][rem]; if (ret != -1) return ret; return ret = min(1 + dp(i + 1, rem), dp(i + 1, (rem * 10 + int(s[i] - 0 )) % 3)); } vi ans; void path(int i, int rem) { if (i == n) return; if (dp(i + 1, (rem * 10 + int(s[i] - 0 )) % 3) == dp(i, rem)) { ans.push_back(s[i] - 0 ); path(i + 1, (rem * 10 + int(s[i] - 0 )) % 3); return; } path(i + 1, rem); } void solve(int cs) { scanf( %s , s); n = strlen(s); int mn = INF, idx = INF; bool isZero = 0; memset(mem, -1, sizeof mem); for (int i = 0; s[i]; i++) { if (s[i] == 0 ) { isZero = 1; continue; } int curr = dp(i + 1, int(s[i] - 0 ) % 3) + i; if (curr < mn) { mn = curr; idx = i; } } if (idx == INF) { printf( %d n , isZero ? 0 : -1); return; } path(idx + 1, int(s[idx] - 0 ) % 3); printf( %c , s[idx]); for (int A : ans) printf( %d , A); printf( n ); } int main() { int t = 1; for (int cs = 1; t--; cs++) { solve(cs); } return 0; }
#include <bits/stdc++.h> using namespace std; void seive(bool a[], long long int n) { for (int i = 2; i * i <= n; i++) { if (a[i] == true) { for (int j = i * i; j <= n; j += i) a[j] = false; } } } int np(bool a[], long long int ppr, long long int n) { ppr++; while (ppr <= n) { if (a[ppr] == true) return ppr; ppr++; } return -1; } int main() { long long int n, i, n_1 = 0, n_2 = 0, sum = 0, s; int t; cin >> n; for (i = 0; i < n; i++) { cin >> t; if (t == 1) n_1++; else n_2++; sum += t; } bool a[sum + 1]; memset(a, true, sizeof(a)); seive(a, sum); s = 0; a[1] = false; long long int ppr = 0, pr; while (s <= sum && pr != -1) { pr = np(a, ppr, sum); if (pr - ppr >= 2 && pr != -1) { s = pr; int m = min(n_2, (pr - ppr) / 2); n_2 -= m; for (i = 0; i < m; i++) cout << 2 << ; m = min(n_1, 2 * ((pr - ppr) / 2 - m)); n_1 -= m; for (i = 0; i < m; i++) cout << 1 << ; ppr = pr; } else if (pr != -1) { n_1--; if (n_1 < 0) { for (i = 0; i < n_1; i++) cout << 1 << ; for (i = 0; i < n_2; i++) cout << 2 << ; pr = -1; } else { cout << 1 << ; } ppr = pr; } else { for (i = 0; i < n_1; i++) cout << 1 << ; for (i = 0; i < n_2; i++) cout << 2 << ; } if (n_1 <= 0 && n_2 <= 0) break; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, z, p, i, j, k, ans, t, a[105], b[105], d[105], f[105][10005]; bool cmp(int x, int y) { return x > y; } int main() { scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %d , &a[i]), z += a[i]; for (i = 1; i <= n; i++) scanf( %d , &b[i]), d[i] = b[i]; sort(d + 1, d + n + 1, cmp); memset(f, 0x80, sizeof(f)); f[0][0] = 0; for (i = 1; i <= n; i++) { p += d[i]; if (p >= z) { ans = i; break; } } for (i = 1; i <= n; i++) for (j = p; j >= b[i]; j--) for (k = 1; k <= ans; k++) f[k][j] = max(f[k][j], f[k - 1][j - b[i]] + a[i]); for (i = z; i <= p; i++) t = max(t, f[ans][i]); printf( %d %d , ans, z - t); return 0; }
#include <bits/stdc++.h> using namespace std; int m[100002]; int main() { memset(m, 255, sizeof m); int runs; scanf( %d , &runs); for (int j = 0; j < runs; j++) { int x, y, ret = 0; scanf( %d%d , &x, &y); for (int i = 1; i * i <= x; i++) if (x % i == 0) { if (m[i] < j - y) ret++; if (i * i < x && m[x / i] < j - y) ret++; m[i] = j; m[x / i] = j; } printf( %d n , ret); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1e5; bool in[N_MAX + 1]; int t, n, a[N_MAX], m[N_MAX], pr[N_MAX + 1]; vector<int> inc; vector<vector<int>> sol; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int k = 1, rem = n; while ((k + 1) * (k + 2) <= 2 * n) k++; for (int i = 1; i <= n; i++) in[i] = true; sol.clear(); while (0 < rem) { inc.clear(); for (int i = 0; i < n; i++) { if (!in[a[i]]) continue; if (inc.empty() || a[i] > inc.back()) { pr[a[i]] = (inc.empty() ? -1 : inc.back()); inc.push_back(a[i]); } else { auto it = upper_bound(inc.begin(), inc.end(), a[i]); *it = a[i]; pr[a[i]] = (it == inc.begin() ? -1 : *(it - 1)); } } if (inc.size() <= k) { int first = sol.size(); sol.push_back(vector<int>()); m[first] = n + 1; for (int i = 0; i < n; i++) { if (!in[a[i]]) continue; if (m[(int)sol.size() - 1] < a[i]) { m[sol.size()] = a[i]; sol.push_back(vector<int>()); sol.back().push_back(a[i]); continue; } int l = first, r = (int)sol.size() - 1; while (l < r) { int mid = (l + r) / 2; if (m[mid] > a[i]) r = mid; else l = mid + 1; } sol[l].push_back(a[i]); m[l] = a[i]; } break; } stack<int> st; int act = inc.back(); while (act != -1) { st.push(act); act = pr[act]; } sol.push_back(vector<int>()); while (!st.empty()) { sol.back().push_back(st.top()); st.pop(); } for (int i : sol.back()) in[i] = false; rem -= inc.size(); while (2 * rem < k * (k + 1)) k--; } cout << sol.size() << n ; for (int i = 0; i < sol.size(); i++) { cout << sol[i].size(); for (int s : sol[i]) cout << << s; cout << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, i1; long long cont = 0; int main() { cin >> n; for (long long i = 0; i < n; ++i) { long long x, y; cin >> x >> y; if (x - y != 1) { cout << NO << endl; } else { long long p = x + y, s = 0; for (long long j = 2; j <= sqrt(p); ++j) { if (p % j == 0) { s = 1; break; } } if (s == 0) { cout << YES << endl; } else { cout << NO << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool Max(T1& a, T2 b) { return a < b ? a = b, 1 : 0; } template <typename T1, typename T2> bool Min(T1& a, T2 b) { return a > b ? a = b, 1 : 0; } const int N = 200002, inf = 1e9 + 1; int s[N], g[N], ans[N]; bool vi[N]; vector<int> cap[N * 10]; void solve() { int n, m; cin >> n; int mncap = N * 10; for (int i = 0; i < (n); ++i) { cin >> s[i] >> g[i]; g[i] += s[i]; ans[i] = g[i]; } int mn = inf; for (int i = 0; i < (n); ++i) { mn++; Min(mn, g[i]); Min(ans[i], mn); } mn = inf; for (int i = n - 1; i >= (0); --i) { mn++; Min(mn, g[i]); Min(ans[i], mn); } long long ret = 0; for (int i = 0; i < (n); ++i) { if (ans[i] < s[i]) { cout << -1 << n ; return; } ret += ans[i] - s[i]; } cout << ret << n ; for (int i = 0; i < (n); ++i) cout << ans[i] << ; cout << n ; } void init() {} int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); int TC = 1; for (int TI = 1; TI <= (TC); ++TI) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Mod = 1e9 + 7; const int N = 1000005; long long frac[N + 5], inv[N + 5]; int n, a[N], y[N], Max; long long ans; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } long long power(long long a, long long b) { long long res = 1; for (; b; b >>= 1) { if (b & 1) res = res * a % Mod; a = a * a % Mod; } return res; } long long C(int m, int n) { return frac[n] * inv[m] % Mod * inv[n - m] % Mod; } int main() { n = read(); frac[1] = 1; frac[0] = 1; for (int i = 2; i <= n + 2; i++) frac[i] = frac[i - 1] * i % Mod; inv[n + 2] = power(frac[n + 2], Mod - 2); for (int i = n + 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % Mod; inv[0] = 1; for (int i = 1; i <= n; i++) a[i] = read(); sort(a + 1, a + n + 1); a[0] = -1; for (int i = 1; i <= n; i++) { Max = max(Max, a[i]); if (a[i] == a[i - 1]) { y[i] = y[i - 1]; continue; } for (int j = i; j >= 0; j--) { if (a[j] != a[i]) { y[i] = n - j; break; } } --y[i]; } for (int i = 1; i <= n; i++) { if (a[i] == Max) continue; ans = (ans + (a[i] * frac[y[i]] % Mod * frac[n - y[i] - 1] % Mod * C(y[i] + 1, n) % Mod)) % Mod; } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; template <typename T> using v = vector<T>; const long long mod1 = 1e9 + 7; const long long mod2 = 36028797018963913; const long long inf = 2e18; void read() {} void print() {} void debug() { cerr << endl; } long long gcd(long long a, long long b) { if (a > b) swap(a, b); if (a) return gcd(a, b % a); return b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } struct vt { long long x, y; vt operator-() { return vt(-this->x, -this->y); } vt operator+(vt b) { return {this->x + b.x, this->y + b.y}; } vt operator-(vt b) { return *this + (-b); } vt operator*(long long b) { return vt(this->x * b, this->y * b); } long long operator*(vt b) { return this->x * b.x + this->y * b.y; } vt operator/(long long b) { return vt(this->x / b, this->y / b); } long long operator%(vt b) { return this->x * b.y - b.x * this->y; } bool operator==(vt b) { return this->x == b.x && this->y == b.y; } bool operator!=(vt b) { return !((*this) == b); } bool collinear(vt b) { return (*this) % b == 0; } bool opposite(vt b) { return this->collinear(b) && (*this) * b < 0; } double length() { return sqrt(this->x * this->x + this->y * this->y); } long long sqLength() { return this->x * this->x + this->y * this->y; } vt() {} vt(long long x, long long y) : x(x), y(y) {} vt(long long x1, long long y1, long long x2, long long y2) : x(x2 - x1), y(y2 - y1) {} }; long long fact(long long a, long long mod) { if (a == 1) return 1; else return (a * fact(a - 1, mod)) % mod; } long long sign(long long a) { if (a > 0) return 1; if (a == 0) return 0; return -1; } bool prime(long long a) { if (a < 2) return 0; if (a == 2) return 1; for (long long b = 2; b <= ceil(sqrt(a)); b++) if (a % b == 0) return 0; return 1; } long long pow(long long a, long long b, long long mod) { if (b == 0) return 1; if (b == 1) return a; long long res = pow(a, b / 2, mod); res = (res * res) % mod; if (b % 2) return (a * res) % mod; return res; } long long hashStr(string& s) { long long base = 26; long long res = 0; for (long long i = 0; i < ((long long)(s).size()); i++) res = ((res * base) % mod2 + s[i] - a + 1) % mod2; return (res + mod2) % mod2; } long long hashStr(long long prevHash, char curChar) { return ((prevHash * 26) % mod2 + curChar - a + 1) % mod2; } struct node { node *left = nullptr, *right = nullptr; long long w, val, sz = 1; node(long long val) : w((rand() << 16) + rand()), val(val) {} }; long long siz(node* a) { if (a == nullptr) return 0; else return a->sz; } node* treap; node* merge1(node* v1, node* v2) { if (v1 == nullptr) return v2; if (v2 == nullptr) return v1; if (v1->w > v2->w) { v1->right = merge1(v1->right, v2); v1->sz = siz(v1->left) + siz(v1->right) + 1; return v1; } else { v2->left = merge1(v1, v2->left); v2->sz = siz(v2->left) + siz(v2->right) + 1; return v2; } } pair<node*, node*> split(node* w, long long k) { if (w == nullptr) return {nullptr, nullptr}; if (siz(w->left) >= k) { auto tmp = split(w->left, k); w->left = tmp.second; w->sz = siz(w->left) + siz(w->right) + 1; return {tmp.first, w}; } else { auto tmp = split(w->right, k - siz(w->left) - 1); w->right = tmp.first; w->sz = siz(w->left) + siz(w->right) + 1; return {w, tmp.second}; } } ifstream fin( input.txt ); ofstream fout( output.txt ); template <typename Arg, typename... Args> void read(Arg& arg, Args&... args) { cin >> (arg); read(args...); } template <typename Arg, typename... Args> void print(Arg arg, Args... args) { cout << (arg) << ; print(args...); } template <typename Arg, typename... Args> void debug(Arg arg, Args... args) { cerr << (arg) << ; debug(args...); } void prnt(node* a) { if (a == nullptr) return; prnt(a->left); print(a->val); prnt(a->right); } long long getInt() { long long a; cin >> a; return a; } void run() { long long w, d; read(w, d); long long a[w + 1]; for (long long i = (0); i < (w - 1); i++) cin >> a[i + 1]; a[w] = 1e9; long long dp[w + 1]; dp[0] = 1e9; a[0] = 1e9; long long l = 0; set<long long> p; p.insert(0); for (long long i = (1); i < (w + 1); i++) { dp[i] = 0; l = max(i - d, l); while ((*p.begin()) < l) p.erase(*p.begin()); while (!p.empty()) { long long cur = *p.begin(); if (dp[cur] <= a[i] - dp[i]) { dp[i] += dp[cur]; dp[cur] = 0; p.erase(cur); } else { long long mn = a[i] - dp[i]; dp[i] = a[i]; dp[cur] -= mn; break; } } if (dp[i]) p.insert(i); } cout << dp[w]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); run(); return 0; }
#include <bits/stdc++.h> using namespace std; int blacks(int *pos, int n); int whites(int *pos, int n); int main() { int n; cin >> n; int board[100]; for (int i = 0; i < n; i++) { board[i] = 0; } for (int i = 0; i < n / 2; i++) { int temp; cin >> temp; board[temp - 1] = 1; } cout << min(blacks(board, n), whites(board, n)); } int blacks(int *pos, int n) { int i = 0; int label = 0; int result = 0; while (i < n) { if (pos[i] == 1) { result += abs(i - label); label += 2; } i++; } return result; } int whites(int *pos, int n) { int i = 0; int label = 1; int result = 0; while (i < n) { if (pos[i] == 1) { result += abs(i - label); label += 2; } i++; } return result; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int i, n, f = 0; cin >> n; vector<int> v(n); vector<int> use(n + 1, 0); for (i = 0; i < n; i++) { cin >> v[i]; if (v[i] < (i + 1)) { f = 1; } } if (f) { cout << -1 << endl; continue; } vector<int> a(n, 0); for (i = 0; i < n; i++) { if (!use[v[i]]) { a[i] = v[i]; use[v[i]] = 1; } } queue<int> q; for (i = 1; i <= n; i++) { if (!use[i]) { q.push(i); } } for (i = 0; i < n; i++) { if (a[i] != 0) { cout << a[i] << ; } else { cout << q.front() << ; q.pop(); } } cout << endl; } }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double PI = acos(-1), eps = 1e-6; const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}; const int N = 1e6 + 5, M = 2e6 + 5, mod = 1e9 + 7; int n; int p[N], c[N]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> n; bool success = true; for (int i = 1, d = 0; i <= n; i++) { cin >> p[i] >> c[i]; if (p[i] < c[i] || p[i] < p[i - 1] || c[i] < c[i - 1] || p[i] - c[i] < d) success = false; d = p[i] - c[i]; } cout << (success ? YES : NO ) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int maxn = 2e5 + 100; int n, val[maxn]; void solve() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , val + i); set<int> s; for (int i = 0; i < n; i++) s.insert((((i + val[i]) % n) + n) % n); puts((s.size() == n) ? YES : NO ); } int main() { int t = 1; scanf( %d , &t); while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct Timer { chrono::time_point<chrono::high_resolution_clock> start, end; chrono::duration<float> dur; Timer() { start = chrono::high_resolution_clock::now(); } ~Timer() { end = chrono::high_resolution_clock::now(); dur = end - start; cout << nTime Elapsed : << dur.count() << s n ; } }; long long int sum() { return 0; } template <typename T, typename... Args> T sum(T a, Args... args) { return a + sum(args...); } template <int D, typename T> struct Vec : public vector<Vec<D - 1, T>> { static_assert(D >= 1, Vector dimension must be greater than zero! ); template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} }; template <typename T> struct Vec<1, T> : public vector<T> { Vec(int n = 0, T val = T()) : vector<T>(n, val) {} }; template <typename T> class SieveEuler { public: vector<T> primes; vector<T> lp; inline SieveEuler(int MAX = 1000000) { lp.resize(MAX + 1, 0); for (long long int i = 2; i <= MAX; i++) { if (lp[i] == 0) { lp[i] = i; primes.push_back(i); } for (int j = 0; j < primes.size() && i * primes[j] <= MAX && primes[j] <= lp[i]; ++j) { if (primes[j] == lp[i]) { lp[i * primes[j]] = primes[j]; } else { lp[i * primes[j]] = primes[j]; } } } } }; long long int EulerPhi(long long int n) { long long int result = n; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } template <typename T> vector<vector<T>> matmul(const vector<vector<T>>& a, const vector<vector<T>>& b) { int n = a.size(), m = b[0].size(), k = b.size(); vector<vector<T>> c(n, vector<T>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int l = 0; l < k; l++) c[i][j] += (a[i][k] * b[k][j]); } } return c; } template <typename T> bool isPrime(T n) { if (n <= 1) return false; else if (n == 2 || n == 3) return true; else if (n % 2 == 0 || n % 3 == 0) return false; T x = 5; T p = 2; while (x * x <= n) { if (n % x == 0) return true; x += p; p = 6 - p; } return true; } template <typename T> class Graph { public: struct edge { int from, to; T cost; }; vector<edge> edgeList; vector<vector<int>> adj; int n; Graph(int _n) : n(_n) { adj.resize(n + 1); } void add(int from, int to) { adj[to].push_back(from); adj[from].push_back(to); } void solve(int m) { int cnt = 0; for (int i = 1; i <= n; i++) { cnt += (adj[i].size() == 1); } cout << setprecision(9) << 2 * ((double)m / cnt) << n ; } }; bool isSubsequenceString(string x, string y) { int i = 0, j = 0; while (i < x.length() && j < y.length()) x[i] == y[j] ? i++, j++ : i++; return (j == y.length()); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int testCases = 1; while (testCases--) { int n, m; cin >> n >> m; Graph<int> g(n); for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; g.add(u, v); } g.solve(m); } }
#include <bits/stdc++.h> using namespace std; int cnt[2][2]; string s; int main() { ios_base::sync_with_stdio(0); cin >> s; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cnt[i][j] = 0; } } for (int i = 0; i < s.length(); i++) { cnt[i & 1][s[i] - a ]++; } long long ans = 0; for (int i = 0; i < 2; i++) { ans += (long long)cnt[0][i] * cnt[1][i]; } cout << ans << ; ans = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ans += (long long)cnt[i][j] * (cnt[i][j] + 1) / 2; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const double pi = 3.14159265358979323846; const long long mxN = 2e5 + 2; void _print(long long t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(double t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << { ; _print(p.first); cerr << , ; _print(p.second); cerr << } ; } template <class T> void _print(vector<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(set<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(multiset<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T, class V> void _print(map<T, V> v) { cerr << [ ; for (auto i : v) { _print(i); cerr << ; } cerr << ] ; } vector<long long> adj[27]; long long degree[27]; bool visited[27]; vector<long long> hold; void dfs(long long src) { visited[src] = true; hold.push_back(src); for (auto edge : adj[src]) { if (!visited[edge]) dfs(edge); } } void solve() { string s; cin >> s; long long n = s.length(); memset(degree, 0, sizeof(degree)); memset(visited, false, sizeof(visited)); hold.clear(); vector<vector<long long>> hehe(26, vector<long long>(26, 0)); for (long long i = 0; i <= 25; i++) adj[i].clear(); for (long long i = 0; i < n - 1; i++) { if (s[i] != s[i + 1] && hehe[s[i] - a ][s[i + 1] - a ] == 0) { adj[s[i] - a ].push_back(s[i + 1] - a ); adj[s[i + 1] - a ].push_back(s[i] - a ); hehe[s[i] - a ][s[i + 1] - a ] = 1; hehe[s[i + 1] - a ][s[i] - a ] = 1; degree[s[i + 1] - a ]++; degree[s[i] - a ]++; } } long long onecount = 0; long long pos = -1; if (n == 1) pos = s[0] - a ; for (long long i = 0; i <= 25; i++) { if (degree[i] > 2) { cout << NO n ; return; } if (degree[i] == 1) { onecount++; pos = i; } } if (n != 1 && onecount != 2) { cout << NO n ; return; } cout << YES n ; dfs(pos); for (long long i = 0; i < 26; i++) { if (!visited[i]) hold.push_back(i); } for (auto x : hold) cout << char(x + a ); cout << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long T = 1; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1024; int size[MAXN], in[MAXN], deg[MAXN]; bitset<MAXN> a[MAXN], c; vector<pair<int, int> > edges; set<pair<int, int> > was; int main() { int n; scanf( %d , &n); bool star = 1; for (int i = 0; (i) < (n); ++i) { scanf( %d , &size[i]); if (size[i] != n) star = 0; for (int j = 0; (j) < (size[i]); ++j) { int l; scanf( %d , &l); a[i][l - 1] = 1; } } if (star) { for (int i = 0; (i) < (n - 1); ++i) printf( %d %d n , i + 1, n); return 0; } for (int i = 0; (i) < (n); ++i) for (int j = (i + 1); (j) < (n); ++j) { c = a[i] & a[j]; unsigned int *cur = (unsigned int *)&c; vector<int> v; for (int g = 0; (g) < (MAXN / 32); ++g) { if (*cur != 0) { for (int k = (32) - 1; (k) >= 0; --k) if (((*cur) >> k) & 1) v.push_back(g * 32 + k); if ((int)((v).size()) > 2) break; } cur++; } if ((int)((v).size()) == 2) edges.push_back(make_pair(v[0], v[1])); } sort((edges).begin(), (edges).end()); edges.resize(unique((edges).begin(), (edges).end()) - edges.begin()); for (auto edge : edges) { in[edge.first] = in[edge.second] = 1; deg[edge.first]++, deg[edge.second]++; was.insert(make_pair(edge.first, edge.second)); was.insert(make_pair(edge.second, edge.first)); } for (int i = 0; (i) < (n); ++i) if (!in[i]) { int minn = 2 * n, ind = -1; for (int j = 0; (j) < (n); ++j) if (a[j][i] == 1 && size[j] < minn) { minn = min(minn, size[j]); ind = j; } assert(ind != -1); vector<int> vIn; for (int j = 0; (j) < (n); ++j) if (a[ind][j] && in[j]) vIn.push_back(j); if ((int)((vIn).size()) > 2) { for (int j = 0; (j) < ((int)((vIn).size())); ++j) { bool ok = 1; for (int g = 0; (g) < ((int)((vIn).size())); ++g) { if (g != j && !was.count(make_pair(vIn[j], vIn[g]))) { ok = 0; break; } } if (ok) { edges.push_back(make_pair(vIn[j], i)); break; } } } else { assert((int)((vIn).size()) == 2); assert(deg[vIn[0]] == 1 || deg[vIn[1]] == 1); if (deg[vIn[0]] == 1 && deg[vIn[1]] == 1) { int ind2 = -1; for (int j = 0; (j) < (n); ++j) if (size[j] != n) { ind2 = j; break; } assert(ind != -1); if (a[ind2] == a[ind]) edges.push_back(make_pair(vIn[0], i)); else edges.push_back(make_pair(vIn[1], i)); } else { for (int j = 0; (j) < (2); ++j) if (deg[vIn[j]] == 1) edges.push_back(make_pair(vIn[j], i)); } } } for (auto edge : edges) printf( %d %d n , edge.first + 1, edge.second + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double PI = acos(-1); const long long mod = 1e9 + 7; const long long OO = 2e18; const int N = 3e5 + 5; int parent[N]; set<int> st; set<int>::iterator it1, it2; int n, m, l, r, x; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) st.insert(i); st.insert(oo); while (m--) { cin >> l >> r >> x; it1 = st.lower_bound(l); while (*it1 <= r) { if (*it1 != x) { parent[*it1] = x; it2 = it1; it1++; st.erase(it2); } else it1++; } } for (int i = 1; i <= n; i++) { cout << parent[i] << ; } }
#include <bits/stdc++.h> using namespace std; bool dp[1001][1001]; int data[1001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; if (n > m) { cout << YES << n ; return 0; } else { for (int i = 1; i <= n; i++) { cin >> data[i], data[i] %= m; if (!data[i]) { cout << YES << n ; return 0; } } dp[0][0] = true; for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i][j] = dp[i - 1][j]; int var = j - data[i]; if (var < 0) var += m; dp[i][j] |= dp[i - 1][var]; } if (dp[i][m]) { cout << YES << n ; return 0; } } } cout << NO << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100009; vector<int> edges[N]; vector<int> a; int n, m; int main() { while (cin >> n >> m) { a.clear(); for (int i = 1; i <= n; ++i) edges[i].clear(), a.push_back(i); for (int i = 0; i < m; ++i) { int ac, b; cin >> ac >> b; edges[ac].push_back(b); edges[b].push_back(ac); } bool t = true; int u, v; for (int i = 0; i < 500; ++i) { random_shuffle(a.begin(), a.end()); bool flg = true; for (int j = 0; j < m && flg; ++j) { u = a[j]; v = a[(j + 1) % n]; for (int k = 0; k < edges[u].size(); ++k) if (edges[u][k] == v) flg = false; } if (flg) { for (int j = 0; j < m; ++j) cout << a[j] << << a[(j + 1) % n] << endl; t = false; break; } } if (t) cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long ans; int main() { int n, i; long long a; cin >> n; ans = 0; for (i = 1; i <= n; i++) { cin >> a; ans = ans + (1 + (a - 1) * (long long)i); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e4 + 5; int dp[53][maxn], mxp[53][maxn], mxs[53][maxn], n, m, a[53][maxn], k, sum[53][maxn], h1, h2, t1, t2; pair<int, int> q1[maxn], q2[maxn]; int main() { scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) scanf( %d , &a[i][j]), sum[i][j] = sum[i][j - 1] + a[i][j]; for (int i = 1; i <= m; ++i) dp[1][i] = sum[1][min(i + k - 1, m)] - sum[1][i - 1]; for (int i = 1; i <= m; ++i) mxp[1][i] = max(mxp[1][i - 1], dp[1][i] + sum[2][min(i + k - 1, m)] - sum[2][i - 1]); for (int i = m; i >= 1; --i) mxs[1][i] = max(mxs[1][i + 1], dp[1][i] + sum[2][min(i + k - 1, m)] - sum[2][i - 1]); for (int i = 2; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (j + k <= m) dp[i][j] = max(dp[i][j], mxs[i - 1][j + k] + sum[i][min(j + k - 1, m)] - sum[i][j - 1]); if (j - k >= 1) dp[i][j] = max(dp[i][j], mxp[i - 1][j - k] + sum[i][min(j + k - 1, m)] - sum[i][j - 1]); } h1 = h2 = 1; t1 = t2 = 0; for (int j = 1; j <= m; ++j) { while (h1 <= t1 && q1[h1].first < j - k + 1) h1++; int num = dp[i - 1][j] - sum[i][j - 1]; while (h1 <= t1 && q1[t1].second <= num) t1--; q1[++t1] = {j, num}; if (h1 <= t1) dp[i][j] = max(dp[i][j], q1[h1].second + sum[i][min(m, j + k - 1)]); } for (int j = m; j >= 1; --j) { while (h2 <= t2 && q2[h2].first > j + k - 1) h2++; if (h2 <= t2) dp[i][j] = max(dp[i][j], q2[h2].second - sum[i][j - 1]); int num = dp[i - 1][j] + sum[i][min(j + k - 1, m)]; while (h2 <= t2 && q2[t2].second <= num) t2--; q2[++t2] = {j, num}; } for (int j = 1; j <= m; ++j) { mxp[i][j] = max(mxp[i][j - 1], dp[i][j] + sum[i + 1][min(m, j + k - 1)] - sum[i + 1][j - 1]); } for (int j = m; j >= 1; --j) { mxs[i][j] = max(mxs[i][j + 1], dp[i][j] + sum[i + 1][min(m, j + k - 1)] - sum[i + 1][j - 1]); } } int ans = 0; for (int i = 1; i <= m; ++i) { if (i + k - 1 > m) break; ans = max(ans, dp[n][i]); } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int tot = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (tot + a <= 500) { tot += a; cout << A ; } else { tot -= b; cout << G ; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 2e5 + 10; const double pi = acos(-1); int main() { int cas; cin >> cas; double n; while (cas--) { cin >> n; printf( %.10lf n , cos(pi / 4.0 / n) / sin(pi / 2.0 / n)); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int a[1000001]; long long int b[1000001]; bool marked[1000001]; int main() { long long int n, i, j, k, x, y, fin_ans = 0, sum = 0; cin >> n >> k; a[0] = 0; for (i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; fin_ans += a[i - 1] * a[i]; } fin_ans += a[n] * a[1]; for (i = 0; i < k; i++) { cin >> x; b[i] = x; } long long val = 0; for (j = 0; j < k; j++) { marked[b[j]] = 1; val += a[b[j]]; long long to_mul = 0; if (b[j] != 1 && b[j] != n) { to_mul = sum - val - a[b[j] + 1]; if (!marked[b[j] - 1]) { to_mul -= a[b[j] - 1]; } } if (b[j] == 1) { to_mul = sum - val - a[b[j] + 1]; to_mul -= a[n]; } if (b[j] == n) { to_mul = sum - val; if (!marked[b[j] - 1]) to_mul -= a[b[j] - 1]; if (!marked[1]) to_mul -= a[1]; } fin_ans += (a[b[j]] * to_mul); } cout << fin_ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<bool> visited; vector<vector<int>> adj; vector<int> depth; void dfs(int v, int d) { if (visited[v]) { return; } visited[v] = true; depth[d]++; for (int i : adj[v]) { dfs(i, d + 1); } } int main() { int n; cin >> n; visited.resize(n + 1); adj.resize(n + 1); for (int i = 2; i <= n; i++) { int to; cin >> to; adj[to].push_back(i); } depth.resize(n + 1); dfs(1, 0); int collected = 0; for (int i : depth) { collected += i % 2; } cout << collected << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int P[111111]; int sz[111111]; int find(int x) { return (P[x] == x ? x : P[x] = find(P[x])); } void Union(int x, int y, int type) { x = find(x), y = find(y); if (sz[x] < sz[y]) swap(x, y); if (x == y) { if (type == -1) P[x] = 0; return; } if (type == -1) return; sz[x] += sz[y]; P[y] = x; } int main() { int N, K; cin >> N >> K; for (int i = 1; i <= N; i++) P[i] = i, sz[i] = 1; while (K--) { int u, v; cin >> u >> v; Union(u, v, 1); } cin >> K; while (K--) { int u, v; cin >> u >> v; Union(u, v, -1); } int best = 0; for (int i = 1; i <= N; i++) if (find(i)) best = max(best, sz[find(i)]); cout << best << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int arr[5009], n; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); int cnt = 0; int ans = 0; int chk; bool fs = false; while (cnt < n) { fs = false; for (int i = n - 1; i >= 0; i--) { if (!fs && arr[i]) { chk = arr[i]; fs = true; cnt++; ans++; arr[i] = 0; continue; } if (arr[i] < chk && arr[i]) { cnt++; chk = arr[i]; arr[i] = 0; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = numeric_limits<int>::max(); const long long LLINF = numeric_limits<long long>::max(); const unsigned long long ULLINF = numeric_limits<unsigned long long>::max(); const double PI = acos(-1.0); int sz = 1; vector<int> compressed; vector<pair<int, pair<int, int>>> queries; map<int, int> order; vector<int> t; vector<set<int>> vert; void build(int n) { while (sz < n) sz <<= 1; t.resize(sz + sz, 0); } void update(int i) { i += sz; if (vert[i - sz].size() == 0) t[i] = 0; else t[i] = *vert[i - sz].rbegin(); while ((i >> 1) > 0) { i >>= 1; t[i] = max(t[i + i], t[i + i + 1]); } } int query(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (tl == l && tr == r) return t[v]; int tm = (tl + tr) >> 1; return max(query(v + v, tl, tm, l, min(r, tm)), query(v + v + 1, tm + 1, tr, max(tm + 1, l), r)); } int getX(int v, int tl, int tr, int l, int y) { if (t[v] <= y) return INF; if (tr <= l) return INF; if (tl == tr) return tl; int mid = (tl + tr) >> 1; if (tl > l) { if (t[v + v] > y) return getX(v + v, tl, mid, l, y); else return getX(v + v + 1, mid + 1, tr, l, y); } return min(getX(v + v, tl, mid, l, y), getX(v + v + 1, mid + 1, tr, l, y)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; queries.resize(n); set<int> d; for (int i = 0; i < n; i++) { string type; cin >> type; if (type == add ) queries[i].first = 0; else if (type == remove ) queries[i].first = 1; else queries[i].first = 2; cin >> queries[i].second.first >> queries[i].second.second; queries[i].second.first, queries[i].second.second; d.insert(queries[i].second.first); } for (auto k : d) { order[k] = compressed.size(); compressed.push_back(k); } build(compressed.size()); vert.resize(compressed.size()); for (auto q : queries) { if (q.first == 0) { vert[order[q.second.first]].insert(q.second.second); update(order[q.second.first]); } else if (q.first == 1) { vert[order[q.second.first]].erase(q.second.second); update(order[q.second.first]); } else { int ans = getX(1, 0, sz - 1, order[q.second.first], q.second.second); if (ans == INF) cout << -1 n ; else { int y = *vert[ans].upper_bound(q.second.second); cout << compressed[ans] << << y << n ; } } } return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; const int OO = (int)1e9 + 7; const int MAX = 109; char c1, c2, c3; string s1, s2, s3; long long n, m, k, t, a, b, c, d, e, f, x, y, z, sol = 0, ans = 0; long long ar[1000009]; map<long long, bool> vis; int fun(int i) { int ret = 0; for (int i = 0; i < n; i++) { if (ar[i] % m == 0 && vis[ar[i] / m]) continue; vis[ar[i]] = 1; ret++; } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); cout << fun(0); cout << n n ; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename... Ts> void do_nothing(Ts &&...vals) {} constexpr long long MOD = 1000000007; constexpr int INF = 0x7f7f7f7f; constexpr double EPS = 1e-8; long long fastPow(long long num, long long exp, long long modNum) { if (exp == 0) return 1; if (exp == 1) return num; else { long long half = fastPow(num, exp / 2, modNum) % modNum; if (exp % 2 == 1) { return (((half * half) % modNum) * num) % modNum; } else { return (half * half) % modNum; } } } long long fastPow(long long num, long long exp) { if (exp == 0) return 1; if (exp == 1) return num; else { long long half = fastPow(num, exp / 2); if (exp % 2 == 1) { return (((half * half)) * num); } else { return (half * half); } } } long long getReverse(long long y, long long modNum) { return fastPow(y, modNum - 2, modNum) % modNum; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); return (a % b == 0) ? b : gcd(b, a % b); } int main(int argn, char **argv) { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); const long long MOD = 1e9 + 7; int T = 1; cin >> T; for (int(I) = (0); (I) <= ((T)-1); (I)++) { long long n, x, y; cin >> n >> x >> y; vector<long long> arr(n); vector<stack<long long>> places(n + 2); for (int(i) = (0); (i) <= ((n)-1); (i)++) { cin >> arr[i]; places[arr[i]].emplace(i); } priority_queue<pair<long long, long long>> pq; long long except; for (int(i) = (1); (i) <= (n + 1); (i)++) { if (places[i].size() == 0) { except = i; } else { pq.emplace(places[i].size(), i); } } vector<long long> ans(n, 0); for (int(i) = (0); (i) <= ((x)-1); (i)++) { auto pa = pq.top(); pq.pop(); long long idx = places[pa.second].top(); places[pa.second].pop(); ans[idx] = pa.second; pq.emplace(places[pa.second].size(), pa.second); } y -= x; vector<long long> pos, val; if (!pq.empty()) { auto pa = pq.top(); pq.pop(); for (int(i) = (0); (i) <= ((pa.first) - 1); (i)++) { val.emplace_back(pa.second); } while (!pq.empty()) { auto next = pq.top(); pq.pop(); for (int(i) = (0); (i) <= ((next.first) - 1); (i)++) { pos.emplace_back(places[next.second].top()); places[next.second].pop(); val.emplace_back(next.second); } } for (int(i) = (0); (i) <= ((pa.first) - 1); (i)++) { pos.emplace_back(places[pa.second].top()); places[pa.second].pop(); } } long long excount = n - x - y, id = 0; while (y > 0) { ans[pos[id]] = val[id]; if (ans[pos[id]] == arr[pos[id]]) { if (excount > 0) { excount--; ans[pos[id]] = 0; } else { break; } } else { y--; } id++; } if (y > 0) { cout << NO << endl; continue; } cout << YES << endl; for (int(i) = (0); (i) <= ((n)-1); (i)++) { if (ans[i] != 0) { cout << ans[i] << ; } else { cout << except << ; } } cout << endl; ; } return 0; }