solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; long long mod_plus(long long a, long long b) { return (a + b) % 1000000007; } long long mod_mul(long long a, long long b) { return (a * b) % 1000000007; } long long m[10], g[10]; long long go(char *second, int now) { if (now < 0) return 0; return mod_plus(m[second[now] - '0'], mod_mul(g[second[now] - '0'], go(second, now - 1))); } void process(int dig, char *second) { int len = strlen(second); m[dig] = go(second, len - 1); long long temp = 1; for (len = 0; second[len]; len++) temp = mod_mul(temp, g[second[len] - '0']); g[dig] = temp; return; } char str[100005], tt[100005]; vector<string> all; int main() { scanf("%s", str); int n, i; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", tt); string now = tt; all.push_back(now); } for (i = 0; i < 10; i++) { m[i] = i; g[i] = 10; } for (i = n - 1; i >= 0; i--) { strcpy(tt, all[i].c_str()); process(tt[0] - '0', tt + 3); } cout << go(str, strlen(str) - 1) << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; int n; string w; string a; string h; string actual = "polycarp"; map<string, vector<string> > tree; int dfs_search(string actual) { int ans = 1; for (auto i : tree[actual]) { ans = max(dfs_search(i) + 1, ans); } return ans; } int main(void) { cin >> n; for (int i = 0; i < n; i++) { cin >> a; cin >> w; cin >> h; for (int x = 0; x < a.length(); x++) { a[x] = tolower(a[x]); } for (int x = 0; x < h.length(); x++) { h[x] = tolower(h[x]); } tree[h].push_back(a); } cout << dfs_search(actual) << endl; }
4
#include <bits/stdc++.h> using namespace std; static const int maxn = 1e5 + 5; static const int maxb = 320; vector<int> light_set[maxn]; vector<int> heavy_set[maxb]; int light_heavy_intersection[maxn][maxb]; int heavy_heavy_intersection[maxn][maxb]; int num_ele[maxn]; int mapper[maxn]; long long lazy[maxn]; long long ans_light[maxn]; long long ans_heavy[maxn]; long long arr[maxn]; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= n; i++) cin >> arr[i]; int light = 0; int heavy = 0; for (int i = 1; i <= m; i++) { cin >> num_ele[i]; if (num_ele[i] > maxb) { mapper[i] = ++heavy; long long sum = 0; for (int j = 1; j <= num_ele[i]; j++) { int p; cin >> p; heavy_set[heavy].push_back(p); sum += arr[p]; } sort(heavy_set[heavy].begin(), heavy_set[heavy].end()); ans_heavy[heavy] = sum; } else { mapper[i] = ++light; light_set[light].resize(num_ele[i]); long long sum = 0; for (int &x : light_set[light]) { cin >> x; sum += arr[x]; } sort(light_set[light].begin(), light_set[light].end()); ans_light[light] = sum; } } for (int lgt = 1; lgt <= light; lgt++) { for (int x : light_set[lgt]) { for (int hvy = 1; hvy <= heavy; hvy++) light_heavy_intersection[lgt][hvy] += binary_search(heavy_set[hvy].begin(), heavy_set[hvy].end(), x); } } for (int hvy = 1; hvy <= heavy; hvy++) { for (int x : heavy_set[hvy]) { for (int i = 1; i <= heavy; i++) if (i != hvy) heavy_heavy_intersection[hvy][i] += binary_search(heavy_set[i].begin(), heavy_set[i].end(), x); } } while (q--) { char type; cin >> type; if (type == '?') { int x; cin >> x; if (num_ele[x] > maxb) { int hvy = mapper[x]; long long res = ans_heavy[hvy] + 1LL * num_ele[x] * lazy[hvy]; for (int i = 1; i <= heavy; i++) if (hvy != i) res += heavy_heavy_intersection[hvy][i] * lazy[i]; cout << res << '\n'; } else { int lgt = mapper[x]; long long res = 0; for (int p : light_set[lgt]) res += arr[p]; for (int z = 1; z <= heavy; z++) res += (light_heavy_intersection[lgt][z] * lazy[z]); cout << res << '\n'; } } else { int x; long long val; cin >> x >> val; if (num_ele[x] > maxb) { int hvy = mapper[x]; lazy[hvy] += val; } else { int lgt = mapper[x]; for (int p : light_set[lgt]) arr[p] += val; for (int hvy = 1; hvy <= heavy; hvy++) ans_heavy[hvy] += (light_heavy_intersection[lgt][hvy] * val); } } } }
17
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0, a = 0, cur = 0, tot = 0; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a; cout << (a + tot) / m << ' '; tot = (a + tot) % m; } return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<string> g, g1; bool used[1000]; int main() { int n, kol = 0; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; g.push_back(s); } for (int i = 0; i < n; i++) { string s; cin >> s; g1.push_back(s); } for (int i = 0; i < g1.size(); i++) { string s; s = g1[i]; for (int j = 0; j < g.size(); j++) { if (!used[j] && g[j] == s) { used[j] = 1; break; } } } for (int i = 0; i < g1.size(); i++) { string s = g1[i]; for (int j = 0; j < g.size(); j++) { if (g[j].size() == s.size() && !used[j]) { kol++; used[j] = 1; break; } } } cout << kol; }
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; int main() { ios_base::sync_with_stdio(0); long long n, l, k; long long ans = 0; cin >> n >> l >> k; vector<long long> A(n); for (int i = 0; i < (n); ++i) cin >> A[i]; vector<vector<long long> > Cnt(k + 1); int d; unordered_map<long long, int> M; { int last = -1; vector<long long> B(A); sort(B.begin(), B.end()); int cnt = 0; for (int i = 0; i < (n); ++i) { if (!i || B[i] != B[i - 1]) { if (last >= 0) Cnt[1][last] = cnt; cnt = 1; Cnt[1].push_back(0); last++; M[B[i]] = last; } if (i && B[i] == B[i - 1]) cnt++; } Cnt[1][last] = cnt; d = last + 1; } Cnt[0] = vector<long long>(d, 0); Cnt[0][0] = 1; for (int(i) = (2); i <= (k); ++(i)) Cnt[i] = vector<long long>(d, 0); for (int(i) = (1); i <= (min(k, l / n)); ++(i)) { long long pref = 0; long long sum = 0; for (int(j) = (0); j <= (d - 1); ++(j)) { pref = (pref + Cnt[i - 1][j]) % MOD; Cnt[i][j] = (pref * Cnt[1][j]) % MOD; sum = (sum + Cnt[i][j]) % MOD; } ans = (ans + (sum % MOD) * ((l / n - i + 1) % MOD)) % MOD; } vector<long long> CntSuf(d, 0); for (int i = 0; i < (l % n); ++i) CntSuf[M[A[i]]]++; for (int(i) = (1); i <= (min(k, (l + n - 1) / n)); ++(i)) { long long pref = 0; for (int(j) = (0); j <= (d - 1); ++(j)) { pref = (pref + Cnt[i - 1][j]) % MOD; ans = (ans + pref * CntSuf[j]) % MOD; } } cout << ans << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 500005; priority_queue<ll, vector<ll>, greater<ll>> pq; int n, a[N], b[N], cnt[N], idx, top, stk[N]; ll sum; int main() { ios::sync_with_stdio(false); ll ans = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], ans += a[i]; sort(a + 1, a + n + 1, [](int a, int b) { return a > b; }); for (int i = 1; i <= n; ++cnt[idx], i++) if (i == 1 || a[i] != a[i - 1]) b[++idx] = a[i]; for (int i = 1; i <= n; i++) { top = 0; ll mx = min(sum - ll(pq.size() * 2), ll(cnt[i])); for (int j = 1; j <= mx; j++) stk[++top] = b[i]; mx = min(sum, ll(cnt[i])) - mx; for (int j = 1; j <= mx; j += 2) { ll t = pq.top(); pq.pop(); if (t < b[i]) { stk[++top] = b[i]; if (j < mx) stk[++top] = b[i]; } else { stk[++top] = t; if (j < mx && b[i] * 2 > t) stk[++top] = b[i] * 2 - t; } } for (int j = 1; j <= top; j++) pq.push(stk[j]); sum += cnt[i]; } while (!pq.empty()) ans -= pq.top(), pq.pop(); cout << ans << endl; return 0; }
22
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long l, r; long long ans[N]; long long store1[N], store2[N]; vector<pair<long long, long long> > lft, rght; long long query1(long long i, long long j, long long k) { cout << 1 << " " << i << " " << j << " " << k << endl; long long x; cin >> x; return x; } long long query2(long long i, long long j, long long k) { cout << 2 << " " << i << " " << j << " " << k << endl; long long x; cin >> x; return x; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; for (long long i = 3; i <= n; i++) { long long side = query2(1, 2, i); long long area = query1(1, 2, i); if (side == 1) { l++; lft.push_back({area, i}); } else { r++; rght.push_back({area, i}); } } sort(lft.rbegin(), lft.rend()); ans[1] = 1; long long idx = 1; if (lft.size()) { long long j = lft[0].second; vector<pair<long long, long long> > L, R; for (long long k = 1; k < lft.size(); k++) { long long side = query2(1, j, lft[k].second); if (side == 1) L.push_back({lft[k].first, lft[k].second}); else R.push_back({lft[k].first, lft[k].second}); } sort(L.begin(), L.end()); sort(R.rbegin(), R.rend()); for (auto it : L) ans[++idx] = it.second; ans[++idx] = j; for (auto it : R) ans[++idx] = it.second; } ans[++idx] = 2; sort(rght.rbegin(), rght.rend()); if (rght.size()) { long long j = rght[0].second; vector<pair<long long, long long> > L, R; for (long long k = 1; k < rght.size(); k++) { long long side = query2(1, j, rght[k].second); if (side == 1) L.push_back({rght[k].first, rght[k].second}); else R.push_back({rght[k].first, rght[k].second}); } sort(L.begin(), L.end()); sort(R.rbegin(), R.rend()); for (auto it : L) ans[++idx] = it.second; ans[++idx] = j; for (auto it : R) ans[++idx] = it.second; } cout << 0 << " "; cout << ans[1] << " "; for (long long i = n; i >= 2; i--) cout << ans[i] << " "; return 0; }
15
#include <bits/stdc++.h> using namespace std; char s[] = {"AEIOUY"}; const int MAXN = 5e5 + 10; bool jd(char ch) { for (int i = 0; i < 6; i++) if (ch == s[i]) return true; return false; } int n; long double ans = 0; long double sum[MAXN]; char a[MAXN]; void cal(int x, int y) { if (x > y) return; if (x == y) { if (jd(a[x])) ans += 1; return; } int mid = (x + y) / 2; cal(x, mid); cal(mid + 1, y); int rl = y - mid; long double q = 0; for (int i = x; i <= mid; i++) { int l = mid - i + 1; int r = l + rl; q += sum[r] - sum[l]; if (jd(a[i])) ans += q; } int ll = mid - x + 1; q = 0; for (int i = y; i > mid; i--) { int l = i - mid; int r = ll + l; q += sum[r] - sum[l]; if (jd(a[i])) ans += q; } } int main() { cin >> a; n = strlen(a); sum[1] = 1; long double p = 1; for (int i = 2; i <= n; i++) { sum[i] = p / i; sum[i] += sum[i - 1]; } cal(0, n - 1); double x = ans; printf("%.10f\n", x); return 0; }
12
#include <bits/stdc++.h> using namespace std; struct pt { int x, y; }; int det(pt a, pt b, pt c) { return a.x * b.y + b.x * c.y + c.x * a.y - a.y * b.x - b.y * c.x - c.y * a.x; } pt a[5], b[5]; bool inside(pt p, pt* a) { int l = 0, g = 0; for (int i = 0; i < 4; i++) { int d = det(p, a[i], a[i + 1]); if (d < 0) l++; else if (d > 0) g++; } return l == 0 || g == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); for (int i = 0; i < 4; i++) cin >> a[i].x >> a[i].y; a[4] = a[0]; for (int i = 0; i < 4; i++) cin >> b[i].x >> b[i].y; b[4] = b[0]; for (int i = 0; i <= 4; i++) { a[i].x *= 2; a[i].y *= 2; b[i].x *= 2; b[i].y *= 2; } for (int i = -500; i <= 500; i++) for (int j = -500; j <= 500; j++) if (inside({i, j}, a) && inside({i, j}, b)) return cout << "YES\n", 0; cout << "NO\n"; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int counterOdd = 0; int counterPr = 0; vector<long> x(n); for (int i = 0; i < n; i++) { cin >> x[i]; if (x[i] % 2 == 1) counterOdd++; else counterPr++; } if (counterOdd > counterPr) { cout << counterPr; } else cout << counterOdd; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, p; cin >> n >> k >> p; vector<int> even; vector<int> odd; for (int i = 0; i < n; i++) { int temp; cin >> temp; if (temp % 2 == 0) { even.push_back(temp); } else { odd.push_back(temp); } } vector<vector<int>> ans(k); int i, j; for (i = 0; i < odd.size() && k - p > i; i++) { ans[i].push_back(odd[i]); } for (j = 0; j < even.size() && p > j; j++) { ans[j + k - p].push_back(even[j]); } int even_size = even.size(); int odd_size = odd.size(); if (odd_size == i && even_size == j) { cout << "YES" << endl; for (int a = 0; a < k; a++) { cout << ans[a].size() << " "; for (int b = 0; b < ans[a].size(); b++) { cout << ans[a][b] << " "; } cout << endl; } return 0; } if (i < k - p) { cout << "NO"; return 0; } int remaining = (odd_size - i) / 2; if (j < p && remaining >= p - j) { while (remaining && (j + k - p) < k) { ans[j + k - p].push_back(odd[i++]); ans[j + k - p].push_back(odd[i++]); remaining--; j++; } } else if (j < p) { cout << "NO" << endl; return 0; } while (j < even_size) { int index = p == 0 ? 0 : k - p; ans[index].push_back(even[j]); j++; } if (p != 0 && i < odd_size && ((odd_size - i) % 2) == 0) { while (i < odd_size) { ans[k - p].push_back(odd[i++]); ans[k - p].push_back(odd[i++]); } } else if (p == 0 && ((odd_size - i) % 2) == 0) { while (i < odd_size) { ans[0].push_back(odd[i++]); ans[0].push_back(odd[i++]); } } else if (i < odd_size) { cout << "NO" << endl; return 0; } cout << "YES" << endl; for (int a = 0; a < k; a++) { cout << ans[a].size() << " "; for (int b = 0; b < ans[a].size(); b++) { cout << ans[a][b] << " "; } cout << endl; } }
9
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x, char ed = '\n') { static short st[30]; short tp = 0; if (x < 0) putchar('-'), x = -x; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar('0' | st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int N = 200500; struct node { int mn, add; long long res, cnt, cc; } t[N << 3]; int p[N], s1[N], s2[N], t1, t2, q, n; inline void Add(int p, int val) { t[p].add += val, t[p].mn += val; } inline void Ans(int p, int c, int mn) { if (t[p].mn != mn) return; t[p].res += t[p].cnt * c, t[p].cc += c; } void spread(int p) { if (t[p].add) Add(p << 1, t[p].add), Add(p << 1 | 1, t[p].add), t[p].add = 0; if (t[p].cc) Ans(p << 1, t[p].cc, t[p].mn), Ans(p << 1 | 1, t[p].cc, t[p].mn), t[p].cc = 0; } void build(int p, int l, int r) { t[p].mn = 1e9, t[p].cnt = 1; if (l == r) return; int mid = (l + r) >> 1; build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r); t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt; } void Update(int p) { if (t[p << 1].mn < t[p << 1 | 1].mn) t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt; else if (t[p << 1].mn > t[p << 1 | 1].mn) t[p].mn = t[p << 1 | 1].mn, t[p].cnt = t[p << 1 | 1].cnt; else t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt; } void update(int p, int l, int r, int L, int R, int val) { if (L <= l && r <= R) return Add(p, val); spread(p); int mid = (l + r) >> 1; if (L <= mid) update(p << 1, l, mid, L, R, val); if (mid < R) update(p << 1 | 1, mid + 1, r, L, R, val); Update(p); } void refresh(int p, int l, int r, int x) { if (l == r) return t[p].mn = 0, void(); int mid = (l + r) >> 1; spread(p); if (x <= mid) refresh(p << 1, l, mid, x); else refresh(p << 1 | 1, mid + 1, r, x); Update(p); } void maintain(int p, int l, int r, int L, int R) { if (L <= l && r <= R) return Ans(p, 1, 0); int mid = (l + r) >> 1; spread(p); if (L <= mid) maintain(p << 1, l, mid, L, R); if (mid < R) maintain(p << 1 | 1, mid + 1, r, L, R); t[p].res = t[p << 1].res + t[p << 1 | 1].res; } long long query(int p, int l, int r, int L, int R) { if (L <= l && r <= R) return t[p].res; int mid = (l + r) >> 1; spread(p); long long ans = 0; if (L <= mid) ans += query(p << 1, l, mid, L, R); if (mid < R) ans += query(p << 1 | 1, mid + 1, r, L, R); return ans; } vector<pair<int, int> > v[N]; long long ans[N]; int main() { read(n); for (int i = 1; i <= n; i++) read(p[i]); read(q); for (int i = 1, l, r; i <= q; i++) read(l), read(r), v[r].emplace_back(l, i); build(1, 1, n); for (int i = 1; i <= n; i++) { while (t1 > 0 && p[s1[t1]] < p[i]) update(1, 1, n, s1[t1 - 1] + 1, s1[t1], -p[s1[t1]]), t1--; while (t2 > 0 && p[s2[t2]] > p[i]) update(1, 1, n, s2[t2 - 1] + 1, s2[t2], p[s2[t2]]), t2--; if (i != 1) update(1, 1, n, 1, i - 1, -1); refresh(1, 1, n, i); s1[++t1] = i, s2[++t2] = i; update(1, 1, n, s1[t1 - 1] + 1, i, p[i]); update(1, 1, n, s2[t2 - 1] + 1, i, -p[i]); Ans(1, 1, 0); for (auto t : v[i]) ans[t.second] = query(1, 1, n, t.first, i); } for (int i = 1; i <= q; i++) write(ans[i]); return 0; }
22
#include <bits/stdc++.h> using namespace std; unsigned long long popcount(unsigned long long value) { unsigned long long pos = 0; while (value) { if (value & 1) ++pos; value >>= 1; } return pos; } unsigned long long GetLowestBitPos(unsigned long long value) { unsigned long long pos = 0; while ((value & 1)) { value >>= 1; ++pos; } return pos; } unsigned long long setbit(unsigned long long v, unsigned long long pos) { return v | 1ULL << pos; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { unsigned long long l, r; cin >> l >> r; unsigned long long ans = l; while (true) { unsigned long long tmp = ans; bitset<64> lol(ans); ans = setbit(ans, GetLowestBitPos(ans)); if (ans > r) { cout << tmp << endl; break; } } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { double W, H, a; cin >> W >> H >> a; if (W < H) swap(W, H); if (a > 90) a = 180 - a; a = (a / 180) * 3.14159265358979323846; double p = (W * W - H * H) / (2 * W); cout.precision(25); if (tan(a) > H / p) { cout << (H * H) / sin(a) << endl; return 0; } double h = H * cos(a); double w = W * cos(a); double s = sin(a); double t = cos(a); double y = (h * (1 + t) - s * w) / (((1 + t) * (1 + t) - s * s) * t); double x = w / (1 + t) - (s * t) / (1 + t) * y; cout << W * H - x * x * tan(a) - y * y * s * t << endl; return 0; }
12
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") #pragma GCC optimize("03") #pragma GCC target("sse4") using namespace std; vector<int> divs[(long long)(1e3 + 5)]; void criba() { for (int i = 1; i < (long long)(1e3 + 5); i++) for (int j = i; j < (long long)(1e3 + 5); j += i) divs[j].push_back(i); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); criba(); for (int i = 0; i < (long long)(1e3 + 5); i++) reverse((divs[i]).begin(), (divs[i]).end()); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> fs((long long)(1e3 + 5)); int ma = 0; for (int i = 0; i < n; i++) { int x; cin >> x; fs[x]++; ma = max(ma, x); } vector<int> res; for (int i = 0; i < fs[ma]; i++) res.push_back(ma); fs[ma] = 0; int x = ma; while (1) { int nex = -1; for (auto d : divs[x]) { int sum = 0; for (int i = d; i < (long long)(1e3 + 5); i += d) sum += fs[i]; if (sum) { nex = d; break; } } if (nex == -1) break; for (int i = nex; i < (long long)(1e3 + 5); i += nex) { for (int j = 0; j < fs[i]; j++) res.push_back(i); fs[i] = 0; } x = nex; } for (int i = 0; i < n; i++) cout << res[i] << ' '; cout << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int iinf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3f; const int N = 5e5; int n, m; string s; vector<int> e[N]; void adde(int u, int v) { e[u].push_back(v), e[v].push_back(u); } const int T = 1e7; int ls[T], rs[T], val[T], tn; int newn() { ls[tn] = rs[tn] = -1, val[tn] = 0; return tn++; } void pushup(int k) { val[k] = 0; if (~ls[k]) val[k] ^= val[ls[k]]; if (~rs[k]) val[k] ^= val[rs[k]]; } void add(int& k, int first, int v, int l = 0, int r = n - 1) { if (r < first || first < l) return; if (!~k) k = newn(); if (l == r) return void(val[k] ^= v); add(ls[k], first, v, l, ((l + r) >> 1)), add(rs[k], first, v, ((l + r) >> 1) + 1, r), pushup(k); } int getv(int k, int first, int l = 0, int r = n - 1) { if (r < first || first < l) return 0; if (!~k) return 0; if (l == r) return val[k]; return getv(ls[k], first, l, ((l + r) >> 1)) + getv(rs[k], first, ((l + r) >> 1) + 1, r); } int merge(int k, int p, int l = 0, int r = n - 1) { if (!~k || !~p) return ~k ? k : p; if (l == r) return val[k] ^= val[p], k; ls[k] = merge(ls[k], ls[p], l, ((l + r) >> 1)), rs[k] = merge(rs[k], rs[p], ((l + r) >> 1) + 1, r), pushup(k); return k; } int dep[N], rt[N]; bool ans[N]; vector<pair<int, int>> ask[N]; void getd(int u = 0, int fa = -1) { for (int v : e[u]) if (v != fa) dep[v] = dep[u] + 1, getd(v, u); } bool ok(int s) { return s == 0 || s == (s & -s); } void Dfs(int u = 0, int fa = -1) { for (int v : e[u]) if (v != fa) Dfs(v, u), rt[u] = merge(rt[u], rt[v]); for (auto i : ask[u]) ans[i.first] = ok(getv(rt[u], i.second)); } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = (1), I = (n); i < I; i++) { int fa; cin >> fa, --fa; adde(i, fa); } fill(dep, dep + n, -1), dep[0] = 0, getd(); cin >> s, fill(rt, rt + n, -1); for (int i = (0), I = (n); i < I; i++) add(rt[i], dep[i], 1 << (s[i] - 'a')); for (int i = (0), I = (m); i < I; i++) { int u, k; cin >> u >> k, --u, --k; ask[u].push_back(make_pair((i), (k))); } Dfs(); for (int i = (0), I = (m); i < I; i++) cout << (ans[i] ? "Yes" : "No") << '\n'; return 0; }
14
#include <bits/stdc++.h> using namespace std; char a[260]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; cin >> a; int x = 0, y = 0, m = 0, flag = 0; int t = 1, tt; for (int i = 0; i < n; i++) { if (a[i] == '(') { flag = 1; tt = i; t = 1; } else if (a[i] == ')') flag = 0; if (a[i] == '_' || a[i] == '(') { x = max(x, m); m = 0; } else if (a[i] != '_' && a[i] != ')' && flag == 0) m++; if (flag == 1) { if (a[tt + 1] != '_' && t == 1 && a[tt + 1] != ')') { y++; t = 0; } if (a[i] == '_' && a[i + 1] != '_' && a[i + 1] != ')') { y++; } } } x = max(x, m); cout << x << ' ' << y << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; struct st { int x, y; int idx; }; st a[200011]; int d, n, m; struct compX { bool operator()(st a, st b) { return a.x < b.x; } }; struct compY { bool operator()(st a, st b) { return a.y < b.y; } }; int vst[100011]; int l[100011]; int r[100011]; int t[100011]; int dd[100011]; int main() { cin >> d; cin >> n >> m; for (int i = 0; i < d; i++) { int x, y; scanf("%d %d", &x, &y); a[i * 2].x = x; a[i * 2].y = y; a[i * 2].idx = i; scanf("%d %d", &x, &y); a[i * 2 + 1].x = x; a[i * 2 + 1].y = y; a[i * 2 + 1].idx = i; } sort(a, a + 2 * d, compX()); int i = 0; int cur = 0; while (i < 2 * d) { int s = i; int temp = cur; while (i < 2 * d && a[i].x == a[s].x) { int idx = a[i].idx; if (vst[idx]) l[idx] = temp - 1; else l[idx] = temp; i++; } for (int j = s; j < i; j++) { int idx = a[j].idx; if (!vst[idx]) { vst[idx] = 1; cur++; } } } i = 2 * d - 1; cur = 0; memset(vst, 0, sizeof vst); while (i >= 0) { int s = i; int temp = cur; while (i >= 0 && a[i].x == a[s].x) { int idx = a[i].idx; if (vst[idx]) r[idx] = temp - 1; else r[idx] = temp; i--; } for (int j = s; j > i; j--) { int idx = a[j].idx; if (!vst[idx]) { vst[idx] = 1; cur++; } } } sort(a, a + 2 * d, compY()); memset(vst, 0, sizeof vst); i = 0; cur = 0; while (i < 2 * d) { int s = i; int temp = cur; while (i < 2 * d && a[i].y == a[s].y) { int idx = a[i].idx; if (vst[idx]) t[idx] = temp - 1; else t[idx] = temp; i++; } for (int j = s; j < i; j++) { int idx = a[j].idx; if (!vst[idx]) { vst[idx] = 1; cur++; } } } i = 2 * d - 1; cur = 0; memset(vst, 0, sizeof vst); while (i >= 0) { int s = i; int temp = cur; while (i >= 0 && a[i].y == a[s].y) { int idx = a[i].idx; if (vst[idx]) dd[idx] = temp - 1; else dd[idx] = temp; i--; } for (int j = s; j > i; j--) { int idx = a[j].idx; if (!vst[idx]) { vst[idx] = 1; cur++; } } } int r1, r2, r3, r4; cin >> r1 >> r2 >> r3 >> r4; int res = -1; for (int i = 0; i < d; i++) { if (l[i] == r1 && r[i] == r2 && t[i] == r3 && dd[i] == r4) { res = i + 1; break; } } cout << res << endl; }
12
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; template <class T> inline void chkmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } long long IsSquare(long long n) { long long res = sqrt(n); if (res * res == n) return res; res += 1; if (res * res == n) return res; return -1; } long long IsCube(long long n) { long long res = cbrt(n); if (res * res * res == n) return res; res += 1; if (res * res * res == n) return res; return -1; } map<long long, int> primes; map<long long, int> doublePrimes; void Add(long long p, int c) { if (primes.find(p) == primes.end()) primes[p] = c; else primes[p] += c; } const long long MOD = 998244353; const int MAXN = 507; long long a[MAXN]; bitset<MAXN> ded; long long GCD(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } int main() { int m; cin >> m; for (int i = 0; i < m; ++i) { long long n; cin >> n; a[i] = n; long long sq = IsSquare(n); if (sq != -1) { long long sq2 = IsSquare(sq); if (sq2 == -1) Add(sq, 2); else Add(sq2, 4); ded[i] = true; continue; } long long cb = IsCube(n); if (cb != -1) { Add(cb, 3); ded[i] = true; continue; } } long long res = 1; for (int i = 0; i < m; ++i) { if (ded[i]) continue; for (int j = 0; j < m; ++j) { long long d = GCD(a[i], a[j]); if (d != 1 && d != a[i]) { Add(d, 1); Add(a[i] / d, 1); ded[i] = true; break; } } if (!ded[i]) { if (doublePrimes.find(a[i]) != doublePrimes.end()) ++doublePrimes[a[i]]; else doublePrimes[a[i]] = 1; } } for (pair<long long, int> c : primes) { res *= c.second + 1; res %= MOD; } for (pair<long long, int> c : doublePrimes) { res *= c.second + 1; res %= MOD; res *= c.second + 1; res %= MOD; } cout << res << '\n'; }
12
#include <bits/stdc++.h> using namespace std; const int maxn = 4e4 + 5; const int inf = 0x3f3f3f3f; int a[maxn]; int b[maxn]; int tong1[maxn]; int tong2[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) cin >> b[i]; int len = 0; for (int i = 0; i < n; i++) { if (a[i] == 1) { len++; } else { for (int j = 1; j <= len; j++) { tong1[j] += len + 1 - j; } len = 0; } } if (len != 0) { for (int j = 1; j <= len; j++) { tong1[j] += len + 1 - j; } len = 0; } for (int i = 0; i < m; i++) { if (b[i] == 1) { len++; } else { for (int j = 1; j <= len; j++) { tong2[j] += len + 1 - j; } len = 0; } } if (len != 0) { for (int j = 1; j <= len; j++) { tong2[j] += len + 1 - j; } len = 0; } long long res = 0; for (int i = 1; i <= n; i++) { if (k % i == 0) { if ((k / i) <= m) res += tong1[i] * tong2[k / i]; } } cout << res << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<int> v; inline bool check(int i, int j) { return (2 * v[i] > v[j] && v[i] != v[j]); } int main() { int n; cin >> n; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { if (check(i - 1, i)) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") using namespace std; int n; int a[100001]; int pos[100001]; int b[100001]; set<int> poses; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; pos[a[i]] = i; } int cnt = 0; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = n - 1; i >= 0; i--) { set<int>::iterator it = poses.lower_bound(pos[b[i]]); if (it != poses.begin()) cnt++; poses.insert(pos[b[i]]); } cout << cnt; return 0; }
5
#include <bits/stdc++.h> using namespace std; int MOD; vector<long long> dif_pos; struct matr { long long m[2][2]; }; matr operator*(matr a, matr b) { matr res; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { res.m[i][j] = 0; for (int k = 0; k < 2; k++) { res.m[i][j] = (res.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD; } } return res; } matr power(matr m, long long p) { if (p == 1) return m; if (p % 2) return m * power(m, p - 1); return power(m * m, p / 2); } int fib(long long num) { if (num == 1) return 1; matr f; f.m[0][0] = 0; f.m[0][1] = f.m[1][0] = f.m[1][1] = 1; f = power(f, num); return f.m[0][1]; } int main() { long long l, r, k; cin >> MOD >> l >> r >> k; if (MOD == 1) { cout << "0\n"; return 0; } for (long long d = 1; d * d <= r; d++) { dif_pos.push_back(d); dif_pos.push_back(r / d); } for (long long d = 1; d * d <= l - 1; d++) { dif_pos.push_back(l / d); } long long ans = 0; for (int i = 0; i < int(dif_pos.size()); i++) { long long hi = r / dif_pos[i]; long long lo = (l - 1) / dif_pos[i]; if (hi - lo >= k) ans = max(ans, dif_pos[i]); } cout << fib(ans) << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<long long, int> a[n]; long long sum = 0ll; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a, a + n); long long j = 0ll; for (int i = n - 1; i >= 0; i--) { sum += (a[i].first * j + 1); j++; } cout << sum << '\n'; for (int i = n - 1; i >= 0; i--) cout << a[i].second + 1 << ' '; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1000001; long long tableau[MAXN]; int32_t main() { long long N, total; cin >> N >> total; long long valeurActu = N * (N + 1) / 2; if (valeurActu > total) { cout << -1 << endl; exit(0); } long long indice = 0; for (long long i = 0; i < N; i++) { tableau[i] = i + 1; } long long plusGrand = 0; while (total - valeurActu > 0 && indice < N / 2) { long long dif = total - valeurActu; long long posF = max(0LL, max(plusGrand, (N - indice - 1) - dif)); valeurActu += (N - indice - 1) - posF; tableau[N - indice - 1] = tableau[posF]; tableau[posF] = N - indice; indice++; plusGrand = posF + 1; } cout << valeurActu << endl; for (long long i = 0; i < N; i++) { cout << i + 1 << " "; } cout << endl; for (long long i = 0; i < N; i++) { cout << tableau[i] << " "; } cout << endl; }
16
#include <bits/stdc++.h> using namespace std; const int MAX = 200005; const int INF = 1000000009; int main() { ios_base::sync_with_stdio(false); int N, X; cin >> N >> X; if (N == 1) { cout << "YES\n"; cout << X << "\n"; return 0; } if (N == 2 && X == 0) { cout << "NO\n"; return 0; } if (N == 2) { cout << "YES\n"; cout << 0 << " " << X << "\n"; return 0; } int val = 0; cout << "YES\n"; srand(time(NULL)); while (true) { vector<int> cur; val = 0; set<int> s; int last = 0; for (int i = 0; i < N - 3; i++) { int r = last + 1; last = r; val ^= r; cur.push_back(r); s.insert(r); } int r = rand() % 1000; cur.push_back(last + r); s.insert(last + r); val ^= (last + r); val ^= X; if (val == 0 && X != 0) { for (int a : cur) cout << a << " "; cout << 0 << " " << X << "\n"; return 0; } int magic = (1 << 19); cur.push_back(magic); cur.push_back(val | magic); s.insert(magic); s.insert(val | magic); if (s.size() == N) { for (int a : cur) cout << a << " "; cout << "\n"; return 0; } } return 0; }
11
#include <bits/stdc++.h> using namespace std; struct edge { long long nxt, to; } e[100001 << 1]; struct edges { long long x, y; } v[100001]; long long n, m, tot, cnt, id[100001], dep[100001], fa[100001], s[100001], son[100001], top[100001], h[100001]; bool ans[100001 << 2], w[100001]; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void add(long long x, long long y) { e[++tot].nxt = h[x]; h[x] = tot; e[tot].to = y; } inline long long ls(long long k) { return k << 1; } inline long long rs(long long k) { return k << 1 | 1; } inline void push_up(long long k) { ans[k] = ans[ls(k)] && ans[rs(k)]; } void build(long long k, long long l, long long r) { if (l == r) { ans[k] = w[l]; return; } long long mid = (l + r) >> 1; build(ls(k), l, mid); build(rs(k), mid + 1, r); push_up(k); } void update(long long pos, long long l, long long r, long long k, bool p) { if (l == r) { ans[k] = p; return; } long long mid = (l + r) >> 1; if (pos <= mid) update(pos, l, mid, ls(k), p); else update(pos, mid + 1, r, rs(k), p); push_up(k); } bool query(long long nl, long long nr, long long l, long long r, long long k) { if (l >= nl && r <= nr) return ans[k]; bool res = 1; long long mid = (l + r) >> 1; if (nl <= mid) res = res && query(nl, nr, l, mid, ls(k)); if (nr > mid) res = res && query(nl, nr, mid + 1, r, rs(k)); return res; } void dfs1(long long k, long long f, long long deep) { fa[k] = f; s[k] = 1; dep[k] = deep; long long maxson = -1; for (register long long i = h[k]; i; i = e[i].nxt) { if (e[i].to == f) continue; dfs1(e[i].to, k, deep + 1); s[k] += s[e[i].to]; if (s[e[i].to] > maxson) { maxson = s[e[i].to]; son[k] = e[i].to; } } } void dfs2(long long k, long long t) { id[k] = ++cnt; w[cnt] = 1; top[k] = t; if (!son[k]) return; dfs2(son[k], t); for (register long long i = h[k]; i; i = e[i].nxt) { if (e[i].to == fa[k] || e[i].to == son[k]) continue; dfs2(e[i].to, e[i].to); } } inline long long q(long long x, long long y) { long long res = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); if (!query(id[top[x]], id[x], 1, n, 1)) return -1; res += id[x] - id[top[x]] + 1; x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (!query(id[x] + 1, id[y], 1, n, 1)) return -1; res += id[y] - id[x]; return res; } signed main() { n = read(); for (long long i = 1; i < n; ++i) { v[i].x = read(), v[i].y = read(); add(v[i].x, v[i].y); add(v[i].y, v[i].x); } dfs1(1, 0, 1); dfs2(1, 1); build(1, 1, n); m = read(); while (m--) { long long opt = read(), x, y; if (opt == 1) { x = read(); if (dep[v[x].x] > dep[v[x].y]) swap(v[x].x, v[x].y); update(id[v[x].y], 1, n, 1, 1); } if (opt == 2) { x = read(); if (dep[v[x].x] > dep[v[x].y]) swap(v[x].x, v[x].y); update(id[v[x].y], 1, n, 1, 0); } if (opt == 3) { x = read(), y = read(); printf("%lld\n", q(x, y)); } } return 0; }
13
#include <bits/stdc++.h> using namespace std; template <class T> void dbs(string str, T t) { cerr << str << " : " << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...); } template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const set<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream& operator<<(ostream& os, const map<S, T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> void prc(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << *i; } cerr << "]\n"; } const int N = 100000; set<int> S[2][2]; int questionCount[N + 1]; pair<int, int> dp[N + 1]; int main() { int n; cin >> n; string s; cin >> s; int m; cin >> m; for (int i = (int)(0); i <= (int)(n - 1); i++) { if (s[i] == '?') continue; S[s[i] - 'a'][i % 2].insert(i); } for (int i = (int)(1); i <= (int)(n); i++) { questionCount[i] = questionCount[i - 1]; if (s[i - 1] == '?') questionCount[i]++; } dp[0] = make_pair(0, 0); for (int i = (int)(1); i <= (int)(n); i++) { dp[i] = dp[i - 1]; if (i < m) continue; bool bad = false; int temp = i - m; temp %= 2; auto it = S[0][temp ^ 1].lower_bound(i); if (it != S[0][temp ^ 1].begin()) { it--; if (*it >= i - m) bad = true; } it = S[1][temp].lower_bound(i); if (it != S[1][temp].begin()) { it--; if (*it >= i - m) bad = true; } if (!bad) { if (dp[i - m].first + 1 > dp[i].first) { dp[i] = make_pair(dp[i - m].first + 1, dp[i - m].second + questionCount[i] - questionCount[i - m]); } else if (dp[i - m].first + 1 == dp[i].first and dp[i - m].second + questionCount[i] - questionCount[i - m] < dp[i].second) { dp[i] = make_pair(dp[i - m].first + 1, dp[i - m].second + questionCount[i] - questionCount[i - m]); } } } cout << dp[n].second << "\n"; return 0; }
13
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long result = 1; while (b > 0) { if (b % 2 == 1) { result *= a; } a *= a; b /= 2; } return result; } long long gcd(long long x, long long y) { long long r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } long long countSetBits(long long x) { long long Count = 0; while (x > 0) { if (x & 1) Count++; x = x >> 1; } return Count; } bool isPerfectSquare(long long n) { long long sr = sqrt(n); if (sr * sr == n) return true; else return false; } long long mod(long long x, long long M) { return ((x % M + M) % M); } long long add(long long a, long long b, long long M) { return mod(mod(a, M) + mod(b, M), M); } long long mul(long long a, long long b, long long M) { return mod(mod(a, M) * mod(b, M), M); } long long powerM(long long a, long long b, long long M) { long long res = 1ll; while (b) { if (b % 2ll == 1ll) { res = mul(a, res, M); } a = mul(a, a, M); b /= 2ll; } return res; } long long nCr(long long n, long long k) { if (n < k) return 0; if (k == 0) return 1; long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); } long long nCrM(long long n, long long r, long long M) { if (n < r) return 0; if (r == 0) return 1; vector<long long> fact(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i, M); } return mul(mul(fact[n], modInverse(fact[r], M), M), modInverse(fact[n - r], M), M); } void solve() { long long n, x; cin >> n >> x; vector<long long> v(n); for (auto& w : v) cin >> w; sort(v.rbegin(), v.rend()); long long ans = 0; long long sum = 0; for (long long i = 0; i < n; i++) { sum += v[i]; if (sum >= x * (i + 1)) { ans++; } else { break; } } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long t = 0; cin >> t; while (t--) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const int mod = 1000000007; const int N = 200005; int n, dp[N], ans; pair<int, int> p[N]; int root, t[30 * N], ls[30 * N], rs[30 * N], vn; void create(int &v) { if (v == 0) v = ++vn; } void update(int &v, int l, int r, int pos, int val) { create(v); if (l == r) t[v] = max(t[v], val); else { if (pos <= ((l + r) >> 1)) update(ls[v], l, ((l + r) >> 1), pos, val); else update(rs[v], ((l + r) >> 1) + 1, r, pos, val); t[v] = max(t[ls[v]], t[rs[v]]); } } int get(int &v, int l, int r, int L, int R) { if (v == 0 || r < L || R < l) return 0; if (L <= l && r <= R) return t[v]; return max(get(ls[v], l, ((l + r) >> 1), L, R), get(rs[v], ((l + r) >> 1) + 1, r, L, R)); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d %d", &p[i].first, &p[i].second); sort(p + 1, p + n + 1); for (int i = n; i > 0; i--) { int L = p[i].first + p[i].second; int R = inf; int pos = p[i].first - p[i].second; dp[i] = get(root, 1, inf, L, R) + 1; if (pos > 0) update(root, 1, inf, pos, dp[i]); } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[i]); cout << ans << endl; return 0; }
10
#include <bits/stdc++.h> int main() { double x; int a, h; double mind = 100000; int mina, minh; scanf("%lf", &x); for (h = 1; h <= 10; h++) { for (a = 1; a <= 10; a++) { double r; r = (double)(a * a * h * h); r /= (double)(a * a + 4.0 * h * h); r = sqrt(r); r = (double)((int)(r * 1000000 + 0.5)) / 1000000; if (r >= x && r - x < mind) { mind = r - x; mina = a; minh = h; } } } printf("%d %d\n", mina, minh); }
10
#include <bits/stdc++.h> using namespace std; namespace Debug { void __print(signed x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << '\'' << x << '\''; } void __print(const char *x) { cout << '\"' << x << '\"'; } void __print(const string &x) { cout << '\"' << x << '\"'; } void __print(bool x) { cout << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; } template <typename T> void __print(const T &x) { int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; } void _print() { cout << "]" << endl; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); } } // namespace Debug using namespace Debug; template <typename Arg1> void p(Arg1 &&arg1) { cout << arg1; } template <typename Arg1, typename... Args> void p(Arg1 &&arg1, Args &&...args) { cout << arg1; p(args...); } template <typename Arg1> void read(Arg1 &&arg1) { cin >> arg1; } template <typename Arg1, typename... Args> void read(Arg1 &&arg1, Args &&...args) { cin >> arg1; read(args...); } const bool DEBUG = true; long long power(long long n, long long m) { long long p = 1; if (m == 0) return 1; p = (power(n, m / 2) % 1000000007); p = (p % 1000000007 * p % 1000000007) % 1000000007; return (m & 1 ? ((p % 1000000007 * n % 1000000007) % 1000000007) : (p % 1000000007)); } void solve() { long long n; cin >> n; vector<pair<long long, long long> > v[5]; for (long long i = 0; i < n; i++) { string s; cin >> s; vector<long long> cnt(5, 0); for (auto x : s) cnt[x - 'a']++; for (long long i = 0; i < 5; i++) { v[i].push_back({cnt[i] - ((long long)s.size() - cnt[i]), ((long long)s.size() - cnt[i])}); } } for (long long i = 0; i < 5; i++) sort(v[i].rbegin(), v[i].rend()); for (long long i = 0; i < 5; i++) { for (long long j = 0; j < (long long)v[i].size(); j++) { v[i][j].first = v[i][j].first + v[i][j].second; } } long long ans = 0; for (long long i = 0; i < 5; i++) { long long count1 = 0, count = 0; long long cnt1 = 0; for (long long j = 0; j < n; j++) { long long f1 = v[i][j].first; long long s1 = v[i][j].second; count1 += f1; count += s1; if (count1 > count) { ++cnt1; } } ans = max(ans, cnt1); } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { if (!1) fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock() / CLOCKS_PER_SEC); long long n; cin >> n; } } __S; bool p[2111111]; long long a[1111]; int main(void) { p[1] = true; for (long long i = 2; i <= 2000000; i++) { if (p[i]) continue; for (long long j = i * i; j <= 2000000; j += i) { p[j] = true; } } long long n; cin >> n; long long a1 = 0; long long a2 = 0; for (long long i = 0; i < (long long)(n); i++) { cin >> a[i]; if (a[i] == 1) a1++; else if (!p[1 + a[i]]) a2 = a[i]; } if (a1 + !!a2 >= 2) { cout << a1 + !!a2 << '\n'; for (long long i = 0; i < (long long)(a1); i++) cout << 1 << " "; if (a2) cout << a2; cout << '\n'; return 0; } for (long long i = 0; i < (long long)(n); i++) { for (long long j = i + 1; j < n; j++) { if (!p[a[i] + a[j]]) { cout << 2 << '\n'; cout << a[i] << " " << a[j] << '\n'; return 0; } } } cout << 1 << '\n'; cout << a[0] << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; a *= 60; a += b; cout << 1440 - a << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long min(long long a, long long b) { return (a < b) ? a : b; } long long max(long long a, long long b) { return (a > b) ? a : b; } long long fp(long long a, long long b) { if (b == 0) return 1; long long x = fp(a, b / 2); x = (x * x) % mod; if (b & 1) x = (x * a) % mod; return x; } const long long N = 1e5 + 5; long long sm1[N] = {0}, sm[N] = {0}; void solve() { long long n, d, m, x; cin >> n >> d >> m; vector<long long> a, b; for (long long i = 1; i <= n; i++) { cin >> x; if (x > m) b.push_back(x); else a.push_back(x); } sort(a.rbegin(), a.rend()); sort(b.rbegin(), b.rend()); sm1[0] = 0; for (long long i = 1; i <= a.size(); i++) { sm1[i] = sm1[i - 1] + a[i - 1]; } sm[0] = 0; for (long long i = 1; i <= b.size(); i++) { sm[i] = sm[i - 1] + b[i - 1]; } long long l = 0, r = b.size(); long long ans = 0; for (long long l = 0; l <= min(b.size(), 1 + (n - 1) / (d + 1)); l++) { long long z; if (l > 1) { z = min(a.size(), n - ((l - 1) * (d + 1) + 1)); } else { z = a.size(); } ans = max(ans, sm[l] + sm1[z]); } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(0)); long long t; t = 1; for (long long i = 1; i <= t; i++) { solve(); } }
10
#include <bits/stdc++.h> using namespace std; int main() { int i = 0, m, n, q; cin >> n >> m; vector<vector<int> > x(n, vector<int>(2)); vector<vector<int> > p(200001); while (i < n) { scanf("%d%d", &x[i][0], &x[i][1]); i++; } i = 1; while (i <= m) { scanf("%d", &q); p[q].push_back(i); i++; } i = 0; vector<vector<int> > e = p; vector<int> s; while (i < n - 1) { if (!p[abs(x[i + 1][0] - x[i][0] + x[i + 1][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[i + 1][0] + x[(i + 2) % n][1] - x[i + 1][1])] .empty()) { s.push_back(p[abs(x[i + 1][0] - x[i][0] + x[i + 1][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[i + 1][0] + x[(i + 2) % n][1] - x[i + 1][1])] .back()); p[abs(x[i + 1][0] - x[i][0] + x[i + 1][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[i + 1][0] + x[(i + 2) % n][1] - x[i + 1][1])] .pop_back(); } else { break; } i += 2; } if (i >= n - 1) { printf("YES\n"); i = 0; while (i < s.size()) { printf("-1 %d ", s[i]); i++; } return 0; } i = 1; s = vector<int>(0); while (i < n) { if (!e[abs(x[(i + 1) % n][0] - x[i][0] + x[(i + 1) % n][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[(i + 1) % n][0] + x[(i + 2) % n][1] - x[(i + 1) % n][1])] .empty()) { s.push_back( e[abs(x[(i + 1) % n][0] - x[i][0] + x[(i + 1) % n][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[(i + 1) % n][0] + x[(i + 2) % n][1] - x[(i + 1) % n][1])] .back()); e[abs(x[(i + 1) % n][0] - x[i][0] + x[(i + 1) % n][1] - x[i][1]) + abs(x[(i + 2) % n][0] - x[(i + 1) % n][0] + x[(i + 2) % n][1] - x[(i + 1) % n][1])] .pop_back(); } else { break; } i += 2; } if (i >= n) { printf("YES\n%d -1 ", s.back()); s.pop_back(); i = 0; while (i < s.size()) { printf("%d -1 ", s[i]); i++; } return 0; } printf("NO"); return 0; }
8
#include <bits/stdc++.h> using namespace std; set<int> s; int n, k; string a; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k >> a; for (int i = 0; i < n - 1; i++) { if (a[i] == '4' && a[i + 1] == '7') s.insert(i); } vector<int> pos; while (!s.empty() && k > 0) { k--; int x = *s.begin(); s.erase(x); pos.push_back(x); if (x % 2 == 0) { a[x] = '4'; a[x + 1] = '4'; if (x + 2 < n && a[x + 2] == '7') s.insert(x + 1); } else { a[x] = '7'; a[x + 1] = '7'; if (x - 1 >= 0 && a[x - 1] == '4') s.insert(x - 1); } if ((int)pos.size() >= 3 && pos[(int)pos.size() - 3] == x) { if (k % 2) { x = pos[(int)pos.size() - 2]; if (x % 2 == 0) { a[x] = '4'; a[x + 1] = '4'; if (x + 2 < n && a[x + 2] == '7') s.insert(x + 1); } else { a[x] = '7'; a[x + 1] = '7'; if (x - 1 >= 0 && a[x - 1] == '4') s.insert(x - 1); } } break; } } cout << a << '\n'; return 0; }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = double; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } const ll MODBASE = 1000000007LL; const int MAXN = 1000010; const int MAXM = 10000010; const int MAXK = 110; const int MAXQ = 200010; int n, nt[MAXM], a[MAXN], res, lab[MAXN]; int ma[MAXM]; vector<ll> b; void sieveBase(int n) { for (int i = (2); i <= (n); i++) { if (nt[i] == 0) { nt[i] = i; for (ll j = (ll)i * i; j <= n; j += i) nt[j] = i; } } } int getRoot(int u) { while (lab[u] > 0) u = lab[u]; return u; } void unionFind(int u, int v) { int x = lab[u] + lab[v]; if (lab[u] < lab[v]) { lab[v] = u; lab[u] = x; } else { lab[u] = v; lab[v] = x; } } void integerFactorization(int p) { b.clear(); while (p > 1) { int h = nt[p]; int w = 1; while (p % h == 0) { p /= h; w *= h; } b.emplace_back((ll)w * w); } } void solve(int pos, ll lef, ll rig, int a) { if (pos >= int((b).size())) { ll g = lef + rig; ll c = g / 2; ll b = max(lef, rig) - c; if (b <= 10000000 && ma[int(b)]) { int u = getRoot(ma[a]); int v = getRoot(ma[int(b)]); if (u != v) unionFind(u, v); } if (c <= 10000000 && ma[int(c)]) { int u = getRoot(ma[a]); int v = getRoot(ma[int(c)]); if (u != v) unionFind(u, v); } return; } lef *= b[pos]; solve(pos + 1, lef, rig, a); lef /= b[pos]; rig *= b[pos]; solve(pos + 1, lef, rig, a); } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); sieveBase(10000000); cin >> n; for (int i = (1); i <= (n); i++) { cin >> a[i]; ma[a[i]] = i; } for (int i = (1); i <= (n); i++) lab[i] = -1; for (int i = (1); i <= (n); i++) if (a[i] == 1) continue; else if (a[i] % 2) { integerFactorization(a[i]); ll lef = b[0]; ll rig = 1; solve(1, lef, rig, a[i]); } else { if (a[i] % 4 == 0) { integerFactorization(a[i]); for (int j = (0); j <= (int((b).size()) - 1); j++) if (b[j] % 2 == 0) { if (j != 0) swap(b[j], b[0]); break; } ll lef = b[0] / 2; ll rig = 2; solve(1, lef, rig, a[i]); } else { continue; } } for (int i = (1); i <= (n); i++) res += (lab[i] < 0); cout << res; return 0; }
17
#include <bits/stdc++.h> using namespace std; deque<int> x; int main() { int n; int i, j, k; cin >> n; for (i = 0; i < n; i++) { scanf("%d", &j); x.push_back(j); } sort(x.begin(), x.end()); int ans = 1; while ((x.size() >= 2)) { if (x[0] == x[1]) ans++; x.pop_front(); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long data[200005][2]; vector<long long> backward[200005]; long long num; long long tot_num = 1; int cycle; int cycle_single; void dfs(int cur_seat, int n, int father) { data[cur_seat][1] = 1; int size_ = (backward[cur_seat]).size(); int i; int want; want = data[cur_seat][0]; if (want != 0) { if (data[want][1] == 0) { dfs(want, n, cur_seat); } else { if (want == father) { if (data[father][0] == cur_seat) { cycle = 1; if (want == cur_seat) cycle_single = 1; } } else { cycle = 1; if (want == cur_seat) cycle_single = 1; } } } for (i = 0; i < size_; i++) { want = backward[cur_seat][i]; if (want != 0) { if (data[want][1] == 0) { dfs(want, n, cur_seat); } else { if (want != father) { cycle = 1; if (want == cur_seat) cycle_single = 1; } else { if (data[cur_seat][0] == father) { cycle = 1; if (want == cur_seat) cycle_single = 1; } } } } } if (data[cur_seat][0] != 0) { num++; } } int main() { int n; scanf("%d", &n); int i; long long want, ind; for (i = 0; i < n; i++) { scanf("%I64d%I64d", &ind, &want); data[ind][0] = want; data[ind][1] = 0; (backward[want]).push_back(ind); } for (i = 1; i <= 2 * n; i++) { if (data[i][1] == 0) { num = 0; cycle = 0; cycle_single = 0; dfs(i, n, -1); if (cycle == 1) { if (cycle_single == 0) num = 2; else num = 1; } else { num++; } tot_num *= num; tot_num %= 1000000007; } } printf("%I64d", tot_num); return 0; }
13
#include <bits/stdc++.h> using namespace std; long long a[305], b[305], c[305]; int main() { int n; pair<long long, long long> p[2]; long long r[2]; for (int i = 0; i < 2; i++) scanf("%lld%lld", &p[i].first, &p[i].second); scanf("%d", &n); int ans = 0; for (int i = 0; i < n; i++) { scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); for (int j = 0; j < 2; j++) r[j] = a[i] * p[j].first + b[i] * p[j].second + c[i]; if ((r[0] < 0 and r[1] > 0) or (r[1] < 0 and r[0] > 0)) ans++; } printf("%d\n", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 100; int a[N], b[N]; vector<int> vec; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); for (int i = 0; i < n; i++) { a[i] = b[n - i - 1]; } while (k > 0) { for (int i = vec.size(); i < n && k > 0; i++) { cout << vec.size() + 1 << " "; for (int i = 0; i < vec.size(); i++) cout << a[vec[i]] << " "; cout << a[i] << endl; k--; } vec.push_back(vec.size()); } }
8
#include <bits/stdc++.h> using namespace std; vector<pair<int, long long> > ans; void solve(long long x, long long y) { while (x != 0 && y != 0) { if (x > y) { ans.push_back(make_pair(0, x / y)); x %= y; } else { ans.push_back(make_pair(1, y / x)); y %= x; } } if (x + y != 1) { printf("Impossible\n"); return; } ans.back().second--; for (auto it : ans) if (it.second != 0) printf("%lld%c", it.second, 'A' + it.first); } int main(int, char **) { long long x, y; scanf("%lld%lld", &x, &y); solve(x, y); return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { vector<pair<int, char> > v(26); for (int i = 0; i < 26; i++) v[i].second = ('a' + i); string s; int n; cin >> s >> n; for (int i = 0; i < s.length(); i++) v[s[i] - 'a'].first++; sort(v.begin(), v.end()); for (int i = 0; i < 26; i++) { if (v[i].first == 0) continue; n -= v[i].first; if (n < 0) break; v[i].first = 0; for (int j = 0; j < s.length(); j++) { if (s[j] == v[i].second) s.erase(j, 1), j--; } } int counter = 0; for (int i = 0; i < 26; i++) if (v[i].first != 0) counter++; cout << counter << endl << s << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int main() { int n, d; while (cin >> n >> d) { double vv = 180.0 / n; int num; double minx = 999999999.0; for (int i = 1; i <= n - 2; i++) { double t = vv * i; if (fabs(t - 1.0 * d) < minx) { minx = fabs(t - 1.0 * d); num = i; } } printf("%d %d %d\n", n, 1, n - num); } }
5
#include <bits/stdc++.h> using namespace std; string s[60000]; bool cmp(const string& a, const string& b) { return (a + b) < (b + a); } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> s[i]; sort(s, s + n, cmp); for (int i = 0; i < n; ++i) cout << s[i]; cout << "\n"; return 0; }
9
#include<bits/stdc++.h> #define fi first #define se second #define lo (o<<1) #define ro (o<<1|1) #define mid (l+r>>1) #define endl '\n' using namespace std; typedef long long ll; typedef vector<int>vi; typedef pair<int, int>pii; const int inf = 0x3f3f3f3f; const ll linf = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 10; const int mod = 1e9 + 7; int n; string l,r; int main() { ios::sync_with_stdio(0),cin.tie(0); // freopen("1.txt","r",stdin); cin>>n>>l>>r; if(l.substr(0,n-1)==r.substr(0,n-1)) { cout<<r; return 0; } unsigned long long a=0,b=0; for(int i=0;i<n;i++) { a=(a*2+(l[i]=='1'))%mod; b=(b*2+(r[i]=='1'))%mod; } b=(b-a+mod+1)%mod; if(l[0]==r[0]) { if(b>=3)r[n-1]='1'; cout<<r; return 0; } for(int i=0;i<n;i++)cout<<1; // bool f[2]{}; // for(int i=1;i<n;i++) // { // if(l[i]=='0')f[0]=1; // if(r[i]=='1')f[1]=1; // } // if(f[0]||f[1]||b>4) // { // // } return 0; }
18
#include <bits/stdc++.h> using namespace std; int n, m, a[1001], x, y, ans; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i < n + 1; i++) cin >> a[i]; for (int i = 0; i < m; i++) { cin >> x >> y; ans += min(a[x], a[y]); } cout << ans; return 0; }
6
#include <bits/stdc++.h> int i, p, n; int main() { scanf("%d", &n); if (n <= 3) printf("%d\n", n <= 2 ? -1 : 210); else { printf("1"); for (p = 50, i = 1; i <= n - 4; ++i) printf("0"), p = p * 10 % 210; printf("%03d\n", p); } }
6
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } inline long long power(long long a, long long n, long long m) { if (n == 0) return 1; long long p = power(a, n / 2, m); p = (p * p) % m; if (n % 2) return (p * a) % m; else return p; } const long long MOD = 1000000007; long long int n, m; int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n >> m; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; arr[i] %= m; } if (n > m) { cout << "YES"; return 0; } long long int dp[n][m]; memset(dp, 0, sizeof(dp)); dp[0][arr[0]] = 1; for (long long int i = 1; i < n; i++) { dp[i][arr[i]] = 1; for (long long int j = 0; j < m; j++) { if (dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; long long int t = arr[i] + j; t %= m; dp[i][t] = dp[i - 1][j]; } } } if (dp[n - 1][0]) { cout << "YES"; } else { cout << "NO"; } return 0; }
11
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18L + 5; const long long siz = 1e6 + 5; void solve() { long long n, k, tmp; cin >> n >> k; map<long long, long long> check; deque<long long> solution; for (int i = 0; i < n; i++) { cin >> tmp; if (check[tmp]) continue; check[tmp]++; long long s = solution.size(); if (s >= k) { check[solution.back()]--; solution.pop_back(); } solution.push_front(tmp); } cout << solution.size() << endl; while (!solution.empty()) { cout << solution.front() << " "; solution.pop_front(); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long t = 1; while (t--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long N = 1000 + 5; long long n, a[N][N]; bool check() { long long val = -1; for (long long i = 0; i < n; ++i) { long long x = 0, y = 0; for (long long j = 0; j < n; ++j) x ^= a[i][j], y ^= a[j][i]; if (val != -1 && x != val) return false; val = x; if (val != -1 && y != val) return false; } return true; } void init() { cin >> n; } void solve() { long long k = 0; for (long long i = 0; i < n / 2; ++i) { for (long long j = 0; j < n / 2; ++j) { a[i][j] = k * 4; a[i + n / 2][j] = k * 4 + 1; a[i + n / 2][j + n / 2] = k * 4 + 2; a[i][j + n / 2] = k * 4 + 3; ++k; } } for (long long i = 0; i < n; ++i) { for (long long j = 0; j < n; ++j) { cout << a[i][j] << (j == n - 1 ? '\n' : ' '); } } } int32_t main() { init(); solve(); }
10
#include <bits/stdc++.h> int minimm(int a, int b) { if (a < b) { return a; } if (b < a) return b; if (a == b) return a; } int find_min(int a, int b, int c) { if (a < b && a < c) { return a; } else if (b < a && b < c) { return b; } else if (c < a && c < b) { return c; } if (a == b && a == c) return a; } int main() { int k2, k3, k5, k6, n256, n32, i, sum = 0; scanf("%d %d %d %d", &k2, &k3, &k5, &k6); int min = find_min(k2, k5, k6); k2 -= min; sum = 256 * min; if (k2 == 0) { printf("%d\n", sum); return 0; } n32 = minimm(k2, k3); sum += 32 * n32; printf("%d\n", sum); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a, t, n, i = 0, j = 0; long long sum = 0, c = 0; int main() { cin >> n; for (j = 0; j < n; j++) { cin >> a; if (a == -1) { c++; if (sum > 0) { c--; sum--; } } else { sum += a; } } cout << c; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool func(char arr[3][3], char brr[3][3]) { if (arr[1][1] == brr[1][1] && arr[1][2] == brr[1][2] && arr[2][2] == brr[2][2]) { return true; } if (arr[1][1] == brr[1][2] && arr[1][2] == brr[2][2] && arr[2][2] == brr[1][1]) { return true; } if (arr[1][1] == brr[2][2] && arr[1][2] == brr[1][1] && arr[2][2] == brr[1][2]) { return true; } return false; } int main() { char arr[3][3], brr[3][3]; cin >> arr[1][1] >> arr[1][2] >> arr[2][1] >> arr[2][2]; cin >> brr[1][1] >> brr[1][2] >> brr[2][1] >> brr[2][2]; if (arr[1][1] == 'X') { swap(arr[1][1], arr[2][1]); } else if (arr[1][2] == 'X') { swap(arr[1][1], arr[1][2]); swap(arr[1][1], arr[2][1]); } else if (arr[2][2] == 'X') { swap(arr[2][1], arr[2][2]); } if (brr[1][1] == 'X') { swap(brr[1][1], brr[2][1]); } else if (brr[1][2] == 'X') { swap(brr[1][1], brr[1][2]); swap(brr[1][1], brr[2][1]); } else if (brr[2][2] == 'X') { swap(brr[2][1], brr[2][2]); } if (func(arr, brr)) { cout << "YES\n"; } else { cout << "NO\n"; } }
4
#include <bits/stdc++.h> using namespace std; int a[100005]; int main(int argc, char** argv) { int n, k; cin >> n >> k; int ans = 0; for (int i = 1; i <= n; i++) cin >> a[i]; while (a[1] < k) { for (int i = 1; i <= n; i++) { if (a[i] != a[i + 1] && a[i] != k) ++a[i]; } ++ans; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long l, r; long long slove(long long x) { long long p = 1, odd = 0, even = 0, flag = 1; while (p <= x) { x -= p; if (flag) odd = (odd + p) % mod; else even = (even + p) % mod; p *= 2; flag ^= 1; } if (flag) odd = (odd + x) % mod; else even = (even + x) % mod; return (odd % mod * odd % mod + (even + 1) % mod * even % mod) % mod; } int main() { cin >> l >> r; cout << (slove(r) - slove(l - 1) + mod) % mod << endl; return 0; }
10
#include <bits/stdc++.h> #define int long long #define ff first #define ss second #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define pii pair<int, int> #define pll pair<ll,ll> #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endl '\n' #define teto(a, b) ((a+b-1)/(b)) using namespace std; // Extra #define forn(i, n) for(int i = 0; i < (int)n; i++) #define forne(i, a, b) for(int i = a; i <= b; i++) #define all(x) x.begin(), x.end() #define dbg(msg, var) cout << msg << " " << var << endl; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // const int MAX = 400010; const ll MOD = (int)1e9 +7; const int INF = 1e9; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ld EPS = 1e-8; struct no{ ll p, s, t, b; // prefix, suffix, total, b no(ll x=0): p(x), s(x), t(x), b(x){} }; struct Segtree{ vector<no> t; int n; Segtree(int n){ this->n = n; t.assign(2*n, no(0)); } no merge(no l, no r){ no ans; ans.p = max(0LL, max(l.p, l.t+r.p)); ans.s = max(0LL, max(r.s, l.s+r.t)); ans.t = l.t+r.t; ans.b = max(max(l.b, r.b), l.s+r.p); return ans; } void build(){ for(int i=n-1; i>0; i--) t[i]=merge(t[i<<1], t[i<<1|1]); } no query(int l, int r){ // idx 0 no a(0), b(0); for(l+=n, r+=n+1; l<r; l>>=1, r>>=1){ if(l&1) a=merge(a, t[l++]); if(r&1) b=merge(t[--r], b); } return merge(a, b); } void update(int p, int value){ for(t[p+=n] = no(value); p >>= 1;) t[p] = merge(t[p<<1], t[p<<1|1]); } }; int res[MAX+2]; Segtree stmaior = Segtree(MAX+2); Segtree stmenor = Segtree(MAX+2); vector<vi> pos(MAX+2); int32_t main() { // sws; int n; cin >> n; vl v(n); forn(i, n) { cin >> v[i]; pos[v[i]].pb(i); stmaior.update(i, 1); stmenor.update(i, -1); } // for(int i = 1; i <= 5; i++) { // cout << "i = " << i << endl; // for(auto j : pos[i]) cout << j << " "; // cout << endl << endl; // } for(int i = 1; i <= 200000; i++) { // igual tem que ser 1 positivo for(auto j : pos[i]) { stmenor.update(j, 1); } // if(i == 4) break; // calcula for(auto j : pos[i]) { // maior auto bl = stmaior.query(0, j); auto br = stmaior.query(j, n-1); ll maior = bl.s + br.p-1; // cout << "maior suf = " << bl.s << " pre = " << br.p << endl; auto sl = stmenor.query(0, j); auto sr = stmenor.query(j, n-1); ll menor = sl.s + sr.p-1; if(menor%2 == 0) menor--; // talvez errado? // cout << "menor suf = " << sl.s << " pre = " << sr.p << endl; res[j] = max(0ll, max(maior/2ll, menor/2ll)); } // atualiza for(auto j : pos[i]) { stmaior.update(j, -1); stmenor.update(j, 1); } } // forn(i, n) { // cout << stmaior.query(i, i).s << " "; // } // cout << endl; // forn(i, n) { // cout << stmenor.query(i, i).s << " "; // } // cout << endl; forn(i, n) { cout << res[i] << " "; } cout << endl; // int overflow, array bounds special cases (n=1?) // do smth instead of nothing and stay organized - divide in subproblems? // WRITE STUFF DOWN DON'T GET STUCK ON ONE APPROACH return 0; }
18
#include<bits/stdc++.h> #define top 1000000007 #define max(a,b) (a>b) ? a:b #define min(a, b) (a<b) ? a:b #define S second #define F first using namespace std; const long long p = 1e8 +1; long long get_min(long long a, long long b, long long c, long long d) { long long x= min(a,b); x =min(x,d); return min(x,c);} int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // cout<<gcd(12, 55); vector <int> v{}; int n; cin>>n; long long pro = 1; v.emplace_back(1); for(int i=2; i<n; ++i) { if(gcd(i, n) == 1){ pro*=i; pro%=n; v.emplace_back(i); } } if(pro!=1){ auto it = find(v.begin(), v.end(), pro); if(it!=v.end()) v.erase(it); } cout<<v.size()<<endl; for(int i=0; i<v.size();++i) cout<<v[i]<<" "; return 0; }
8
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> v(n), x(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n; i++) { cin >> x[i]; } sort(v.begin(), v.end()); sort(x.begin(), x.end()); for (int i = 0; i < n; i++) { cout << v[i]; if (i != 0 || i != n - 1) { cout << " "; } } cout << "\n"; for (int i = 0; i < n; i++) { cout << x[i]; if (i != 0 || i != n - 1) { cout << " "; } } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int test; cin >> test; while (test--) { solve(); } }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long int ull; typedef long double ld; ll gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(int a, int b) { return a * b / gcd(a, b); } int main() { ll n; cin >> n; ll x; ll i; int dp[4] = {0}; for (i = 0; i < n; i++) { cin >> x; dp[3] = max(dp[2], dp[3]) + (x == 2); dp[2] = max(dp[1], dp[2]) + (x == 1); dp[1] = max(dp[0], dp[1]) + (x == 2); dp[0] = dp[0] + (x == 1); } cout << max(dp[2], dp[3]); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1}; const double eps = 1e-9; const int INF = 0x7FFFFFFF; const long long INFLL = 0x7FFFFFFFFFFFFFFFLL; const double pi = acos(-1); struct point_i { int x, y; point_i(int _x = 0, int _y = 0) { x = _x; y = _y; } }; struct point { double x, y; point(double _x = 0, double _y = 0) { x = _x; y = _y; } bool operator<(const point &other) const { if (fabs(x - other.x) < eps) return y < other.y; return x < other.x; } }; struct line_s { point beg, end; line_s() { beg = point(0, 0); end = point(0, 0); } line_s(point a, point b) : beg(a), end(b) {} bool intersect(const line_s &other, point &ans) { double mua, mub; double denom, numera, numerb; denom = (other.end.y - other.beg.y) * (end.x - beg.x) - (other.end.x - other.beg.x) * (end.y - beg.y); numera = (other.end.x - other.beg.x) * (beg.y - other.beg.y) - (other.end.y - other.beg.y) * (beg.x - other.beg.x); numerb = (end.x - beg.x) * (beg.y - other.beg.y) - (end.y - beg.y) * (beg.x - other.beg.x); if (denom == 0 && numera == 0 && numerb == 0) return 0; mua = numera / denom; mub = numerb / denom; if (mua < 0 || mua > 1 || mub < 0 || mub > 1) { ans.x = 0; ans.y = 0; return 0; } ans.x = beg.x + mua * (end.x - beg.x); ans.y = beg.y + mua * (end.y - beg.y); return 1; } }; struct line { double a, b, c, len; line() { a = b = c = 0.0; } line(const point &a_, const point &b_) { if (fabs(a_.x - b_.x) < eps) { a = 1.0; b = 0.0; c = -a_.x; } else { a = -(double)(a_.y - b_.y) / (a_.x - b_.x); b = 1.0; c = -(double)(a_.x * a) - (double)(a_.y * b); } if (fabs(a) < eps) a = fabs(a); if (fabs(b) < eps) b = fabs(b); if (fabs(c) < eps) c = fabs(c); double x = a_.x - b_.x, y = a_.y - b_.y; len = sqrt((x * x) + (y * y)); } bool parallel(const line &other) { return (fabs(a - other.a) < eps && fabs(b - other.b) < eps); } bool same(const line &other) { return (*this).parallel(other) && fabs(c - other.c) < eps; } }; int dp[3002][102][102]; int n, LEN, a, b; const int MOD = 1000000007; vector<pair<int, int> > vc[2]; int main() { scanf("%d%d", &n, &LEN); vc[0].push_back(make_pair(-1, -1)); vc[1].push_back(make_pair(-1, -1)); for (int(i) = (0), _t = (n); i < (_t); ++(i)) { scanf("%d%d", &a, &b); vc[0].push_back(make_pair(a, b)); vc[1].push_back(make_pair(b, a)); } for (int(j) = (0), _t = (100); j <= (_t); ++(j)) dp[0][0][j] = 1; for (int(i) = (1), _t = (LEN); i <= (_t); ++(i)) for (int(j) = (1), _t = (n); j <= (_t); ++(j)) { if (i - (((vc[0][j].first) < (vc[0][j].second)) ? (vc[0][j].first) : (vc[0][j].second)) < 0) continue; for (int(k) = (0), _t = (n); k <= (_t); ++(k)) { if (j == k) continue; if (i - vc[0][j].first < 0 || (i - vc[0][j].first > 0 && k == 0)) ; else { dp[i][j][vc[0][j].second] += dp[i - vc[0][j].first][k][vc[0][j].first]; dp[i][j][vc[0][j].second] %= MOD; } if (vc[0][j] != vc[1][j]) if (i - vc[1][j].first < 0 || (i - vc[1][j].first > 0 && k == 0)) ; else { dp[i][j][vc[1][j].second] += dp[i - vc[1][j].first][k][vc[1][j].first]; dp[i][j][vc[1][j].second] %= MOD; } } } int ans = 0; for (int(i) = (0), _t = (101); i < (_t); ++(i)) for (int(j) = (0), _t = (101); j < (_t); ++(j)) { ans += dp[LEN][i][j]; ans %= MOD; } cout << ans << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; int book[1000100]; int day[1000100]; int que[1000100]; int main() { int n, i; cin >> n; int people = 0; int sum = 0; int a; int flag = 1; int all = 0, count = 1; int begin = 1, end = 1; int j; for (i = 1; i <= n; i++) { cin >> a; if (a > 0) { if (book[a] == 0) { book[a] = 1; people++; all++; que[end++] = a; } else flag = 0; } if (a < 0) { if (book[-a] == 1) { all++; people--; book[-a] = 2; if (people == 0) { day[count++] = all; all = 0; for (j = begin; j < end; j++) { book[que[j]] = 0; } begin = end; } } else flag = 0; } } if (flag && n % 2 == 0 && all == 0) { cout << count - 1 << endl; for (i = 1; i < count; i++) { cout << day[i] << " "; } } else cout << "-1"; }
6
#include <bits/stdc++.h> using namespace std; vector<int> x[100010]; vector<int> y[100010]; bool can[100010]; int ssqrt(int s) { int x = sqrt(s); if (x * x == s) return x; else return -1; } void init() { for (int i = 1; i < 100010; i++) { can[i] = false; for (int a = 0; a * a <= i; a++) { int b = ssqrt(i - a * a); if (b != -1) { can[i] = true; x[i].push_back(a); y[i].push_back(b); } } } } int pp[100010]; int qq; struct Node { int x, y; double ang; } p[100010]; bool cmp(Node a, Node b) { return a.ang < b.ang; } int myabs(int x) { return max(x, -x); } int main() { init(); int n; scanf("%d", &n); qq = 0; for (int i = 1; i < 100010; i++) { if (can[i]) pp[qq++] = i; if (qq == n) break; } int sum = 0; for (int i = 0; i < qq; i++) sum += pp[i]; if (sum & 1) { for (int i = pp[qq - 1] + 1; i < 100010; i++) if (can[i]) { pp[qq] = i; break; } for (int i = qq - 1; i >= 0; i--) if ((pp[i] + pp[qq]) & 1) { swap(pp[i], pp[qq]); break; } } while (1) { sort(pp, pp + qq); reverse(pp, pp + qq); int xx = 0; int yy = 0; for (int i = 0; i < qq; i++) { int len = x[pp[i]].size(); int j = rand() % len; if (myabs(xx + x[pp[i]][j]) < myabs(xx - x[pp[i]][j])) { xx += x[pp[i]][j]; p[i].x = x[pp[i]][j]; } else { xx -= x[pp[i]][j]; p[i].x = -x[pp[i]][j]; } if (myabs(yy + y[pp[i]][j]) < myabs(yy - y[pp[i]][j])) { yy += y[pp[i]][j]; p[i].y = y[pp[i]][j]; } else { yy -= y[pp[i]][j]; p[i].y = -y[pp[i]][j]; } } if (xx == 0 && yy == 0) break; } for (int i = 0; i < qq; i++) p[i].ang = atan2(p[i].y, p[i].x); sort(p, p + qq, cmp); printf("YES\n"); int xx = 0; int yy = 0; for (int i = 0; i < qq; i++) { xx += p[i].x; yy += p[i].y; printf("%d %d\n", xx, yy); } return 0; }
17
#include <bits/stdc++.h> using namespace std; const int N = 5005; const int inf = 1e9 + 7; int pr[] = {2, 3, 5, 7, 11, 13}; int up; int tot[105]; int q[50000005], vis[1000005]; int sz; pair<int, int> srt[1000005]; void dfs(int x, int y) { if (y == sz) { q[x] = 1; return; } dfs(x, y + 1); x *= pr[y]; while (x <= up) { dfs(x, y + 1); x *= pr[y]; } } int ans[N]; int main() { int n; scanf("%d", &n); up = n * n * 2; sz = 6; if (n <= 100) sz = 4; else if (n <= 200) sz = 5; dfs(1, 0); int cnt = 0; for (int i = (1); i <= (up); i++) if (q[i]) { cnt++; srt[cnt].second = i; for (int j = (0); j <= (sz - 1); j++) if (i % pr[j] == 0) { srt[cnt].first ^= (1 << (j - 1)); tot[j]++; } } sort(srt + 1, srt + 1 + cnt); int cur = cnt; for (int i = (1); i <= (cnt); i++) { int flag = 1; for (int j = (0); j <= (sz - 1); j++) if (srt[i].second % pr[j] == 0) if (tot[j] <= (n + 1) / 2) { flag = 0; break; } if (flag && cur > n) { cur--; vis[i] = 1; for (int j = (0); j <= (sz - 1); j++) if (srt[i].second % pr[j] == 0) tot[j]--; } } for (int i = (1); i <= (cnt); i++) if (!vis[i]) printf("%d ", srt[i].second); puts(""); return 0; }
15
#include <bits/stdc++.h> using namespace std; template <class T> inline bool read(T &x) { x = 0; bool f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return 0; f |= ch == '-'; } for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); if (f) x = -x; return 1; } template <class T> void write(T x) { if (x < 0) { putchar('-'); write(-x); return; } if (x > 9) write(x / 10); putchar(48 | x % 10); } template <class T> inline void writeln(T x) { write(x); putchar('\n'); } template <class T> inline void writespace(T x) { write(x); putchar(' '); } const string taskname = ""; long long n, m, len, s; long long ans[400010], c[100010]; long long tot, h[100010], ver[200010], nxt[200010]; bool vis[100010]; inline void add(long long u, long long v) { ver[++tot] = v; nxt[tot] = h[u]; h[u] = tot; } void dfs(long long u, long long fa) { vis[u] = 1; ans[++len] = u; c[u] ^= 1; for (register long long i = h[u]; i; i = nxt[i]) { long long v = ver[i]; if (!vis[v]) { dfs(v, u); ans[++len] = u; c[u] ^= 1; } } if (c[u]) { ans[++len] = fa; ans[++len] = u; c[u] ^= 1; c[fa] ^= 1; } } signed main() { srand(20050210); read(n); read(m); while (m--) { long long u, v; read(u); read(v); add(u, v); add(v, u); } for (register long long i = 1; i <= n; ++i) { read(c[i]); if (c[i] == 1) s = i; } if (s) { dfs(s, 0); for (register long long i = 1; i <= n; ++i) if (!vis[i] && c[i]) { write(-1); return 0; } } if (len > 1 && ans[len - 1] == 0) len -= 3; writeln(len); for (register long long i = 1; i <= len; ++i) writespace(ans[i]); return 0; }
14
#include <bits/stdc++.h> using namespace std; template <typename T> void chmin(T &x, const T &y) { if (x > y) x = y; } template <typename T> void chmax(T &x, const T &y) { if (x < y) x = y; } int read() { char c; while ((c = getchar()) < '-') ; if (c == '-') { int x = (c = getchar()) - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return -x; } int x = c - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return x; } const int N = 50 + 5; int fa[N], du[N]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } pair<int, int> ans[N]; int main() { int n = read(), m = read(); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { int x = read(), y = read(); ++du[x]; ++du[y]; fa[find(x)] = find(y); } for (int i = 1; i <= n; ++i) if (du[i] > 2) { puts("NO"); return 0; } int top = 0; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) if (find(i) != find(j) && du[i] < 2 && du[j] < 2) { ans[++top] = make_pair(i, j); ++du[i]; ++du[j]; fa[find(i)] = find(j); } if (m + top == n - 1) { ++top; for (int i = 1; i <= n; ++i) while (du[i] < 2) { ++du[i]; if (!ans[top].first) ans[top].first = i; else ans[top].second = i; } } for (int i = 1; i <= n; ++i) if (find(i) != find(1)) { puts("NO"); return 0; } puts("YES"); printf("%d\n", top); for (int i = 1; i <= top; ++i) printf("%d %d\n", ans[i].first, ans[i].second); }
15
#include <bits/stdc++.h> using namespace std; int ans; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; if (a[0] == 1) { int x = 0; for (int i = 1; i < n; ++i) { if (a[i] != i + 1) break; x++; } ans = max(ans, x); } if (a[n - 1] == 1000) { int x = 0; for (int i = n - 2; i >= 0; --i) { if (a[i] != 1000 - n + i + 1) break; x++; } ans = max(ans, x); } for (int i = 0; i < n; ++i) { int x = 0; for (int j = i; j < n - 1; ++j) { if (a[j + 1] != a[j] + 1) { i = j; break; } x++; } ans = max(ans, x - 1); } cout << ans; }
5
#include <bits/stdc++.h> const int Max = 1e6 + 100; using namespace std; vector<int> vec[Max]; int dist[Max]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> dist[i]; } for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; vec[u].push_back(v); vec[v].push_back(u); } int mx = -2e9, cnt = 0, tmp = 0; for (int i = 1; i <= n; i++) mx = max(mx, dist[i]); for (int i = 1; i <= n; i++) { if (mx == dist[i]) cnt++; if (mx - 1 == dist[i]) tmp++; } int ans; if (cnt == 1 && tmp == 0) { cout << mx << endl; return 0; } ans = 2e9; for (int i = 1; i <= n; i++) { int cnt1 = 0, tmp1 = 0; if (dist[i] == mx) cnt1++; for (int j = 0; j < vec[i].size(); j++) { int v = vec[i][j]; if (dist[v] == mx) cnt1++; if (dist[v] == mx - 1) tmp1++; } if (cnt1 != cnt) ans = min(ans, mx + 2); if (cnt1 == cnt) { if (tmp1 == tmp) { if (cnt1 == 1) ans = min(ans, mx); else ans = min(ans, mx + 1); } else { ans = min(mx + 1, ans); } } } cout << ans << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; int t, x, p, k, cnt, fac[1009]; long long solve(long long mm) { long long s = 0; for (int i = 1; i < (1 << cnt); ++i) { long long tmp = 1, sign = -1; for (int j = 0; j < cnt; ++j) { if ((i >> j) & 1) { tmp *= fac[j]; sign *= -1; } } s += mm / tmp * sign; } return mm - s; } int main() { scanf("%d", &t); while (t--) { scanf("%d%d%d", &x, &p, &k); cnt = 0; for (int i = 2; i * i <= p; ++i) { if (p % i == 0) { fac[cnt++] = i; while (p % i == 0) p /= i; } } if (p > 1) fac[cnt++] = p; long long kk = solve(x) + k; long long lef = 1, rig = 1e13, ans = 1; while (lef <= rig) { long long mid = (lef + rig) >> 1; if (solve(mid) >= kk) { ans = mid; rig = mid - 1; } else lef = mid + 1; } printf("%lld\n", ans); } return 0; }
14
#include <bits/stdc++.h> using namespace std; long long check(int a, int b, int c, int d, int n, int m) { if (a + c <= m) { if (b <= n and d <= n) { return a * b + c * d; } } if (b + d <= n) { if (a <= m and c <= m) { return a * b + c * d; } } return 0; } int main() { int n, a, b; cin >> n >> a >> b; int x[n]; int y[n]; for (int i = 0; i < n; i++) { cin >> x[i]; cin >> y[i]; } long long ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { ans = max(ans, check(x[i], y[i], x[j], y[j], a, b)); ans = max(ans, check(y[i], x[i], x[j], y[j], a, b)); ans = max(ans, check(x[i], y[i], y[j], x[j], a, b)); ans = max(ans, check(y[i], x[i], y[j], x[j], a, b)); } } } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const string LN = "\n"; const double eps = 1e-9; const double pi = acos(-1.0); const int INF = (int)2e9 + 7; const long long LINF = (long long)9e18 + 7; const long long P = 353251; const long long P1 = 239017; const long long P2 = 239017; const long long MOD = 1e9 + 7; const long long MOD2 = 1e9 + 9; int n; int a[307]; int c[307]; map<int, int> ans[307]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int get(int k, int was) { if (was == 1) return 0; if (k == n) { if (was == 1) return 0; return INF; } if (ans[k][was] != 0) return ans[k][was]; int ansnow = c[k] + get(k + 1, a[k]); if (was != 1) ansnow = min(ansnow, get(k + 1, was)); if (was != -1 && was != 1 && get(k + 1, gcd(was, a[k])) != INF) ansnow = min(ansnow, c[k] + get(k + 1, gcd(was, a[k]))); ans[k][was] = ansnow; return ansnow; } const bool is_testing = 0; int main() { srand('D' + 'E' + 'N' + 'I' + 'S' + 'S' + 'O' + 'N'); if (is_testing) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } else { } cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> c[i]; if (n == 1) { if (a[0] == 1) cout << c[0]; else cout << -1; exit(0); } int aha = get(0, -1); if (aha == INF) cout << -1; else cout << aha; if (is_testing) { cout << LN << 1.0 * clock() / CLOCKS_PER_SEC << LN; } }
11
#include <bits/stdc++.h> using namespace std; int main() { long long int n, c, i; cin >> n >> c; long long int a[n - 1], b[n - 1]; for (i = 0; i < n - 1; i++) { cin >> a[i]; } for (i = 0; i < n - 1; i++) { cin >> b[i]; } b[0] = b[0] + c; for (i = 1; i < n - 1; i++) { a[i] = min(a[i - 1] + a[i], b[i - 1] + a[i]); b[i] = min(b[i - 1] + b[i], a[i - 1] + b[i] + c); } cout << 0 << " "; for (i = 0; i < n - 1; i++) { cout << min(a[i], b[i]) << " "; } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long solve(long long n) { for (long long i = n / 2; i > 1; --i) { if (n % i == 0) { return solve(i) + i; } } return 1; } int main() { long long n; cin >> n; cout << solve(n) + n; }
2
#include <bits/stdc++.h> using namespace std; int dcmp(double a, double b) { return fabs(a - b) <= 0.0000000001 ? 0 : (a > b) ? 1 : -1; } long long fun(long long lev) { return (3 * ((lev) * (lev + 1) / 2) - lev); } int main() { long long n, i, s = 0, ans; cin >> n; for (i = 1;; i++) { ans = fun(i); if (ans > n) break; if ((i + n) % 3 == 0) s++; } cout << s << endl; }
9
#include <bits/stdc++.h> using namespace std; int psum(long long v) { long long ans = (v * (v + 1000000007 - 1)) % 1000000007; ans = (ans * 500000004LL) % 1000000007; return (int)ans; } pair<long long, long long> getDP(int x, int y, int k) { if (x == 0 || y == 0 || k == 0) return make_pair(0, 0); if (x == 1) { int low = min(y, k); return make_pair(low, psum(low)); } if (y == 1) { int low = min(x, k); return make_pair(low, psum(low)); } if (k == 1) { int low = min(x, y); return make_pair(low, low); } pair<long long, long long> v1 = getDP((x + 1) / 2, (y + 1) / 2, (k + 1) / 2); pair<long long, long long> v2 = getDP(x / 2, (y + 1) / 2, k / 2); pair<long long, long long> v3 = getDP((x + 1) / 2, y / 2, k / 2); pair<long long, long long> v4 = getDP(x / 2, y / 2, (k + 1) / 2); int num = (((long long)v1.first) + ((long long)v2.first) + ((long long)v3.first) + ((long long)v4.first)) % 1000000007; int ans = (2LL * v1.second + 1000000007 - v1.first) % 1000000007; ans = (ans + 2LL * v4.second + 1000000007 - v4.first) % 1000000007; ans = (ans + 2LL * v2.second + 2LL * v3.second) % 1000000007; return make_pair(num, ans); } int mask[31]; pair<long long, long long> getWays(int x, int y, int k) { if (x == 0 || y == 0 || k == 0) return make_pair(0, 0); int totNum = 0; int totSum = 0; for (int t = 0; t < 2; t++) { for (int i = 30; i >= 0; i--) if (x & (1 << i)) { x ^= (1 << i); for (int j = i - 1; j >= 0; j--) if (y & (1 << j)) { y ^= (1 << j); if (((x ^ y) >> i) < (k >> i)) { long long cnum = ((1LL << i) * (1LL << j)) % 1000000007; totNum = (totNum + cnum) % 1000000007; totSum = (totSum + (1LL << j) * psum(1LL << i)) % 1000000007; totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007; } else if (((x ^ y) >> i) == (k >> i)) { long long lowk = (k & ((1 << i) - 1)); long long cnum = (lowk * (1LL << j)) % 1000000007; totNum = (totNum + cnum) % 1000000007; totSum = (totSum + (1LL << j) * psum(lowk)) % 1000000007; totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007; } y ^= (1 << j); } x ^= (1 << i); } swap(x, y); } for (int i = 30; i >= 0; i--) if (x & (1 << i)) if (y & (1 << i)) { x ^= (1 << i); y ^= (1 << i); if (((x ^ y) >> i) < (k >> i)) { long long cnum = ((1LL << i) * (1LL << i)) % 1000000007; totNum = (totNum + cnum) % 1000000007; totSum = (totSum + (1LL << i) * psum(1LL << i)) % 1000000007; totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007; } else if (((x ^ y) >> i) == (k >> i)) { long long lowk = (k & ((1 << i) - 1)); long long cnum = (lowk * (1LL << i)) % 1000000007; totNum = (totNum + cnum) % 1000000007; totSum = (totSum + (1LL << i) * psum(lowk)) % 1000000007; totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007; } x ^= (1 << i); y ^= (1 << i); } return make_pair(totNum, (totNum + totSum) % 1000000007); } int main() { int Q, x1, y1, x2, y2, k; cin >> Q; for (int i = 0; i < Q; i++) { cin >> x1 >> y1 >> x2 >> y2 >> k; int ans = getWays(x2, y2, k).second; ans = (ans + 1000000007 - getWays(x2, y1 - 1, k).second) % 1000000007; ans = (ans + 1000000007 - getWays(x1 - 1, y2, k).second) % 1000000007; ans = (ans + getWays(x1 - 1, y1 - 1, k).second) % 1000000007; cout << ans << '\n'; } }
18
#include <bits/stdc++.h> using namespace std; int delta[310000]; int orig[310000]; int main() { long double sum = 0; long double ans; int num = 1; int n; scanf("%d", &n); int cmd, a, x; for (int i = 0; i < n; ++i) { scanf("%d", &cmd); if (cmd == 1) { scanf("%d", &a); scanf("%d", &x); if (a > num) a = num; delta[a - 1] += x; sum += a * x; } if (cmd == 2) { scanf("%d", &x); delta[num] = 0; orig[num] = x; sum += x; ++num; } if (cmd == 3) { --num; delta[num - 1] += delta[num]; orig[num] += delta[num]; sum -= orig[num]; } ans = (long double)num; ans = sum / ans; double da = (double)ans; cout << fixed << setprecision(10) << da; cout << "\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> C(n), R(n); char c[200]; for (int i = 0; i < n; i++) { scanf("%s", c); for (int j = 0; j < n; j++) if (c[j] == 'C') { C[j]++; R[i]++; } } int ans = 0; for (int i = 0; i < n; i++) { ans += (C[i] * (C[i] - 1)) / 2 + (R[i] * (R[i] - 1)) / 2; } printf("%d", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e3 + 3; int n, r; int x[MAX_N]; double y[MAX_N]; int main() { scanf("%d%d", &n, &r); for (int i = (1); i <= (n); i++) scanf("%d", &x[i]); for (int i = (1); i <= (n); i++) { y[i] = r; for (int j = (1); j <= (i - 1); j++) { if (x[j] + r < x[i] - r || x[j] - r > x[i] + r) continue; y[i] = max( y[i], y[j] + sqrt((2 * r) * (2 * r) - (x[i] - x[j]) * (x[i] - x[j]))); } } for (int i = (1); i <= (n); i++) printf("%.9lf ", y[i]); }
7
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, a, b; cin >> n; n = n / 2; while (n--) { cin >> a >> b; cout << -b << " " << a << " "; } cout << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; inline void read(register int *n) { register char c; *n = 0; do { c = getchar(); } while (c < '0' || c > '9'); do { *n = c - '0' + *n * 10; c = getchar(); } while (c >= '0' && c <= '9'); } vector<string> rot(vector<string> V) { vector<string> res(((int)(V[0]).size()), ""); for (register int j = (0); j < (int)(((int)(V[0]).size())); ++j) for (int i = ((int)(V).size()) - 1; i >= 0; i--) res[j] += V[i][j]; return res; } int main(int argv, char **argc) { int n, m; cin >> n >> m; vector<string> V(n); for (register int i = (0); i < (int)(n); ++i) cin >> V[i]; int tot = 0; pair<int, int> best(100, 100); for (register int i = (1); i < (int)(n + 1); ++i) if (n % i == 0) for (register int j = (1); j < (int)(m + 1); ++j) if (m % j == 0) { set<vector<string> > st; for (register int r = (0); r < (int)(n / i); ++r) for (register int c = (0); c < (int)(m / j); ++c) { vector<string> dest; for (register int ii = (r * i); ii < (int)(r * i + i); ++ii) { string tmp = ""; for (register int jj = (c * j); jj < (int)(c * j + j); ++jj) tmp += V[ii][jj]; dest.push_back(tmp); } set<vector<string> > hmm; hmm.insert(dest); dest = rot(dest); hmm.insert(dest); dest = rot(dest); hmm.insert(dest); dest = rot(dest); hmm.insert(dest); st.insert(*hmm.begin()); } if (((int)(st).size()) != (n * m) / (i * j)) continue; tot++; if (i * j < best.first * best.second) { best.first = i; best.second = j; } } cout << tot << endl << best.first << " " << best.second << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const int MAXN = 2e5 + 5; int n; vector<int> G[MAXN]; long long fact[MAXN]; long long f(int v, int p = -1) { long long ret = 1; for (int u : G[v]) { if (u == p) continue; ret = ret * f(u, v) % MOD; } ret = ret * fact[G[v].size()] % MOD; return ret; } int main() { scanf("%d", &n); fact[0] = 1; for (int i = 1; i < (int)n; i++) fact[i] = fact[i - 1] * i % MOD; for (int i = 0; i < (int)n - 1; i++) { int v, u; scanf("%d %d", &v, &u); v--; u--; G[v].push_back(u); G[u].push_back(v); } printf("%lld\n", f(0) * n % MOD); }
11
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long mod = 1000000009; long long mod2 = 1000000009; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool is_vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 1; return 0; } double getDistance(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } long long extended_euclidean(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long g = extended_euclidean(b, a % b, y, x); y -= (a / b) * x; return g; } long long power(long long base, long long p) { if (p == 1) return base; if (!p) return 1ll; long long ret = power(base, p / 2); ret *= ret; ret %= mod; if (p & 1) ret *= base; return ret % mod; } pair<long long, long long> vec(pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> ret = pair<long long, long long>(a.first - b.first, a.second - b.second); return ret; } long long getArea(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { pair<long long, long long> x = vec(a, b); pair<long long, long long> y = vec(c, b); return abs(x.first * y.second - x.second * y.first); } bool intersect(const complex<double> &a, const complex<double> &b, const complex<double> &p, const complex<double> &q, complex<double> &ret) { double d1 = ((conj(p - a) * (b - a)).imag()); double d2 = ((conj(q - a) * (b - a)).imag()); ret = (d1 * q - d2 * p) / (d1 - d2); return fabs(d1 - d2) > 1e-9; } double getDistance(complex<double> a, complex<double> b) { return sqrt((a.real() - b.real()) * (a.real() - b.real()) + (a.imag() - b.imag()) * (a.imag() - b.imag())); } vector<int> start, fini; long long acc[N * 3]; long long l[N * 3]; long long shiftlft(long long k, long long n) { if (k <= 0) return n; if (l[k] != -1) return (l[k] * n) % mod; long long shift = power(53ll, k); long long d = power(shift, mod - 2); l[k] = d; n *= d; return n % mod; } bool is[N * 2]; int main() { int n; scanf("%d", &n); memset(l, -1, sizeof(l)); int sum = 0, len = 0; long long cur = 1; for (int i = (0); i <= (n - 1); ++i) { string s; cin >> s; if (i) s = " " + s; for (int j = (0); j <= ((int)s.length() - 1); ++j) { int idx = (s[j] - 'a') + 1; if (idx < 0) idx = 28; acc[len + j] = (idx)*cur; if (len + j) acc[len + j] += acc[len + j - 1]; acc[len + j] %= mod; cur *= 53ll; cur %= mod; } sum += (int)s.length(); start.push_back(len + (i > 0)); len += s.length(); fini.push_back(len - 1); is[len - 1] = 1; } int mn = sum; for (int i = (0); i <= ((int)start.size() - 1); ++i) { int words = 0; for (int j = (0); j <= ((int)fini.size() - 1); ++j) { if (fini[j] < start[i]) continue; words++; long long cur = acc[fini[j]]; if (start[i]) { cur -= acc[start[i] - 1]; cur += mod; cur %= mod; } cur = shiftlft(start[i], cur); int len = fini[j] - start[i] + 1, prv = fini[j], kam = 1; for (int k = (i + 1); k <= ((int)start.size() - 1); ++k) { if (start[k] <= prv) continue; if (start[k] + len - 1 >= sum) continue; if (!is[start[k] + len - 1]) continue; long long val = acc[start[k] + len - 1] - acc[start[k] - 1] + mod; val %= mod; val = shiftlft(start[k], val); if (cur == val) { kam++; prv = start[k] + len - 1; } } if (kam > 1) { mn = min(mn, sum - len * kam + words * kam); } } } cout << mn; return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { int n; string tempS; cin >> n; bool purple = 1, green = 1, blue = 1, orange = 1, red = 1, yellow = 1; for (int i = 0; i < n; i++) { cin >> tempS; if (tempS == "purple") purple = 0; if (tempS == "green") green = 0; if (tempS == "blue") blue = 0; if (tempS == "orange") orange = 0; if (tempS == "red") red = 0; if (tempS == "yellow") yellow = 0; } cout << 6 - n << endl; if (purple) cout << "Power" << endl; if (green) cout << "Time" << endl; if (blue) cout << "Space" << endl; if (orange) cout << "Soul" << endl; if (red) cout << "Reality" << endl; if (yellow) cout << "Mind" << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 10; vector<int> g[maxn]; bool vis[maxn]; int n, m, q; void add(int x, int y) { g[x].push_back(y); } void dfs(int x) { int si = g[x].size(); vis[x] = true; for (int i = 0; i < si; i++) { int to = g[x][i]; if (vis[to]) continue; dfs(to); } } void work() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < q; i++) { int r, c; scanf("%d%d", &r, &c); add(r - 1, c - 1 + n); add(c - 1 + n, r - 1); } long long ans = 0; for (int i = 0; i < n + m; i++) { if (!vis[i]) { dfs(i); ans++; } } printf("%lld\n", ans - 1); } int main() { work(); return 0; }
11
#include <bits/stdc++.h> char A[13][5] = {{"C"}, {"C#"}, {"D"}, {"D#"}, {"E"}, {"F"}, {"F#"}, {"G"}, {"G#"}, {"A"}, {"B"}, {"H"}}; int aa, bb, cc; void find(char a[], char b[], char c[]) { int i; for (i = 0; i < 12; i++) { if (strcmp(A[i], a) == 0) aa = i + 1; if (strcmp(A[i], b) == 0) bb = i + 1; if (strcmp(A[i], c) == 0) cc = i + 1; } } int judge(int n, int m, int k) { int num1 = m - n, num2 = k - m, num3 = k - n; if ((num1 == 4 || m + 12 - n == 4) && (num2 == 3 || k + 12 - m == 3) && (num3 == 7 || k + 12 - n == 7)) return 1; if ((num1 == 3 || m + 12 - n == 3) && (num2 == 4 || k + 12 - m == 4) && (num3 == 7 || k + 12 - n == 7)) return 2; return 0; } int main() { char a[5], b[5], c[5]; int ans, n1, n2, n3, n4, n5, n6; while (scanf("%s%s%s", a, b, c) != EOF) { find(a, b, c); n1 = judge(aa, bb, cc); n2 = judge(aa, cc, bb); n3 = judge(cc, aa, bb); n4 = judge(cc, bb, aa); n5 = judge(bb, aa, cc); n6 = judge(bb, cc, aa); if (n1 == 1 || n2 == 1 || n3 == 1 || n4 == 1 || n5 == 1 || n6 == 1) printf("major\n"); else if (n1 == 2 || n2 == 2 || n3 == 2 || n4 == 2 || n5 == 2 || n6 == 2) printf("minor\n"); else printf("strange\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct tri { long long a, b, c; }; const long long N = 1e3 + 10; const long long inf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3f; const long long mod = 1e9 + 7; long long t[3], n, c; priority_queue<long long, vector<long long>, greater<long long>> q[3]; int32_t main() { ios::sync_with_stdio(0), cin.tie(0); for (long long i = 0; i < 3; i++) { long long a; cin >> a; a = min(a, 100000ll); while (a--) q[i].push(0); } cin >> t[0] >> t[1] >> t[2]; cin >> n; long long ans = 0; for (long long i = 0; i < n; i++) { long long c; cin >> c; long long a = c; for (long long j = 0; j < 3; j++) { long long b = q[j].top(); q[j].pop(); c = max(c, b) + t[j]; q[j].push(c); } ans = max(ans, c - a); } cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; cout << (a + b + c) / 2 << endl; } }
0
#include <bits/stdc++.h> using namespace std; void ga(int N, int *A) { for (int i(0); i < N; i++) scanf("%d", A + i); } char s[51][51]; int N, dp[51][51][51][51]; int dyn(int x, int y, int X, int Y) { if (x > X || y > Y || x >= N || y >= N || X < 0 || Y < 0) return 0; if (x == X && y == Y && s[x][y] ^ 35) return 0; int &v = dp[x][y][X][Y], I = max(X - x, Y - y); if (~v) return v; v = I + 1; for (int k(x); k < X; k++) v = min(v, dyn(x, y, k, Y) + dyn(k + 1, y, X, Y)); for (int k(y); k < Y; k++) v = min(v, dyn(x, y, X, k) + dyn(x, k + 1, X, Y)); return v; } int main(void) { scanf("%d", &N), (memset(dp, -1, sizeof(dp))); for (int i(0); i < N; i++) scanf("%s", s[i]); printf("%d\n", dyn(0, 0, N - 1, N - 1)); return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int rank; cin >> rank; vector<int> counts(rank + 2); for (int i = 0; i < n; i++) { int r; cin >> r; counts[r]++; } vector<int> changes(rank + 2); int sessions = 0; while (true) { if (counts[rank] == n) { break; } for (int i = 1; i < rank; i++) { if (counts[i] > 0) { changes[i + 1]++; changes[i]--; } } for (int i = 1; i <= rank; i++) { counts[i] += changes[i]; changes[i] = 0; } sessions++; } cout << sessions; }
4
#include <bits/stdc++.h> using namespace std; int n; long long r1 = 0, r2 = 0, ans = 2e18; struct point { long long x, y; } a, b, p[2005]; long long dis(point a, point b) { long long x = a.x - b.x; long long y = a.y - b.y; return x * x + y * y; } int main() { scanf("%d%I64d%I64d%I64d%I64d", &n, &a.x, &a.y, &b.x, &b.y); for (int i = 1; i <= n; i++) scanf("%I64d%I64d", &p[i].x, &p[i].y); p[0] = a; for (int i = 0; i <= n; i++) { r1 = dis(a, p[i]); r2 = 0; for (int j = 1; j <= n; j++) if (dis(p[j], a) > r1) r2 = max(r2, dis(p[j], b)); ans = min(ans, r1 + r2); } printf("%I64d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n; int a[111111]; int b[111111]; map<int, int> s; int dist[111111]; int pos[111111]; int main(void) { istream_iterator<int> in(cin); n = *in++; for (int i = 1; i <= n; i++) a[i] = *in++; for (int i = 1; i <= n; i++) b[i] = *in++; for (int i = 1; i <= n; i++) pos[b[i]] = i; for (int i = 1; i <= n; i++) { dist[a[i]] = pos[a[i]] - i; if (s.find(dist[a[i]]) == s.end()) { s.insert(make_pair(dist[a[i]], 1)); } else { s[dist[a[i]]]++; } } for (int i = 0; i < n; i++) { int ans = 1 << 29; map<int, int>::iterator j = s.lower_bound(i); if (j == s.end()) { j--; ans = min(ans, abs(j->first - i)); } else { ans = min(ans, abs(j->first - i)); if (j != s.begin()) { j--; ans = min(ans, abs(j->first - i)); } } cout << ans << "\n"; if (s[dist[b[i + 1]]] == 1) s.erase(dist[b[i + 1]]); else s[dist[b[i + 1]]]--; dist[b[i + 1]] += n; s[dist[b[i + 1]]]++; } return 0; }
13
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const long long p = 1000000007; int main() { long long n; scanf("%lld", &n); ; long long Test = 2 * n; stack<long long> op; stack<long long> nb; while (Test--) { char c; cin >> c; if (c == '+') op.push(1); else { long long temp; cin >> temp; op.push(0); nb.push(temp); } } stack<long long> ans; priority_queue<long long, vector<long long>, greater<long long> > que; while (!op.empty()) { long long nowop = op.top(); op.pop(); if (nowop == 1) { if (!que.empty()) { ans.push(que.top()); que.pop(); } else { cout << "NO\n"; return 0; } } else { long long nownb = nb.top(); nb.pop(); if (que.empty() || nownb <= que.top()) { que.push(nownb); } else { cout << "NO\n"; return 0; } } } cout << "YES\n"; while (!ans.empty()) { cout << ans.top() << ' '; ans.pop(); } cout << '\n'; }
9
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { string str; cin>>str; printf("%d\n",str.length()); } }
0
#include <bits/stdc++.h> using namespace std; int n, m, tam; int get(int i, int j) { int pos = i + j + 1; if (pos <= tam / 2) return pos; return tam - pos + 1; } void solvetask() { cin >> n >> m; tam = n + m - 1; int a[n][m]; vector<int> ida(n + m), volta(n + m), cnt(n + m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; int pos = get(i, j); if ((tam & 1) && pos == (tam + 1) / 2) continue; if (i + j + 1 <= tam / 2) { ida[pos] += a[i][j]; cnt[pos]++; } else { volta[pos] += a[i][j]; cnt[pos]++; } } } int ans = 0; for (int i = 1; i <= tam / 2; i++) { ans += min(ida[i] + volta[i], cnt[i] - ida[i] - volta[i]); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; while (t--) solvetask(); }
7
#include <bits/stdc++.h> #include <stdio.h> using namespace std; #define all(v) v.begin(), v.end() #define eb emplace_back #define ll long long void solve() { int n; cin >> n; string s; cin >> s; vector<ll> pref(n, 0); vector<ll> cnt(n, 0); for(int i = 0; i < n; i += 1) { ll w = 0, f = 0; if (i > 0) { w = pref[i - 1]; f = cnt[i - 1]; } if (s[i] == '*') { pref[i] = w + i + 1; cnt[i] = f + 1; } else { pref[i] = w; cnt[i] = f; } } auto get = [&] (int l, int r) { if (l > r) return 0ll; if (l == 0) { return pref[r]; } return pref[r] - pref[l - 1]; }; auto get_cnt = [&] (int l, int r) { if (l > r) return 0ll; if (l == 0) { return cnt[r]; } return cnt[r] - cnt[l - 1]; }; auto f = [] (int x) { return x * 1ll * (x + 1) / 2; }; auto calc = [&] (int x) { ll before = get_cnt(0, x - 1); ll after = get_cnt(x + 1, n - 1); ll before_sum = get(0, x - 1); ll after_sum = get(x + 1, n - 1); ll res = before * 1ll * (x + 1) - before_sum - f(before); res += after_sum - after * 1ll * (x + 1) - f(after); if (s[x] != '*') { res += min(before, after); } return res; }; ll ans = 1e18; for(int i = 0; i < n; i += 1) { ans = min(ans, calc(i)); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("taskA.in", "r", stdin); // freopen("taskA.out", "w", stdout); int t; cin >> t; while(t--) { solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; inline int input() { char ch = getchar(); while (ch < '0' || '9' < ch) ch = getchar(); int x = 0; do { x = x * 10 + ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); return x; } inline void output(long long x) { static int st[20], top; top = 0; while (x) st[top++] = x % 10, x /= 10; for (; top; --top) putchar(st[top - 1] + '0'); } int n, op[150010]; int pre[150010], suc[150010]; long long ans[150010]; long long sum; int tot[150010], ta[150010]; inline void insert(int x, int c) { int a = 0; for (register int i = x; i; i -= ((i) & -(i))) a += ta[i]; sum += (long long)a * c; for (; x <= n; x += ((x) & -(x))) tot[x] += c; } inline void add(int st) { for (register int x = st; x <= n; x += ((x) & -(x))) ta[x]++; for (register int x = n, y = st - 1; x != y;) if (x > y) sum += tot[x], x -= ((x) & -(x)); else sum -= tot[y], y -= ((y) & -(y)); } int fir[150010 * 4], nf[150010 * 4], sec[150010 * 4]; inline void upd(int k) { int l = k << 1, r = k << 1 | 1; if (fir[l] > fir[r]) { fir[k] = fir[l], nf[k] = nf[l]; sec[k] = ((sec[l]) > (fir[r]) ? (sec[l]) : (fir[r])); } else if (fir[l] < fir[r]) { fir[k] = fir[r], nf[k] = nf[r]; sec[k] = ((fir[l]) > (sec[r]) ? (fir[l]) : (sec[r])); } else { fir[k] = fir[l], nf[k] = nf[l] + nf[r]; sec[k] = ((sec[l]) > (sec[r]) ? (sec[l]) : (sec[r])); } } inline void pd(int k) { fir[k] < fir[k << 1] ? fir[k << 1] = fir[k] : 0; fir[k] < fir[k << 1 | 1] ? fir[k << 1 | 1] = fir[k] : 0; } int st, en, c; void find(int k, int l, int r) { if (fir[k] <= c || !nf[k]) return; if (c > sec[k]) { insert(fir[k], -nf[k]); insert(fir[k] = c, nf[k]); return; } if (l == r) return; pd(k); int mid = l + r >> 1; find(k << 1, l, mid); find(k << 1 | 1, mid + 1, r); upd(k); } void getmin(int k, int l, int r) { if (fir[k] <= c || !nf[k]) return; if (st <= l && r <= en) { find(k, l, r); return; } pd(k); int mid = l + r >> 1; if (st <= mid) getmin(k << 1, l, mid); if (mid < en) getmin(k << 1 | 1, mid + 1, r); upd(k); } void se(int k, int l, int r, int x, int c) { if (l == r) { if (nf[k]) insert(fir[k], -1); else insert(x, -1); insert(fir[k] = c, nf[k] = 1); return; } pd(k); int mid = l + r >> 1; if (x <= mid) se(k << 1, l, mid, x, c); else se(k << 1 | 1, mid + 1, r, x, c); upd(k); } inline void solve() { memset(tot, 0, sizeof(tot)), memset(ta, 0, sizeof(ta)); memset(fir, 0, sizeof(fir)), memset(nf, 0, sizeof(nf)), memset(sec, 0, sizeof(sec)); sum = 0; for (int i = 1; i <= n; ++i) pre[i] = i - 1, suc[i] = i + 1; for (int i = n; i >= 1; --i) { int x = op[i]; suc[pre[x]] = suc[x]; pre[suc[x]] = pre[x]; } int head = n + 1, tail = 0; for (int i = 1; i <= n; ++i) { int x = op[i]; head = min(head, x); tail = ((tail) > (x) ? (tail) : (x)); if (head <= pre[x]) st = head, en = pre[x], c = pre[x], getmin(1, 1, n); se(1, 1, n, head, tail); if (suc[x] <= n) se(1, 1, n, suc[x], tail); add(x); ans[i] += sum; } } int main() { n = input(); for (int i = 1; i <= n; ++i) op[input()] = i; solve(); for (int i = 1; i <= n; ++i) op[i] = n - op[i] + 1; solve(); for (int i = 1; i <= n; ++i) output(ans[i] + 1), putchar('\n'); return 0; }
25
#include <bits/stdc++.h> using std::cin; using std::cout; using std::deque; using std::endl; using std::fill_n; using std::max; using std::memset; using std::min; int n; const int maxn = 100; int len; int cnt[26]; char buf[101]; struct edge { int t, u, c; edge* next; edge* pair; }; int pn; const int maxpn = maxn + 26 + 2; edge* e[maxpn]; int d[maxpn]; bool v[maxpn]; int s, t; int dist, cost; int flow; void add_edge(int u, int v, int lmt, int cst) { edge* tmp = new edge; tmp->t = v; tmp->u = lmt; tmp->c = cst; tmp->next = e[u]; e[u] = tmp; tmp->pair = new edge; tmp->pair->t = u; tmp->pair->u = 0; tmp->pair->c = -cst; tmp->pair->next = e[v]; e[v] = tmp->pair; tmp->pair->pair = tmp; } bool modlabel() { deque<int> q; fill_n(d, pn, INT_MAX); memset(v, 0, sizeof(v)); q.push_back(s); d[s] = 0; v[s] = true; while (!q.empty()) { int p = q.front(); q.pop_front(); v[p] = false; for (edge* i = e[p]; i; i = i->next) if (i->u && d[p] + i->c < d[i->t]) { d[i->t] = d[p] + i->c; if (!v[i->t]) { if (q.empty() || d[i->t] <= q.front()) q.push_front(i->t); else q.push_back(i->t); v[i->t] = true; } } } for (int i = 0; i != pn; ++i) for (edge* j = e[i]; j; j = j->next) j->c += d[i] - d[j->t]; dist += d[t]; return d[t] != INT_MAX; } int aug(int p, int m) { if (p == t) { cost += dist * m; return m; } int l = m; v[p] = true; for (edge* i = e[p]; i; i = i->next) if (i->u && !i->c && !v[i->t]) { int f = aug(i->t, min(i->u, l)); i->u -= f; i->pair->u += f; l -= f; if (!l) return m; } return m - l; } void init() { cin >> buf >> n; pn = n + 26 + 2; s = 0; t = pn - 1; memset(cnt, 0, sizeof(cnt)); for (char* i = buf; *i; ++i) ++cnt[*i - 'a'], ++len; for (int i = 0; i != 26; ++i) add_edge(i + n + 1, t, cnt[i], 0); for (int i = 0; i != n; ++i) { int a; cin >> buf >> a; add_edge(s, i + 1, a, 0); memset(cnt, 0, sizeof(cnt)); for (char* j = buf; *j; ++j) ++cnt[*j - 'a']; for (int j = 0; j != 26; ++j) add_edge(i + 1, j + n + 1, cnt[j], i + 1); } } int main() { init(); while (modlabel()) for (;;) { memset(v, 0, sizeof(v)); int f = aug(s, INT_MAX); if (!f) break; flow += f; } if (flow == len) cout << cost << endl; else cout << -1 << endl; }
12
#include <bits/stdc++.h> using namespace std; const long long int N = 300009; const long long int mod = 998244353; long long int n, k; long long int a[N], sum = 0; void pre() {} int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); ; pre(); cin >> n >> k; vector<long long int> pos; for (long long int i = 0; i < (n); ++i) { cin >> a[i]; if (a[i] >= (n - k + 1)) { pos.push_back(i); sum += a[i]; } } long long int ans = 1; for (long long int i = 1; i < ((long long int)((pos).size())); ++i) { ans *= (pos[i] - pos[i - 1]); ans %= mod; } cout << sum << " " << ans << "\n"; return 0; }
5