func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int N = 512345, lgN = 20, rootN = 1123; const double eps = 1e-11; vector<int> cities; int root = -1; int dp[N]; bool visited[N], rem[N]; vector<int> adj[N]; void dfsTree(int v) { int sum = 0; auto it = lower_bound(cities.begin(), cities.end(), v); if (it < cities.end() && *it == v) ++sum; for (int i = 0; i < adj[v].size(); ++i) { int u = adj[v][i]; if (visited[u]) continue; visited[u] = true; dfsTree(u); sum += dp[u]; } if (sum == 0) rem[v] = true; else if (sum == cities.size() && root == -1) root = v; else if (sum == cities.size()) rem[v] = true; dp[v] = sum; } pair<int, int> bfsDistance(int src) { memset(visited, false, sizeof visited); queue<pair<int, int> > q; q.push(make_pair(src, 0)); visited[src] = true; pair<int, int> ret = make_pair(src, 0); while (!q.empty()) { int v = q.front().first, dist = q.front().second; q.pop(); if (dist > ret.second || (dist == ret.second && v < ret.first)) ret = make_pair(v, dist); for (int i = 0; i < adj[v].size(); ++i) { int u = adj[v][i]; if (visited[u] || rem[u]) continue; visited[u] = true; q.push(make_pair(u, dist + 1)); } } return ret; } int main() { int n, c; scanf( %d %d , &n, &c); int u, v; for (int i = 1; i < n; ++i) { scanf( %d %d , &u, &v); adj[u].push_back(v), adj[v].push_back(u); } cities.resize(c); for (int i = 0; i < cities.size(); ++i) scanf( %d , &cities[i]); sort(cities.begin(), cities.end()); visited[1] = true; dfsTree(1); pair<int, int> x = bfsDistance(root); pair<int, int> y = bfsDistance(x.first); printf( %d n , min(x.first, y.first)); int ct = 0; for (int i = 1; i <= n; ++i) if (!rem[i]) ++ct; int ans = 2 * (ct - 1) - y.second; printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int f[200010], fi[200010]; const int MOD = 1e9 + 7; int add(long long a, long long b) { return (a + b) % MOD; } int mul(long long a, long long b) { return (a * b) % MOD; } int fp(long long a, int b) { if (b == 0) return 1; int tp = fp(a, b >> 1); tp = mul(tp, tp); if (b & 1) return mul(tp, a); else return tp; } int comb(int n, int k) { if (k > n) return 0; return mul(f[n], mul(fi[n - k], fi[k])); } int h, w, n, dp[2010]; vector<pair<int, int> > v; int main() { f[0] = fi[0] = 1; for (int i = 1; i <= 200002; i++) { f[i] = mul(i, f[i - 1]); fi[i] = fp(f[i], MOD - 2); } scanf( %d%d%d , &h, &w, &n); v.resize(n); for (int i = 0; i < n; i++) { scanf( %d%d , &v[i].first, &v[i].second); } sort(v.begin(), v.end()); v.emplace_back(h, w); for (int i = 0; i <= n; i++) { dp[i] = comb(v[i].first + v[i].second - 2, v[i].first - 1); for (int j = 0; j < i; j++) { if (v[j].first <= v[i].first && v[j].second <= v[i].second) { dp[i] = add(dp[i], -mul(comb(v[i].first + v[i].second - (v[j].first + v[j].second), v[i].first - v[j].first), dp[j])); if (dp[i] < 0) dp[i] += MOD; } } } printf( %d n , dp[n]); return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> grid; int main() { int n; cin >> n; string line = ; for (int i = 0; i < n; i++) { cin >> line; grid.push_back(line); } bool rowblock = false; bool colblock = false; for (int i = 0; i < n; i++) { bool found = false; for (int j = 0; j < n; j++) { if (grid[i][j] == . ) { found = true; } } if (!found) { rowblock = true; } } for (int j = 0; j < n; j++) { bool found = false; for (int i = 0; i < n; i++) { if (grid[i][j] == . ) { found = true; } } if (!found) { colblock = true; } } if (rowblock && colblock) { cout << -1 << endl; return 0; } if (rowblock == false) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == . ) { cout << i + 1 << << j + 1 << endl; break; } } } } else { for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { if (grid[i][j] == . ) { cout << i + 1 << << j + 1 << endl; break; } } } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; const long long MOD = 1e9 + 7; long long n, a[55]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[0] == a[n / 2]) cout << Bob ; else cout << Alice ; }
#include <bits/stdc++.h> using namespace std; int n; int a[1005]; int main() { cin >> n; int x; int ans = 0; if (n == 1) { cin >> x; if (x) cout << YES << endl; else cout << NO << endl; return 0; } for (int i = 0; i < n; i++) { cin >> x; ans += x; } if (ans == n - 1) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double ex = 1e-6; double pow(double x, int n) { double ret = 1; while (n) { if (n % 2) ret *= x; n >>= 1; x *= x; } return ret; } int main() { double n; int m; while (scanf( %lf %d , &n, &m) != EOF) { double ans = n; double i = n - 1; double k; for (i; i >= 1; i -= 1) { k = pow(i / n, m); if (k < ex) break; ans -= k; } printf( %0.4lf n , ans); } }
#include <bits/stdc++.h> using namespace std; struct coord { int c, f, v; }; coord var, pos; int n, m, i, x, cont, h; int pasos[1001][1001]; bool ya[1001][1001], e; char mapa[1001][1001]; queue<coord> cola; stack<coord> pila; int main() { scanf( %d%d , &n, &m); for (i = 0; i < n; i++) { scanf( %s , mapa[i]); } for (i = 0; i < n; i++) { for (x = 0; x < m; x++) { if (mapa[i][x] == T ) ya[i][x] = true; else if (mapa[i][x] == E ) { ya[i][x] = true; var.c = i; var.f = x; var.v = 0; cola.push(var); } } } while (!cola.empty()) { pos = cola.front(); cola.pop(); if (pos.c - 1 >= 0 && ya[pos.c - 1][pos.f] == false) { pos.c--; cola.push(pos); ya[pos.c][pos.f] = true; pasos[pos.c][pos.f] = pasos[pos.c + 1][pos.f] + 1; if (mapa[pos.c][pos.f] == S ) { h = pasos[pos.c][pos.f]; } pos.c++; } if (pos.c + 1 < n && ya[pos.c + 1][pos.f] == false) { pos.c++; cola.push(pos); ya[pos.c][pos.f] = true; pasos[pos.c][pos.f] = pasos[pos.c - 1][pos.f] + 1; if (mapa[pos.c][pos.f] == S ) { h = pasos[pos.c][pos.f]; } pos.c--; } if (pos.f - 1 >= 0 && ya[pos.c][pos.f - 1] == false) { pos.f--; cola.push(pos); ya[pos.c][pos.f] = true; pasos[pos.c][pos.f] = pasos[pos.c][pos.f + 1] + 1; if (mapa[pos.c][pos.f] == S ) { h = pasos[pos.c][pos.f]; } pos.f++; } if (pos.f + 1 < m && ya[pos.c][pos.f + 1] == false) { pos.f++; cola.push(pos); ya[pos.c][pos.f] = true; pasos[pos.c][pos.f] = pasos[pos.c][pos.f - 1] + 1; if (mapa[pos.c][pos.f] == S ) { h = pasos[pos.c][pos.f]; } pos.f--; } } for (i = 0; i < n; i++) { for (x = 0; x < m; x++) { if (mapa[i][x] - 0 > 0 && mapa[i][x] - 0 < 10) { if (pasos[i][x] <= h && ya[i][x] == true) cont += mapa[i][x] - 0 ; } } } printf( %d , cont); }
#include <bits/stdc++.h> using namespace std; void solve() { int n, t; while (cin >> n >> t) { int x, y; vector<pair<int, int> > a; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { cin >> x >> y; a.push_back(make_pair(x, y)); } for (int i = 0; i < n; i++) { while (a[i].first < t) a[i].first += a[i].second; v.push_back(make_pair(a[i].first, i)); } sort(v.begin(), v.end()); cout << (v[0].second) + 1 << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
#include <bits/stdc++.h> using namespace std; struct edge { int y, stat; }; struct query { int curNode; int curLength; int curGood; }; vector<edge> vec[100005]; pair<long long, long long> best[100005]; long long path[100005], pathStatus[100005]; int n, m; queue<query> q; set<pair<int, int> > badRoads; map<pair<int, int>, int> goodRoads; int main() { scanf( %d %d , &n, &m); for (int i = 0; i < m; i++) { int x, y, z; scanf( %d %d %d , &x, &y, &z); if (z == 1) goodRoads[make_pair(min(x, y), max(x, y))]++; x--, y--; vec[x].push_back({y, z}); vec[y].push_back({x, z}); } for (int i = 0; i < n; i++) best[i].first = 1e10; q.push({0, 0, 0}); while (!q.empty()) { query ab = q.front(); q.pop(); if (best[ab.curNode].first < ab.curLength || (best[ab.curNode].first == ab.curLength && best[ab.curNode].second > ab.curGood)) continue; int vSize = vec[ab.curNode].size(); for (int i = 0; i < vSize; i++) { int nextNode = vec[ab.curNode][i].y; int nextGood = vec[ab.curNode][i].stat + ab.curGood; if (ab.curLength + 1 < best[nextNode].first || (best[nextNode].first == ab.curLength + 1 && nextGood > best[nextNode].second)) { best[nextNode].first = ab.curLength + 1; best[nextNode].second = nextGood; q.push({nextNode, ab.curLength + 1, nextGood}); path[nextNode] = ab.curNode; pathStatus[nextNode] = vec[ab.curNode][i].stat; } } } int myNode = n - 1; while (myNode) { if (pathStatus[myNode]) { int minVal = min(path[myNode] + 1, myNode + 1LL); int maxVal = max(path[myNode] + 1, myNode + 1LL); goodRoads[make_pair(minVal, maxVal)]--; } else badRoads.insert({path[myNode] + 1, myNode + 1}); myNode = path[myNode]; } vector<pair<pair<int, int>, int> > ans; for (auto it : badRoads) ans.push_back(make_pair(make_pair(it.first, it.second), 1)); for (auto it : goodRoads) { if (it.second == 0) continue; for (int i = 0; i < it.second; i++) ans.push_back(make_pair(make_pair(it.first.first, it.first.second), 0)); } int curSize = ans.size(); printf( %d n , curSize); for (int i = 0; i < curSize; i++) printf( %d %d %d n , ans[i].first.first, ans[i].first.second, ans[i].second); }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; long long int n1, m1; cin >> n1 >> m1; long long int a[n], b[m]; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 0; i < m; i++) cin >> b[i]; if (a[n1 - 1] < b[m - m1]) cout << YES << endl; else cout << NO << endl; } int32_t main() { ios_base ::sync_with_stdio(false), cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> const int INF_INT = 0x3f3f3f3f; const long long INF_LL = 0x7f7f7f7f; const int MOD = 1e9 + 7; const double eps = 1e-10; const double pi = acos(-1); using namespace std; vector<int> g[100010]; int n; double ans; void dfs(int fa, int u, int cnt) { ans += 1.0 / cnt; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == fa) continue; dfs(u, v, cnt + 1); } } int main(int argc, char const *argv[]) { scanf( %d , &n); for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); g[u].push_back(v); g[v].push_back(u); } ans = 0.0; dfs(0, 1, 1); printf( %.12f n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = 100100; inline void add(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } inline int mul(int a, int b) { return (a * 1LL * b) % MOD; } inline int fastPow(int a, int b) { int r = 1; while (b) { if (b & 1) { r = mul(r, a); } a = mul(a, a); b >>= 1; } return r; } inline int inverse(int a) { return fastPow(a, MOD - 2); } int f[MAXN]; int invf[MAXN]; int ten[MAXN]; inline int c(int n, int k) { if (k < 0) { return 0; } if (n < k) { return 0; } return mul(mul(f[n], invf[k]), invf[n - k]); } int main() { clock_t start_time = clock(); f[0] = invf[0] = 1; ten[0] = 1; for (int i = 1; i < MAXN; i++) { ten[i] = mul(ten[i - 1], 10); f[i] = mul(f[i - 1], i); invf[i] = inverse(f[i]); } int n, k; while (scanf( %d%d , &n, &k) == 2) { char ss[n + 1]; scanf( %s , ss); vector<int> s(n, 0); for (int i = 0; i < n; i++) { s[i] = ss[i] - 0 ; if (i > 0) { add(s[i], s[i - 1]); } } int ans = 0; for (int d = 0; d <= n - 2; d++) { int val = mul(ten[d], c(n - d - 2, k - 1)); val = mul(val, s[n - d - 2]); add(ans, val); } for (int i = 0; i < n; i++) { int val = mul(ten[n - i - 1], ss[i] - 0 ); val = mul(val, c(i, k)); add(ans, val); } printf( %d n , ans); } (void)42; ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long n, R = 0, L = 0; cin >> n; long long l[n], r[n]; for (int x = 0; x < n; x++) { cin >> l[x] >> r[x]; L += l[x]; R += r[x]; } int val = abs(R - L); int ans = 0; for (int x = 0; x < n; x++) { int tL = L - (l[x] - r[x]); int tR = R - (r[x] - l[x]); int i = abs(tL - tR); if (i > val) { ans = x + 1; val = i; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } long long int nCrModPFermat(long long int n, long long int r, long long int p) { if (r == 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 % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } void SieveOfEratosthenes(long long int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long int p = 2; p * p <= n; p++) { if (prime[p] == true) for (long long int i = p * p; i <= n; i += p) prime[i] = false; } for (long long int p = 2; p <= n; p++) if (prime[p]) cout << p << ; } int main() { fast(); int t = 1; while (t--) { int n, k; cin >> n >> k; int v[n]; vector<int> a; map<int, int> m; for (int i = 0; i < n; ++i) cin >> v[i]; a.push_back(v[0]); for (int i = 1; i < n; ++i) { if (v[i] != a[a.size() - 1]) a.push_back(v[i]); } n = a.size(); m[a[0]]++; for (int i = 1; i < n; ++i) { if (i < n - 1 && a[i - 1] == a[i + 1]) m[a[i]] += 2; else m[a[i]]++; } int maxi = 0, ans; for (auto x : m) { if (maxi < x.second) { maxi = x.second; ans = x.first; } } cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; const int B = 27397, MOD = 1e9 + 7; const int B1 = 33941, MOD1 = 1e9 + 9; int n, q; struct Node { int nxt[11]; long long sum[11]; Node() { for (int i = 0; i < 10; ++i) nxt[i] = i; memset(sum, 0, sizeof sum); } }; class Tournament { private: int offset, lo, hi, x, y; int nxt_left[11], nxt_right[11]; long long new_sum[11]; vector<Node> tree; void merge(int node) { for (int i = 0; i < 10; ++i) tree[node].sum[i] = tree[2 * node].sum[i] + tree[2 * node + 1].sum[i]; } void propagate(int node) { if (node < offset) { for (int i = 0; i < 10; ++i) { nxt_left[i] = tree[node].nxt[tree[2 * node].nxt[i]]; nxt_right[i] = tree[node].nxt[tree[2 * node + 1].nxt[i]]; } for (int i = 0; i < 10; ++i) { tree[2 * node].nxt[i] = nxt_left[i]; tree[2 * node + 1].nxt[i] = nxt_right[i]; } } memset(new_sum, 0, sizeof new_sum); for (int i = 0; i < 10; ++i) new_sum[tree[node].nxt[i]] += tree[node].sum[i]; for (int i = 0; i < 10; ++i) { tree[node].sum[i] = new_sum[i]; tree[node].nxt[i] = i; } } void update(int node, int from, int to) { propagate(node); if (to <= lo || hi <= from) return; if (from >= lo && to <= hi) { tree[node].nxt[x] = y; propagate(node); return; } update(2 * node, from, (from + to) / 2); update(2 * node + 1, (from + to) / 2, to); merge(node); } long long query(int node, int from, int to) { propagate(node); if (to <= lo || hi <= from) return 0; if (from >= lo && to <= hi) { long long ret = 0; for (int i = 0; i < 10; ++i) ret += (long long)i * tree[node].sum[i]; return ret; } long long left = query(2 * node, from, (from + to) / 2); long long right = query(2 * node + 1, (from + to) / 2, to); return left + right; } public: Tournament(int n) { for (offset = 1; offset <= n; offset <<= 1) ; tree.resize(2 * offset); } void insert(int i, int val) { i += offset; int coef = 1; while (val > 0) { tree[i].sum[val % 10] += (long long)coef; coef *= 10; val /= 10; } for (i >>= 1; i > 0; i >>= 1) merge(i); } void update(int l, int r, int _x, int _y) { lo = l; hi = r; x = _x; y = _y; update(1, 0, offset); } long long query(int l, int r) { lo = l; hi = r; return query(1, 0, offset); } }; int main(void) { scanf( %d%d , &n, &q); Tournament T(n); for (int i = 0; i < n; ++i) { int x; scanf( %d , &x); T.insert(i, x); } for (int i = 0; i < q; ++i) { int t; scanf( %d , &t); if (t == 1) { int l, r, x, y; scanf( %d%d%d%d , &l, &r, &x, &y); --l; if (x == y) continue; T.update(l, r, x, y); continue; } int l, r; scanf( %d%d , &l, &r); --l; printf( %lld n , T.query(l, r)); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > adj[100100]; long long under[100100]; long long n; long long dfs(long long u, long long p) { under[u] = 0; long long next, edge; for (long long i = 0; i < adj[u].size(); i++) { next = adj[u][i].first; edge = adj[u][i].second; if (next != p) { under[u] += edge + dfs(next, u); } } return under[u]; } long long f(long long u, long long p) { long long sum = 0; long long edge, next; for (long long i = 0; i < adj[u].size(); i++) { next = adj[u][i].first; edge = adj[u][i].second; if (next != p) sum += edge + under[next]; } if (sum == 0) return 0; long long best = 100000000ll * 100000000ll; long long now; for (long long i = 0; i < adj[u].size(); i++) { next = adj[u][i].first; edge = adj[u][i].second; if (next != p) { now = (sum - (edge + under[next])) * 2 + edge + f(next, u); best = min(best, now); } } return best; } int main() { cin >> n; long long x, y, edge; for (long long i = 1; i < n; i++) { cin >> x >> y >> edge; adj[x].push_back(pair<long long, long long>(y, edge)); adj[y].push_back(pair<long long, long long>(x, edge)); } dfs(1, -1); cout << f(1, -1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; pair<int, int> answer[404]; set<pair<int, int> > S; set<pair<int, int> >::iterator it, ii; bool check(pair<int, int> c) { ii = S.lower_bound(make_pair(c.first, 0)); if (ii != S.end()) { if (c.first + c.second > (*ii).first) return false; } if (ii != S.begin()) { ii--; if (c.first < (*ii).first + (*ii).second) return false; } return true; } int main() { scanf( %d , &n); S.insert(make_pair(0, 1)); for (int i = 0; i < n; ++i) { int x, y; scanf( %d %d , &x, &y); pair<int, int> c = make_pair(x, y); if (check(c)) { answer[i] = c; S.insert(c); continue; } for (it = S.begin(); it != S.end(); it++) { c = make_pair((*it).first + (*it).second, y); if (check(c)) { S.insert(c); answer[i] = c; break; } } } for (int i = 0; i < n; ++i) printf( %d %d n , answer[i].first, answer[i].second + answer[i].first - 1); }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> g, rv; int ds[3030][3030]; vector<int> md[3030]; int rd[3030][3030]; vector<int> mr[3030]; int main() { int n, m; scanf( %d %d , &n, &m); g.assign(n, vector<int>()); rv.assign(n, vector<int>()); while (m--) { int a, b; scanf( %d %d , &a, &b); a--; b--; if (a == b) continue; g[a].push_back(b); rv[b].push_back(a); } memset(ds, -1, sizeof(ds)); memset(rd, -1, sizeof(rd)); for (int i = 0; i < n; i++) { ds[i][i] = rd[i][i] = 0; queue<int> q; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (i != u) pq.push({ds[i][u], u}); while (pq.size() > 3) { pq.pop(); } for (int j = 0; j < g[u].size(); j++) { int v = g[u][j]; if (ds[i][v] >= 0) { continue; } ds[i][v] = ds[i][u] + 1; q.push(v); } } while (!pq.empty()) { int u = pq.top().second; pq.pop(); md[i].push_back(u); } q = queue<int>(); q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); if (i != u) pq.push({rd[i][u], u}); while (pq.size() > 3) { pq.pop(); } for (int j = 0; j < rv[u].size(); j++) { int v = rv[u][j]; if (rd[i][v] >= 0) continue; rd[i][v] = rd[i][u] + 1; q.push(v); } } while (!pq.empty()) { int u = pq.top().second; pq.pop(); mr[i].push_back(u); } } int ma = 0; int mi[4]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (ds[i][j] < 0) continue; for (int k = 0; k < mr[i].size(); k++) { for (int l = 0; l < md[j].size(); l++) { int a = mr[i][k], b = md[j][l]; if (a == b || a == j) continue; if (b == i) continue; int dist = ds[a][i] + ds[i][j] + ds[j][b]; if (dist > ma) { ma = dist; mi[0] = a; mi[1] = i; mi[2] = j; mi[3] = b; } } } } } printf( %d %d %d %d n , mi[0] + 1, mi[1] + 1, mi[2] + 1, mi[3] + 1); }
#include <bits/stdc++.h> using namespace std; const int N = 1000006; const long long int M = 935172938408392767LL; vector<long long int> kub[N]; long long int kra[N]; long long int maska[N]; int deg[N]; int main() { int n, m, a, b; long long int wynik = 0; scanf( %d%d , &n, &m); kra[1] = 1; for (int i = 2; i <= n; i++) kra[i] = (kra[i - 1] * 2) % M; for (int i = 0; i < m; i++) { scanf( %d%d , &a, &b); maska[a] += kra[b]; maska[b] += kra[a]; maska[a] %= M; maska[b] %= M; deg[a]++; deg[b]++; } for (int i = 1; i <= n; i++) { kub[deg[i]].push_back(maska[i]); kub[deg[i]].push_back((maska[i] + kra[i]) % M); } for (int i = 0; i <= n; i++) { sort(kub[i].begin(), kub[i].end()); a = 1; for (int j = 1; j < kub[i].size(); j++) { if (kub[i][j - 1] == kub[i][j]) a++; else { wynik += ((a - 1) * (long long)a) / 2; a = 1; } } wynik += ((a - 1) * (long long)a) / 2; } printf( %I64d n , wynik); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)4e18 + 7; inline void minimize(long long &x, const long long &y) { if (x > y) x = y; } struct help { int x, s, r; help() { x = s = r = 0; } void input(void) { x = s = r = 0; int k; scanf( %d%d%d , &x, &r, &k); for (int zz = 0; zz < (k); zz = zz + 1) { int v; scanf( %d , &v); v--; s |= (1 << (v)); } } bool operator<(const help &a) const { return (r < a.r); } }; long long f[(1 << (20)) + 7]; help a[111]; int n, m, b; void init(void) { scanf( %d%d%d , &n, &m, &b); for (int i = (1); i <= (n); i = i + 1) a[i].input(); sort(a + 1, a + n + 1); } void process(void) { for (int i = 0; i < ((1 << (m))); i = i + 1) f[i] = INF; f[0] = 0; long long res = INF; for (int i = (1); i <= (n); i = i + 1) { long long cost = 1LL * b * a[i].r; for (int j = 0; j < ((1 << (m))); j = j + 1) if (f[j] < INF) minimize(f[j | a[i].s], f[j] + a[i].x); minimize(res, f[(1 << (m)) - 1] + cost); } if (res < INF) cout << res; else cout << -1; } int main(void) { init(); process(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n; bool init[MAXN], goal[MAXN]; vector<int> e[MAXN]; bool mark[MAXN]; bool even = true; bool eo[2]; queue<int> ans; void dfs(int v) { if (mark[v]) return; mark[v] = true; bool change = false; if ((init[v] != goal[v]) ^ eo[even]) { ans.push(v + 1); eo[even] = !eo[even]; change = true; } for (int i = 0; i < e[v].size(); i++) { int u = e[v][i]; even = !even; dfs(u); even = !even; } if (change) eo[even] = !eo[even]; } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; e[u].push_back(v); e[v].push_back(u); } for (int i = 0; i < n; i++) cin >> init[i]; for (int i = 0; i < n; i++) cin >> goal[i]; dfs(0); cout << ans.size() << endl; while (ans.size() > 0) { cout << ans.front() << endl; ans.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); using ll = long long int; using ld = long double; using pi = pair<int, int>; const double PI = acos(-1.0); const double eps = 1e-9; const ll mod = 1e9 + 7; const int inf = 1e7; const int MAXN = 1e5 + 5; void cp() { int n; cin >> n; vector<int> arr(n); for (int &x : arr) cin >> x; int mn = *min_element(arr.begin(), arr.end()); for (int &x : arr) x -= mn, x++; int one = count(arr.begin(), arr.end(), 1); if (one > n / 2) cout << Bob n ; else cout << Alice n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; while (t--) { cp(); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int Find(string x) { if (x == rock ) return 1; if (x == scissors ) return 2; if (x == paper ) return 3; } int main() { string a, b, c; int sa, sb, sc; cin >> a >> b >> c; sa = Find(a), sb = Find(b), sc = Find(c); if (sa == 1 && sb == 2 && sc == 2) printf( F ); else if (sa == 2 && sb == 3 && sc == 3) printf( F ); else if (sa == 3 && sb == 1 && sc == 1) printf( F ); else if (sa == 2 && sb == 1 && sc == 2) printf( M ); else if (sa == 3 && sb == 2 && sc == 3) printf( M ); else if (sa == 1 && sb == 3 && sc == 1) printf( M ); else if (sa == 2 && sb == 2 && sc == 1) printf( S ); else if (sa == 3 && sb == 3 && sc == 2) printf( S ); else if (sa == 1 && sb == 1 && sc == 3) printf( S ); else printf( ? ); return 0; }
#include <bits/stdc++.h> using namespace std; long long head[100005]; long long memory[100005]; int n, m; bool possible(long long t) { int curr = 1; for (int i = 1; i <= n; i++) { long long headloc = head[i]; if (headloc <= memory[curr]) { headloc += t; while (headloc >= memory[curr] && curr <= m) curr++; } else { if (headloc - t > memory[curr]) return false; long long d = headloc - memory[curr]; long long next = max(headloc + (t - d) / 2, headloc + (t - 2 * d)); headloc = next; while (headloc >= memory[curr] && curr <= m) curr++; } if (curr == m + 1) return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> head[i]; for (int i = 1; i <= m; i++) cin >> memory[i]; long long start = 0; long long endi = 20000000000; while (start < endi) { long long mid = (start + endi) / 2; if (possible(mid)) endi = mid; else start = mid + 1; } cout << endi; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; const int base = 31337; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int logo = 20; const int off = 1 << logo; const int treesiz = off << 1; int n, m, k; vector<int> graph[maxn]; int a[maxn], ss[maxn], cs[maxn]; bool bio[maxn]; int cnt = 0; int dfs(int x) { bio[x] = true; cnt += graph[x].size(); int out = 1; for (int i = 0; i < graph[x].size(); i++) { int tren = graph[x][i]; if (!bio[tren]) out += dfs(tren); } return out; } int main() { scanf( %d%d%d , &n, &m, &k); for (int i = 0; i < k; i++) scanf( %d , a + i); for (int i = 0; i < m; i++) { int a, b; scanf( %d%d , &a, &b); graph[a].push_back(b); graph[b].push_back(a); } memset(bio, false, sizeof bio); int maxi = 0; for (int i = 0; i < k; i++) { ss[i] = dfs(a[i]); maxi = max(maxi, ss[i]); cs[i] = cnt / 2, cnt = 0; } int ax = 0; for (int i = 1; i <= n; i++) { if (!bio[i]) ax += dfs(i); } long long tren = 0; long long sol = 0; for (int i = 0; i < k; i++) { tren += ss[i] * (ss[i] - 1) / 2 - cs[i]; } cnt /= 2; for (int i = 0; i < k; i++) { int pt = a[i]; int ts = ss[i] + ax; sol = max(sol, tren - (ss[i] * (ss[i] - 1) / 2 - cs[i]) + (ts * (ts - 1) / 2 - cnt - cs[i])); } printf( %lld n , sol); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 300005; struct Point { int x, y; Point(int _x = 0, int _y = 0) { x = _x; y = _y; } Point operator-(const Point &R) const { return Point(x - R.x, y - R.y); } long long operator^(const Point &R) const { return (long long)x * R.y - (long long)y * R.x; } long long operator%(const Point &R) const { return (long long)x * R.x + (long long)y * R.y; } bool operator<(const Point &R) const { if (x != R.x) return x < R.x; return y < R.y; } }; int n, m, p; Point W[N], K[N]; map<Point, int> Hash; long long res[N], ans[N]; int a[N]; bool cmpP(const int &A, const int &B) { return W[A] < W[B]; } bool cmpQ(const int &A, const int &B) { return (long long)K[A].x * K[B].y < (long long)K[B].x * K[A].y; } void update(vector<int> &P, vector<int> &Q) { if (P.empty() || Q.empty()) return; sort(P.begin(), P.end(), cmpP); vector<int> A, B; for (int i = 0; i < Q.size(); ++i) if (K[Q[i]].y >= 0) { A.push_back(Q[i]); } sort(A.begin(), A.end(), cmpQ); vector<Point> s(P.size() + 1); int t = 0; for (int i = 0; i < P.size(); ++i) { Point D = W[P[i]]; while (t > 1 && ((D - s[t - 2]) ^ (s[t - 1] - s[t - 2])) <= 0) --t; s[t++] = D; } for (int i = 0, j = 0; i < A.size(); ++i) { while (j + 1 < t && (K[A[i]] % s[j + 1]) >= (K[A[i]] % s[j])) ++j; res[A[i]] = max(res[A[i]], K[A[i]] % s[j]); } t = 0; for (int i = 0; i < P.size(); ++i) { Point D = W[P[i]]; while (t > 1 && ((D - s[t - 2]) ^ (s[t - 1] - s[t - 2])) >= 0) --t; s[t++] = D; } } int del[N]; void divide(const vector<int> &A) { if (A.size() <= 1) return; vector<int> P, Q; int r = A.size(), mid = r / 2; for (int i = 0; i < mid; ++i) P.push_back(A[i]); divide(P); P.clear(); for (int i = 0; i < r; ++i) if (A[i] > 0 && (A[i] & 1)) del[A[i] >> 1] = 1; for (int i = 0; i < mid; ++i) if (A[i] > 0 && (~A[i] & 1)) { if (!del[A[i] >> 1]) P.push_back(A[i] >> 1); else del[A[i] >> 1] = 2; } for (int i = mid; i < r; ++i) if (A[i] < 0) Q.push_back(-A[i]); update(P, Q); Q.clear(); int c1 = 0, c2 = 0; for (int i = r - 1; i >= mid; --i) { if (A[i] > 0 && (A[i] & 1) && del[A[i] >> 1] == 2) { Q.push_back(A[i] ^ 1); ++c1; } else if (A[i] < 0) { Q.push_back(A[i]); ++c2; } } for (int i = 0; i < r; ++i) if (A[i] > 0 && (A[i] & 1)) del[A[i] >> 1] = 0; if (c1 && c2) divide(Q); P.clear(); for (int i = mid; i < r; ++i) P.push_back(A[i]); divide(P); } int iid[N]; void work() { scanf( %d , &n); vector<int> A; for (int i = 0; i < n; ++i) { Point S; scanf( %d , &a[i]); if (a[i] == 3) { scanf( %d , &S.x); S.y = 1; ++m; res[m] = -1LL << 60; K[m] = S; a[i] = -m; } else { if (a[i] == 1) { scanf( %d%d , &S.x, &S.y); W[++p] = S; iid[i] = p; a[i] = p << 1; } else { int j; scanf( %d , &j); a[i] = iid[j - 1] << 1 | 1; } } A.push_back(a[i]); } divide(A); for (int i = 1; i <= m; ++i) { if (res[i] == -1LL << 60) { puts( EMPTY SET ); } else { printf( %lld n , res[i]); } } } int main() { work(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int MAXN = 1e6 + 7; const double EPS = 1e-6; const int M = 15; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0)); vector<vector<int> > dp(M + 1); vector<vector<pair<int, int> > > nxt(M + 1); for (int m = 0; m <= M; m++) { int N = (1 << m); dp[m].resize(N, INF); nxt[m].resize(N); queue<int> q; if (m < M) { q.push(0); dp[m][0] = 0; } else { for (int i = 0; i < 8; i++) { q.push(i << 12); dp[M][i << 12] = 0; } } while (!q.empty()) { int mask = q.front(); q.pop(); for (int a = 0; a < m; a++) { for (int k = 1; k < m; k++) { int b = a + k, c = a + 2 * k; if (c >= m) break; int mask1 = mask; mask1 ^= (1 << a); mask1 ^= (1 << b); mask1 ^= (1 << c); if (dp[m][mask1] != INF) continue; dp[m][mask1] = dp[m][mask] + 1; nxt[m][mask1] = {a, k}; q.push(mask1); } } } } int n; cin >> n; vector<int> all(n); for (int i = 0; i < n; i++) { cin >> all[i]; } if (n < 15) { int mask = 0; for (int i = n - 1; i >= 0; i--) { mask = mask * 2 + all[i]; } if (dp[n][mask] == INF) { cout << NO n ; return 0; } cout << YES n ; cout << dp[n][mask] << n ; while (mask) { int a = nxt[n][mask].first, k = nxt[n][mask].second; mask ^= (1 << a); mask ^= (1 << (a + k)); mask ^= (1 << (a + 2 * k)); cout << a + 1 << << a + 1 + k << << a + 1 + 2 * k << n ; } return 0; } cout << YES n ; vector<pair<int, int> > res; for (int i = 0, sz = M, fl = false; !fl; i += 12) { if (i >= n - 14) { sz = 14; i = n - 14; fl = true; } int mask = 0; for (int j = sz - 1; j >= 0; j--) { mask = mask * 2 + all[i + j]; } assert(dp[sz][mask] != INF); while (dp[sz][mask]) { int a = nxt[sz][mask].first, k = nxt[sz][mask].second; int b = a + k, c = a + 2 * k; all[i + a] ^= 1; all[i + b] ^= 1; all[i + c] ^= 1; mask ^= (1 << a); mask ^= (1 << b); mask ^= (1 << c); res.push_back({i + a, k}); } } for (int i = 0; i < n; i++) { assert(!all[i]); } cout << res.size() << n ; for (auto p : res) { int a = p.first, k = p.second; cout << a + 1 << << a + k + 1 << << a + 2 * k + 1 << n ; } }
# include <algorithm> # include <iostream> # include <numeric> # include <vector> # include <set> using namespace std; int main() { int n, a, b; while (cin >> n >> a >> b) { string s; cin >> s; set<char> chars_set; for (char c : s) { chars_set.insert(c); } vector<char> chars(chars_set.begin(), chars_set.end()); int cc = (int)chars.size(); vector<int> L(cc, n + 1); vector<int> R(cc, -1); vector<int> cnt(cc, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < cc; j++) { if (s[i] == chars[j]) { L[j] = min(L[j], i); R[j] = max(R[j], i); cnt[j] += 1; } } } vector<int> order(cc); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { return L[i] < L[j]; }); vector<int> order2 = order; sort(order2.begin(), order2.end(), [&](int i, int j) { return R[i] < R[j]; }); vector<int> order3 = order; sort(order3.begin(), order3.end(), [&](int i, int j) { return (L[i] + R[i]) / 2 < (L[j] + R[j]) / 2; }); vector<int> order4 = order; sort(order4.begin(), order4.end(), [&](int i, int j) { return cnt[i] < cnt[j]; }); vector<bool> can(1 << cc, 0); can[0] = 1; for (int t = 1; t < (1 << cc); t++) { int max_R = -1; int min_L = -1; int total = 0; for (int i : order) { if (!(t & (1 << i))) { continue; } if (min_L == -1) { min_L = L[i]; } total += cnt[i]; max_R = max(max_R, R[i]); } int len = max_R - min_L + 1; if (total * b >= a * len) { can[t] = 0; for (int i = 0; i < cc; i++) { if (t & (1 << i)) { if (can[t ^ (1 << i)]) { can[t] = 1; break; } } } if (can[t]) { continue; } } int nt = 0; for (auto& v : {order, order2, order3, order4}) { for (int i : v) { if (!(t & (1 << i))) { continue; } nt |= (1 << i); if (nt != t && can[nt] && can[t ^ nt]) { can[t] = 1; break; } } } } string ret = ; for (int i = 0; i < cc; i++) { if (can[(1 << cc) - 1 - (1 << i)]) { ret += chars[i]; } } cout << ret.size(); for (char c : ret) { cout << << c; } cout << n ; } return 0; } //
#include <bits/stdc++.h> const int maxn = 10010; const int mod = 1000000007; int n; char ch[4][maxn]; int f[maxn][8][2]; int obs[maxn]; int ot, vt; void part0() { memset(obs, 0, sizeof(obs)); for (int j = 1; j <= n; j++) for (int i = 3; i >= 1; i--) obs[j] = (obs[j] << 1) + ((ch[i][j] != . ) ? 1 : 0); for (int i = 1; i <= 3; i++) for (int j = 1; j <= n; j++) if (ch[i][j] == O ) { ot = j; vt = 1 << (i - 1); } } void init() { scanf( %d , &n); for (int i = 1; i <= 3; i++) scanf( %s , ch[i] + 1); } void solve() { part0(); memset(f, 0, sizeof(f)); f[0][7][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j < 8; j++) for (int k = 0; k < 2; k++) { if (!f[i - 1][j][k]) continue; int cnt = j ^ 7; if (cnt & obs[i]) continue; int oc = cnt | obs[i]; for (int c = 0; c <= 6; c += 3) { if (oc & c) continue; int oct = oc | c; if ((i == ot - 1) && (cnt & vt) || (i == ot) && (!(oc ^ vt)) && (vt != 2) || (i - 2 == ot) && (cnt & vt)) { f[i][oct][1] = (f[i][oct][1] + f[i - 1][j][k]) % mod; } else f[i][oct][k] = (f[i][oct][k] + f[i - 1][j][k]) % mod; } } } void print() { printf( %d n , f[n][7][1]); } int main() { init(); solve(); print(); return 0; }
#include <bits/stdc++.h> using namespace std; int bs(int arr[], int n, int target) { int left = 0, mid, right = n - 1; while (left <= right) { mid = left + (right - left) / 2; if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return left + 1; } int main() { int n, m; cin >> n >> m; int songs[n]; int c, t, sum = 0; for (int i = 0; i < n; i++) { cin >> c >> t; sum += (c * t); songs[i] = sum; } int query; while (m--) { cin >> query; cout << bs(songs, n, query) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int N = 5e5; int main() { long long n, a, b, c, t; cin >> n >> a >> b >> c >> t; long long arr[n + 1]; for (long long i = 1; i <= n; i++) { cin >> arr[i]; } long long s = n * a; if (b >= c) { cout << s << endl; } else { long long q = 1; long long s2 = 0; long long q2 = 1; long long s3 = 0; for (long long i = 1; i <= n; i++) { if (arr[i] != t) { s2 += (t - arr[i]) * c; q++; } } for (long long i = 1; i <= n; i++) { arr[i] = a - (t - arr[i]) * b; } for (long long i = 1; i <= n; i++) { s3 += arr[i]; } cout << s2 + s3; } }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; int main() { int n, d, h; cin >> n >> d >> h; if (h * 2 < d || h > n || d > n || h > d || n < d + 1 || (h == 1 && d == 1 && (n != 2))) cout << -1; else { if (h == 1) { for (int i = 2; i <= n; i++) cout << 1 << << i << endl; return 0; } for (int i = 1; i <= h; i++) cout << i << << i + 1 << endl; for (int i = 1; i <= d - h; i++) cout << ((i == 1) ? 1 : i + h) << << i + h + 1 << endl; for (int i = 0; i < n - d - 1; i++) cout << 2 << << n - i << endl; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) #pragma GCC target( sse2 ) using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cerr << name << : << arg1 << n ; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); std::cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } long long max(long long a, long long b) { return (a > b) ? a : b; } long long min(long long a, long long b) { return (a < b) ? a : b; } void amax(long long& a, long long b) { a = max(a, b) % 1000000007; } void amin(long long& a, long long b) { a = min(a, b) % 1000000007; } void inc(long long& a, long long b) { a = (a + b) % 1000000007; } void dec(long long& a, long long b) { a = (a - b + 1000000007) % 1000000007; } long long solve() { long long n, p; cin >> n >> p; vector<long long> in(n + 10, 1e9), out(n + 10, 0); for (long long i = 0; i < p; i++) { long long x; cin >> x; amin(in[x], i + 1); amax(out[x], i + 1); } long long ans = 0; for (long long i = 1; i < n + 1; i++) { if (out[i] == 0) { ans++; } long long cur = out[i]; if (in[i] > out[i + 1] && i + 1 <= n) ans++; if (in[i] > out[i - 1] && i > 1) ans++; } cout << ans; return 0; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, res; cin >> n; while (1) { if (n % 3 != 0) { res = (n / 3) + 1; break; } n = n / 3; } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; int n, arr[206]; char str[206]; inline bool check1() { for (int i = 0; i < (n >> 1); ++i) { if (arr[i] >= arr[i + (n >> 1)]) return 0; } return 1; } inline bool check2() { for (int i = 0; i < (n >> 1); ++i) { if (arr[i] <= arr[i + (n >> 1)]) return 0; } return 1; } int main() { scanf( %d , &n); scanf( %s , str); n = strlen(str); for (int i = 0; i < n; ++i) arr[i] = (int)(str[i] - 0 ); sort(arr, arr + (n / 2)); sort(arr + (n / 2), arr + n); puts((check1() || check2()) ? YES : NO ); return 0; }
#include <bits/stdc++.h> using namespace std; long long int n, k; void inp() { cin >> n >> k; return; } void solve() { set<long long int> second; for (long long int i = 1; i <= k; i++) if (second.count(n % i)) return void(cout << No n ); else second.insert(n % i); cout << Yes n ; return; } int main() { clock_t beg = clock(); { cin.tie(0); ios_base::sync_with_stdio(false); }; inp(); solve(); clock_t end = clock(); fprintf(stderr, %.3f sec n , (double)(end - beg) / CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> const int MAXN = 5; const double eps = 1e-4; struct node { double x, y; node(double x = 0, double y = 0) : x(x), y(y) {} inline bool operator<(const node &rhs) const { if (fabs(x - rhs.x) >= eps) return x < rhs.x; if (fabs(y - rhs.y) >= eps) return y < rhs.y; return 0; } inline bool operator==(const node &rhs) const { return !(*this < rhs) && !(rhs < *this); } } p[MAXN * MAXN << 1]; struct circle { double a, b, r; circle(double a = 0, double b = 0, double r = 0) : a(a), b(b), r(r) {} } c[MAXN]; std::vector<node> ans; inline void getcircle_intersection(circle x, circle y) { if (x.r < y.r) std::swap(x, y); if (fabs((x.r + y.r) * (x.r + y.r) - (x.a - y.a) * (x.a - y.a) - (x.b - y.b) * (x.b - y.b)) < eps) { const double dx = (y.a - x.a), dy = (y.b - x.b), k = x.r / (x.r + y.r); ans.push_back(node(x.a + dx * k, x.b + dy * k)); return; } if (fabs(y.r + sqrt((x.a - y.a) * (x.a - y.a) + (x.b - y.b) * (x.b - y.b)) - x.r) < eps) { const double dx = (y.a - x.a), dy = (y.b - x.b), k = x.r / (x.r - y.r); ans.push_back(node(x.a + dx * k, x.b + dy * k)); return; } if ((x.r + y.r) * (x.r + y.r) < (x.a - y.a) * (x.a - y.a) + (x.b - y.b) * (x.b - y.b)) return; if (y.r + sqrt((x.a - y.a) * (x.a - y.a) + (x.b - y.b) * (x.b - y.b)) < x.r) return; const double lk = (x.a - y.a) / (y.b - x.b), lb = (x.r * x.r - y.r * y.r + y.a * y.a - x.a * x.a + y.b * y.b - x.b * x.b) / (2 * y.b - 2 * x.b); const double fa = lk * lk + 1, fb = 2 * lk * lb - 2 * x.a - 2 * x.b * lk, fc = x.a * x.a + x.b * x.b + lb * lb - 2 * x.b * lb - x.r * x.r; double dt = fb * fb - 4 * fa * fc; if (fabs(dt) > eps && dt < 0) return; dt = fabs(dt); const double ax1 = (-fb + sqrt(dt)) / (2 * fa), ax2 = (-fb - sqrt(dt)) / (2 * fa); ans.push_back(node(ax1, lk * ax1 + lb)); ans.push_back(node(ax2, lk * ax2 + lb)); } inline bool point_on_circle(const node &x, const circle &y) { return fabs((x.x - y.a) * (x.x - y.a) + (x.y - y.b) * (x.y - y.b) - y.r * y.r) <= eps; } int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { double a, b, r; scanf( %lf%lf%lf , &a, &b, &r); c[i].a = a * cos(233) + b * sin(233); c[i].b = -a * sin(233) + b * cos(233); c[i].r = r; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) getcircle_intersection(c[i], c[j]); std::sort(ans.begin(), ans.end()); int sz = std::unique(ans.begin(), ans.end()) - ans.begin(); int tot = 0; for (int i = 1; i <= n; i++) { int ptot = 0; for (unsigned j = 0; j < sz; j++) ptot += point_on_circle(ans[j], c[i]); tot += ptot + !ptot; } if (sz == 0) tot--; printf( %d n , tot - sz + 2); return 0; }
#include <bits/stdc++.h> const int maxn = 2e5 + 5; using namespace std; int a[maxn * 2]; int main() { int n; cin >> n; int flag = 0; for (int t = 0; t < n; t++) { scanf( %d , &a[t]); if (a[t] == 1) { flag = 1; } } for (int t = n; t < 2 * n; t++) { a[t] = a[t - n]; } int s = 1; int maxnn = -1; for (int t = 1; t < 2 * n; t++) { if (a[t] == a[t - 1] && a[t] == 1) { s++; } else { s = 1; } if (s > maxnn) { maxnn = s; } } if (maxnn > n) { maxnn = n; } if (flag == 1) cout << maxnn << endl; else { cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> computer(100, 0); int n, q, free_computers; long s; int t, k, d; void question() { cin >> t >> k >> d; free_computers = 0; for (int j = 0; j < n && free_computers < k; j++) { if (computer[j] <= t) free_computers++; } if (k > free_computers) cout << -1 << n ; else { s = 0; for (int j = 0; j < n && k != 0; j++) { if (computer[j] <= t) { k--; computer[j] = d + t; s += j + 1; } } cout << s << n ; } } int main() { cin >> n >> q; for (int i = 0; i < q; i++) question(); return 0; }
#include <bits/stdc++.h> using namespace std; int Int() { int x; cin >> x; return x; } double din() { double x; cin >> x; return x; } string Str() { string x; getline(cin, x); return x; } char Ch() { char x; cin >> x; return x; } int main() { int h1 = Int(); char ch = Ch(); int m1 = Int(); int h2 = Int(); ch = Ch(); int m2 = Int(); int x = (((h2 * 60) + m2) - ((h1 * 60) + m1)) / 2; int y = ((h1 * 60) + m1) + x; int z; h1 = y / 60; m1 = y % 60; cout << setfill( 0 ) << setw(2) << h1 << : << setfill( 0 ) << setw(2) << m1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[200008], sign[200008]; int b[200008]; int main() { int n; scanf( %d , &n); int t; memset(a, 0, sizeof(a)); memset(sign, 0, sizeof(sign)); for (int i = 0; i < n; i++) { scanf( %d , &t); int k = t; int time = 0; b[i] = t; while (k) { a[k] += time; sign[k]++; if (k & 1 && k > 1) { int kk = k / 2 * 2, tim = time + 2; while (kk < 200008) { a[kk] += tim; sign[kk]++; kk <<= 1; tim++; } } k >>= 1; time++; } k = t, time = 0; while (k < 200008) { a[k] += time; sign[k]++; k <<= 1; time++; } sign[t]--; } int res = 0x3f3f3f3f; for (int i = 1; i < 200008; i++) { if (sign[i] == n) { res = min(res, a[i]); } } printf( %d n , res); }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << n ; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cout.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } long long pows(long long b, long long e) { if (e == 0) return 1; else if (e % 2 == 0) { long long a = pow(b, e / 2); return a * a; } else { long long a = pow(b, e / 2); return b * a * a; } } long long powm(long long x, long long y, long long m) { x = x % m; long long res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long modInverse(long long a, long long m = 1000000007) { if (m == 1) return 0; long long m0 = m, y = 0, x = 1; while (a > 1) { long long q = a / m, t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, i, j, k, x, y, z, e, f, p, q, g, l, r, w, h, count1 = 0, prod = 1, a, b, c, d, index, x1, x2, diff, ans = 0, sum = 0, sum1 = 0, sum2 = 0, flag = 0, flag1 = 0, flag2 = 0; string s, s1, s2; cin >> n; if (n == 1) { cout << 1 ; return 0; } else { ans = 1; k = 3; for (i = 2; i <= n; i++) { ans += k * 4; k += 3; } cout << ans; } }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } class Graph { public: int vn; int sumcost = 0; vector<vector<pair<int, int>>> g; Graph(int n) { vn = n; g.resize(n); } virtual void con(int a, int b, int w) = 0; int getWeight(int f, int t) { auto itr = lower_bound((g[f]).begin(), (g[f]).end(), make_pair(t, INT_MIN)); if (itr != g[f].end()) return itr->second; return INT_MIN; } int Costsum() { return sumcost; } void scan(int edcount, bool oindexed, bool w) { for (int i = (0); (edcount) > i; i++) { int a, b, c = 1; scanf( %d %d , &a, &b); if (w) scanf( %d , &c); con(a - oindexed, b - oindexed, c); } } }; class BiDGraph : public Graph { public: BiDGraph(int n) : Graph(n) {} void con(int a, int b, int w = 1) { g[a].push_back({b, w}); g[b].push_back({a, w}); sumcost++; } }; class DGraph : public Graph { public: DGraph(int n) : Graph(n) {} void con(int a, int b, int w = 1) { g[a].push_back({b, w}); sumcost++; } }; signed main() { int n; scanf( %d , &n); printf( %d n , (n - 1) / 2); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> arr; int n, a; cin >> n; for (int i = 0; i < n; i++) { cin >> a; arr.push_back(a); } sort(arr.begin(), arr.end()); cout << arr[n - 1] << endl; arr.clear(); return 0; }
#include <bits/stdc++.h> int len, p; char s[111111], buf[111111]; void add(char ch) { buf[len++] = ch; } void out() { printf( < ); for (int i = 0; i < len; i++) printf( %c , buf[i]); printf( > n ); len = 0; } int main() { gets(s); len = p = 0; for (p = 0; s[p]; p++) { if (s[p] == ) { p++; while (s[p] != ) { add(s[p]); p++; } out(); } else if (s[p] == ) { if (len) out(); } else add(s[p]); } if (len) out(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2, s3, s4; long long int n; cin >> s1 >> s2; cin >> n; for (long long int i = 0; i < n; i++) { cout << s1 << << s2 << endl; cin >> s3 >> s4; if (s1.compare(s3) == 0) { s1 = s4; } else { s2 = s4; } } cout << s1 << << s2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100; char s[maxn]; int l; long long make(int x, long long u) { int z = l - x; long long k = 1, u1 = 0; for (int i = 0; i < z; i++) { u1 += (s[l - 1 - i] - 0 ) * k; k *= 10; } u1 += u / k * k; if (u1 <= u) u1 += k; return u1; } int main() { int t; cin >> t; while (t--) { scanf( %s , s); l = strlen(s); long long u = 1988; for (int i = l - 1; i >= 4; i--) { u = make(i, u); } cout << u << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, q, g[13][13], lca[13][13]; long long dp[1 << 13][13]; long long f(int s, int root) { s &= ~(1 << root); if (s == 0) return 1; if (dp[s][root] != -1) return dp[s][root]; long long res = 0; int must = 0; for (int i = (0); i < (n); i++) if (s & 1 << i) { must = i; break; } for (int x = (0); x < (1 << n); x++) if (((x | s) == s) && (x & 1 << must)) { int ny = s ^ x; int y = ny | 1 << root; bool bad = false; for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) if ((x & 1 << i) && (x & 1 << j)) { if (lca[i][j] != -1 && (y & 1 << lca[i][j])) { bad = true; } } for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) if ((x & 1 << i) && (y & 1 << j)) { if (lca[i][j] != -1 && lca[i][j] != root) { bad = true; } } for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) if ((x & 1 << i) && (ny & 1 << j)) { if (g[i][j]) bad = true; } int c = 0, x_root = -1; for (int i = (0); i < (n); i++) if (x & 1 << i) { if (g[root][i]) { c++; x_root = i; } } if (c >= 2) bad = true; if (bad) continue; for (int i = (0); i < (n); i++) if (x & 1 << i) { if (x_root != -1 && i != x_root) continue; res += f(x, i) * f(y, root); } } return dp[s][root] = res; } int main() { cin >> n >> m >> q; for (int i = (0); i < (m); i++) { int u, v; cin >> u >> v; u--; v--; g[u][v] = g[v][u] = 1; } memset(lca, -1, sizeof(lca)); for (int i = (0); i < (n); i++) lca[i][i] = i; for (int i = (0); i < (q); i++) { int a, b, c; cin >> a >> b >> c; a--; b--; c--; if (lca[a][b] != -1 && lca[a][b] != c) { cout << 0 << endl; return 0; } lca[a][b] = lca[b][a] = c; } memset(dp, -1, sizeof(dp)); cout << f((1 << n) - 1, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; long long MAX = 1e17; string s1, s2, v; int dp[101][101][101], pre[101][101][101]; void prt(int a, int b, int c) { if (a == 0 or b == 0) return; int x = pre[a][b][c]; if (x == 0) { prt(a - 1, b - 1, c); } else if (x == 1) { prt(a - 1, b - 1, c - 1); cout << s1[a - 1]; } else if (x == 2) { prt(a - 1, b, c); } else if (x == 3) { prt(a, b - 1, c); } else if (x < 0) { x = -x - 1; prt(a - 1, b - 1, x); cout << s1[a - 1]; } } int main() { ios_base::sync_with_stdio(0); ; cin >> s1 >> s2 >> v; int n, m, k; n = s1.size(); m = s2.size(); k = v.size(); int lps[k]; int p = 0, q2 = 1; lps[0] = 0; while (q2 < k) { if (v[p] == v[q2]) lps[q2++] = ++p; else if (p == 0) lps[q2++] = 0; else p = lps[p - 1]; } for (int i = 1; i < n + 1; i++) { for (int j = 1; j < m + 1; j++) { for (int l = 0; l < k; l++) { if (i < l or j < l) continue; if (l == 0) { if (s1[i - 1] == s2[j - 1]) { char q = s1[i - 1]; int x = dp[i - 1][j - 1][0], z = 0; for (int f = 0; f < k; f++) { int y = dp[i - 1][j - 1][f]; int p = f; while (p >= 0) { if (q == v[p]) break; if (p == 0) { if (1 + y > x) z = -1 - f; x = max(x, 1 + y); break; } p = lps[p - 1]; } } dp[i][j][0] = x; pre[i][j][0] = z; } else { if (dp[i - 1][j][l] > dp[i][j - 1][l]) pre[i][j][l] = 2; else pre[i][j][l] = 3; dp[i][j][l] = max(dp[i - 1][j][l], dp[i][j - 1][l]); } } else { if (s1[i - 1] == s2[j - 1]) { char q = s1[i - 1]; int x = dp[i - 1][j - 1][l], z = 0; for (int f = l - 1; f < k; f++) { if (q != v[l - 1]) break; int y = dp[i - 1][j - 1][f]; int p = f; while (p >= l - 1) { if (q == v[p] and p > l - 1) break; if (p == l - 1) { if (1 + y > x) z = -1 - f; x = max(x, 1 + y); break; } p = lps[p - 1]; } } dp[i][j][l] = x; pre[i][j][l] = z; } else { if (dp[i - 1][j][l] > dp[i][j - 1][l]) pre[i][j][l] = 2; else pre[i][j][l] = 3; dp[i][j][l] = max(dp[i - 1][j][l], dp[i][j - 1][l]); } } } } } int maxx = -1, crt = 0; for (int i = 0; i < k; i++) if (dp[n][m][i] > maxx) { crt = i; maxx = dp[n][m][i]; } if (dp[n][m][crt] == 0) cout << 0; else prt(n, m, crt); cout << endl; }
#include <bits/stdc++.h> #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) using namespace std; inline bool isDigit(char c) { return 0 <= c && c <= 9 ; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline bool isSquare(T x) { int y = sqrt(x + 0.5); return (y * y) == x; } template <class T1, class T2> inline T1 gcd(T1 a, T2 b) { return b ? gcd(b, a % b) : a; } template <class T1, class T2> inline T1 eqMin(T1& x, const T2& y) { if (T1(y) < x) return x = y; return x; } template <class T1, class T2> inline T1 eqMax(T1& x, const T2& y) { if (T1(y) > x) return x = y; return x; } template <class T1, class T2> inline T1 min(const T1& x, const T2& y) { return x < (T1)y ? x : (T1)y; } template <class T1, class T2> inline T1 max(T1& x, const T2& y) { return x > (T1)y ? x : (T1)y; } template <typename T> inline T getint() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ); if (ch == - ) p = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); return x * p; } template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << ( << p.first << , << p.second << ) ; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << { ; bool was = false; for (const T& x : v) { if (was) os << , ; was = true; os << x; } os << } ; return os; } template <class T> ostream& operator<<(ostream& os, const set<T>& v) { os << [ ; bool was = false; for (const T& x : v) { if (was) os << , ; was = true; os << x; } os << ] ; return os; } template <class T> ostream& operator<<(ostream& os, const multiset<T>& v) { os << [ ; bool was = false; for (const T& x : v) { if (was) os << , ; was = true; os << x; } os << ] ; return os; } template <class T1, class T2> ostream& operator<<(ostream& os, const map<T1, T2>& m) { os << < ; bool was = false; for (const auto& x : m) { if (was) os << , ; was = true; os << x; } os << > ; return os; } template <size_t sz> ostream& operator<<(ostream& os, const bitset<sz>& bit) { os << / ; for (int i = 0; i < sz; i++) os << bit[i]; os << ; return os; } const double PI = acos(-1); const double EPS = 1e-8; const int INF = (int)2e9; const long long LINF = (long long)2e18; const int MOD = (int)1e9 + 7; const int MAXN = (int)1e5 + 10; int n, s, x; int val[MAXN], nxt[MAXN]; int p[MAXN]; bool us[MAXN]; int cnt = 0; void read(int x) { if (us[x]) return; us[x] = true; cout << ? << x << n ; cout.flush(); cin >> val[x] >> nxt[x]; cout.flush(); } int main() { ios_base::sync_with_stdio(false); srand(time(NULL)); cin >> n >> s >> x; cout.flush(); for (int i = 1; i <= n; i++) { p[i] = i; } random_shuffle(p + 1, p + n + 1); read(s); int ss = s; s = 0; val[0] = -1; for (int i = 1; i <= min(n / 2, 1300); i++) { read(p[i]); if (val[p[i]] < x && val[p[i]] > val[s]) { s = p[i]; } } if (val[ss] < x && val[ss] > val[s]) { s = ss; } if (s) { while (val[s] < x) { s = nxt[s]; if (s == -1) { cout << ! -1 << n ; cout.flush(); return 0; } read(s); if (val[s] >= x) { cout << ! << val[s] << n ; cout.flush(); return 0; } if (nxt[s] == -1) { cout << ! -1 << n ; cout.flush(); return 0; } } } cout << ! << val[ss] << n ; cout.flush(); }
#include <bits/stdc++.h> using namespace std; const int N = 510, mod = 1LL * 1000 * 1000 * 1000 + 7; int dp[N][N], n, b[N], num[N][N]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> b[i]; for (int j = 1; j <= n; j++) for (int i = j - 1; i >= 0; i--) { if (j - i <= 2) { dp[i][j] = num[i][j] = 1; continue; } for (int k = i + 1; k + 1 < j; k++) if (b[k] < b[j - 1]) (num[i][j] += 1LL * num[i][k + 1] * dp[k][j - 1] % mod) %= mod; for (int k = i + 1; k < j; k++) (dp[i][j] += 1LL * num[i][k + 1] * dp[k][j] % mod) %= mod; } cout << dp[0][n] << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int mini; map<char, int> M; set<int> s; int n; void rec(int ind, vector<int> c, vector<int> d) { if (ind == 10) { int rem = n; int X = c.size() + d.size(); int i, j; set<int> S = s; set<int>::iterator it; for (i = 0; i < c.size(); i++) { for (j = 0; j < d.size(); j++) { if (S.find(5 * c[i] + d[j]) != S.end()) { S.erase(5 * c[i] + d[j]); } } } for (i = 0; i < c.size(); i++) { int count = 0, flag; for (j = 0; j < 5; j++) { if (S.find(5 * c[i] + j) != S.end()) { count++; flag = j; } } if (count == 1) { S.erase(5 * c[i] + flag); } } for (i = 0; i < d.size(); i++) { int count = 0, flag; for (j = 0; j < 5; j++) { if (S.find(5 * j + d[i]) != S.end()) { count++; flag = j; } } if (count == 1) { S.erase(5 * flag + d[i]); } } if (S.size() == 1 || S.size() == 0) { mini = min(mini, X); } return; } if (ind < 5) { rec(ind + 1, c, d); c.push_back(ind); rec(ind + 1, c, d); } else { rec(ind + 1, c, d); d.push_back(ind - 5); rec(ind + 1, c, d); } } int main() { mini = 10; M[ R ] = 0; M[ G ] = 1; M[ B ] = 2; M[ Y ] = 3; M[ W ] = 4; cin >> n; int i; string st; for (i = 0; i < n; i++) { cin >> st; s.insert(5 * M[st[0]] + st[1] - 1 ); } vector<int> c; vector<int> d; rec(0, c, d); cout << mini << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 110000; const int M = 1100000; const long long mod = 1e9 + 7; int n, m; vector<pair<int, int> > g[N]; int edge[N]; int col[N], dep[N]; int val[N], f[N]; void dfs(int x, int fa) { f[x] = fa; dep[x] = dep[fa] + 1; for (auto p : g[x]) { int y = p.first, id = p.second; if (y == fa) continue; edge[y] = id; dfs(y, x); } } struct query { int x, y, w; friend bool operator<(query a, query b) { return a.w < b.w; } } a[N], b[N]; int main() { scanf( %d , &n); for (int i = 1; i < n; i++) { int x, y; scanf( %d %d , &x, &y); g[x].push_back(make_pair(y, i)), g[y].push_back(make_pair(x, i)); } dfs(1, 0); scanf( %d , &m); for (int i = 1; i <= n; i++) val[i] = 1e6; for (int i = 1; i <= m; i++) { scanf( %d %d %d , &a[i].x, &a[i].y, &a[i].w); b[i] = a[i]; } sort(a + 1, a + 1 + m); for (int i = 1; i <= m; i++) { int nx = a[i].x, ny = a[i].y; while (dep[nx] != dep[ny]) { if (dep[nx] > dep[ny]) val[edge[nx]] = a[i].w, nx = f[nx]; else val[edge[ny]] = a[i].w, ny = f[ny]; } while (nx != ny) { val[edge[nx]] = a[i].w, nx = f[nx]; val[edge[ny]] = a[i].w, ny = f[ny]; } } int flag = 0; for (int i = 1; i <= m; i++) { int nx = b[i].x, ny = b[i].y; int mn = 1e9; while (dep[nx] != dep[ny]) { if (dep[nx] > dep[ny]) mn = min(mn, val[edge[nx]]), nx = f[nx]; else mn = min(mn, val[edge[ny]]), ny = f[ny]; } while (nx != ny) { mn = min(mn, val[edge[nx]]), nx = f[nx]; mn = min(mn, val[edge[ny]]), ny = f[ny]; } if (mn != b[i].w) flag = 1; } if (flag) printf( -1 n ); else { for (int i = 1; i < n; i++) printf( %d , val[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> t; while (t--) { cin >> n; if (n % 7) cout << (n / 7) + 1 << n ; else cout << n / 7 << n ; } return 0; }
#include<iostream> #include<algorithm> using namespace std; long long cmp(long long a,long long b) { return a > b; } int main() { int t,n,i; long long array[200000],alice,bob; cin>>t; while(t--) { alice=0; bob=0; cin>>n; for(i=0;i<n;i++) { scanf( %lld ,&array[i]); } sort(array,array+n,cmp); for(i=0;i<n;i++) { if(i%2==0) { if(array[i]%2==0)alice+=array[i]; } else { if(array[i]%2!=0)bob+=array[i]; } } if(alice>bob)cout<< Alice <<endl; else if(alice<bob)cout<< Bob <<endl; else cout<< Tie <<endl; } }
#include <bits/stdc++.h> using namespace std; static inline void canhazfast() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a / b; a %= b; swap(a, b); x0 -= q * x1; swap(x0, x1); y0 -= q * y1; swap(y0, y1); } x = x0; y = y0; return a; } static inline int ctz(unsigned x) { return __builtin_ctz(x); } static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); } static inline int clz(unsigned x) { return __builtin_clz(x); } static inline int clzll(unsigned long long x) { return __builtin_clzll(x); } static inline int popcnt(unsigned x) { return __builtin_popcount(x); } static inline int popcntll(unsigned long long x) { return __builtin_popcountll(x); } static inline int bsr(unsigned x) { return 31 ^ clz(x); } static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); } struct Node { long long mx, lz; }; Node t[1 << 19]; int a[1 << 18]; long long build(int u, int b, int e, long long s = 0) { if (b + 1 == e) { t[u].mx = a[b] - s; return s + a[b]; } int m = (b + e) / 2, lc = 2 * u, rc = 2 * u + 1; s = build(lc, b, m, s); s = build(rc, m, e, s); t[u].mx = max(t[lc].mx, t[rc].mx); return s; } void push(int u, int sz) { if (!t[u].lz) return; t[u].mx += t[u].lz; if (sz > 1) { t[2 * u].lz += t[u].lz; t[2 * u + 1].lz += t[u].lz; } t[u].lz = 0; } void upd(int u, int b, int e, int pos, int d) { int sz = e - b; if (pos >= e) return push(u, e - b); if (pos < b) return t[u].lz -= d, push(u, sz); if (sz == 1) return t[u].lz += d, push(u, sz); push(u, sz); int m = (b + e) / 2, lc = 2 * u, rc = 2 * u + 1; upd(lc, b, m, pos, d); upd(rc, m, e, pos, d); t[u].mx = max(t[lc].mx, t[rc].mx); } int get(int u, int b, int e) { push(u, e - b); if (t[u].mx < 0) return -2; if (b + 1 == e) return t[u].mx == 0 ? b : -2; int m = (b + e) / 2, lc = 2 * u, rc = 2 * u + 1; int res = get(lc, b, m); if (res < 0) res = get(rc, m, e); return res; } int main() { canhazfast(); int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) cin >> a[i]; build(1, 0, n); for (; q; --q) { int p, x; cin >> p >> x; --p; upd(1, 0, n, p, x - a[p]); a[p] = x; cout << get(1, 0, n) + 1 << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string t; cin >> t; int n = t.size(); int start = (n % 2) ? n / 2 : n / 2 - 1; int j = 1; int k = n / 2; putchar(t[start]); while (k--) { if (start + j <= n - 1) { putchar(t[start + j]); } if (start - j >= 0) { putchar(t[start - j]); } ++j; } return 0; }
#include <bits/stdc++.h> using namespace std; long n, c[long(1e7 + 50)], cnt; void sieve() { c[1] = 1; for (long i = 2; i <= long(1e7); ++i) if (!c[i]) for (long j = 2; i * j <= long(1e7); ++j) c[i * j] = 1; } bool Prime(long x) { if (x < long(1e7)) { if (c[x]) return false; return true; } for (long i = 2; i <= trunc(sqrt(x)); ++i) if (!(x % i)) return false; return true; } int main() { scanf( %ld , &n); if (n <= 7) printf( 1 n%ld , n); else { printf( 3 n ); sieve(); n -= 3; for (long i = n; i >= 1; --i) if (Prime(i) && Prime(n - i)) { printf( %ld %ld %ld , i, n - i, 3); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int const maxn = 1e5 + 5; int n, m, k, p[105], second[105], mx[105]; int ans; int main() { scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= n; ++i) scanf( %d , &p[i]); for (int i = 1; i <= n; ++i) { scanf( %d , &second[i]); if (mx[second[i]] < p[i]) mx[second[i]] = p[i]; } for (int i = 1; i <= k; ++i) { int v; scanf( %d , &v); if (mx[second[v]] > p[v]) ans++; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); const long long mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long tt = 1; while ((tt)--) { long long n, m; cin >> n >> m; if (min(n, m) & 1) { cout << Akshat << n ; } else { cout << Malvika << n ; } } }
#include <bits/stdc++.h> using namespace std; int a[205], max1, n, k, t; int main() { cin >> t; while (t > 0) { t--; scanf( %d%d , &n, &k); for (int i = 0; i < k; i++) scanf( %d , &a[i]); max1 = 0; if (k == 1) cout << max(n - a[0] + 1, a[0]) << endl; else { for (int i = 1; i < k; i++) { if ((a[i] - a[i - 1]) / 2 + 1 > max1) max1 = (a[i] - a[i - 1]) / 2 + 1; } int d = max(n - a[k - 1] + 1, a[0]); if (d > max1) cout << d << endl; else { cout << max1 << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int a[500010], b[500010], l[500010], lb[39], at[39]; vector<int> v[500010]; int main() { int n, q, p = 0, i, r, x, y; cin >> n; for (i = 1; i <= n; i++) scanf( %d , &a[i]); cin >> q; for (i = 1; i <= q; i++) { scanf( %d%d , &l[i], &x); v[x].push_back(i); } for (r = 1; r <= n; r++) { x = a[r]; p = r; for (i = 20; ~i; i--) if ((1 << i) & x) { if (!lb[i]) { lb[i] = x; at[i] = p; break; } if (at[i] < p) swap(lb[i], x), swap(at[i], p); x ^= lb[i]; } for (auto x : v[r]) for (i = 20; ~i; --i) if (at[i] >= l[x]) b[x] = max(b[x], b[x] ^ lb[i]); } for (i = 1; i <= q; i++) printf( %d n , b[i]); }
#include <bits/stdc++.h> using namespace std; long long modpow(long long b, long long e, long long m) { b %= m; if (e == 0) return 1; if (e % 2 == 1) return (b * modpow(b, e - 1, m)) % m; return modpow((b * b) % m, e / 2, m); } long long powten[1005]; vector<long long> mods; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, k, m; cin >> n >> k >> m; for (int i = 0; i < 1005; i++) { if (i == 0) powten[i] = 1; else powten[i] = (10 * powten[i - 1]) % k; } mods.resize(k, 0); for (int i = 0; i < 10; i++) { if (i % k != 0 or i == 0) mods[i % k] = (mods[i % k] + 1) % m; } for (int i = 1; i < n; i++) { vector<long long> temp; temp.resize(k, 0); for (int j = 0; j < 10; j++) { if (i == n - 1 and j == 0) continue; long long del = (j * powten[i]) % k; for (int prev = 0; prev < k; prev++) temp[(prev + del) % k] = (temp[(prev + del) % k] + mods[prev]) % m; } temp[0] = ((i == n - 1) ? 0 : 1); mods = temp; } if (n == 1) mods[0] = 0; long long total = (9 * modpow(10, n - 1, m)) % m; for (int i = 0; i < k; i++) total = (total - mods[i] + m) % m; cout << total << n ; return 0; }
#include <bits/stdc++.h> using namespace std; bool sortin(const pair<long long int, long long int> &e, const pair<long long int, long long int> &f) { return (e.first < f.first); } bool POT(long long int x) { return x && (!(x & (x - 1))); } long long int a, b, c, i, j, k, l, m, n, p, q, r, x, y, z, ts, mn = 10e17, mod = 10e8 + 7; long long int ar[1000002], br[1000002], xr[1000002]; int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; string s; cin >> s >> a >> b; n = s.size(); p = 1; for (long long int i = n - 1; i >= 0; i--) { xr[i] = ((s[i] - 0 ) * p + xr[i + 1]) % b; p = (p * 10) % b; } xr[n] = 1; p = 0; for (long long int i = 0; i <= n - 2; i++) { p = (p * 10 + s[i] - 0 ) % a; if (p == 0 and xr[i + 1] == 0 and s[i + 1] != 0 ) { cout << YES ; cout << endl; cout << s.substr(0, i + 1); cout << endl; cout << s.substr(i + 1, n - i - 1); return 0; } } cout << NO ; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unrolled-loops ) int n, m, b, w, color[200010], ok[200010]; vector<int> a[200010]; void dfs(int u, int c) { color[u] = c; ok[u] = 1; b += (c == 0); w += (c == 1); for (int i = 0; i < a[u].size(); i++) if (!ok[a[u][i]]) dfs(a[u][i], 1 - c); } void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) { a[i].clear(); color[i] = 0; ok[i] = 0; } int u, v; for (int i = 0; i < m; i++) { cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } b = 0; w = 0; dfs(1, 0); cout << min(b, w) << n ; if (b < w) for (int i = 1; i <= n; i++) { if (!color[i]) cout << i << ; } else for (int i = 1; i <= n; i++) { if (color[i]) cout << i << ; } cout << n ; } int main() { int t; cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; inline int Get() { int res = 0, q = 1; char ch = getchar(); while ((ch < 0 || ch > 9 ) && ch != - ) ch = getchar(); if (ch == - ) q = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) res = res * 10 + ch - 0 , ch = getchar(); return res * q; } const double eps = 1e-12, pi = M_PI; const int oo = (int)2e9, mod = (int)1e9 + 7; const long long INF = (long long)1e17; int T; long long a, n, p, h; long long Calc(long long a, long long n, long long p) { long long rd = a * n / p; if (!rd) return ((a) > (p - a * n) ? (a) : (p - a * n)); long long dt = a * n % p, st = p - p % a - a; if (dt < st) --rd; return Calc(min(a - p % a, p % a), rd, a); } int main() { for (scanf( %d n , &T); T--;) { scanf( %I64d %I64d %I64d %I64d n , &a, &n, &p, &h), a %= p; if (a * n < p) printf( %s n , a <= h ? YES : NO ); else printf( %s n , Calc(a, n, p) <= h ? YES : NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; if (n & 1) { for (i = 0; i < n; i++) cout << (i) << ; cout << n ; for (i = 0; i < n; i++) cout << (i) << ; cout << n ; for (i = 0; i < n; i++) cout << ((2 * i) % n) << ; cout << n ; ; } else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int mex(set<int> s) { int v = 0; while (s.count(v) > 0) v++; return v; } int g[65]{0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10}; const int MAXN = 1000007; int s[MAXN]; map<pair<int, long long>, int> dp; int grundy(pair<int, long long> s) { if (dp.find(s) != dp.end()) return dp[s]; long long mask = s.second; int &res = dp[s]; set<int> S; for (int i = 1; i <= 60; i++) { if (!(mask & (1ll << i)) && s.first >= i) { S.insert(grundy(make_pair(s.first - i, (mask | (1ll << i))))); } } while (S.count(res) > 0) res++; return res; } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &s[i]); } if (n == 1) printf( NO n ); else { int XOR = 0; for (int i = 0; i < n; i++) { XOR ^= grundy(make_pair(s[i], 0)); } if (XOR == 0) printf( YES n ); else printf( NO n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long L, v, l, r; cin >> L >> v >> l >> r; long long tot = L / v; long long minus = r / v; minus -= (l - 1) / v; cout << tot - minus << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000000]; int ans[1000000]; int cnt; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ans[a[i]]++; } for (int j = 1; j <= n; j++) if (ans[j] == 0) cnt++; cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct habijabi { long int start_time, end_time; } work[500500]; bool cmp(habijabi a, habijabi b) { if (a.end_time == b.end_time) { return a.start_time < b.start_time; } return a.end_time < b.end_time; } int main() { long int t, n, i, j; scanf( %ld , &n); for (i = 0; i < n; i++) { scanf( %ld %ld , &work[i].start_time, &work[i].end_time); } sort(work, work + n, cmp); int ans = 0, prev_end = -1; for (i = 0; i < n; i++) { if (work[i].start_time > prev_end) { ans++; prev_end = work[i].end_time; } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; using namespace std; int v[5005]; int main() { int t1; cin >> t1; for (int t = 1; t <= t1; t++) { int n, d; cin >> n >> d; memset(v, 0, sizeof(v)); int l = 1, nr = n - 1; v[0] = 1; for (; nr >= v[l - 1] * 2; l++) { v[l] = v[l - 1] * 2; d -= v[l] * l; nr -= v[l]; } if (nr == 0) l--; else d -= nr * l, v[l] = nr; if (d < 0) { cout << NO n ; continue; } int l2; for (l2 = l; d > 0 && l >= 1; l--) { if (v[l] == 1) continue; if (l2 + 1 - l > d) { v[l]--; v[l + d]++; d = 0; break; } v[++l2] = 1; d -= l2 - l; v[l]--; l++; } if (d == 0) { cout << YES n ; queue<int> p[5005]; p[0].push(1); p[0].push(1); for (int i = 1, node = 2; i <= l2; i++) if (v[i] > 0) { cout << p[i - 1].front() << ; p[i - 1].pop(); p[i].push(node); p[i].push(node); v[i--]--; node++; } cout << n ; } else { cout << NO n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, i, j, n, mx, p, b[1001], m; cin >> n >> m; for (i = 1; i <= 1000; i++) b[i] = 0; for (i = 1; i <= m; i++) { mx = -1; p = -1; for (j = 1; j <= n; j++) { cin >> a; if (a > mx) { mx = a; p = j; } } b[p]++; } int bmax = b[1], pmax = 1; for (i = 2; i <= 1000; i++) if (b[i] > bmax) { bmax = b[i]; pmax = i; } cout << pmax; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; bool solve(int, int); bool trySolve(int, int); bool trySolve(int w, int m) { return (m % w == 0 && solve(w, m / w)); } bool solve(int w, int m) { return (w <= 3 || m == 1 || trySolve(w, m - 1) || trySolve(w, m) || trySolve(w, m + 1)); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int w, m; cin >> w >> m; if (w == 2 || w == 3) cout << YES ; else { if (solve(w, m)) cout << YES ; else cout << NO ; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, v[1010]; long double p[1010][1010], a[1010], d[1010]; int work() { int k; long double x = 1e100L; for (int i = 1; i <= n; i++) if (!v[i] && (d[i] + a[i]) / (1 - a[i]) <= x) x = (d[i] + a[i]) / (1 - a[i]), k = i; v[k] = 1, d[k] = (d[k] + a[k]) / (1 - a[k]); if (k == 1) return 1; for (int i = 1; i <= n; i++) if (!v[i]) d[i] += a[i] * p[i][k] * (d[k] + 1), a[i] *= 1 - p[i][k]; return 0; } int main() { cin >> n; for (int i = 1, x; i <= n; i++) { for (int j = 1; j <= n; j++) cin >> x, p[i][j] = 0.01L * x; a[i] = 1; } a[n] = 0; for (int i = 1; i <= n; i++) if (work()) { cout << fixed << setprecision(15) << d[1]; return 0; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int c, d, x, y, a, b; cin >> a >> b >> x >> y >> c >> d; if (c == 0 and d == 0) { if ((a == x and b == y) or (a == -x and b == -y) or (a == y and b == -x) or (a == -y and b == x)) cout << YES << endl; else cout << NO << endl; return 0; } bool possible = false; long long int X = x, Y = y; if ((c * (x - a) + d * (y - b)) % (c * c + d * d) == 0 and (d * (x - a) - c * (y - b)) % (c * c + d * d) == 0) possible = true; x = -Y, y = X; if ((c * (x - a) + d * (y - b)) % (c * c + d * d) == 0 and (d * (x - a) - c * (y - b)) % (c * c + d * d) == 0) possible = true; x = -X, y = -Y; if ((c * (x - a) + d * (y - b)) % (c * c + d * d) == 0 and (d * (x - a) - c * (y - b)) % (c * c + d * d) == 0) possible = true; x = Y, y = -X; if ((c * (x - a) + d * (y - b)) % (c * c + d * d) == 0 and (d * (x - a) - c * (y - b)) % (c * c + d * d) == 0) possible = true; if (possible) cout << YES << endl; else cout << NO << endl; }
#include <bits/stdc++.h> using namespace std; template <class T> int chkmax(T& a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int chkmin(T& a, T b) { if (b < a) { a = b; return 1; } return 0; } template <class iterator> void output(iterator begin, iterator end, ostream& out = cerr) { while (begin != end) { out << (*begin) << ; begin++; } out << endl; } template <class T> void output(T x, ostream& out = cerr) { output(x.begin(), x.end(), out); } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int INF = 1e9; const int N = 26; const int A = 5e4 + 10; vector<int> value; int p[N][A * N]; signed main() { fast_io(); int n; cin >> n; for (int i = 0; i < 2 * n; ++i) { int x; cin >> x; value.push_back(x); } sort(value.begin(), value.end()); int sum = 0; for (int i = 2; i < 2 * n; ++i) { sum += value[i]; } for (int i = 0; i < N; ++i) { for (int j = 0; j < A * N; ++j) { p[i][j] = -1; } } p[0][0] = -2; for (int i = 2; i < value.size(); ++i) { int cur = value[i]; for (int j = n - 1; j > 0; --j) { for (int x = cur; x < A * N; ++x) { if (p[j - 1][x - cur] != -1 && p[j][x] == -1) { p[j][x] = i; } } } } int best = INF; for (int i = 0; i < N * A; ++i) { if (abs(i - (sum - i)) < abs(best - (sum - best)) && p[n - 1][i] != -1) { best = i; } } vector<int> type(2 * n); type[1] = 1; for (int i = n - 1; i > 0; --i) { type[p[i][best]] = 1; best -= value[p[i][best]]; } vector<vector<int> > ans(2); for (int i = 0; i < 2 * n; ++i) { ans[type[i]].push_back(value[i]); } sort(ans[0].begin(), ans[0].end()); sort(ans[1].rbegin(), ans[1].rend()); for (auto v : ans) { for (auto x : v) { cout << x << ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int sum = accumulate(a.begin(), a.end(), 0); vector<vector<int>> dp(n + 1, vector<int>(2, -1)); function<int(int, int)> solve = [&](int idx, int cur) { if (idx == n) return 0; if (dp[idx][cur] != -1) return dp[idx][cur]; int res = 0; if (cur == 0) res = max(a[idx] + solve(idx + 1, !cur), solve(idx + 1, cur)); else res = min(solve(idx + 1, !cur), a[idx] + solve(idx + 1, cur)); return dp[idx][cur] = res; }; int ans = solve(0, 0); cout << sum - ans << << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int nmax = 3e5 + 42; int n, m; struct edge { int u, v, c; }; edge inp[nmax]; edge make_edge(int u, int v, int c) { edge x; x.u = u; x.v = v; x.c = c; return x; } bool cmp(edge a, edge b) { return a.c < b.c; } int ans[nmax], new_values[nmax]; int main() { cin >> n >> m; int a, b, c; for (int i = 1; i <= m; i++) { scanf( %i%i%i , &a, &b, &c); inp[i] = make_edge(a, b, c); } sort(inp + 1, inp + m + 1, cmp); for (int i = 1; i <= m; i++) { int j = i + 1; while (j <= m && inp[i].c == inp[j].c) j++; for (int k = i; k < j; k++) { new_values[inp[k].v] = max(new_values[inp[k].v], ans[inp[k].u] + 1); } for (int k = i; k < j; k++) { ans[inp[k].v] = new_values[inp[k].v]; } i = j - 1; } int outp = 0; for (int i = 1; i <= n; i++) outp = max(outp, ans[i]); cout << outp << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; int x[24 + 2], y[24 + 2]; int dist[24 + 2][24 + 2]; int dp[(1 << 24) + 2]; int nxt[(1 << 24) + 2]; int n; int sqr_dis(int a, int b) { return (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]); } int solve(int mask) { if (!mask) return 0; int &ret = dp[mask]; if (ret != -1) return ret; int i, j, k; ret = inf; for (i = 0; i < n; i++) if (mask & (1 << i)) { for (j = 0; j < n; j++) if (mask & (1 << j)) { k = dist[0][i + 1] + dist[i + 1][j + 1] + dist[j + 1][0] + solve((mask & ~(1 << i)) & ~(1 << j)); if (k < ret) { ret = k; nxt[mask] = (1 << i) | (1 << j); } } break; } return ret; } int main() { int i, j, k; scanf( %d %d %d , &x[0], &y[0], &n); for (i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]); for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) dist[i][j] = dist[j][i] = sqr_dis(i, j); k = 1 << n; for (i = 0; i < k; i++) dp[i] = -1; printf( %d n , solve(k - 1)); printf( 0 ); k--; while (k) { for (i = 0; i < n; i++) if (nxt[k] & (1 << i)) printf( %d , i + 1); printf( 0 ); k ^= nxt[k]; } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int n, arr[1000], dp[1000][10]; int solve(int i, int flag) { if (i >= n) return 0; if (dp[i][flag] != -1) return dp[i][flag]; if (arr[i] == 0) dp[i][flag] = 1 + solve(i + 1, 0); else if (arr[i] == 1) { if (flag == 0 || flag == 1) { dp[i][flag] = min(solve(i + 1, 2), 1 + solve(i + 1, 0)); } else dp[i][flag] = 1 + solve(i + 1, 0); } else if (arr[i] == 2) { if (flag == 0 || flag == 2) { dp[i][flag] = min(1 + solve(i + 1, 0), solve(i + 1, 1)); } else dp[i][flag] = 1 + solve(i + 1, 0); } else { if (flag == 0) { dp[i][flag] = min(solve(i + 1, 2), min(1 + solve(i + 1, 0), solve(i + 1, 1))); } else if (flag == 1) { dp[i][flag] = min(solve(i + 1, 2), 1 + solve(i + 1, 0)); } else { dp[i][flag] = min(solve(i + 1, 1), 1 + solve(i + 1, 0)); } } return dp[i][flag]; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void hike() { int64_t n; cin >> n; string s, t; cin >> s >> t; vector<int64_t> f; for (int64_t i = 0; i < n; ++i) { if (s[i] != t[i]) { f.push_back(i); } } if (f.size() != 2) { cout << No n ; return; } char x1 = s[f[0]], y1 = t[f[0]]; char x2 = s[f[1]], y2 = t[f[1]]; s[f[0]] = y2; t[f[1]] = x1; if (s == t) { cout << Yes n ; return; } s[f[0]] = x1; t[f[1]] = y2; s[f[1]] = y1; t[f[0]] = x2; if (s == t) { cout << Yes n ; return; } cout << No n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int64_t t = 1; cin >> t; while (t--) hike(); return 0; }
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << Error : << errorCode << endl; exit(42); } struct Treap { int x, y; int size; int64_t sum; Treap *l, *r; Treap(int x) : x(x), y(randGen()), size(1), sum(x), l(nullptr), r(nullptr) {} ~Treap() { if (l != nullptr) { delete l; } if (r != nullptr) { delete r; } } }; typedef Treap* pTreap; inline int getSize(pTreap t) { return (t != nullptr) ? t->size : 0; } inline int64_t getSum(pTreap t) { return (t != nullptr) ? t->sum : 0; } inline void update(pTreap t) { if (t != nullptr) { t->size = getSize(t->l) + 1 + getSize(t->r); t->sum = getSum(t->l) + t->x + getSum(t->r); } } inline void split(pTreap t, pTreap& l, pTreap& r, int x) { if (t == nullptr) { l = r = nullptr; } else if (x <= t->x) { split(t->l, l, t->l, x); r = t; } else { split(t->r, t->r, r, x); l = t; } update(l); update(r); } inline void splitSize(pTreap t, pTreap& l, pTreap& r, int count) { if (t == nullptr) { l = r = nullptr; } else if (count <= getSize(t->l)) { splitSize(t->l, l, t->l, count); r = t; } else { splitSize(t->r, t->r, r, count - getSize(t->l) - 1); l = t; } update(l); update(r); } void merge(pTreap& t, pTreap l, pTreap r) { if (l == nullptr || r == nullptr) { t = (l != nullptr) ? l : r; } else if (l->y > r->y) { merge(l->r, l->r, r); t = l; } else { merge(r->l, l, r->l); t = r; } update(t); } inline void insert(pTreap& t, pTreap it) { if (t == nullptr) { t = it; } else if (it->y > t->y) { split(t, it->l, it->r, it->x); t = it; } else if (it->x <= t->x) { insert(t->l, it); } else { insert(t->r, it); } update(t); } inline void erase(pTreap& t, int x) { if (t != nullptr) { if (x == t->x) { pTreap oldT = t; merge(t, t->l, t->r); oldT->l = oldT->r = nullptr; delete oldT; } else if (x < t->x) { erase(t->l, x); } else { erase(t->r, x); } } update(t); } inline void unite(pTreap& t, pTreap l, pTreap r) { if (l == nullptr || r == nullptr) { t = (l != nullptr) ? l : r; } else { if (r->y > l->y) { swap(l, r); } pTreap rl, rr; split(r, rl, rr, l->x); unite(l->l, l->l, rl); unite(l->r, l->r, rr); t = l; } update(t); } void outItem(pTreap t) { cout << t->x << ; } void out(pTreap t) { if (t != nullptr) { out(t->l); outItem(t); out(t->r); } } pTreap nthElement(pTreap t, int n) { if (t == nullptr) { return nullptr; } else if (n == getSize(t->l)) { return t; } else if (n < getSize(t->l)) { return nthElement(t->l, n); } else { return nthElement(t->r, n - getSize(t->l) - 1); } } pTreap find(pTreap t, int x) { if (t == nullptr) { return nullptr; } else if (x == t->x) { return t; } else if (x < t->x) { return find(t->l, x); } else { return find(t->r, x); } } signed main() { ios_base::sync_with_stdio(false); int64_t n, m; cin >> n >> m; vector<int64_t> l(n); for (int64_t i = 1; i < n; i++) { cin >> l[i]; } vector<pair<int64_t, int64_t> > q(m); for (int64_t i = 0; i < m; i++) { cin >> q[i].first >> q[i].second; q[i].first--; } vector<vector<pair<int64_t, int64_t> > > sumByLen(n); for (int64_t i = 0; i < m; i++) { int64_t v = q[i].first; int64_t rng = q[i].second; int64_t len = 0; sumByLen[v].emplace_back(rng, -1); while (v != 0) { int64_t p = (v - 1) / 2; len += l[v]; int64_t pL = p * 2 + 1, pR = p * 2 + 2; int64_t q = -1; if (pL < n && pL != v) { q = pL; } else if (pR < n && pR != v) { q = pR; } if (q >= 0) { sumByLen[q].emplace_back(rng - len - l[q], -1); } v = p; } } for (int64_t i = 0; i < n; i++) { sort(sumByLen[i].begin(), sumByLen[i].end()); } function<pTreap(int64_t, int64_t)> dfs = [&](int64_t v, int64_t fUp) { pTreap p = new Treap(fUp); for (int64_t i = 1; i <= 2; i++) { if (v * 2 + i < n) { pTreap add = dfs(v * 2 + i, fUp + l[v * 2 + i]); unite(p, p, add); } } vector<pTreap> pieces; pieces.reserve(sumByLen[v].size()); int64_t cumSize = 0, cumSum = 0; for (auto& it : sumByLen[v]) { int64_t val = it.first + fUp; int64_t& res = it.second; pieces.push_back(nullptr); split(p, pieces.back(), p, val); cumSize += getSize(pieces.back()); cumSum += getSum(pieces.back()); res = (int64_t)val * cumSize - cumSum; } pTreap q = nullptr; for (auto it : pieces) { merge(q, q, it); } merge(q, q, p); return q; }; dfs(0, 0); vector<int64_t> answers(m); for (int64_t i = 0; i < m; i++) { int64_t v = q[i].first; int64_t rng = q[i].second; int64_t len = 0; answers[i] += lower_bound(sumByLen[v].begin(), sumByLen[v].end(), make_pair(rng, (int64_t)-1)) ->second; while (v != 0) { int64_t p = (v - 1) / 2; len += l[v]; if (rng >= len) { answers[i] += rng - len; } int64_t pL = p * 2 + 1, pR = p * 2 + 2; int64_t q = -1; if (pL < n && pL != v) { q = pL; } else if (pR < n && pR != v) { q = pR; } if (q >= 0) { answers[i] += lower_bound(sumByLen[q].begin(), sumByLen[q].end(), make_pair(rng - len - l[q], (int64_t)-1)) ->second; } v = p; } cout << answers[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, i; cin >> t; for (i = 0; i < t; i++) { string s; cin >> s; int j = 0, k, l = s.length(), sum = 0; while (j < l) { if (s[j] == 1 ) { k = j; j++; while (s[j] == 0 ) j++; if (j - k > 1 && s[j] == 1 ) sum = sum + j - k - 1; } else j++; } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; long long int v[N]; inline void outv(vector<long long int> v) { for (long long int i = 0; i < v.size(); i++) { cout << v[i] << ; } cout << n ; } inline long long int pow(long long int x, long long int n) { long long int ans = 1; while (n > 0) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } int solve() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; string n; cin >> n; int even = 0; int s = 0; int z = 0; for (auto c : n) { if ((c) % 2 == 0) even++; s += c - 0 ; if (c == 0 ) z++; } if (s % 3 == 0 && even > 1 && z > 0) return cout << red << n , 0; ; return cout << cyan << n , 0; ; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; int t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> int main() { int n, k; scanf( %d %d , &n, &k); switch (k) { case 1: printf( -1 n ); break; case 2: if (n < 5) { printf( -1 n ); break; } printf( %d n , n - 1); for (int i = 1; i < n; i++) printf( %d %d n , i, i + 1); break; case 3: if (n < 4) { printf( -1 n ); break; } printf( %d n1 2 n2 3 n , n - 1); for (int i = 4; i <= n; i++) printf( 3 %d n , i); break; default: printf( -1 n ); break; } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int INF = 1000 * 1000 * 1000 + 7; const long long LINF = INF * (long long)INF; const int MAX = 1000 * 100 + 47; long long A[MAX]; long long P[MAX]; long long Q[MAX]; long long R[MAX]; int main() { int n; long long p, q, r; cin >> n >> p >> q >> r; for (int i = (0); i < (n); i++) cin >> A[i]; for (int i = (0); i < (n); i++) { P[i] = p * A[i]; if (i) P[i] = max(P[i], P[i - 1]); Q[i] = P[i] + q * A[i]; if (i) Q[i] = max(Q[i], Q[i - 1]); R[i] = Q[i] + r * A[i]; if (i) R[i] = max(R[i], R[i - 1]); } cout << R[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool ma[101][101]; int m, n; bool a[101][101]; int steluta(int i, int j) { int t = 1; while (j + t <= m && j - t >= 1 && i + t <= n && i - t >= 1 && ma[i][j + t] == 1 && ma[i][j - t] == 1 && ma[i - t][j] == 1 && ma[i + t][j] == 1) { a[i][j + t] = 1; a[i][j - t] = 1; a[i - t][j] = 1; a[i + t][j] = 1; t++; } t--; if (t) a[i][j] = 1; return t; } int main() { int i, j, ramas = 0, c = 0; char ch; cin >> n >> m; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> ch; if (ch == * ) { ma[i][j] = 1; } } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (ma[i][j] == 1) { int val = steluta(i, j); if (val) { for (int w = val; w <= val; w++) { c++; } } } } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (a[i][j] == 0 && ma[i][j] == 1) { ramas++; } } } if (ramas > 0) { cout << -1 ; } else { cout << c << n ; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (ma[i][j] == 1) { int val = steluta(i, j); if (val) { for (int w = val; w <= val; w++) { cout << i << << j << << w << n ; } } } } } } return 0; }
#include <bits/stdc++.h> using namespace std; double dist(double a, double b, double c, double d) { return sqrt((a - c) * (a - c) + (b - d) * (b - d)); } int main() { double x, y, r, a, b, c, d; cin >> x >> y >> r >> a >> b >> c; d = dist(x, y, a, b); if (d - r - c > 0) printf( %.9lf n , (d - r - c) / 2); else { if (d >= abs(r - c) && d <= r + c) printf( 0.000000000000 n ); else { if (r > c) printf( %.9lf n , (r - (d + c)) / 2); else printf( %.9lf n , (c - (d + r)) / 2); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(const T &a) { return a * a; } int x, k, timer, n[3], t[3], y[3], head[3], tail[3]; pair<int, int> z[3][100000]; int main() { scanf( %d%d%d%d%d%d%d , &k, &n[0], &n[1], &n[2], &t[0], &t[1], &t[2]); while (1) { timer++; if (timer >= t[0] + t[1] + t[2]) { for (int i = 0; i < (int)3; i++) if (z[i][tail[i]].first == timer) y[i] -= z[i][tail[i]].second, tail[i]++; x = min(n[2] - y[2], min(n[1] - y[1], n[0] - y[0])); if (x) { k -= x; y[2] += x, y[1] += x, y[0] += x; z[0][head[0]++] = make_pair(timer + t[0], x); z[1][head[1]++] = make_pair(timer + t[1], x); z[2][head[2]++] = make_pair(timer + t[2], x); } if (k <= 0) { printf( %d n , timer); return 0; } } } }
#include <bits/stdc++.h> int main(void) { char z[201], *p; const int H[] = {0, 1, 2, 3, 4, 2, 6, 7, 5, 6}; int s = 999, n = 2, Q[2][8] = {}; while (n--) for (gets(p = z); *p; ++Q[n][H[*p++ - 0 ]]) ; while (s && 8 ^ ++n) if (Q[1][n]) s = std::min(s, Q[0][n] / Q[1][n]); printf( %d , s); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; const int MAXM = 105; const int inf = 0x3f3f3f3fLL; struct NODE { long long a, b; int c, id; }; struct ANS { int prei, prej, prek; long long val; ANS() {} ANS(int _i, int _j, int _k, long long _val) : prei(_i), prej(_j), prek(_k), val(_val) {} }; NODE a[MAXN]; long long d[MAXN][MAXN][MAXM]; ANS pre[MAXN][MAXN][MAXM]; bool cmp(NODE a, NODE b) { return a.c < b.c; } void update(int i, int j, int k, int x, int y, int z, long long val) { if (j == y) { j = y; } if (d[i][j][k] == -1 || d[i][j][k] < d[x][y][z] + val) { d[i][j][k] = d[x][y][z] + val; pre[i][j][k] = ANS(x, y, z, val); } } void printans(int i, int j, int k) { if (i == 0) return; printans(i - 1, pre[i][j][k].prej, pre[i][j][k].prek); printf( %d %I64d n , a[j].id, pre[i][j][k].val); } void solved(int cas) { int n, m; long long K; scanf( %d %d %I64d , &m, &n, &K); for (int i = 1; i <= n; i++) { scanf( %I64d %I64d %d , &a[i].a, &a[i].b, &a[i].c); a[i].id = i; } sort(a + 1, a + n + 1, cmp); memset(d, -1, sizeof(d)); a[0].a = a[0].b = 0; for (int i = 1; i <= n; i++) for (long long j = a[i].a; j <= a[i].b; j++) { d[1][i][j - a[i].a] = j; pre[1][i][j - a[i].a] = ANS(0, -1, -1, j); } for (int i = 1; i < m; i++) { for (int j = i; j <= n; j++) { for (int k = 0; k <= a[j].b - a[j].a; k++) { if (d[i][j][k] == -1) continue; long long lastnum = a[j].a + k; for (int l = j + 1; l <= n; l++) { if (a[l].c == a[j].c) continue; long long temp = lastnum + K; if (temp >= a[l].a && temp <= a[l].b) update(i + 1, l, temp - a[l].a, i, j, k, temp); temp = lastnum * K; if (temp >= a[l].a && temp <= a[l].b) update(i + 1, l, temp - a[l].a, i, j, k, temp); } } } } long long ans = -1; int ansi, ansj; for (int i = m; i <= n; i++) for (int j = 0; j <= 100; j++) { if (d[m][i][j] > ans) { ans = d[m][i][j]; ansi = i; ansj = j; } } if (ans == -1) printf( NO n ); else { printf( YES n ); printans(m, ansi, ansj); } } int main() { int nT = 1; for (int i = 1; i <= nT; i++) solved(i); }
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0L); signed main() { long long n; cin >> n; long double x[n], y[n]; for (long long i = (0); i < (n); ++i) cin >> x[i] >> y[i]; long long ans = 0; for (long long i = (0); i < (n); ++i) { vector<long double> v; for (long long j = (0); j < (n); ++j) if (j != i) { v.push_back(atan2(y[j] - y[i], x[j] - x[i])); } sort(v.begin(), v.end()); long long f[n - 1], s[n - 1]; long long k = 0; for (long long j = (0); j < (n - 1); ++j) { while (k < n - 1 && v[k] < v[j] + pi) ++k; f[j] = k; s[j] = (j ? s[j - 1] : 0) + f[j]; } for (long long j = (0); j < (n - 1); ++j) { ans += (s[f[j] - 1] - s[j]) - (f[j]) * (f[j] - j - 1); } } cout << ans * (n - 4) / 2 << endl; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 0, s = 0; char c = getchar(); while (c > 9 || c < 0 ) f = (c == - ), c = getchar(); while (c >= 0 && c <= 9 ) s = (s << 3) + (s << 1) + (c ^ 0 ), c = getchar(); return f ? -s : s; } long long n, m, k; vector<long long> ch[200005]; vector<long long> he[200005]; long long it1, it2; long long t1, t2; long long sx, sy, sfx; long long tx, ty, tfx; long long nx, ny; char cc[5]; long long ans = 0; bool flag = 0; inline long long ne(long long op, long long t, long long x) { if (op == 1) return ch[t][upper_bound(ch[t].begin(), ch[t].end(), x) - ch[t].begin()]; else return he[t][upper_bound(he[t].begin(), he[t].end(), x) - he[t].begin()]; } inline long long ft(long long op, long long t, long long x) { if (op == 1) return ch[t] [lower_bound(ch[t].begin(), ch[t].end(), x) - ch[t].begin() - 1]; else return he[t] [lower_bound(he[t].begin(), he[t].end(), x) - he[t].begin() - 1]; } inline long long fd(long long op, long long t, long long x) { if (op == 1) { long long tt = lower_bound(ch[t].begin(), ch[t].end(), x) - ch[t].begin(); if (tt >= ch[t].size()) return -1; if (ch[t][tt] == x) return tt; else return -1; } else { long long tt = lower_bound(he[t].begin(), he[t].end(), x) - he[t].begin(); if (tt >= he[t].size()) return -1; if (he[t][tt] == x) return tt; else return -1; } } bool ff = 1, fff = 1; int main() { n = read(); m = read(); k = read(); for (int i = 1; i <= k; i++) { t1 = read(); t2 = read(); ch[t2 - t1 + n + 1].push_back(t1); he[t1 + t2].push_back(t1); } for (int i = 0; i <= n + 1; i++) { ch[m + 1 - i + n + 1].push_back(i); he[m + 1 + i].push_back(i); ch[-i + n + 1].push_back(i); he[i].push_back(i); } for (int i = 0; i <= m + 1; i++) { ch[i + n + 1].push_back(0); he[i].push_back(0); ch[i - n - 1 + n + 1].push_back(n + 1); he[n + 1 + i].push_back(n + 1); } for (int i = 0; i <= n + m + 2; i++) sort(he[i].begin(), he[i].end()); for (int i = 0; i <= m + n + 2; i++) sort(ch[i].begin(), ch[i].end()); sx = read(); sy = read(); scanf( %s , cc); if (cc[0] == N ) { if (cc[1] == E ) sfx = 0; else sfx = 1; } else { if (cc[1] == E ) sfx = 2; else sfx = 3; } tx = sx; ty = sy; tfx = sfx; sx = sy = sfx = -1; while (tx != sx || ty != sy || tfx != sfx || fff) { if (fff && tx == sx && ty == sy && tfx == sfx) fff = 0; if (tfx == 0) { nx = ne(2, tx + ty, tx); ny = tx + ty - nx; ans += nx - tx; it1 = fd(2, tx + ty - 1, nx - 1); it2 = fd(2, tx + ty + 1, nx); if (it1 != -1 && it2 != -1) { tfx = 3; tx = nx - 1; ty = ny + 1; flag = 1; } else if (it1 != -1) { tfx = 2; tx = nx; ty = ny + 1; } else if (it2 != -1) { tfx = 1; tx = nx - 1; ty = ny; } else { tfx = 3; tx = nx - 1; ty = ny + 1; flag = 1; } } else if (tfx == 3) { nx = ft(2, tx + ty, tx); ny = tx + ty - nx; ans += tx - nx; it1 = fd(2, tx + ty - 1, nx); it2 = fd(2, tx + ty + 1, nx + 1); if (it1 != -1 && it2 != -1) { tfx = 0; tx = nx + 1; ty = ny - 1; flag = 1; } else if (it1 != -1) { tfx = 2; tx = nx + 1; ty = ny; } else if (it2 != -1) { tfx = 1; tx = nx; ty = ny - 1; } else { tfx = 0; tx = nx + 1; ty = ny - 1; flag = 1; } } else if (tfx == 2) { nx = ne(1, ty - tx + n + 1, tx); ny = ty - tx + nx; ans += nx - tx; it1 = fd(1, ty - tx + n + 1 - 1, nx); it2 = fd(1, ty - tx + n + 1 + 1, nx - 1); if (it1 != -1 && it2 != -1) { tfx = 1; tx = nx - 1; ty = ny - 1; flag = 1; } else if (it1 != -1) { tfx = 3; tx = nx - 1; ty = ny; } else if (it2 != -1) { tfx = 0; tx = nx; ty = ny - 1; } else { tfx = 1; tx = nx - 1; ty = ny - 1; flag = 1; } } else if (tfx == 1) { nx = ft(1, ty - tx + n + 1, tx); ny = ty - tx + nx; ans += tx - nx; it1 = fd(1, ty - tx + n + 1 - 1, nx + 1); it2 = fd(1, ty - tx + n + 1 + 1, nx); if (it1 != -1 && it2 != -1) { tfx = 2; tx = nx + 1; ty = ny + 1; flag = 1; } else if (it1 != -1) { tfx = 3; tx = nx; ty = ny + 1; } else if (it2 != -1) { tfx = 0; tx = nx + 1; ty = ny; } else { tfx = 2; tx = nx + 1; ty = ny + 1; flag = 1; } } if (ff) { sx = tx; sy = ty; sfx = tfx; ff = 0; ans = 0; } } while (tx != sx || ty != sy || tfx != sfx) ; if (flag) printf( %lld , ans / 2); else printf( %lld , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; long long n, k, sum; long long a[maxn]; long long goal[maxn]; long long max(long long x, long long y) { return x > y ? x : y; } long long cntmx = -9e18, cntmn = 9e18; void solve3() { long long m = k; for (register int i = n; i >= 1; --i) { if (m >= (n - i + 1) * (a[i] - a[i - 1])) { m -= (n - i + 1) * (a[i] - a[i - 1]); } else { cntmn = a[i] - m / (n - i + 1); break; } } if (cntmn == 9e18) cntmn = a[1] - m / n; m = k; for (register int i = 1; i <= n; ++i) { if (m >= i * (a[i + 1] - a[i])) { m -= i * (a[i + 1] - a[i]); } else { cntmx = a[i] + m / i; break; } } if (cntmx == -9e18) cntmx = a[n] + m; if (cntmn - cntmx <= 0) { if (sum % n == 0) printf( 0 n ); else printf( 1 n ); } else printf( %lld n , max(0LL, cntmn - cntmx)); } int main() { scanf( %lld%lld , &n, &k); for (register int i = 1; i <= n; ++i) scanf( %lld , &a[i]), sum += a[i]; sort(a + 1, a + n + 1); solve3(); fclose(stdin); fclose(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; char mi, ma; int max = 0, min = INT_MAX; pair<int, char> p[4]; for (int i = 0; i < 4; ++i) { cin >> s; p[i].first = s.length() - 2; p[i].second = s[0]; } sort(p, p + 4); int flag1 = 0, flag2 = 0; for (int j = 1; j < 4; ++j) { if (2 * (p[0].first) <= p[j].first) flag1 += 1; } for (int j = 0; j < 3; ++j) { if (p[3].first >= 2 * (p[j].first)) flag2 += 1; } if (flag1 == 3 && flag2 != 3) cout << p[0].second << n ; else if (flag2 == 3 && flag1 != 3) cout << p[3].second << n ; else cout << C n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j; long long int x, y; cin >> n >> x >> y; y -= n; if (y < 0) cout << -1 << endl; else { x -= (n - 1); x -= (1 + y) * (1 + y); if (x > 0) cout << -1 << endl; else { cout << (1 + y) << endl; for (int i = 0; i < n - 1; i++) cout << 1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, ji[300], k, qi, mz[400010]; char s[400010], tp[400010]; int main() { scanf( %d%s , &n, s + 1); for (int i = 1; i <= n; i++) { ji[s[i]]++; } for (int i = 0; i < 300; i++) if (ji[i] & 1) k++; qi = k; if (!qi) { for (k = 1;; k++) if (n % k == 0) break; n /= k; printf( %d n , k); if (n & 1) { for (int i = 0, tt = 0; i < 300; i++) { while (ji[i]) { mz[++tt] = i; ji[i]--; if (tt == k) goto qaz; } } qaz:; n >>= 1; for (int i = 1, p = 0; i <= k; i++) { for (int j = 1; j <= n; j++) { while (!ji[p]) p++; ji[p] -= 2; tp[j] = tp[n * 2 + 2 - j] = p; } tp[n + 1] = mz[i]; printf( %s , tp + 1); } return 0; } n >>= 1; for (int i = 1, p = 0; i <= k; i++) { for (int j = 1; j <= n; j++) { while (!ji[p]) p++; ji[p] -= 2; tp[j] = tp[n * 2 + 1 - j] = p; } printf( %s , tp + 1); } return 0; } for (;; k++) if (n % k == 0 && (n / k) & 1) break; printf( %d n , k); n /= k; int tt = 0; for (int i = 0; i < 300; i++) if (ji[i] & 1) { mz[++tt] = i; ji[i]--; } if (tt == k) goto qazz; for (int i = 0; i < 300; i++) { while (ji[i]) { mz[++tt] = i; ji[i]--; if (tt == k) goto qazz; } } qazz:; n >>= 1; for (int i = 1, p = 0; i <= k; i++) { for (int j = 1; j <= n; j++) { while (!ji[p]) p++; ji[p] -= 2; tp[j] = tp[n * 2 + 2 - j] = p; } tp[n + 1] = mz[i]; printf( %s , tp + 1); } return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; #define lli long long int #define llu unsigned long long int #define pb push_back #define rt return 0 #define endln n #define all(x) x.begin(), x.end() #define sz(x) (lli)(x.size()) #define len(x) (lli)(x.length()) const lli MOD = 1e9 + 7; const double PI = 2 * acos(0.0); // cout << fixed << setprecision(0) << pi <<endl; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> // new_data_set; // for multiset // typedef tree<int, null_type, less_equal<int>, rb_tree_tag, // tree_order_statistics_node_update> // new_data_set; // order_of_key(val): returns the number of values less than val // find_by_order(k): returns an iterator to the kth largest element (0-based) void solve() { lli n, a; cin >> n; vector<lli> v; for (lli i = 0; i < n; i++) { cin >> a; v.pb(a); } for (lli i = 0; i < n; i++) { lli tmp = v[i], ind = 0, one; while (tmp > 0) { if (tmp % 2 == 1) { one = ind; } tmp /= 2; ind++; } lli p = pow(2, one); cout << p << ; } cout << endl; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); lli t; cin >> t; while (t--) solve(); rt; }
#include <bits/stdc++.h> using namespace std; const int Max_N = 1E6 + 10; int n, m, ans = 1, p[Max_N], color[Max_N]; vector<int> chil[Max_N]; bool mark[Max_N]; int dfs(int i) { int res = 0; mark[i] = true; for (int u : chil[i]) if (!mark[u] && color[i] != color[u]) ++res; return res; } int main() { cin >> n; for (int i = 1; i < n; i++) { cin >> p[i]; p[i]--; chil[p[i]].push_back(i); } for (int i = 0; i < n; i++) cin >> color[i]; for (int i = 0; i < n; i++) if (!mark[i]) ans += dfs(i); cout << ans; }