solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const long long MAXN = 511111; long long m; vector<vector<pair<long long, pair<long long, long long> > > > qry; vector<vector<pair<long long, long long> > > g; vector<long long> leafs, idx; vector<long long> d, ans; vector<pair<long long, long long> > border; long long curt; void dfs1(long long v, long long p, long long h) { if (v != 0 && g[v].size() == 1) { border[v] = {curt, curt}; leafs.push_back(curt); d.push_back(h); curt++; return; } curt++; for (long long i = 0; i < g[v].size(); i++) { long long to = g[v][i].first; if (to == p) continue; dfs1(to, v, h + g[v][i].second); border[v] = {min(border[v].first, border[to].first), max(border[v].second, border[to].second)}; } } long long tree[4 * MAXN], add[4 * MAXN]; void build(long long tl, long long tr, long long v) { if (tl == tr) { tree[v] = d[tl]; return; } long long mid = (tl + tr) >> 1; build(tl, mid, v << 1); build(mid + 1, tr, v << 1 | 1); tree[v] = min(tree[v << 1], tree[v << 1 | 1]); } void upd(long long tl, long long tr, long long v, long long l, long long r, long long x) { if (l > tr || r < tl) return; if (l <= tl && tr <= r) { add[v] += x; return; } long long mid = (tl + tr) >> 1; upd(tl, mid, v << 1, l, r, x); upd(mid + 1, tr, v << 1 | 1, l, r, x); tree[v] = min(tree[v << 1] + add[v << 1], tree[v << 1 | 1] + add[v << 1 | 1]); } long long get(long long tl, long long tr, long long v, long long l, long long r) { if (l > tr || r < tl) return LLONG_MAX; if (l <= tl && tr <= r) return (tree[v] + add[v]); long long mid = (tl + tr) >> 1; return (min(get(tl, mid, v << 1, l, r), get(mid + 1, tr, v << 1 | 1, l, r)) + add[v]); } void dfs2(long long v, long long p) { for (long long i = 0; i < qry[v].size(); i++) { long long num = qry[v][i].first, l = qry[v][i].second.first, r = qry[v][i].second.second; long long L = lower_bound(leafs.begin(), leafs.end(), l) - leafs.begin(); long long R = lower_bound(leafs.begin(), leafs.end(), r) - leafs.begin(); if (R == leafs.size() || leafs[R] > r) R--; ans[num] = get(0, m - 1, 1, L, R); } for (long long i = 0; i < g[v].size(); i++) { long long to = g[v][i].first; if (to == p) continue; upd(0, m - 1, 1, 0, m - 1, g[v][i].second); upd(0, m - 1, 1, idx[border[to].first], idx[border[to].second], -2 * g[v][i].second); dfs2(to, v); upd(0, m - 1, 1, 0, m - 1, -g[v][i].second); upd(0, m - 1, 1, idx[border[to].first], idx[border[to].second], 2 * g[v][i].second); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, q; cin >> n >> q; g.resize(n), qry.resize(n), border.resize(n, {INT_MAX, -1}), ans.resize(q); for (long long i = 1; i < n; i++) { long long p, w; cin >> p >> w; p--; g[p].push_back({i, w}); g[i].push_back({p, w}); } for (long long i = 0; i < n; i++) sort(g[i].begin(), g[i].end()); for (long long i = 0; i < q; i++) { long long v, l, r; cin >> v >> l >> r; v--, l--, r--; qry[v].push_back({i, {l, r}}); } dfs1(0, -1, 0); m = leafs.size(); idx.resize(n); for (long long i = 0; i < m; i++) idx[leafs[i]] = i; build(0, m - 1, 1); dfs2(0, -1); for (long long i = 0; i < q; i++) cout << ans[i] << '\n'; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string x, c, v, k; getline(cin, x); int sz = x.size(); for (int i = 0; i < sz; i++) { c += x[i]; if (x[i] == '!' || x[i] == '?' || x[i] == '.' || x[i] == ',') c += ' '; } sz = c.size(); for (int i = 0; i < sz; i++) { if (c[i] == ' ' && c[i - 1] == ' ') continue; else v += c[i]; } sz = v.size(); for (int i = 0; i < sz; i++) { if (v[i] == ' ' && (v[i + 1] == '!' | v[i + 1] == '?' || v[i + 1] == '.' || v[i + 1] == ',')) continue; else k += v[i]; } cout << k << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; using LD = long double; using PLL = pair<long long, long long>; using VII = vector<pair<int, int> >; template <class T> bool power_2(T v) { static_assert(std::is_integral<T>::value, "type should be integral"); return v && !(v & (v - 1)); } template <class T> istream& operator>>(istream& st, vector<T>& container) { for (auto& u : container) st >> u; return st; } template <class T> ostream& operator<<(ostream& st, const vector<T>& container) { for (auto& u : container) st << u << '\n'; return st; } template <class T, size_t N> istream& operator>>(istream& st, array<T, N>& container) { for (auto& u : container) st >> u; return st; } template <class T, size_t N> ostream& operator<<(ostream& st, const array<T, N>& container) { for (auto u : container) st << u << ' '; return st; } template <class T, class U> istream& operator>>(istream& st, pair<T, U>& p) { st >> p.first >> p.second; return st; } template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; } template <class T, class U, class V> pair<T, U> operator*(pair<T, U> p, V val) { return {p.first * val, p.second * val}; } template <class T, class U, class V> pair<T, U> operator/(pair<T, U> p, V val) { return {p.first / val, p.second / val}; } template <class T, class U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } template <class T, class U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first * b.first + a.second * b.second; } template <class T> T crossProduct(pair<T, T> a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template <class T> T lengthPow(pair<T, T> a) { return a.first * a.first + a.second * a.second; } template <class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); } const int N = 1e6 + 57, inf = 1e9 + 7; const long long MOD = 1e9 + 7; const long long INF = 1e18 + 7; const long double PI = acos(-1); const LD EPS = 1e-12; const int pod = 29; long long hasz[N]; long long potegi[N]; char start; string s, t; int pos1 = 0, pos2 = 0; bool teSame(int a, int b, int c, int d) { if (b - a != d - c) return 0; long long odej = 0; if (a > 0) odej = hasz[a - 1] * potegi[b - a + 1] % MOD; long long l = (hasz[b] - odej + MOD) % MOD; if (c > 0) odej = hasz[c - 1] * potegi[d - c + 1] % MOD; long long p = (hasz[d] - odej + MOD) % MOD; if (l == p) { return 1; int zmiana = (d - c) / 6; for (int i = 0; i < 6; ++i) { if (t[a + i * zmiana] != t[c + i * zmiana]) return 0; } return 1; } return 0; } bool check(int dl1, int dl2) { int pos = 0; int posS = 0; int posT = pos2 * dl1; if (teSame(0, dl1 - 1, posT, posT + dl2 - 1)) return 0; while (pos < t.size()) { if (s[posS] == start) { if (!teSame(0, dl1 - 1, pos, pos + dl1 - 1)) return 0; pos += dl1; } else { if (!teSame(posT, posT + dl2 - 1, pos, pos + dl2 - 1)) return 0; pos += dl2; } ++posS; } return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout << fixed << setprecision(9); cin >> s >> t; int n = t.size(); potegi[0] = 1; for (int i = 1; i <= t.size(); ++i) { potegi[i] = potegi[i - 1] * pod % MOD; } for (int i = 0; i < t.size(); ++i) { if (i > 0) hasz[i] = hasz[i - 1] * pod % MOD; hasz[i] += (t[i] - 'a' + 1); hasz[i] %= MOD; } start = s[0]; long long zera = 0, jed = 0; for (auto u : s) { if (u == start) ++zera; else ++jed; } for (int i = 0; i < s.size(); ++i) { if (s[i] != start) { pos2 = i; break; } } int res = 0; long long dl = 1; while (dl * zera + jed <= t.size()) { int zostalo = t.size() - dl * zera; if (zostalo % jed != 0) { ++dl; continue; } int dl2 = zostalo / jed; if (check(dl, dl2)) ++res; ++dl; } cout << res; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; if (k > n) { cout << -1; return 0; } vector<int> cord; int kk; for (int i = 0; i < n; i++) cin >> kk, cord.push_back(kk); sort(cord.begin(), cord.end()); cout << 0 << " " << cord[n - k] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > fc; vector<int> fr; int x[100000]; bool ok() { for (int i = 0; i < (int)(fc.size()); ++i) if (fr[i] < fc[i].second) return false; return true; } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 2; i * i <= k; ++i) if (k % i == 0) { int z = 1; k /= i; while (k % i == 0) k /= i, ++z; fc.push_back(make_pair(i, z)); } if (k > 1) fc.push_back(make_pair(k, 1)); fr.resize(fc.size()); int l = 0; long long an = 0; for (int i = 0; i < (int)(n); ++i) { scanf("%d", x + i); int t = x[i]; for (int j = 0; j < (int)(fc.size()); ++j) while (t % fc[j].first == 0) ++fr[j], t /= fc[j].first; while (l <= i && ok()) { int t = x[l++]; for (int j = 0; j < (int)(fc.size()); ++j) while (t % fc[j].first == 0) --fr[j], t /= fc[j].first; } an += l; } printf("%lld\n", an); }
5
#include <bits/stdc++.h> using namespace std; void Arr2DInit(vector<vector<int> > &a, int n, int m) { a.resize(n); for (int i = 0; i < n; i++) a[i].resize(m); } template <class T> T minn(T a, T b) { return a < b ? a : b; } template <class T> T maxx(T a, T b) { return a > b ? a : b; } long long Pow(long long a, long long n) { if (n == 0) return 1; if (n == 1) return a; if (n % 2 == 0) return Pow(a, n / 2) * Pow(a, n / 2); else return Pow(a, n / 2) * Pow(a, n / 2) * a; } const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; string direct = "UDRL"; struct SAVE { int a, b, i; }; bool Cmp(SAVE a, SAVE b) { return abs(a.a - a.b) > abs(b.a - b.b); } int main() { int n, x, maxrank = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> x; maxrank = maxx(maxrank, x); } cout << maxx(0, maxrank - 25); }
0
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; double a[103]; for (int i = 0; i < n; i++) scanf("%lf", &a[i]); sort(a, a + n); double ans = 0.0, curr = 1.0; for (int i = n - 1; i >= 0; i--) { if (ans < ((ans * (1.00 - a[i])) + (curr * (a[i])))) { ans = (double)ans * (1.00 - a[i]) + curr * a[i]; } curr = curr * (1.00 - a[i]); } printf("%.10lf", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; const int N = 100005; long long n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int ans = 0; for (int i = 1; i < n; i++) { int cur = 1; bool ok = 1; for (int j = 1; j < n - 1; j++) { if ((cur * i - 1) % n == 0) ok = 0; cur = (cur * i) % n; } if ((cur * i - 1) % n != 0) ok = 0; ans += ok; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int w; int main() { ios_base::sync_with_stdio(false); cin >> w; while (w--) { int n; cin >> n; if (n == 1) { cout << -1 << "\n"; continue; } n -= 2; for (int i = 1; i <= n; i++) cout << 6; cout << 4 << 3 << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long arr[n]; map<long long, long long> dis; queue<long long> q; for (long long i = 0; i < n; i++) { cin >> arr[i]; q.push(arr[i]); dis[arr[i]] = 0; } long long ans = 0; vector<long long> res; while (!q.empty()) { if (res.size() == m) break; long long cur = q.front(); q.pop(); if (dis[cur] != 0) { res.push_back(cur); ans += dis[cur]; } if (!dis.count(cur - 1)) { dis[cur - 1] = dis[cur] + 1; q.push(cur - 1); } if (!dis.count(cur + 1)) { dis[cur + 1] = dis[cur] + 1; q.push(cur + 1); } } cout << ans << endl; for (auto x : res) cout << x << " "; }
5
#include <bits/stdc++.h> using namespace std; void read(vector<long long> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } void print(vector<long long> v) { for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } long long n, m, a, b, temp; string str; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m; cin >> n >> m; long long s, e; cin >> s >> e; long long v; cin >> v; vector<long long> stairs(s); read(stairs); vector<long long> ele(e); read(ele); int q; cin >> q; long long c, d; long long ans; long long distance; while (q--) { cin >> a >> b >> c >> d; ans = 100000000000000000; if (a > c) { swap(a, c); swap(b, d); } if (a == c) { cout << abs(b - d); cout << '\n'; continue; } if (b == d) { if (ele.size() != 0) { auto ne = lower_bound(ele.begin(), ele.end(), b); if (ne != ele.end()) { distance = *ne - b; ans = min(ans, 2 * distance + ((c - a) + (v - 1)) / v); } if (ne != ele.begin()) { ne--; distance = b - *ne; ans = min(ans, 2 * distance + ((c - a) + (v - 1)) / v); } } if (stairs.size() != 0) { auto ns = lower_bound(stairs.begin(), stairs.end(), b); if (ns != stairs.end()) { distance = *ns - b; ans = min(ans, 2 * distance + (c - a)); } if (ns != stairs.begin()) { ns--; distance = b - *ns; ans = min(ans, 2 * distance + c - a); } } } else if (b < d) { if (ele.size() != 0) { auto ne = lower_bound(ele.begin(), ele.end(), b); if (ne != ele.end()) { if (*ne <= d) ans = min(ans, d - b + ((c - a) + (v - 1)) / v); else ans = min(ans, d - b + 2 * (*ne - d) + ((c - a) + (v - 1)) / v); } if (ne != ele.begin()) { ne--; ans = min(ans, d - b + 2 * (b - *ne) + ((c - a) + (v - 1)) / v); } } if (stairs.size() != 0) { auto ns = lower_bound(stairs.begin(), stairs.end(), b); if (ns != stairs.end()) { if (*ns <= d) ans = min(ans, d - b + (c - a)); else ans = min(ans, d - b + 2 * (*ns - d) + (c - a)); } if (ns != stairs.begin()) { ns--; ans = min(ans, d - b + 2 * (b - *ns) + c - a); } } } else { if (ele.size() != 0) { auto ne = lower_bound(ele.begin(), ele.end(), b); if (ne != ele.end()) { ans = min(ans, b - d + 2 * (*ne - b) + ((c - a) + v - 1) / v); } if (ne != ele.begin()) { ne--; if (*ne >= d) ans = min(ans, b - d + ((c - a) + v - 1) / v); else ans = min(ans, b - d + ((c - a) + v - 1) / v + 2 * (d - *ne)); } } if (stairs.size() != 0) { auto ns = lower_bound(stairs.begin(), stairs.end(), b); if (ns != stairs.end()) { ans = min(ans, b - d + 2 * (*ns - b) + (c - a)); } if (ns != stairs.begin()) { ns--; if (*ns >= d) ans = min(ans, b - d + c - a); else ans = min(ans, b - d + c - a + 2 * (d - *ns)); } } } cout << ans; cout << '\n'; } }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int k = 0; k < n; k++) { string s, t; cin >> s >> t; int cnt = 1, ind = 0; vector<pair<char, int> > a, b; for (int i = 1; i < s.size(); i++) { if (s[i] != s[i - 1]) { a.push_back({s[i - 1], cnt}); cnt = 1; } else cnt++; } a.push_back({s[s.size() - 1], cnt}); cnt = 1; for (int i = 1; i < t.size(); i++) { if (t[i] != t[i - 1]) { b.push_back({t[i - 1], cnt}); cnt = 1; } else cnt++; } b.push_back({t[t.size() - 1], cnt}); if (a.size() != b.size()) cout << "NO" << endl; else { for (int i = 0; i < a.size(); i++) { if (a[i].first != b[i].first || a[i].second > b[i].second) { cout << "NO" << endl; ind = 1; break; } } if (ind == 0) { cout << "YES" << endl; } } } return 0; }
2
#include <bits/stdc++.h> int a[1000001], c[12000]; void prime() { int n, m, i, j, k = 1, s; memset(a, 0, sizeof(a)); a[0] = a[1] = 1; for (i = 0; i <= 1000001; i++) { if (a[i] == 0) { for (j = i + i; j <= 1000001; j += i) a[j] = 1; } } } int rev(int n) { int i, j, k, t = 0; for (i = n; i > 0; i /= 10) { t = t * 10 + i % 10; } return t; } void eprime() { int i, j, k, l, m, n; int s; for (i = 1, j = 1; i <= 1000001; i++) { if (a[i] == 0) { s = rev(i); if (s != i) if (a[s] == 0) { c[j++] = i; } } } } int main() { prime(); eprime(); int n, m, i, j, k; while (scanf("%d", &n) == 1) { printf("%d\n", c[n]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a, b; long long T; cin >> n >> a >> b >> T; string s; cin >> s; bool c[n]; for (int i = 0; i < n; i++) c[i] = (s[i] == 'w'); long long l[n]; l[0] = 1 + b * c[0]; for (int i = 1; i < n; i++) { l[i] = l[i - 1] + a + 1 + b * c[i]; } long long r[n]; r[0] = 1 + b * c[n - 1]; for (int i = 1; i < n; i++) { r[i] = r[i - 1] + a + 1 + b * c[n - i - 1]; } long long ans = 0; for (int i = 0; i < n; i++) { long long t = T; if (l[i] > t) break; t -= l[i]; long long now = i + 1; if (a * now > t) { ans = max(ans, now); continue; } t -= a * now; now += upper_bound(r, r + n, t) - r; if (now > n) now = n; ans = max(ans, now); } l[0] = 1 + b * c[0]; for (int i = 1; i < n; i++) { l[i] = l[i - 1] + a + 1 + b * c[n - i]; } r[0] = 1 + b * c[1]; for (int i = 1; i < n - 1; i++) { r[i] = r[i - 1] + a + 1 + b * c[i + 1]; } for (int i = 0; i < n; i++) { long long t = T; if (l[i] > t) break; t -= l[i]; long long now = i + 1; if (a * now > t) { ans = max(ans, now); continue; } t -= a * now; now += upper_bound(r, r + n, t) - r; if (now > n) now = n; ans = max(ans, now); } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m; int a[100005]; int sum[25][100005]; bool candy[25][100005]; int scandy[25][100005] = {0}; int mylog(int x) { int i, j; for (i = 0; x > 0; i++) x >>= 1; return i; } int main() { int i, j, k; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; sum[0][i] = a[i]; candy[0][i] = scandy[0][i] = 0; } for (i = 1; i < 25; i++) { for (j = 0; j + (1 << (i - 1)) < n; j++) { sum[i][j] = (sum[i - 1][j] + sum[i - 1][j + (1 << (i - 1))]) % 10; candy[i][j] = (sum[i - 1][j] + sum[i - 1][j + (1 << (i - 1))]) >= 10; scandy[i][j] = scandy[i - 1][j] + scandy[i - 1][j + (1 << (i - 1))] + candy[i][j]; } } cin >> m; for (i = 0; i < m; i++) { int st, en; int ans = 0; cin >> st >> en; st--; en--; int logg = mylog(en - st); cout << scandy[logg][st] << endl; } return 0; }
3
#include <bits/stdc++.h> int main() { unsigned long long int x, y, z; scanf("%llu %llu %llu", &x, &y, &z); printf("%s", (y + z) > (x + 1) ? "Black" : "White"); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 200004, M = 19; vector<int> g[N]; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int d[N], fa[N][M], n; long long w[N][M], a[N], mod = 1000000007; void bfs() { d[1] = 1; for (int i = 0; i < M; i++) w[1][i] = a[1]; queue<int> q; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (d[v]) continue; d[v] = d[u] + 1; fa[v][0] = u; w[v][0] = a[v]; for (int i = 1; i < M; i++) { fa[v][i] = fa[fa[v][i - 1]][i - 1]; w[v][i] = gcd(w[v][i - 1], w[fa[v][i - 1]][i - 1]); } q.push(v); } } } int main() { int x, y; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i < n; i++) { scanf("%d %d", &x, &y); g[x].push_back(y); g[y].push_back(x); } bfs(); long long ans = 0; for (int i = 1; i <= n; i++) { long long nw = a[i]; int pre = d[i]; for (;;) { int v = i; for (int j = M - 1; j >= 0; j--) if (w[v][j] == 0) v = fa[v][j]; for (int j = M - 1; j >= 0; j--) if (gcd(nw, w[v][j]) == nw) v = fa[v][j]; ans = (ans + nw * (pre - d[v])) % mod; if (v == 0) break; nw = gcd(nw, a[v]); pre = d[v]; } } printf("%lld\n", ans); }
6
#include <bits/stdc++.h> using namespace std; int day(string s) { if (s == "monday") { return 1; } if (s == "tuesday") { return 2; } if (s == "wednesday") { return 3; } if (s == "thursday") { return 4; } if (s == "friday") { return 5; } if (s == "saturday") { return 6; } if (s == "sunday") { return 0; } } int main() { string s, s1; cin >> s >> s1; if ((day(s) + 30) % 7 == day(s1) || (day(s) + 28) % 7 == day(s1) || (day(s) + 31) % 7 == day(s1)) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long x1, y1, x2, y2, n, ai, bi, ci, ans = 0; cin >> x1 >> y1 >> x2 >> y2 >> n; for (int i = 0; i < n; i++) { cin >> ai >> bi >> ci; long long f1 = ai * x1 + bi * y1 + ci; long long f2 = ai * x2 + bi * y2 + ci; if ((f1 > 0 && f2 < 0) || (f1 < 0 && f2 > 0)) ans++; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int val[26]; string second; for (int i = 0; i < 26; i++) { cin >> val[i]; } cin >> second; vector<long long int> adj[26]; long long int pref[second.size()]; for (int i = 0; i < second.size(); i++) { adj[second[i] - 'a'].push_back(i); if (i == 0) { pref[i] = val[second[i] - 'a']; } else { pref[i] = pref[i - 1] + val[second[i] - 'a']; } } long long int ans = 0; for (char c = 'a'; c <= 'z'; c++) { long long int sum = 0; unordered_map<long long int, long long int> mp; for (int j = 0; j < adj[c - 'a'].size(); j++) { ans += mp[pref[adj[c - 'a'][j]] - val[c - 'a']]; mp[pref[adj[c - 'a'][j]]]++; } } cout << ans << endl; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60777216") using namespace std; const long long P = 101113; bool cmp(pair<int, int> lhs, pair<int, int> rhs) { int l = lhs.second - lhs.first + 1; int r = rhs.second - rhs.first + 1; if (l != r) return l < r; return lhs < rhs; } int main() { int n; while (scanf("%d", &n) == 1) { vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); vector<long long> hashes(n); vector<long long> powers(n); powers[0] = 1; for (int i = 1; i < n; ++i) powers[i] = powers[i - 1] * P % 1000000007LL; hashes[0] = v[0] % 1000000007LL; for (int i = 1; i < n; ++i) hashes[i] = (hashes[i - 1] + powers[i] * v[i] % 1000000007LL) % 1000000007LL; map<int, vector<int> > memo; for (int i = 0; i < n; ++i) memo[v[i]].push_back(i); vector<pair<int, int> > pairs; for (auto it = memo.begin(); it != memo.end(); ++it) { for (int i = 0; i < it->second.size(); ++i) { for (int j = i + 1; j < it->second.size(); ++j) { int I = it->second[i]; int J = it->second[j]; int len = J - I - 1; if (J + len - 1 < n) { long long secondHash = hashes[J + len]; if (J - 1 >= 0) secondHash -= hashes[J - 1]; long long firstHash = hashes[I + len]; if (I - 1 >= 0) firstHash -= hashes[I - 1]; firstHash = (firstHash % 1000000007LL + 1000000007LL) % 1000000007LL; secondHash = (secondHash % 1000000007LL + 1000000007LL) % 1000000007LL; firstHash *= powers[J - I]; firstHash %= 1000000007LL; if (secondHash == firstHash) pairs.push_back(make_pair(I, J)); } } } } sort(pairs.begin(), pairs.end(), cmp); for (auto it = pairs.begin(); it != pairs.end(); ++it) { ; }; int leftBound = 0; for (auto it = pairs.begin(); it != pairs.end(); ++it) { if (it->first >= leftBound) { leftBound = it->second; } } printf("%d\n", n - leftBound); for (int i = leftBound; i < n; ++i) printf("%d ", v[i]); puts(""); } return 0; }
7
#include <bits/stdc++.h> using namespace std; char s[200005]; int offset = 100000; int sum[200005], n; set<int> st[200005]; int main() { scanf("%d%s", &n, s + 1); for (int i = 1; i <= n; i++) { if (s[i] == '1') sum[i] = sum[i - 1] + 1; else sum[i] = sum[i - 1] - 1; } st[0 + offset].insert(0); for (int i = 1; i <= n; i++) st[offset + sum[i]].insert(i); int ans = 0; for (int i = 1; i < 200005; i++) { if (st[i].size() < 2) continue; int l = *st[i].begin(); int r = *st[i].rbegin(); ans = max(ans, r - l); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long n, s, a[100]; int iv[100], ans; int inv(long long x) { int X = x % mod, res = 1, m = mod - 2; while (m) { if (m & 1) res = 1LL * res * X % mod; X = 1LL * X * X % mod, m >>= 1; } return res; } int calc(long long num) { if (num < 0) return 0; int res = 1; for (long long i = num + n - 1; i > num; --i) res = 1LL * i % mod * res % mod; for (int i = 1; i < n; ++i) res = 1LL * iv[i] * res % mod; return res; } int main() { scanf("%lld%lld", &n, &s); for (int i = 1; i <= n; ++i) iv[i] = inv(i); for (int i = 1; i <= n; ++i) scanf("%lld", a + i); for (int i = 0; i < (1 << n); ++i) { long long num = s, cnt = 0; for (int j = 1; j <= n; ++j) if (1 << (j - 1) & i) num -= a[j] + 1, ++cnt; if (num < 0) continue; int res = calc(num); if (cnt & 1) ans -= res, ans += ans < 0 ? mod : 0; else ans += res, ans -= ans >= mod ? mod : 0; } printf("%d\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace ::std; const long long maxn = 22; const long long mod = 1e9 + 7; const long long inf = 50; char dp[1 << maxn]; char kiupp[1 << maxn]; long long kiup[1 << maxn]; bool good[1 << maxn]; bool ger[maxn][maxn]; long long ham[maxn]; int main() { long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; a--; b--; ger[a][b] = 1; ger[b][a] = 1; ham[a] ^= (1 << b); ham[b] ^= (1 << a); } for (long long i = 0; i < n; i++) { ger[i][i] = 1; } good[0] = 1; for (long long mas = 1; mas < (1 << n); mas++) { long long v = mas; if ((v & (-v)) == v) { good[v] = 1; } else { long long u = __builtin_ctz(v); v -= (v & (-v)); good[mas] = good[v]; for (long long i = 0; i < n; i++) { if ((v >> i) & 1) { good[mas] &= ger[i][u]; } } } } fill(dp, dp + (1 << maxn), inf); dp[0] = 0; for (long long mas = 1; mas < (1 << n); mas++) { if (good[mas]) { dp[mas] = 0; } for (long long i = 0; i < n; i++) { if ((mas >> i) & 1) { long long v = (mas | ham[i]); if (dp[v] > dp[mas] + 1) { dp[v] = dp[mas] + 1; kiup[v] = mas; kiupp[v] = i; } } } } long long v = (1 << n) - 1; cout << (int)dp[v] << endl; vector<long long> vec; while (dp[v] > 0) { vec.push_back(kiupp[v] + 1); v = kiup[v]; } while (vec.size()) { cout << vec.back() << ' '; vec.pop_back(); } }
8
#include <bits/stdc++.h> using namespace std; int n, a[17], b[5][5]; long long sum; map<long long, int> M; bool check() { for (int i = 1; i <= n; ++i) { int t = 0; for (int j = 1; j <= n; ++j) { t += b[i][j]; } if (t != sum) return false; } for (int j = 1; j <= n; ++j) { int t = 0; for (int i = 1; i <= n; ++i) { t += b[i][j]; } if (t != sum) return false; } int t = 0; for (int i = 1; i <= n; ++i) { t += b[i][i]; } if (t != sum) return false; t = 0; for (int i = 1; i <= n; ++i) { t += b[i][n - i + 1]; } if (t != sum) return false; cout << sum << endl; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << b[i][j]; if (j == n) { cout << endl; } else { cout << ' '; } } } return true; } bool dfs(int i, int j, long long tsum) { if (i == n + 1) { if (check()) return true; return false; } else { if (j == n) { long long r = sum - tsum; if (M[r] == 0) return false; --M[r]; b[i][j] = r; if (dfs(i + 1, 1, 0)) return true; ++M[r]; return false; } for (map<long long, int>::iterator it = M.begin(); it != M.end(); ++it) { if (it->second) { --M[it->first]; b[i][j] = it->first; if (dfs(i, j + 1, tsum + it->first)) return true; ++M[it->first]; } } return false; } } int main() { cin >> n; for (int i = 1, k = 0; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[++k]; M[a[k]] += 1; } } sort(a + 1, a + 1 + n * n); for (int i = 1, k = 0; i <= n; ++i) for (int j = 1; j <= n; ++j) sum += a[++k]; sum /= n; dfs(1, 1, 0); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long BigMod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return R; } const int N = 500050; const long long inf = 2e18; long long dp[N], arr[N], arr2[N]; char str[N]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n, m, k, a, b, t; long long sum = 0; string s; cin >> t; while (t--) { cin >> a >> b; if (a % b) { cout << a << endl; continue; } long long z = sqrtl(b); vector<pair<long long, long long> > v; n = b; for (int i = 2; i <= z; i++) { long long cn = 0; while (n % i == 0) { cn++; n /= i; } if (cn) v.push_back({i, cn}); } if (n > 1) v.push_back({n, 1LL}); vector<pair<long long, long long> > g; vector<long long> d; n = a; for (int i = 0; i < v.size(); i++) { long long cn = 0; while (n % v[i].first == 0) { cn++; n /= v[i].first; } if (cn >= v[i].second) { long long an = 1LL; for (long long j = 1; j <= cn - v[i].second + 1; j++) { an *= v[i].first; } d.push_back(an); } } if (d.size() == 0) { cout << (a / b) << endl; } else { sort(d.begin(), d.end()); long long ans = (a / d[0]); cout << ans << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k; long long dp[300000 + 100]; bool is_prime[300000 + 100]; void solve() { memset(dp, 0, sizeof(dp)); fill(is_prime, is_prime + n + 1, true); is_prime[0] = is_prime[1] = false; for (int i = 1; i <= n; i++) { for (int j = i * 2; j <= n; j += i) { dp[j]++; is_prime[j] = i == 1; } dp[i] += dp[i - 1]; } vector<int> res; if (dp[n] >= k) { n = lower_bound(dp, dp + n + 1, k) - dp; k = dp[n] - k; for (int i = 1; i <= n; i++) { if (!is_prime[i]) res.push_back(i); else { int d = n / i; if (d <= k) { k -= d; } else { res.push_back(i); } } } } if (k > 0) { cout << "No\n"; return; } printf("Yes\n%d\n", res.size()); for (int i = 0; i < res.size(); i++) cout << res[i] << " "; cout << endl; } int main() { cin >> n >> k; solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int lsone(int n) { return (n & -n); } void mult(long long int a[25][25], long long int b[25][25], long long int c[25][25], int m, int n, int p) { for (int i = 1; i <= m; i++) { for (int j = 1; j <= p; j++) { c[i][j] = 0; for (int k = 1; k <= n; k++) { c[i][j] += (a[i][k] * b[k][j]) % 1000000007; c[i][j] %= 1000000007; } } } } void mat_pow(long long int a[25][25], long long int c[25][25], int n, long long int p) { if (p == 0) { for (int i = 1; i <= n; i++) c[i][i] = 1; } else if (p == 1) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) c[i][j] = a[i][j]; } } else { long long int d[25][25]; mat_pow(a, d, n, p / 2); if (p % 2) { long long int e[25][25]; mult(d, d, e, n, n, n); mult(e, a, c, n, n, n); } else { mult(d, d, c, n, n, n); } } } long long int pow1(long long int a, long long int b) { if (b == 0) return 1ll; else if (b == 1) return a; else { long long int x = pow1(a, b / 2); x *= x; x %= 1000000007; if (b % 2) { x *= a; x %= 1000000007; } return x; } } string n; int dp[1100000]; int sum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; dp[0] = 0; for (int i = 1; i <= 1e6; i++) { if (sum(i) == i) { continue; } else { dp[i] = 1 + dp[sum(i)]; } } cin >> n; if (n.size() == 1) { cout << 0 << "\n"; return 0; } int ans = 1; int begin = 0; for (int i = 0; i < n.size(); i++) begin += (n[i] - '0'); cout << (dp[begin] + ans) << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, d1[2001], d2[2001], p1, p2; int main() { cin.sync_with_stdio(false); cin >> n; long long result = 0; for (int i = 0; i < n; i++) { cin >> p1 >> p2; int d1v = p1 + p2; int d2v = p1 + 1000 - p2; result += d1[d1v] + d2[d2v]; d1[d1v]++; d2[d2v]++; } cout << result << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; string str, ans = ""; cin >> str; int arr[26]; for (int i = 0; i < 26; i++) arr[i] = 0; for (int i = 0; i < str.length(); i++) arr[str[i] - 'a']++; bool test = true; for (int i = 0; i < 26; i++) { if (arr[i] % k != 0) { test = false; break; } } if (!test) cout << -1; else { for (int i = 0; i < 26; i++) { if (arr[i] != 0) { arr[i] = arr[i] / k; while (arr[i] != 0) { ans += char(i + 'a'); arr[i]--; } } } for (int i = 0; i < k; i++) cout << ans; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int maxm = 1e6 + 5; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; int main() { int t; cin >> t; while (t--) { int a[4]; int ans = 0; for (int i = 0; i < 4; i++) { cin >> a[i]; if (a[i] % 2 == 1) ans++; } if (ans <= 1) { cout << "Yes\n"; continue; } ans = 0; int flag = 1; for (int i = 0; i < 3; i++) { a[i]--; if (a[i] < 0) { flag = 0; } if (a[i] % 2 == 1) ans++; } a[3] += 3; if (a[3] % 2 == 1) ans++; if (flag == 0) { cout << "No\n"; continue; } if (ans <= 1) { cout << "Yes\n"; continue; } cout << "No\n"; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> seg(n); int xs, ys; cin >> xs >> ys; int xb = xs, yb = ys; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; seg[i] = abs(x - xb) + abs(y - yb); xb = x, yb = y; } seg[n - 1] = abs(xs - xb) + abs(ys - yb); vector<int> rod(m); for (int i = 0; i < m; i++) cin >> rod[i]; int ok = -1; vector<int> patt[2]; for (int k = 0; k < 2; k++) { patt[k].resize(n / 2); for (int i = 0; i < n / 2; i++) { if (k == 0) patt[k][i] = seg[2 * i] + seg[2 * i + 1]; else patt[k][i] = seg[2 * i] + seg[(2 * i + n - 1) % n]; } int usedcnt = 0; vector<bool> used(m); for (int i = 0; i < n / 2; i++) { for (int j = 0; j < m; j++) { if (used[j]) continue; if (patt[k][i] == rod[j]) { patt[k][i] = j + 1; used[j] = true; usedcnt++; break; } } } if (usedcnt == n / 2) { ok = k; break; } } cout << (~ok ? "YES" : "NO") << endl; if (~ok) { if (ok == 0) for (int i = 0; i < n / 2; i++) cout << (i ? " " : "") << -1 << " " << patt[ok][i]; else for (int i = 0; i < n / 2; i++) cout << (i ? " " : "") << patt[ok][i] << " " << -1; } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.second * p1.first * (100 - p2.second) > p2.second * p2.first * (100 - p1.second); } }; double sum[100000]; pair<int, int> pr[100000]; int main() { int n, i, j; scanf("%d", &n); double ans = 0; for (i = 0; i < n; i++) { scanf("%d %d", &pr[i].first, &pr[i].second); ans += pr[i].first; } sort(pr, pr + n, cmp()); sum[n] = 0; for (i = n - 1; i >= 0; i--) { sum[i] = sum[i + 1] + 100 - pr[i].second; } for (i = 0; i < n; i++) { double p = pr[i].second; p /= 10000; ans += p * (sum[i + 1]) * pr[i].first; } printf("%.8lf\n", ans); return 0; }
6
#include <bits/stdc++.h> using std::cerr; using std::cin; using std::cout; using std::endl; using std::map; using std::max; using std::min; using std::pair; using std::queue; using std::set; using std::sort; using std::stack; using std::string; using std::swap; using std::to_string; using std::vector; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto& x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { if (false) cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { if (false) cerr << " " << to_string(H); debug_out(T...); } inline void fast_io() { cin.tie(0); std::ios::sync_with_stdio(false); } template <typename T> inline T read() { T tmp; cin >> tmp; return tmp; } auto readi = []() { return read<int>(); }; int main() { fast_io(); const int n = readi(); vector<pair<int, int> > v(n, {0, 1}); for (auto& i : v) i.first = readi(); ; sort(v.begin(), v.end()); if (false) cerr << __PRETTY_FUNCTION__ << ":" << 79 << "\t - \t"; debug_out("v: ", v); int cnt = 0; for (int i = 0; i < n; ++i) { bool placed = false; for (int j = i + 1; j < n; ++j) { if (v[j].first - v[j].second + 1 >= v[i].second) { v[j].second += v[i].second; v[j].first = v[i].first; placed = true; if (false) cerr << __PRETTY_FUNCTION__ << ":" << 89 << "\t - \t"; debug_out("moved ", i, "over", j, "v[j] = ", v[j]); break; } } if (!placed) ++cnt; } cout << cnt; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 105; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; long long res[100010]; vector<long long> g[100010]; int main() { int n, m; long long maxl = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y); } for (int i = 1; i <= m; i++) sort(g[i].begin(), g[i].end(), greater<long long>()); for (int i = 1; i <= m; i++) { long long temp = 0; for (int j = 0; j < g[i].size(); j++) { temp += g[i][j]; if (temp >= 0) res[j + 1] += temp; else break; } } for (int i = 1; i <= n; i++) maxl = max(maxl, res[i]); printf("%I64d\n", maxl); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, indS, indM, minFlick, maxFlick; string sherlock, sherlock_temp, moriary; cin >> n; cin >> sherlock >> moriary; sherlock_temp = sherlock; sort(sherlock.rbegin(), sherlock.rend()); sort(moriary.rbegin(), moriary.rend()); maxFlick = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (sherlock[j] != '-') { if (moriary[i] > sherlock[j]) { sherlock[j] = '-'; maxFlick++; break; } } } } sherlock = sherlock_temp; sort(sherlock.begin(), sherlock.end()); sort(moriary.begin(), moriary.end()); indS = indM = 0; while (indS < n && indM < n) { if (sherlock[indS] <= moriary[indM]) { indS++; indM++; } else indM++; } minFlick = n - indS; cout << minFlick << endl << maxFlick << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int vocala(char c) { if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') return 1; if (c == 'y' || c == 'Y') return 1; return 0; } int isprime(long long n) { if (n <= 1) return 0; if (n <= 3) return 1; if (n % 2 == 0 || n % 3 == 0) return 0; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } int isfibo(long long n) { long long a = 5 * n * n + 4; long long b = a - 8; if (sqrt(a) == int(sqrt(a)) || sqrt(b) == int(sqrt(b))) return 1; return 0; } int gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } ifstream in("input.txt"); ofstream out("output.txt"); long long const nrmax = 1e18; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; if (n == k && n % 2 != 0) cout << n; else { cout << "2"; } in.close(); out.close(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const int mod = 1000000007; const int mod2 = 1000000006; const long long infl = 0x3f3f3f3f3f3f3f3fLL; template <typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; } template <typename T> inline bool RD(T& a, T& b) { return RD(a) && RD(b); } template <typename T> inline bool RD(T& a, T& b, T& c) { return RD(a, b) && RD(c); } template <typename T> inline bool RD(T& a, T& b, T& c, T& d) { return RD(a, b, c) && RD(d); } template <typename T> inline bool RD(T& a, T& b, T& c, T& d, T& e) { return RD(a, b, c, d) && RD(e); } template <typename T> inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f) { return RD(a, b, c, d, e) && RD(f); } inline int RD() { int v; RD(v); return v; } char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; inline char getchar_buffered() { return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; } template <typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; } template <typename T> inline bool RDB(T& a, T& b) { return RDB(a) && RDB(b); } template <typename T> inline bool RDB(T& a, T& b, T& c) { return RDB(a, b) && RDB(c); } template <typename T> inline bool RDB(T& a, T& b, T& c, T& d) { return RDB(a, b, c) && RDB(d); } template <typename T> inline bool RDB(T& a, T& b, T& c, T& d, T& e) { return RDB(a, b, c, d) && RDB(e); } template <typename T> inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f) { return RDB(a, b, c, d, e) && RDB(f); } inline int RDB() { int v; RDB(v); return v; } template <typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); } template <typename T> inline void WR_(const T& a) { _WR(a); putchar(' '); } template <typename T> inline void WR(const T& a) { _WR(a); putchar('\n'); } template <typename T> inline void _WR(const T& a, const T& b) { WR_(a); _WR(b); } template <typename T> inline void WR_(const T& a, const T& b) { WR_(a); WR_(b); } template <typename T> inline void WR(const T& a, const T& b) { WR_(a); WR(b); } template <typename T> inline void _WR(const T& a, const T& b, const T& c) { WR_(a, b); _WR(c); } template <typename T> inline void WR_(const T& a, const T& b, const T& c) { WR_(a, b); WR_(c); } template <typename T> inline void WR(const T& a, const T& b, const T& c) { WR_(a, b); WR(c); } template <typename T> inline void _WR(const T& a, const T& b, const T& c, const T& d) { WR_(a, b, c); _WR(d); } template <typename T> inline void WR_(const T& a, const T& b, const T& c, const T& d) { WR_(a, b, c); WR_(d); } template <typename T> inline void WR(const T& a, const T& b, const T& c, const T& d) { WR_(a, b, c); WR(d); } template <typename T> inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e) { WR_(a, b, c, d); _WR(e); } template <typename T> inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e) { WR_(a, b, c, d); WR_(e); } template <typename T> inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e) { WR_(a, b, c, d); WR(e); } template <typename T> inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { WR_(a, b, c, d, e); _WR(f); } template <typename T> inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { WR_(a, b, c, d, e); WR_(f); } template <typename T> inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { WR_(a, b, c, d, e); WR(f); } void OUT() { std::cout << std::endl; } template <typename T> inline void _OUT(const T& a) { std::cout << a; } template <typename T> inline void OUT_(const T& a) { std::cout << a << ' '; } template <typename T> inline void OUT(const T& a) { std::cout << a << std::endl; } template <typename T, typename U> inline void _OUT(const T& a, const U& b) { std::cout << a << " " << b; } template <typename T, typename U> inline void OUT_(const T& a, const U& b) { std::cout << a << " " << b << " "; } template <typename T, typename U> inline void OUT(const T& a, const U& b) { std::cout << a << " " << b << std::endl; } template <typename T, typename U, typename V> inline void _OUT(const T& a, const U& b, const V& c) { OUT_(a, b); _OUT(c); } template <typename T, typename U, typename V> inline void OUT_(const T& a, const U& b, const V& c) { OUT_(a, b); OUT_(c); } template <typename T, typename U, typename V> inline void OUT(const T& a, const U& b, const V& c) { OUT_(a, b); OUT(c); } template <typename T, typename U, typename V, typename W> inline void _OUT(const T& a, const U& b, const V& c, const W& d) { OUT_(a, b, c); _OUT(d); } template <typename T, typename U, typename V, typename W> inline void OUT_(const T& a, const U& b, const V& c, const W& d) { OUT_(a, b, c); OUT_(d); } template <typename T, typename U, typename V, typename W> inline void OUT(const T& a, const U& b, const V& c, const W& d) { OUT_(a, b, c); OUT(d); } template <typename T, typename U, typename V, typename W, typename X> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e) { OUT_(a, b, c, d); _OUT(e); } template <typename T, typename U, typename V, typename W, typename X> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e) { OUT_(a, b, c, d); OUT_(e); } template <typename T, typename U, typename V, typename W, typename X> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e) { OUT_(a, b, c, d); OUT(e); } template <typename T, typename U, typename V, typename W, typename X, typename Y> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f) { OUT_(a, b, c, d, e); _OUT(f); } template <typename T, typename U, typename V, typename W, typename X, typename Y> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f) { OUT_(a, b, c, d, e); OUT_(f); } template <typename T, typename U, typename V, typename W, typename X, typename Y> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f) { OUT_(a, b, c, d, e); OUT(f); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g) { OUT_(a, b, c, d, e, f); _OUT(g); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g) { OUT_(a, b, c, d, e, f); OUT_(g); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g) { OUT_(a, b, c, d, e, f); OUT(g); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h) { OUT_(a, b, c, d, e, f, g); _OUT(h); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h) { OUT_(a, b, c, d, e, f, g); OUT_(h); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h) { OUT_(a, b, c, d, e, f, g); OUT(h); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i) { OUT_(a, b, c, d, e, f, g, h); _OUT(i); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i) { OUT_(a, b, c, d, e, f, g, h); OUT_(i); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i) { OUT_(a, b, c, d, e, f, g, h); OUT(i); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B, typename C> inline void _OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i, const C& j) { OUT_(a, b, c, d, e, f, g, h, i); _OUT(j); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B, typename C> inline void OUT_(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i, const C& j) { OUT_(a, b, c, d, e, f, g, h, i); OUT_(j); } template <typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A, typename B, typename C> inline void OUT(const T& a, const U& b, const V& c, const W& d, const X& e, const Y& f, const Z& g, const A& h, const B& i, const C& j) { OUT_(a, b, c, d, e, f, g, h, i); OUT(j); } template <typename T> inline T max(const T& a, const T& b, const T& c) { return std::max(std::max(a, b), c); } template <typename T> inline T max(const T& a, const T& b, const T& c, const T& d) { return std::max(max(a, b, c), d); } template <typename T> inline T max(const T& a, const T& b, const T& c, const T& d, const T& e) { return std::max(max(a, b, c, d), e); } template <typename T> inline T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { return std::max(max(a, b, c, d, e), f); } template <typename T> inline T min(const T& a, const T& b, const T& c) { return std::min(std::min(a, b), c); } template <typename T> inline T min(const T& a, const T& b, const T& c, const T& d) { return std::min(min(a, b, c), d); } template <typename T> inline T min(const T& a, const T& b, const T& c, const T& d, const T& e) { return std::min(min(a, b, c, d), e); } template <typename T> inline T min(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) { return std::min(min(a, b, c, d, e), f); } inline long long madd(long long a, long long b, const long long mod = 1000000007) { return (a + b) % mod; } inline long long madd(long long a, long long b, long long c, const long long mod = 1000000007) { return (a + b + c) % mod; } inline long long madd(long long a, long long b, long long c, long long d, const long long mod = 1000000007) { return (a + b + c + d) % mod; } inline long long madd(long long a, long long b, long long c, long long d, long long e, const long long mod = 1000000007) { return (a + b + c + d + e) % mod; } inline long long madd(long long a, long long b, long long c, long long d, long long e, long long f, const long long mod = 1000000007) { return (a + b + c + d + e + f) % mod; } inline long long msub(long long a, long long b, const long long mod = 1000000007) { long long ret = a - b; while (ret < 0) ret += mod; return ret; } inline long long mmul(long long a, long long b, const long long mod = 1000000007) { return a * b % mod; } inline long long mmul(long long a, long long b, long long c, const long long mod = 1000000007) { return a * b % mod * c % mod; } inline long long mmul(long long a, long long b, long long c, long long d, const long long mod = 1000000007) { return a * b % mod * c % mod * d % mod; } inline long long mmul(long long a, long long b, long long c, long long d, long long e, const long long mod = 1000000007) { return a * b % mod * c % mod * d % mod * e % mod; } inline long long mmul(long long a, long long b, long long c, long long d, long long e, long long f, const long long mod = 1000000007) { return a * b % mod * c % mod * d % mod * e % mod * f % mod; } inline long long mpow(long long x, long long k, const long long mod = 1000000007) { x %= mod; long long ret = 1; while (k) { if (k & 1) ret = ret * x % mod; x = x * x % mod; k >>= 1; } return ret; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <typename T, typename U> inline bool cmax(T& a, const U& b) { return a < b ? a = b, true : false; } template <typename T, typename U> inline bool cmin(T& a, const U& b) { return a > b ? a = b, true : false; } template <typename T, typename U> inline T cadd(T& a, const U& b, const int mod = 1000000007) { return a = (a + b) % mod; } template <typename T, typename U> inline T csub(T& a, const U& b, const int mod = 1000000007) { a = (a - b + mod) % mod; while (a < 0) a += mod; return a; } template <typename T, typename U> inline T cmul(T& a, const U& b, const int mod = 1000000007) { return a = (1ll * a * b) % mod; } template <typename T> inline T clow(T& a, const int mod = 1000000007) { while (a < 0) a += mod; return a; } template <typename T> inline T cup(T& a, const int mod = 1000000007) { while (a >= mod) a -= mod; return a; } template <typename T> inline T cboth(T& a, const int mod = 1000000007) { while (a < 0) a += mod; while (a >= mod) a -= mod; return a; } template <typename T> inline T vlow(T a, const int mod = 1000000007) { while (a < 0) a += mod; return a; } template <typename T> inline T vup(T a, const int mod = 1000000007) { while (a >= mod) a -= mod; return a; } template <typename T> inline T vboth(T a, const int mod = 1000000007) { while (a < 0) a += mod; while (a >= mod) a -= mod; return a; } template <typename T> inline int lowbit(T x) { return x & -x; } template <typename T> inline int setbit(T x, int i) { return x | ((T)(1) << i); } template <typename T> inline int setbit2(T& x, int i) { return x |= ((T)(1) << i); } template <typename T> inline int resetbit(T x, int i) { return x & ~((T)(1) << i); } template <typename T> inline int resetbit2(T& x, int i) { return x &= ~((T)(1) << i); } template <typename T> inline bool testbit(T x, int i) { return (bool)(x & ((T)(1) << i)); } template <typename T> inline int cntbits1(T x) { int cnt = 0; while (x) { ++cnt; x &= x - 1; } return cnt; } template <typename T> inline int cntbits(T x) { int cnt = 1; while (x >> cnt) ++cnt; return cnt; } void delay_s(int x) { time_t t0 = time(0); while (time(0) < t0 + x) ; } void delay_ms(int x) { clock_t c0 = clock(); while (clock() < c0 + x) ; } double run_time() { return 1.0 * clock() / CLOCKS_PER_SEC; } char* local_time() { time_t t; time(&t); return ctime(&t); } const int dx[8] = {0, 1, 0, -1, -1, 1, 1, -1}; const int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int _ = (int)(1e4 + 10); const int __ = (int)(1e6 + 10); const int LOG = 17; int n; int t[_]; int ret[_]; int cnt[_]; inline void _main() { RDB(n); { for (int _i_ = (int)(1); _i_ < (int)(n + 1); ++_i_) RDB((t)[_i_]); }; for (int i = (int)(1); i < (int)(n + 1); ++i) { memset((cnt), (0), sizeof(cnt)); priority_queue<pair<int, int> > pq; for (int j = (int)(i); j < (int)(n + 1); ++j) { ++cnt[t[j]]; pq.push(make_pair(cnt[t[j]], -t[j])); ++ret[-pq.top().second]; } } { for (int _i_ = (int)(1); _i_ < (int)(n + 1 - 1); ++_i_) WR_((ret)[_i_]); WR((ret)[n + 1 - 1]); }; } void generate_test_case() { exit(0); } int main() { _main(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int getzn(string s, int o) { string z("0"); int a = 0; for (int i = 0; i < s.size(); i++) if (a < 60) { a *= o; a += s[i] - z[0]; } return a; } int main() { string z("0A:"), s, s1, s2; cin >> s; int dif = (int)z[1] - (int)z[0] - 10, i = 0, maxx = 0, ans = 0; while (s[i] != z[2]) { if ((int)s[i] >= (int)z[1]) s[i] = (char)(s[i] - dif); s1 += s[i]; maxx = max(maxx, (int)(s[i] - z[0])); i++; } i++; while (i < s.size()) { if ((int)s[i] >= (int)z[1]) s[i] = (char)(s[i] - dif); s2 += s[i]; maxx = max(maxx, (int)(s[i] - z[0])); i++; } bool a[62]; for (i = maxx + 1; i < 62; i++) { a[i] = (getzn(s1, i) < 24 && getzn(s2, i) < 60); ans += a[i]; } if (a[61]) ans = -1; if (ans <= 0) { cout << ans << endl; } else { for (i = maxx + 1; i < 62; i++) { if (a[i]) cout << i << " "; } cout << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b; if (a > b) cout << a - 1 << " " << b << endl; else cout << b - 1 << " " << a << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 2000000000; const int MAX_N = 3000; long long a[MAX_N]; long long b[MAX_N]; int main() { long long n, k, p, ans; cin >> n >> k >> p; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < k; i++) cin >> b[i]; sort(a, a + n); sort(b, b + k); ans = INF; for (int i = 0; i + n <= k; i++) { long long q = 0; for (int j = 0; j < n; j++) q = max(q, abs(b[i + j] - a[j]) + abs(p - b[i + j])); ans = min(ans, q); } reverse(a, a + n); reverse(b, b + k); for (int i = 0; i + n <= k; i++) { long long q = 0; for (int j = 0; j < n; j++) q = max(q, abs(b[i + j] - a[j]) + abs(p - b[i + j])); ans = min(ans, q); } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; int A[320 * 2][320 * 2], S[320 * 2]; int f[100030], v[100030]; int n, nsqrt; void add_in_bucata(int buc, int p, int import, int pasi) { int i; for (i = A[buc][0]; i && pasi && f[A[buc][i]] < import; --i, --pasi) A[buc][i + 1] = A[buc][i]; A[buc][i + 1] = p; A[buc][0]++; if (S[buc] < import) S[buc] = import; } void add(int p, int import, int pasi) { int buc = (p - 1) / nsqrt + 1; int nr = pasi; for (int i = buc; i; --i) if (i != 1 && nr - A[i][0] > 0 && S[i] < import) nr -= A[i][0]; else { add_in_bucata(i, p, import, nr); break; } } void optimize() { v[0] = 0; for (int i = 1; i <= nsqrt; ++i) { if (A[i][0] == 0) break; for (int j = 1; j <= A[i][0]; ++j) v[++v[0]] = A[i][j]; } for (int i = 1; i <= nsqrt; ++i) { S[i] = 0; for (int j = 1; j <= nsqrt && j + (i - 1) * nsqrt <= v[0]; ++j) { A[i][j] = v[j + (i - 1) * nsqrt]; A[i][0] = j; if (S[i] < f[A[i][j]]) S[i] = f[A[i][j]]; } } } int main() { scanf("%d", &n); nsqrt = (int)sqrt(n) + 1; for (int i = 1; i <= n; ++i) { int x, y; scanf("%d%d", &x, &y); f[i] = x; add(i, x, y); if (i % nsqrt == 0) optimize(); } for (int i = 1; i <= nsqrt; ++i) for (int j = 1; j <= A[i][0]; ++j) printf("%d ", A[i][j]); printf("\n"); return 0; }
7
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:64000000") const int maxn = 1 << 10; const int inf = 1000000007; const int mod = 1000000007; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; vector<int> e[maxn]; int d[maxn]; char c[maxn][maxn]; int main() { int k; while (cin >> k) { for (int i = 0; i < maxn; i++) e[i].clear(); int cur = 1, nw = 3; while (k > 1) { if (k % 2 == 0) { int a1 = nw++; int a2 = nw++; int a3 = nw++; d[a1] = d[cur] + 1; d[a2] = d[cur] + 1; d[a3] = d[cur] + 2; e[cur].push_back(a1); e[cur].push_back(a2); e[a1].push_back(a3); e[a2].push_back(a3); cur = a3; k /= 2; } else { int a1 = nw++; e[cur].push_back(a1); d[a1] = d[cur] + 1; k--; } } e[cur].push_back(2); d[2] = d[cur] + 1; for (int i = 1; i < nw; i++) { if (e[i].empty() && i != 2) { if (d[i] + 1 < d[2]) { int a1 = nw++; d[a1] = d[i] + 1; e[i].push_back(a1); } else e[i].push_back(2); } } for (int i = 1; i < nw; i++) for (int j = 1; j < nw; j++) c[i][j] = 'N'; for (int i = 1; i < nw; i++) for (int j = 0; j < e[i].size(); j++) c[i][e[i][j]] = c[e[i][j]][i] = 'Y'; cout << nw - 1 << endl; for (int i = 1; i < nw; i++) { for (int j = 1; j < nw; j++) { cout << c[i][j]; } cout << endl; } } return 0; }
5
#include <bits/stdc++.h> bool is_array(std::string s, std::string t) { std::sort(std::begin(s), std::end(s)); std::sort(std::begin(t), std::end(t)); return s == t; } bool is_automaton(const std::string& s, const std::string& t) { int size = s.size(), x = 0; for (const auto& c : t) { int i = std::find(std::begin(s) + x, std::end(s), c) - std::begin(s); if (i == size) return false; x = i + 1; } return true; } bool is_both(const std::string& s, const std::string& t) { std::vector<int> da(26, 0), db(26, 0); for (const auto& c : s) da[c - 'a']++; for (const auto& c : t) db[c - 'a']++; for (int i = 0; i < 26; i++) if (da[i] < db[i]) return false; return true; } int main(int argc, char** argv) { std::cin.tie(0); std::ios_base::sync_with_stdio(0); std::string s, t; std::cin >> s >> t; if (is_array(s, t)) { std::cout << "array" << std::endl; } else if (is_automaton(s, t)) { std::cout << "automaton" << std::endl; } else if (is_both(s, t)) { std::cout << "both" << std::endl; } else { std::cout << "need tree" << std::endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; namespace IO { void setIn(string s) { freopen(s.c_str(), "r", stdin); } void setOut(string s) { freopen(s.c_str(), "w", stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (s.size()) { setIn(s + ".inp"); setOut(s + ".out"); } else { } } } // namespace IO using namespace IO; namespace Function { template <typename T1, typename T2> void amax(T1 &a, T2 b) { assert(!(typeid(a).name() == typeid(int).name() && typeid(b).name() == typeid(long long).name())); if (a < b) a = b; } template <typename T1, typename T2> void amin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T> void compress(T &a) { sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); } template <typename T1, typename T2, typename T3> int position(T1 Begin, T2 sz, T3 val, bool type = 0) { if (type == 0) { return lower_bound(Begin, Begin + sz, val) - Begin; } return upper_bound(Begin, Begin + sz, val) - Begin; } template <typename T> long long sqr(T x) { return 1LL * x * x; } template <typename T1, typename T2> long long GCD(T1 a, T2 b) { return b == 0 ? a : GCD(b, a % b); } template <typename T1, typename T2> long long LCM(T1 a, T2 b) { return 1LL * a / GCD(a, b) * b; } } // namespace Function using namespace Function; namespace Output { void print(int x) { cout << x << "\n"; } void print(unsigned int x) { cout << x << "\n"; } void print(long unsigned int x) { cout << x << "\n"; } void print(long long x) { cout << x << "\n"; } void print(unsigned long long x) { cout << x << "\n"; } void print(float x) { cout << x << "\n"; } void print(double x) { cout << x << "\n"; } void print(long double x) { cout << x << "\n"; } void print(char x) { cout << x << "\n"; } void print(char *x) { cout << x << "\n"; } void print(unsigned char x) { cout << x << "\n"; } void print(const char *x) { cout << x << "\n"; } void print(string x) { cout << x << "\n"; } void print(bool x) { cout << x << "\n"; } template <class T, class... Ts> void print(T t, Ts... ts) { cout << t << " "; print(ts...); } template <typename T1, typename T2> void print(pair<T1, T2> a) { print(a.first, a.second); } template <typename T> void print(T a) { for (auto it : a) { print(it); } } template <class T, class... Ts> void prine(T t, Ts... ts) { print(t, ts...); exit(0); } } // namespace Output using namespace Output; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int INF = 1e9 + 10; const long long INFL = 1e19; const int MOD = 1e9 + 7; const int N = 1e5 + 10; int main() { setIO(); int T; cin >> T; while (T--) { long long n; cin >> n; string N = to_string(n); N = '0' + N; int s; cin >> s; int cnt = 0; for (char x : N) { cnt += x - '0'; } if (cnt <= s) { print(0); continue; } int sz = (int)N.size(); unsigned long long res = INFL; unsigned long long cur = 0; for (int i = 0; i < sz; i++) { cur *= 10; cur += N[i] - '0'; unsigned long long tmp = cur + 1; string S = to_string(tmp); cnt = 0; for (char y : S) { cnt += y - '0'; } if (cnt > s) { continue; } for (int j = i + 1; j < sz; j++) { tmp *= 10; } amin(res, tmp - n); } print(res); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int x; int t; cin >> t; while (t--) { cin >> x; cout << 1 << " " << x - 1 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double eps = 1e-9; const int maxn = 3e2 + 5; map<string, int> gt; int id[maxn], sum[maxn]; int main() { ios::sync_with_stdio(false); int n; cin >> n; int cnt = 1; sum[0] = 0; for (int i = 1; i < n + 1; i++) { string s; cin >> s; int num = gt[s]; if (num) { id[i] = num; } else { gt[s] = cnt; id[i] = cnt++; } sum[i] = sum[i - 1] + s.length(); } int ans = sum[n] + n - 1; for (int i = 1; i < n + 1; i++) { for (int j = i; j < n + 1; j++) { int len = j - i + 1; int tol = 0; for (int k = j + 1; k < n + 1; k++) { if (k + len - 1 > n) break; bool ok = true; for (int h = 0; h < len; h++) { if (id[i + h] == id[k + h]) continue; ok = false; break; } if (ok) { ++tol; k += len - 1; } } if (tol == 0) continue; int now = sum[n] + n - 1 - (sum[j] - sum[i - 1] - 1) * (tol + 1); if (now < ans) ans = now; } } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; inline bool equal(double _x, double _y) { return _x > _y - 1e-9 && _x < _y + 1e-9; } map<int, int> mp; map<int, vector<int> > p; vector<int> v; int main() { int n, t, cur = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &t); mp[t]++; p[t].push_back(i); } for (int i = 0; i < n; i++, cur++) { while (mp[cur] == 0 && cur > 2) cur -= 3; if (mp[cur] == 0) break; mp[cur]--; v.push_back(p[cur].back()); p[cur].pop_back(); } if ((int)v.size() == n) { puts("Possible"); for (auto i : v) printf("%d ", i); puts(""); } else puts("Impossible"); }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> V; V.push_back(1); for (int i = 1; i < n; i++) { V.push_back(1); while (V[V.size() - 1] == V[V.size() - 2]) { V.pop_back(); V[V.size() - 1] += 1; } } for (int i = 0; i < V.size(); i++) cout << V[i] << " "; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, k, p, c = 0, d = 0; cin >> n >> m >> k; p = k; long long sx[k], sy[k], fx[k], fy[k]; while (p--) { cin >> sx[c++] >> sy[d++]; } p = k; c = 0; d = 0; while (p--) { cin >> fx[c++] >> fy[d++]; } cout << (n * m + n + m - 3) << "\n"; for (int i = 0; i < n - 1; i++) cout << "U"; for (int i = 0; i < m - 1; i++) cout << "L"; for (int i = 0; i < n; i++) { for (int j = 0; j < m - 1; j++) { if (i % 2) cout << "L"; else cout << "R"; } if (i < n - 1) cout << "D"; else break; } }
4
#include <bits/stdc++.h> using namespace std; int n; const int M = 500; struct Data { int c[100010], C[1010]; void add(int x, int y) { c[x] += y; x++; while (x <= n && x % M != 0) { c[x] += y; x++; } while (x <= n) { C[x / M] += y; x += M; } return; } int query(int x) { return C[x / M] + c[x]; } } S; struct Q { int l, r, id; }; vector<Q> qy[100010]; struct Tree { int ch[26]; int fail; vector<int> vec; vector<int> ed; } T[100010]; int tot; int insert(char* str, int len, int o) { int now = 0; for (int i = 0; i < len; i++) { int c = str[i] - 'a'; if (!T[now].ch[c]) T[now].ch[c] = ++tot; now = T[now].ch[c]; T[now].vec.push_back(o); } T[now].ed.push_back(o); return now; } vector<int> to[100010]; queue<int> q; void bfs() { for (int i = 0; i < 26; i++) if (T[0].ch[i]) { T[T[0].ch[i]].fail = 0; q.push(T[0].ch[i]); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) if (T[u].ch[i]) { T[T[u].ch[i]].fail = T[T[u].fail].ch[i]; q.push(T[u].ch[i]); } else T[u].ch[i] = T[T[u].fail].ch[i]; to[T[u].fail].push_back(u); } return; } int L[100010], pos[100010]; long long ans[100010]; int sum[100010]; void dfs(int u, int x) { sum[u] = 0; for (int i = 0; i < T[u].vec.size(); i++) sum[u] += (T[u].vec[i] == x); for (int i = 0; i < to[u].size(); i++) { dfs(to[u][i], x); sum[u] += sum[to[u][i]]; } return; } long long f[100010]; void work(int x) { dfs(0, x); for (int i = 1; i <= n; i++) f[i] = sum[pos[i]] + f[i - 1]; for (int i = 0; i < qy[x].size(); i++) ans[qy[x][i].id] = f[qy[x][i].r] - f[qy[x][i].l - 1]; return; } void solve(int u) { for (int i = 0; i < T[u].ed.size(); i++) S.add(T[u].ed[i], 1); for (int i = 0; i < T[u].vec.size(); i++) { int x = T[u].vec[i]; if (L[x] >= M) continue; for (int j = 0; j < qy[x].size(); j++) ans[qy[x][j].id] += S.query(qy[x][j].r) - S.query(qy[x][j].l - 1); } for (int i = 0; i < to[u].size(); i++) solve(to[u][i]); for (int i = 0; i < T[u].ed.size(); i++) S.add(T[u].ed[i], -1); return; } char st[100010]; int main() { int qnum; scanf("%d %d", &n, &qnum); for (int i = 1; i <= n; i++) { scanf("%s", st); L[i] = strlen(st); pos[i] = insert(st, L[i], i); } bfs(); for (int i = 1; i <= qnum; i++) { int l, r, k; scanf("%d %d %d", &l, &r, &k); Q qry; qry.id = i; qry.l = l; qry.r = r; qy[k].push_back(qry); } for (int i = 1; i <= n; i++) if (L[i] >= M) work(i); solve(0); for (int i = 1; i <= qnum; i++) printf("%I64d\n", ans[i]); return 0; }
11
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-6; const int INF = 0x3fffffff; const long long LINF = INF * 1ll * INF; const double PI = acos(-1.0); using namespace std; int main(void) { long long n; cin >> n; set<long long> ans; for (long long t = 0; t < 63; t++) { long long low = 1, high = 100000000000; while (low <= high) { long long mid = (low + high) >> 1; if (mid * ((1ll << t) - 1) < 0) { high = mid - 1; continue; } long long x = mid; if (x % 2 == 0) x = x / 2 * (x - 1); else x = (x - 1) / 2 * x; long long tmp = x + mid * ((1ll << t) - 1); if (tmp < 0 || tmp > n) high = mid - 1; else if (tmp == n) { if (mid % 2 != 0) ans.insert(mid * (1ll << t)); break; } else low = mid + 1; } } if (ans.size() == 0) puts("-1"); else { for (set<long long>::iterator it = ans.begin(); it != ans.end(); it++) { cout << *it << endl; } } }
5
#include <bits/stdc++.h> using namespace std; struct forward_lists { int L, N; vector<int> next; explicit forward_lists(int L = 0, int N = 0) { assign(L, N); } int rep(int l) const { return l + N; } int head(int l) const { return next[rep(l)]; } bool empty(int l) const { return head(l) == -1; } void init(int l, int n) { next[rep(l)] = n, next[n] = -1; } void clear(int l) { next[rep(l)] = -1; } void exchange(int l, int t) { swap(next[rep(l)], next[rep(t)]); } void push(int l, int n) { insert(rep(l), n); } void insert(int i, int n) { next[n] = next[i], next[i] = n; } void pop(int l) { assert(head(l) != -1), next[rep(l)] = next[head(l)]; } void clear() { fill(begin(next) + N, end(next), -1); } void assign(int L, int N) { this->L = L, this->N = N, next.assign(L + N, -1); } }; struct linked_lists { int L, N; vector<int> next, prev; explicit linked_lists(int L = 0, int N = 0) { assign(L, N); } int rep(int l) const { return l + N; } int head(int l) const { return next[rep(l)]; } int tail(int l) const { return prev[rep(l)]; } bool empty(int l) const { return next[rep(l)] == rep(l); } int add_list() { return next.push_back(rep(L)), prev.push_back(rep(L)), L++; } void clear(int l) { assert(0 <= l && l < L), next[rep(l)] = prev[rep(l)] = rep(l); } void init(int l, int n) { meet(rep(l), n, rep(l)); } void push_front(int l, int n) { meet(rep(l), n, head(l)); } void push_back(int l, int n) { meet(tail(l), n, rep(l)); } void insert_before(int i, int n) { meet(prev[i], n, i); } void insert_after(int i, int n) { meet(i, n, next[i]); } void erase(int n) { meet(prev[n], next[n]); } void pop_front(int l) { meet(rep(l), next[head(l)]); } void pop_back(int l) { meet(prev[tail(l)], rep(l)); } void splice_front(int l, int b) { assert(0 <= l && l < L && 0 <= b && b < L); if (l != b && !empty(b)) meet(tail(b), head(l)), meet(rep(l), head(b)), clear(b); } void splice_back(int l, int b) { assert(0 <= l && l < L && 0 <= b && b < L); if (l != b && !empty(b)) meet(tail(l), head(b)), meet(tail(b), rep(l)), clear(b); } void clear() { iota(begin(next) + N, end(next), N); iota(begin(prev) + N, end(prev), N); } void assign(int L, int N) { this->L = L, this->N = N; next.resize(N + L), prev.resize(N + L), clear(); } private: inline void meet(int u, int v) { next[u] = v, prev[v] = u; } inline void meet(int u, int v, int w) { meet(u, v), meet(v, w); } }; struct micali_vazirani { int V, E = 0; vector<int> adj, off, mate; vector<array<int, 2>> edge; micali_vazirani(int V, const vector<array<int, 2>>& g) : V(V), E(g.size()), adj(2 * E), off(V + 1, 0), mate(V, -1), edge(g) { for (auto [u, v] : edge) off[u + 1]++, off[v + 1]++; partial_sum(begin(off), end(off), begin(off)); vector<int> cur = off; int e = 0; for (auto [u, v] : edge) adj[cur[u]++] = adj[cur[v]++] = e++; } inline int other(int e, int u) const { return u ^ edge[e][0] ^ edge[e][1]; } inline int len(int u) const { return off[u + 1] - off[u]; } void bootstrap() { linked_lists buck(V, V); vector<int> cnt(V, 0); for (int u = 0; u < V; u++) { if (mate[u] == -1 && len(u)) { cnt[u] = len(u); buck.push_back(cnt[u], u); } } int s = 1; while (s < V) { if (buck.empty(s)) { s++; continue; } int u = buck.head(s); buck.erase(u); assert(mate[u] == -1); for (int i = off[u]; i < off[u + 1]; i++) { int e = adj[i], v = other(e, u); if (mate[v] == -1) { mate[u] = v, mate[v] = u; buck.erase(v); break; } } if (mate[u] == -1) continue; for (int w : {u, mate[u]}) { for (int i = off[w]; i < off[w + 1]; i++) { int e = adj[i], t = other(e, w); if (mate[t] == -1) { cnt[t]--; buck.erase(t); if (cnt[t] > 0) { buck.push_back(cnt[t], t); s = min(s, cnt[t]); } } } } } } int max_matching() { int more = 1; init(); while (more && count_matched < V / 2) { reset_search(); more = search(); } return count_matched; } private: static inline constexpr int inf = INT_MAX / 2; static inline constexpr int mv_max_phases = 10; struct link_t { int hi = -1, lo = -1; }; struct node_t { int minlevel = inf, maxlevel = inf, level[2] = {inf, inf}; int vis = -1, bloom = -1, petal = -1; link_t trail[2] = {}; int arc[2] = {}, preds = 0, succs = 0; bool color = 0, erased = 0; }; struct bloom_t { int peak, base, star; }; vector<int> pred, succ; vector<node_t> node; vector<bloom_t> bloom; forward_lists phaselist, bridges; vector<bool> seen; int count_matched, phase, blooms, pending, ddfsid, barrier, exposed[2]; inline void add_phase(int lvl, int u) { phaselist.push(lvl, u), pending++; } inline void add_bridge(int lvl, int e) { bridges.push(lvl, e); } inline int findstar(int u) { int b = node[u].bloom; if (b != -1) { if (node[bloom[b].star].bloom != -1) bloom[b].star = findstar(bloom[b].star); return bloom[b].star; } return u; } void init() { pred.resize(2 * E); succ.resize(2 * E); node.resize(V); phaselist.assign(V, V); bridges.assign(V, E); count_matched = 0; for (int u = 0; u < V; u++) { assert(mate[u] == -1 || mate[mate[u]] == u); count_matched += u < mate[u]; } } void reset_search() { phase = blooms = ddfsid = pending = 0; phaselist.clear(), bridges.clear(); for (int u = 0; u < V; u++) { node[u] = node_t(); if (mate[u] == -1) { add_phase(0, u); node[u].minlevel = node[u].level[0] = 0; } } seen.assign(E, false); bloom.clear(); } int search() { bool done = false; int more, augmentations = 0, good = 0; while (!done && good < mv_max_phases && count_matched < V / 2) { done = MIN(); more = MAX(); good += more > 0; count_matched += more; augmentations += more; phase++; } return augmentations; } void visit_prop(int e, int u, int v, bool parity) { assert(!seen[e] && !node[u].erased && !node[v].erased); if (node[v].minlevel == inf) { node[v].minlevel = node[v].level[!parity] = phase + 1; add_phase(phase + 1, v); } assert(node[v].minlevel == phase + 1 && node[v].level[!parity] == phase + 1); pred[off[v] + node[v].preds++] = u; succ[off[u] + node[u].succs++] = v; seen[e] = true; } void visit_bridge(int e, bool parity) { auto [u, v] = edge[e]; assert(!seen[e] && !node[u].erased && !node[v].erased); assert(node[u].level[parity] < inf && node[v].level[parity] < inf); int tenacity = node[u].level[parity] + node[v].level[parity] + 1; int lvl = tenacity >> 1; assert(phase <= lvl && lvl < V); add_bridge(lvl, e); seen[e] = true; } void bfs_visit(int e, int u, int v) { bool parity = phase % 2; if (node[v].minlevel > phase) { visit_prop(e, u, v, parity); } else if (node[v].level[parity] < inf) { visit_bridge(e, parity); } } bool MIN() { if (pending == 0) return true; bool parity = phase % 2; for (int u = phaselist.head(phase); u != -1; u = phaselist.next[u]) { pending--; if (node[u].erased) continue; if (parity == 0) { for (int i = off[u]; i < off[u + 1]; i++) { int e = adj[i], v = other(e, u); if (mate[u] != v && !seen[e] && !node[v].erased) bfs_visit(e, u, v); } } else if (!node[mate[u]].erased) { for (int i = off[u]; i < off[u + 1]; i++) { int e = adj[i], v = other(e, u); if (mate[u] == v && !seen[e]) bfs_visit(e, u, v); if (mate[u] == v) break; } } } return false; } void push_dfs(int& h, bool c, int v, int w) { assert(node[h].vis != ddfsid || (node[h].color == c && node[h].arc[c] > 1)); node[h].vis = ddfsid; node[h].color = c; node[w].arc[c] = 0; node[w].trail[c].hi = h; node[w].trail[c].lo = v; h = w; } bool pop_dfs(int& h, bool c) { h = node[h].trail[c].hi; int i = node[h].arc[c], s = node[h].preds; return i < s; } void advance_dfs(int& h, bool c) { int& i = node[h].arc[c]; int v = pred[off[h] + i++], w = findstar(v); lazy_erase_predecessors(h, i); push_dfs(h, c, v, w); } bool reverse_dfs(int& h, bool c, int b) { bool ok = false; while (!ok && h != b) { ok = pop_dfs(h, c); } return ok; } bool backtrack_dfs(int& h, bool c, int b) { int x = h, lvl = node[x].minlevel; while ((h == x || node[h].minlevel > lvl) && reverse_dfs(h, c, b)) { do { advance_dfs(h, c); } while (node[h].vis != ddfsid && node[h].minlevel > lvl); } return h != b; } int ddfs(int peak) { int r = findstar(edge[peak][0]), b = findstar(edge[peak][1]); if (r == b) return -1; int red_barrier = r; ++ddfsid, barrier = b; node[r].arc[0] = node[b].arc[1] = 0; node[r].trail[0] = node[b].trail[1] = {}; while (node[r].minlevel != 0 || node[b].minlevel != 0) { if (node[r].minlevel >= node[b].minlevel) { advance_dfs(r, 0); } else { advance_dfs(b, 1); } if (r == b) { if (!backtrack_dfs(b, 1, barrier)) { b = barrier = r; if (!backtrack_dfs(r, 0, red_barrier)) { r = barrier = b; return 0; } } } } assert(r != b); exposed[0] = r, exposed[1] = b; return 1; } int MAX() { int augmentations = 0; for (int peak = bridges.head(phase); peak != -1; peak = bridges.next[peak]) { auto [red, blue] = edge[peak]; if (node[red].erased || node[blue].erased) continue; auto what = ddfs(peak); if (what == 1) { augment_path(peak); augmentations++; } else if (what == 0) { form_bloom(peak); blooms++; } } return augmentations; } void bloom_build_petals() { for (bool c : {0, 1}) { int u = barrier, v = node[u].trail[c].hi; while (v != -1) { node[v].petal = node[u].trail[c].lo; u = v, v = node[u].trail[c].hi; } } } void bloom_dfs_level(int u) { if (u == barrier) return; int lvl = 2 * phase + 1 - node[u].minlevel; assert(!node[u].erased && node[u].bloom == -1 && node[u].vis == ddfsid); assert(lvl < V && node[u].maxlevel == inf && node[u].level[lvl % 2] == inf); node[u].bloom = blooms; node[u].maxlevel = node[u].level[lvl % 2] = lvl; add_phase(lvl, u); for (int i = 0; i < node[u].preds; i++) { int v = pred[off[u] + i], w = findstar(v); if (node[w].bloom == -1 && node[u].color == node[w].color) bloom_dfs_level(w); } if (lvl % 2 == 0) { for (int i = off[u]; i < off[u + 1]; i++) { int e = adj[i], v = other(e, u); if (!node[v].erased && !seen[e] && node[v].level[0] < inf) visit_bridge(e, 0); } } } void form_bloom(int peak) { assert(node[barrier].bloom == -1); bloom.push_back({peak, barrier, barrier}); bloom_build_petals(); bloom_dfs_level(findstar(edge[peak][0])); bloom_dfs_level(findstar(edge[peak][1])); } using path_t = list<int>; inline void add_path(path_t& path, bool back, path_t&& subpath) { path.splice(back ? end(path) : begin(path), subpath); } inline void add_path(path_t& path, bool back, int node) { back ? path.push_back(node) : path.push_front(node); } path_t walk_bloom(int u, bool down) { int B = node[u].bloom; if (node[u].minlevel % 2 == 0) { return walk_down(u, B, down); } else { int t = edge[bloom[B].peak][!node[u].color]; auto path = walk_peak(u, B, !down); add_path(path, down, walk_base(t, B, down)); return path; } } path_t walk_star(int u, int star, bool down) { path_t path; while (u != star) { add_path(path, down, walk_bloom(u, down)); u = bloom[node[u].bloom].base; } return path; } path_t walk_peak(int u, int B, bool down) { bool c = node[u].color; int t = edge[bloom[B].peak][c]; path_t path{u}, top_path; while (node[t].bloom != B) { add_path(top_path, down, walk_bloom(t, down)); t = bloom[node[t].bloom].base; } while (u != t) { int v = node[u].trail[c].lo; add_path(path, !down, walk_star(v, u, down)); u = node[u].trail[c].hi; add_path(path, !down, u); } add_path(path, !down, move(top_path)); return path; } path_t walk_base(int u, int B, bool down) { int base = bloom[B].base; path_t path; while (u != base) { if (node[u].bloom == B) { add_path(path, down, u); u = node[u].petal; } else { add_path(path, down, walk_bloom(u, down)); u = bloom[node[u].bloom].base; } } return path; } path_t walk_down(int u, int B, bool down) { int base = bloom[B].base; path_t path; while (u != base) { if (node[u].bloom == B) { add_path(path, down, u); u = pred[off[u]]; } else { add_path(path, down, walk_bloom(u, down)); u = bloom[node[u].bloom].base; } } return path; } path_t find_path(int top, int c, bool down) { int u = exposed[c], w = findstar(top); path_t path; while (u != w) { int v = node[u].trail[c].lo; add_path(path, !down, u); add_path(path, !down, walk_star(v, u, down)); u = node[u].trail[c].hi; } add_path(path, !down, w); add_path(path, !down, walk_star(top, w, down)); return path; } void augment_path(int peak) { auto path = find_path(edge[peak][0], 0, false); auto rest = find_path(edge[peak][1], 1, true); path.splice(end(path), rest); assert(path.size() == 2u * phase + 2); auto ait = begin(path), bit = next(ait); while (bit != end(path)) { int u = *ait++, v = *bit++; assert(!node[u].erased && !node[v].erased); if (mate[v] != u) mate[u] = v, mate[v] = u; } for (int u : path) { node[u].erased = true; } erase_successors(path); } void erase_successors(path_t& path) { while (!path.empty()) { int u = path.front(); path.pop_front(); for (int i = 0; i < node[u].succs; i++) { int v = succ[off[u] + i]; if (!node[v].erased && lazy_erase_predecessors(v)) { node[v].erased = true; path.push_back(v); } } } } inline bool lazy_erase_predecessors(int v, int i = 0) { int& s = node[v].preds; while (i < s && node[pred[off[v] + i]].erased) { swap(pred[off[v] + i], pred[off[v] + s - 1]), s--; } return i == s; } }; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } vector<bool> used(N + 1); for (int i = 0; i < N; i++) { used[a[i]] = true; } vector<int> unused; for (int i = 1; i <= N; i++) { if (!used[i]) { unused.push_back(i); } } const int NUMS = *max_element(begin(a), end(a)) + 1; vector<int> taken(NUMS); for (int i = 0; i + 1 < N; i++) { if (a[i] == a[i + 1] && a[i] != 0) { taken[a[i]] = true; } } vector<array<int, 2>> edges; for (int i = 0; i + 1 < N; i++) { if (taken[a[i]] || taken[a[i + 1]]) { continue; } if (a[i] == 0 && a[i + 1] == 0) { edges.push_back({i + NUMS, i + 1 + NUMS}); } else if (a[i] == 0 && a[i + 1] != 0) { edges.push_back({i + NUMS, a[i + 1]}); } else if (a[i] != 0 && a[i + 1] == 0) { edges.push_back({a[i], i + 1 + NUMS}); } } micali_vazirani mv(NUMS + N, edges); mv.bootstrap(); mv.max_matching(); for (int k = 0, e = 0, E = edges.size(); e < E; e++) { auto [u, v] = edges[e]; if (mv.mate[u] == v) { if (u >= NUMS && v >= NUMS) { a[u - NUMS] = a[v - NUMS] = unused[k++]; } else if (u >= NUMS) { a[u - NUMS] = v; } else if (v >= NUMS) { a[v - NUMS] = u; } } } for (int i = 0; i < N; i++) { if (a[i] == 0) { a[i] = 1; } } for (int i = 0; i < N; i++) { cout << a[i] << " \n"[i + 1 == N]; } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 31, K = 202; int n, k, a[N]; double dp[K][N][N]; int trans(int l, int r, int pos) { if (pos < l || pos > r) return pos; return l + r - pos; } void solve34() { double p = 2.00 / (n * (n + 1)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dp[0][i][j] = (a[i] > a[j]); for (int i = 1; i <= k; ++i) for (int a = 1; a <= n; ++a) for (int b = 1; b <= n; ++b) for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) { int lasA = trans(l, r, a), lasB = trans(l, r, b); dp[i][a][b] += p * dp[i - 1][lasA][lasB]; } double ans = 0; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) ans += dp[k][i][j]; printf("%.10lf\n", ans); exit(0); } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", a + i); solve34(); }
5
#include <bits/stdc++.h> using namespace std; long long signed pow_(long long signed a, long long signed e, long long signed m = 1000000007) { long long signed r = 1; while (e) { if (e & 1) r = r * a % m; a = a * a % m; e >>= 1; } return r; } long long inv(long long signed a) { return pow_(a, 1000000007 - 2, 1000000007); } long long add(long long signed a, long long signed b) { return (a + b + 1000000007) % 1000000007; } long long mul(long long signed a, long long signed b) { return (1000000007 + a * b % 1000000007) % 1000000007; } long long di(long long signed a, long long signed b) { return mul(a, inv(b)); } long long signed ceil(long long signed a, long long signed b) { return a / b + (a % b != 0); } void print(vector<long long> a) { for (long long i = 0; i < a.size(); i++) cout << a[i] << " "; cout << '\n'; } void print(long long signed a, long long signed b, char k = '\n') { cout << a << " " << b << k; } void print(vector<long long> *a, long long n) { for (long long i = 0; i < n; i++) print(a[i]); } void print(long long *a, long long n) { for (long long i = 0; i < n; i++) cout << a[i] << " "; cout << '\n'; } long long arr[1000005]; long long signed A, B; bool v[1000005]; long long sp[1000005]; vector<long long> PM; void Sieve() { PM.push_back(2); for (long long i = 2; i < 1000005; i += 2) sp[i] = 2; for (long long signed i = 3; i < 1000005; i += 2) { if (!v[i]) { PM.push_back(i); sp[i] = i; for (long long signed j = i; (j * i) < 1000005; j += 2) { if (!v[j * i]) v[j * i] = true, sp[j * i] = i; } } } } long long solve() { cin >> A >> B; bool f = false; long long signed R = 1; for (auto i : PM) { if (B % i == 0) { long long signed X = A; while (X % B == 0) X /= i; R = max(R, X); } } while (A % B == 0) { A /= B; } cout << max(R, A) << '\n'; return 0; } signed main() { Sieve(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } long long gcd(long long a, long long b) { if (b > a) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } long long mexpo(long long a, long long b, long long m) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b = b >> 1; } return res; } vector<int> prime; void seive(long long n) { int lp[n + 1]; for (int i = 2; i <= n; ++i) { if (lp[i] == 0) { lp[i] = i; prime.push_back(i); } for (int j = 0; j < (int)prime.size() && prime[j] <= lp[i] && i * prime[j] <= n; ++j) lp[i * prime[j]] = prime[j]; } } int main() { clock_t begin = clock(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int T; cin >> T; while (T--) { int n; cin >> n; string s; cin >> s; int idx = 0; for (int i = n - 3; i >= 0; i--) { if (s[i] - '0' < s[n - 1] - '0' && s[i] != '0') { swap(s[i], s[n - 1]); } } int count = 0; for (int i = 0; i <= n - 2; i++) { if (s[i] != '0') { count += (s[i] - '0'); count++; } } count += (s[n - 1] - '0'); cout << count << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, vis1[200007], a[200007], vis[200007], k, r, f, i1 = -1; vector<int> v[200007], ans; void DFS(int x) { cout << x << " "; vis[x] = 1e7; for (int i = 0; i < v[x].size(); i++) { if (!vis[v[x][i]]) DFS(v[x][i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; v[i].push_back(a[i]); v[a[i]].push_back(i); } for (int i = 1; i <= n; i++) { int x = i; while (!vis1[x]) { vis1[x] = i; x = a[x]; } if (i != vis1[x]) continue; k++; ans.push_back(x); } for (int i = 0; i < ans.size(); i++) { if (a[ans[i]] == ans[i]) { i1 = i; break; } } if (i1 == -1) { a[ans[0]] = ans[0]; i1 = 0; k++; } for (int i = 0; i < ans.size(); i++) { if (i == i1) continue; a[ans[i]] = ans[i1]; } cout << k - 1 << endl; for (int i = 1; i <= n; i++) cout << a[i] << " "; }
4
#include <bits/stdc++.h> using namespace std; int main() { int i, n, m, j; string s; map<string, int> ma; pair<string, int> p; cin >> n >> m; for (i = 0; i < n; i++) { cin >> s; for (j = 0; j < m; j++) { if (j % 2 == 0) s[j] = 155 - s[j]; } ma[s] = i + 1; } map<string, int>::iterator itr; int arr[n]; i = 1; for (itr = ma.begin(); itr != ma.end(); itr++) { arr[n - i] = (*itr).second; i++; } for (i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, x, y, conl[25], conr[25]; long long a[300010], c[300010][41], fac[300010], inv[300010], ans, s[300010], t[300010]; inline long long C(int n, int m) { if (n < 0 || m < 0 || n > m) return 0; return fac[m] * inv[n] % 998244353 * inv[m - n] % 998244353; } inline long long ksm(long long x, int y) { long long res = 1; while (y) { if (y & 1) res *= x, res %= 998244353; x *= x, x %= 998244353; y >>= 1; } return res; } inline void init() { fac[0] = fac[1] = 1; for (register int i = 2; i <= n; ++i) fac[i] = 1ll * i * fac[i - 1] % 998244353; for (register int i = 0; i <= n; ++i) inv[i] = ksm(fac[i], 998244353 - 2); } int main() { cin >> n >> m; init(); for (register int i = 1; i <= n; ++i) cin >> s[i] >> t[i], a[s[i]]++, a[t[i] + 1]--; for (register int i = 1; i <= m; ++i) cin >> conl[i] >> conr[i]; for (register int i = 1; i <= n; ++i) a[i] += a[i - 1]; for (register int j = 0; j <= 2 * m; ++j) { for (register int i = 1; i <= n; ++i) { if (i >= j && a[i] >= j && a[i] >= i) c[i][j] = C(i - j, a[i] - j); c[i][j] += c[i - 1][j]; } } for (register int i = 0; i < (1 << m); ++i) { long long boul = 1, bour = n; long long num = 0, cnt = 0; map<int, int> vis; for (register int j = 1; j <= m; ++j) { if (i & (1 << (j - 1))) { int l = conl[j], r = conr[j]; if (vis.find(l) == vis.end()) num++, boul = max(boul, s[l]), bour = min(bour, t[l]), vis[l] = 1; if (vis.find(r) == vis.end()) num++, bour = min(bour, t[r]), boul = max(boul, s[r]), vis[r] = 1; cnt++; } } long long add = 0; if (boul <= bour) add = (c[bour][num] - c[boul - 1][num] + 998244353) % 998244353; if (cnt & 1) ans = (ans - add + 998244353) % 998244353; else ans = (ans + add) % 998244353; } cout << ans; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string x, p = "", ma; int y, w = 0, g = 0; cin >> x; char o = 'z'; cin >> y; string str[y]; string ss = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" "zzzzzzzzzzzzzzzzzzzzzzzzzzzz"; string k = ss; for (int i = 0; i < y; i++) cin >> str[i]; for (int i = 0; i < y; i++) { if (str[i][0] == x[0]) { w++; for (int k = 1; k < x.size(); k++) { if (str[i][w] == x[k]) { w++; } } if (w == x.size()) { ss = min(ss, str[i]); } } w = 0; } if (ss == k) { cout << x; } else cout << ss; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n, k, p, nn; vector<int> ans; cin >> n >> k; nn = n; if (n / 2 > k || (n == 1 && k != 0)) { cout << -1 << endl; return 0; } p = k - (n / 2) + 1; ans.push_back(p); ans.push_back(p * 2); nn -= 2; p = 1; while (nn > 0) { if (p != ans[0] && p != ans[1] && p + 1 != ans[0] && p + 1 != ans[1]) { ans.push_back(p); ans.push_back(p + 1); nn -= 2; p += 2; } else { p += 100; } } for (int i = 0; i < n; i++) { if (ans[i] <= 0) { return -1; } cout << ans[i] << " "; } cout << endl; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long N = 205; const long long mod = 1000000007; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; vector<pair<long long, long long>> segs(n); vector<long long> cnt(N, 0); for (long long i = 0; i < n; ++i) { cin >> segs[i].first >> segs[i].second; ++cnt[segs[i].first]; --cnt[segs[i].second + 1]; } for (long long i = 0; i + 1 < N; ++i) { cnt[i + 1] += cnt[i]; } vector<long long> ans(n); for (long long i = 0; i < N; ++i) { while (cnt[i] > k) { long long pos = -1; for (long long p = 0; p < n; ++p) { if (!ans[p] && (segs[p].first <= i && i <= segs[p].second) && (pos == -1 || segs[p].second > segs[pos].second)) { pos = p; } } assert(pos != -1); for (long long j = segs[pos].first; j <= segs[pos].second; ++j) { --cnt[j]; } ans[pos] = 1; } } cout << accumulate(ans.begin(), ans.end(), 0LL) << endl; for (long long i = 0; i < n; ++i) { if (ans[i]) cout << i + 1 << " "; } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAX = 1e5 + 5; int n, s, h, m; vector<pair<int, int> > a; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin >> n >> s; for (int i = 0; i < n; i++) { cin >> h >> m; a.push_back(make_pair(h, m)); } bool done = false; if (a[0].first * 60 + a[0].second >= s + 1) cout << "0 0"; else { for (int i = 0; i < n - 1; i++) { int gap = (a[i + 1].first - a[i].first) * 60 + (a[i + 1].second - a[i].second); if (gap >= 2 * (s + 1)) { done = true; if (a[i].second + s + 1 >= 60) { int mins = s + 1 - (60 - a[i].second); if (mins == 60) cout << a[i].first + 2 << " 0"; else cout << a[i].first + 1 << " " << mins; } else cout << a[i].first << " " << a[i].second + s + 1; break; } } if (!done) { if (a[n - 1].second + s + 1 >= 60) { int mins = s + 1 - (60 - a[n - 1].second); if (mins == 60) cout << a[n - 1].first + 2 << " 0"; else cout << a[n - 1].first + 1 << " " << mins; } else cout << a[n - 1].first << " " << a[n - 1].second + s + 1; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k, q; int x[200002] = {0}; cin >> n >> k >> q; int a, b; while (n--) { cin >> a >> b; x[a]++; x[b + 1]--; } for (int i = 1; i <= 200000; i++) { x[i] += x[i - 1]; } for (int i = 1; i <= 200000; i++) { if (x[i] < k) x[i] = 0; else x[i] = 1; } for (int i = 1; i <= 200000; i++) x[i] += x[i - 1]; while (q--) { cin >> a >> b; cout << (x[b] - x[a - 1]) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, win; long double arr[20][20], mem[(1 << 18) + 5]; long double solve(int mask) { int alive = __builtin_popcount(mask); if (alive == n) return 1; if (mem[mask] != -1) return mem[mask]; long double u = alive * (alive + 1) / 2.0, ret = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if ((mask & (1 << i)) && !(mask & (1 << j))) ret += arr[i][j] / u * solve(mask | (1 << j)); return mem[mask] = ret; } int main() { for (int i = 0; i < (1 << 18) + 5; i++) mem[i] = -1; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> arr[i][j]; for (int i = 0; i < n; i++) cout << fixed << setprecision(10) << solve((1 << i)) << ' '; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k, sum = 0, i = 0; cin >> n >> k; if (240 - k > 4) { for (i = 0;; i += 5) { sum += i + 5; if (sum > 240 - k) { i--; break; } } } else { cout << "0"; return 0; } if (i / 5 + 1 > n) cout << n; else cout << i / 5 + 1; }
0
#include <bits/stdc++.h> using namespace std; long long int ans[200009]; void solve() { long long int n, m; cin >> n >> m; long long int a[m + 2]; for (long long int i = 1; i <= m; i++) { cin >> a[i]; } long long int pos[m + 2]; long long int q = n; for (long long int i = m; i > 0; i--) { if (q <= 0) { cout << -1; return; } long long int x = n - a[i] + 1; q = min(q, x); pos[i] = q; q--; } q = 1; for (long long int i = 1; i <= m; i++) { long long int x = min(q, pos[i]); pos[i] = x; q = x + a[i]; } if (q <= n) { cout << -1; return; } for (long long int i = 1; i <= m; i++) cout << pos[i] << ' '; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; for (long long int i = 0; i < t; i++) { solve(); } }
5
#include <bits/stdc++.h> using namespace std; string s, p; int main() { cin >> s; int n = s.size(), i, j; int m, cnt = 0; cin >> m; int t1 = 0, t2 = 0; for (i = 0; i < m; i++) { cin >> p; j = 0; while (j < n) { t1 = 0; t2 = 0; while (s[j] == p[0] || s[j] == p[1]) { if (s[j] == p[0]) { t1++; } else { t2++; } j++; } cnt += min(t1, t2); j++; } } cout << cnt; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, ans; cin >> n; vector<long long int> v(n); long long int diff = 0; set<long long int> st; for (long long int(i) = (0); (i) < (n); (i)++) { cin >> v[i]; } for (long long int(i) = (0); (i) < (n); (i)++) { if (v[i] == -1) { if (v[i - 1] != -1 && i - 1 >= 0) st.insert(v[i - 1]); if (v[i + 1] != -1 && i + 1 < n) st.insert(v[i + 1]); } } if (st.size() == 0) { cout << 0 << " " << 0 << endl; } else { set<long long int>::iterator it; it = st.end(); it--; ans = (*(st.begin()) + *(it)) / 2; for (long long int(i) = (0); (i) < (n); (i)++) { if (v[i] == -1) v[i] = ans; } for (long long int(i) = (1); (i) < (n); (i)++) { diff = max(diff, abs(v[i] - v[i - 1])); } cout << diff << " " << ans << endl; } } return 0; }
3
#include <bits/stdc++.h> char str[5004]; int main() { scanf("%s", str); int n = strlen(str); int pb = -1; int answer = 0; for (int _k = (0), _n = (n - 4), i = _k; i <= _n; ++i) { if (strncmp(str + i, "bear", 4) == 0) { answer += (i - pb) * (n - i - 3); pb = i; i += 3; } } printf("%d", answer); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 101; const int RAN = 10001; int n, a[MAX]; int sum[MAX], nx[MAX]; int best[2][RAN]; bool dup[2][RAN]; int ans() { int res = 1; sort(a + 1, a + n + 1); for (int i = 0, j = 0; i <= n + 1 && j <= n; i++) { if (i <= n && a[i] == a[j]) continue; while (j < i) nx[j++] = i; } fill(best[0], best[0] + RAN, -1); fill(dup[0], dup[0] + RAN, 0); best[0][0] = 0; for (int k = (1); k < (n + 1); ++k) { fill(best[k & 1], best[k & 1] + RAN, -1); fill(dup[k & 1], dup[k & 1] + RAN, 0); for (int j = (0); j < (RAN); ++j) { if (best[(k - 1) & 1][j] < 0) continue; for (int x = best[(k - 1) & 1][j] + 1; x <= n; x = nx[x]) { int& val = best[k & 1][j + a[x]]; if (val >= 0 || dup[(k - 1) & 1][j]) dup[k & 1][j + a[x]] = 1; if (val < 0 || val > x) val = x; } } for (int i = (1); i < (MAX); ++i) if (sum[i] >= k && !dup[k & 1][i * k]) if (res < k) res = k; } int numtype = 0; for (int i = (1); i < (MAX); ++i) if (sum[i] > 0) numtype++; if (numtype == 2) res = n; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a[0] = 0; for (int i = (0); i < (n); ++i) { cin >> a[i + 1]; sum[a[i + 1]]++; } cout << ans() << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { char arr[10][10]; int k; for (int r = 0; r < 8; r++) { for (int c = 0; c < 8; c++) { cin >> arr[r][c]; } } int mn = 100, mx = 100; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (arr[i][j] == 'W') { int cnt = 0, f = 0; for (int k = i; k >= 0; k--) { if (arr[k][j] == 'B') { f = 1; break; } cnt++; } if (!f) mn = min(mn, cnt); } if (arr[i][j] == 'B') { int cnt = 0, f = 0; for (int k = i; k < 8; k++) { if (arr[k][j] == 'W') { f = 1; break; } cnt++; } if (!f) mx = min(mx, cnt); } } } if (mn == mx || mn < mx) { cout << "A\n"; } else cout << "B\n"; }
2
#include <bits/stdc++.h> using namespace std; struct UF { vector<int> e; UF(int n) : e(n, -1) {} bool sameSet(int a, int b) { return find(a) == find(b); } int size(int x) { return -e[find(x)]; } int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); } bool join(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; return true; } }; template <class T> struct RMQ { vector<vector<T>> jmp; RMQ(const vector<T>& V) : jmp(1, V) { for (int pw = 1, k = 1; pw * 2 <= (int)(V).size(); pw *= 2, ++k) { jmp.emplace_back((int)(V).size() - pw * 2 + 1); for (int j = 0; j < ((int)(jmp[k]).size()); ++j) jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]); } } T query(int a, int b) { assert(a < b); int dep = 31 - __builtin_clz(b - a); return min(jmp[dep][a], jmp[dep][b - (1 << dep)]); } }; struct LCA { int T = 0; vector<int> time, path, ret; RMQ<int> rmq; LCA(vector<vector<int>>& C) : time((int)(C).size()), rmq((dfs(C, 0, -1), ret)) {} void dfs(vector<vector<int>>& C, int v, int par) { time[v] = T++; for (int y : C[v]) if (y != par) { path.push_back(v), ret.push_back(time[v]); dfs(C, y, v); } } int lca(int a, int b) { if (a == b) return a; tie(a, b) = minmax(time[a], time[b]); return path[rmq.query(a, b)]; } }; pair<int, int> makeEdge(int a, int b) { return pair<int, int>(min(a, b), max(a, b)); } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int n, m; cin >> n >> m; UF uf(n); vector<vector<int>> edges(n); int cnt = 0; for (int _ = 0; _ < (m); ++_) { int u, v; cin >> u >> v; u--; v--; if (!uf.sameSet(u, v)) { uf.join(u, v); edges[u].push_back(v); edges[v].push_back(u); cnt++; } } assert(cnt == n - 1); vector<int> dad(n, -2); dad[0] = -1; queue<int> q; q.push(0); while ((int)(q).size()) { int u = q.front(); q.pop(); for (auto& v : edges[u]) if (dad[v] == -2) { dad[v] = u; q.push(v); } } vector<vector<int>> paths; set<pair<int, int>> activeEdges; LCA lca(edges); int queries; cin >> queries; for (int _ = 0; _ < (queries); ++_) { int a, b; cin >> a >> b; a--; b--; int p = lca.lca(a, b); vector<int> tempPath(1, a); while (a != p) { int temp = dad[a]; pair<int, int> e = makeEdge(a, temp); if (activeEdges.count(e)) activeEdges.erase(e); else activeEdges.insert(e); a = temp; tempPath.push_back(a); } vector<int> tempPath2(1, b); if (p == b) tempPath2.pop_back(); while (b != p) { int temp = dad[b]; pair<int, int> e = makeEdge(b, temp); if (activeEdges.count(e)) activeEdges.erase(e); else activeEdges.insert(e); b = temp; if (b != p) tempPath2.push_back(b); } reverse(begin(tempPath2), end(tempPath2)); for (auto& elem : tempPath2) tempPath.push_back(elem); paths.push_back(tempPath); } if ((int)(activeEdges).size()) { cout << "NO" << endl; vector<int> valency(n); vector<vector<int>> edgesToUse(n); for (auto& e : activeEdges) { valency[e.first]++; valency[e.second]++; edgesToUse[e.first].push_back(e.second); edgesToUse[e.second].push_back(e.first); } for (int i = 0; i < (n); ++i) if (valency[i] == 1) { q.push(i); } int ans = 0; while ((int)(q).size()) { int u = q.front(); q.pop(); if (valency[u] != 1) continue; ans++; while ((int)(edgesToUse[u]).size()) { int v = edgesToUse[u].back(); edgesToUse[u].pop_back(); pair<int, int> e = makeEdge(u, v); if (activeEdges.count(e)) { activeEdges.erase(e); valency[u]--; valency[v]--; if (valency[u] == 1) q.push(u); u = v; } } } cout << ans << endl; } else { cout << "YES" << endl; for (auto& v : paths) { cout << (int)(v).size() << endl; for (auto& elem : v) cout << elem + 1 << " "; cout << endl; } } }
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; const int M = 26; const long long mod = 1e9 + 7; const long long MOD = 998244353; const int P = 1336; const long double eps = 0.000000001; const long long inf = 1e18 + 7; mt19937 gen(time(0)); vector<int> g[N]; int sz[N], a[N]; int ask(int v, int u) { cout << "? " << v + 1 << " " << u + 1 << endl; int c; cin >> c; if (c == -1) exit(0); c--; return c; } void answ(int v) { cout << "! " << v + 1 << endl; exit(0); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); sz[x]++; sz[y]++; } for (int i = 0; i < n; i++) { a[i] = 1; } int can = n / 2; while (can--) { int v = -1, u = -1; for (int i = 0; i < n; i++) { if (sz[i] == 1) { if (v == -1) v = i; else if (u == -1) u = i; } } for (auto to : g[v]) { sz[to]--; } for (auto to : g[u]) { sz[to]--; } sz[v]--; sz[u]--; int c = ask(v, u); if (c == v) answ(v); if (c == u) answ(u); a[v] = 0; a[u] = 0; } for (int i = 0; i < n; i++) { if (a[i]) answ(i); } }
5
#include <bits/stdc++.h> using namespace std; string str; priority_queue<pair<int, pair<int, int> > > pq; int main() { int n, a, b, k; cin >> n >> a >> b >> k; cin >> str; str = " " + str; int j; int cate_pune = 0; for (int i = 1; i <= n; i = j + 1) { j = i; if (str[i] == '0') { while (j + 1 <= n && str[j + 1] == '0') ++j; if (j - i + 1 >= b) { cate_pune += (j - i + 1) / b; pq.push({-(j - i + 1) / b, {i, j}}); } } } vector<int> ans; while (cate_pune >= a) { int cnt = -pq.top().first; int x = pq.top().second.first; int y = pq.top().second.second; pq.pop(); ans.push_back(x + b - 1); if ((y - x + 1) < 2 * b) cate_pune--; else { cate_pune--; pq.push({-(cnt - 1), {x + b, y}}); } } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " \n"[i + 1 == ans.size()]; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n]; long long ma = -999; long long mi = 10000000000; for (int i = 0; i < n; i++) { cin >> a[i]; ma = max(ma, a[i]); mi = min(mi, a[i]); } long long t1 = 0, t2 = 0; for (int i = 0; i < n; i++) { t1 += (a[i] == mi); t2 += (a[i] == ma); } cout << ma - mi << " " << (mi == ma ? n * (n - 1) / 2 : t1 * t2) << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; bool A[2000][2000]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { char c = getchar(); while (c != '0' && c != '1') c = getchar(); A[i][j] = (c == '1'); } int Ans = 0; bool R[2000], C[2000], D[2000]; memset(R, 0, sizeof(R)); memset(C, 0, sizeof(C)); memset(D, 0, sizeof(D)); for (int i = 0; i < N; i++) for (int j = N - 1; j > i; j--) if (A[i][j] ^ R[i] ^ C[j]) { R[i] = !R[i]; C[j] = !C[j]; D[i] = !D[i]; D[j] = !D[j]; Ans++; } memset(R, 0, sizeof(R)); memset(C, 0, sizeof(C)); for (int i = N - 1; i >= 0; i--) for (int j = 0; j < i; j++) if (A[i][j] ^ R[i] ^ C[j]) { R[i] = !R[i]; C[j] = !C[j]; D[i] = !D[i]; D[j] = !D[j]; Ans++; } for (int i = 0; i < N; i++) if (A[i][i] ^ D[i]) Ans++; cout << Ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using u64 = unsigned long long; using u32 = unsigned; using vi = vector<int>; using vl = vector<ll>; const int MOD = 1e9 + 7; const int MX = 1e5 + 10; const ll BIG = 1e18; using pi = pair<int, int>; void setIO(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if ((int)(name).size()) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } int main() { setIO(); int T; cin >> T; while (T--) { int n; cin >> n; vi a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; sort(begin(a), end(a)); ll second = 0; for (int i = (0); i < (n); ++i) { second += a[i]; } if (second % n == 0) cout << "0\n"; else cout << "1\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; char s[N]; int sum[N], cnt[N]; int main() { cin >> (s + 1); int n = strlen(s + 1); for (int i = (1); i < (n + 1); i++) { cnt[i] = cnt[i - 1] + (s[i] == '?'); sum[i] = sum[i - 1]; if (s[i] == '(' || s[i] == '?') sum[i]++; else sum[i]--; } int ans = 0; for (int i = 1; i <= n; i++) { if (s[i] == ')') continue; int l = 0, cc = 0; for (int j = i; j <= n; j++) { if (s[j] == ')') { l--; if (l < 0 && cc > 0) l += 2, cc--; else if (l < 0) break; } else if (s[j] == '(') l++; else { l--; cc++; if (l < 0) l += 2, cc--; } if (l == 0) ans++; } } cout << ans << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { string a[100000], b[100000]; cin >> a[0] >> b[0]; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } cout << a[0] << " " << b[0] << endl; for (int i = 1; i <= n; i++) { if (a[i] == a[i - 1]) { a[i] = b[i - 1]; cout << a[i] << " " << b[i] << endl; } else if (a[i] == b[i - 1]) { a[i] = a[i - 1]; cout << a[i] << " " << b[i] << endl; } } }
0
#include <bits/stdc++.h> using namespace std; const int N = 300000 + 10; int n, d; int r[N], v[N]; int mx, ans = 1; int read() { int sum = 0; char c = getchar(), flag = ' '; while (c < '0' || c > '9') flag = c, c = getchar(); while (c >= '0' && c <= '9') sum = sum * 10 + c - '0', c = getchar(); if (flag == '-') sum *= -1; return sum; } int main() { n = read(), d = read(); for (int i = 1; i <= n; i++) r[i] = read(); for (int i = 1; i <= n; i++) v[i] = read(); mx = r[d] + v[1]; for (int i = 1, j = n; j > 1; i++, j--) while (i <= n && (i == d || r[i] + v[j] > mx)) ans += (i != d), i++; if (ans) cout << ans; else puts("fuck"); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> string tostr(T a) { stringstream ss; ss << a; return ss.str(); } template <typename C> void PRINTALL(const C& arr, string delim = " ") { for (typename C::const_iterator it = arr.begin(); it != arr.end(); it++) cout << *(it) << " "; cout << endl; } long long int mod = 1000000007; long long int modpow(int k) { if (!k) return 1; long long int a = 2; for (int i = 1; i < k; i++) { a = (a * 2) % mod; } return a; } int main() { int n; string s; cin >> s; n = s.length(); int count = 0; long long int sum = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == '1') { sum = (sum + modpow(2 * n - i - 2)) % mod; count++; } } cout << sum << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; double dp[2][5010]; int main() { for (int i = 0; i < 2; i++) for (int j = 0; j < 5010; j++) dp[i][j] = 0.0; int n, t; scanf("%d %d", &n, &t); vector<pair<double, int> > v; for (int i = 0; i < n; i++) { double a; int tx; scanf("%lf %d", &a, &tx); a /= 100.0; v.push_back(make_pair(a, tx)); } for (int m = n - 1; m >= 0; m--) { double p = v[m].first; double q = (1.0 - p); double qq = 1; int lo = t - 1, hi = t - 1; int tt = v[m].second; for (int i = 0; i < tt - 1; i++) qq *= q; double sum = (tt == 1 ? 1 : p) * (dp[(m + 1) & 1][lo + 1] + 1); bool ja = (tt == 1); while (1) { dp[m & 1][lo] = sum; sum *= q; lo--; if (lo < 0) break; sum += p * (dp[(m + 1) & 1][lo + 1] + 1); if (!ja) { if (hi - lo + 1 == tt) { ja = true; sum -= (qq * p * (dp[(m + 1) & 1][lo + tt] + 1)); sum += (qq * (dp[(m + 1) & 1][lo + tt] + 1)); } } else { sum -= (q * qq * (dp[(m + 1) & 1][lo + tt + 1] + 1)); sum -= (qq * p * (dp[(m + 1) & 1][lo + tt] + 1)); sum += (qq * (dp[(m + 1) & 1][lo + tt] + 1)); } } } printf("%.10lf\n", dp[0][0]); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, a[4][2], b[4][2], x[4], y[4], xx[4], yy[4], t1, t2, ans, res[4], tmp1[4], tmp2[4]; bool flag[4]; void dfs(int step) { if (step >= 4) { int sum = 0; for (int i = 0; i < 4; i++) sum = max(sum, abs(a[i][0] - b[res[i]][0]) + abs(a[i][1] - b[res[i]][1])); if (sum < ans) { ans = sum; for (int i = 0; i < 4; i++) xx[i] = b[res[i]][0], yy[i] = b[res[i]][1]; } return; } for (int i = 0; i < 4; i++) { if (flag[i]) continue; if (a[step][0] != b[i][0] && a[step][1] != b[i][1]) continue; flag[i] = 1; res[step] = i; dfs(step + 1); flag[i] = 0; } } int main() { scanf("%d", &n); while (n--) { for (int i = 0; i < 4; i++) scanf("%d%d", &a[i][0], &a[i][1]); ans = 1000000000; for (int s = 0; s < (1 << 4); s++) { t1 = t2 = 0; for (int i = 0; i < 4; i++) { if ((1 << i) & s) y[t2++] = a[i][1]; else x[t1++] = a[i][0]; } sort(x, x + t1); sort(y, y + t2); for (int i = 0; i < t1 - 1; i++) if (x[i] == x[i + 1]) x[i] = 1000000000; for (int i = 0; i < t2 - 1; i++) if (y[i] == y[i + 1]) y[i] = 1000000000; sort(x, x + t1); sort(y, y + t2); while (t1 && x[t1 - 1] == 1000000000) t1--; while (t2 && y[t2 - 1] == 1000000000) t2--; if (t1 > 2 || t2 > 2) continue; if (t1 <= 1 && t2 <= 1) continue; bool rev = 0; if (t1 < t2) { swap(t1, t2); for (int i = 0; i < 4; i++) swap(x[i], y[i]); rev = 1; } if (t1 == 2 && t2 == 2) { int xa = x[0], xb = x[1], ya = y[0], yb = y[1]; if (xb - xa != yb - ya) continue; b[0][rev] = xa, b[0][1 ^ rev] = ya; b[1][rev] = xa, b[1][1 ^ rev] = yb; b[2][rev] = xb, b[2][1 ^ rev] = ya; b[3][rev] = xb, b[3][1 ^ rev] = yb; dfs(0); } if (t1 == 2 && t2 == 1) { int xa = x[0], xb = x[1], ya = y[0]; b[0][rev] = xa, b[0][1 ^ rev] = ya; b[1][rev] = xa, b[1][1 ^ rev] = ya + xb - xa; b[2][rev] = xb, b[2][1 ^ rev] = ya; b[3][rev] = xb, b[3][1 ^ rev] = ya + xb - xa; dfs(0); b[0][rev] = xa, b[0][1 ^ rev] = ya; b[1][rev] = xa, b[1][1 ^ rev] = ya - xb + xa; b[2][rev] = xb, b[2][1 ^ rev] = ya; b[3][rev] = xb, b[3][1 ^ rev] = ya - xb + xa; dfs(0); } if (t1 == 2 && t2 == 0) { int xa = x[0], xb = x[1]; int c1 = 0, c2 = 0; for (int j = 0; j < 4; j++) { if (a[j][rev] == xa) tmp1[c1++] = a[j][1 ^ rev]; else tmp2[c2++] = a[j][1 ^ rev]; } if (c1 != 2 || c2 != 2) continue; if (tmp1[0] > tmp1[1]) swap(tmp1[0], tmp1[1]); if (tmp2[0] > tmp2[1]) swap(tmp2[0], tmp2[1]); y[0] = tmp1[0], y[1] = tmp1[1] - x[1] + x[0]; y[2] = tmp2[0], y[3] = tmp2[1] - x[1] + x[0]; sort(y, y + 4); int ya = (y[0] + y[3]) / 2; int yb = ya + x[1] - x[0]; b[0][rev] = xa, b[0][1 ^ rev] = ya; b[1][rev] = xa, b[1][1 ^ rev] = yb; b[2][rev] = xb, b[2][1 ^ rev] = ya; b[3][rev] = xb, b[3][1 ^ rev] = yb; dfs(0); } } if (ans == 1000000000) printf("-1\n"); else { printf("%d\n", ans); for (int i = 0; i < 4; i++) printf("%d %d\n", xx[i], yy[i]); } } return 0; }
11
#include <bits/stdc++.h> using namespace std; bool sortin(const pair<long long int, long long int> &e, const pair<long long int, long long int> &f) { return (e.first < f.first); } long long int a, b, c, i, j, k, l, m, n, p, q, r, x, y, z, ts, mn = 10e17, mod = 10e8 + 7; long long int ar[250002], br[250002], xr[250002]; int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; cin >> n; l = log2(n); for (long long int i = l + 1; i >= 0; i--) { p = pow(2, i); q = (p / 2) * (p - 1); if (n % q == 0) { cout << q; return 0; } } cout << 1; }
1
#include <bits/stdc++.h> using namespace std; int main() { long a, S, i; for (cin >> a, i = 0, S = 0; i < 1; S += (6 * a * (a - 1) + 1), cout << S, ++i) ; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int n, x, y, cnt = 0; cin >> n; map<pair<int, int>, int> mpxy; map<int, int> mpx; map<int, int> mpy; for (int i = 0; i < n; i++) { cin >> x >> y; mpx[x]++; mpy[y]++; mpxy[{x, y}]++; } for (auto i : mpx) { long long int i1 = i.second; if (i1 % 2 == 0) cnt += (i1 / 2) * (i1 - 1); else cnt += (i1) * ((i1 - 1) / 2); } for (auto i : mpy) { long long int i1 = i.second; if (i1 % 2 == 0) cnt += (i1 / 2) * (i1 - 1); else cnt += (i1) * ((i1 - 1) / 2); } for (auto i : mpxy) { long long int i1 = i.second; if (i1 % 2 == 0) cnt -= (i1 / 2) * (i1 - 1); else cnt -= (i1) * ((i1 - 1) / 2); } cout << cnt << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, ans; int main() { scanf("%d:%d", &n, &m); while (1) { if (n % 10 == m / 10 && n / 10 == m % 10) break; ans++; m++; if (m == 60) { m = 0; n++; } if (n == 24) n = 0; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; long long p; cin >> n >> p; vector<pair<long long, int> > ar; vector<long long> ans(n); for (int i = 0; i < n; i++) { long long x; cin >> x; ar.push_back(make_pair(x, i)); } sort(ar.begin(), ar.end()); set<int> waiting; int q_smallest = n + 1; queue<int> q; long long time = 0; int turn; for (int i = 0; i < n || waiting.size() != 0 || q.size() != 0;) { if (q.size() == 0 && waiting.size() == 0) { q.push(ar[i].second); q_smallest = ar[i].second; time = ar[i++].first; } if (q.size() != 0) { turn = q.front(); if (q_smallest == q.front()) q_smallest = n + 1; q.pop(); } else if (waiting.size() != 0) { turn = *(waiting.begin()); waiting.erase(waiting.begin()); } time += p; ans[turn] = time; while (i < n && ar[i].first <= time) { if ((q.size() == 0 || (q.size() > 0 && q_smallest > ar[i].second)) && turn > ar[i].second) { q.push(ar[i].second); q_smallest = ar[i].second; } else { waiting.insert(ar[i].second); } i++; } } for (int i = 0; i < n; i++) cout << ans[i] << " "; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int a, b; cin >> a >> b; int p1 = 0, p2 = 0, d = 0; if (a < b) { p1 = a; p2 = 6 - b + 1; for (int i = a + 1; i < b; i++) { if (abs(i - a) < abs(i - b)) p1++; else if (abs(i - a) > abs(i - b)) p2++; else d++; } } else if (b < a) { p2 = b; p1 = 6 - a + 1; for (int i = b + 1; i < a; i++) { if (abs(i - a) < abs(i - b)) p1++; else if (abs(i - a) > abs(i - b)) p2++; else d++; } } else { cout << 0 << " " << 6 << " " << 0; return 0; } cout << p1 << " " << d << " " << p2; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[2000]; int n; inline bool check() { int c = 0; for (int i = 0; i < n; ++i) if (a[i] + 1 == a[i + 1]) ++c; if (c == n) return true; else return false; } inline void push() { for (int i = 0; i <= n; ++i) { if (i % 2 == 0) { if (a[i] == n) a[i] = 0; else a[i] = a[i] + 1; } else { if (a[i] == 0) a[i] = n; else a[i] = a[i] - 1; } } } int main() { scanf("%d", &n); --n; for (int i = 0; i <= n; ++i) { scanf("%d", &a[i]); if (i % 2 == 0) { if (a[i] == n) a[i] = 0; else a[i] = a[i] + 1; } else { if (a[i] == 0) a[i] = n; else a[i] = a[i] - 1; } } if (check()) { puts("Yes"); return 0; } else { for (int i = 0; i < n; ++i) { push(); if (check()) { puts("Yes"); return 0; } } } puts("No"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int lg(long long n) { assert(n > 0); int ans = -1; while (n) { ans++; n >>= 1; } return ans; } pair<long long, long long> operator-(const pair<long long, long long> &a, const pair<long long, long long> &b) { return make_pair(a.first - b.first, a.second - b.second); } long long cross(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.first * b.second - b.first * a.second; } void solve() { int n; cin >> n; vector<pair<long long, long long> > cord(n); for (int i = 0; i < n; i++) cin >> cord[i].first >> cord[i].second; string op; cin >> op; int firs = 0; for (int i = 1; i < n; i++) { if (cord[i].first < cord[firs].first) { firs = i; } } vector<int> ans({firs}); vector<int> vis(n); vis[firs] = 1; for (int step = 0; step < n - 2; step++) { bool needright = op[step] == 'R'; int choose = 0; while (vis[choose]) choose++; assert(choose < n); int lst = ans.back(); for (int i = choose + 1; i < n; i++) { if (vis[i]) continue; bool dir = cross(cord[i] - cord[choose], cord[choose] - cord[lst]) < 0; if (needright == dir) choose = i; } vis[choose] = 1; ans.push_back(choose); } for (int i = 0; i < n; i++) { if (!vis[i]) { ans.push_back(i); break; } } for (int i = 0; i < n; i++) cout << ans[i] + 1 << " \n"[i == n - 1]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = (1e6) + 7; const int inf = (1e9) + 7; const long long LLinf = (1e18) + 7; int main() { int n; scanf("%d", &n); if (n < 4) { printf("NO"); return 0; } if (n % 2 == 0) { printf("YES\n4 * 3 = 12\n"); printf("2 * 12 = 24\n"); for (int i = 5; i <= n; i += 2) printf("%d - %d = 1\n", i + 1, i); for (int i = 0; i < n / 2 - 1; i++) printf("1 * 24 = 24\n"); } else { printf("YES\n 5 * 4 = 20\n"); printf("20 + 2 = 22\n"); printf("22 + 3 = 25\n"); printf("25 - 1 = 24\n"); for (int i = 6; i <= n; i += 2) printf("%d - %d = 1\n", i + 1, i); for (int i = 0; i < n / 2 - 2; i++) printf("1 * 24 = 24\n"); } }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long maxn = 1e6 + 10; long long a[maxn]; long long d1 = 0, d2 = 0; int main() { ios_base::sync_with_stdio(0); long long n, m; cin >> n >> m; long long temp = n - 1; while (temp) { temp /= 7; d1++; } temp = m - 1; while (temp) { temp /= 7; d2++; } if (n == 1) d1 = 1; if (m == 1) d2 = 1; if (d1 + d2 > 7) { cout << 0; return 0; } vector<int> v; for (int i = 0; i < 7; i++) v.push_back(i); set<vector<int> > s; long long ans = 0; map<pair<int, int>, int> mp; do { long long hour = 0; for (int i = 0; i < d1; i++) { hour *= 7; hour += v[i]; } long long minu = 0; for (int i = d1; i < d1 + d2; i++) { minu *= 7; minu += v[i]; } if (hour < n && minu < m && !mp[{hour, minu}]) { ans++; mp[{hour, minu}] = 1; } } while (next_permutation(v.begin(), v.end())); cout << ans; }
4
#include <bits/stdc++.h> using namespace std; const int N = 505; char a[N][N]; int Pref[N][N]; int Get(int i1, int j1, int i2, int j2) { i1++; j1++; i2--; j2--; if (i1 > i2 || j1 > j2) return 0; return Pref[i2][j2] - Pref[i1 - 1][j2] - Pref[i2][j1 - 1] + Pref[i1 - 1][j1 - 1]; } int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1); for (int i = 2; i < n; i++) for (int j = 2; j < m; j++) if (a[i - 1][j] == '1' && a[i][j - 1] == '1' && a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '1') Pref[i][j]++; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) Pref[i][j] += Pref[i - 1][j] + Pref[i][j - 1] - Pref[i - 1][j - 1]; long long Ans = 0; for (int i1 = 1; i1 <= n; i1++) { for (int i2 = i1; i2 <= n; i2++) { for (int j1 = 1, j2 = 1; j1 <= m; j1++) { while (j2 <= m && Get(i1, j1, i2, j2) < k) j2++; Ans += (long long)(m - j2 + 1); } } } cout << Ans; }
6
#include <bits/stdc++.h> using namespace std; long long n, m, sum[501000], now, minv = 0x3f3f3f3f, p = 1; pair<long long, long long> nums[501000]; signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; ++i) { scanf("%lld", &nums[i].first); nums[i].second = i; } scanf("%lld", &m); sort(nums + 1, nums + 1 + n); for (long long i = 1; i <= n; ++i) sum[i] = sum[i - 1] + nums[i].first; for (long long i = 1; i <= m; ++i) now += nums[i].first * (i - 1) - sum[i - 1]; minv = now; for (long long i = 2; i < n - m + 2; ++i) { now += (m - 1) * (nums[i + m - 1].first + nums[i - 1].first) - (sum[i + m - 2] - sum[i - 1]) * 2; if (now < minv) { minv = now; p = i; } } for (long long i = p; i < p + m; ++i) printf("%lld ", nums[i].second); return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,fma") using std::cin; using std::cout; using std::queue; using std::vector; using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; using vpii = vector<pii>; using vpll = vector<pll>; const auto ready = []() { cin.tie(0); cout.tie(0); std::ios_base::sync_with_stdio(false); return true; }(); int main() { int n, k; cin >> n >> k; vi vec(n); for (int i = 0; i < n; ++i) cin >> vec[i]; int ans = 0; int unique = 0; vi used(1000010, 0); int ptr = 0; auto check = [&](int dl) { for (int i = 0; i < 1000010; ++i) used[i] = 0; bool ok = false; unique = 0; for (int i = 0; i < dl; ++i) { if (!used[vec[i]]) ++unique; ++used[vec[i]]; } if (unique <= k) ok = true, ptr = 0; for (int i = 1; i <= (n - dl); ++i) { int hello = vec[i - 1 + dl]; int goodbye = vec[i - 1]; used[hello]++; unique += (used[hello] == 1); used[goodbye]--; unique -= (used[goodbye] == 0); if (unique <= k) ok = true, ptr = i; } return ok; }; int l = 1, r = n + 1; while (r - l > 1) { int mid = l + r >> 1; if (check(mid)) l = mid; else r = mid; } check(l); cout << ptr + 1 << " " << ptr + l << "\n"; return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; int n, pos[30]; vector<int> vec; char ans[1001][1001]; int main() { cin >> n; if (n == 1) { printf("2\nNY\nYN"); return 0; } for (int i = 0; i < 1001; i++) for (int j = 0; j < 1001; j++) ans[i][j] = 'N'; for (int i = 0; i < 30; i++) if (n & (1 << i)) vec.push_back(i); int mx = vec[vec.size() - 1]; pos[0] = 0, pos[mx] = 1; int ind = 2, l = 0; for (int i = 0; i < mx - 1; i++) { ans[l][ind] = ans[ind][l] = 'Y'; ind++; ans[l][ind] = ans[ind][l] = 'Y'; ind++; ans[ind - 1][ind] = ans[ind][ind - 1] = 'Y'; ans[ind - 2][ind] = ans[ind][ind - 2] = 'Y'; pos[i + 1] = ind; l = ind++; } ans[l][ind] = ans[ind][l] = 'Y'; ind++; ans[l][ind] = ans[ind][l] = 'Y'; ind++; ans[ind - 1][1] = ans[1][ind - 1] = 'Y'; ans[ind - 2][1] = ans[1][ind - 2] = 'Y'; for (int i = 0; i < vec.size() - 1; i++) { int x = vec[i]; int y = mx - x; l = 0; for (int j = 0; j < y * 2 - 1; j++) ans[l][ind] = ans[ind][l] = 'Y', l = ind++; ans[l][pos[y]] = ans[pos[y]][l] = 'Y'; } cout << ind << endl; for (int i = 0; i < ind; i++) { for (int j = 0; j < ind; j++) cout << ans[i][j]; cout << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; char x[20]; int main() { scanf("%s", x); int len = (int)strlen(x); for (int i = 0, id; i < len; i++) { id = x[i] - '0'; if (id > 4) { if (i == 0 && id == 9) continue; id = 9 - id; x[i] = id + '0'; } } printf("%s\n", x); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long mod = 1e9 + 7; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long fast_pow(long long base, long long n, long long M = inf) { if (n == 0) return 1; if (n == 1) return base; long long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } long long comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long n, i, j, x, y, r, c(0); cin >> n; pair<long long, long long> a[n]; for (i = 0; i < n; ++i) { cin >> x >> y; a[i] = {x, y}; } sort(a, a + n, comp); r = a[0].second; for (i = 1; i < n; ++i) { if (a[i].second < r) ++c; else r = a[i].second; } cout << c; }
3