solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { long long b1, q, l, m; cin >> b1 >> q >> l >> m; long long bad = 0; set<long long> prev; for (long long i = 0; i < m; ++i) { long long a; cin >> a; long long cop = a; prev.insert(cop); } long long ans = 0; int i = 0; vector<long long> out; while (i <= 1000000) { i++; if (abs(b1) > l) continue; if (prev.find(b1) == prev.end()) out.push_back(b1); b1 = b1 * q; } if (out.size() > 100000) cout << "inf"; else cout << out.size(); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int a; while (cin >> a) { if (a == 20) cout << 15; else if (a <= 10 || a >= 22) cout << 0; else if ((a >= 11 && a <= 21)) cout << 4; else cout << 10; cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long dp[20][20]; int p[20][20]; long long e10[19]; const int H = 1, M = 2; int main() { { if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n; cin >> n; string s; cin >> s; vector<int> a(s.size()); for (int i = 0; i != 2 * n; ++i) a[i] = s[i] - '0'; e10[0] = 1; for (int i = 1; i <= 18; ++i) e10[i] = 10 * e10[i - 1]; dp[0][0] = 0; for (int i = 1; i <= n; ++i) { dp[i][0] = dp[i - 1][0] + e10[n - i] * a[i - 1]; p[i][0] = H; } for (int j = 1; j <= n; ++j) { dp[0][j] = dp[0][j - 1] + e10[n - j] * a[j - 1]; p[0][j] = M; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { long long h = dp[i - 1][j] + e10[n - i] * a[i + j - 1]; long long m = dp[i][j - 1] + e10[n - j] * a[i + j - 1]; if (h >= m) { dp[i][j] = h; p[i][j] = H; } else if (m > h) { dp[i][j] = m; p[i][j] = M; } } int i = n, j = n; string ans; while (i != 0 || j != 0) { if (p[i][j] == H) { ans.push_back('H'); --i; } else if (p[i][j] == M) { ans.push_back('M'); --j; } } for (int i = ans.size() - 1; i >= 0; --i) cout << ans[i]; return 0; }
16
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } const int maxn = 200200; const int mod = 1000000007; int n, k, a, b, q; pair<int, int> t[maxn << 2]; pair<int, int> combine(pair<int, int> a, pair<int, int> b) { pair<int, int> c; c.first = a.first + b.first; c.second = a.second + b.second; return c; } void add(int v, int l, int r, int pos, int val) { int m = (l + r) >> 1; if (l == r) { t[v].first = min(a, t[v].first + val); t[v].second = min(b, t[v].second + val); return; } if (pos <= m) add(v << 1, l, m, pos, val); else add(v << 1 | 1, m + 1, r, pos, val); t[v] = combine(t[v << 1], t[v << 1 | 1]); } pair<int, int> get(int v, int l, int r, int L, int R) { int m = (l + r) >> 1; if (L > R) return make_pair(0, 0); if (l == L && r == R) return t[v]; else return combine(get(v << 1, l, m, L, min(R, m)), get(v << 1 | 1, m + 1, r, max(L, m + 1), R)); } void solve() { scanf("%d %d %d %d %d", &n, &k, &a, &b, &q); while (q--) { int x, y, p, type; scanf("%d", &type); if (type == 1) { scanf("%d %d", &x, &y); add(1, 1, n, x, y); } else { scanf("%d", &p); int res = get(1, 1, n, 1, p - 1).second + get(1, 1, n, p + k, n).first; printf("%d\n", res); } } } int main() { int t = 1; while (t--) { solve(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long mo = 1000000007; template <typename _T> _T Fabs(_T x) { return x < 0 ? -x : x; } template <typename _T> void read(_T &x) { _T f = 1; x = 0; char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while ('0' <= s && s <= '9') { x = (x << 3) + (x << 1) + (s ^ 48); s = getchar(); } x *= f; } long long n, fac[200005], inv[200005], f[200005], ans; long long pow2[200005], inv2[200005], t[200005], T; long long qkpow(long long a, long long s) { long long t = 1; while (s) { if (s & 1) t = 1ll * a * t % mo; a = 1ll * a * a % mo; s >>= 1; } return t; } void init() { inv[0] = fac[0] = f[1] = inv[1] = fac[1] = 1; for (int i = 2; i <= 2e5; i++) { fac[i] = fac[i - 1] * i % mo; f[i] = (mo - mo / i) * f[mo % i] % mo; inv[i] = inv[i - 1] * f[i] % mo; } pow2[0] = inv2[0] = 1; for (int i = 1; i <= 2e5; i++) { pow2[i] = pow2[i - 1] * 2ll % mo; inv2[i] = inv2[i - 1] * f[2] % mo; } } long long C(long long x, long long y) { if (x < 0 || y < 0 || y > x) return 0; return fac[x] * inv[y] % mo * inv[x - y] % mo; } signed main() { read(n); read(T); init(); long long now = n, val = 1; for (int i = 1; i <= n; i++) read(t[i]), t[i] += t[i - 1]; for (int i = 1; i <= n; i++) { int s = min(n, T - t[i]); if (s < 0) break; val = (2ll * val - C(i - 1, now) + mo) % mo; while (now > s) val = (val - C(i, now) + mo) % mo, now--; ans = (ans + val * inv2[i] % mo) % mo; } printf("%lld\n", (ans + mo) % mo); return 0; }
16
#include <bits/stdc++.h> using namespace std; int a[1010], n, k; int main() { while (cin >> n >> k) { for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { int j; for (j = 1; j <= n; j++) if (!a[j]) break; cout << j << ' '; for (int q = 1; q <= j - k; q++) a[q]--; a[j]--; } cout << endl; } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, p[N], bulb[N]; int main() { memset(bulb, -1, sizeof(bulb)); ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); cin >> n; for (int i = (2); i <= (n); ++i) cin >> p[i], bulb[p[i]] = 0; for (int i = (1); i <= (n); ++i) bulb[i] = -bulb[i]; for (int i = (n); i >= (2); --i) bulb[p[i]] += bulb[i]; sort(bulb + 1, bulb + n + 1); for (int i = (1); i <= (n); ++i) cout << bulb[i] << " "; cout << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; const long long LINF = 2e18; void inv(bool e); template <typename T> void die(T& a); bool brute, alter; int cnt_tests = 1; int n; vector<int> arr; void inp() { cin >> n; int x; for (int i = 0; i < n; i++) { cin >> x; arr.push_back(x); } } const int MAX = 1e6; bool used[MAX + 10], used2[MAX + 10]; void solve() { for (auto x : arr) { used[x] = 1; } vector<int> ans; int p = 1; for (auto x : arr) { if (!used[MAX - x + 1]) { ans.push_back(MAX - x + 1); } else { if (!used2[x]) { while (used[p] || used[MAX - p + 1]) { p++; } ans.push_back(p); ans.push_back(MAX - p + 1); used2[MAX - x + 1] = 1; p++; } } } cout << ((int)(ans).size()) << '\n'; long long check = 0ll; for (auto x : ans) { cout << x << ' '; check += MAX - x; } cerr << '\n' << check; } void stress() {} void naive() {} void run(); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (0) { freopen("stack.in", "r", stdin); freopen("stack.out", "w", stdout); } brute = false; for (int i = 0; (i < cnt_tests); i++) { run(); } cerr << '\n' << "Time: " << clock() / 1000.0 << " ms"; return 0; } void run() { if (!brute) { inp(); } else { stress(); } solve(); } template <typename T> void die(T& a) { cout << a; exit(0); } void inv(bool e) { if (!e) { vector<int> a; a[-1] += 1; } }
9
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int p; cin >> p; while (p--) { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; if (a == 1 && b == 0 && c == 0 && d == 1 && e == 0) { cout << 'a'; } if (a == 1 && b == 1 && c == 0 && d == 2 && e == 0) { cout << 'b'; } if (a == 2 && b == 0 && c == 0 && d == 1 && e == 1) { cout << 'c'; } if (a == 2 && b == 1 && c == 0 && d == 1 && e == 2) { cout << 'd'; } if (a == 1 && b == 1 && c == 0 && d == 1 && e == 1) { cout << 'e'; } if (a == 2 && b == 1 && c == 0 && d == 2 && e == 1) { cout << 'f'; } if (a == 2 && b == 2 && c == 0 && d == 2 && e == 2) { cout << 'g'; } if (a == 1 && b == 2 && c == 0 && d == 2 && e == 1) { cout << 'h'; }/* if (a == 1 && b == 1 && c == 0 && d == 1 && e == 1) { cout << 'i'; }*/ if (a == 1 && b == 2 && c == 0 && d == 1 && e == 2) { cout << 'j'; } if (a == 1 && b == 0 && c == 1 && d == 2 && e == 0) { cout << 'k'; } if (a == 1 && b == 1 && c == 1 && d == 3 && e == 0) { cout << 'l'; } if (a == 2 && b == 0 && c == 1 && d == 2 && e == 1) { cout << 'm'; } if (a == 2 && b == 1 && c == 1 && d == 2 && e == 2) { cout << 'n'; } if (a == 1 && b == 1 && c == 1 && d == 2 && e == 1) { cout << 'o'; } if (a == 2 && b == 1 && c == 1 && d == 3 && e == 1) { cout << 'p'; } if (a == 2 && b == 2 && c == 1 && d == 3 && e == 2) { cout << 'q'; } if (a == 1 && b == 2 && c == 1 && d == 3 && e == 1) { cout << 'r'; }/* if (a == 1 && b == 1 && c == 1 && d == 2 && e == 1) { cout << 's'; }*/ if (a == 1 && b == 2 && c == 1 && d == 2 && e == 2) { cout << 't'; } if (a == 1 && b == 0 && c == 2 && d == 2 && e == 1) { cout << 'u'; } if (a == 1 && b == 1 && c == 2 && d == 3 && e == 1) { cout << 'v'; } if (a == 1 && b == 2 && c == 1 && d == 1 && e == 3) { cout << 'w'; } if (a == 2 && b == 0 && c == 2 && d == 2 && e == 2) { cout << 'x'; } if (a == 2 && b == 1 && c == 2 && d == 2 && e == 3) { cout << 'y'; } if (a == 1 && b == 1 && c == 2 && d == 2 && e == 2) { cout << 'z'; } } }
18
#include <bits/stdc++.h> using namespace std; bool check(string w, vector<string> s, long long int m) { for (long long int i = 0; i < s.size(); i++) { long long int dif = 0; for (long long int j = 0; j < m; j++) if (s[i][j] != w[j]) dif++; if (dif > 1) return false; } return true; } void solve() { long long int n, m; cin >> n >> m; vector<string> s(n); for (long long int i = 0; i < n; i++) cin >> s[i]; for (long long int i = 0; i < m; i++) { string w = s[0]; for (char c = 'a'; c <= 'z'; c++) { w[i] = c; if (check(w, s, m)) { cout << w << endl; return; } } } cout << -1 << endl; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int q; cin >> q; while (q--) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<long long> G[100010], ter; long long n, root, c[100010], par[100010], dt[100010][2], ans; void dfs(int v, int pv) { dt[v][0] = 1; dt[v][1] = 0; for (int w : G[v]) if (w != pv) { dfs(w, v); dt[v][1] *= dt[w][0]; dt[v][1] += dt[v][0] * dt[w][1]; dt[v][0] *= dt[w][0]; dt[v][1] %= 1000000007, dt[v][0] %= 1000000007; } if (c[v] == 1) dt[v][1] = dt[v][0]; else dt[v][0] += dt[v][1]; } int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 1, t; i < n; i++) { cin >> t; G[i].push_back(t), G[t].push_back(i); } for (int i = 0; i < n; i++) cin >> c[i]; dfs(0, 0); cout << dt[0][1] << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; char ln[200000], b[20][20]; int dg[20]; int main() { gets(ln); int n, i, j, k, l, r = 0, st, t, q = 0; scanf("%d ", &n); for (i = 0; i < n; i++) gets(b[i]); for (i = 0; i < n; i++) { dg[i] = 20; for (j = 0; j < n; j++) { for (k = 0; b[i][k]; k++) { for (l = 0; b[j][l] && b[j][l] == b[i][k + l]; l++) ; if (!b[j][l]) dg[i] = min(dg[i], k + l); } } } st = -1; bool f; for (i = 0; ln[i]; i++) { f = 0; t = 20; for (j = 0; j < n; j++) { for (k = 0, l = i; b[j][k] && b[j][k] == ln[l]; k++, l++) ; if (!b[j][k]) { f = 1; if (t > dg[j]) t = dg[j]; } } if (f) { t = i + t - 2 - st; if (t > q) { q = t; r = st + 1; } st = i; } } t = i - 1 - st; if (t > q) { q = t; r = st + 1; } cout << q << ' ' << r << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0; char c = getchar(); bool f = 0; while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } if (f) return -sum; return sum; } const int N = 600000; const int INF = 2000000000; int n, m, cnt, A[N], B[N]; namespace LCT { int f[N], son[N][2], mn[N], v[N], sz[N]; bool rev[N]; inline void in(int x) { v[x] = INF; sz[x] = (x <= n); mn[x] = x; } inline bool RT(int x) { return son[f[x]][0] != x && son[f[x]][1] != x; } inline void up(int &x, int y) { if (y && v[x] > v[y]) x = y; } inline void up(int x) { sz[x] = sz[son[x][0]] + sz[son[x][1]] + (x <= n); mn[x] = x; up(mn[x], mn[son[x][0]]); up(mn[x], mn[son[x][1]]); } inline void down(int x) { if (rev[x]) rev[x] = 0, swap(son[x][0], son[x][1]), rev[son[x][0]] ^= 1, rev[son[x][1]] ^= 1; } inline void dn(int x) { if (!RT(x)) dn(f[x]); down(x); } inline void R(int x) { int p = f[x], g = f[p]; bool a = son[p][1] == x, b = !a; son[p][a] = son[x][b]; f[son[x][b]] = p; if (!RT(p)) son[g][son[g][1] == p] = x; f[x] = g; son[x][b] = p; f[p] = x; up(p); up(x); } inline void splay(int x) { dn(x); int p; while (!RT(x)) { if (RT(p = f[x])) { R(x); break; } ((son[f[p]][0] == p) ^ (son[p][0] == x)) ? R(x) : R(p); R(x); } } inline void access(int x) { for (int y = 0; x; y = x, x = f[x]) splay(x), son[x][1] = y, up(x); } inline void mroot(int x) { access(x); splay(x); rev[x] ^= 1; } inline int find(int x) { access(x); splay(x); while (son[x][0]) x = son[x][0]; return x; } inline void link(int x, int y) { mroot(x); f[x] = y; } inline void cut(int x, int y) { mroot(x); access(y); splay(y); son[y][0] = 0; f[x] = 0; up(y); } bool ind[N], cir[N]; int em; inline void link(int x, int y, int k) { int a = find(x), b = find(y); if (a != b) { link(x, k); link(y, k); ind[k] = 1; return; } mroot(x); access(y); splay(y); b = sz[y]; if (v[k] > v[a = mn[y]]) cut(A[a], a), cut(B[a], a), link(x, k), link(y, k), ind[a] = 0, ind[k] = 1; if (b & 1) { v[k] > v[a] ? cir[a] = 1 : cir[k] = 1; em++; } } } // namespace LCT using namespace LCT; map<pair<int, int>, int> H; int X[N], Y[N], id[N]; bool tp[N]; int main() { int i, j, x, y; pair<int, int> o; cnt = n = read(); m = read(); for (i = 1; i <= n; i++) in(i); for (i = 1; i <= m; i++) { X[i] = x = read(); Y[i] = y = read(); o = make_pair(x, y); if (!H[o]) id[i] = ++cnt, in(cnt), H[o] = cnt, tp[i] = 1, A[cnt] = X[i], B[cnt] = Y[i]; else v[x = H[o]] = i, id[i] = x, H[o] = 0; } for (i = 1; i <= m; i++) { if (tp[i]) link(X[i], Y[i], id[i]); else if (ind[id[i]]) cut(X[i], id[i]), cut(Y[i], id[i]); else if (cir[id[i]]) cir[id[i]] = 0, em--; puts(em ? "NO" : "YES"); } return 0; }
17
#include <bits/stdc++.h> using namespace std; int dp[10500], s[120], a[120]; int main() { int n, m, nn; cin >> n >> m; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { cin >> nn; a[0] = 0; for (int j = 1; j <= nn; j++) { cin >> a[j]; a[j] += a[j - 1]; } memset(s, 0, sizeof(s)); for (int j = 0; j < nn; j++) { for (int k = 0; k <= nn - j; k++) { s[j + k] = max(s[j + k], a[nn] - a[nn - k] + a[j]); } } for (int j = m; j > 0; j--) { for (int k = min(j, nn); k > 0; k--) { dp[j] = max(dp[j], dp[j - k] + s[k]); } } } cout << dp[m] << endl; }
11
#include <bits/stdc++.h> const long long MaxN = 1e6 + 50; const long long MaxM = 1050; const long long p = 1e9 + 7; template <class t> inline void read(t &s) { s = 0; register long long f = 1; register char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); s *= f; return; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), x = -x; long long buf[21], top = 0; while (x) buf[++top] = x % 10, x /= 10; if (!top) buf[++top] = 0; while (top) putchar(buf[top--] ^ '0'); return; } inline void setIn(std::string s) { freopen(s.c_str(), "r", stdin); return; } inline void setOut(std::string s) { freopen(s.c_str(), "w", stdout); return; } inline void setIO(std::string s = "") { setIn(s + ".in"); setOut(s + ".out"); return; } template <class t> inline bool checkmin(t &x, t y) { if (x > y) { x = y; return 1; } return 0; } template <class t> inline bool checkmax(t &x, t y) { if (x < y) { x = y; return 1; } return 0; } inline long long lowbit(long long x) { return x & (-x); } long long fac[MaxN], inv[MaxN]; long long g[MaxM][MaxM], h[MaxM], n, m, K; inline long long fpow(long long a, long long b) { register long long res = 1; for (; b; b >>= 1, a = a * a % p) if (b & 1) res = res * a % p; return res; } inline long long C(long long n, long long m) { if (n < m) return 0; return fac[n] * inv[m] % p * inv[n - m] % p; } inline long long sqr(long long x) { return x * x % p; } signed main(void) { read(n, m, K); if (m == 1) return write(fpow(K, n)), std::puts(""), 0; fac[0] = 1; for (long long i = 1; i < MaxN; ++i) fac[i] = fac[i - 1] * i % p; inv[MaxN - 1] = fpow(fac[MaxN - 1], p - 2); for (long long i = MaxN - 2; ~i; --i) inv[i] = inv[i + 1] * (i + 1) % p; g[0][0] = 1; for (long long i = 1; i <= n; ++i) for (long long j = 1; j <= i && j <= K; ++j) g[i][j] = (g[i - 1][j - 1] + g[i - 1][j] * j % p) % p; register long long ans = 0; for (long long i = 0; i <= n; ++i) { register long long x = fpow(i, n * (m - 2)) * C(K, i) % p; for (long long j = i; j <= n; ++j) (ans += C(K - i, 2 * (j - i)) * C(2 * (j - i), j - i) % p * sqr(g[n][j] * fac[j] % p) % p * x % p) %= p; } write(ans), std::puts(""); return 0; }
15
#include <bits/stdc++.h> using namespace std; long long fun(int n, int m) { long long s = 1; for (int i = 1; i <= m; i++) { s *= n; s %= 1000000007; } return s; } int main() { int n, k; long long s; cin >> n >> k; s = fun(n - k, n - k) * fun(k, k - 1); s = s % 1000000007; cout << s << endl; return 0; }
7
#include<bits/stdc++.h> using namespace std; int t, n; long long b[200005]; long long sum; int main() { ios::sync_with_stdio(0); cin >> t; while(t--) { cin >> n; for(int i = 1; i <= n + 2; i++) cin >> b[i]; sort(b + 1, b + n + 1 + 2); sum = 0LL; for(int i = 1; i <= n; i++) sum += b[i]; long long tmp = sum + b[n + 1] - b[n + 2]; int p; bool flag = false; for(int i = 1; i <= n + 1; i++) if(tmp == b[i]) p = i, flag = true; if(flag) { for(int i = 1; i <= n + 1; i++) if(i != p) cout << b[i] << ' '; cout << endl; continue; } if(sum == b[n + 1]) { for(int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl; continue; } cout << -1 << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int break_point() { char c; while ((c = getchar()) != '\n') ; return 0; } template <typename T> void read_integer(T &r) { bool sign = 0; r = 0; char c; while (1) { c = getchar(); if (c == '-') { sign = 1; break; } if (c != ' ' && c != '\n') { r = c - '0'; break; } } while (1) { c = getchar(); if (c == ' ' || c == '\n') break; r = r * 10 + (c - '0'); } if (sign) r = -r; } long long binpow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a; a *= a; b >>= 1; } return ret; } long long binpow(long long a, long long b, int mod) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % mod; a = (a * a) % mod; b >>= 1; } return ret; } inline int getbit(int x, int b) { return (x >> b) & 1; } inline int setbit(int x, int b) { return x | (1 << b); } inline void _setbit(int &x, int b) { x = setbit(x, b); } inline long long setbit(long long x, int b) { return x | (1ll << b); } inline void _setbit(long long &x, int b) { x = setbit(x, b); } inline int unsetbit(int x, int b) { return x & (INT_MAX - (1 << b)); } inline void _unsetbit(int &x, int b) { x = unsetbit(x, b); } inline int countbit(int x) { x = x - ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline long long countbit(long long x) { return countbit(int(x & INT_MAX)) + countbit(int(x >> 32) & INT_MAX); } inline void printbit(int x, int len) { for (int i = len - 1; i >= 0; i--) printf("%d", getbit(x, i)); } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } template <typename A, typename B> ostream &operator<<(ostream &stream, const pair<A, B> &p) { stream << "{" << p.first << "," << p.second << "}"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const vector<A> &v) { stream << "["; for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " "; stream << "]"; return stream; } template <typename A, typename B> ostream &operator<<(ostream &stream, const map<A, B> &v) { stream << "["; for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " "; stream << "]"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const set<A> &v) { stream << "["; for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " "; stream << "]"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const stack<A> &v) { stack<A> st = v; stream << "["; while (!st.empty()) { stream << st.top() << " "; st.pop(); } stream << "]"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const priority_queue<A> &v) { priority_queue<A> q = v; stream << "["; while (!q.empty()) { stream << q.top() << " "; q.pop(); } stream << "]"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const queue<A> &v) { queue<A> q = v; stream << "["; while (!q.empty()) { stream << q.front() << " "; q.pop(); } stream << "]"; return stream; } template <typename A> ostream &operator<<(ostream &stream, const deque<A> &v) { deque<A> q = v; stream << "["; while (!q.empty()) { stream << q.front() << " "; q.pop_front(); } stream << "]"; return stream; } void run(); int main() { srand(time(NULL)); run(); return 0; } const int mod = 1e9 + 7; const int N = 4003; void run() { int c, d, n, m; int k; scanf("%d%d%d%d", &c, &d, &n, &m); scanf("%d", &k); int res = INT_MAX; for (int i = 0; i <= 100000; ++i) { int cost = i * c + max(0, n * m - k - i * n) * d; if (cost < res) res = cost; } printf("%d", res); putchar('\n'); }
7
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.first << ", " << x.second << ")"; return o; } template <typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; } template <typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; } double time() { return double(clock()) / CLOCKS_PER_SEC; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> x(n); for (auto& a : x) cin >> a, a--; vector<int> c(n); for (auto& a : c) cin >> a, a--; vector<int> v(n); vector<vector<int>> a; for (int i = 0; i < n; i++) { if (!v[i]) { int j = i; vector<int> b; while (!v[j]) { b.push_back(j); v[j] = 1; j = x[j]; } a.push_back(b); } } int ans = 1 << 30; for (auto& p : a) { int k = (int)(p).size(); for (int i = 1; i <= k; i++) { if (k % i == 0) { for (int l = 0; l < i; l++) { int f = 0; for (int j = l; j < k; j += i) { if (c[p[j]] != c[p[l]]) { f = 1; break; } } if (!f) { ans = min(ans, i); break; } } } } } cout << ans << endl; } }
14
#include <bits/stdc++.h> using namespace std; char str[150], ch[150]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", str + 1); while (q--) { int l, r; scanf("%d%d", &l, &r); memset(ch, '\0', sizeof(ch)); for (int i = 1; i <= r - l + 1; i++) ch[i] = str[i + l - 1]; int dir = 1, s[10], pos = 0, last = 0, pre = 0; memset(s, 0, sizeof(s)); for (;;) { pos += dir; if (pos < 1 || pos > r - l + 1) break; if (ch[pos] == '<') { if ((ch[pre] == '<' || ch[pre] == '>')) { ch[pre] = '#'; last = pos; } dir = -1; pre = pos; } else if (ch[pos] == '>') { if ((ch[pre] == '>' || ch[pre] == '<')) { ch[pre] = '#'; last = pos; } dir = 1; pre = pos; } else if (ch[pos] == '#') { continue; } else { s[ch[pos] - '0']++; if (ch[pos] == '0') ch[pos] = '#'; else if (ch[pos] != '#') ch[pos]--; pre = pos; } } for (int i = 0; i < 10; i++) printf("%d ", s[i]); printf("\n"); } }
7
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); long long tt; cin >> tt; vector<long long> three; long long count = 1; while (count <= 2000000000000000000) { three.push_back(count); count *= 3; } while (tt--) { long long n; cin >> n; long long m = n; long long index = -1; long long index2 = -1; long long ans = 2000000000000000000; for (long long i = 39; i >= 0; i--) { if (three[i] <= n) { n -= three[i]; if (n == 0) { ans = m; break; } } else { ans = min(ans, m + three[i] - n); } } cout << ans << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; int n, k, d, t, i, j, sz; set<int> s; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> k >> d; int a[n]; int mn = n + 1; for (i = 0; i < n; ++i) cin >> a[i]; for (i = 0; i + d - 1 < n; ++i) { for (j = i; j < d + i; ++j) s.insert(a[j]); sz = s.size(); mn = min(mn, sz); s.clear(); } cout << mn << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int n, p[maxn]; bool vis[maxn]; int main() { scanf("%d", &n); for (int i = 1; i < n + 1; i++) scanf("%d", p + i); int max1 = 0, max2 = 0; long long ans = 0; for (int i = 1; i < n + 1; i++) if (!vis[i]) { int len = 0; int x = i; while (!vis[x]) { vis[x] = true; x = p[x]; len++; } if (len > max1) { max2 = max1; max1 = len; } else if (len > max2) max2 = len; ans += (long long)len * len; } ans -= (long long)max1 * max1; ans -= (long long)max2 * max2; long long l = max1 + max2; ans += l * l; printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k, flag = 1; cin >> n >> k; if (k % 3 == 0) { n %= (k + 1); if (n != k && n % 3 == 0) flag = 0; } else { if (n % 3 == 0) flag = 0; } cout << (flag ? "Alice\n" : "Bob\n"); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int a[1000111]; int main() { int n, w; scanf("%d", &n); memset(a, 0, sizeof a); for (int i = 0; i < (int)(n); i++) { scanf("%d", &w); a[w]++; } int ans = 0; for (int i = 0; i < (int)(1000100); i++) { a[i + 1] += a[i] / 2; ans += a[i] & 1; } printf("%d\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int h[100005]; int n, a, r, m; long long costMv(long long x, long long y) { return x * m + y * (a + r); } long long cost(int target) { long long dp = 0; long long dm = 0; for (int(i) = 0; (i) < (n); (i)++) { if (h[i] > target) { dp += h[i] - target; } if (h[i] < target) { dm += target - h[i]; } } long long mv = min(dp, dm); long long ans = (dp - mv) * r + (dm - mv) * a; long long l = 0, r = mv; while (r - l > 2) { long long m1 = l + (r - l) / 3; long long m2 = r - (r - l) / 3; if (costMv(m1, mv - m1) > costMv(m2, mv - m2)) { l = m1; } else { r = m2; } } long long tmp = costMv(l, mv - l); for (long long i = l; i <= r; i++) { tmp = min(tmp, costMv(i, mv - i)); } ans += tmp; return ans; } int main(void) { scanf("%d%d%d%d", &n, &a, &r, &m); for (int(i) = 0; (i) < (n); (i)++) { scanf("%d", &h[i]); } int l = 0, r = 1000000000; while (r - l > 2) { int m1 = l + (r - l) / 3; int m2 = r - (r - l) / 3; if (cost(m1) > cost(m2)) { l = m1; } else { r = m2; } } long long ans = cost(l); for (int(i) = (l); (i) < (r + 1); (i)++) { ans = min(ans, cost(i)); } printf("%lld\n", ans); }
13
#include <bits/stdc++.h> using namespace std; const int N = 100050, M = 40; long long a[N], x[N], y[N]; long long num; struct trie { trie *next[2]; trie() { next[0] = next[1] = 0; } void insert(int i = M) { if (i == -1) return; bool ind = num & (1LL << i); if (next[ind] == 0) next[ind] = new trie(); next[ind]->insert(i - 1); } }; long long find(trie *x, trie *y, long long mask = 0, int i = M) { if (i == -1) return mask; long long ans = 0; bool b = true; if (x->next[0] && y->next[1]) { b = false; ans = max(ans, find(x->next[0], y->next[1], mask ^ (1LL << i), i - 1)); } if (x->next[1] && y->next[0]) { b = false; ans = max(ans, find(x->next[1], y->next[0], mask ^ (1LL << i), i - 1)); } if (b && x->next[1] && y->next[1]) ans = max(ans, find(x->next[1], y->next[1], mask, i - 1)); if (b && x->next[0] && y->next[0]) ans = max(ans, find(x->next[0], y->next[0], mask, i - 1)); return ans; } int main() { trie r1, r2; int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%I64d", a + i); num = 0; for (int ID = 0; ID <= n; ++ID) { num ^= a[ID]; r1.insert(); } num = 0; for (int ID = n + 1; ID > 0; --ID) { num ^= a[ID]; r2.insert(); } printf("%I64d", find(&r1, &r2)); return 0; }
14
#include <bits/stdc++.h> using namespace std; struct A { int all; int start; } all[200005]; int ans[200005]; bool F(A a, A b) { return a.all < b.all; } priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > how; int main() { int N, M, K, i, j, now = 1; pair<int, int> t; scanf("%d %d %d", &N, &M, &K); for (i = 0; i < N; i++) { scanf("%d", &all[i].all); all[i].start = i; } sort(all, all + N, F); for (i = 0; i < N; i++) { if (i == 0) { ans[all[i].start] = 1; how.push(make_pair(all[i].all, now)); } else { t = how.top(); if (t.first + K < all[i].all) { how.pop(); ans[all[i].start] = t.second; how.push(make_pair(all[i].all, t.second)); } else { ans[all[i].start] = now + 1; how.push(make_pair(all[i].all, now + 1)); now++; } } } printf("%d\n", now); for (i = 0; i < N; i++) printf("%d ", ans[i]); return 0; }
8
#include <bits/stdc++.h> const int N = 200500; using namespace std; int n, tdx, tdy, vtmn, vtmx; int sm[N][3]; string s; bool cx[5]; void out() { exit(0); } bool check(int k) { int xn, yn, xt, yt; for (int x = 1; x <= n + 1 - k; x++) { xn = sm[n][0] - sm[x + k - 1][0] + sm[x - 1][0]; yn = sm[n][1] - sm[x + k - 1][1] + sm[x - 1][1]; xt = tdx - xn; yt = tdy - yn; if (abs(xt) + abs(yt) <= k && (k - abs(xt) - abs(yt)) % 2 == 0) return true; } return false; } int main() { cin >> n >> s >> tdx >> tdy; if (n < abs(tdx) + abs(tdy) || (n > abs(tdx) + abs(tdy) && (n - (abs(tdx) + abs(tdy)) % 2 == 1))) cout << -1, out(); if ((tdx == 0 || tdy == 0) && abs(tdx) + abs(tdy) == n) { if (tdx == tdy) cout << -1, out(); if (tdx == 0) { vtmn = INT_MAX, vtmx = INT_MIN; if (tdy > 0) { for (int x = 0; x <= s.size() - 1; x++) if (s[x] != 'U') vtmn = min(x, vtmn), vtmx = max(x, vtmx); if (vtmn == INT_MAX) cout << 0, out(); else cout << vtmx - vtmn + 1, out(); } else { for (int x = 0; x <= s.size() - 1; x++) if (s[x] != 'D') vtmn = min(x, vtmn), vtmx = max(x, vtmx); if (vtmn == INT_MAX) cout << 0, out(); else cout << vtmx - vtmn + 1, out(); } } else { vtmn = INT_MAX, vtmx = INT_MIN; if (tdx > 0) { for (int x = 0; x <= s.size() - 1; x++) if (s[x] != 'R') vtmn = min(x, vtmn), vtmx = max(x, vtmx); if (vtmn == INT_MAX) cout << 0, out(); else cout << vtmx - vtmn + 1, out(); } else { for (int x = 0; x <= s.size() - 1; x++) if (s[x] != 'L') vtmn = min(x, vtmn), vtmx = max(x, vtmx); if (vtmn == INT_MAX) cout << 0, out(); else cout << vtmx - vtmn + 1, out(); } } } sm[0][0] = sm[0][1] = 0; for (int x = 1; x <= s.size(); x++) { sm[x][1] = sm[x - 1][1]; sm[x][0] = sm[x - 1][0]; if (s[x - 1] == 'L') sm[x][0]--; if (s[x - 1] == 'R') sm[x][0]++; if (s[x - 1] == 'U') sm[x][1]++; if (s[x - 1] == 'D') sm[x][1]--; } if (sm[n][0] == tdx && sm[n][1] == tdy) cout << 0, out(); int l = 1, r = n, mid, ans; ans = -1; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) ans = mid, r = --mid; else l = ++mid; } cout << ans; }
10
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[m + 1], b[m + 1], i; for (i = 0; i < m; i++) cin >> a[i] >> b[i]; string s; for (i = 0; i < n; i++) { if (i % 2 == 0) s += '1'; else s += '0'; } cout << s << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int count = 0; int i; while (n) { i = n % 10; n /= 10; count++; } if (count == 1) count = 1; else if (count == 2) count = 3; else if (count == 3) count = 6; else count = 10; printf("%d\n", (i - 1) * 10 + count); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 400005; const long long MOD = 1000000007; const long long INF = (long long)1e9 + 5; struct ACA { static const int MAXL = 400005, SIGMA = 26; int n, link[MAXL], next[MAXL][SIGMA]; int trie[MAXL][SIGMA], tag[MAXL], nocc[MAXL]; int new_node(int p) { link[n] = tag[n] = nocc[n] = -1; for (int i = 0; i < SIGMA; i++) { next[n][i] = 0; trie[n][i] = -1; } return n++; } void build(vector<string> &dict) { n = 0; new_node(0); for (int i = 0; i < dict.size(); i++) { int v = 0; for (char ch : dict[i]) { int to = ch - 'a'; if (trie[v][to] == -1) { trie[v][to] = next[v][to] = new_node(v); } v = trie[v][to]; } tag[v] = i; } queue<int> Q; link[0] = 0; Q.push(0); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int to = 0; to < SIGMA; to++) { if (trie[v][to] != -1) { int u = trie[v][to]; link[u] = v == 0 ? 0 : next[link[v]][to]; nocc[u] = tag[link[u]] != -1 ? link[u] : nocc[link[u]]; for (int j = 0; j < SIGMA; j++) { if (trie[u][j] == -1) { next[u][j] = next[link[u]][j]; } } Q.push(u); } } } } } aca; int N, Q, cur, ans[MAXN], occ[MAXN]; vector<string> dict; vector<int> child[MAXN], q_on_node[MAXN]; int qs[MAXN]; int pa[MAXN]; char add[MAXN]; map<string, int> trans; void dfs(int v) { for (int qid : q_on_node[v]) { ans[qid] = occ[qs[qid]]; } for (int to : child[v]) { int store = cur; ; cur = aca.next[cur][add[to] - 'a']; int tmp = cur; while (tmp != -1) { if (aca.tag[tmp] != -1) occ[aca.tag[tmp]]++; tmp = aca.nocc[tmp]; } dfs(to); tmp = cur; while (tmp != -1) { if (aca.tag[tmp] != -1) occ[aca.tag[tmp]]--; tmp = aca.nocc[tmp]; } cur = store; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 1; i <= N; i++) { int t, id; char ch; cin >> t; if (t == 1) { cin >> ch; id = 0; } else { cin >> id >> ch; } pa[i] = id; child[id].push_back(i); add[i] = ch; } cin >> Q; for (int _q = 0; _q < Q; _q++) { int id; string s; cin >> id >> s; q_on_node[id].push_back(_q); trans.insert({s, trans.size()}); qs[_q] = trans[s]; } dict.resize(trans.size()); for (auto &p : trans) { dict[p.second] = p.first; } aca.build(dict); cur = 0; dfs(0); for (int i = 0; i < Q; i++) { cout << ans[i] << '\n'; } return 0; }
19
#include <bits/stdc++.h> using namespace std; int n, k, t, tmp, ans1, ans2; int cnt, a[110]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &k); t = k >> 1; for (int i = 1; i <= t; i++) { scanf("%d", &tmp); ans1 += tmp; } if (k & 1) { scanf("%d", &tmp); a[++cnt] = tmp; } for (int i = 1; i <= t; i++) { scanf("%d", &tmp); ans2 += tmp; } } sort(a + 1, a + cnt + 1, greater<int>()); for (int i = 1; i <= cnt; i++) { if (i & 1) ans1 += a[i]; else ans2 += a[i]; } printf("%d %d\n", ans1, ans2); return 0; }
12
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); const long long INF = 0x3f3f3f3f3f3f3f3f; const int inf = 0x3f3f3f3f; const int N = 2e5 + 5; int n; int a[N]; int b[N]; int c[N]; vector<int> e[N]; int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<int> d[N]; void INIT() { for (int i = 1; i <= n; i++) { e[i].clear(); } } bool check(int x) { for (int i = 1; i <= n; i++) b[i] = a[i]; memset(c, 0, sizeof(c)); for (int i = 1; i <= x; i++) { if (i > 200000) break; for (auto v : e[i]) { c[v] = i; } } for (int i = 1; i <= n; i++) { if (c[i] == 0) continue; d[c[i]].push_back(i); } int now = 0; for (int i = 1; i <= x; i++) { now++; if (i <= 200000) { for (auto v : d[i]) { if (now >= b[v]) { now -= b[v]; b[v] = 0; } else { b[v] -= now; now = 0; } } } else { now += (x - 200001); break; } } for (int i = 1; i <= n; i++) { d[i].clear(); } int sum = 0; for (int i = 1; i <= n; i++) { sum += b[i]; } sum *= 2; return now >= sum; } void solve() { INIT(); int m; cin >> (m); for (int i = 1; i <= n; i++) cin >> (a[i]); for (int i = 1; i <= m; i++) { int d, t; cin >> d >> t; e[d].push_back(t); } int l = 1, r = 4000000; int ans; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << (ans) << '\n'; } int main() { ios::sync_with_stdio(false); while (cin >> n) { solve(); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; int n, m; vector<int> G[MAX]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } if (m == n - 1) { int flag = 0; for (int i = 1; i <= n; i++) { if (G[i].size() == n - 1) { printf("star topology\n"); return 0; } if (G[i].size() > 2) flag = 1; } if (flag) printf("unknown topology\n"); else printf("bus topology\n"); } else { for (int i = 1; i <= n; i++) { if (G[i].size() != 2) { printf("unknown topology\n"); return 0; } } printf("ring topology\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; void solve() { string s; cin >> s; int n; cin >> n; vector<int> v(n); string res(n, 'a'); for (int i = 0; i < (n); ++i) { cin >> v[i]; } vector<int> alpha(26, 0); for (auto c : s) { alpha[c - 'a']++; } int j = 25; vector<int> index; int c = n; while (c) { index.clear(); for (int i = 0; i < (n); ++i) { if (v[i] == 0) index.push_back(i); } for (int i = 0; i < (n); ++i) { if (v[i] > 0) { for (auto d : index) { v[i] -= abs(i - d); } } } while (alpha[j] < index.size()) { j--; } for (auto d : index) { res[d] = 'a' + j; v[d] = -1; c--; } j--; } cout << res << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
10
#include <bits/stdc++.h> using namespace std; long long int c(long long int x) { if (x < 10) return x; long long int y = x, a = x % 10; while (y / 10) y /= 10; x = x / 10 + 9; if (a < y) return x - 1; return x; } int main() { long long int a, b; cin >> a >> b; cout << c(b) - c(a - 1); }
7
#include <bits/stdc++.h> using namespace std; vector<int> edge[5010]; int size[5010], dp[5010], ans[5010], n; void solve(int cur, int pre) { size[cur] = 1; for (vector<int>::iterator iter = edge[cur].begin(); iter != edge[cur].end(); iter++) if (*iter != pre) solve(*iter, cur), size[cur] += size[*iter]; memset(dp, 0, sizeof dp); dp[0] = 1; for (vector<int>::iterator iter = edge[cur].begin(); iter != edge[cur].end(); iter++) if (*iter != pre) for (int i = (n); i >= (size[*iter]); i--) if (dp[i - size[*iter]]) dp[i] = 1; int k = n - size[cur]; for (int i = (n); i >= (k); i--) if (dp[i - k]) dp[i] = 1; for (int i = (1); i <= (n - 2); i++) if (dp[i]) ans[i] = 1; } void lemon() { scanf("%d", &n); for (int i = (1); i <= (n); i++) edge[i].clear(); for (int i = (1); i <= (n - 1); i++) { int x, y; scanf("%d%d", &x, &y); edge[x].push_back(y); edge[y].push_back(x); } memset(ans, 0, sizeof ans); solve(1, -1); int final = 0; for (int i = (1); i <= (n - 2); i++) if (ans[i]) final++; printf("%d\n", final); for (int i = (1); i <= (n - 2); i++) if (ans[i]) printf("%d %d\n", i, n - 1 - i); } int main() { lemon(); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 300010; vector<pair<int, char> > gr[N]; vector<int> nivel[N]; pair<int, int> pai[N]; int dist[N]; int solve(vector<int> at, int root) { if ((int)at.size() <= 1) return root ? (int)at.size() : 0; int res = 0; for (char c = 'a'; c <= 'z'; c++) { vector<int> next; for (auto u : at) { for (auto v : gr[u]) { if (dist[v.first] < dist[u]) continue; if (v.second == c) next.push_back(v.first); } } res += solve(next, 0); } return res + (int)at.size() + (root ? 0 : -1); } int main(void) { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; char c; scanf("%d %d %c", &u, &v, &c); gr[u].emplace_back(v, c); gr[v].emplace_back(u, c); } queue<int> q; memset(dist, 0x3f3f3f3f, sizeof dist); dist[1] = 0; q.push(1); while (!q.empty()) { int u = q.front(); nivel[dist[u]].push_back(u); q.pop(); for (auto v : gr[u]) { if (dist[v.first] > dist[u] + 1) { pai[v.first] = make_pair(u, v.second); dist[v.first] = dist[u] + 1; q.push(v.first); } } } int res = 0, at = 0; for (int i = 1; i <= n; i++) { int x = 0; for (auto u : nivel[i - 1]) { vector<int> w; for (auto v : gr[u]) if (dist[v.first] > dist[u]) w.push_back(v.first); x += solve(w, 1); } if (res < x) { res = x; at = i; } } cout << n - res << endl << at << endl; return 0; }
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int i, j = 0, k, a = 0, b = 0, n, m, x; vector<int> vec, vec1; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x; vec.push_back(x); vec1.push_back(x); } sort(vec.begin(), vec.end()); while (n--) { sort(vec1.begin(), vec1.end()); a += vec1[m - 1]; vec1[m - 1]--; if (vec[j] <= 0) j++; b += vec[j]; vec[j]--; } cout << a << " " << b; }
3
#include <bits/stdc++.h> using namespace std; int n, k, ans, aux, maximum; int kit[150]; set<int> kitchen; set<int>::iterator it; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &aux); kit[aux]++; kitchen.insert(aux); maximum = max(maximum, kit[aux]); } maximum = ceil(maximum / (float)k); maximum *= k; aux = 0; for (it = kitchen.begin(); it != kitchen.end(); ++it) { aux = kit[*it]; ans += maximum - aux; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int oo = 1 << 29, N = 1e5 + 10; const long double PI = acos(0.0) * 2; int btns, lights, x, tmp; bool vis[N]; int main() { cin >> btns >> lights; for (int i = 0; i < btns; ++i) { cin >> x; for (int j = 0; j < x; ++j) { cin >> tmp; vis[tmp - 1] = 1; } } for (int i = 0; i < lights; ++i) if (!vis[i]) { puts("NO"); return 0; } puts("YES"); return 0; }
0
#include <bits/stdc++.h> const long long MOD = 1e9 + 7; const long long INF = 1011111111; const long long LLINF = 1000111000111000111LL; const long double EPS = 1e-10; const long double PI = 3.14159265358979323; using namespace std; long long powm(long long base, long long exp, long long mod = MOD) { long long ans = 1; while (exp) { if (exp & 1) ans = (ans * base) % mod; exp >>= 1, base = (base * base) % mod; } return ans; } const long long N = 1000005; long long conf_color[N], team_color[N], team_number[N]; vector<long long> adj[N], wish_list[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, e, x, y, l, tot_hate = 0, t = 0; cin >> n >> e; while (e--) { cin >> x >> y; if (x > y) swap(x, y); adj[y].push_back(x); } for (long long i = 1; i < n + 1; i++) { long long got[2] = {0, 0}; for (auto j : adj[i]) got[conf_color[j]]++; conf_color[i] = (got[0] >= got[1]); } for (long long i = 1; i < n + 1; i++) { cin >> l; while (l--) { cin >> x; wish_list[i].push_back(x); t = max(x, t); } } srand(time(NULL)); while (1) { for (long long i = 1; i < t + 1; i++) team_color[i] = rand() % 2; bool mk = true; for (long long i = 1; i < n + 1; i++) { long long req = conf_color[i]; bool ok = false; for (auto j : wish_list[i]) if (req == team_color[j]) { team_number[i] = j; ok = true; break; } if (!ok) { mk = false; break; } } if (!mk) continue; for (long long i = 1; i < n + 1; i++) cout << team_number[i] << " "; cout << '\n'; for (long long i = 1; i < t + 1; i++) cout << team_color[i] + 1 << " "; cout << '\n'; exit(0); } return 0; }
16
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, i, j, cnt = 0; cin >> n >> m; char x; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> x; if ((i == n - 1 && x == 'D') || (j == m - 1 && x == 'R')) cnt++; } } cout << cnt << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 5e5 + 5, M = 1e6 + 5; int n, q, sz, a[N], mn[2100000], nm[2100000], la[2100000]; void upd(int x) { if (la[x] != 0) { la[2 * x + 1] += la[x]; la[2 * x + 2] += la[x]; if (mn[2 * x + 1] != INT32_MAX) mn[2 * x + 1] += la[x]; if (mn[2 * x + 2] != INT32_MAX) mn[2 * x + 2] += la[x]; la[x] = 0; } } void seg(int l, int r, int x, int lx, int rx, int v) { if (lx >= l && rx <= r) { la[x] += v; if (mn[x] != INT32_MAX) mn[x] += v; } else if (lx < r && rx > l) { upd(x); int mid = (lx + rx) / 2; seg(l, r, 2 * x + 1, lx, mid, v); seg(l, r, 2 * x + 2, mid, rx, v); mn[x] = min(mn[2 * x + 1], mn[2 * x + 2]); if (mn[2 * x + 1] == mn[2 * x + 2]) nm[x] = nm[2 * x + 1] + nm[2 * x + 2]; else if (mn[2 * x + 1] < mn[2 * x + 2]) nm[x] = nm[2 * x + 1]; else nm[x] = nm[2 * x + 2]; } } void st(int x) { int xx = x + sz - 1; int i = 0, l = 0, r = sz, mid; while (i < xx) { upd(i); mid = (l + r) / 2; if (x < mid) { i = 2 * i + 1; r = mid; } else { i = 2 * i + 2; l = mid; } } mn[xx] = INT32_MAX; nm[xx] = 0; while (xx > 0) { --xx; xx /= 2; mn[xx] = min(mn[2 * xx + 1], mn[2 * xx + 2]); if (mn[2 * xx + 1] == mn[2 * xx + 2]) nm[xx] = nm[2 * xx + 1] + nm[2 * xx + 2]; else if (mn[2 * xx + 1] < mn[2 * xx + 2]) nm[xx] = nm[2 * xx + 1]; else nm[xx] = nm[2 * xx + 2]; } } void sst(int x) { int xx = x + sz - 1; int i = 0, l = 0, r = sz, mid; while (i < xx) { upd(i); mid = (l + r) / 2; if (x < mid) { i = 2 * i + 1; r = mid; } else { i = 2 * i + 2; l = mid; } } mn[xx] = la[xx]; nm[xx] = 1; while (xx > 0) { --xx; xx /= 2; mn[xx] = min(mn[2 * xx + 1], mn[2 * xx + 2]); if (mn[2 * xx + 1] == mn[2 * xx + 2]) nm[xx] = nm[2 * xx + 1] + nm[2 * xx + 2]; else if (mn[2 * xx + 1] < mn[2 * xx + 2]) nm[xx] = nm[2 * xx + 1]; else nm[xx] = nm[2 * xx + 2]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; sz = 1; while (sz < M) sz *= 2; fill(mn + sz - 1, mn + 2 * sz, INT32_MAX); for (int i = 0; i < n; i++) { cin >> a[i]; mn[sz - 1 + a[i]] = 0; nm[sz - 1 + a[i]] = 1; } for (int i = sz - 2; i >= 0; i--) { mn[i] = min(mn[2 * i + 1], mn[2 * i + 2]); if (mn[2 * i + 1] == mn[2 * i + 2]) nm[i] = nm[2 * i + 1] + nm[2 * i + 2]; else if (mn[2 * i + 1] < mn[2 * i + 2]) nm[i] = nm[2 * i + 1]; else nm[i] = nm[2 * i + 2]; } for (int i = 0; i < n - 1; i++) { if (a[i] < a[i + 1]) seg(a[i], a[i + 1], 0, 0, sz, 1); } int x, v; while (q--) { cin >> x >> v; st(a[--x]); sst(v); if (x < n - 1) { if (a[x] < a[x + 1]) seg(a[x], a[x + 1], 0, 0, sz, -1); if (v < a[x + 1]) seg(v, a[x + 1], 0, 0, sz, 1); } if (x > 0) { if (a[x] > a[x - 1]) seg(a[x - 1], a[x], 0, 0, sz, -1); if (v > a[x - 1]) seg(a[x - 1], v, 0, 0, sz, 1); } a[x] = v; cout << nm[0] << '\n'; } }
25
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; while (cin >> n >> m) { string s; cin >> s; char minn = s[0], maxx = s[0]; for (int i = 1; i < n; i++) { minn = min(minn, s[i]); maxx = max(maxx, s[i]); } if (n < m) { cout << s; for (int i = n; i < m; i++) cout << minn; cout << endl; } else { int i; for (i = m - 1; i >= 0; i--) { if (s[i] != maxx) break; } char lb = maxx; for (int j = 0; j < n; j++) { if (s[j] > s[i]) { lb = min(s[j], lb); } } s[i] = lb; for (int j = i + 1; j < m; j++) { s[j] = minn; } cout << s.substr(0, m) << endl; } } }
7
#include <bits/stdc++.h> int main() { int w = 0, k = 0, n = 0, i = 0, s = 0; while (w < 1 || k < 1 || k > 1000 || w > 1000 || n < 0 || n > 1000000000) { scanf("%d", &k); scanf("%d", &n); scanf("%d", &w); } for (i = 1; i < (w + 1); i++) s += i * k; if (n > s) printf("%d", 0); else printf("%d", s - n); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, test, i, j, ara[200003 + 2], mark[200003 + 2]; cin >> test; while (test--) { cin >> n; memset(mark, 0, sizeof(mark)); for (int i = 1; i <= n; i++) cin >> ara[i]; int mx = 0; for (int i = 1; i <= n; i++) { int x = ara[i]; if (mark[x] == 0) { int cnt = 0; while (mark[x] == 0) { mark[x] = -1; x = ara[x]; cnt++; } for (int i = 1; i <= n; i++) { if (mark[i] == -1) mark[i] = cnt; } } } for (int i = 1; i <= n; i++) { if (i != 1) cout << " "; cout << mark[i]; } cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long BIG = 100000000100000; long long a[100001]; long long b[100001]; long long n, m; long long max(long long a, long long b) { if (a > b) return a; else return b; } long long ypa(long long k) { for (long long i = 1; i <= n; i++) b[i] = a[i]; long long t = n; long long g = m; long long s; while (g > 0 && t != 0) { while (b[t] == 0 && t != 0) t--; if (t >= k) return 0; g--; s = max(0, k - t); while (s != 0 && t != 0) { long long mn = min(s, b[t]); s -= mn; b[t] -= mn; while (b[t] == 0 && t != 0) t--; } } if (t == 0) return 1; else return 0; } int main() { cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> a[i]; long long l = 1, r = BIG; long long mid; while (r - l > 1) { mid = (l + r) / 2; if (ypa(mid)) { r = mid; } else { l = mid; } } cout << r << endl; return 0; }
14
#include <bits/stdc++.h> using namespace std; void sign(pair<long long, long long> &a) { if (a.second < 0) { a.first *= -1; a.second *= -1; } } bool smaller(pair<long long, long long> a, pair<long long, long long> b) { sign(a); sign(b); return a.first * b.second < b.first * a.second; } void intersect(pair<long long, long long> t1a, pair<long long, long long> t2a, pair<long long, long long> t1b, pair<long long, long long> t2b, pair<long long, long long> &r1, pair<long long, long long> &r2) { if (smaller(t1a, t1b)) r1 = t1b; else r1 = t1a; if (smaller(t2a, t2b)) r2 = t2a; else r2 = t2b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, x1, y1, x2, y2; cin >> n >> x1 >> y1 >> x2 >> y2; pair<long long, long long> t1 = {0, 1}, t2 = {1000000, 1}; for (int i = 0; i < n; ++i) { long long rx, ry, vx, vy; cin >> rx >> ry >> vx >> vy; if (ry >= y2) { ry = y1 - (ry - y2); vy = -vy; } if (rx >= x2) { rx = x1 - (rx - x2); vx = -vx; } if (rx <= x1 and vx <= 0) { cout << -1 << '\n'; return 0; } if (ry <= y1 and vy <= 0) { cout << -1 << '\n'; return 0; } vector<pair<long long, long long> > inter(4); inter[0] = {x1 - rx, vx}; inter[1] = {y1 - ry, vy}; inter[2] = {x2 - rx, vx}; inter[3] = {y2 - ry, vy}; if (vx == 0 and vy == 0) continue; pair<long long, long long> cand1, cand2; if (ry <= y1) { if (vx > 0) { intersect(inter[0], inter[2], inter[1], inter[3], cand1, cand2); } else if (vx == 0) { cand1 = inter[1]; cand2 = inter[3]; } else { intersect(inter[2], inter[0], inter[1], inter[3], cand1, cand2); } } else { if (vy > 0) { if (vx > 0) { intersect(inter[0], inter[2], inter[1], inter[3], cand1, cand2); } else if (vx == 0) { cand1 = inter[1]; cand2 = inter[3]; } else { intersect(inter[2], inter[0], inter[1], inter[3], cand1, cand2); } } else if (vy == 0) { if (vx > 0) { cand1 = inter[0]; cand2 = inter[2]; } else { cand1 = inter[2]; cand2 = inter[0]; } } else { if (vx > 0) { intersect(inter[0], inter[2], inter[3], inter[1], cand1, cand2); } else if (vx == 0) { cand1 = inter[3]; cand2 = inter[1]; } else { intersect(inter[2], inter[0], inter[3], inter[1], cand1, cand2); } } } intersect(t1, t2, cand1, cand2, t1, t2); } cout.setf(ios::fixed); cout.precision(20); if (!smaller(t1, t2)) cout << -1 << '\n'; else cout << t1.first / ((long double)(t1.second)) << '\n'; }
15
#include <bits/stdc++.h> using namespace std; const double INF = 1000000000; const long long INFLL = 1000000000000000005ll; const long long MOD = 1000000007; enum { F1, F2, F3, Q1, Q2, Q3 }; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> E; int K, N1, N2, N3, T1, T2, T3, W1, W2, W3; int main() { cin >> K >> N1 >> N2 >> N3 >> T1 >> T2 >> T3; for (long long i = (1); i <= (K); i++) E.push({0, Q1}); int ans; while (!E.empty()) { pair<int, int> p = E.top(); E.pop(); int id = p.second; int t = p.first; ans = t; if (id == Q1) { if (N1) { N1--; E.push({t + T1, F1}); } else W1++; } if (id == F1) { N1++; E.push({t, Q2}); if (W1) { E.push({t + T1, F1}); W1--; N1--; } } if (id == Q2) { if (N2) { N2--; E.push({t + T2, F2}); } else W2++; } if (id == F2) { N2++; E.push({t, Q3}); if (W2) { E.push({t + T2, F2}); W2--; N2--; } } if (id == Q3) { if (N3) { N3--; E.push({t + T3, F3}); } else W3++; } if (id == F3) { N3++; if (W3) { E.push({t + T3, F3}); W3--; N3--; } } } cout << ans << "\n"; }
11
#include <bits/stdc++.h> using namespace std; vector<vector<char> > vec; vector<vector<bool> > visited; queue<pair<int, int> > q; vector<pair<int, int> > temp; vector<vector<pair<int, int> > > perm; pair<int, int> p; int a, b, c, f, s, counter; bool edge; int dirx[4] = {0, 0, -1, 1}; int diry[4] = {1, -1, 0, 0}; bool check(int x, int y) { if (x < a && y < b && x > -1 && y > -1 && visited[x][y] == false && vec[x][y] == '.') return true; else return false; } void bfs(int i, int j) { edge = false; temp.clear(); q.push({i, j}); visited[i][j] = true; temp.push_back({i, j}); if (i == 0 || j == 0 || i == a - 1 || j == b - 1) edge = true; while (q.size() > 0) { p = q.front(); q.pop(); f = p.first; s = p.second; for (int ii = 0; ii < 4; ii++) { if (check(f + dirx[ii], s + diry[ii])) { q.push({f + dirx[ii], s + diry[ii]}); visited[f + dirx[ii]][s + diry[ii]] = true; temp.push_back({f + dirx[ii], s + diry[ii]}); if (f + dirx[ii] == 0 || s + diry[ii] == 0 || f + dirx[ii] == a - 1 || s + diry[ii] == b - 1) edge = true; } } } if (edge == false) { perm.push_back(temp); } } bool compare(const vector<pair<int, int> > vec1, const vector<pair<int, int> > vec2) { return vec1.size() < vec2.size(); } int main() { cin >> a >> b >> c; vec.resize(a, vector<char>(b)); visited.resize(a, vector<bool>(b, false)); for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { cin >> vec[i][j]; } } for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { if (check(i, j)) { bfs(i, j); } } } sort(perm.begin(), perm.end(), compare); for (int i = 0; i < perm.size() - c; i++) { for (int j = 0; j < perm[i].size(); j++) { vec[perm[i][j].first][perm[i][j].second] = '*'; counter++; } } cout << counter << '\n'; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { cout << vec[i][j]; } cout << '\n'; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 500; const long long mod = 1e9 + 7; const long long cmod = 998244353; const long long inf = 1LL << 61; const int M = 1e6 + 500; const long long int ths = 1LL << 40; const int NN = 5e3 + 6; void solve() { long long int n, k; cin >> n >> k; long long int a[n]; stack<long long int> st; long long int now = 1; for (int i = 0; i < n; i++) { if (i < k) cin >> a[i]; else { if (st.size() == 0) a[i] = n; else a[i] = st.top() - 1; } st.push(a[i]); while (st.size() and st.top() == now) { st.pop(); now++; } } if (st.size() > 0) { cout << -1; } else { for (int i = 0; i < n; i++) cout << a[i] << ' '; } cout << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int t[4 * N]; int mod[4 * N]; void update(int v, int x) { t[v] += x; mod[v] += x; } void push(int v) { if (mod[v]) { update(2 * v + 1, mod[v]); update(2 * v + 2, mod[v]); mod[v] = 0; } } void modify(int ql, int qr, int x, int l, int r, int v) { if (qr < l || r < ql) return; if (ql <= l && r <= qr) { update(v, x); } else { push(v); int m = (l + r) >> 1; modify(ql, qr, x, l, m, 2 * v + 1); modify(ql, qr, x, m + 1, r, 2 * v + 2); t[v] = max(t[2 * v + 1], t[2 * v + 2]); } } int get(int ql, int qr, int l, int r, int v) { if (qr < l || r < ql) return 0; if (ql <= l && r <= qr) return t[v]; push(v); int m = (l + r) >> 1; return max(get(ql, qr, l, m, 2 * v + 1), get(ql, qr, m + 1, r, 2 * v + 2)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int d, n, m; cin >> d >> n >> m; vector<pair<int, int>> a(m + 1); for (int i = 0; i < m; ++i) { cin >> a[i].first >> a[i].second; } a[m] = {d, 0}; sort(a.begin(), a.end()); long long ans = 0; int have = n; set<pair<int, int>> gs; for (int i = 0; i < (int)a.size(); ++i) { int dst = a[i].first - (i == 0 ? 0 : a[i - 1].first); if (have >= dst) { have -= dst; } else { int need = dst - have; have -= dst; while (need) { if (gs.empty()) { cout << "-1\n"; return 0; } auto p = *gs.begin(); gs.erase(gs.begin()); int id = p.second; int can = n - get(id, i - 1, 0, m, 0); int tk = min(need, can); have += tk; need -= tk; can -= tk; ans += (long long)tk * p.first; modify(id, i - 1, tk, 0, m, 0); if (can > 0) { gs.insert(p); } } } modify(i, i, have, 0, m, 0); gs.insert({a[i].second, i}); } cout << ans << '\n'; }
14
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, U; char s[20]; struct table { int f, tp; string s; table(void) {} table(int _f, int t, string a) : f(_f), tp(t), s(a) {} inline bool operator<(const table &b) const { if (s.size() == b.s.size()) return s < b.s; return s.size() < b.s.size(); } }; table ans[N][4]; int vis[N][4]; set<table> S; set<table>::iterator it; vector<table> don; inline void Push(int f, int tp, string s) { table u = table(f, tp, s); if (!vis[f][tp]) { vis[f][tp] = 1; ans[f][tp] = u; S.insert(u); } else if (u < ans[f][tp]) { S.erase(ans[f][tp]); ans[f][tp] = u; S.insert(u); } } int main(void) { Push(240, 0, "x"); Push(204, 0, "y"); Push(170, 0, "z"); U = (1 << 8) - 1; while (!S.empty()) { it = S.begin(); table cur = *it; S.erase(it); Push(cur.f, 0, '(' + cur.s + ')'); if (cur.tp == 0) Push(cur.f ^ U, 0, '!' + cur.s); don.push_back(cur); for (table x : don) { if (cur.tp < 2 && x.tp < 2) Push(cur.f & x.f, 1, min(cur.s, x.s) + '&' + max(cur.s, x.s)); Push(cur.f | x.f, 2, min(cur.s, x.s) + '|' + max(cur.s, x.s)); } } for (int i = 0; i <= U; i++) ans[i][0] = min(ans[i][0], min(ans[i][1], ans[i][2])); scanf("%d", &n); while (n--) { scanf("%s", s); int f = 0; for (int i = 7; ~i; i--) f = f << 1 | (s[i] - '0'); cout << ans[f][0].s << endl; } return 0; }
16
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; cin >> n >> m; vector<string> in(n); for (int i = 0; i < n; i++) { cin >> in[i]; } vector<vector<bool> > grid(n, vector<bool>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = (in[i][j] == 'B'); } } vector<pair<int, int> > black; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (in[i][j] == 'B') black.push_back(make_pair(i, j)); sort(black.begin(), black.end()); bool ans = true; for (int i = 0; i < black.size(); i++) { for (int j = i + 1; j < black.size(); j++) { int r1 = black[i].first; int c1 = black[i].second; int r2 = black[j].first; int c2 = black[j].second; bool p1 = true; bool p2 = true; for (int k = min(r1, r2); k <= max(r1, r2); k++) p1 = p1 & grid[k][c1]; for (int k = min(r1, r2); k <= max(r1, r2); k++) p2 = p2 & grid[k][c2]; for (int k = min(c1, c2); k <= max(c1, c2); k++) p1 = p1 & grid[r2][k]; for (int k = min(c1, c2); k <= max(c1, c2); k++) p2 = p2 & grid[r1][k]; ans = ans & (p1 | p2); } } if (ans) cout << "YES"; else cout << "NO"; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10, modd = 1e9 + 7, inf = 0x3f3f3f3f, INF = 0x7fffffff, hmod1 = 0x48E2DCE7, hmod2 = 0x60000005; const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; const double eps = 1e-8; template <class T> inline void scand(T &x) { char c; x = 0; while ((c = getchar()) < '0') ; while (c >= '0' && c <= '9') x = x * 10 + (c - 48), c = getchar(); } inline long long min(long long a, long long b) { return a < b ? a : b; } inline long long max(long long a, long long b) { return a > b ? a : b; } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } inline long long exgcd(long long a, long long b, long long &x, long long &y) { long long d; (b == 0 ? (x = 1, y = 0, d = a) : (d = exgcd(b, a % b, y, x), y -= a / b * x)); return d; } inline long long qpow(long long a, long long n) { long long sum = 1; while (n) { if (n & 1) sum = sum * a % modd; a = a * a % modd; n >>= 1; } return sum; } inline long long qmul(long long a, long long n) { long long sum = 0; while (n) { if (n & 1) sum = (sum + a) % modd; a = (a + a) % modd; n >>= 1; } return sum; } inline long long inv(long long a) { return qpow(a, modd - 2); } inline long long madd(long long a, long long b) { return (a % modd + b % modd) % modd; } inline long long mmul(long long a, long long b) { return a % modd * b % modd; } inline void uadd(long long &a, long long b) { a = madd(a, b); } inline void umul(long long &a, long long b) { a = mmul(a, b); } long long l, p, num[maxn], len[2], tl[maxn], tr[maxn], ans[2][maxn]; long long get_next() { long long pp = p + 1; while (pp < l && num[pp + 1] == 0) pp++; return pp; } int cmp() { for (int i = len[0]; i >= 1; i--) { if (ans[0][i] < ans[1][i]) return 0; else if (ans[1][i] < ans[0][i]) return 1; } return 0; } void calc(long long k, int id) { memset(tl, 0, sizeof(tl)); memset(tr, 0, sizeof(tr)); long long pl = 0, pr = 0, t = 0; len[id] = max(k, l - k) + 1; for (int i = k; i >= 1; i--) tl[++pl] = num[i]; for (int i = l; i > k; i--) tr[++pr] = num[i]; for (int i = 1; i <= len[id]; i++) { long long res = tl[i] + tr[i] + t; ans[id][i] = res % 10; t = res / 10; } } void print(int id, long long len) { while (ans[id][len] == 0) len--; for (int i = len; i >= 1; i--) printf("%I64d", ans[id][i]); putchar('\n'); } int main() { cin >> l; for (int i = 1; i <= l; i++) scanf("%1I64d", &num[i]); p = 0; while (p <= l) { long long np = get_next(); if (abs(l - 2 * np) < abs(l - 2 * p)) p = np; else { calc(p, 0); calc(np, 1); if (len[0] != len[1]) { if (len[0] < len[1]) print(0, len[0]); else print(1, len[1]); } else { int id = cmp(); print(id, len[id]); } break; } } }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve(int n, int x1, int x2, const vector<pair<ll, int>> &c, bool sw = false) { vector<pair<int, int>> min_s1; for (int i = 0; i < n; ++i) { int y = c[i].first; int min_req = i + (x1 + y - 1) / y; min_s1.emplace_back(i, min_req); if (0) cerr << i << " " << y << " " << min_req << endl; } for (int i = n - 1; i >= 0; --i) { ll max_load = c[i].first * (n - i); if (0) cerr << i << " " << max_load << endl; if (max_load < x2) { continue; } if (0) cerr << i << " " << min_s1.back().first << endl; while (!min_s1.empty() && min_s1.back().second > i) { min_s1.pop_back(); } if (min_s1.empty()) { break; } cout << "Yes" << endl; int st1 = min_s1.back().first; int st2 = i; int k1 = i - st1; int k2 = n - i; if (sw) { swap(st1, st2); swap(k1, k2); } cout << k1 << " " << k2 << endl; for (int j = st1; j < st1 + k1; ++j) { cout << c[j].second << " "; } cout << endl; for (int j = st2; j < st2 + k2; ++j) { cout << c[j].second << " "; } cout << endl; exit(0); } } int main(void) { std::ios_base::sync_with_stdio(0); ll n, x1, x2; cin >> n >> x1 >> x2; vector<pair<ll, int>> c; c.resize(n); for (int i = 1; i <= n; ++i) { int x; cin >> x; c[i - 1] = {x, i}; } sort(c.begin(), c.end()); solve(n, x1, x2, c); solve(n, x2, x1, c, true); cout << "No" << endl; }
9
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { string s; cin >> s; int ans = 1; for (int i = 0; i <= 9; i++) { for (int j = 0; j <= 9; j++) { bool f = 0; int sum = 0; for (int k = 0; k < s.size(); k++) { if (s[k] - '0' == i || s[k] - '0' == j) { if (f == 0) { if (s[k] - '0' == i) { f = 1; sum++; } } else { if (s[k] - '0' == j) { f = 0; sum++; } } } } if (i != j && sum % 2) sum--; ans = max(ans, sum); } } cout << s.size() - ans << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; const int maxn = 2010; const int maxnode = 200010; long long ans, w[maxn], f[maxnode]; struct Point { long long w, rw; int r, c; bool operator<(const Point& tp) const { if (r != tp.r) return r < tp.r; else return c < tp.c; } } a[maxn]; void init() { f[0] = 1; for (int i = 1; i < maxnode; ++i) f[i] = f[i - 1] * i % M; } long long pow(long long a, long long b) { long long tp = a % M, ret = 1; while (b) { if (b & 1) ret = tp * ret % M; tp = tp * tp % M; b >>= 1; } return ret; } long long Cm(int p, int q) { long long n = p, m = q; if (m > n) return 0; return f[n] * pow(f[m] * f[n - m], M - 2) % M; } int r, c, n; int main() { init(); while (~scanf("%d%d%d", &r, &c, &n)) { for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].r, &a[i].c); sort(a, a + n); for (int i = 0; i < n; ++i) { a[i].w = Cm(a[i].r + a[i].c - 2, min(a[i].r - 1, a[i].c - 1)); a[i].rw = Cm(r + c - a[i].r - a[i].c, min(r - a[i].r, c - a[i].c)); } ans = Cm(r + c - 2, min(r - 1, c - 1)); for (int i = 0; i < n; ++i) { for (int j = i - 1; j >= 0; --j) { if (a[j].r <= a[i].r && a[j].c <= a[i].c) { long long w = Cm(a[i].r + a[i].c - a[j].r - a[j].c, min(a[i].r - a[j].r, a[i].c - a[j].c)); w = w * a[j].w % M; a[i].w = (a[i].w - w + M) % M; } } ans = (ans - (a[i].w * a[i].rw) % M + M) % M; } printf("%I64d\n", ans); } return 0; }
14
#include <bits/stdc++.h> using namespace std; int n, m, s, num[100100], ck, ans; pair<int, int> a, b; struct Kuai { int cnt[100100], num[400 << 1], size; inline void add(int u, int v) { int i; cnt[v]++, size++; for (i = size; i > u; i--) num[i] = num[i - 1]; num[u] = v; } inline void del(int u) { int i; cnt[num[u]]--, size--; for (i = u; i <= size; i++) num[i] = num[i + 1]; } inline int ask(int u, int v, int w) { int i, res = 0; for (i = u; i <= v; i++) res += (num[i] == w); return res; } } ku[400]; inline void build() { int i; for (i = 1; i <= n; i++) { ku[(i - 1) / s + 1].add((i - 1) % s + 1, num[i]); } } inline void rb() { int i, j, k = 0; for (i = 1; i <= ck; i++) { for (j = 1; j <= ku[i].size; j++) { num[++k] = ku[i].num[j]; ku[i].cnt[num[k]] = 0; } ku[i].size = 0; } build(); } int main() { int i, j, o, p, q, t, tmp; cin >> n; s = sqrt(n); ck = (n - 1) / s + 1; for (i = 1; i <= n; i++) scanf("%d", &num[i]); build(); cin >> m; for (i = 1; i <= m; i++) { if (i % s == 0) rb(); scanf("%d%d%d", &o, &p, &q); p = (p + ans - 1) % n + 1; q = (q + ans - 1) % n + 1; if (p > q) swap(p, q); if (o == 1) { if (p == q) continue; t = 0; for (j = 1;; j++) { if (t + ku[j].size >= q) break; t += ku[j].size; } tmp = ku[j].num[q - t]; ku[j].del(q - t); t = 0; for (j = 1;; j++) { if (t + ku[j].size >= p) break; t += ku[j].size; } ku[j].add(p - t, tmp); } else { scanf("%d", &o); o = (o + ans - 1) % n + 1; ans = 0; t = 0; for (j = 1;; j++) { if (t + ku[j].size >= p) break; t += ku[j].size; } a = make_pair(j, p - t); for (;; j++) { if (t + ku[j].size >= q) break; t += ku[j].size; } b = make_pair(j, q - t); if (a.first == b.first) ans = ku[a.first].ask(a.second, b.second, o); else { for (j = a.first + 1; j < b.first; j++) ans += ku[j].cnt[o]; ans += ku[a.first].ask(a.second, ku[a.first].size, o); ans += ku[b.first].ask(1, b.second, o); } printf("%d\n", ans); } } }
19
#include <bits/stdc++.h> using namespace std; int n, m, x, y, E[333333][5], g[333333], d[333333]; void dfs(int x) { int cnt = 0; for (int i = (1); i <= (d[x]); i++) if (g[x] == g[E[x][i]]) ++cnt; if (cnt >= 2) { g[x] ^= 1; for (int i = (1); i <= (d[x]); i++) if (g[x] == g[E[x][i]]) dfs(E[x][i]); } } int main() { while (~scanf("%d%d", &n, &m)) { fill(d, d + n, 0); fill(g, g + n, 0); for (int i = (1); i <= (m); i++) { scanf("%d%d", &x, &y), x--, y--; E[x][++d[x]] = y; E[y][++d[y]] = x; } for (int i = (0); i <= (n - 1); i++) dfs(i); for (int i = (0); i <= (n - 1); i++) printf("%d", g[i]); puts(""); } return 0; }
14
#include <bits/stdc++.h> using namespace std; void fun() {} long long int md = 1e9 + 7; long long int __gcd(long long int a, long long int b) { if (b == 0) return a; return __gcd(b, a % b); } long long int poww(long long int a, long long int b, long long int md) { if (b < 0) return 0; if (a == 0) return 0; long long int res = 1; while (b) { if (b & 1) { res = (1ll * res * a) % md; } a = (1ll * a * a) % md; b >>= 1; } return res; } long long int poww(long long int a, long long int b) { if (b < 0) return 0; if (a == 0) return 0; long long int res = 1; while (b) { if (b & 1) { res = (1ll * res * a); } a = (1ll * a * a); b >>= 1; } return res; } void ainp(long long int arr[], long long int n) { for (long long int i = 1; i <= n; i++) cin >> arr[i]; } void disp(long long int arr[], long long int n) { for (long long int i = 1; i <= n; i++) cout << arr[i] << " "; cout << "\n"; } void dispv(vector<long long int> &v) { for (long long int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << "\n"; } long long int divide(long long int a, long long int b, long long int md) { long long int rr = a * (poww(b, md - 2, md)); rr %= md; return rr; } const long long int size = 2001; long long int ans[size]; long long int k; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); fun(); cin >> k; k += 2000; cout << "2000\n-1 "; for (long long int i = 2; i <= 2000; i++) { long long int val = min(1000000ll, k); cout << val << " "; k -= val; } cout << "\n"; return 0; }
12
#include <bits/stdc++.h> using namespace std; const int maxx = 800010; const double pi = acos(-1.0); const double eps = 1e-15; long long t, n, m, k, a, b, q, a1, a2, a3; long long ans = 0, total = 0, sum = 0, cnt = 0; long long d[maxx], bj[maxx]; struct node { long long l; long long r; long long valx; long long valy; } tree[maxx]; void pushup(long long p) { tree[p].valx = tree[p << 1].valx + tree[p << 1 | 1].valx; tree[p].valy = tree[p << 1].valy + tree[p << 1 | 1].valy; } void build(long long p, long long l, long long r) { tree[p].l = l, tree[p].r = r; long long mid = (l + r) >> 1; if (l == r) return; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); pushup(p); } void update(long long p, long long c) { long long l = tree[p].l, r = tree[p].r; long long mid = (l + r) >> 1; if (l == r) { tree[p].valx = min(a, bj[l]); tree[p].valy = min(b, bj[l]); return; } if (c <= mid) update(p << 1, c); else update(p << 1 | 1, c); pushup(p); } long long query(long long p, long long L, long long R, long long opt) { if (L > R) return 0; long long l = tree[p].l, r = tree[p].r; if (L <= l && R >= r) { if (opt) return tree[p].valx; else return tree[p].valy; } long long res = 0, mid = (l + r) >> 1; if (L <= mid) res += query(p << 1, L, R, opt); if (R > mid) res += query(p << 1 | 1, L, R, opt); return res; } int main() { scanf("%lld%lld%lld%lld%lld", &n, &k, &a, &b, &q); build(1, 1, n); for (register int i = 1; i <= q; i++) { scanf("%lld", &a1); if (a1 == 1) { scanf("%lld%lld", &a2, &a3); bj[a2] += a3; update(1, a2); } else { scanf("%lld", &a2); long long u1 = query(1, 1, a2 - 1, 0), u2 = query(1, a2 + k, n, 1); ans = u1 + u2; printf("%lld\n", ans); } } return 0; }
9
#include <bits/stdc++.h> const int MAX_N = (int)2e5 + 123; const double eps = 1e-6; const int mod = (int)1e9 + 7; const int inf = (int)1e9 + 123; using namespace std; int n; int a[MAX_N], b[MAX_N]; int maxi[20][MAX_N], mini[20][MAX_N], lg[MAX_N]; void calc_max() { for (int i = 1; i <= n; i++) maxi[0][i] = a[i]; for (int it = 1; it < 20; it++) { for (int i = 1; i + (1 << it) - 1 <= n; i++) { maxi[it][i] = max(maxi[it - 1][i], maxi[it - 1][i + (1 << (it - 1))]); } } } void calc_min() { for (int i = 1; i <= n; i++) mini[0][i] = b[i]; for (int it = 1; it < 20; it++) { for (int i = 1; i + (1 << it) - 1 <= n; i++) { mini[it][i] = min(mini[it - 1][i], mini[it - 1][i + (1 << (it - 1))]); } } } int get_max(int l, int r) { int g = lg[r - l + 1]; return max(maxi[g][l], maxi[g][r - (1 << g) + 1]); } int get_min(int l, int r) { int g = lg[r - l + 1]; return min(mini[g][l], mini[g][r - (1 << g) + 1]); } int get_cnt_max(int id, int R) { int l = id, r = R, mid = -1, ans = -1; int val = get_max(id, R); while (l <= r) { mid = (l + r) / 2; if (get_max(id, mid) == val) { ans = mid; r = mid - 1; } else l = mid + 1; } assert(ans != -1); return ans; } int get_cnt_min(int id, int R) { int l = id, r = R, mid = -1, ans = -1; int val = get_min(id, R); while (l <= r) { mid = (l + r) / 2; if (get_min(id, mid) == val) { ans = mid; r = mid - 1; } else l = mid + 1; } assert(ans != -1); return ans; } long long int all; void calc(int id) { int l = id, r = n, mid = -1, ans = n + 1; while (l <= r) { mid = (l + r) / 2; if (get_max(id, mid) > get_min(id, mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } ans--; if (ans < id || get_max(id, ans) != get_min(id, ans)) return; all += (ans - max(get_cnt_max(id, ans), get_cnt_min(id, ans)) + 1); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); calc_max(), calc_min(); for (int i = 1, j = 0; i <= n; i++) { if ((1 << (j + 1)) == i) j++; lg[i] = j; } for (int i = 1; i <= n; i++) calc(i); printf("%lld", all); return 0; }
13
#include <bits/stdc++.h> using namespace std; long long a[1000001]; map<long long, long long> fr; int main() { long long n, i, ok = -1, nr = 0, act; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; fr[a[i]]++; if (fr[a[i]] >= 2) { nr++; ok = a[i]; } } if (nr >= 2 || ok == 0 || (ok > 0 && fr[ok - 1] >= 1)) { cout << "cslnb" << '\n'; return 0; } sort(a + 1, a + n + 1); act = 0; for (i = 1; i <= n; i++) { act = act + a[i] - i + 1; } if (act % 2 == 1) cout << "sjfnb" << '\n'; else cout << "cslnb" << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; int N; queue<int> D; queue<int> R; int main() { cin >> N; char c; for (int i = 0; i < N; ++i) { cin >> c; (c == 'D' ? D : R).push(i); } while (D.size() > 0 && R.size() > 0) { if (D.front() < R.front()) { R.pop(); D.push(D.front() + N); D.pop(); } else { D.pop(); R.push(R.front() + N); R.pop(); } } cout << (D.size() > 0 ? 'D' : 'R') << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<ll, ll>; using vi = vc<ll>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream& operator<<(ostream& os, const array<t, n>& first) { return os << vc<t>(first.begin(), first.end()); } template <ll i, class T> void print_tuple(ostream&, const T&) {} template <ll i, class T, class H, class... Args> void print_tuple(ostream& os, const T& t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, ll suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(ll n, ll off = 0) { vi v(n); for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; return v; } template <class T> void print(const vector<T>& v, ll suc = 1) { for (ll i = ll(0); i < ll(v.size()); i++) print(v[i], i == ll(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T& t) { return t * t; } void yes(bool ex = true) { cout << "Yes" << "\n"; if (ex) exit(0); } void no(bool ex = true) { cout << "No" << "\n"; if (ex) exit(0); } void possible(bool ex = true) { cout << "Possible" << "\n"; if (ex) exit(0); } void impossible(bool ex = true) { cout << "Impossible" << "\n"; if (ex) exit(0); } constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; const ll inf = infLL; ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } ll popcount(signed t) { return __builtin_popcount(t); } ll popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(ll i) { return i && (i & -i) == i; } ll mask(ll i) { return (ll(1) << i) - 1; } bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } template <class t> void mkuni(vc<t>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } ll rand_int(ll l, ll r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t>& first) { for (ll i = ll(0); i < ll(ll(first.size())); i++) swap(first[i], first[rand_int(0, i)]); } template <class t> ll lwb(const vc<t>& v, const t& first) { return lower_bound(v.begin(), v.end(), first) - v.begin(); } using uint = unsigned; using ull = unsigned long long; struct modinfo { uint mod, root; }; template <modinfo const& ref> struct modular { static constexpr uint const& mod = ref.mod; static modular root() { return modular(ref.root); } uint v; modular(ll vv = 0) { s(vv % mod + mod); } modular& s(uint vv) { v = vv < mod ? vv : vv - mod; return *this; } modular operator-() const { return modular() - *this; } modular& operator+=(const modular& rhs) { return s(v + rhs.v); } modular& operator-=(const modular& rhs) { return s(v + mod - rhs.v); } modular& operator*=(const modular& rhs) { v = ull(v) * rhs.v % mod; return *this; } modular& operator/=(const modular& rhs) { return *this *= rhs.inv(); } modular operator+(const modular& rhs) const { return modular(*this) += rhs; } modular operator-(const modular& rhs) const { return modular(*this) -= rhs; } modular operator*(const modular& rhs) const { return modular(*this) *= rhs; } modular operator/(const modular& rhs) const { return modular(*this) /= rhs; } modular pow(ll n) const { modular res(1), x(*this); while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } modular inv() const { return pow(mod - 2); } friend modular operator+(ll x, const modular& y) { return modular(x) + y; } friend modular operator-(ll x, const modular& y) { return modular(x) - y; } friend modular operator*(ll x, const modular& y) { return modular(x) * y; } friend modular operator/(ll x, const modular& y) { return modular(x) / y; } friend ostream& operator<<(ostream& os, const modular& m) { return os << m.v; } friend istream& operator>>(istream& is, modular& m) { ll x; is >> x; m = modular(x); return is; } bool operator<(const modular& r) const { return v < r.v; } bool operator==(const modular& r) const { return v == r.v; } bool operator!=(const modular& r) const { return v != r.v; } explicit operator bool() const { return v; } }; extern constexpr modinfo base{1000000007, 0}; using mint = modular<base>; const ll vmax = (1 << 21) + 10; mint fact[vmax], finv[vmax], invs[vmax]; void initfact() { fact[0] = 1; for (ll i = ll(1); i < ll(vmax); i++) { fact[i] = fact[i - 1] * i; } finv[vmax - 1] = fact[vmax - 1].inv(); for (ll i = vmax - 2; i >= 0; i--) { finv[i] = finv[i + 1] * (i + 1); } for (ll i = vmax - 1; i >= 1; i--) { invs[i] = finv[i] * fact[i - 1]; } } mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; } mint binom(ll first, ll second) { return fact[first + second] * finv[first] * finv[second]; } mint catalan(ll n) { return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0); } ll gcd(ll first, ll second) { return second ? gcd(second, first % second) : first; } template <class num> struct Vector { vector<num> v; Vector(ll s = 0) { v.resize(s, num(0)); } ll size() const { return v.size(); } num& operator[](ll i) { return v[i]; } num const& operator[](ll i) const { return v[i]; } Vector& operator+=(const Vector& rhs) { assert(size() == rhs.size()); for (ll i = ll(0); i < ll(size()); i++) v[i] += rhs[i]; return *this; } Vector& operator-=(const Vector& rhs) { assert(size() == rhs.size()); for (ll i = ll(0); i < ll(size()); i++) v[i] -= rhs[i]; return *this; } Vector& operator*=(const num& x) { for (ll i = ll(0); i < ll(size()); i++) v[i] *= x; return *this; } Vector& operator/=(const num& x) { num y = num(1) / x; for (ll i = ll(0); i < ll(size()); i++) v[i] *= y; return *this; } Vector operator+(const Vector& rhs) const { return Vector(*this) += rhs; } Vector operator-(const Vector& rhs) const { return Vector(*this) -= rhs; } Vector operator*(const num& x) const { return Vector(*this) *= x; } Vector operator/(const num& x) const { return Vector(*this) /= x; } bool operator==(const Vector& rhs) const { return v == rhs.v; } }; template <class num> num dot(const Vector<num>& first, const Vector<num>& second) { assert(first.size() == second.size()); const ll s = first.size(); num ans(0); for (ll i = ll(0); i < ll(s); i++) ans += first[i] * second[i]; return ans; } template <class num> ostream& operator<<(ostream& os, const Vector<num>& v) { return os << v.v; } template <class num> struct Matrix { using V = Vector<num>; vector<V> m; Matrix(ll s = 0, num z = num(0)) { m.resize(s, V(s)); for (ll i = ll(0); i < ll(size()); i++) m[i][i] = z; } ll size() const { return m.size(); } Matrix operator*(const Matrix& rhs) const { assert(size() == rhs.size()); Matrix res(size()); for (ll i = ll(0); i < ll(size()); i++) for (ll j = ll(0); j < ll(size()); j++) for (ll k = ll(0); k < ll(size()); k++) res[i][j] += m[i][k] * rhs[k][j]; return res; } Matrix& operator*=(const Matrix& rhs) { return *this = (*this) * rhs; } V operator*(const V& rhs) const { assert(size() == rhs.size()); V res(size()); for (ll i = ll(0); i < ll(size()); i++) res[i] = dot(m[i], rhs); return res; } V& operator[](ll i) { return m[i]; } V const& operator[](ll i) const { return m[i]; } Matrix& operator/=(const num& r) { for (ll i = ll(0); i < ll(m.size()); i++) m[i] /= r; return *this; } Matrix operator/(const num& r) const { return Matrix(*this) /= r; } bool operator==(const Matrix& rhs) const { return m == rhs.m; } }; template <class num> ostream& operator<<(ostream& os, const Matrix<num>& x) { const ll s = x.size(); os << "----------" << endl; for (ll i = ll(0); i < ll(s); i++) { for (ll j = ll(0); j < ll(s); j++) { os << x[i][j]; if (j == s - 1) { os << endl; } else { os << " "; } } } return os << "----------"; } template <class num> num det(Matrix<num> first) { const ll s = first.size(); num ans(1); for (ll i = ll(0); i < ll(s); i++) { for (ll j = ll(i + 1); j < ll(s); j++) if (first[j][i]) { ans = -ans; swap(first[j], first[i]); break; } if (!first[i][i]) return 0; ans *= first[i][i]; for (ll j = ll(i + 1); j < ll(s); j++) { mint w = -first[j][i] / first[i][i]; for (ll k = ll(i); k < ll(s); k++) first[j][k] += first[i][k] * w; } } return ans; } using V = Vector<mint>; using M = Matrix<mint>; M matpow(M first, ll n) { M res(ll(first.size()), 1); while (n) { if (n & 1) res *= first; first *= first; n >>= 1; } return res; } void slv() { ll n, k; cin >> n >> k; { ll g = gcd(n, k); n /= g; k /= g; } M x(5, 1), y(5, 1); x[1][4] = 1; x[2][0] = 1; x[3][1] = 1; y[0][0] = mint(2).inv(); y[1][1] = mint(2).inv(); M z; ll w = n, h = k; while (1) { if (w == 0) { z = matpow(y, h); break; } else if (h == 0) { z = matpow(x, w); break; } else if (w >= h) { y = matpow(x, w / h) * y; w %= h; } else { void(0); assert(w < h); x = x * matpow(y, h / w); h %= w; } } V tmp(5); tmp[0] = 1; tmp[4] = 1; V res = z * tmp; mint x0 = res[1] / (1 - res[0]); mint ans = x0 * res[2] + res[3]; ans /= n; ans += 1; print(ans); } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll t; cin >> t; for (ll _ = ll(0); _ < ll(t); _++) slv(); }
25
#include <bits/stdc++.h> using namespace std; const int N = 5000007; struct node { int l, r; int s; }; int a[N]; int sets[107]; node seg[N]; queue<int> garbage; int tt; int new_node() { int u; if (garbage.size()) u = garbage.front(), garbage.pop(); else u = ++tt; seg[u].l = seg[u].r = seg[u].s = 0; return u; } void add(int& cur, int l, int r, int j, int x) { if (cur == 0) cur = new_node(); if (l == r) { seg[cur].s += x; } else { if (j <= ((l + r) >> 1)) add(seg[cur].l, l, ((l + r) >> 1), j, x); else add(seg[cur].r, ((l + r) >> 1) + 1, r, j, x); seg[cur].s = (seg[cur].l ? seg[seg[cur].l].s : 0) + (seg[cur].r ? seg[seg[cur].r].s : 0); } } inline int sz(int x) { return x == 0 ? 0 : seg[x].s; } void split(int t1, int& t2, int l, int r, int x) { if (!t1) return; t2 = new_node(); if (((l + r) >> 1) == x) { swap(seg[t1].r, seg[t2].r); } else if (((l + r) >> 1) < x) { split(seg[t1].r, seg[t2].r, ((l + r) >> 1) + 1, r, x); } else { swap(seg[t1].r, seg[t2].r); split(seg[t1].l, seg[t2].l, l, ((l + r) >> 1), x); } if (l == r) { if (x < ((l + r) >> 1)) swap(seg[t1].s, seg[t2].s); } else { seg[t1].s = (seg[t1].l ? seg[seg[t1].l].s : 0) + (seg[t1].r ? seg[seg[t1].r].s : 0); seg[t2].s = (seg[t2].l ? seg[seg[t2].l].s : 0) + (seg[t2].r ? seg[seg[t2].r].s : 0); } } void merge(int& t1, int t2) { if (!t1 or !t2) return void(t1 = (t1 ? t1 : t2)); merge(seg[t1].l, seg[t2].l); merge(seg[t1].r, seg[t2].r); seg[t1].s += seg[t2].s; garbage.push(t2); } void go(int cur, int l, int r, int val = 0) { if (!cur) return; if (l == r) { if (seg[cur].s) { if (val == 0) cout << l << ' '; else a[l] = val; } } else { go(seg[cur].l, l, ((l + r) >> 1), val); go(seg[cur].r, ((l + r) >> 1) + 1, r, val); } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; add(sets[a[i]], 1, n, i, 1); } int q; cin >> q; while (q--) { int l, r, x, y; cin >> l >> r >> x >> y; int aux1 = 0, aux2 = 0; split(sets[x], aux1, 1, n, l - 1); split(aux1, aux2, 1, n, r); merge(sets[x], aux2); merge(sets[y], aux1); } for (int i = 1; i <= 100; ++i) { go(sets[i], 1, n, i); } for (int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << '\n'; }
17
#include <bits/stdc++.h> using namespace std; int a[15][15]; int main() { ios::sync_with_stdio(0); memset(a, 0, sizeof(a)); int r, c, n, k; cin >> r >> c >> n >> k; int ann = 0; for (int j = 0; j < n; j++) { int x, y; cin >> x >> y; x--; y--; a[x][y] = 1; } for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) for (int i1 = i; i1 < r; i1++) for (int j1 = j; j1 < c; j1++) { int ans = 0; for (int ii = i; ii <= i1; ii++) for (int jj = j; jj <= j1; jj++) if (a[ii][jj]) ans++; if (ans >= k) ann++; } cout << ann << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int res = 1; int op = 0; int op2 = 1; bool mul = false; int val = -1; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { res *= i; int count = 0; while (n % i == 0) { count++; n /= i; } if (val != -1 && val != count) mul = true; while (count > op2) { op2 *= 2; op++; } if (count != op2) mul = true; val = count; } } if (n != 1 && op != 0) mul = true; cout << res * n << " " << op + mul << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long int q; cin >> q; for (long long int t = 0; t < q; ++t) { long long int n; cin >> n; long long int a[n]; set<long long int> s; map<long long int, long long int> m; for (long long int i = 0; i < n; ++i) { cin >> a[i]; s.insert(a[i]); if (m.find(a[i]) != m.end()) { ++m[a[i]]; } else { m.insert(pair<long long int, long long int>(a[i], 1)); } } vector<long long int> v; auto r = s.begin(); for (r = s.begin(); r != s.end(); ++r) { v.push_back(m[*r]); } sort(v.begin(), v.end()); long long int zx = v.size(); long long int max = v[zx - 1], klj = 0; long long int sum = v[zx - 1]; for (long long int i = zx - 2; i >= 0 && klj == 0; --i) { if (v[i] >= max) { sum += (max - 1); --max; } else { sum += v[i]; max = v[i]; } if (max <= 0) ++klj; } cout << sum << endl; } }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, ans = 0; cin >> n; vector<int> a(n + 1, 0); for (int i = 1; i < n + 1; i++) cin >> a[i]; for (int i = 1; i < n + 1; i++) { long long prev = a[i - 1]; long long now = a[i]; if (prev == now) continue; if (prev > now) { ans += ((prev - now) * (now)); } else { ans += ((now - prev) * (n + 1 - now)); } } cout << ans, cout << "\n"; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
13
#include <bits/stdc++.h> using namespace std; int N; int main(void) { scanf("%d", &N); printf("%d\n", 6 * N * (N - 1) + 1); return 0; }
5
#include <bits/stdc++.h> using namespace std; int arr[200005]; int happy[200005]; inline long max(long x, long y) { return x > y ? x : y; } int main() { long N, W, K; while (scanf("%ld %ld %ld", &N, &W, &K) != EOF) { long result = 0, RESULT = 0; set<pair<int, int> > S_, S; for (int i = 1; i <= N; ++i) scanf("%d", happy + i); for (int i = 1; i <= N; ++i) scanf("%d", arr + i); int l, r; l = r = 1; while (r <= N) { while (r <= N) { if (W) { if (K >= (arr[r] + 1) / 2) { K -= (arr[r] + 1) / 2; RESULT = max(RESULT, result += happy[r]); S_.insert(make_pair(arr[r], r)); r++; W--; } else break; } else { int tmp = S_.begin()->first; if (tmp <= arr[r] && K >= (arr[r] + 1) / 2 - (tmp + 1) / 2 + tmp) { K -= (arr[r] + 1) / 2 - (tmp + 1) / 2 + tmp; RESULT = max(RESULT, result += happy[r]); auto p = S_.begin(); S.insert(*p); S_.erase(p); S_.insert(make_pair(arr[r], r)); r++; } else if (tmp > arr[r] && K >= arr[r]) { K -= arr[r]; RESULT = max(RESULT, result += happy[r]); S.insert(make_pair(arr[r], r)); r++; } else break; } } if (l < r) { if (S.find(make_pair(arr[l], l)) != S.end()) { K += arr[l]; result -= happy[l]; S.erase(make_pair(arr[l], l)); } else { K += (arr[l] + 1) / 2; result -= happy[l]; S_.erase(make_pair(arr[l], l)); W++; if (!S.empty()) { auto p = --S.end(); K += p->first - (p->first + 1) / 2; S_.insert(*p); W--; S.erase(p); } } l++; } else l++, r++; } printf("%d\n", RESULT); } }
14
#include <bits/stdc++.h> using namespace std; inline int read() { int x(0), w(1); char c = getchar(); while (c ^ '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') w = -1, c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return x * w; } int n, d, h, cur; int main() { n = read(), d = read(), h = read(); if (d > 2 * h || d < h) { printf("-1"); return 0; } if (d == 1 && n > 2) { printf("-1"); return 0; } cur = 1; for (int i = 1; i <= h; ++i) { printf("%d %d\n", cur, cur + 1); ++cur; } if (d > h) { ++cur; printf("%d %d\n", 1, cur); for (int i = 1; i < d - h; ++i) { printf("%d %d\n", cur, cur + 1); ++cur; } } while (cur + 1 <= n) { cur++; printf("%d %d\n", h, cur); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; vector<int> adj[MAXN << 1], door[MAXN]; stack<int> scc; int cur[MAXN], low[MAXN << 1], num[MAXN << 1], compID[MAXN << 1], vis[MAXN << 1]; int n, m, cnt = 0, idx = 0; template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } struct TwoSat { void addEdge(int u, int v) { adj[u ^ 1].push_back(v); adj[v ^ 1].push_back(u); } void tarjan(int u) { vis[u] = 1; scc.push(u); low[u] = num[u] = ++cnt; for (int i = 0; i < ((int)adj[u].size()); i++) { int v = adj[u][i]; if (!vis[v]) { tarjan(v); minimize(low[u], low[v]); } else if (vis[v] == 1) minimize(low[u], num[v]); } if (low[u] == num[u]) { int v; idx++; do { v = scc.top(); scc.pop(); compID[v] = idx; vis[v] = 2; } while (u - v); } } bool answer() { for (int i = 1; i <= (m << 1); i++) if (!vis[i]) tarjan(i); for (int i = 1; i <= m; i++) if (compID[i << 1] == compID[i << 1 | 1]) return false; return true; } void buildGR() { for (int i = 1; i <= n; i++) { if (((int)door[i].size()) != 2) continue; int u = door[i][0] << 1, v = door[i][1] << 1; if (!cur[i]) { addEdge(u, v); addEdge(u | 1, v | 1); } else { addEdge(u | 1, v); addEdge(u, v | 1); } } } }; TwoSat twosat; int main(void) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", cur + i); for (int t, u, i = 1; i <= m; i++) for (scanf("%d", &t); t; t--) scanf("%d", &u), door[u].push_back(i); twosat.buildGR(); bool flag = twosat.answer(); puts(flag ? "YES" : "NO"); }
12
#include <bits/stdc++.h> using namespace std; int main() { char A[100005], B[100005]; int n; cin >> A; cin >> B; n = strlen(A); int c4 = 0, c7 = 0; for (int i = 0; i < n; i++) { if (A[i] != B[i]) { if (A[i] == '4') c4++; else c7++; } } int a = (c4 > c7) ? c4 : c7; cout << a << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int combine(int n, int r) { long long int sum = 1; for (int i = 1; i <= r; i++) { sum *= n - i + 1; sum /= i; } return sum; } int main() { int x, y, c; cin >> x >> y >> c; long long int sum = 0; for (int i = 4; i < c; i++) { sum += combine(x, i) * combine(y, c - i); } cout << sum << endl; }
6
#include <bits/stdc++.h> using namespace std; ifstream in("in.in"); ofstream out("out.out"); string s; int solve() { cin >> s; int st = 0; int num = 0; for (int i = 0; i < s.length(); i++) { if ((st == 1) && (s[i] == 'a')) st = 2; if ((s[i] != 'a') && (st == 0)) st = 1; if (st == 1) s[i]--; } if (!st) s[s.length() - 1] = 'z'; cout << s; return 0; } int main() { ios::sync_with_stdio(0); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int d[maxn], t[maxn], n, k, maxk, root, mx[maxn], mn[maxn], sz[maxn]; bool flag[maxn]; vector<int> v[maxn]; queue<int> q; void dfs(int dep, int u, int f = 0) { sz[u] = 1; if (v[u].size() > 1) mn[u] = 1e9; for (int i = 0; i < v[u].size(); i++) { if (v[u][i] == f) continue; dfs(dep + 1, v[u][i], u); sz[u] += sz[v[u][i]]; mx[u] = max(mx[v[u][i]], mx[u]); mn[u] = min(mx[v[u][i]], mn[u]); } mx[u]++; mn[u]++; } bool dfs1(int dep, int u, int f = 0) { bool ret = 1; ret &= (mn[u] == mx[u]); if (dep == k) ret &= (sz[u] == 1); else if (u != root) ret &= (v[u].size() > 3); else ret &= (v[u].size() > 2); for (int i = 0; i < v[u].size(); i++) if (v[u][i] != f) ret &= dfs1(dep + 1, v[u][i], u); return ret; } int main() { scanf("%d%d", &n, &k); for (int i = 1, tmp1, tmp2; i < n; i++) { scanf("%d%d", &tmp1, &tmp2); v[tmp1].push_back(tmp2); v[tmp2].push_back(tmp1); d[tmp1]++; d[tmp2]++; } for (int i = 1; i <= n; i++) { if (d[i] == 1) { flag[i] = 1; q.push(i); t[i] = 0; } } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < v[u].size(); i++) { if (t[v[u][i]] != 0 && (fabs(t[u] - t[v[u][i]]) > 1)) { puts("No"); return 0; } if (flag[v[u][i]]) continue; if (t[v[u][i]] != 0 && t[v[u][i]] != t[u] + 1) { puts("No"); return 0; } t[v[u][i]] = t[u] + 1; if (t[v[u][i]] > k) { puts("No"); return 0; } if (t[v[u][i]] == k && root == 0) { root = v[u][i]; } if (t[v[u][i]] == k && root != 0 && root != v[u][i]) { puts("No"); return 0; } --d[v[u][i]]; if (root != v[u][i] && d[v[u][i]] == 1) { q.push(v[u][i]); flag[v[u][i]] = 1; } } } if (root == 0) { puts("No"); return 0; } dfs(0, root, 0); if (dfs1(0, root)) puts("Yes"); else puts("No"); }
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[101]; for (int i = 0; i < n; i++) { cin >> arr[i]; } if (n % 2 != 0 && arr[0] % 2 != 0) { if (arr[n - 1] % 2 != 0) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const double PI = acos(-1.0); const double epsilon = PI / 180.0; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int N = 3e5 + 10, M = 1e6 + 10; int n, q; int a[N]; int tr[N]; vector<pair<int, int> > query[N]; int ans[N]; int lowbit(const int& x) { return x & -x; } void add(const int& x, const int& d) { for (int i = x; i <= n + 1; i += lowbit(i)) tr[i] += d; } int sum(const int& x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += tr[i]; return res; } int getPos(const int& x, int l, int r) { if (r < l) return -1; while (l < r) { int mid = l + r + 1 >> 1; if (sum(mid) >= x) l = mid; else r = mid - 1; } if (sum(l) < x) return -1; return l; } int main() { cin >> n >> q; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] = i - a[i]; } for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; x++; y = n - y; query[y].push_back({x, i}); } for (int i = 1; i <= n; i++) { if (a[i] >= 0) { int pos = getPos(a[i], 1, i); if (pos != -1) { add(1, 1); add(pos + 1, -1); } } for (int j = 0; j < query[i].size(); j++) { ans[query[i][j].second] = sum(query[i][j].first); } } for (int i = 0; i < q; i++) cout << ans[i] << "\n"; return 0; }
15
#include <bits/stdc++.h> using namespace std; const int S = 450; int n, m, ans, a[100005], cnt[100005 / S + 3][100005 * 2], tag[100005 / S + 3], bel[100005]; int dep[100005], fa[100005], top[100005], son[100005], siz[100005], dfn[100005], tim; bool col[100005]; int fir[100005], nxt[100005]; void line(int x, int y) { nxt[y] = fir[x], fir[x] = y; } void dfs1(int u) { dep[u] = dep[fa[u]] + 1, siz[u] = 1; for (int v = fir[u]; v; v = nxt[v]) dfs1(v), siz[u] += siz[v], siz[v] > siz[son[u]] && (son[u] = v); } void dfs2(int u, int tp) { top[u] = tp, dfn[u] = ++tim; if (son[u]) dfs2(son[u], tp); for (int v = fir[u]; v; v = nxt[v]) if (!top[v]) dfs2(v, v); } void Rev(int i, int v) { col[i] ^= 1, cnt[bel[i]][a[i]] -= v; if (a[i] + tag[bel[i]] > n) ans -= v; } void brute(int l, int r, int v) { for (int i = l, x = bel[l]; i <= r; i++) { if (!col[i]) { ans += (a[i] + v + tag[x] > n) - (a[i] + tag[x] > n); cnt[x][a[i]]--, cnt[x][a[i] + v]++; } a[i] += v; } } void mdf(int l, int r, int v) { if (bel[l] == bel[r]) return brute(l, r, v); brute(l, (bel[l] + 1) * S, v), brute(bel[r] * S + 1, r, v); for (int i = bel[l] + 1; i < bel[r]; i++) { tag[i] += v; if (v > 0) ans += cnt[i][n + 1 - tag[i]]; else ans -= cnt[i][n - tag[i]]; } } int main() { scanf("%d%d", &n, &m); for (int i = 2; i <= n; i++) scanf("%d", &fa[i]), line(fa[i], i); dfs1(1), dfs2(1, 1); for (int i = 1, x; i <= n; i++) scanf("%d", &x), a[dfn[i]] = n - x; for (int i = 1; i <= n; i++) cnt[bel[i] = (i - 1) / S][a[i]]++; for (int x, y; m--;) { scanf("%d", &x), y = x > 0 ? 1 : -1, x = abs(x); Rev(dfn[x], y); for (; x; x = fa[top[x]]) mdf(dfn[top[x]], dfn[x], y); printf("%d%c", ans, !m ? 10 : 32); } }
21
#include <bits/stdc++.h> using namespace std; void Fast_IO() { ios::sync_with_stdio(0); cin.tie(0); } class sparseTable { public: long long n, k = 25; vector<vector<long long> > st; long long init() { return -1000000000; } long long compute(long long a, long long b) { return max(a, b); } sparseTable(vector<long long> a, long long N) { st.clear(); n = N; st.resize(n); for (long long i = (long long)0; i <= (long long)n - 1; i++) st[i].resize(k + 1), st[i][0] = a[i]; for (long long j = (long long)1; j <= (long long)k; j++) for (long long i = (long long)0; i <= (long long)n - (1 << j); i++) st[i][j] = compute(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } long long rq(long long left, long long right) { long long result = init(); for (long long j = (long long)k; j >= (long long)0; j--) if ((1 << j) <= right - left + 1) result = compute(result, st[left][j]), left += (1 << j); return result; } }; int main() { Fast_IO(); long long N, w; cin >> N >> w; vector<vector<long long> > adj(N); for (long long i = (long long)0; i <= (long long)N - 1; i++) { long long Li; cin >> Li; while (Li--) { long long x; cin >> x; adj[i].push_back(x); } } long long ans[w + 1]; for (long long i = (long long)0; i <= (long long)w; i++) ans[i] = 0; for (auto v : adj) { long long n = (long long)int(v.size()); sparseTable st(v, n); if (n <= w - 1 - n) { long long maxi = st.rq(0, n - 1); maxi = max(maxi, 0LL); ans[n] += maxi; ans[w - n] -= maxi; } for (long long i = (long long)0; i <= (long long)n - 1; i++) { long long l = 0, r = i; long long l2 = 0, r2 = n - 1; if (i >= w - n) l2 = max(l2, i - w + n); l = max(l, l2); r = min(r, r2); if (l <= r) { long long range = st.rq(l, r); if (i < w - n) range = max(range, 0LL); ans[i] += range; ans[i + 1] -= range; } } for (long long i = (long long)0; i <= (long long)n - 1; i++) { if (w - 1 - i <= n - 1) continue; long long r = n - 1, l = n - 1 - i; if (l <= r) { long long range = st.rq(l, r); if (w - 1 - i > n - 1) range = max(range, 0LL); ans[w - 1 - i] += range; ans[w - i] -= range; } } } for (long long i = (long long)1; i <= (long long)w; i++) ans[i] += ans[i - 1]; for (long long i = (long long)0; i <= (long long)w - 1; i++) cout << ans[i] << " "; return 0; }
14
#include <bits/stdc++.h> using namespace std; int N, M, K; char a[1005][1005]; long long cnt[26][1005][1005]; long long cst[1005][1005]; pair<int, pair<pair<int, int>, pair<int, int> > > qry[300005]; long long scst[1005][1005]; long long sless[1005][1005], cless[1005][1005], ssless[1005][1005], scless[1005][1005]; long long smore[1005][1005], cmore[1005][1005], ssmore[1005][1005], scmore[1005][1005]; int main() { scanf("%d%d%d\n", &N, &M, &K); for (int i = 1; i <= N; i++) { gets(a[i] + 1); for (int j = 1; j <= M; j++) a[i][j] -= 'a'; } for (int i = 1; i <= K; i++) { int x1, y1, x2, y2; char ch; scanf("%d%d%d%d %c", &x1, &y1, &x2, &y2, &ch); ch -= 'a'; qry[i] = {int(ch), {{x1, y1}, {x2, y2}}}; cnt[ch][x1][y1]++; cnt[ch][x1][y2 + 1]--; cnt[ch][x2 + 1][y1]--; cnt[ch][x2 + 1][y2 + 1]++; } sort(qry + 1, qry + K + 1); for (int k = 0; k < 26; k++) for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) cnt[k][i][j] += cnt[k][i - 1][j] + cnt[k][i][j - 1] - cnt[k][i - 1][j - 1]; for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) { long long tot = 0; for (int k = 0; k < 26; k++) tot += cnt[k][i][j]; cnt[a[i][j]][i][j] += (K - tot); } for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) { for (int k = 0; k < 26; k++) cst[i][j] += cnt[k][i][j] * abs(k - a[i][j]); scst[i][j] = scst[i - 1][j] + scst[i][j - 1] - scst[i - 1][j - 1] + cst[i][j]; } for (int k = 1; k < 26; k++) for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) { cmore[i][j] += cnt[k][i][j]; smore[i][j] += cnt[k][i][j] * k; scmore[i][j] = scmore[i - 1][j] + scmore[i][j - 1] - scmore[i - 1][j - 1] + cmore[i][j]; ssmore[i][j] = ssmore[i - 1][j] + ssmore[i][j - 1] - ssmore[i - 1][j - 1] + smore[i][j]; } int ch = 0; long long ans = 1LL << 61; for (int i = 1; i <= K; i++) { int c = qry[i].first; int x1 = qry[i].second.first.first; int y1 = qry[i].second.first.second; int x2 = qry[i].second.second.first; int y2 = qry[i].second.second.second; while (ch < c) { ch++; for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) { cless[i][j] += cnt[ch - 1][i][j]; sless[i][j] += cnt[ch - 1][i][j] * (ch - 1); scless[i][j] = scless[i - 1][j] + scless[i][j - 1] - scless[i - 1][j - 1] + cless[i][j]; ssless[i][j] = ssless[i - 1][j] + ssless[i][j - 1] - ssless[i - 1][j - 1] + sless[i][j]; cmore[i][j] -= cnt[ch][i][j]; smore[i][j] -= cnt[ch][i][j] * ch; scmore[i][j] = scmore[i - 1][j] + scmore[i][j - 1] - scmore[i - 1][j - 1] + cmore[i][j]; ssmore[i][j] = ssmore[i - 1][j] + ssmore[i][j - 1] - ssmore[i - 1][j - 1] + smore[i][j]; } } long long cost1 = scst[N][M] - (scst[x2][y2] - scst[x2][y1 - 1] - scst[x1 - 1][y2] + scst[x1 - 1][y1 - 1]); long long cost2 = 1LL * c * (scless[x2][y2] - scless[x2][y1 - 1] - scless[x1 - 1][y2] + scless[x1 - 1][y1 - 1]) - (ssless[x2][y2] - ssless[x1 - 1][y2] - ssless[x2][y1 - 1] + ssless[x1 - 1][y1 - 1]); long long cost3 = (ssmore[x2][y2] - ssmore[x1 - 1][y2] - ssmore[x2][y1 - 1] + ssmore[x1 - 1][y1 - 1]) - 1LL * c * (scmore[x2][y2] - scmore[x2][y1 - 1] - scmore[x1 - 1][y2] + scmore[x1 - 1][y1 - 1]); long long cost = cost1 + cost2 + cost3; ans = min(ans, cost); } printf("%I64d\n", ans); return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x; long long w = 0, b = 0; for (int i = 0; i < n; ++i) { cin >> x; if (x % 2 == 0) { w += x / 2; b += x / 2; } else if (i % 2) { w += x / 2; b += x / 2 + 1; } else { w += x / 2 + 1; b += x / 2; } } cout << min(w, b) << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 1010; int n, cnt1, cnt2; pair<long long, long long> l[N], r[N]; struct point { long long x, y; inline point operator-(const point &p) { return (point){x - p.x, y - p.y}; } } p[N]; inline long long cross(point a, point b) { return a.x * b.y - a.y * b.x; } inline long long query(int opt, int a, int b, int c) { printf("%d %d %d %d\n", opt, a, b, c), fflush(stdout); return read(); } int main() { n = read(); int pos = 2; for (register int i = (3); i <= (n); i++) if (query(2, 1, i, pos) > 0) pos = i; vector<pair<long long, long long> > v; for (register int i = (2); i <= (n); i++) if (i != pos) v.push_back(make_pair(query(1, 1, pos, i), i)); sort((v).begin(), (v).end()); vector<int> l, r; for (register int i = (0); i < (((int)(v).size()) - 1); i++) if (query(2, 1, v[i].second, v[i + 1].second) == 1) l.push_back(v[i].second); else r.push_back(v[i].second); l.push_back(v.back().second); printf("0 %d %d ", 1, pos); for (auto i : l) printf("%d ", i); reverse((r).begin(), (r).end()); for (auto i : r) printf("%d ", i); }
15
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long dp[2000001]; bool is_prime[2000001]; void seive(long long N) { for (long long i = 0; i < N; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (long long i = 2; i * i <= N; i++) { if (is_prime[i] == true) { for (long long j = i * i; j <= N; j += i) { is_prime[j] = false; } } } } long long power(long long a, long long b, long long p) { if (a == 0) return 0; long long res = 1; a %= p; while (b > 0) { if (b & 1) res = (res * a) % p; b >>= 1; a = (a * a) % p; } return res; } void solve() { string u, v; cin >> u >> v; long long cnt = 0; for (long long i = 0; i < u.size(); i++) { if (u[i] != v[i]) cnt++; } if (cnt % 2 == 1) { cout << "impossible" << endl; return; } string p = ""; long long fg = 1; for (long long i = 0; i < u.size(); i++) { if (u[i] != v[i] && fg == 1) { p += u[i]; fg = 0; } else if (u[i] != v[i] && fg == 0) { p += v[i]; fg = 1; } else p += v[i]; } cout << p << endl; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\n"; ; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long exgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long ans = exgcd(b, a % b, x, y); long long tmp = x; x = y; y = tmp - a / b * y; return ans; } long long a[maxn]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); long long Max = 1e18; int f = 0; for (long long i = 1; i <= 1e5; i++) { long long num = 1; long long sum = 0; for (int j = 0; j < n; j++) { if (num > 1e14) { f = 1; break; } sum += abs(num - a[j]); num *= i; } if (f == 1) break; Max = min(sum, Max); } cout << Max << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, m, k, s; const int maxk = 10, maxm = 2005; int val[maxk][maxk]; int dp[2][maxm], ip[maxm][maxm], ptr; void solve1(int first, int second) { for (int i = 0; i < maxm; i++) dp[0][i] = dp[1][i] = 3e6; ptr = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { dp[1 - ptr][j] = min(dp[1 - ptr][j - 1], dp[ptr][j]); if (ip[i][j] == first) { dp[1 - ptr][j] = min(dp[1 - ptr][j], i + j); } if (ip[i][j] == second) { val[first][second] = max(val[first][second], i + j - dp[1 - ptr][j]); } } ptr = 1 - ptr; } } void solve2(int first, int second) { for (int i = 0; i < maxm; i++) dp[0][i] = dp[1][i] = -3e6; ptr = 0; for (int i = m; i >= 1; i--) { for (int j = 1; j <= n; j++) { dp[1 - ptr][j] = max(dp[1 - ptr][j - 1], dp[ptr][j]); if (ip[i][j] == first) dp[1 - ptr][j] = max(dp[1 - ptr][j], i - j); if (ip[i][j] == second) { val[first][second] = max(val[first][second], -i + j + dp[1 - ptr][j]); } } ptr = 1 - ptr; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> m >> n >> k >> s; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) cin >> ip[i][j]; } for (int i = 1; i <= k; i++) { for (int j = 1; j <= k; j++) { solve1(i, j); solve2(i, j); } } int ans = 0, pre, cur; cin >> pre; for (int i = 2; i <= s; i++) { cin >> cur; ans = max(ans, max(val[pre][cur], val[cur][pre])); pre = cur; } cout << ans; return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int b, p, f, h, c; cin >> b >> p >> f >> h >> c; int t2 = min(b / 2, f); int ans = t2 * c + min((b - t2 * 2) / 2, p) * h; int t1 = min(b / 2, p); b -= t1 * 2; ans = max(ans, t1 * h + min(b / 2, f) * c); while (t1--) { b += 2; ans = max(ans, t1 * h + min(b / 2, f) * c); } cout << ans << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k, r; cin >> n >> k; if (n == k) cout << n; else cout << 2; cout << "\n"; ; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<long long> a; vector<pair<long long, long long>> v; long long b; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> b; a.push_back(b); } long long m = a[0]; long long idx; long long flag = 0; long long cnt = 0; for (long long i = 0; i < n - 1; i++) { flag = 0; m = a[i]; for (long long j = i + 1; j < n; j++) { if (a[j] < m) { flag = 1; m = a[j]; idx = j; } } if (flag == 1) { swap(a[i], a[idx]); v.push_back(make_pair(i, idx)); cnt++; } } cout << cnt << endl; for (long long i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << endl; } }
4
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout << setprecision(20); long long int t = 1; cin >> t; while (t--) { long long int n, g, b, ans = 0; cin >> n >> g >> b; long long int h = (n + 1) / 2; if (h <= g) cout << n; else { long long int w = h / g, l = (n) / 2; ans = (w) * (g) + ((w - 1) * b); if (h % g != 0) { h -= (w * g); w++; ans += b; } else h -= (w * g); if ((w - 1) * b - l >= 0) l = 0; else { l -= (w - 1) * b; } ans += h + l; cout << ans; } cout << endl; } return 0; }
6
#include <bits/stdc++.h> const int N = (int)1e5 + 228; using namespace std; int a1[N], a2[N], sz; long long n, p[N], ans, nn; void rec2(int pos, long long x, long long y) { if (pos == sz + 1) { long long z = nn / x / y; if ((x + y + z) % 2 == 0 && x > 1 && y > 1 && z > 1 && x + y - z > 0 && y + z - x > 0 && x + z - y > 0) { ans++; } return; } for (int i = 0; i <= a2[pos]; i++) { rec2(pos + 1, x, y); y *= p[pos]; } } void rec(int pos, long long x) { if (pos == sz + 1) { if (x > 1) rec2(1, x, 1); return; } for (int i = 0; i <= a1[pos]; i++) { a2[pos] = a1[pos] - i; rec(pos + 1, x); x *= p[pos]; } } int main() { cin >> n; nn = n / 3; if (n % 3 != 0) { cout << 0 << endl; return 0; } n /= 3; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { p[++sz] = i; while (n % i == 0) { n /= i; a1[sz]++; } } } if (n != 1) { p[++sz] = n; a1[sz] = 1; } rec(1, 1); cout << ans << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { long long int N; cin >> N; vector<long long int> Numbers(N); map<long long int, long long int> Count; for (long long int i = 0; i < N; i++) { cin >> Numbers[i]; Count[Numbers[i]]++; } long long int Max = 0; for (auto i : Numbers) Max = max(Max, Count[i]); cout << N - Max; }
5
#include <bits/stdc++.h> using namespace std; int l, r; vector<long long int> tt; void generate() { long long int x = 2; int idx; tt.push_back(1); while (x <= 2000000000ll) { tt.push_back(x); x *= 2; } idx = tt.size(); x = 3; while (x <= 2000000000ll) { tt.push_back(x); x *= 3; } int i, j; int n = tt.size(); for (i = 1; i < idx; i++) { for (j = idx; j < n; j++) { if (tt[i] * tt[j] > 2000000000ll) break; tt.push_back(tt[i] * tt[j]); } } } void solve() { int ans = 0; for (long long int x : tt) { if (x >= l && x <= r) ans++; } cout << ans << "\n"; } int main() { cin >> l >> r; generate(); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const int W = 12, N = 6e5; int countOfA[1 << W], numOfP[N]; char s[W + 1]; map<char, int> st = {{'A', 0}, {'O', 0}, {'X', 0}, {'a', 2}, {'o', 1}, {'x', 1}}, add = {{'A', 1}, {'O', -1}, {'X', 2}, {'a', -1}, {'o', 1}, {'x', -1}}; int dp[1 << W]; int main() { int w, n, m; scanf("%d%d%d", &w, &n, &m); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); countOfA[x]++; } for (int i = 0; i < (1 << w); i++) { for (int j = 0; j < (1 << w); j++) { int x = 0; for (int k = 0; k < w; k++) { x = x * 3 + ((i >> k) & 1) + ((j >> k) & 1); } numOfP[x] += countOfA[i] * countOfA[j]; } } for (int i = 0; i < m; i++) { scanf(" %s", s); int y = 0, d = 0; for (int j = 0, p = 1; j < w; j++, p *= 3) { if (add[s[j]] == -1) y |= 1 << j; dp[1 << j] = add[s[j]] * p; d += st[s[j]] * p; } long long int ans = numOfP[d]; for (int j = 1; j < (1 << w); j++) { dp[j] = dp[j & -j] + dp[j ^ (j & -j)]; if ((j & y) == 0) { ans += numOfP[dp[j] + d]; } } printf("%lld\n", ans); } return 0; }
20
#include <bits/stdc++.h> using namespace std; int main() { int n, sum = 0, l = 0; cin >> n; int q = 1; while ((sum + ((q * (q + 1)) / 2)) <= n) { sum += ((q * (q + 1)) / 2); q++; l++; } cout << l << endl; }
0