func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int x[20005]; int y[20005]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { int xx, yy; scanf( %d %d , &xx, &yy); x[xx + 10000]++; y[yy + 10000]++; } long long ret = 0, sumx = 0, sumy = 0, tx = 0, ty = 0, dx = 0, dy = 0; for (int i = 0; i <= 20000; i++) { ret += (long long)x[i] * tx; ret += (long long)y[i] * ty; dx += sumx; dy += sumy; sumx += x[i]; sumy += y[i]; dx += sumx; dy += sumy; tx += dx; ty += dy; } cout << ret << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; bool maze[3][100001]; int judge(int r, int c) { if (r == 1) { if (c == 100000) return maze[2][99999] + maze[2][100000]; return maze[2][c - 1] + maze[2][c] + maze[2][c + 1]; } else { if (c == 1) return maze[1][1] + maze[1][2]; if (c == 100000) return maze[1][c - 1] + maze[1][c]; return maze[1][c - 1] + maze[1][c] + maze[1][c + 1]; } } int main() { memset(maze, 0, sizeof(maze)); int n, q, cont = 0, r, c; cin >> n >> q; for (int i = 1; i <= q; i++) { cin >> r >> c; if (maze[r][c] == 0) { maze[r][c] = 1; cont += judge(r, c); } else { maze[r][c] = 0; cont -= judge(r, c); } cout << (cont == 0 ? Yes : No ) << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int ESP = 1e18; long long int FCB = 1e9 + 7; long long int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const long long int N = 5005; long long int l[N], r[N]; vector<pair<long long int, long long int>> vv; bool fly(long long int x, long long int y) { if (l[x] >= r[y]) return false; if (r[x] <= l[y]) return false; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int n; cin >> n; for (long long int i = 1; i < n + 1; i++) cin >> l[i] >> r[i]; for (long long int i = 1; i < n + 1; i++) { for (long long int j = 1; j < n + 1; j++) { if (i != j && fly(i, j)) vv.push_back(make_pair(i, j)); } } map<long long int, long long int> mp; long long int zz = vv.size(); for (long long int i = 0; i < zz; i++) { mp[vv[i].first]++; mp[vv[i].second]++; } vector<long long int> ans; for (long long int i = 1; i <= n; i++) { if (mp[i] == zz) ans.push_back(i); } long long int dd = ans.size(); cout << dd << n ; for (long long int i = 0; i < dd; i++) cout << ans[i] << ; cout << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m, k, a[111111]; int main() { cin >> n >> m >> k; if (n % 2 == 0) { cout << 0 << endl; return 0; } for (int i = 0; i < n; i++) cin >> a[i]; int maxans = (int)2e9; for (int i = 0; i < n; i += 2) if (a[i] < maxans) maxans = a[i]; cout << min((long long)maxans, ((long long)m / ((n + 1) / 2)) * k) << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int a[222], b[222]; int main() { int n, ans = 0; cin >> n; int p, q, r, s; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 0; i < n; i++) { p = q = r = s = 0; for (int j = 0; j < n; j++) { if (j == i) continue; if (a[j] > a[i] && b[j] == b[i]) p++; if (a[j] < a[i] && b[j] == b[i]) q++; if (a[j] == a[i] && b[j] > b[i]) r++; if (a[j] == a[i] && b[j] < b[i]) s++; } if (p && q && r && s) ans++; } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int i, temp, temp2, a[n + 1]; for (int i = 0; i < n + 1; i++) a[i] = 0; for (int i = 0; i < n - 1; i++) { cin >> temp >> temp2; a[temp]++; a[temp2]++; } int s = 0; for (int i = 0; i < n + 1; i++) { if (a[i] < 2) s++; } cout << s - 1 << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int t, num[30], ans; char s[100005]; int main() { cin >> t; while (t--) { ans = 0; memset(num, 0, sizeof(num)); scanf( %s , s + 1); int len = strlen(s + 1); for (int i = 1; i <= len; i++) { num[s[i] - A + 1]++; } if (num[ L - A + 1] != 0 && num[ R - A + 1] != 0 && (num[ U - A + 1] == 0 || num[ D - A + 1] == 0)) { printf( 2 n ); printf( LR n ); } else if (num[ U - A + 1] != 0 && num[ D - A + 1] != 0 && (num[ L - A + 1] == 0 || num[ R - A + 1] == 0)) { printf( 2 n ); printf( UD n ); } else if (num[ L - A + 1] != 0 && num[ R - A + 1] != 0 && num[ U - A + 1] != 0 && num[ D - A + 1] != 0) { ans += min(num[ L - A + 1], num[ R - A + 1]) * 2; ans += min(num[ U - A + 1], num[ D - A + 1]) * 2; printf( %d n , ans); for (int i = 1; i <= min(num[ L - A + 1], num[ R - A + 1]); i++) { printf( L ); } for (int i = 1; i <= min(num[ U - A + 1], num[ D - A + 1]); i++) { printf( U ); } for (int i = 1; i <= min(num[ L - A + 1], num[ R - A + 1]); i++) { printf( R ); } for (int i = 1; i <= min(num[ U - A + 1], num[ D - A + 1]); i++) { printf( D ); } printf( n ); } else { printf( 0 n n ); } } }
|
#include <bits/stdc++.h> using namespace std; using namespace std; const long long mod = 1e9 + 7; const int inf = 1e9; string s; long long expMod(long long base, long long ex, long long mod) { if (ex == 0) return 1; long long temp = expMod(base, ex / 2, mod); temp = (temp * temp) % mod; if (ex % 2) temp = (temp * base) % mod; return temp; } long long solve(string x) { long long n = x.size(); if (n == 1) { if (x[0] == 1 ) return 1; else return 0; } string temp = x.substr(1); long long sz = temp.size(); long long ret = 0; if (x[0] == 0 ) { ret = (1LL * 2 * solve(temp)) % mod; } else { ret = (expMod(2, 2 * sz, mod) + 2 * solve(temp)) % mod; } return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> s; cout << solve(s) << n ; return 0; }
|
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); int64_t sum1 = 0; int64_t sum2 = 0; int to_double = 0; int zero = 0; std::set<std::pair<int, int>> doubled; std::set<std::pair<int, int>> pend; for (int i = 1; i <= n; i++) { int x, y; scanf( %d%d , &x, &y); if (y > 0) { if (x == 1) to_double++; sum1 += y; pend.insert({y, x}); } else { y = -y; sum1 -= y; auto it = pend.find({y, x}); if (it != pend.end()) { pend.erase(it); } else { it = doubled.find({y, x}); doubled.erase(it); if (x == 0) zero--; sum2 -= y; } if (x == 1) { to_double--; if (doubled.size() > 0) { auto it = doubled.begin(); std::pair<int, int> now = *it; if (now.second == 0) zero--; doubled.erase(it); sum2 -= now.first; pend.insert(now); } } } if (doubled.size() < to_double && pend.size() > 0) { auto it = --pend.end(); std::pair<int, int> max = *it; doubled.insert(max); if (max.second == 0) zero++; sum2 += max.first; pend.erase(it); } if (doubled.size() > 0 && pend.size() > 0) { auto it = doubled.begin(); auto it2 = --pend.end(); doubled.erase(it); pend.erase(it2); std::pair<int, int> now1 = *it; std::pair<int, int> now2 = *it2; if (now2.first > now1.first) { sum2 += now2.first - now1.first; if (now2.second == 0) zero++; if (now1.second == 0) zero--; doubled.insert(now2); pend.insert(now1); } else { doubled.insert(now1); pend.insert(now2); } } if (doubled.size() > 0 && zero == 0 && doubled.size() == to_double) { auto it1 = doubled.begin(); std::pair<int, int> now = *it1; doubled.erase(it1); sum2 -= now.first; if (pend.size() > 0) { auto it2 = --pend.end(); std::pair<int, int> now2 = *it2; pend.erase(it2); doubled.insert(now2); zero++; sum2 += now2.first; } pend.insert(now); } printf( %lld n , sum1 + sum2); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; if (k % (n - 1) == 0) { cout << (k / (n - 1) * n) - 1 << endl; } else { cout << k / (n - 1) * n + k % (n - 1) << endl; } } }
|
#include <bits/stdc++.h> using namespace std; bool isSeparator(char ch) { return (ch == , || ch == || ch == ; ); } int main() { int i, j; bool isNumber; string s, ss; vector<string> words[2]; cin >> s; s += ; ss = , isNumber = true; for (i = 0; i < s.length(); i++) { if (isSeparator(s[i])) { if (ss.empty() || (isNumber && ss.length() > 1 && ss[0] == 0 )) isNumber = false; words[isNumber].push_back(ss); ss = ; isNumber = true; } else { ss += s[i]; if (!isdigit(s[i])) isNumber = false; } } for (j = 1; j >= 0; j--) { if (words[j].size()) { cout << ; for (i = 0; i < words[j].size(); i++) { if (i) cout << , ; cout << words[j][i]; } cout << << endl; } else cout << - << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int mN = 222222; int n; int a[mN], outa[mN], outb[mN], ge[mN]; vector<int> ans; struct Bit { int c[mN]; void clear() { memset(c, 0, sizeof(c)); } int lob(int x) { return x & (-x); } void add(int x, int dt) { while (x <= n) { c[x] += dt; x += lob(x); } } int sum(int x) { int re = 0; while (x > 0) { re += c[x]; x -= lob(x); } return re; } } bit; void calc(int o[]) { for (int i = 1; i <= n; i++) { cin >> a[i]; } bit.clear(); for (int i = n; i >= 1; i--) { o[i] = bit.sum(a[i] + 1); bit.add(a[i] + 1, 1); } } int bins(int key) { int l = 1, r = n; key++; while (l < r) { int mid = (l + r) / 2; if ((mid)-bit.sum(mid) >= key) r = mid; else l = mid + 1; } return l - 1; } void out(int o[]) { bit.clear(); for (int i = 1; i <= n; i++) { int x = bins(o[i]); ans.push_back(x); bit.add(x + 1, 1); } } int main() { cin >> n; calc(outa); calc(outb); for (int i = n - 1; i >= 1; i--) { ge[i] += outa[i] + outb[i]; int mod = (n + 1) - i; if (ge[i] >= mod) { ge[i - 1] += ge[i] / mod; ge[i] %= mod; } } out(ge); for (int i = 0, n_ = (ans.size()); i < n_; i++) cout << ans[i] << ((i == ans.size() - 1) ? n : ); }
|
#include <bits/stdc++.h> using namespace std; const int N = 4e6 + 7, M = 2e6; const long long mod = 1e9 + 7; inline int read() { int ret = 0; char ch = getchar(); bool f = 1; for (; !isdigit(ch); ch = getchar()) f ^= !(ch ^ - ); for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - 48; return f ? ret : -ret; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long ksm(long long a, long long b, long long mod) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } int head[N], NEXT[M], ver[M], tot; void link(int u, int v) { ver[++tot] = v; NEXT[tot] = head[u]; head[u] = tot; } int indeg[N], in[N], out[N], lazy[N], a[N]; int ant; struct node { int l, r, mid; int val; int maxn; } tree[N]; void dfs(int x) { in[x] = ++ant; for (int i = head[x]; i; i = NEXT[i]) { int y = ver[i]; dfs(y); } out[x] = ant; } void build(int x, int l, int r) { tree[x].l = l; tree[x].r = r; lazy[x] = 0; if (l == r) { tree[x].maxn = 0; return; } int mi = tree[x].mid = l + r >> 1; build(x << 1, l, mi); build(x << 1 | 1, mi + 1, r); tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn); } void down(int x) { if (lazy[x] != 0) { lazy[x << 1] += lazy[x]; lazy[x << 1 | 1] += lazy[x]; tree[x << 1].maxn += lazy[x]; tree[x << 1 | 1].maxn += lazy[x]; lazy[x] = 0; } } void change(int x, int l, int r, int num) { if (l > tree[x].r || r < tree[x].l) return; if (l <= tree[x].l && r >= tree[x].r) { tree[x].maxn += num; lazy[x] += num; return; } else { down(x); if (l <= tree[x].mid) change(x << 1, l, r, num); if (r > tree[x].mid) change(x << 1 | 1, l, r, num); tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn); } } int ask(int x, int l, int r) { return tree[x].maxn; } int main() { int n, k; int ant = 0; scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } stack<int> s; for (int i = 1; i <= n; i++) { if (s.empty()) s.push(i); else { while (!s.empty() && a[s.top()] < a[i]) { link(i, s.top()); indeg[s.top()]++; s.pop(); } s.push(i); } } for (int i = 1; i <= n; i++) { if (indeg[i]) continue; link(n + 1, i); } dfs(n + 1); build(1, in[n + 1], out[n + 1]); for (int i = 1; i <= n; i++) { if (i > k) { change(1, in[i - k], out[i - k], -1); change(1, in[i], out[i], 1); printf( %d , ask(1, in[n + 1], out[n + 1])); } else { change(1, in[i], out[i], 1); if (i == k) printf( %d , ask(1, in[n + 1], out[n + 1])); } } puts( ); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, W, i, j, k; cin >> n >> m >> W; long long w[n], b[n]; for (i = 0; i < n; i++) cin >> w[i]; for (i = 0; i < n; i++) cin >> b[i]; vector<long long> G[n]; for (i = 0; i < m; i++) { cin >> j >> k; j--; k--; G[j].push_back(k); G[k].push_back(j); } bool u[n]; memset(u, 0, sizeof(u)); vector<pair<long long, long long> > v; vector<vector<long long> > vl; for (i = 0; i < n; i++) { if (u[i]) continue; queue<long long> q; vector<long long> l; q.push(i); pair<long long, long long> p = pair<long long, long long>(0, 0); while (!q.empty()) { k = q.front(); q.pop(); if (u[k]) continue; u[k] = 1; l.push_back(k); p.first += b[k]; p.second += w[k]; for (j = 0; j < G[k].size(); j++) q.push(G[k][j]); } v.push_back(p); vl.push_back(l); } long long V = v.size(), ans = 0; long long dp[V + 1][W + 1]; memset(dp, 0, sizeof(dp)); for (i = 0; i < V; i++) { for (j = 0; j < W; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); for (k = 0; k < vl[i].size(); k++) { if (j + w[vl[i][k]] > W) continue; dp[i + 1][j + w[vl[i][k]]] = max(dp[i + 1][j + w[vl[i][k]]], dp[i][j] + b[vl[i][k]]); } if (j + v[i].second > W) continue; dp[i + 1][j + v[i].second] = max(dp[i + 1][j + v[i].second], dp[i][j] + v[i].first); } } for (i = 0; i <= V; i++) for (j = 0; j <= W; j++) ans = max(ans, dp[i][j]); cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); long long power(long long b, long long e, long long m) { if (e == 0) return 1; if (e & 1) return b * power(b * b % m, e / 2, m) % m; return power(b * b % m, e / 2, m); } long long power(long long b, long long e) { if (e == 0) return 1; if (e & 1) return b * power(b * b, e / 2); return power(b * b, e / 2); } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } int n; const int N = 300005; vector<int> v[N]; bool vis[N]; int val[N]; int dp[N][2], ans = 0; void dfs(int s) { vis[s] = true; for (auto j : v[s]) { if (!vis[j]) { dfs(j); dp[s][0] += dp[j][0]; dp[s][1] += dp[j][1]; } } if (val[s]) ++dp[s][val[s] - 1]; } void dfs2(int s = 1) { vis[s] = true; for (auto j : v[s]) { if (!vis[j]) { int k = (dp[j][0] > 0); if ((k > 0 && dp[j][1] == 0) || (k == 0 && dp[j][1] >= 0)) { int z = ((dp[1][0] - dp[j][0]) > 0); if ((z > 0 && dp[1][1] - dp[j][1] == 0) || (z == 0 && dp[1][1] - dp[j][1] >= 0)) ++ans; } dfs2(j); } } } int _runtimeTerror_() { int i; cin >> n; for (i = 1; i <= n; ++i) cin >> val[i]; for (i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1); memset(vis, false, sizeof(vis)); dfs2(); cout << ans << n ; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) _runtimeTerror_(); cerr << n << 1.0 * clock() / CLOCKS_PER_SEC; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; sort(arr.begin(), arr.end()); int i = 0, j = n - 1; vector<int> left; vector<int> right; while (i <= j) { right.push_back(arr[j--]); left.push_back(arr[i++]); } if ((arr.size() & 1) == 1) left.erase(left.begin() + left.size() - 1); int k = right.size() - 1; cout << k << n ; int p = 0; for (int i = 0; i < right.size(); i++) { cout << right[i] << ; if (p < left.size()) cout << left[p++] << ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int p, x, ans = 0; bool b; cin >> p; for (int i = 1; i < p; i++) { b = 1; x = 1; for (int j = 1; j < p - 1; j++) { x = (x * i) % p; if ((x - 1) % p == 0) b = 0; } if (((x * i) - 1) % p != 0) b = 0; if (b) ans++; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, i, j, A[200005], s = 1; cin >> n; for (long long(i) = 0; (i) < (n); (i) = (i + 1)) { cin >> A[i]; if (A[i] == n) { k = i; } } for (j = k; j < n - 1; j++) { if (A[j] < A[j + 1]) { s = 0; break; } } if (s != 0) { for (j = k; j > 0; j--) { if (A[j] < A[j - 1]) { s = 0; break; } } s == 0 ? cout << NO : cout << YES ; } else { cout << NO ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n; const int maxN = 2005; int a[maxN][maxN]; int b[maxN][maxN]; vector<pair<int, int>> by[maxN]; const int dx[3] = {-1, 0, 0}; const int dy[3] = {0, -1, 1}; int his_val[maxN]; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; if (n % 2 == 1) { cout << NONE ; return 0; } for (int j = 0; j < n; j++) { a[0][j] = j / 2; b[0][j] = 0; } for (int i = 0; i + 1 < n; i++) { for (int j = 0; j < n; j++) { int xr = 0; int c = 0; vector<int> vals; vals.emplace_back(a[i][j]); for (int z = 0; z < 3; z++) { int ni = i + dx[z]; int nj = j + dy[z]; if (ni < 0 || ni >= n) continue; if (nj < 0 || nj >= n) continue; vals.emplace_back(a[ni][nj]); } int cnt = 0; int he = -1; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); for (int& t : vals) { int pp = 0; for (int z = 0; z < 3; z++) { int ni = i + dx[z]; int nj = j + dy[z]; if (ni < 0 || ni >= n) continue; if (nj < 0 || nj >= n) continue; if (a[ni][nj] == t) pp ^= 1; if (a[i][j] == t) pp ^= 1; } if (pp && t != a[i][j]) { cnt++; he = t; } } assert(cnt == 0 || cnt == 1); if (cnt == 0) a[i + 1][j] = a[i][j]; else a[i + 1][j] = he; int pp = 0; for (int z = 0; z < 3; z++) { int ni = i + dx[z]; int nj = j + dy[z]; if (ni < 0 || ni >= n) continue; if (nj < 0 || nj >= n) continue; pp ^= b[ni][nj]; pp ^= b[i][j]; pp ^= 1; } b[i + 1][j] = (pp ^ 1) ^ b[i][j]; } } memset(his_val, -1, sizeof his_val); bool ok = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; if (c == . ) continue; int d = 0; if (c == S ) d = 0; else d = 1; d ^= b[i][j]; if (his_val[a[i][j]] == -1) his_val[a[i][j]] = d; if (his_val[a[i][j]] != d) ok = false; } } if (!ok) { cout << NONE n ; return 0; } bool hs = false; for (int z = 0; z < n / 2; z++) { if (his_val[z] == -1) hs = true; } if (hs) { cout << MULTIPLE n ; return 0; } cout << UNIQUE n ; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int d = his_val[a[i][j]] ^ b[i][j]; if (d == 0) cout << S ; else cout << G ; } cout << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; constexpr int N = 1e6 + 10; constexpr int M = 30; long long a1 = 2, a2 = 4, n, ans = 2, t = 4, dis[N], dp[N][M], v, dist1, dist2; int get_lca(int a, int b) { if (dis[a] < dis[b]) swap(a, b); while (dis[a] != dis[b]) a = dp[a][(int)log2(dis[a] - dis[b])]; for (int i = M - 1; i >= 0; i--) { if (a == b) return a; else if (dp[a][i] != dp[b][i]) { a = dp[a][i]; b = dp[b][i]; return get_lca(a, b); } } return dp[a][0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; dp[2][0] = 1; dp[3][0] = 1; dp[4][0] = 1; dis[2] = dis[3] = dis[4] = 1; for (int i = 0; i < n; i++) { cin >> v; dp[++t][0] = v; dp[++t][0] = v; dis[t - 1] = dis[v] + 1; dis[t] = dis[v] + 1; for (int j = 1; j < M; j++) { dp[t - 1][j] = dp[dp[t - 1][j - 1]][j - 1]; dp[t][j] = dp[dp[t][j - 1]][j - 1]; } dist1 = dis[t] + dis[a1] - 2 * dis[get_lca(t, a1)]; dist2 = dis[t] + dis[a2] - 2 * dis[get_lca(t, a2)]; if (dist1 >= dist2 && dist1 > ans) { ans = dist1; a2 = t; } else if (dist2 > dist1 && dist2 > ans) { ans = dist2; a1 = t; } cout << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { vector<long long> v, z; long long n, i, j, k; cin >> n; while (n != 0) { v.push_back(n % 10); n = n / 10; } long long p = 1; for (i = 0; i < v.size(); i++) { p = p * v[i]; } z.push_back(p); for (i = 0; i < v.size() - 1; i++) { if (v[i] != 9) { for (j = i + 1; j < v.size(); j++) { if (v[j] > 0) { v[j]--; break; } } if (j != v.size()) { for (k = i; k < j; k++) { v[k] = 9; } } p = 1; for (k = 0; k < v.size() - 1; k++) { p = p * v[k]; } if (v[v.size() - 1] != 0) p = p * v[v.size() - 1]; z.push_back(p); } } long long max = 0; for (i = 0; i < z.size(); i++) { if (z[i] > max) max = z[i]; } cout << max; }
|
#include <bits/stdc++.h> using namespace std; vector<int> road_wt[2 * 100000 + 5]; vector<int> rail_wt[2 * 100000 + 5]; vector<int> road[2 * 100000 + 5]; vector<int> rail[2 * 100000 + 5]; int visited[2 * 100000 + 5]; struct r { int city; signed long long int cost; int type; r() {} r(int c, signed long long int co, int t) { city = c; cost = co; type = t; } }; signed long long int cost[100000 + 5]; bool way(r a, r b) { return a.cost > b.cost; } struct OrderBy { bool operator()(struct r a, struct r b) { return a.cost > b.cost or a.cost == b.cost and a.type < b.type; } }; priority_queue<struct r, std::vector<struct r>, OrderBy> q; int main() { int n, k, m, a, b, s; signed long long int w, y; cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { cin >> a >> b >> w; road[a].push_back(b); road[b].push_back(a); road_wt[a].push_back(w); road_wt[b].push_back(w); } for (int i = 1; i <= k; ++i) { cin >> s >> y; rail[1].push_back(s); rail_wt[1].push_back(y); } q.push(r(1, 0, 0)); int ans = 0; while (!q.empty()) { r temp = q.top(); int v = temp.city; q.pop(); if (visited[v]) continue; visited[v] = 1; cost[v] = temp.cost; if (temp.type == 1) ++ans; for (int i = 0; i < road[v].size(); ++i) { int u = road[v][i]; if (visited[u]) continue; q.push(r(u, temp.cost + road_wt[v][i], 2)); } for (int i = 0; i < rail[v].size(); ++i) { int u = rail[v][i]; if (visited[u]) continue; q.push(r(u, temp.cost + rail_wt[v][i], 1)); } } cout << k - ans; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int qtn; cin >> qtn; vector<long long int> resp(qtn + 1); resp[qtn] = 1000000000000000010; for (int i = 0; i < qtn >> 1; i++) { long long int n; cin >> n; resp[qtn - i - 1] = min(resp[qtn - i], n); resp[i] = n - resp[qtn - i - 1]; if (i) { if (resp[i] < resp[i - 1]) { resp[i] = resp[i - 1]; resp[qtn - i - 1] = n - resp[i]; } } } for (int i = 0; i < qtn; i++) { cout << resp[i] << ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int a[26], l[26], n, k, x, se, t; char b; string s; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> b >> s; if (x == 1) { if (i == n - 1) break; if (b != . ) se++; continue; } if (b != ? ) for (int j = 0; j < s.size(); j++) l[s[j] - a ]++; if (b == ! ) { for (int j = 0; j < s.size(); j++) { a[s[j] - a ]++; } } if (b == . ) { for (int j = 0; j < s.size(); j++) { a[s[j] - a ]--; } } if (b == ? && i != n - 1) a[s[0] - a ]--; if (b != ? ) for (int j = 0; j < 26; j++) { if (l[j] > 1) a[j] = a[j] - l[j] + 1; l[j] = 0; } k = 0; for (int j = 0; j < 26; j++) k = max(a[j], k); for (int j = 0; j < 26; j++) if (a[j] == k) t++; if (t == 1) x = 1; t = 0; } cout << se; return 0; }
|
#include <bits/stdc++.h> using namespace std; int xs[8] = {1, 1, 1, 0, 0, -1, -1, -1}; int ys[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int x1, y1, x2, y2; cin >> x1 >> y1; cin >> x2 >> y2; int nx, ny; int mV = INT_MAX; for (int i = 0; i < 8; i++) { nx = xs[i] + x2; ny = ys[i] + y2; mV = min(abs(nx - x1) + abs(ny - y1), mV); } cout << 2 * mV + 8 << n ; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long m, h1, a1, x1, y1, h2, a2, x2, y2; long long p1, p2, q1, q2; while (cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2) { p1 = p2 = q1 = q2 = 0; for (long long i = 1; i <= 2 * m; i++) { h1 = (h1 * x1 + y1) % m; if (h1 == a1) { if (p1 == 0) p1 = i; else if (q1 == 0) { q1 = i - p1; break; } } } for (long long i = 1; i <= 2 * m; i++) { h2 = (h2 * x2 + y2) % m; if (h2 == a2) { if (p2 == 0) p2 = i; else if (q2 == 0) { q2 = i - p2; break; } } } if (p1 == 0 || p2 == 0) { puts( -1 ); continue; } long long ans1 = p1, ans2 = p2; long long i; for (i = 1; i <= 2 * m; i++) { if (ans1 == ans2) { printf( %I64d n , ans1); break; } if (ans1 > ans2) { ans2 += q2; } if (ans1 < ans2) { ans1 += q1; } } if (i > 2 * m) { puts( -1 ); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; if (n > 2) cout << n - 2 << endl; else if (n <= 2) cout << n + 2 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t, i, flag; cin >> t; while (t--) { string s1, s2; cin >> s1 >> s2; flag = 0; for (i = 0; i < s1.length(); i++) { if (find(s2.begin(), s2.end(), s1[i]) == s2.end()) flag++; } if (flag == s1.length()) cout << NO << endl; else cout << YES << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; string s; int a[30]; int main() { int n; cin >> n; cin >> s; for (int i = 0; i < n; i++) { a[s[i] - a ]++; } for (int i = 0; i < 26; i++) { int temp = i; for (int j = 0; j < a[temp]; j++) { cout << char( a + temp); } } cout << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e3 + 5; const int INF = 0x3f3f3f3f; const double eps = 1e-9; short int mp[MAXN][MAXN]; int d[MAXN][2]; bool vis[MAXN]; int n, m, s, t, u, v; void dijkstra(int s, int idx) { memset(vis, false, sizeof(vis)); for (int i = 1; i <= n; i++) d[i][idx] = INF; d[s][idx] = 0; for (int i = 0; i < n; i++) { int x = -1, m = INF; for (int j = 1; j <= n; j++) if (!vis[j] && d[j][idx] < m) m = d[x = j][idx]; if (x != -1) { vis[x] = true; for (int j = 1; j <= n; j++) { if (mp[x][j] == 1 && d[j][idx] > d[x][idx] + 1) { d[j][idx] = d[x][idx] + 1; } } } } } int main() { scanf( %d %d %d %d , &n, &m, &s, &t); for (int i = 0; i < m; i++) { scanf( %d %d , &u, &v); mp[u][v] = mp[v][u] = 1; } dijkstra(s, 0); dijkstra(t, 1); int ans = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (mp[i][j] == 0 && d[i][0] + d[j][1] + 1 >= d[t][0] && d[i][1] + d[j][0] + 1 >= d[t][0]) ans++; } } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a[101] = {0}, s = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> m; a[m]++; s = max(a[m], s); } cout << s; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; void solve() { int n, ans = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; ans = max(ans, a[i] - i - 1); } cout << ans << n ; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( -O3 ) struct vertex { bool vis = false; vector<pair<long long int, long long int> > adj; long long int low = 1000000007; long long int dp = 0; long long int parent = -1; }; long long int tim = 0, n, m; vector<pair<long long int, long long int> > edges; bool flag = false; void dfs(vertex x[], long long int sor) { x[sor].vis = true; tim++; x[sor].dp = tim; x[sor].low = tim; for (auto y : x[sor].adj) { if (!x[y.first].vis) { x[y.first].parent = sor; dfs(x, y.first); x[sor].low = min(x[sor].low, x[y.first].low); if (x[y.first].low > x[sor].dp) flag = true; else edges[y.second] = {sor, y.first}; } else if (y.first != x[sor].parent) { x[sor].low = min(x[sor].low, x[y.first].dp); if (x[y.first].dp < x[sor].dp) { edges[y.second] = {sor, y.first}; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; vertex x[n]; for (long long int a = 0; a < m; a++) { long long int u, v; cin >> u >> v; u--, v--; x[u].adj.push_back({v, a}); x[v].adj.push_back({u, a}); edges.push_back({u, v}); } dfs(x, 0); if (flag) cout << 0 << endl; else for (auto y : edges) cout << y.first + 1 << << y.second + 1 << endl; return 0; }
|
#include <bits/stdc++.h> int mod = 1000000000 + 7; long long int inf = 1000000000000000000; using namespace std; mt19937 unlucko(chrono::steady_clock::now().time_since_epoch().count()); vector<int> tree[200000], way(200000), dp(200000), ans(200000); void dfs(int node, int pnode) { dp[node] = way[node]; for (auto it : tree[node]) { if (it == pnode) continue; dfs(it, node); dp[node] += max(dp[it], 0); } } void jfs(int node, int pnode, int curr) { ans[node] = dp[node] + curr; for (auto it : tree[node]) { if (it == pnode) continue; jfs(it, node, max(ans[node] - max(0, dp[it]), 0)); } } int main() { iostream::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> way[i]; if (!way[i]) way[i]--; } for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; --x; y--; tree[x].push_back(y); tree[y].push_back(x); } dfs(0, 0); jfs(0, 0, 0); for (int i = 0; i < n; i++) cout << ans[i] << ; }
|
#include <bits/stdc++.h> constexpr int maxn = 2e5 + 5; constexpr int inf = 1e9; constexpr int mod = 1e9 + 7; using namespace std; using lint = long long; using pii = pair<int, int>; int t, n, a[maxn], b[maxn]; lint ans; map<int, int> ma, mb; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; ma[a[i]]++; mb[b[i]]++; } ans = 1ll * n * (n - 1) * (n - 2) / 6; for (int i = 0; i < n; i++) ans -= 1ll * (ma[a[i]] - 1) * (mb[b[i]] - 1); cout << ans << n ; ma.clear(); mb.clear(); } }
|
#include <bits/stdc++.h> using namespace std; vector<long long> ad[200009]; long long vis[200009]; long long cnt, ans; void dfs(int u) { vis[u] = 1; cnt++; for (auto x : ad[u]) { if (!vis[x]) dfs(x); } } int main() { long long i, j, k, l, m, n, u, v; cin >> n >> k; for (i = 0; i < k; i++) { cin >> u >> v; ad[u].push_back(v); ad[v].push_back(u); } for (i = 1; i <= n; i++) { if (ad[i].size() == 0) continue; if (!vis[i]) { cnt = 0; dfs(i); cnt--; ans += cnt; } } cout << k - ans << n ; }
|
#include <bits/stdc++.h> int query(int aa[][150 + 1], int l0, int l1, int r0, int r1) { return l0 > l1 || r0 > r1 ? 0 : ((long long)aa[l1][r1] - aa[l0 - 1][r1] - aa[l1][r0 - 1] + aa[l0 - 1][r0 - 1]) % 1000000007; } void add(int aa[][150 + 1], int l, int r, int a) { aa[l][r] = (aa[l][r] + a) % 1000000007; } int main() { static int dp00[150 + 1][150 + 1], dp01[150 + 1][150 + 1], dp10[150 + 1][150 + 1], dp11[150 + 1][150 + 1]; static int dq00[150 + 1][150 + 1], dq01[150 + 1][150 + 1], dq10[150 + 1][150 + 1], dq11[150 + 1][150 + 1]; int n, m, l, r, ans; scanf( %d%d , &n, &m); ans = 0; while (n--) { for (l = 1; l <= m; l++) for (r = 1; r <= m; r++) { dp00[l][r] = (dp00[l][r] + dp00[l][r - 1]) % 1000000007; dp01[l][r] = (dp01[l][r] + dp01[l][r - 1]) % 1000000007; dp10[l][r] = (dp10[l][r] + dp10[l][r - 1]) % 1000000007; dp11[l][r] = (dp11[l][r] + dp11[l][r - 1]) % 1000000007; } for (l = 1; l <= m; l++) for (r = 1; r <= m; r++) { dp00[l][r] = (dp00[l][r] + dp00[l - 1][r]) % 1000000007; dp01[l][r] = (dp01[l][r] + dp01[l - 1][r]) % 1000000007; dp10[l][r] = (dp10[l][r] + dp10[l - 1][r]) % 1000000007; dp11[l][r] = (dp11[l][r] + dp11[l - 1][r]) % 1000000007; } for (l = 1; l <= m; l++) for (r = 1; r <= m; r++) dq00[l][r] = dq01[l][r] = dq10[l][r] = dq11[l][r] = 0; for (l = 1; l <= m; l++) for (r = l; r <= m; r++) { dq00[l][r] = 1; add(dq00, l, r, query(dp00, l, m, 1, r)); add(dq01, l, r, query(dp01, l, r, r, m)); add(dq01, l, r, query(dp00, l, r, r + 1, m)); add(dq10, l, r, query(dp10, 1, l, l, r)); add(dq10, l, r, query(dp00, 1, l - 1, l, r)); add(dq11, l, r, query(dp11, 1, l, r, m)); add(dq11, l, r, query(dp01, 1, l - 1, r, m)); add(dq11, l, r, query(dp10, 1, l, r + 1, m)); add(dq11, l, r, query(dp00, 1, l - 1, r + 1, m)); ans = (ans + (long long)dq00[l][r] + dq01[l][r] + dq10[l][r] + dq11[l][r]) % 1000000007; } for (l = 0; l <= m; l++) for (r = 0; r <= m; r++) { dp00[l][r] = dq00[l][r]; dp01[l][r] = dq01[l][r]; dp10[l][r] = dq10[l][r]; dp11[l][r] = dq11[l][r]; } } if (ans < 0) ans += 1000000007; printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> int a, b, i, j, x, y, ch, P[202], Q[202], X[202], Y[202]; bool ran(int A, int B, int C) { if (A <= B && B <= C) return true; else return false; } int main() { for (i = 0; i <= 23; i++) X[i] = 25, Y[i] = i; for (i = 24; i < 200; i++) { X[i] = i + 2; Y[i] = i; } for (i = 0; i <= 13; i++) P[i] = 15, Q[i] = i; for (i = 14; i < 200; i++) P[i] = i + 2, Q[i] = i; int T; scanf( %d , &T); while (T--) { scanf( %d%d , &a, &b); ch = 0; for (i = 0; i < 200; i++) { x = a - X[i]; y = b - Y[i]; if (x == 50 && ran(0, y, 46)) { ch = 1; puts( 3:0 ); printf( %d:%d %d:%d %d:%d n , X[i], Y[i], 25, y / 2, 25, y - y / 2); break; } } for (i = 0; ch == 0 && i < 200; i++) { x = a - X[i]; y = b - Y[i]; if (ran(50, x, 73) && ran(25, y, 71)) { ch = 1; puts( 3:1 ); y -= 25; printf( %d:%d %d:%d %d:%d %d:%d n , x - 50, 25, X[i], Y[i], 25, y / 2, 25, y - y / 2); break; } x = a - Y[i]; y = b - X[i]; if (x == 75 && ran(0, y, 69)) { ch = 1; puts( 3:1 ); printf( %d:%d %d:%d %d:%d %d:%d n , 25, (y + 1) / 3, Y[i], X[i], 25, (y + 1) / 3, 25, y - (y + 1) / 3 * 2); break; } } for (i = 0; ch == 0 && i < 200; i++) { x = a - X[i]; y = b - Y[i]; if (ran(40, x, 86) && ran(50, y, 86)) { ch = 1; puts( 3:2 ); y -= 50; x -= 40; j = 0; while (1) { if (ran(0, y - j, 23) && ran(0, j, 13)) break; j++; } printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , x / 2, 25, x - x / 2, 25, X[i], Y[i], 25, y - j, 15, j); break; } x = a - Y[i]; y = b - X[i]; if (ran(65, x, 88) && ran(25, y, 84)) { ch = 1; puts( 3:2 ); x -= 65; y -= 25; j = 0; while (1) { if (ran(0, y - j, 46) && ran(0, j, 13)) break; j++; } printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , x, 25, Y[i], X[i], 25, (y - j) / 2, 25, (y - j) - (y - j) / 2, 15, j); break; } } for (i = 0; ch == 0 && i < 200; i++) { x = a - P[i]; y = b - Q[i]; if (ran(50, x, 96) && ran(50, y, 96)) { ch = 1; puts( 3:2 ); y -= 50; x -= 50; printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , x / 2, 25, x - x / 2, 25, 25, y / 2, 25, y - y / 2, P[i], Q[i]); break; } } for (i = 0; ch == 0 && i < 200; i++) { x = a - Y[i]; y = b - X[i]; if (ran(50, x, 86) && ran(40, y, 86)) { ch = 1; puts( 2:3 ); y -= 40; x -= 50; j = 0; while (1) { if (ran(0, x - j, 23) && ran(0, j, 13)) break; j++; } printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , 25, y - y / 2, 25, y / 2, Y[i], X[i], x - j, 25, j, 15); break; } x = a - X[i]; y = b - Y[i]; if (ran(25, x, 84) && ran(65, y, 88)) { ch = 1; puts( 2:3 ); y -= 65; x -= 25; j = 0; while (1) { if (ran(0, x - j, 46) && ran(0, j, 13)) break; j++; } printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , 25, y, X[i], Y[i], (x - j) / 2, 25, (x - j) - (x - j) / 2, 25, j, 15); break; } } for (i = 0; ch == 0 && i < 200; i++) { x = a - Q[i]; y = b - P[i]; if (ran(50, x, 96) && ran(50, y, 96)) { ch = 1; puts( 2:3 ); y -= 50; x -= 50; printf( %d:%d %d:%d %d:%d %d:%d %d:%d n , 25, y - y / 2, 25, y / 2, x / 2, 25, x - x / 2, 25, Q[i], P[i]); break; } } for (i = 0; i < 200 && ch == 0; i++) { x = a - Y[i]; y = b - X[i]; if (ran(50, y, 73) && ran(25, x, 71)) { ch = 1; puts( 1:3 ); x -= 25; printf( %d:%d %d:%d %d:%d %d:%d n , 25, y - 50, Y[i], X[i], x / 2, 25, x - x / 2, 25); break; } x = a - X[i]; y = b - Y[i]; if (ran(0, x, 69) && y == 75) { ch = 1; puts( 1:3 ); printf( %d:%d %d:%d %d:%d %d:%d n , X[i], Y[i], (x + 1) / 3, 25, (x + 1) / 3, 25, x - (x + 1) / 3 * 2, 25); break; } } for (i = 0; i < 200 && ch == 0; i++) { x = a - Y[i]; y = b - X[i]; if (y == 50 && ran(0, x, 46)) { ch = 1; puts( 0:3 ); printf( %d:%d %d:%d %d:%d n , Y[i], X[i], x / 2, 25, x - x / 2, 25); break; } } if (ch == 0) { puts( Impossible ); } } }
|
#include <bits/stdc++.h> using namespace std; const long long N = 3e5; const long long P = 1e9 + 7; long long n; long long col[N], f[N], g[N]; vector<long long> a[N]; long long read(void) { long long s = 0, w = 0; char c = getchar(); while (!isdigit(c)) w |= c == - , c = getchar(); while (isdigit(c)) s = s * 10 + c - 48, c = getchar(); return w ? -s : s; } void Dfs(long long x, long long fa) { col[x] ? f[x] = 1 : g[x] = 1; for (long long i = 0; i < a[x].size(); ++i) { long long y = a[x][i]; if (y == fa) continue; Dfs(y, x); f[x] = (f[x] * (f[y] + g[y]) + g[x] * f[y]) % P; g[x] = (g[x] * (f[y] + g[y])) % P; } return; } signed main(void) { n = read(); for (long long i = 1; i < n; ++i) a[read()].push_back(i); for (long long i = 0; i < n; ++i) col[i] = read(); Dfs(0, 0); cout << f[0] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; int a[n], i, j, k, l, m; string s, temp; for (i = 0; i < n; i++) cin >> a[i]; char c = a , d; for (i = 0; i <= 50; i++) { s += a ; } cout << s << endl; for (i = 0; i < n; i++) { k = a[i]; temp = 0 ; for (j = 0; j < k; j++) { temp += s[j]; c = s[j]; } for (j = k; j <= 50; j++) { if (s[j] != z ) { d = s[j] + 1; } else { d = a ; } temp += d; } s = temp; cout << temp << endl; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k1, k2, k3; cin >> k1 >> k2 >> k3; int n = k1 + k2 + k3; int maps[n]; for (int i = 0; i < n; i++) { int inp; cin >> inp; inp--; if (i < k1) maps[inp] = 0; else if (i < k1 + k2) maps[inp] = 1; else maps[inp] = 2; } int costsk1[n]; int costsk2[n]; int costsk3[n]; int profits[n]; int maxprofits[n]; for (int i = 0; i < n; i++) { costsk1[i] = (i == 0 ? 0 : costsk1[i - 1]) + (maps[i] == 0 ? 0 : 1); costsk2[i] = (i == 0 ? 0 : costsk2[i - 1]) + (maps[i] == 1 ? 0 : 1); costsk3[i] = (i == 0 ? 0 : costsk3[i - 1]) + (maps[i] == 2 ? 0 : 1); profits[i] = costsk2[i] - costsk1[i]; maxprofits[i] = max((i == 0 ? 0 : maxprofits[i - 1]), profits[i]); } int minn = costsk3[n - 1]; for (int i = 0; i < n; i++) { minn = min(minn, costsk2[i] - maxprofits[i] + costsk3[n - 1] - costsk3[i]); } cout << minn << endl; }
|
#include <bits/stdc++.h> using namespace std; void yala_bena() { cin.tie(0); std::ios::sync_with_stdio(0); } const int m_r = 999999; int k = -1; int bestsum, limit; vector<long long> ans; int binary(int number) { while (number) { k++; if (number % 2) return k; number /= 2; } } void sum_it_up(int sum, int index, vector<int> p) { if (sum + ans[index] <= bestsum) { sum += ans[index]; p.push_back(index + 1); } if (sum == bestsum) { cout << p.size() << endl; for (int i = 0; i < p.size(); i++) { if (i == p.size() - 1) cout << p[i] << endl; else cout << p[i] << ; } exit(0); } for (int i = index + 1; i < ans.size(); i++) { if (sum + ans[i] <= bestsum) sum_it_up(sum, i, p); } } int main() { yala_bena(); cin >> bestsum >> limit; for (int i = limit; i >= 1; i--) { int a = (int)pow(2, binary(i)); k = -1; if (a <= bestsum) bestsum -= a, ans.push_back(i); } if (bestsum == 0) { cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) i == ans.size() - 1 ? cout << ans[i] << endl : cout << ans[i] << ; return 0; } cout << -1 << endl; }
|
#include <bits/stdc++.h> using namespace std; const int INF = (1u << 31) - 1; const int MAXN = 100086; int mice[MAXN], arr[MAXN], chese[MAXN]; int main() { for (int n, m; ~scanf( %d%d%*d%*d , &n, &m);) { for (int i = 0; i < n; i++) { scanf( %d , &mice[i]); } for (int i = 0; i < m; i++) { scanf( %d , &chese[i]); } fill(arr, arr + m, INF); int ans = 0; for (int i = 0; i < n; i++) { int left, right = lower_bound(chese, chese + m, mice[i]) - chese; if (right == m) { left = --right; } else if (right == 0 || mice[i] == chese[right]) { left = right; } else { left = right - 1; if (abs(mice[i] - chese[left]) < abs(mice[i] - chese[right])) { right = left; } else if (abs(mice[i] - chese[left]) > abs(mice[i] - chese[right])) { left = right; } } int dist = abs(mice[i] - chese[left]); if (arr[left] == INF) { ans++; arr[left] = dist; } else if (arr[left] == dist) { ans++; } else if (arr[left] > dist && left == right) { arr[left] = dist; } else if (left != right) { dist = abs(mice[i] - chese[right]); arr[right] = dist; ans++; } } printf( %d n , n - ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int a[101][101], f[101], ans[101]; int main() { int n, m; cin >> n >> m; memset(a, 0, sizeof(a)); memset(f, 0, sizeof(f)); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } for (int i = 1; i <= m; i++) { int t, t1, mv = -1; for (int j = 1; j <= n; j++) { if (a[i][j] > mv) { mv = a[i][j]; t = i; t1 = j; } } f[t] = t1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (i == f[j]) ans[i]++; } } int max(0), out(1); ; for (int i = 1; i <= n; i++) { if (ans[i] > max) { max = ans[i]; out = i; } } cout << out; }
|
#include <bits/stdc++.h> using namespace std; const int mod = 7 + (int)1e9; long long process(string s) { for (int i = 0; i < s.size(); i++) s[i] = (s[i] - 0 ) % 2 + 0 ; return stoll(s); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; char c; map<long long, int> m; cin >> n; while (n--) { string a; cin >> c >> a; if (c == + ) { m[process(a)]++; } else if (c == - ) { m[process(a)]--; } else if (c == ? ) { cout << m[stoll(a)] << endl; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; int n, a, b; int max(int a, int b) { if (a > b) return a; return b; } bool checkNorm(pair<int, int> f, pair<int, int> s) { int x1, y1; x1 = max(f.first, s.first), y1 = f.second + s.second; if (x1 <= a && y1 <= b) return 1; y1 = max(f.second, s.second), x1 = f.first + s.first; if (x1 <= a && y1 <= b) return 1; return 0; } pair<int, int> rotate(pair<int, int> p) { swap(p.first, p.second); return p; } int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> a >> b; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; v.push_back(make_pair(x, y)); } int mx = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (checkNorm(v[i], v[j])) { mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); } if (checkNorm(v[i], rotate(v[j]))) { mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); } if (checkNorm(rotate(v[i]), (v[j]))) { mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); } if (checkNorm(rotate(v[i]), rotate(v[j]))) { mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); } } } cout << mx; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int cnt[100005]; int cmax[10]; memset(cnt, 0, sizeof(cnt)); memset(cmax, 0, sizeof(cmax)); for (int i = 1; i <= n; i++) { int a; cin >> a; cnt[a]++; if (cnt[a] < 10) cmax[cnt[a]]++; } int t; cin >> t; while (t--) { char ch; int x; cin >> ch >> x; if (ch == + ) { cnt[x]++; if (cnt[x] < 10) cmax[cnt[x]]++; } else { if (cnt[x] < 10) cmax[cnt[x]]--; cnt[x]--; } if (cmax[8]) cout << YES << endl; else if (cmax[4] >= 2) cout << YES << endl; else if (cmax[4]) { if (cmax[2] >= 3) cout << YES << endl; else if (cmax[2] >= 2 && cmax[6]) cout << YES << endl; else cout << NO << endl; } else cout << NO << endl; } }
|
#include <bits/stdc++.h> using namespace std; list<char> lanc; using It = list<char>::iterator; bool torolni_kell(It mit) { if (*mit == - ) return false; char akt = *mit, el = *prev(mit), kov = *next(mit); return (el != - && el != akt) || (kov != - && kov != akt); } int main() { ios_base::sync_with_stdio(false); string be; cin >> be; be = - + be + - ; lanc.insert(begin(lanc), begin(be), end(be)); int valasz = 0; vector<It> to_check; for (It it = next(lanc.begin()); *it != - ; ++it) to_check.push_back(it); while (1) { vector<It> torlendok; for (It akt : to_check) if (torolni_kell(akt)) torlendok.push_back(akt); to_check = {}; for (int i = 0; i < torlendok.size(); i++) { if ((i == 0 || prev(torlendok[i]) != torlendok[i - 1]) && (to_check.empty() || to_check.back() != prev(torlendok[i]))) to_check.push_back(prev(torlendok[i])); if ((i + 1 == torlendok.size() || next(torlendok[i]) != torlendok[i + 1]) && (to_check.empty() || to_check.back() != next(torlendok[i]))) to_check.push_back(next(torlendok[i])); } if (torlendok.empty()) break; for (auto akt : torlendok) lanc.erase(akt); valasz++; } cout << valasz; }
|
#include <bits/stdc++.h> using namespace std; long long ten[20], a, b, l, ans = (long long)1 << 40; long long lp, rp, mp, mt; int ls, rs, ms; int calc(long long x) { int y = 0; for (; x; x /= 10) if (x % 10 == 4 || x % 10 == 7) ++y; return y; } void askl() { lp = a; ls = -1; int j = -1; for (int i = 0; ten[i] <= a; ++i) { int y = a % ten[i + 1]; if ((y / ten[i] == 4 || y / ten[i] == 7) && a + ten[i] - y % ten[i] <= b) j = i, lp = a + ten[i] - y % ten[i], ls = 0; } for (int i = j - 1; i >= 0; --i) if (a % ten[i + 1] / ten[i] < 9) { ls = a % ten[i + 1] / ten[i] < 8 ? i + 1 : i; break; ; } if (ls >= 0) for (; ten[ls] < lp - a; ++ls) ; } void askr() { rp = a + l; rs = -1; int j = -1; for (int i = 0; ten[i] <= b; ++i) { int y = b % ten[i + 1]; if ((y / ten[i] == 4 || y / ten[i] == 7) && b - y % ten[i] > a) j = i, rp = b - y % ten[i], rs = 0; } for (int i = j - 1; i >= 0; --i) if (b % ten[i + 1] / ten[i] > 0) { rs = b % ten[i + 1] / ten[i] > 3 ? i + 1 : i; break; } if (rs >= 0) for (; ten[rs] < b + 1 - rp; ++rs) ; } int checkl(long long x) { if (!x) return 99; int y = 0; for (; x % 10 == 0; x /= 10) ++y; return x % 10 == 4 || x % 10 == 7 ? y : -1; } int checkr(long long x) { if (!x) return 99; int y = 0; for (; x % 10 == 0; x /= 10) ++y; return x % 10 == 5 || x % 10 == 8 ? y : -1; } void askm() { mp = lp + 1; ms = -1; for (int i = lp + 1, j = 0; i < rp; i += ten[j]) { int k = checkl(i); if (k >= j) { if (k > j || ms < 0) { mp = i; ms = k; mt = 0; } else if (!mt) mt = i - mp; j = k; } } } long long next(long long x, int y, int z) { for (int i = 0;; ++i) { int dx = x / ten[i] % 10, w = calc(x); if (w > y) x += ten[i], w = calc(x); if (w + i + 1 == y && dx < 7) { ++w; if (dx < 4) x += (4 - dx) * ten[i]; else x += (7 - dx) * ten[i]; } if (w <= y && w + i >= y) { for (int j = 0; j < y - w; ++j) x += 4 * ten[j]; break; } x = (x / ten[i + 1] + 1) * ten[i + 1]; } if (z) { int w = x; for (; w % 10 == 9; w /= 10) ; if (w % 10 == 3 || w % 10 == 4 || w % 10 == 6 || w % 10 == 7) x = next(x / 10 + 1, y, 0) * 10; } return x; } void init() { scanf( %I64d%I64d , &a, &l); b = a + l - 1; ten[0] = 1; for (int i = 1; i <= 12; ++i) ten[i] = ten[i - 1] * 10; askl(); askr(); askm(); } void make(int x) { for (int i = 0; i <= 9; ++i) { long long y = ms < 0 ? i : (i * 10 + x) * ten[ms]; long long ml = (y - mp + lp + ten[ms + 2]) % ten[ms + 2]; long long mr = (y + rp - mp) % ten[ms + 2]; int z = ml > y || mr < y, w = 0; if (mp < rp) { if (i >= 4 && i <= 7) { if (z) continue; if (i > 4 && i < 7 && (ml < 5 * ten[ms + 1] || mr > 7 * ten[ms + 1])) continue; if (i == 4 && (ml < 4 * ten[ms + 1] || mr > 5 * ten[ms + 1])) continue; if (i == 7 && (ml < 7 * ten[ms + 1] || mr > 8 * ten[ms + 1])) continue; } if (i >= 0 && i <= 3) { if (ml > y && ml < 8 * ten[ms + 1]) continue; if (mr > 4 * ten[ms + 1] || mr < y) continue; } if (i >= 8 && i <= 9) { if (ml > y || ml < 8 * ten[ms + 1]) continue; if (mr > 4 * ten[ms + 1] && mr < y) continue; } } if (~ls) { int tl = checkr(ml); if (tl < ls) continue; if (tl == 99) w = -1; } if (~rs) { int tl = checkl(mr); if (tl < rs) continue; if (tl == 99) w = 1; } if (w) for (int j = ms + 2; ten[j] <= b * 100; ++j) for (int k = 0; k < 2; ++k) { int flag = 0; long long tp; if (w < 0 && j >= ls) { flag = 1; tp = ((k ? 5 : 8) * ten[j] - lp + a + ten[j + 1]) % ten[j + 1]; } if (w > 0 && j >= rs) { flag = 1; tp = ((k ? 4 : 7) * ten[j] - rp + a + ten[j + 1]) % ten[j + 1]; } if (flag) { long long ts = a / ten[j + 1] + (a % ten[j + 1] >= tp); int tn = calc(a) - calc(tp); if (tn >= 0) ans = min(ans, next(ts, tn, 0) * ten[j + 1] + tp); } } else { long long tp = (y - mp + a + ten[ms + 2]) % ten[ms + 2]; long long ts = a / ten[ms + 2] + (a % ten[ms + 2] >= tp); int tn = calc(a) - calc(tp); if (tn >= 0) ans = min(ans, next(ts, tn, z) * ten[ms + 2] + tp); } } } void work() { if (!mt) if (ms < 0) make(0); else { if (mp - lp <= 3 * ten[ms] && rp - mp <= 7 * ten[ms]) make(7); if (rp - mp <= 3 * ten[ms] && mp - lp <= 7 * ten[ms]) make(4); } else make(mt / ten[ms] == 3 ? 4 : 7); printf( %I64d n , ans); } int main() { init(); work(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { bool f = false; set<int> v1; int n, ans = -1; cin >> n; int a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; v1.insert(a[i]); } for (int i = 1; i <= 1024; i++) { long long count = 0; for (int j = 0; j < n; j++) { int m = a[j] ^ i; v1.insert(a[j] ^ i); if (v1.size() > n) { count++; v1.erase(v1.find(m)); break; } } if (count == 0) { ans = i; break; } } cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int cnt[32][N + 10]; int main() { for (int i = 1; i <= N; i++) { int n = i, t = 0; while (n > 0) { cnt[t][i] = cnt[t][i - 1] + n % 2; t++; n /= 2; } } int t; cin >> t; while (t--) { int l, r; cin >> l >> r; int k[32] = {}; for (int i = 0; i < 32; i++) { k[i] = cnt[i][r] - cnt[i][l - 1]; } int res = 0; for (int i = 0; i < 32; i++) { res = max(res, k[i]); } cout << (r - l + 1) - res << n ; } }
|
#include <bits/stdc++.h> using namespace std; int n, q; int sz, nxt[(1 << 20)][2], son[(1 << 20)], val[(1 << 20)]; bool vis[(1 << 20)]; void insert(int v, int add) { int x = 0; for (int i = 18; i >= 0; i--) { int c = (v >> i) & 1; if (!nxt[x][c]) nxt[x][c] = sz++; x = nxt[x][c]; son[x] += add; } val[x] = v; } int query(int v) { int x = 0; for (int i = 18; i >= 0; i--) { int c = (v >> i) & 1; if (son[nxt[x][c]] == (1 << i)) x = nxt[x][c ^ 1]; else x = nxt[x][c]; } return val[x]; } int main() { scanf( %d %d , &n, &q); sz++; for (int i = 0; i < (1 << 19); i++) insert(i, 0); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); if (vis[x]) continue; insert(x, 1); vis[x] = 1; } int cur = 0, x; while (q--) { scanf( %d , &x); cur ^= x; printf( %d n , query(cur) ^ cur); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const int MAX = 100005; bool check(int x, int y) { int cnt1[11] = {0}; int cnt2[11] = {0}; while (x > 0) { cnt1[x % 10]++; x /= 10; } while (y > 0) { cnt2[y % 10]++; y /= 10; } for (int i = 0; i < 10; i++) { if (cnt1[i] > 0 and cnt2[i] > 0) return true; } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int x; cin >> x; vector<int> v; for (int i = 1; i * i <= x; i++) { if (x % i == 0) { if (i * i == x) v.push_back(i); else v.push_back(i), v.push_back(x / i); } } int ans = 0, sz = v.size(); for (int i = 0; i < sz; i++) { if (check(x, v[i])) ans++; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 3e6 + 7; long long int a[N], b[N], c[N], tree[N], lazy[N], cnt, sum, s2, mx; vector<long long int> v, u; vector<pair<long long int, long long int>> vp; map<long long int, long long int> mp; bool vis[111111]; pair<long long int, long long int> p; set<pair<long long int, long long int>> ss; queue<long long int> q; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m = 0, d, i = 0, s1 = 0, s2 = 0, q = 5, x = 0, y, k, j; string s; cin >> n; if (n % 2 == 0) m = n / 2 - 1; else m = n / 2; double mx = 0.0; for (i = m; i > 0; i--) { map<long long int, long long int> mm; cnt = 0; for (j = 2; j <= i; j++) { if (i % j == 0) mm[j]++; } for (j = 2; j <= (n - i); j++) { if ((n - i) % j == 0 && mm[j] != 0) { cnt = 1; break; } } if (cnt == 0) return cout << i << << n - i, 0; } }
|
#include <bits/stdc++.h> using namespace std; int n; int A[1000]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; int G = 0, C = 0; for (int i = 0; i < n; i++) { if (A[i] == 0) { G = min(C, G) + 1; C = G; } else if (A[i] == 1) { int temp = G; G = min(G, C) + 1; C = min(temp, C + 1); } else if (A[i] == 2) { int temp = C; C = min(G, C) + 1; G = min(temp, G + 1); } else { int temp = G; G = min(C, G + 1); C = min(temp, C + 1); } } cout << min(G, C) << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> angles(n); for (int i = 0; i < n; i++) { int angle; cin >> angle; angles[i] = angle; } int difference = 360; for (int i = 0; i < n; i++) { int current_angle = angles[i]; for (int j = 1; j < n; j++) { int curr_diff = max(current_angle, 360 - current_angle) - min(current_angle, 360 - current_angle); difference = min(difference, curr_diff); current_angle += angles[(i + j) % n]; } int curr_diff = max(current_angle, 360 - current_angle) - min(current_angle, 360 - current_angle); difference = min(difference, curr_diff); } cout << difference << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<pair<long long, long long> > v(n); for (long long i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long a, b; a = v[n - 1].first; for (long long i = 0; i < n; i++) swap(v[i].first, v[i].second); sort(v.begin(), v.end()); b = v[0].first; if (a - b <= 0) cout << 0 << endl; else cout << a - b << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; int P, n, m, s[maxn]; long long K; struct que { long long x; int prey, y; bool operator<(const que &a) const { return x < a.x; } } q[maxn]; struct Matrix { int a[2][2]; Matrix() { memset(a, 0, sizeof a); } Matrix(int a_, int b_) { a[0][0] = a_, a[0][1] = 1; a[1][0] = b_, a[1][1] = 0; } void init(int x) { memset(a, 0, sizeof a); a[0][0] = a[1][1] = x; } Matrix operator*(const Matrix &b) { Matrix c; c.init(0); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) c.a[i][j] = (c.a[i][j] + 1ll * a[i][k] * b.a[k][j] % P) % P; return c; } } tr[maxn * 4]; int le, ri; void build(int rt, int l, int r) { if (l == r) { tr[rt] = Matrix(s[l], s[l - 1]); return; } int mid = (l + r) / 2; build(rt * 2, l, mid); build(rt * 2 + 1, mid + 1, r); tr[rt] = tr[rt * 2] * tr[rt * 2 + 1]; } Matrix query(int rt, int l, int r) { if (le <= l && ri >= r) return tr[rt]; int mid = (l + r) / 2; if (ri <= mid) return query(rt * 2, l, mid); if (le > mid) return query(rt * 2 + 1, mid + 1, r); return query(rt * 2, l, mid) * query(rt * 2 + 1, mid + 1, r); } Matrix qk_pow(Matrix a, long long b) { Matrix p; p.init(1); for (; b; b /= 2, a = a * a) if (b & 1) p = p * a; return p; } int get(int x) { return x ? x : n; } Matrix query(long long l, long long r) { long long rr = l + n - get((l - 1) % n); if (rr > r) { le = get((l - 1) % n), ri = get((r - 1) % n); return query(1, 1, n); } else { Matrix c; c.init(0); le = get((l - 1) % n), ri = n; c = query(1, 1, n); c = c * qk_pow(tr[1], (r - rr) / n); if ((r - rr) % n) le = 1, ri = (r - rr) % n, c = c * query(1, 1, n); return c; } } int main() { scanf( %lld%d%d , &K, &P, &n); if (K == 0 || K == 1) { printf( %lld , K % P); return 0; } for (int i = 0; i < n; i++) scanf( %d , &s[i]); s[n] = s[0]; build(1, 1, n); scanf( %d , &m); for (int i = 1; i <= m; i++) scanf( %lld%d , &q[i].x, &q[i].y); sort(q + 1, q + m + 1); while (q[m].x > K) m--; if (q[m].x != K) q[++m].x = K, q[m].y = s[m % n]; Matrix ans; ans.a[0][0] = 1; for (int i = 1; i <= m; i++) q[i].prey = q[i - 1].x + 1 == q[i].x ? q[i - 1].y : s[(q[i].x - 1) % n]; q[0].x = 1, q[0].y = s[1], q[0].prey = s[0]; for (int i = 1; i <= m; i++) { ans = ans * Matrix(q[i - 1].y, q[i - 1].prey); if (q[i].x == q[i - 1].x + 1) continue; ans = ans * Matrix(s[(q[i - 1].x + 1) % n], q[i - 1].y); if (q[i].x == q[i - 1].x + 2) continue; ans = ans * query(q[i - 1].x + 3, q[i].x); } printf( %d n , ans.a[0][0]); fclose(stdin); fclose(stdout); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long n, MOD, f[(long long)4e6 + 5], sum[(long long)4e6 + 5], d[(long long)4e6 + 5]; signed main() { cin >> n >> MOD; long long Sum = 0; for (long long i = 1; i <= n; i++) { if (i == 1) f[1] = 1; else f[i] = (Sum + d[i] + sum[i - 1]) % MOD; sum[i] = (sum[i - 1] + f[i]) % MOD; for (long long j = 1; j * i <= n; j++) { d[j * i] = (d[j * i] + f[i]) % MOD; if (j * i + j <= n) d[j * i + j] = (d[j * i + j] - f[i] + MOD) % MOD; } Sum = (Sum + d[i]) % MOD; } cout << f[n] % MOD; return 0; }
|
#include <bits/stdc++.h> using ll = long long int; using namespace std; int main() { int n; cin >> n; string s; cin >> s; int cnt = 0; int negative = -1; int ans = 0; for (int i = 0; i < n; ++i) { if (s[i] == ) ) --cnt; else if (s[i] == ( ) ++cnt; if (cnt < 0 && negative == -1) negative = i; if (cnt == 0 && negative != -1) { ans += i - negative + 1; negative = -1; } } if (cnt != 0) { cout << -1 << endl; } else { cout << ans << endl; } }
|
#include <bits/stdc++.h> using namespace std; const long long N = 5056, M = 1e9 + 7; long long n, a[N], pr[N], ans[N * 2], h[N * 2]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; pr[0] = 1; for (int i = 1; i <= n - 1; ++i) { cin >> a[i]; pr[i] = a[i] * pr[i - 1] % M; for (int j = 1; j <= i; ++j) ans[j] += pr[i]; } for (int i = n - 1; i >= 1; --i) { for (int j = n + n - 2; j >= 2; --j) h[j] = (a[i + 1] * a[i + 1] % M) * h[j - 2] % M; long long p = 1; for (int j = 2; i + j - 1 <= n; ++j) { (h[j] += p * (j > 2 ? 2 : 1)) %= M; p = (p * a[i + j - 1]) % M; } for (int j = 2; j <= 2 * n - 2; ++j) ans[j] += a[i] * (a[i] - 1) / 2 % M * pr[i - 1] % M * h[j] % M; } for (int k = 1; k <= n + n - 2; ++k) cout << ans[k] % M << n [k == n + n - 2]; }
|
#include <bits/stdc++.h> using namespace std; int transition; int dp[10][2][1 << 9]; int solve(int idx, int tight, int mask, string &s) { if (idx == s.size()) { return __builtin_popcount(mask) == s.size(); } if (dp[idx][tight][mask] != -1) return dp[idx][tight][mask]; int &ans = dp[idx][tight][mask]; ans = 0; if (idx == transition) { for (int i = 0; i <= s[idx] - 0 ; i++) { ans += solve(idx + 1, s[idx] - 0 == i, mask | (1 << i), s); } } else { if (tight) { for (int i = 0; i <= s[idx] - 0 ; i++) { ans += solve(idx + 1, s[idx] - 0 == i, mask | (1 << i), s); } } else { for (int i = 0; i < 7; i++) { ans += solve(idx + 1, 0, mask | (1 << i), s); } } } return ans; } string base7(int n) { string ans; if (n == 0) { ans.push_back( 0 ); return ans; } while (n) { ans.push_back( 0 + n % 7); n /= 7; } reverse(ans.begin(), ans.end()); return ans; } int main() { int n, m; cin >> n >> m; m--; n--; memset(dp, -1, sizeof(dp)); string hours = base7(n); string minutes = base7(m); string s = hours + minutes; transition = hours.size(); if (s.size() > 7) cout << 0 << n ; else { cout << solve(0, 1, 0, s) << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int gcD(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } const int MAXN = 2e5 + 10; int a[MAXN]; int N; int main() { scanf( %d , &N); int g = 0; for (int i = 0; i < N; i++) { scanf( %d , &a[i]); g = gcD(a[i], g); } for (int i = 0; i < N; i++) { a[i] /= g; while ((a[i] & 1) == 0) a[i] = a[i] >> 1; while (a[i] % 3 == 0) a[i] /= 3; if (a[i] != 1) { printf( No n ); return 0; } } printf( Yes n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 310000; int n, m, cs[N], cd[N], nr, rez[N], aint[4 * N], full[4 * N]; vector<int> v[N]; vector<pair<int, int> > x[N]; bool ver[N]; void df(int nod) { ver[nod] = 1; cs[nod] = ++nr; for (vector<int>::iterator it = v[nod].begin(); it != v[nod].end(); ++it) if (!ver[*it]) df(*it); cd[nod] = nr; } void update(int nod, int pozx, int pozy, int poz1, int poz2, int val) { if (pozx >= poz1 && poz2 >= pozy) { full[nod] += val; if (full[nod]) aint[nod] = pozy - pozx + 1; else aint[nod] = aint[2 * nod] + aint[2 * nod + 1]; return; } int mid = (pozx + pozy) / 2; if (mid >= poz1) update(2 * nod, pozx, mid, poz1, poz2, val); if (mid < poz2) update(2 * nod + 1, mid + 1, pozy, poz1, poz2, val); if (full[nod]) aint[nod] = pozy - pozx + 1; else aint[nod] = aint[2 * nod + 1] + aint[2 * nod]; } void df2(int nod) { vector<pair<int, int> >::iterator i; vector<int>::iterator it; ver[nod] = 1; for (i = x[nod].begin(); i != x[nod].end(); ++i) { update(1, 1, n, i->first, i->second, 1); } rez[nod] = max(0, aint[1] - 1); for (it = v[nod].begin(); it != v[nod].end(); ++it) if (!ver[*it]) df2(*it); for (i = x[nod].begin(); i != x[nod].end(); ++i) update(1, 1, n, i->first, i->second, -1); } int main() { int i, a, b; cin >> n >> m; for (i = 1; i != n; ++i) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } df(1); for (i = 1; i <= m; ++i) { cin >> a >> b; x[a].push_back(pair<int, int>(cs[a], cd[a])); x[b].push_back(pair<int, int>(cs[b], cd[b])); x[a].push_back(pair<int, int>(cs[b], cd[b])); x[b].push_back(pair<int, int>(cs[a], cd[a])); } for (i = 1; i <= n; ++i) ver[i] = 0; df2(1); for (i = 1; i <= n; ++i) cout << rez[i] << ; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int func1(long long int i) { return i * (i + 1) * (2 * i + 1) / 6 - (i + i + 2) * i * (i + 1) / 2 + (i + i + 1 + i * i) * i; } long long int func2(long long int i) { return i + i * i - i * (i + 1) / 2; } int main() { long long int inp; cin >> inp; set<pair<long long int, long long int> > s; for (long long int i = 1; func1(i) <= inp; i++) if ((inp - func1(i)) % func2(i) == 0) { s.insert(make_pair(i, i + (inp - func1(i)) / func2(i))); s.insert(make_pair(i + (inp - func1(i)) / func2(i), i)); } cout << s.size() << n ; for (auto i = s.begin(); i != s.end(); i++) cout << (*i).first << << (*i).second << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; double dp[200]; int next1[200]; int cnt(int i, int j) { if ((11 - i) % 2 == 1) { return (10 - i) * 10 + j; } else { return (10 - i) * 10 + (11 - j); } } int main() { for (int i = (1); i < (11); i++) { for (int j = (1); j < (11); j++) { int x; cin >> x; int now = cnt(i, j); int net = cnt(i - x, j); next1[now] = net; } } for (int i = (0); i < (6); i++) dp[99 - i] = 6; for (int i = 93; i >= 1; i--) { dp[i] = 1; for (int j = (1); j < (7); j++) { dp[i] += min(dp[i + j], dp[next1[i + j]]) / 6; } } printf( %.10lf , dp[1]); return 0; }
|
#include <bits/stdc++.h> using namespace std; int f[1005][1005]; int main() { int k; scanf( %d , &k); int w[39], cnt = 0; while (k) { w[cnt++] = k % 2; k /= 2; } f[1][3] = f[3][1] = 1; int now = 3; for (int i = 1; i < cnt; i++) { f[now][now + 1] = f[now + 1][now] = 1; f[now][now + 2] = f[now + 2][now] = 1; f[now + 1][now + 3] = f[now + 3][now + 1] = 1; f[now + 2][now + 3] = f[now + 3][now + 2] = 1; now += 3; } int tmp = now + 1; f[now][2] = f[2][now] = 1; for (int i = 0; i < (cnt - 2) * 2; i++) { f[tmp][tmp + 1] = f[tmp + 1][tmp] = 1; tmp++; } f[tmp][now] = f[now][tmp] = 1; int pos = now + 1; now = tmp; int t = 1; for (int i = 0; i < cnt; i++) { if (w[i] == 1) { f[t * 3][pos] = f[pos][t * 3] = 1; } t++; pos += 2; } printf( %d n , now); for (int i = 1; i <= now; i++) { for (int j = 1; j <= now; j++) if (f[i][j]) printf( Y ); else printf( N ); puts( ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; char string_in_buffer[(int)2e5]; void fast_scan(int &first) { scanf( %d , &first); } void fast_scan(long long &first) { scanf( %lld , &first); } void fast_scan(unsigned long long &first) { scanf( %llu , &first); } void fast_scan(double &first) { scanf( %lf , &first); } void fast_scan(long double &first) { scanf( %Lf , &first); } void fast_scan(char &first) { scanf( %c , &first); if (first == n ) { fast_scan(first); } } void fast_scan(string &first) { scanf( %s , string_in_buffer); first = string(string_in_buffer); } template <class TFirst, class TSecond> void fast_scan(pair<TFirst, TSecond> &p) { fast_scan(p.first); fast_scan(p.second); } template <class T> void fast_scan(vector<T> &v) { for (auto &first : v) fast_scan(first); } void fast_print(const int &first) { printf( %d , first); } void fast_print(const long long &first) { printf( %lld , first); } void fast_print(const unsigned long long &first) { printf( %llu , first); } void fast_print(const double &first) { printf( %.15lf , first); } void fast_print(const long double &first) { printf( %.15Lf , first); } void fast_print(const char &first) { printf( %c , first); }; void fast_print(const string &first) { printf( %s , first.c_str()); } template <class TFirst, class TSecond> void fast_print(const pair<TFirst, TSecond> &p) { fast_print(p.first); fast_print( ); fast_print(p.second); } template <class T> void fast_print(const vector<T> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print( ); fast_print(v[i]); } } template <class T> void fast_print(const vector<vector<T>> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print( n ); fast_print(v[i]); } } using namespace std; namespace smart_io { string print_start = ; string sep = ; bool first_print = false; void precall_print() { fast_print(print_start); print_start = n ; first_print = true; } } // namespace smart_io template <class T> ostream &operator,(ostream &os, const T &object) { if (!smart_io::first_print) { fast_print(smart_io::sep); } else { smart_io::first_print = false; } fast_print(object); return os; } template <class T> istream &operator,(istream &is, T &object) { fast_scan(object); return is; } namespace typedefs {} namespace numbers_operation { template <class T> T floor_mod(T a, T b) { if (a % b == 0) return 0; if (a >= 0 && b >= 0) return a % b; if (a <= 0 && b <= 0) return a % b; return abs(b) - (abs(a) % abs(b)); } } // namespace numbers_operation using namespace numbers_operation; using namespace typedefs; int n; vector<string> v; bool expand(string &base, int i, string t) { for (int j = 0; j < ((int)t.size()); j++) { if (j + i >= ((int)base.size())) { base += t[j]; } else { if (base[j + i] != t[j]) { return false; } } } return true; } signed main(signed argc, char *argv[]) { { cin, n; v.resize(n); cin, v; } { for (string s : v) { if (((int)set<char>(s.begin(), s.end()).size()) != ((int)s.size())) { smart_io::precall_print(); cout, NO ; return 0; } } } { while (true) { vector<int> not_first_vec(26); vector<int> cnt(26); for (int i = 0; i < n; i++) { vector<int> local(26); for (int j = 0; j < ((int)v[i].size()); j++) { local[v[i][j] - a ]++; } for (int j = 0; j < 26; j++) { if (local[j] > 1) { smart_io::precall_print(); cout, NO ; return 0; } } cnt[v[i][0] - a ]++; for (int j = 1; j < ((int)v[i].size()); j++) { not_first_vec[v[i][j] - a ]++; cnt[v[i][j] - a ]++; } } char c = ! ; for (int i = 0; i < 26; i++) { if (not_first_vec[i] <= 1 && cnt[i] > 1) { c = i + a ; } } if (c == ! ) break; vector<pair<int, int>> where; for (int i = 0; i < n; i++) { for (int j = 0; j < ((int)v[i].size()); j++) { if (v[i][j] == c) { where.emplace_back(i, j); } } } if (where.empty()) continue; sort(where.begin(), where.end(), [](pair<int, int> a, pair<int, int> b) { return ((int)v[a.first].size()) < ((int)v[b.first].size()); }); int not_first = 0; pair<int, int> f = where[0]; for (int i = 0; i < ((int)where.size()); i++) { if (where[i].second != 0) { not_first++; f = where[i]; } } if (not_first > 1) { smart_io::precall_print(); cout, NO ; return 0; } string base = v[f.first]; for (int i = 0; i < ((int)where.size()); i++) { if (where[i].first == f.first) continue; if (!expand(base, f.second, v[where[i].first])) { smart_io::precall_print(); cout, NO ; return 0; } } vector<bool> blocked(n, false); for (pair<int, int> p : where) { blocked[p.first] = true; } vector<string> _new; for (int i = 0; i < n; i++) { if (!blocked[i]) { _new.push_back(v[i]); } } _new.push_back(base); n = ((int)_new.size()); v = _new; int tt = 0; } } {} { sort(v.begin(), v.end()); string rez = ; for (string s : v) { rez += s; } if (((int)set<char>(rez.begin(), rez.end()).size()) == ((int)rez.size())) { smart_io::precall_print(); cout, rez; } else { smart_io::precall_print(); cout, NO ; } } }
|
#include <bits/stdc++.h> using namespace std; int heap(int a, int t) { int tmp; if (a > t) swap(a, t); if (t % a == 0) return a; return heap(a, t % a); } int main() { string x, y; cin >> x >> y; bool f = true; for (int i = 0; i < x.size(); i++) { if (x[i] != y[x.size() - 1 - i]) f = false; } if (f) cout << YES << endl; else cout << NO << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, q, l; int A[N], sum[N]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , A + i); sum[i] = sum[i - 1] + A[i]; } scanf( %d , &q); while (q--) { scanf( %d , &l); printf( %d n , lower_bound(sum, sum + n + 1, l) - sum); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long maxn = 3e5 + 5; long long a[maxn], ps[maxn], mark[maxn]; set<pair<long long, long long> > st; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i]; ps[n] = a[n]; st.insert(pair<long long, long long>(ps[n], n)); for (long long i = n - 1; i >= 1; i--) { ps[i] = ps[i + 1] + a[i]; if (i == 1) continue; st.insert(pair<long long, long long>(ps[i], i)); } for (long long i = 1; i < k; i++) { pair<long long, long long> p = *st.rbegin(); mark[p.second]++; st.erase(p); } long long c = 1, ans = 0; for (long long i = 1; i <= n; i++) { if (mark[i]) { c++; } ans += c * a[i]; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 200010; char A[MAXN]; char B[MAXN]; int n; void open() { freopen( B.in , r , stdin); freopen( B.out , w , stdout); } void close() { fclose(stdin); fclose(stdout); } void init() { scanf( %s , A + 1); scanf( %s , B + 1); } bool equal(int l1, int r1, int l2, int r2) { int flag = 1; for (int i = 0, END = r1 - l1; i <= END; i++) if (A[i + l1] != B[i + l2]) { flag = 0; break; } if (flag) return true; else if ((r1 - l1 + 1) & 1) return false; int mid1 = (l1 + r1) / 2, mid2 = (l2 + r2) / 2; if (equal(l1, mid1, l2, mid2)) { if (equal(mid1 + 1, r1, mid2 + 1, r2)) return true; } if (equal(mid1 + 1, r1, l2, mid2)) { if (equal(l1, mid1, mid2 + 1, r2)) return true; } } int main() { init(); int n1 = strlen(A + 1), n2 = strlen(B + 1); if (n1 != n2) { printf( NO n ); } else { if (equal(1, n1, 1, n2)) printf( YES n ); else printf( NO n ); } close(); return 0; }
|
#include <bits/stdc++.h> using namespace std; struct point { int x, y; int num; int mark; }; point p[100010]; int ans[100010]; point pt; int sum1, sum2; bool cmp(point a, point b) { return (a.x - pt.x) * (b.y - pt.y) - (a.y - pt.y) * (b.x - pt.x) < 0; } bool cmp2(point a, point b) { return a.y != b.y ? a.y < b.y : a.x < b.x; } void work(int l, int r) { if (r < l) return; int maxi = 0; int marki = 0; maxi = min_element(p + l, p + r + 1, cmp2) - p; swap(p[maxi], p[l]); pt = p[l]; int k; sort(p + l + 1, p + r + 1, cmp); sum1 = sum2 = 0; for (k = r; !(pt.mark != p[k].mark && sum1 == sum2); k--) if (p[k].mark == pt.mark) sum1++; else sum2++; marki = k; if (pt.mark) ans[p[k].num] = pt.num; else ans[pt.num] = p[k].num; work(l + 1, marki - 1); work(marki + 1, r); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = (1); i <= (2 * n); ++i) { cin >> p[i].x >> p[i].y; p[i].mark = (i > n); p[i].num = i > n ? i - n : i; } work(1, 2 * n); for (int i = (1); i <= (n); ++i) { cout << ans[i] << n ; } }
|
#include <bits/stdc++.h> using namespace std; int n; vector<vector<pair<int, int>>> g; vector<int> p; vector<bool> was; vector<int> sz; long long res; void calcSz(int v, int par = -1) { sz[v] = 1; for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (to != par && !was[to]) { calcSz(to, v); sz[v] += sz[to]; } } } int findCentroid(int v, int n, int par = -1) { for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (to != par && !was[to] && 2 * sz[to] > n) { return findCentroid(to, n, v); } } return v; } int getInc(int v, int edge, int par = -1) { int res = 1; for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (to != par && !was[to] && g[v][i].second >= edge) { res += getInc(to, g[v][i].second, v); } } return res; } int getDec(int v, int edge, int par = -1) { int res = 1; for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (to != par && !was[to] && g[v][i].second <= edge) { res += getDec(to, g[v][i].second, v); } } return res; } int build(int v) { calcSz(v); v = findCentroid(v, sz[v]); was[v] = true; int inc0 = 0; int inc1 = 0; int dec0 = 0; int dec1 = 0; for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (!was[to]) { int inc = getInc(to, g[v][i].second, v); int dec = getDec(to, g[v][i].second, v); if (g[v][i].second == 0) { res += inc * 1ll * (dec0 + 1); res += dec * 1ll * (inc0 + inc1 + 1); inc0 += inc; dec0 += dec; } else { res += inc * 1ll * (dec0 + dec1 + 1); res += dec * 1ll * (inc1 + 1); inc1 += inc; dec1 += dec; } } } for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].first; if (!was[to]) { p[build(to)] = v; } } return v; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; g.resize(n); p.resize(n); was.resize(n); sz.resize(n); for (int i = 0; i < n - 1; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; g[a].push_back({b, c}); g[b].push_back({a, c}); } build(0); cout << res << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; pair<int, int> arr[100]; int res[100]; int main() { int N, W; scanf( %d%d , &N, &W); for (int i = 0; i < N; i++) { scanf( %d , &arr[i].first); arr[i].second = i; } sort(arr, arr + N); for (int i = 0; i < N; i++) { if (W < (arr[i].first + 1) / 2) { printf( -1 n ); return 0; } W -= (arr[i].first + 1) / 2; res[arr[i].second] = (arr[i].first + 1) / 2; } int p = N - 1; while (W > 0) { int delta = min(W, arr[p].first - res[arr[p].second]); W -= delta; res[arr[p].second] += delta; p--; } for (int i = 0; i < N; i++) printf( %d , res[i]); putchar( n ); }
|
#include <bits/stdc++.h> using namespace std; const int INF = 10000000; const int MAX = 150; string s; int found(string aux) { int ans = 0; int cnt = 0; for (int i = 0; i < s.size(); i++) { for (int j = 0, k = i; j < aux.size(); j++, k++) { if (k == s.size()) break; if (aux[j] == s[k]) cnt++; else { cnt = 0; break; } } if (cnt == aux.size()) ans++; } return ans; } int main() { cin >> s; int ans = 0; ans += found( Danil ); ans += found( Olya ); ans += found( Slava ); ans += found( Ann ); ans += found( Nikita ); if (ans == 1) puts( YES ); else puts( NO ); return 0; }
|
#include <bits/stdc++.h> using namespace std; pair<long long, long long> a[1000005]; bool fl1, fl2, fl3, fl4; long long dist(pair<long long, long long> a, pair<long long, long long> b) { return abs(a.first - b.first) + abs(a.second - b.second); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i, j, k, l, m, n, t, r, cnt; long long flag = 0; long long an = 0, ans = 0; t = 1; while (t--) { cin >> n; long long x, y; for (i = 0; i < n; i++) { cin >> x >> y; a[i] = make_pair(x, y); } long long xmi, xma, yma, ymi; xmi = 1LL * 1e18; ymi = 1LL * 1e18; yma = -1LL * 1e18; xma = -1LL * 1e18; for (i = 0; i < n; i++) { xmi = min(xmi, a[i].first); xma = max(xma, a[i].first); ymi = min(ymi, a[i].second); yma = max(yma, a[i].second); } ans = 2LL * (xma - xmi + yma - ymi); vector<pair<long long, long long> > b; for (i = 0; i < n; i++) { if (a[i].first == xmi && !fl1) { fl1 = true; b.push_back(a[i]); } if (a[i].first == xma && !fl2) { fl2 = true; b.push_back(a[i]); } if (a[i].second == ymi && !fl3) { fl3 = true; b.push_back(a[i]); } if (a[i].second == yma && !fl4) { fl4 = true; b.push_back(a[i]); } } b.push_back(make_pair(xmi, ymi)); b.push_back(make_pair(xmi, yma)); b.push_back(make_pair(xma, yma)); b.push_back(make_pair(xma, ymi)); an = -1LL * 1e18; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { for (k = 0; k < 4; k++) { if (b[j] == a[i] || b[k] == a[i]) continue; an = max(an, dist(a[i], b[j]) + dist(b[k], a[i]) + dist(b[j], b[k])); } } } cout << an << ; for (i = 4; i <= n; i++) { cout << ans << ; } cout << endl; } }
|
#include <bits/stdc++.h> using namespace std; int n, m; char arr[1503][1503]; int inix, iniy; bool visit[4503][4504]; int X[] = {0, 0, 1, -1}; int Y[] = {1, -1, 0, 0}; int nn, mm; int colax[4503 * 4503]; int colay[4503 * 4503]; bool dentro(int a, int b) { if (a < 0 || b < 0 || a >= nn || b >= mm) return false; return true; } inline void bfs(int x, int y) { visit[x][y] = true; int xx, yy; int q = 0; colax[q] = x; colay[q++] = y; for (int j = 0; j < q; j++) { x = colax[j]; y = colay[j]; for (int i = 0; i < 4; i++) { xx = x + X[i]; yy = y + Y[i]; if (!visit[(xx + nn) % nn][(yy + mm) % mm] && arr[(xx + n) % n][(yy + m) % m] != # ) { visit[(xx + nn) % nn][(yy + mm) % mm] = true; colax[q] = (xx + nn) % nn; colay[q++] = (yy + mm) % mm; } } } } int main() { scanf( %d %d , &n, &m); nn = n * 3; mm = m * 3; for (int i = 0; i < n; i++) scanf( %s , arr[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (arr[i][j] == S ) { inix = i, iniy = j; }; iniy += m; inix += n; bfs(inix, iniy); for (int i = -1; i < 2; i++) for (int j = -1; j < 2; j++) { if (i == j && i == 0) continue; if (visit[inix + i * n][iniy + j * m]) { cout << Yes << endl; return 0; } } cout << No << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, w, a[105], cnt1 = 0, cnt = 0, b[105], c[105], e[105], colour[105]; memset(colour, 0, sizeof colour); scanf( %d%d , &n, &w); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); c[i] = a[i]; cnt1 = a[i] / 2; if (a[i] % 2 == 1) cnt1++; b[i] = cnt1; cnt += cnt1; } if (cnt > w) { printf( -1 n ); return 0; } if (w > cnt) { int dif = w - cnt; sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (c[i] == a[j] && colour[j] == 0) { e[i] = j; colour[j] = 1; break; } } } int d = n; while (d > 0) { if (c[d] % 2 == 1) { int k = c[d] / 2 + 1; int diff = c[d] - k; if (dif > diff) { dif -= diff; b[e[d]] += diff; } else b[e[d]] += dif, dif = 0; } else { int k = c[d] / 2; int diff = c[d] - k; if (dif > diff) { dif -= diff; b[e[d]] += diff; } else b[e[d]] += dif, dif = 0; } if (dif == 0) break; d--; } if (dif > 0) { printf( -1 n ); return 0; } for (int i = 1; i <= n; i++) printf( %d , b[i]); } else if (cnt == w) { for (int i = 1; i <= n; i++) printf( %d , b[i]); } }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; template <typename T> inline void read(T &AKNOI) { T x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) flag = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 0 ; ch = getchar(); } AKNOI = flag * x; } template <typename T> inline void cmin(T &x, T y) { if (y < x) x = y; } template <typename T> inline void cmax(T &x, T y) { if (x < y) x = y; } char s[MAXN], t[MAXN]; int n, m, k[MAXN], len[MAXN]; int tot, ch[MAXN][26], fail[MAXN], ed[MAXN], ff[MAXN]; int q[MAXN], head, tail; vector<int> mtch[MAXN]; void Insert(int id) { int p = 0, l = len[id] = strlen(t + 1); for (int i = 1; i <= l; ++i) { int x = t[i] - a ; if (!ch[p][x]) { ch[p][x] = ++tot; } p = ch[p][x]; } ed[p] = id; } void GetFail() { head = 1, tail = 0; for (int i = 0; i < 26; ++i) { if (ch[0][i]) { q[++tail] = ch[0][i]; } } while (head <= tail) { int u = q[head++]; for (int i = 0; i < 26; ++i) { if (ch[u][i]) { int v = ch[u][i]; fail[v] = ch[fail[u]][i]; ff[v] = (ed[fail[v]] ? fail[v] : ff[fail[v]]); q[++tail] = v; } else { ch[u][i] = ch[fail[u]][i]; } } } } void Run() { int p = 0; for (int i = 1; i <= n; ++i) { p = ch[p][s[i] - a ]; int u = (ed[p] ? p : ff[p]); for (; u; u = ff[u]) { mtch[ed[u]].push_back(i); } } } void init() { scanf( %s , s + 1); n = strlen(s + 1); read(m); for (int i = 1; i <= m; ++i) { read(k[i]); scanf( %s , t + 1); Insert(i); } GetFail(); } void solve() { Run(); for (int i = 1; i <= m; ++i) { if (mtch[i].size() < k[i]) { printf( -1 n ); } else { int ans = n + 1; for (int j = 0, sz = mtch[i].size(); j + k[i] - 1 < sz; ++j) { cmin(ans, mtch[i][j + k[i] - 1] - mtch[i][j]); } printf( %d n , ans + len[i]); } } } int main() { init(); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXINT = 1073741823; int main() { ios::sync_with_stdio(false); cin.tie(0); int t, n, a, b, x; bool win; cin >> t; for (int q = 0; q < t; q++) { cin >> n >> a >> b; win = false; for (int i = 0; i < a; i++) { cin >> x; if (x == n) win = true; } for (int i = 0; i < b; i++) cin >> x; cout << (win ? YES : NO ) << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const char elem[][4] = { H , He , Li , Be , B , C , N , O , F , Ne , Na , Mg , Al , Si , P , S , Cl , Ar , K , Ca , Sc , Ti , V , Cr , Mn , Fe , Co , Ni , Cu , Zn , Ga , Ge , As , Se , Br , Kr , Rb , Sr , Y , Zr , Nb , Mo , Tc , Ru , Rh , Pd , Ag , Cd , In , Sn , Sb , Te , I , Xe , Cs , Ba , La , Ce , Pr , Nd , Pm , Sm , Eu , Gd , Tb , Dy , Ho , Er , Tm , Yb , Lu , Hf , Ta , W , Re , Os , Ir , Pt , Au , Hg , Tl , Pb , Bi , Po , At , Rn , Fr , Ra , Ac , Th , Pa , U , Np , Pu , Am , Cm , Bk , Cf , Es , Fm , Md , No , Lr , Rf , Db , Sg , Bh , Hs , Mt , Ds , Rg , Cn , Uut , Fl , Uup , Lv , Uus , Uuo , }; const int MAXN = 18; string ele; vector<int> can[110]; map<string, int> ele_map; int n, m, max_v, vec[MAXN], tar[MAXN], dp[1 << MAXN][MAXN], pre[1 << MAXN][MAXN]; int main() { ele_map.clear(); for (int i = 1; i <= 100; ++i) ele_map[elem[i - 1]] = i; while (cin >> n >> m) { for (int i = 0; i < n; ++i) { cin >> ele; vec[i] = ele_map[ele]; } max_v = 0; for (int i = 0; i < m; ++i) { cin >> ele; tar[i] = ele_map[ele]; max_v = max(max_v, tar[i]); } for (int i = 0; i <= max_v; ++i) can[i].clear(); for (int mask = 0; mask < (1 << n); ++mask) { int sum = 0; for (int i = 0; i < n; ++i) { if (mask & (1 << i)) sum += vec[i]; } if (sum <= max_v) can[sum].push_back(mask); } memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int mask = 0; mask < (1 << n); ++mask) { for (int i = 0; i < m; ++i) { if (dp[mask][i]) { int v = tar[i]; for (int j = 0; j < can[v].size(); ++j) { int nmask = can[v][j]; if ((mask & nmask) == 0) { dp[mask | nmask][i + 1] = 1; pre[mask | nmask][i + 1] = mask; } } } } } int mask = (1 << n) - 1; if (dp[mask][m] == 0) { puts( NO ); } else { puts( YES ); for (int i = m; i > 0; --i) { vector<int> add; int nmask = mask ^ pre[mask][i]; for (int j = 0; j < n; ++j) { if (nmask & (1 << j)) add.push_back(j); } for (int j = 0; j < add.size(); ++j) { if (j) putchar( + ); printf( %s , elem[vec[add[j]] - 1]); } printf( ->%s n , elem[tar[i - 1] - 1]); mask = pre[mask][i]; } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; int N; bool vis[MAX]; vector<pair<int, bool> > neigh[MAX]; bool islucky(int a) { while (a > 0) { if (a % 10 != 4 and a % 10 != 7) return false; a /= 10; } return true; } long long p3(long long N) { return (N) * (N - 1) * (N - 2); } long long solve(int a) { vector<int> q; int qnum = 0; q.push_back(a); while (qnum < q.size()) { int cur = q[qnum]; vis[cur] = true; for (int i = 0; i < neigh[cur].size(); i++) { if (neigh[cur][i].second) continue; int cneigh = neigh[cur][i].first; if (vis[cneigh]) continue; q.push_back(cneigh); } qnum++; } long long sz = q.size(); return (2 * (sz) * (sz - 1) * ((long long)N - sz) + p3(sz)); } int main() { cin >> N; for (int i = 0; i < N - 1; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; bool il = islucky(c); neigh[a].push_back(pair<int, bool>(b, il)); neigh[b].push_back(pair<int, bool>(a, il)); } long long ans = p3(N); for (int i = 0; i < MAX; i++) vis[i] = false; for (int i = 0; i < N; i++) { if (vis[i]) continue; ans -= solve(i); } cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; int n, l, r, ql, qr; int w[100010]; int main() { cin >> n >> l >> r >> ql >> qr; for (int i = 1; i <= n; ++i) cin >> w[i]; int f[100010]; f[0] = 0; for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + w[i]; int ans = INT_MAX; for (int i = 0; i <= n; ++i) { int v = f[i] * l + (f[n] - f[i]) * r; if (i < n / 2) v += qr * max(0, (n - 2 * i - 1)); else v += ql * max(0, (2 * i - n - 1)); ans = min(ans, v); } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int m, k = 1, i; for (i = 0; i < s.size(); i++) { m = int(s[i]) - 96; if (m == k) k++; else if (m > k) { cout << NO ; return 0; } } cout << YES ; }
|
#include <bits/stdc++.h> using namespace std; int n, i, j, x, y, a[179000]; int main() { for (cin >> n >> x >> y, i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) { for (j = max(1, i - x); j <= min(n, i + y); j++) if (i != j && a[j] <= a[i]) break; if (j > min(n, i + y)) break; } cout << i; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; double arr[n]; double temp = 0; for (int i = 0; i < n; i++) { cin >> temp; arr[i] = temp; } long long sum = 0; double fractpart, intpart; int arr2[n]; for (int i = 0; i < n; i++) { arr2[i] = ((int)floor(arr[i])); sum += arr2[i]; } int count = -sum; int total = 0, index = 0; while (count != 0) { if (arr[index] - floor(arr[index]) < 0.000001) { index++; continue; } count--; arr2[index]++; index++; } for (int i = 0; i < n; i++) { cout << arr2[i] << n ; } }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; string A, B; int a[N], b[N]; int main() { while (cin >> A >> B) { int len1 = A.size(), len2 = B.size(); for (int i = 0; i <= len1 - 1; i++) a[i] = A[i] - 0 ; for (int i = 0; i <= len2 - 1; i++) b[i] = B[i] - 0 ; int sum = 0; for (int i = 0; i <= len2 - 1; i++) sum ^= (a[i] ^ b[i]); int ans = sum == 0; for (int i = 1; i <= len1 - len2; i++) { sum ^= a[i - 1]; sum ^= a[i - 1 + len2]; ans += sum == 0; } printf( %d n , ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; ifstream fin( test.in ); struct intr { int l, r, id; }; intr q[200005]; int p, n, qq; long long sol; int a[200005]; long long rez[200005]; int mp[5 * 200005]; bool cmp(intr &q1, intr &q2) { return (q1.r / p < q2.r / p || (q1.r / p == q2.r / p && q1.l < q2.l)); } void add(int x) { int v = mp[x]; sol = sol - 1LL * v * v * x; sol = sol + 1LL * (v + 1) * (v + 1) * x; mp[x]++; } void del(int x) { int v = mp[x]; sol = sol - 1LL * v * v * x; if (v >= 1) { sol = sol + 1LL * (v - 1) * (v - 1) * x; mp[x]--; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int i, lx, rx; cin >> n >> qq; for (i = 1; i <= n; i++) cin >> a[i]; p = sqrt(n); for (i = 1; i <= qq; i++) { cin >> q[i].l >> q[i].r; q[i].id = i; } sort(q + 1, q + qq + 1, cmp); lx = 1; rx = 0; sol = 0; for (i = 1; i <= qq; i++) { while (q[i].r > rx) add(a[++rx]); while (q[i].r < rx) del(a[rx--]); while (q[i].l > lx) del(a[lx++]); while (q[i].l < lx) add(a[--lx]); rez[q[i].id] = sol; } for (i = 1; i <= qq; i++) cout << rez[i] << n ; }
|
#include <bits/stdc++.h> using namespace std; long long int vis1[1000000]; long long int ara1[100005], ara2[100005]; map<long long int, long long int> vis; map<long long int, long long int> str2; priority_queue<long long int> pq; vector<long long int> vec; int main() { long long int a, b, c = 0, sum = 0, n; scanf( %lld , &n); for (long long int i = 1; i <= n; i++) { scanf( %lld , &a); long long int mx = 0; string str; cin >> str; c = -1; for (long long int i = 0; i < a; i++) { if (str[i] != A && c != -1) { c++; if (c > mx) mx = c; } else if (str[i] == A ) c = 0; } cout << mx << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int const inf = 1e18; long long int const maxn = 2e5 + 5; long long int const mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<long long int> arr(26); for (auto &u : arr) cin >> u; string s; cin >> s; long long int n = s.length(); map<long long int, long long int> mp[26]; vector<long long int> brr(n); for (long long int i = 0; i < n; i++) { brr[i] = arr[s[i] - a ]; if (i != 0) brr[i] += brr[i - 1]; } long long int ans = 0; mp[s[0] - a ][brr[0]]++; for (long long int i = 1; i < n; i++) { ans += mp[s[i] - a ][brr[i - 1]]; mp[s[i] - a ][brr[i]]++; } cout << ans << n ; ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long N = 20; long long n, m, dp[N][1 << N], B, res, cb[1 << N]; bool used[N][1 << N], w[N][N]; long long cbit(long long x) { long long c = 0; while (x) { c += x & 1; x /= 2; } return c; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (long long i = 0; i < m; ++i) { long long a, b; cin >> a >> b; a--; b--; w[a][b] = 1; w[b][a] = 1; } B = 1 << n; for (long long i = 0; i < B; ++i) cb[i] = cbit(i); queue<pair<long long, long long>> q; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < n; ++j) { for (long long ma = 0; ma < B; ++ma) { dp[j][ma] = 0; used[j][ma] = 0; } } q.push({i, 1 << i}); dp[i][1 << i] = 1; while (!q.empty()) { long long v = q.front().first, mask = q.front().second, nmask; q.pop(); for (long long u = i; u < n; ++u) { nmask = mask | (1 << u); if (nmask == mask || !w[u][v]) continue; dp[u][nmask] += dp[v][mask]; if (!used[u][nmask]) { used[u][nmask] = 1; q.push({u, nmask}); } } } long long av = 0; for (long long j = 0; j < n; ++j) { for (long long mask = 0; mask < B; ++mask) { if (cb[mask] >= 3 && w[i][j] && dp[j][mask]) { av += dp[j][mask]; } } } res += av / 2; } cout << res << n ; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 100; vector<int> vec[maxn]; int n, m, k; int d[maxn], cnt; bool vis[maxn]; void dfs(int x) { vis[x] = 1; d[++cnt] = x; for (int i = 0; i < vec[x].size(); i++) { if (vis[vec[x][i]]) continue; dfs(vec[x][i]); d[++cnt] = x; } } int main() { scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= m; i++) { int u, v; scanf( %d%d , &u, &v); vec[u].push_back(v); vec[v].push_back(u); } dfs(1); int x = (2 * n + k - 1) / k; for (int i = 1; i <= k; i++) { int c = min(x, cnt); if (c == 0) { cout << 1 1 << endl; continue; } cout << c << ; for (int i = 1; i <= c && cnt != 0; i++, cnt--) { cout << d[cnt] << ; } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1) { cout << -1 ; return 0; } if (max(c, d) > min(a, b)) { cout << -1 ; return 0; } if (a + b <= c + d) { cout << -1 ; return 0; } if (c == d) { a--; for (int i = 1; i <= a - c; i++) cout << 4 ; if (a == c - 1 && b > 1) { cout << 7 ; b--; } for (int i = 1; i <= c; i++) cout << 47 ; for (int i = 1; i <= b - c; i++) cout << 7 ; if (a - c >= 0) cout << 4 ; } if (c > d) { for (int i = 1; i <= a - c; i++) cout << 4 ; for (int i = 1; i <= c; i++) cout << 47 ; for (int i = 1; i <= b - c; i++) cout << 7 ; } if (c < d) { if (b > 1) { cout << 7 ; b--; } for (int i = 1; i < a - c; i++) cout << 4 ; for (int i = 1; i <= c; i++) cout << 47 ; for (int i = 1; i <= b - c; i++) cout << 7 ; cout << 4 ; } }
|
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define pre(i,a,b) for(int i=a;i>=b;i--) #define N 505 using namespace std; int f[52][95][905][2],n,mx=450,cc=45; char s[N]; int calc(int x,int cur,int res,int op){ if(x==n+1&&!cur)return 0; if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f; if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1]; f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1))); return f[x][cur+cc][res+mx][op==1]; } int main(){ scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ; memset(f,~0,sizeof(f)); printf( %d n ,calc(0,0,0,1)); return 0; } /* #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define pre(i,a,b) for(int i=a;i>=b;i--) #define N 505 using namespace std; int f[52][95][905][2],n,mx=450,cc=45; char s[N]; int calc(int x,int cur,int res,int op){ if(x==n+1&&!cur)return 0; if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f; if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1]; f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1))); return f[x][cur+cc][res+mx][op==1]; } int main(){ scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ; memset(f,~0,sizeof(f)); printf( %d n ,calc(0,0,0,1)); return 0; } */
|
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; long long int dp1[N], dp2[N], Sa[N], Sw[N], dp[20][N], A[N]; int P[20][N], H[N], n, q; vector<pair<int, int> > adj[N]; void preDFS(int v, int p = -1) { if (!~p) P[0][v] = v; for (int i = 1; i < 20; i++) P[i][v] = P[i - 1][P[i - 1][v]]; for (pair<int, int> u : adj[v]) { if (u.first != p) { P[0][u.first] = v; H[u.first] = H[v] + 1; Sw[u.first] = Sw[v] + u.second, Sa[u.first] = Sa[v] + A[u.first]; preDFS(u.first, v); } } } int getpar(int v, int h) { for (int i = h; i; i -= i & -i) v = P[__builtin_ctz(i)][v]; return v; } int LCA(int u, int v) { if (H[u] < H[v]) swap(u, v); u = getpar(u, H[u] - H[v]); if (u == v) return v; for (int i = 19; ~i; i--) if (P[i][u] != P[i][v]) u = P[i][u], v = P[i][v]; return P[0][v]; } void DFSdn(int v, int p = -1) { for (pair<int, int> u : adj[v]) { if (u.first != p) DFSdn(u.first, v), dp1[v] += max(0ll, dp1[u.first] - 2 * u.second + A[u.first]); } } void DFSup(int v, int p = -1) { for (pair<int, int> u : adj[v]) { if (u.first != p) { dp2[u.first] = max(0ll, A[v] + dp2[v] + dp1[v] - max(0ll, dp1[u.first] - 2 * u.second + A[u.first])); dp2[u.first] = max(0ll, dp2[u.first] - 2 * u.second); DFSup(u.first, v); } } } void NIMA(int v, int p = -1) { for (pair<int, int> u : adj[v]) { if (u.first != p) { dp[0][u.first] = dp1[v] - max(0ll, dp1[u.first] - 2 * u.second + A[u.first]); NIMA(u.first, v); } } } long long int get(int v, int h) { long long int ret = 0; for (int i = h; i; i -= i & -i) ret += dp[__builtin_ctz(i)][v], v = P[__builtin_ctz(i)][v]; return ret; } int main() { scanf( %d%d , &n, &q); for (int i = 1; i <= n; i++) scanf( %lld , &A[i]); for (int i = 1; i < n; i++) { int u, v, w; scanf( %d%d%d , &u, &v, &w); adj[u].push_back({v, w}); adj[v].push_back({u, w}); } preDFS(1); DFSdn(1); DFSup(1); NIMA(1); for (int i = 1; i < 20; i++) for (int j = 1; j <= n; j++) dp[i][j] = dp[i - 1][j] + dp[i - 1][P[i - 1][j]]; for (; q; q--) { int u, v; scanf( %d%d , &u, &v); if (H[u] < H[v]) swap(u, v); if (u == v) { printf( %lld n , dp1[u] + dp2[u] + A[u]); continue; } int p = LCA(u, v); long long int a = get(u, H[u] - H[p]), b = get(v, H[v] - H[p]); long long int ret = Sa[u] - Sa[p] + A[p] + Sa[v] - Sa[p] - (Sw[u] - Sw[p] + Sw[v] - Sw[p]); printf( %lld n , a + b + dp2[p] - dp1[p] + ret + dp1[u] + dp1[v]); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 10, mod = 1e9 + 7, inf = 2e9, lg = 22, maskm = (1 << lg) - 1, maxm = (1 << lg) + 7; long long a[maxn], dp[maxm]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; dp[a[i]] = a[i]; } for (int i = 1; i < maxm; i++) { for (int j = i; j && dp[i] == 0; j -= (j & -j)) { dp[i] = dp[i ^ (j & -j)]; } } for (int i = 0; i < n; i++) { long long tmp = maskm ^ a[i]; if (dp[tmp] == 0) cout << -1 << ; else cout << dp[tmp] << ; } }
|
#include <bits/stdc++.h> using namespace std; bool vis[100010]; vector<int> dp[5][100010]; int k, l; int a[6][6]; int dfs(int now) { int i, j, x = 0, sum = 0; for (i = 0; i < now; i++) x = x * 10 + a[now][i]; if (i == l - 1) return dp[1][x].size(); for (i = 0; i < dp[l - now][x].size(); i++) { int tem = dp[l - now][x][i]; for (j = l - 1; j >= now; j--) { a[j][now] = a[now][j] = tem % 10; tem /= 10; } sum += dfs(now + 1); } return sum; } int main() { int i, j, n; int m = sqrt(100000.5); for (i = 2; i <= m; i++) if (!vis[i]) { for (j = i * i; j <= 100000; j += i) vis[j] = 1; } for (i = 2; i < 100000; i++) if (!vis[i]) { int tem = i; tem /= 10; for (j = 1; j <= 4; j++) { dp[j][tem].push_back(i); tem /= 10; } } scanf( %d , &n); char str[7]; for (j = 1; j <= n; j++) { scanf( %s , str); l = strlen(str); for (i = 0; str[i] != 0 ; i++) { a[0][i] = a[i][0] = str[i] - 0 ; } printf( %d n , dfs(1)); } }
|
#include <bits/stdc++.h> const int maxn = 5e5 + 10; using namespace std; int main() { string s; long long n; cin >> n; while (n--) { cin >> s; int b = 0, e = s.size() - 1; while (b < e && s[b] == s[e]) b++, e--; if (b >= e) cout << s << n ; else { string s1, s2, cmax; for (int i = b; i <= e; i++) { s1 += s[i]; s2 = s1; reverse(s2.begin(), s2.end()); if (s1 == s2) cmax = s1; } string s3, s4, cmax2; for (int i = e; i >= b; i--) { s3 += s[i]; s4 = s3; reverse(s4.begin(), s4.end()); if (s3 == s4) cmax2 = s3; } if (cmax.size() > cmax2.size()) cout << s.substr(0, b) << cmax << s.substr(e + 1) << endl; else cout << s.substr(0, b) << cmax2 << s.substr(e + 1) << endl; } } }
|
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { x = 0; int f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } x *= f; } const int MAXN = 2e3 + 7; int n; string ch[MAXN][2]; vector<int> g[MAXN]; inline int neg(int u) { return (u > n ? u - n : n + u); } inline void eadd(int u, int v) { g[u].push_back(v); } inline void iadd(int u, int v) { eadd(u, v); eadd(neg(v), neg(u)); } bool mk[MAXN]; int dt[MAXN], cmp[MAXN], t = 0, cc = 0; stack<int> stk; int scc(int u) { stk.push(u); mk[u] = true; int lw = dt[u] = ++t; for (auto v : (g[u])) { if (!dt[v]) { int nl = scc(v); lw = min(lw, nl); } else if (mk[v]) lw = min(lw, dt[v]); } mk[u] = false; if (dt[u] == lw) { ++cc; while (stk.top() != u) { cmp[stk.top()] = cc; if (cmp[neg(stk.top())] == cc) return -1; stk.pop(); } if (cmp[neg(stk.top())] == cc) return -1; cmp[stk.top()] = cc; stk.pop(); } return lw; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = (1); i <= (n); ++i) { string s0, s1; cin >> s0 >> s1; ch[i][0] = s0.substr(0, 3); ch[i][1] = s0.substr(0, 2) + s1.substr(0, 1); } for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (n); ++j) if (i != j) { if (ch[i][0] == ch[j][0]) iadd(i, neg(j)), iadd(neg(i), neg(j)); ; if (ch[i][1] == ch[j][1]) iadd(neg(i), j); if (ch[i][0] == ch[j][1]) iadd(i, j); } for (int i = (1); i <= (2 * n); ++i) if (!dt[i]) if (scc(i) < 0) { cout << NO n ; return 0; } cout << YES n ; for (int i = (1); i <= (n); ++i) { if (cmp[i] > cmp[neg(i)]) cout << ch[i][1] << n ; else cout << ch[i][0] << n ; } return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.