func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; string vow = aeiou ; long long month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const long long dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; const long long dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; const long long dx4[] = {-1, 0, 1, 0}; const long long dy4[] = {0, 1, 0, -1}; const long long dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const long long dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1}; const long double pie = 3.1415926535897932384626; const long long special_prime = 982451653l; const long long mod = 1e9 + 7; const long long inf = 2e18; template <class T> istream &operator>>(istream &in, vector<T> &a) { for (auto &i : a) cin >> i; return in; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { for (auto &i : a) cout << i << ; return out; } template <class T, class U> ostream &operator<<(ostream &out, const map<T, U> &a) { for (auto &i : a) cout << ( << i.first << , << i.second << ) n ; return out; } template <class T, class U> ostream &operator<<(ostream &out, const unordered_map<T, U> &a) { for (auto &i : a) cout << ( << i.first << , << i.second << ) n ; return out; } long long GCD(long long a, long long b) { if (!b) return a; return GCD(b, a % b); } long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); } bool comp(long long a, long long b) { return a > b; } void sieve(vector<long long> &prime) { prime[0] = false; prime[1] = false; for (long long p = 2; p * p <= 99999; p++) { if (prime[p] == true) { for (long long i = p * p; i <= 99999; i += p) prime[i] = false; } } } void solve() { long long n, g, b; cin >> n >> g >> b; long long d = n / 2 + n % 2; if (d % g) cout << max(n, (d / g) * (g + b) + d % g) << n ; else cout << max((d / g) * (g + b) - b, n) << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; cout << setprecision(15) << fixed; for (long long i = 1; i <= t; i++) solve(); } |
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; const int N = (int)1e6 + 6; int f[30][N]; int pref[30][N]; int lp[N]; void precalc() { pref[0][0] = f[0][0] = 1; for (int i = 1; i < 30; i++) { f[i][0] = 2; pref[i][0] = f[i][0] + pref[i - 1][0]; pref[i][0] -= pref[i][0] >= mod ? mod : 0; } for (int level = 1; level < N; level++) { pref[0][level] = f[0][level] = 1; for (int pw = 1; pw < 30; pw++) { f[pw][level] = pref[pw][level - 1]; pref[pw][level] = f[pw][level] + pref[pw - 1][level]; pref[pw][level] -= pref[pw][level] >= mod ? mod : 0; } } for (int i = 2; i < N; i++) { if (lp[i]) continue; for (int j = i; j < N; j += i) { lp[j] = lp[j] ? lp[j] : i; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); precalc(); int T; scanf( %d , &T); while (T--) { int r, n; scanf( %d%d , &r, &n); if (n == 1) { printf( 1 n ); continue; } long long ans = 1; while (n > 1) { int p = lp[n]; int pw = 0; while (lp[n] == p) { n /= p; ++pw; } ans = ans * f[pw][r] % mod; } printf( %lld n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } const int MAXN = 3e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e7 + 9; int a[MAXN]; int main() { int n; scanf( %d , &n); for (int i = (1); i < (n + 1); ++i) { scanf( %d , &a[i]); } int ans1 = 0; int t1 = 1; for (int i = (n); i > (0); --i) { if (a[t1] != a[i]) { ans1 = i - t1; break; } else { t1++; i++; } } int ans2 = 0; int t2 = n; for (int i = (1); i < (n + 1); ++i) { if (a[t2] != a[i]) { ans2 = t2 - i; break; } else { t2--; i--; } } printf( %d n , max(ans1, ans2)); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n, sum = 1; cin >> n; sum += 6 * ((n * (n + 1)) / 2); cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 500005; const int M = 998244353; int n, a[N], i, s; long long f[N], c[N], x, y, z, ans[N], tp; long long qpow(long long a, long long b) { long long s = a, ans = 1; while (b) { if (b & 1) ans = ans * s % M; s = s * s % M; b >>= 1; } return ans; } int main() { scanf( %d , &n); for (i = 1; i <= n; ++i) { scanf( %d , &a[i]); s += a[i]; } long long is = qpow(s, M - 2), in = qpow(n - 1, M - 2); c[1] = 1 - n; f[0] = f[1] = 1; for (i = 1; i < s; ++i) { x = i * is % M, y = (s - i) * is % M * in % M; z = qpow(y, M - 2); f[i + 1] = (f[i] * (x + y) - f[i - 1] * x) % M * z % M; c[i + 1] = (c[i] * (x + y) - 1 - c[i - 1] * x) % M * z % M; } ans[0] = -c[s] * qpow(f[s], M - 2) % M; for (i = 1; i < s; ++i) ans[i] = (f[i] * ans[0] + c[i]) % M; for (i = 1; i <= n; ++i) tp += ans[a[i]]; tp -= ans[0] * (n - 1); cout << (tp % M * qpow(n, M - 2) % M + M) % M; } |
#include <bits/stdc++.h> using namespace std; long long w[2], b[2], q[2], mod = 998244353, fac[100001]; long long modpow(long long b, long long p) { long long ret = 1; while (p > 0) { if (p & 1) ret = ret * b % mod; b = b * b % mod; p >>= 1; } return ret; } long long choose(long long n, long long m) { long long denom = modpow(fac[m], mod - 2) * modpow(fac[n - m], mod - 2) % mod; return (fac[n] * denom % mod); } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, qq = 0; bool bw = true, wb = true, both = true; string s; fac[0] = 1; for (int i = 1; i < 100001; i++) fac[i] = fac[i - 1] * i % mod; cin >> n; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < 2; j++) { if (s[j] == W ) w[j]++; if (s[j] == B ) b[j]++; if (s[j] == ? ) q[j]++; } if (s == ?? ) qq++; if (s == WW || s == BB ) bw = wb = both = false; if (s == W? || s == ?B || s == WB ) bw = false; if (s == B? || s == ?W || s == BW ) wb = false; } if (n == 1) { if (s == WW | s == BB ) cout << 0; else if (s == ?? ) cout << 2; else cout << 1; return 0; } long long ans = 0; for (int i = 0; i < q[0] + 1; i++) { if (w[0] + i < b[1] || w[0] + i > b[1] + q[1]) continue; ans = (ans + choose(q[0], i) * choose(q[1], w[0] + i - b[1])) % mod; } if (both) { ans -= modpow(2, qq); if (bw) ans++; if (wb) ans++; } ans = ((ans % mod) + mod) % mod; cout << ans; } |
#include <bits/stdc++.h> using std::abs; using std::bitset; using std::cerr; using std::cin; using std::copy; using std::cout; using std::endl; using std::fill; using std::fixed; using std::greater; using std::lower_bound; using std::map; using std::max; using std::min; using std::next_permutation; using std::pair; using std::priority_queue; using std::queue; using std::reverse; using std::rotate; using std::set; using std::sort; using std::string; using std::swap; using std::unique; using std::unordered_map; using std::unordered_set; using std::upper_bound; using std::vector; int const INF = (int)1e9; long long const INFL = (long long)1e18; long double const PI = 3.1415926535897932384626433832795028; bool isPrime(int x) { for (int a = 2; a * a <= x; ++a) { if (x % a == 0) { return false; } } return true; } int const N = 100100; int len[N]; int g[N], next[N]; int find(int v) { if (next[v] != v) { int root = find(next[v]); len[v] += len[next[v]]; next[v] = root; } return next[v]; } int v[N], res[N]; bool did[N]; int calc(int a, int b) { int ret = 0; find(a); b -= len[a]; a = next[a]; while (b > 0 && !did[a]) { ++ret; did[a] = true; next[a] = find(g[a]); len[a] = 1 + len[g[a]]; b -= len[a]; a = find(g[a]); } return ret; } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { next[i] = i; len[i] = 0; cin >> g[i]; --g[i]; } for (int i = 1; i <= m; ++i) { cin >> v[i]; } for (int i = 1; i <= m; ++i) { int b; cin >> b; int a = (v[i] + res[i - 1] - 1) % n; res[i] = calc(a, b); } for (int i = 1; i <= m; ++i) { cout << res[i] << n ; } } int main() { solve(); } |
#include <bits/stdc++.h> using namespace std; bool vis[305][305]; string s[305]; int len[305][305]; int d[305]; int hs[305][305]; int n; const int mod = 9e8 + 7; int qpow(int x, int y) { long long ret = 1; long long now = x; while (y) { if (y & 1) ret = ret * now % mod; now = now * now % mod; y >>= 1; } return ret; } void init() { memset(vis, false, sizeof(vis)); for (int i = 1; i <= n; i++) { d[i] = 0; len[i][0] = s[i].size(); for (int j = 0; j < s[i].size(); j++) { d[i] = (d[i] * 233LL + s[i][j] - a + 1) % mod; } } for (int i = 1; i <= n; i++) { hs[i][0] = d[i]; for (int j = 1; j + i <= n; j++) { len[i][j] = len[i + j][0] + len[i][j - 1]; hs[i][j] = (1LL * hs[i][j - 1] * 2333LL + d[i + j]) % mod; } } } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) cin >> s[i]; init(); int ans = 0; for (int i = 1; i <= n; i++) { int ls = 0; for (int j = 0; i + j <= n; j++) { ls += len[i + j][0]; int nx = i + j + 1; int cnt = 1; while (nx + j <= n) { if (hs[i][j] == hs[nx][j] && len[i][j] == len[nx][j]) { cnt++; nx += j + 1; } else { nx++; } } if (cnt > 1) ans = max(ans, (ls - 1) * cnt); } } ans = n - 1 - ans; for (int i = 1; i <= n; i++) ans += len[i][0]; cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> v[100005]; int p[100005]; int h[100005]; int happy[100005]; int sad[100005]; bool vis[100005]; bool ok; void init(int n) { for (int i = 1; i < n + 1; i++) { v[i].clear(); vis[i] = false; } ok = true; } void dfs(int node) { vis[node] = true; long long int happyChild = 0; long long int sadChild = p[node]; for (int child : v[node]) { if (vis[child]) continue; dfs(child); happyChild += happy[child]; sadChild += sad[child]; } long long int gg = (long long int)h[node] - (happyChild - sadChild); if (gg >= 0 && gg % 2 == 0 && gg / 2 <= sadChild) { long long int x = gg / 2; happy[node] = x + happyChild; sad[node] = sadChild - x; } else { ok = false; return; } } int main() { int t, n, m, a, b; scanf( %d , &t); while (t--) { scanf( %d %d , &n, &m); init(n); for (int i = 1; i < n + 1; i++) scanf( %d , &p[i]); for (int i = 1; i < n + 1; i++) scanf( %d , &h[i]); for (int i = 0; i < n - 1; i++) { scanf( %d %d , &a, &b); v[a].push_back(b); v[b].push_back(a); } dfs(1); if (ok) puts( YES ); else puts( NO ); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long j, k, a, b, s, ss, l, m, n, t, d; long long e, i, c[221]; vector<long long> p[111], q[111], r, v; char w; map<long long, long long> y, z; long long too(long long i, long long j) { long long l = 0, d = 0; for (k = 0; k < q[i].size(); k++) { l += q[i][k]; r.push_back(l); } l = 0; for (k = 0; k < q[j].size(); k++) { l += q[j][k]; r.push_back(l); } l = r.size(); sort(r.begin(), r.end()); for (k = 0; k < l; k++) c[k] = 0; for (k = 0; k < l; k++) { long long x, a = 0, b = 0, e = 0; x = r[k]; while (x > q[i][e] && e < q[i].size()) { a += p[i][e] * q[i][e]; x -= q[i][e]; e++; } if (e < q[i].size()) a += p[i][e] * x; x = r[k]; e = 0; while (x > q[j][e] && e < q[j].size()) { b += p[j][e] * q[j][e]; x -= q[j][e]; e++; } if (e < q[j].size()) b += p[j][e] * x; if (a > b) c[k] = 1; if (a < b) c[k] = 2; } e = 0; while (c[e] == 0) e++; for (k = e + 1; k < l; k++) { if (c[k] == 0) c[k] = c[k - 1]; } for (k = e + 1; k < l; k++) if (c[k] != c[k - 1]) d++; return d; } int main() { cin >> n >> s; for (i = 0; i < n; i++) { cin >> k; for (j = 0; j < k; j++) { cin >> a >> b; p[i].push_back(a); q[i].push_back(b); } } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { ss += too(i, j); r = v; } } cout << ss; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = int(1e6) + 50; char S[N], T[N]; int slen, tlen, lef[N], rig[N], cnt[255][255]; inline int getID(char c) { if (c == R ) return 0; if (c == G ) return 1; if (c == B ) return 2; throw; } int main() { scanf( %s , S + 1); slen = strlen(S + 1); scanf( %s , T + 1); tlen = strlen(T + 1); int l = 1, r = 1; while (l <= slen && r <= tlen) { lef[l] = r; r += S[l] == T[r]; ++l; } slen = min(slen, l - 1); l = r = 1; while (l <= slen && r <= tlen) { rig[l] = r; l += S[l] == T[r]; ++r; } tlen = min(tlen, r - 1); for (int i = l + bool(rig[l]); i <= slen; ++i) { rig[i] = tlen; } long long res = 0; for (int i = 1; i <= slen; ++i) { for (int j = max(1, lef[i - 1]); j < lef[i]; ++j) --cnt[T[j]][T[j - 1]]; for (int j = 1 + rig[i - 1]; j <= rig[i]; ++j) ++cnt[T[j]][T[j - 1]]; res += rig[i] - lef[i] + 1; if (S[i - 1] != S[i]) res -= cnt[S[i - 1]][S[i]]; } printf( %I64d n , res); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k; long long ans; int main() { cin >> n >> m >> k; if (k > n + m - 2) { cout << -1 << endl; return 0; } if (n >= m) swap(n, m); if (k <= m - 1) { ans = (long long)(m / (k + 1)) * n; } else { ans = (long long)(n / (k - m + 2)); } if (k <= n - 1) { ans = max(ans, (long long)(n / (k + 1)) * m); } else { ans = max(ans, (long long)(m / (k - n + 2))); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; int n, m, k; int h[MAXN]; void load() { scanf( %d%d%d , &n, &m, &k); for (int i = 0; i < n; i++) scanf( %d , h + i); } bool solve() { long long sum = m; int ind = 0; while (ind < n - 1) { if (h[ind] >= h[ind + 1] - k) { int nxt = max(h[ind + 1] - k, 0); sum += h[ind] - nxt; } else { if (sum + h[ind] < h[ind + 1] - k) return false; sum -= h[ind + 1] - k - h[ind]; } ind++; } return true; } int main() { int t; scanf( %d , &t); while (t--) { load(); if (solve()) printf( YES n ); else printf( NO n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; int n, k; int a[MAXN], f[MAXN]; int bsL(int l, int r, int val) { int pos = -1; while (l <= r) { int mid = (l + r) >> 1; if (a[mid] >= val) { pos = mid; r = mid - 1; } else l = mid + 1; } return pos; } bool check(int x) { f[1] = 1; for (int i = 1, l = 1; i <= n; i++) { while (a[i] - a[l] > x) l++; f[i + 1] = (i >= k && f[i - k + 1] - f[l - 1] > 0); f[i + 1] += f[i]; } return f[n + 1] - f[n] > 0; } int bs(int l, int r) { int pos = 0; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) { pos = mid; r = mid - 1; } else l = mid + 1; } return pos; } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); cout << bs(0, 1e9); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 10; long long h[N], e[2 * N], ne[2 * N], idx; void add(long long a, long long b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } struct node { long long sum, add; }; node tr[N << 2]; long long arr[N]; long long f[N][20]; long long dep[N], fa[N], son[N], sz[N]; long long n, q; void dfs1(long long u, long long fz) { fa[u] = fz; dep[u] = dep[fz] + 1; sz[u] = 1; f[u][0] = fz; for (long long i = 1; i < 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; for (long long i = h[u]; i != -1; i = ne[i]) { long long j = e[i]; if (j == fz) continue; dfs1(j, u); sz[u] += sz[j]; if (sz[j] > sz[son[u]]) { son[u] = j; } } } long long id[N], top[N], uid[N], tot; void dfs2(long long u, long long topx) { top[u] = topx; id[u] = ++tot; uid[tot] = u; if (!son[u]) return; dfs2(son[u], topx); for (long long i = h[u]; i != -1; i = ne[i]) { long long j = e[i]; if (j == fa[u] || j == son[u]) continue; dfs2(j, j); } } long long get_lca(long long a, long long b) { while (top[a] != top[b]) { if (dep[top[a]] > dep[top[b]]) a = fa[top[a]]; else b = fa[top[b]]; } return dep[a] > dep[b] ? b : a; } long long topC(long long x, long long c) { for (long long i = 19; i >= 0; i--) { if ((1 << i) <= c) { x = f[x][i]; c -= (1 << i); } } return x; } long long root; void pushup(long long u) { tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum; } void pushdown(long long l, long long r, long long u) { if (tr[u].add) { long long mid = l + r >> 1; tr[u << 1].sum += (mid - l + 1) * tr[u].add; tr[u << 1 | 1].sum += (r - mid) * tr[u].add; tr[u << 1].add += tr[u].add; tr[u << 1 | 1].add += tr[u].add; tr[u].add = 0; } } long long query(long long l, long long r, long long a, long long b, long long u) { if (l <= a && b <= r) { return tr[u].sum; } pushdown(a, b, u); long long mid = a + b >> 1; long long ans = 0; if (l <= mid) ans += query(l, r, a, mid, u << 1); if (r > mid) ans += query(l, r, mid + 1, b, u << 1 | 1); return ans; } void ud(long long l, long long r, long long a, long long b, long long d, long long u) { if (l <= a && b <= r) { tr[u].add += d; tr[u].sum += (b - a + 1) * d; return; } pushdown(a, b, u); long long mid = a + b >> 1; if (l <= mid) ud(l, r, a, mid, d, u << 1); if (r > mid) ud(l, r, mid + 1, b, d, u << 1 | 1); pushup(u); } void update(long long u, long long x) { ud(id[u], id[u] + sz[u] - 1, 1, n, x, 1); } long long qry(long long u) { return query(id[u], id[u] + sz[u] - 1, 1, n, 1); } void build(long long l, long long r, long long u) { tr[u].add = 0; if (l == r) { tr[u].sum = arr[uid[l]]; return; } long long mid = l + r >> 1; build(l, mid, u << 1); build(mid + 1, r, u << 1 | 1); pushup(u); } int main() { ios::sync_with_stdio(false); std::cin.tie(0); memset(h, -1, sizeof h); cin >> n >> q; for (long long i = 1; i <= n; ++i) cin >> arr[i]; for (long long i = 1; i < n; ++i) { long long a, b; cin >> a >> b; add(a, b); add(b, a); } dfs1(1, 1); dfs2(1, 1); build(1, n, 1); root = 1; while (q--) { long long t; cin >> t; long long v, u, x; if (t == 1) { cin >> v; root = v; } else if (t == 2) { cin >> u >> v >> x; long long t1 = get_lca(u, root); long long t2 = get_lca(v, root); long long t3 = get_lca(u, v); long long ans = t1; if (dep[ans] < dep[t2]) ans = t2; if (dep[ans] < dep[t3]) ans = t3; long long lca = get_lca(root, ans); if (ans == root) { update(1, x); } else if (lca != ans) { update(ans, x); } else { update(1, x); long long s = topC(root, dep[root] - dep[ans] - 1); update(s, -x); } } else { cin >> v; long long ans = v; long long lca = get_lca(root, ans); if (ans == root) { cout << qry(1) << endl; } else if (lca != ans) { cout << qry(ans) << endl; } else { long long s = topC(root, dep[root] - dep[ans] - 1); cout << (qry(1) - qry(s)) << endl; } } } } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double EPS = 1e-9; const int INF = 1 << 29; int const NN = 5e3 + 3; int const MX = 1e9 + 9; int a[505][505]; int mx[505][505]; int main() { int n, m, K, q; cin >> n >> m >> K >> q; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) a[i][j] = MX; } while (q--) { int x, y, t; cin >> x >> y >> t; a[x][y] = t; } int ans = MX; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { mx[i][j] = a[i][j]; for (int k = 2; i + k - 1 <= n && k <= K; ++k) { mx[i][j] = max(mx[i][j], a[i + k - 1][j]); } } } for (int i = 1; i + K - 1 <= n; ++i) { for (int j = 1; j + K - 1 <= m; ++j) { int mxx = -1; for (int jj = j; jj <= j + K - 1; ++jj) { mxx = max(mxx, mx[i][jj]); } ans = min(ans, mxx); } } if (ans < MX) { printf( %d , ans); ; putchar( n ); } else puts( -1 ); return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() { int n, k; cin >> n >> k; if(2 * k + 1 > n) { cout << -1 n ; return; } vector<int> v(n); int p = n; for(int i = 1; i < 1 + 2 * k; i += 2) v[i] = p--; p = 1; for(int i = 0; i < n; i++) { if(v[i]) continue; v[i] = p++; } for(auto &x : v) cout << x << ; cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while(t--) solve(); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; char S[maxn][10]; int ju(char a[], char b[]) { int n = strlen(a), m = strlen(b); int x = 0, y = 0; for (int i = 0; i < n; i++) x = x * 10 + a[i] - 0 ; for (int j = 0; j < m; j++) y = y * 10 + (b[j] == ? ? 9 : b[j] - 0 ); return x < y; } int f(int ind) { int m = strlen(S[ind - 1]), n = strlen(S[ind]); if (m < n) { for (int i = 0; i < n; i++) if (S[ind][i] == ? ) { if (i == 0) S[ind][i] = 1 ; else S[ind][i] = 0 ; } } else if (m > n) { return 1; } else { for (int i = 0; i < n; i++) { if (S[ind][i] == ? ) for (int j = 0; j <= 9; j++) { S[ind][i] = 0 + j; if (ju(S[ind - 1], S[ind])) break; } } if (!ju(S[ind - 1], S[ind])) return 1; } return 0; } int main() { int n; while (scanf( %d , &n) > 0) { int si = 1; S[0][0] = 0 ; S[0][1] = 0 ; for (int i = 1; i <= n; i++) { scanf( %s , S[i]); if (si && f(i)) si = 0; } if (si) { printf( YES n ); for (int i = 1; i <= n; i++) printf( %s n , S[i]); } else { printf( NO n ); } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct Friend { string name; int x, y, z; } f[123]; bool Isde(char *a) { if (a[0] <= a[1]) return false; if (a[1] <= a[3]) return false; if (a[3] <= a[4]) return false; if (a[4] <= a[6]) return false; if (a[6] <= a[7]) return false; return true; } bool Isiden(char *a) { if (a[0] != a[1]) return false; if (a[1] != a[3]) return false; if (a[3] != a[4]) return false; if (a[4] != a[6]) return false; if (a[6] != a[7]) return false; return true; } int main() { int n; while (cin >> n) { int mx = 0, my = 0, mz = 0; for (int i = 0; i < n; ++i) { int m; cin >> m >> f[i].name; f[i].x = f[i].y = f[i].z; char str[100]; for (int j = 0; j < m; ++j) { cin >> str; if (Isiden(str)) f[i].x++; else if (Isde(str)) f[i].y++; else f[i].z++; } mx = max(f[i].x, mx); my = max(f[i].y, my); mz = max(f[i].z, mz); } bool flag = false; cout << If you want to call a taxi, you should call: ; for (int i = 0; i < n; ++i) { if (f[i].x == mx) { if (flag) cout << , ; cout << f[i].name; flag = true; } } cout << . << endl; flag = false; cout << If you want to order a pizza, you should call: ; for (int i = 0; i < n; ++i) { if (f[i].y == my) { if (flag) cout << , ; cout << f[i].name; flag = true; } } cout << . << endl; flag = false; cout << If you want to go to a cafe with a wonderful girl, you should call: ; for (int i = 0; i < n; ++i) { if (f[i].z == mz) { if (flag) cout << , ; cout << f[i].name; flag = true; } } cout << . << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int L = 0, R = 10000; int n, vote, tot; vector<int> p[110000]; pair<int, int> x[110000]; long long sum[110000]; long long he[4 * 110000]; int num[4 * 110000]; long long ask(int root, int l, int r, int k) { if (k <= 0) return 0; else if (num[root] == k) return he[root]; else if (l == r) return (long long)k * l; else { int mid = (l + r) >> 1; if (num[2 * root] < k) { return ask(2 * root, l, mid, num[2 * root]) + ask(2 * root + 1, mid + 1, r, k - num[2 * root]); } else { return ask(2 * root, l, mid, k); } } } void add(int root, int l, int r, int k) { if (l > r || k < l || k > r) return; else if (l == r) { num[root]++; he[root] += k; } else { num[root]++; he[root] += k; int mid = (l + r) >> 1; add(2 * root, l, mid, k); add(2 * root + 1, mid + 1, r, k); } } int main() { cin >> n; int mx = 0; for (int i = 0, a, b; i < n; i++) { scanf( %d%d , &a, &b); if (a != 0) p[a].push_back(b); else vote++; } for (int i = 1; i < 110000; i++) { sort(p[i].begin(), p[i].end()); reverse(p[i].begin(), p[i].end()); mx = max(mx, (int)p[i].size()); } for (int i = 1; i < 110000; i++) { int m = (int)p[i].size(); for (int j = 0; j < m; j++) { x[tot++] = pair<int, int>(j + 1, p[i][j]); } } sort(x, x + tot); for (int i = 0; i < tot; i++) { if (i == 0) sum[i] = x[i].second; else sum[i] = sum[i - 1] + x[i].second; } long long ans = 1e10; int pre = 0; for (int i = 0; i <= mx + 1; i++) { int indx = upper_bound(x, x + tot, pair<int, int>(i, 1e9)) - x; long long cans = sum[tot - 1] - sum[indx] + x[indx].second; if (indx == tot) cans = 0; while (pre < indx) { add(1, L, R, x[pre].second); pre++; } indx = tot - indx + vote; if (i >= indx) { indx = i - indx + 1; cans += ask(1, L, R, indx); } ans = min(ans, cans); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; long long inv(long long a) { long long u = 0, v = 1, b = mod; while (a) { long long t = b / a; b -= t * a, u -= t * v; swap(a, b); swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } struct mint { int x; mint() : x(0) {} mint(int x) : x(x) {} mint(long long x) : x() { if (x >= mod || x < -mod) x %= mod; if (x < 0) x += mod; this->x = x; } mint &operator-=(const mint &rhs) { x -= rhs.x; if (x < 0) x += mod; return *this; } mint &operator+=(const mint &rhs) { x += rhs.x; if (x >= mod) x -= mod; return *this; } mint &operator*=(const mint &rhs) { x = (long long)x * rhs.x % mod; return *this; } mint &operator/=(const mint &rhs) { x = (long long)x * inv(rhs.x) % mod; return *this; } bool operator<(const mint &rhs) { return x < rhs.x; } bool operator==(const mint &rhs) { return x == rhs.x; } bool operator!=(const mint &rhs) { return x != rhs.x; } friend mint operator+(const mint &lhs, const mint &rhs) { mint ret(lhs); return ret += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { mint ret(lhs); return ret -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { mint ret(lhs); return ret *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { mint ret(lhs); return ret /= rhs; } friend ostream &operator<<(ostream &os, const mint &rhs) { os << rhs.x; return os; } friend istream &operator>>(istream &is, mint &rhs) { long long a; is >> a; rhs = a; return is; } }; mint fpow(mint a, long long b) { mint r = 1; for (; b; b >>= 1) { if (b & 1) r = r * a; a = a * a; } return r; } const int N = 1e6 + 5; mint pw[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; pw[0] = 1; for (int i = 1; i <= max(n, m); ++i) pw[i] = pw[i - 1] * 2; int f0 = 1, f1 = 1, cntx = n, cnty = m, fx = 0, fy = 0; vector<int> cnt(2); vector<vector<int>> cntr(n, vector<int>(2, 0)), cntc(m, vector<int>(2, 0)); map<pair<int, int>, int> mp; auto Erase = [&](int x, int y) { if (!mp.count({x, y})) return; int p = mp[{x, y}]; mp.erase({x, y}); if (--cntr[x][p ^ y & 1] == 0) { if (cntr[x][p ^ y & 1 ^ 1]) fx--; else cntx += 1; } if (--cntc[y][p ^ x & 1] == 0) { if (cntc[y][p ^ x & 1 ^ 1]) fy--; else cnty += 1; } --cnt[x & 1 ^ y & 1 ^ p]; }; auto Insert = [&](int x, int y, int p) { mp[{x, y}] = p; if (cntr[x][p ^ y & 1]++ == 0) { if (cntr[x][p ^ y & 1 ^ 1]) fx++; else cntx -= 1; } if (cntc[y][p ^ x & 1]++ == 0) { if (cntc[y][p ^ x & 1 ^ 1]) fy++; else cnty -= 1; } ++cnt[x & 1 ^ y & 1 ^ p]; }; while (k--) { int x, y, op; cin >> x >> y >> op; --x, --y; Erase(x, y); if (op != -1) Insert(x, y, op); mint ans = -(cnt[0] == 0) - (cnt[1] == 0); if (fx == 0) ans += pw[cntx]; if (fy == 0) ans += pw[cnty]; cout << ans << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { long long int i, a, p, b, p1, p2; cin >> a; b = a; p = 0; p1 = p2 = 1; for (i = 2; i <= sqrt(b); i++) { if (b % i == 0) { b = b / i; p++; if (p == 1) p1 = i; if (p == 2) p2 = i; i--; } } if (p == 0) cout << 1 << endl << 0 << endl; if (p == 1) cout << 2 << endl; if (p >= 2) cout << 1 << endl << p1 * p2 << endl; } |
#include <bits/stdc++.h> using namespace std; template <class P, class Q> ostream& operator<<(ostream& stream, pair<P, Q> v) { stream << ( << v.first << , << v.second << ) ; return stream; } template <class T> ostream& operator<<(ostream& stream, const vector<T> v) { stream << [ ; for (int i = 0; i < (int)v.size(); i++) stream << v[i] << ; stream << ] ; return stream; } template <class T> T smin(T& a, T b) { if (a > b) a = b; return a; } template <class T> T smax(T& a, T b) { if (a < b) a = b; return a; } const long long M = 10004205361450474; int ask(vector<long long> s) { int sz = s.size(); cout << sz << ; for (int i = 0; i < sz; ++i) cout << s[i] << n [i == sz - 1]; fflush(stdout); int v = 0; cin >> v; if (v == -1) { exit(0); } return v; } const int N = 1e4 + 5; long long dp[N][6]; long long f(int A, int B) { if (!B) return 0; if (dp[A][B] != -1) return dp[A][B]; long long& ans = dp[A][B]; ans = 0; const int W = A; for (int i = 0; i < W; ++i) { ans += f(A, B - 1) + 1; A = min(10000ll, A + f(A, B - 1) + 1); } ans += f(A, B - 1); return ans; } long long g(long long a, int b) { return f(min(10000ll, a), b); } int main(void) { memset(dp, -1, sizeof(dp)); const long long c1 = M - f(10000, 4); const long long c2 = 2046; const long long c3 = 6; long long l, r; int used = 5; --used; if (ask(vector<long long>{c1}) > 0) { l = c1 + 1; r = M; goto shit; } --used; if (ask(vector<long long>{c2}) > 0) { l = c2 + 1; r = c1 - 1; goto shit; } --used; if (ask(vector<long long>{c3}) > 0) { l = c3 + 1; r = c2 - 1; } else { l = 1; r = c3 - 1; } shit: for (int cs = used - 1; cs >= 0; --cs) { vector<long long> s; for (long long i = l; i + g(i, cs) <= r && s.size() < min(10000ll, l); i += g(i, cs) + 1) s.push_back(i + g(i, cs)); int v = ask(s); if (!v) { smin(r, s[0] - 1); } else if (v == s.size()) { smax(l, s.back() + 1); } else { smin(r, s[v] - 1); smax(l, s[v - 1] + 1); } } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U& x) { value = normalize(x); } template <typename U> static Type normalize(const U& x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type& operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); } template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); } Modular& operator++() { return *this += 1; } Modular& operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(value * rhs.value); return *this; } Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } friend const Type& abs(const Modular& x) { return x.value; } template <typename U> friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); template <typename V, typename U> friend V& operator>>(V& stream, Modular<U>& number); private: Type value; }; template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> power(const Modular<T>& a, const U& b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> bool IsZero(const Modular<T>& number) { return number() == 0; } template <typename T> string to_string(const Modular<T>& number) { return to_string(number()); } template <typename U, typename T> U& operator<<(U& stream, const Modular<T>& number) { return stream << number(); } template <typename U, typename T> U& operator>>(U& stream, Modular<T>& number) { typename common_type<typename Modular<T>::Type, long long>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } constexpr int md = 998244353; using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; vector<Mint> fact(1, 1); vector<Mint> inv_fact(1, 1); Mint C(int n, int k) { if (k < 0 || k > n) { return 0; } while ((int)fact.size() < n + 1) { fact.push_back(fact.back() * (int)fact.size()); inv_fact.push_back(1 / fact.back()); } return fact[n] * inv_fact[k] * inv_fact[n - k]; } template <typename T> class NTT { public: using Type = typename decay<decltype(T::value)>::type; static Type md; static Modular<T> root; static int base; static int max_base; static vector<Modular<T>> roots; static vector<int> rev; static void clear() { root = 0; base = 0; max_base = 0; roots.clear(); rev.clear(); } static void init() { md = T::value; assert(md >= 3 && md % 2 == 1); auto tmp = md - 1; max_base = 0; while (tmp % 2 == 0) { tmp /= 2; max_base++; } root = 2; while (power(root, (md - 1) >> 1) == 1) { root++; } assert(power(root, md - 1) == 1); root = power(root, (md - 1) >> max_base); base = 1; rev = {0, 1}; roots = {0, 1}; } static void ensure_base(int nbase) { if (md != T::value) { clear(); } if (roots.empty()) { init(); } if (nbase <= base) { return; } assert(nbase <= max_base); rev.resize(1 << nbase); for (int i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } roots.resize(1 << nbase); while (base < nbase) { Modular<T> z = power(root, 1 << (max_base - 1 - base)); for (int i = 1 << (base - 1); i < (1 << base); i++) { roots[i << 1] = roots[i]; roots[(i << 1) + 1] = roots[i] * z; } base++; } } static void fft(vector<Modular<T>>& a) { int n = (int)a.size(); assert((n & (n - 1)) == 0); int zeros = __builtin_ctz(n); ensure_base(zeros); int shift = base - zeros; for (int i = 0; i < n; i++) { if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for (int k = 1; k < n; k <<= 1) { for (int i = 0; i < n; i += 2 * k) { for (int j = 0; j < k; j++) { Modular<T> x = a[i + j]; Modular<T> y = a[i + j + k] * roots[j + k]; a[i + j] = x + y; a[i + j + k] = x - y; } } } } static vector<Modular<T>> multiply(vector<Modular<T>> a, vector<Modular<T>> b) { if (a.empty() || b.empty()) { return {}; } int eq = (a == b); int need = (int)a.size() + (int)b.size() - 1; int nbase = 0; while ((1 << nbase) < need) nbase++; ensure_base(nbase); int sz = 1 << nbase; a.resize(sz); b.resize(sz); fft(a); if (eq) b = a; else fft(b); Modular<T> inv_sz = 1 / static_cast<Modular<T>>(sz); for (int i = 0; i < sz; i++) { a[i] *= b[i] * inv_sz; } reverse(a.begin() + 1, a.end()); fft(a); a.resize(need); return a; } }; template <typename T> typename NTT<T>::Type NTT<T>::md; template <typename T> Modular<T> NTT<T>::root; template <typename T> int NTT<T>::base; template <typename T> int NTT<T>::max_base; template <typename T> vector<Modular<T>> NTT<T>::roots; template <typename T> vector<int> NTT<T>::rev; template <typename T> vector<Modular<T>> inverse(const vector<Modular<T>>& a) { assert(!a.empty()); int n = (int)a.size(); vector<Modular<T>> b = {1 / a[0]}; while ((int)b.size() < n) { vector<Modular<T>> x(a.begin(), a.begin() + min(a.size(), b.size() << 1)); x.resize(b.size() << 1); b.resize(b.size() << 1); vector<Modular<T>> c = b; NTT<T>::fft(c); NTT<T>::fft(x); Modular<T> inv = 1 / static_cast<Modular<T>>((int)x.size()); for (int i = 0; i < (int)x.size(); i++) { x[i] *= c[i] * inv; } reverse(x.begin() + 1, x.end()); NTT<T>::fft(x); rotate(x.begin(), x.begin() + (x.size() >> 1), x.end()); fill(x.begin() + (x.size() >> 1), x.end(), 0); NTT<T>::fft(x); for (int i = 0; i < (int)x.size(); i++) { x[i] *= c[i] * inv; } reverse(x.begin() + 1, x.end()); NTT<T>::fft(x); for (int i = 0; i < ((int)x.size() >> 1); i++) { b[i + ((int)x.size() >> 1)] = -x[i]; } } b.resize(n); return b; } template <typename T> vector<Modular<T>> inverse_old(vector<Modular<T>> a) { assert(!a.empty()); int n = (int)a.size(); if (n == 1) { return {1 / a[0]}; } int m = (n + 1) >> 1; vector<Modular<T>> b = inverse(vector<Modular<T>>(a.begin(), a.begin() + m)); int need = n << 1; int nbase = 0; while ((1 << nbase) < need) { ++nbase; } NTT<T>::ensure_base(nbase); int size = 1 << nbase; a.resize(size); b.resize(size); NTT<T>::fft(a); NTT<T>::fft(b); Modular<T> inv = 1 / static_cast<Modular<T>>(size); for (int i = 0; i < size; ++i) { a[i] = (2 - a[i] * b[i]) * b[i] * inv; } reverse(a.begin() + 1, a.end()); NTT<T>::fft(a); a.resize(n); return a; } template <typename T> vector<Modular<T>> operator*(const vector<Modular<T>>& a, const vector<Modular<T>>& b) { if (a.empty() || b.empty()) { return {}; } if (min(a.size(), b.size()) < 150) { vector<Modular<T>> c(a.size() + b.size() - 1, 0); for (int i = 0; i < (int)a.size(); i++) { for (int j = 0; j < (int)b.size(); j++) { c[i + j] += a[i] * b[j]; } } return c; } return NTT<T>::multiply(a, b); } template <typename T> vector<Modular<T>>& operator*=(vector<Modular<T>>& a, const vector<Modular<T>>& b) { return a = a * b; } class graph { public: int n; vector<vector<int>> adj; vector<int> degree; vector<pair<int, int>> edge; vector<int> depth; vector<bool> covered; graph(int N) : n(N), adj(vector<vector<int>>(n)), degree(vector<int>(n)), depth(vector<int>(n)), covered(vector<bool>(n, false)) {} void add(int X, int Y) { degree[X]++, degree[Y]++; edge.emplace_back(X, Y); adj[X].push_back(Y); adj[Y].push_back(X); } void dfs(int N) { if (covered[N] == true) return; covered[N] = true; for (int i : adj[N]) { if (covered[i] == false) { depth[i] = depth[N] + 1; dfs(i); } } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; int n = s.size(), m = t.size(); while (t.size() < s.size()) t += z ; assert(t.size() == s.size()); vector<graph> gr(n - m + 1, graph(6)); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { vector<Mint> a(n), b(n); for (int k = 0; k < n; k++) { if (int(s[k] - a ) == i) a[k] = 1; if (int(t[k] - a ) == j) b[k] = 1; } reverse(a.begin(), a.end()); for (int k = 0; k < n; k++) a.push_back(0), b.push_back(b[k]); a *= b; for (int k = 2 * n - 1, cnt = 0; cnt < n - m + 1; k--, cnt++) { if (a[k]()) { gr[cnt].add(i, j); } } } } for (int i = 0; i < n - m + 1; i++) { int ans = 0; for (int j = 0; j < 6; j++) { if (gr[i].covered[j] == false) { gr[i].dfs(j); ans += 1; } } cout << 6 - ans << ; } cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long f[60][110][2]; long long bin[60], num[60], s, tt; inline long long rd() { long long x = 0; char ch = getchar(); for (; ch < 0 || ch > 9 ; ch = getchar()) ; for (; ch >= 0 && ch <= 9 ; ch = getchar()) x = x * 10 + ch - 0 ; return x; } int main() { s = rd(); bin[0] = 1; for (int i = 1; i <= 51; i++) bin[i] = bin[i - 1] * 2; long long ans = 0; for (int h = 1; bin[h] - 1 <= s; h++) { long long hh = s % (bin[h] - 1); for (int j = h - 1; j; j--) if (bin[j] - 1 <= hh) hh -= bin[j] - 1; ans += (!hh); } for (int h0 = 1; h0 <= 50; h0++) for (int h1 = 1; h1 <= 50; h1++) if (bin[h0 + 1] + bin[h1 + 1] - 3 <= s - bin[h1] + 1) { long long hh = (s - bin[h1] + 1) % (bin[h0 + 1] + bin[h1 + 1] - 3); for (int n = 0; n <= h0 + h1 - 2 && n <= hh; n++) { tt = 0; long long now = hh + n; if (now & 1) continue; memset(num, 0, sizeof(num)); for (now >>= 1; now; now >>= 1) num[++tt] = now & 1; memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int i = 1; i <= tt; i++) for (int j = 0; j <= n; j++) for (int k = 0; k < 2; k++) if (f[i - 1][j][k]) { for (int l1 = 0; l1 < 2; l1++) for (int l2 = 0; l2 < 2; l2++) { if ((i >= h0 && l1) || (i >= h1 && l2)) continue; int hh = l1 + l2 + k, now = j + l1 + l2; if ((hh & 1) == num[i]) f[i][now][hh >> 1] += f[i - 1][j][k]; } } ans += f[tt][n][0]; } } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int shortestPaths[100][100]; int graph[100][100]; void bfs(int start, int n) { queue<int> q; q.push(start); int visited[n]; for (int i = 0; i < n; i++) { visited[i] = 0; } visited[start] = 1; int dist = 1; while (true) { int sz = q.size(); if (sz == 0) { break; } for (int i = 0; i < sz; i++) { int cur = q.front(); q.pop(); for (int j = 0; j < 100; j++) { if (graph[cur][j] == 1 && visited[j] == 0) { visited[j] = 1; shortestPaths[start][j] = dist; q.push(j); } } } dist++; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { shortestPaths[i][j] = 0; graph[i][j] = 0; } } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { graph[i][j] = (int)(s[j] - 0 ); } } int m; cin >> m; vector<int> vertices; for (int i = 0; i < m; i++) { int a; cin >> a; a--; vertices.push_back(a); } for (int i = 0; i < n; i++) { bfs(i, n); } vector<int> result; result.push_back(vertices[0]); int last = vertices[0]; int ind = 0; for (int i = 1; i < m; i++) { if (i != 1) { if (shortestPaths[last][vertices[i]] < i - ind) { result.push_back(vertices[i - 1]); ind = i - 1; last = vertices[i - 1]; } } } result.push_back(vertices[vertices.size() - 1]); cout << result.size() << endl; for (int i = 0; i < result.size(); i++) { cout << result[i] + 1 << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 111111; long long tree[maxn * 4]; void Insert(int o, int l, int r, int k, int v) { if (l == r) { tree[o] = v; return; } int mid = (l + r) >> 1; if (k <= mid) Insert(o * 2, l, mid, k, v); else Insert(o * 2 + 1, mid + 1, r, k, v); tree[o] = tree[o * 2] + tree[o * 2 + 1]; } long long Query(int o, int l, int r, int L, int R) { if (L <= l && r <= R) return tree[o]; int mid = (l + r) >> 1; long double ans = 0; if (L <= mid) ans += Query(o * 2, l, mid, L, R); if (R > mid) ans += Query(o * 2 + 1, mid + 1, r, L, R); return ans; } int n, x, a[maxn], f[maxn]; int main() { cin >> n; long double ans = 0; for (int i = 1; i <= n; i++) cin >> a[i], f[a[i]] = i; for (int i = 1; i <= n; i++) { ans += Query(1, 1, n, a[i], n); Insert(1, 1, n, a[i], 1); } ans *= ((long long)n * (n + 1)); memset(tree, 0, sizeof(tree)); long long last = 0; for (int i = 1; i <= n; i++) { ans -= (last + Query(1, 1, n, a[i], n)); last = last + Query(1, 1, n, a[i], n); Insert(1, 1, n, a[i], f[a[i]] * 2); } for (int i = 1; i <= n; i++) ans += ((long long)(n - i + 1) * i * (i - 1) / 2); ans /= ((long long)n * (n + 1)); cout << setprecision(15) << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int a[200001][22]; int p[200001]; int d[200001]; int lca(int u, int v) { if (d[u] < d[v]) swap(u, v); int k = d[u] - d[v]; while (k > 0) { int t = log2(k); k -= (1 << t); u = a[u][t]; } if (u == v) return v; for (int i = 20; i >= 0; i--) { if (a[u][i] != a[v][i]) u = a[u][i], v = a[v][i]; } return p[v]; } int orobitiasvla(int x, int v) { while (x > 0) { int t = log2(x); v = a[v][t]; x -= (1 << t); } return v; } int main() { int n; cin >> n; int cnt = 2; for (int i = 1; i <= n; i++) { for (int j = 0; j <= 20; j++) a[i][j] = -1; } d[1] = 0; int d1 = 1, d2 = 1; int mx = 0; n--; while (n--) { int k; cin >> k; a[cnt][0] = k; p[cnt] = k; d[cnt] = d[k] + 1; for (int i = 1; i <= 20; i++) if (d[cnt] >= (1 << i)) a[cnt][i] = orobitiasvla((1 << i) - 1, k); int y = lca(cnt, d1); int cost = d[cnt] - d[y] + d[d1] - d[y]; y = lca(cnt, d2); int cost1 = d[cnt] - d[y] + d[d2] - d[y]; if (cost1 > mx || cost > mx) { if (cost > cost1) d2 = cnt; else d1 = cnt; mx = max(cost, cost1); } cout << mx << ; cnt++; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 7; int a, b, n, ans; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> a >> b, n = a + b; for (int l = 1, r, s; l <= n; l = r + 1) { s = n / l, r = n / s; int al = (a + s) / (s + 1), ar = a / s, bl = (b + s) / (s + 1), br = b / s; if (al <= ar && bl <= br) ans += min(ar + br, r) - max(al + bl, l) + 1; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, a, b, c, d, s, len; vector<long long> z, t, v[2]; map<long long, int> ma; int main() { ios_base::sync_with_stdio(false); cin >> n >> a >> b >> c >> d >> s >> len; t.assign(n + 1, -1); v[0].assign(n + 1, s); v[1].assign(n + 1, s); for (int i = 0; i < n; ++i) { int q; cin >> t[i + 1] >> q; v[0][i + 1] = v[0][i] + (q ? a : -b); v[1][i + 1] = v[1][i] + (q ? c : -d); } int lim = 0; ++ma[v[1][0]]; for (int i = 0; i < n + 1; ++i) { if (v[0][i] < 0) break; if (!(--ma[v[1][i]])) ma.erase(v[1][i]); int be = t[i] + 1; while (lim < n + 1 && t[lim] < be + len) { ++ma[v[1][lim]]; ++lim; } if (i + 1 == lim || v[0][i] - v[1][i] + ma.begin()->first >= 0) { cout << be << endl; return 0; } } cout << -1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int A[100], L[100], n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> A[i], A[i]--; for (int i = 0; i < m; ++i) cin >> L[i]; for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { vector<int> cnt(m + 1, 0); for (int k = i; k < j; ++k) cnt[A[k]]++; bool fl = true; for (int k = 0; k < m; ++k) fl &= (L[k] == cnt[k]); if (fl) { cout << YES n ; return 0; } } } cout << NO n ; } |
#include <bits/stdc++.h> using namespace std; int n, k, b[1000010]; int main() { scanf( %d%d , &n, &k); int i; if (k * 3 > n || k == 1) puts( -1 ); else { for (i = 1; i <= (int)(k); ++i) b[i] = b[i + 2 * k] = i, b[i + k] = i - 1; b[k + 1] = k; for (i = 3 * k + 1; i <= n; ++i) b[i] = 1; for (i = 1; i <= (int)(n); ++i) printf( %d , b[i]); puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[9][5], b[8]; long long dis(int i, int j) { long long x = a[i][0] - a[j][0], y = a[i][1] - a[j][1], z = a[i][2] - a[j][2]; return x * x + y * y + z * z; } bool check() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (i == j) continue; if (j < i) b[j] = dis(i, j); else b[j - 1] = dis(i, j); } sort(b, b + 7); long long t1 = b[0], t2 = 2 * b[0], t3 = 3 * b[0]; if (t1 == 0 || t1 != b[1] || t1 != b[2] || t2 != b[3] || t2 != b[4] || t2 != b[5] || t3 != b[6]) return false; } return true; } bool is_ok(int cur) { if (cur == 8) { return check(); } sort(a[cur], a[cur] + 3); do { if (is_ok(cur + 1)) return true; } while (next_permutation(a[cur], a[cur] + 3)); return false; } int main() { for (int i = 0; i < 8; i++) cin >> a[i][0] >> a[i][1] >> a[i][2]; if (!is_ok(0)) puts( NO ); else { puts( YES ); for (int i = 0; i < 8; i++) for (int j = 0; j < 3; j++) { cout << a[i][j] << ; if (j == 2) cout << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int constexpr nax = 3e5 + 5; using ll = int64_t; const int INF = 1e9; template <typename T> struct fenwick { T bit[nax]; fenwick() { memset(bit, 0, sizeof(bit)); } void update(int x, T val) { for (; x < nax; x += x & -x) bit[x] += val; } T get(int x) { int res = 0; for (; x > 0; x -= x & -x) res += bit[x]; return res; } T get(int l, int r) { return get(r) - get(l - 1); } }; struct segtree { static constexpr int Mx = 4 * nax; ll sg[Mx] = {}, tag[Mx] = {}; inline void pull(int v) { sg[v] = min(sg[v * 2], sg[v * 2 + 1]); } segtree() { fill(sg, sg + Mx, INF); fill(tag, tag + Mx, 0); } inline void push(int v) { if (v * 2 + 1 < Mx && tag[v]) { sg[v * 2] += tag[v]; sg[v * 2 + 1] += tag[v]; tag[v * 2] += tag[v]; tag[v * 2 + 1] += tag[v]; } tag[v] = 0; } inline void update(int v, int x, int y, int l, int r, ll val) { push(v); if (l > r) return; if (l == x && r == y) { sg[v] += val; tag[v] += val; return push(v); } int mid = x + y >> 1; update(v * 2, x, mid, l, min(r, mid), val); update(v * 2 + 1, mid + 1, y, max(l, mid + 1), r, val); pull(v); } int find_first_neg(int v, int x, int y) { push(v); if (x == y) { assert(sg[v] <= 0); return x; } else { int mid = x + y >> 1; if (sg[v * 2] <= 0) return find_first_neg(v * 2, x, mid); else return find_first_neg(v * 2 + 1, mid + 1, y); } } }; segtree sg; fenwick<int> sum; int n, nq; int ans[nax]; int b[nax]; vector<pair<int, int>> q[nax]; int main() { cin.tie(0)->sync_with_stdio(false); cin >> n >> nq; for (int i = 1; i <= n; ++i) { cin >> b[i]; b[i] = i < b[i] ? INF : i - b[i]; } for (int i = 1; i <= nq; ++i) { int x, y; cin >> x >> y; q[x + 1].emplace_back(n - y, i); } for (int i = n; i >= 1; --i) { sg.update(1, 1, n, i, i, (ll)b[i] - INF); while (sg.sg[1] <= 0) { int v = sg.find_first_neg(1, 1, n); sum.update(v, 1); sg.update(1, 1, n, v, v, INF); sg.update(1, 1, n, v, n, -1); } for (const auto &[r, id] : q[i]) ans[id] = sum.get(i, r); } for (int i = 1; i <= nq; ++i) { cout << ans[i] << n ; } } |
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 239; int n, m, k; vector<set<int>> row; vector<set<int>> column; vector<set<int>> row_free; vector<set<int>> column_free; int get_row_left(int i, int to, int from) { auto it = row[i].lower_bound(from); if (it == row[i].begin()) { return to; } it--; if (to <= *(it) && *(it) <= from) { return *(it) + 1; } else { return to; } } int get_row_right(int i, int to, int from) { auto it = row[i].lower_bound(from); if (it == row[i].end()) { return to; } if ((*it) <= to) { return (*it) - 1; } else { return to; } } int get_column_up(int j, int to, int from) { auto it = column[j].lower_bound(from); if (it == column[j].begin()) { return to; } it--; if (to <= *(it) && *(it) <= from) { return *(it) + 1; } else { return to; } } int get_column_down(int j, int to, int from) { auto it = column[j].lower_bound(from); if (it == column[j].end()) { return to; } if ((*it) <= to) { return (*it) - 1; } else { return to; } } vector<pair<int, int>> all_next(int i, int j) { int ff = get_row_left(i, max(0, j - k), j); int ss = get_row_right(i, min(m - 1, j + k), j); int tt = get_column_up(j, max(0, i - k), i); int th = get_column_down(j, min(n - 1, i + k), i); vector<pair<int, int>> ans; if (j > 0) { auto it = row_free[i].lower_bound(ff); while (true) { if (it == row_free[i].end()) { break; } if (*(it) >= j) { break; } ans.push_back({i, *(it)}); it++; } } if (j + 1 < m) { auto it = row_free[i].upper_bound(j); while (true) { if (it == row_free[i].end()) { break; } if (*(it) > ss) { break; } ans.push_back({i, *(it)}); it++; } } if (i > 0) { auto it = column_free[j].lower_bound(tt); while (true) { if (it == column_free[j].end()) { break; } if (*(it) >= i) { break; } ans.push_back({*(it), j}); it++; } } if (i + 1 < n) { auto it = column_free[j].upper_bound(i); while (true) { if (it == column_free[j].end()) { break; } if (*(it) > th) { break; } ans.push_back({*(it), j}); it++; } } return ans; } void bfs(int si, int sj, int ei, int ej) { vector<vector<int>> d(n, vector<int>(m, inf)); d[si][sj] = 0; queue<pair<int, int>> q; q.push({si, sj}); row_free[si].erase(sj); column_free[sj].erase(si); while (!q.empty()) { auto tmp = q.front(); q.pop(); int ci = tmp.first; int cj = tmp.second; auto next = all_next(ci, cj); for (auto [ni, nj] : next) { if (d[ni][nj] > d[ci][cj] + 1) { d[ni][nj] = d[ci][cj] + 1; row_free[ni].erase(nj); column_free[nj].erase(ni); q.push({ni, nj}); } } } int res = d[ei][ej]; if (res == inf) { cout << -1; } else { cout << res; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; row.resize(n); column.resize(m); row_free.resize(n); column_free.resize(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; if (c == # ) { row[i].insert(j); column[j].insert(i); } else { row_free[i].insert(j); column_free[j].insert(i); } } } int si, sj, ei, ej; cin >> si >> sj >> ei >> ej; si--; sj--; ei--; ej--; bfs(si, sj, ei, ej); } |
#include <bits/stdc++.h> using namespace std; string s, t; int dp[5005][5005]; int get(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i >= s.size() or j >= t.size()) return 0; int ret = get(i, j + 1); if (s[i] == t[j]) ret = (ret + 1 + get(i + 1, j + 1)) % 1000000007; return dp[i][j] = ret; ; } int main() { for (int i = 0; i < 5005; ++i) for (int j = 0; j < 5005; ++j) dp[i][j] = -1; cin >> s >> t; long long ret = 0; for (int i = 0; i < s.size(); ++i) ret = (ret + get(i, 0)) % 1000000007; cout << ret << endl; } |
#include <bits/stdc++.h> using namespace std; long long t, n, k; long long f(long long x) { if (x == 0) return k * (k + 1) / 2; long long t = x % k, val = f(x / k), ofs = 0; if (val < k * t + 2) { ofs = k; } else if ((t + 1) * k < val) { ofs = 0; } else { ofs = (t + 1) * k - val + 1; } return k * (k + 1) / 2 - t + ofs; } long long rjesi(long long x) { long long buk = (x - 1) / (k * k + 1); long long pos = (k * k + 1) * buk + f(buk); if (x != pos) { long long lef = buk + (pos < x); return (x - 1 - lef) / k - lef + x; } else { long long t = buk % k, val = f(buk / k); if ((t + 1) * k < val) { return rjesi(buk / k * (k * k + 1) + (t + 1) * k) + 1; } else { return rjesi(buk / k * (k * k + 1) + (t + 1) * k + 1) + 1; } } } int main() { cin >> t; while (t--) { cin >> n >> k; cout << rjesi(n) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 305; long long dp[maxn][maxn][6]; int n, x[maxn], y[maxn]; vector<pair<double, pair<int, int>>> seg; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < 6; k++) dp[i][j][k] = 0; if (i == j) dp[i][j][0] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { int xx = x[j] - x[i], yy = y[j] - y[i]; seg.push_back({atan2(xx, yy), {i, j}}); } } } sort(seg.begin(), seg.end()); int m = seg.size(); for (int cc = 0; cc < m; cc++) { int i = seg[cc].second.first, j = seg[cc].second.second; for (int k = 0; k < n; k++) for (int f = 0; f < 5; f++) dp[k][i][f + 1] += dp[k][j][f]; } long long ret = 0; for (int i = 0; i < n; i++) ret += dp[i][i][5]; cout << ret << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; if ((n & (n - 1)) == 0) { cout << 0 << n ; } else { int k = 0; while ((1 << k + 1) <= n) { k++; } n ^= 1 << k; int ans = 0; for (int i = 0; i < k; ++i) { int j = i + 1; if (j >= k) { j -= k; } if (n & (1 << j)) { ans |= 1 << i; } } cout << ans << n ; } } |
#include <bits/stdc++.h> using namespace std; bool isprime(int n) { if (n == 2) return 1; for (int i = 2; i < n; i++) { if (n % i == 0) return 0; } return 1; } int main() { int n, m; cin >> n >> m; int flag = 1; if (isprime(n) && isprime(m)) { for (int i = n + 1; i < m; i++) { if (isprime(i)) { cout << NO << endl; flag = 0; break; } } if (flag) cout << YES << endl; } else { cout << NO << endl; } } |
#include <bits/stdc++.h> const int N = 200000 + 10; int a[N], n, K; int main() { scanf( %d%d , &n, &K); for (int i = 0; i < n; ++i) scanf( %d , &a[i]); long long pre = 0; int tot = n - 1, id = 0; for (int i = 0; i < n; ++i) { long long d = pre - (long long)a[i] * (tot - id) * id; if (d < K) { printf( %d n , i + 1); --tot; } else { pre = pre + (long long)a[i] * id; ++id; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[105][105], t, m, n, ret, cnt, mn; int main(void) { cin >> t; for (int i = 0; i < t; i++) { ret = 0, cnt = 0, mn = 101; cin >> n >> m; for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { cin >> a[j][k]; if (a[j][k] <= 0) cnt++; if (abs(a[j][k]) < mn) mn = abs(a[j][k]); } } for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { ret += abs(a[j][k]); } } if (cnt % 2 == 1) ret -= 2 * mn; cout << ret << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 510; int n, m; double xa, ya, x2, y2; double v, vx1, vx2, vy1, vy2, t; int check(double k) { double x, y; x = xa + vx1 * min(k, t) + vx2 * max(0.0, k - t); y = ya + vy1 * min(k, t) + vy2 * max(0.0, k - t); double dist = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)); if (v * k >= dist) return 1; return 0; } int main() { int i, j, k; while (~scanf( %lf%lf%lf%lf , &xa, &ya, &x2, &y2)) { scanf( %lf%lf , &v, &t); scanf( %lf%lf , &vx1, &vy1); scanf( %lf%lf , &vx2, &vy2); double l = 0.0, r = 1e9; while ((r - l) > 1e-6) { double mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } printf( %.10lf n , l); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, x, sum = 0, q; vector<int> v; vector<int>::iterator it; scanf( %d , &n); while (n--) { scanf( %d , &x); sum += x; v.push_back(sum); } int sz = v.size(); scanf( %d , &q); while (q--) { scanf( %d , &x); it = lower_bound(v.begin(), v.end(), x); printf( %d n , (it - v.begin()) + 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[3005]; int main() { int n; int cnt = 0; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) cnt++; } } if (cnt % 2 == 0) printf( %.6f , cnt * 2.0); else printf( %.6f , cnt * 2.0 - 1); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long r = n % 3; if (r != 2) cout << 1ll << << 1ll << << n - 2 << endl; else cout << 1ll << << 2ll << << n - 3 << endl; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,unroll-loops ) #pragma GCC target( avx,avx2,fma ) using namespace std; template <typename T> using vc = vector<T>; template <typename T> using uset = unordered_set<T>; template <typename A, typename B> using umap = unordered_map<A, B>; template <typename T, typename Comp = less<T>> using pq = priority_queue<T, vc<T>, Comp>; template <typename T> using minpq = pq<T, greater<T>>; using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using ppi = pair<pii, int>; using pip = pair<int, pii>; using vi = vc<int>; using vii = vc<pii>; constexpr char el = n ; constexpr char sp = ; constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL; char g[8][8]; int dr[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int dc[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; vi a[8][8]; bool good(int r, int c) { return 0 <= r && r < 8 && 0 <= c && c < 8; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed; for (auto& x : g) for (auto& y : x) cin >> y; for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) if (g[i][j] == S ) for (int k = i; k < 8; ++k) a[k][j].push_back(k - i); for (auto& x : a) for (auto& y : x) sort(y.begin(), y.end()); queue<tuple<int, int, int>> q; q.emplace(7, 0, 0); while (!q.empty()) { auto [r, c, d] = q.front(); q.pop(); for (int i = 0; i < 9; ++i) { int r1 = r + dr[i]; int c1 = c + dc[i]; if (r1 == 0 && c1 == 7) { cout << WIN ; exit(0); } if (!good(r1, c1) || binary_search(begin(a[r1][c1]), end(a[r1][c1]), d) || binary_search(begin(a[r1][c1]), end(a[r1][c1]), d + 1)) continue; q.emplace(r1, c1, d + 1); } } cout << LOSE ; } |
#include <bits/stdc++.h> using namespace std; #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define fileio freopen( in.in , r , stdin),freopen( out.out , w , stdout); #define ll long long #define int long long #define FF first #define SS second #define mp make_pair #define pb push_back #define pii pair<ll,ll> #define pll pair<long long,long long> #define sd(x) scanf( %d ,&x) #define slld(x) scanf( %lld ,&x) #define pd(x) prllf( %d n ,x) #define plld(x) prllf( %lld n ,x) #define pss prllf #define MOD 1000000007 #define eps 0.00001 #define endl n #define debug(n1) cout<<n1<<endl ll n,k,s[1005],e[1005],c[1005]; bool y[1005]; const ll INF = 1e17+5; int mm[100005]; vector<int>temp[100005]; struct MinimumCostMaximumFlow { typedef ll Index; typedef ll Flow; typedef ll Cost; static const Flow InfCapacity = INF; struct Edge { Index to; Index rev; Flow capacity; Cost cost; }; vector<vector<Edge> > g; void init(Index n) { g.assign(n, vector<Edge>()); } void addEdge(bool inv,Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost() ) { Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost; if(inv) { temp[i].push_back(mm[i]); } mm[i]++; mm[j]++; g[i].push_back(e); g[j].push_back(f); g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1; } void addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) { addEdge(0,i, j, capacity, cost); addEdge(0,j, i, capacity, cost); } pair<Cost, Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity, bool useSPFA = false) { ll n = g.size(); vector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n); pair<Cost, Flow> total = make_pair(0, 0); vector<Cost> potential(n); while(f > 0) { fill(dist.begin(), dist.end(), INF); if(useSPFA || total.second == 0) { deque<Index> q; q.push_back(s); dist[s] = 0; vector<bool> inqueue(n); while(!q.empty()) { Index i = q.front(); q.pop_front(); inqueue[i] = false; for(Index ei = 0; ei < (Index)g[i].size(); ei ++) { const Edge &e = g[i][ei]; Index j = e.to; Cost d = dist[i] + e.cost; if(e.capacity > 0 && d < dist[j]) { if(!inqueue[j]) { inqueue[j] = true; q.push_back(j); } dist[j] = d; prev[j] = i; prevEdge[j] = ei; } } } } else { vector<bool> vis(n); priority_queue<pair<Cost, Index> > q; q.push(make_pair(-0, s)); dist[s] = 0; while(!q.empty()) { Index i = q.top().second; q.pop(); if(vis[i]) continue; vis[i] = true; for(Index ei = 0; ei < (Index)g[i].size(); ei ++) { const Edge &e = g[i][ei]; if(e.capacity <= 0) continue; Index j = e.to; Cost d = dist[i] + e.cost + potential[i] - potential[j]; if(dist[j] > d) { dist[j] = d; prev[j] = i; prevEdge[j] = ei; q.push(make_pair(-d, j)); } } } } if(dist[t] == INF) break; if(!useSPFA) for(Index i = 0; i < n; i ++) potential[i] += dist[i]; Flow d = f; Cost distt = 0; for(Index v = t; v != s; ) { Index u = prev[v]; const Edge &e = g[u][prevEdge[v]]; d = min(d, e.capacity); distt += e.cost; v = u; } f -= d; total.first += d * distt; total.second += d; for(Index v = t; v != s; v = prev[v]) { Edge &e = g[prev[v]][prevEdge[v]]; e.capacity -= d; g[e.to][e.rev].capacity += d; } } return total; } }; int find_num(string ss) { int ans=0; int tt=1; reverse(ss.begin(),ss.end()); for(auto i:ss) { ans+=(i- 0 )*tt; tt*=2; } return ans; } vector<vector<int> >adj; int sz[1000005]; int vis[1000005]; int a[100005]; vector<string>fi; int d; void print(int yy) { for(int i=0;i<10;i++) { if(((1<<i)&yy)) { fi.push_back(to_string(d-i-1)); } } } void dfs(int ver,int x) { vis[ver]=1; print(a[ver]^x); int cnt=0; for(auto i:adj[ver]) { if(vis[i]==0) { if(cnt) { fi.push_back( R ); print(a[ver]); dfs(i,a[ver]); } else { dfs(i,a[ver]); } cnt++; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if(fopen( INPUT.txt , r )) { freopen( INPUT.txt , r ,stdin); freopen( OUTPUT.txt , w ,stdout); } #endif int n; cin>>d>>n; string se[n]; for(int i=0;i<n;i++) { cin>>se[i]; a[i+1]=find_num(se[i]); } adj.resize(n+1); MinimumCostMaximumFlow kad; int source=0; int sink=2*n+5; kad.init(2*n+10); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i!=j) { if((a[i]&a[j])==a[j]) { kad.addEdge(1,n+j,i,1,-(__builtin_popcount(a[j])+1)); } } } kad.addEdge(0,i,n+i,1,0); kad.addEdge(0,source,i,1,0); kad.addEdge(0,n+i,sink,1,-(__builtin_popcount(a[i])+1)); } kad.addEdge(0,source,sink,n,0); pair<int,int> ans=kad.minimumCostMaximumFlow(source,sink,n,0); for(int i=n+1;i<=2*n;i++) { for(auto j:temp[i]) { if(kad.g[i][j].capacity==0) { adj[i-n].push_back(kad.g[i][j].to); } } } set<pair<int,int> >ss; int cc=0; for(int i=1;i<=n;i++) { ss.insert({a[i],i}); } while(!ss.empty()) { auto j=ss.begin(); if(vis[j->second]) { ss.erase(j); continue; } if(cc) fi.push_back( R ); cc++; dfs(j->second,0); } cout<<fi.size()<< n ; for(auto i:fi) cout<<i<< ; } |
#include <bits/stdc++.h> using namespace std; namespace INIT { char buf[1 << 15], *fs, *ft; inline char getc() { return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft)) ? 0 : *fs++; } inline int read() { int x = 0, f = 1; char ch = getc(); while (!isdigit(ch)) { if (ch == - ) f = -1; ch = getc(); } while (isdigit(ch)) { x = x * 10 + ch - 0 ; ch = getc(); } return x * f; } } // namespace INIT using namespace INIT; int n, p[1000010], vis[1000010], ans[1000010]; vector<int> a[1000010], b[1000010]; int main() { n = read(); for (int i = 1; i <= n; ++i) p[i] = read(); for (int i = 1; i <= n; ++i) { int num = 0; for (int j = i; !vis[j]; j = p[j]) vis[j] = 1, ++num, a[i].push_back(j); b[num].push_back(i); } for (int i = 1; i <= n; ++i) if (i % 2 == 0 && (b[i].size() & 1)) { puts( -1 ); return 0; } memset(vis, 0, sizeof(vis)); for (int i = 0; i <= (int)b[1].size() - 1; ++i) ans[b[1][i]] = b[1][i]; for (int i = 2; i <= n; ++i) for (int j = 0; j <= (int)b[i].size() - 1; ++j) { if (i & 1) { int x = a[b[i][j]][0], y = a[b[i][j]][i / 2 + 1]; while (!vis[x]) { vis[x] = vis[y] = 1; ans[x] = y; x = p[x]; ans[y] = x; y = p[y]; } } else { int x = a[b[i][j]][0], y = a[b[i][j + 1]][0]; while (!vis[x] && !vis[y]) { vis[x] = vis[y] = 1; ans[x] = y; x = p[x]; ans[y] = x; y = p[y]; } j++; } } for (int i = 1; i <= n; ++i) printf( %d , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; const int MAXM = 20010; const int MAXNODE = 16 * MAXN; const int LOGMAXN = 50; const int INF = 0x3f3f3f3f; const double eps = 1e-9; int N, M; int b[MAXM], z[MAXM], x[3 * (MAXN + MAXM)]; struct Tree { int x, h; double l, r; } a[MAXN]; struct SegmentTree { double mul[MAXNODE]; void build(int o, int L, int R) { mul[o] = 1; if (L >= R) return; int mid = L + (R - L) / 2; build(o << 1, L, mid); build(o << 1 | 1, mid + 1, R); } void pushdown(int o) { mul[o << 1] *= mul[o]; mul[o << 1 | 1] *= mul[o]; mul[o] = 1; } void update(int o, int L, int R, int ql, int qr, double p) { if (ql <= L && qr >= R) { mul[o] *= p; return; } pushdown(o); int mid = L + (R - L) / 2; if (ql <= mid) update(o << 1, L, mid, ql, qr, p); if (qr > mid) update(o << 1 | 1, mid + 1, R, ql, qr, p); } double query(int o, int L, int R, int pos) { if (L >= R) return mul[o]; pushdown(o); int mid = L + (R - L) / 2; double ans; if (pos <= mid) ans = query(o << 1, L, mid, pos); else ans = query(o << 1 | 1, mid + 1, R, pos); return ans; } } tree; int main() { while (scanf( %d%d , &N, &M) != EOF) { int k = 0; for (int i = 0; i < N; i++) { scanf( %d%d%lf%lf , &a[i].x, &a[i].h, &a[i].l, &a[i].r); a[i].l /= 100; a[i].r /= 100; x[k++] = a[i].x; x[k++] = a[i].x - a[i].h; x[k++] = a[i].x + a[i].h; } for (int i = 0; i < M; i++) { scanf( %d%d , &b[i], &z[i]); x[k++] = b[i]; } sort(x, x + k); k = unique(x, x + k) - x; tree.build(1, 1, k); for (int i = 0; i < N; i++) { int qm = lower_bound(x, x + k, a[i].x) - x + 1; int ql = lower_bound(x, x + k, a[i].x - a[i].h) - x + 1, qr = lower_bound(x, x + k, a[i].x + a[i].h) - x + 1; tree.update(1, 1, k, ql, qm - 1, (1 - a[i].l)); tree.update(1, 1, k, qm + 1, qr, (1 - a[i].r)); } double ans = 0; for (int i = 0; i < M; i++) { int pos = lower_bound(x, x + k, b[i]) - x + 1; ans += z[i] * tree.query(1, 1, k, pos); } printf( %.10lf n , ans); } return 0; } |
#include <bits/stdc++.h> const int N = 502; void sw(float* x) { float tmp = *x; *x = *(x + 2); *(x + 2) = *(x + 1); *(x + 1) = tmp; } int main() { int _t; scanf( %d , &_t); while (_t--) { static float arr[N]; static int anss[N * N]; static int tm[N]; memset(tm, 0, sizeof(tm)); int n; scanf( %d , &n); int f2 = -1; for (int i = 1; i <= n; i++) { int a; scanf( %d , &a); tm[a]++; if (tm[a] == 2) f2 = a; arr[i] = a + 0.0001 * tm[a]; } int nx = 0; for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) if (arr[i] > arr[j]) nx++; if (nx & 1) { if (f2 != -1) { int f21 = -1, f22 = -1; for (int i = 1; i <= n; i++) if (((int)arr[i]) == f2) { if (f21 == -1) f21 = i; else if (f22 == -1) f22 = i; else break; } std::swap(arr[f21], arr[f22]); nx++; } else { printf( -1 n ); continue; } } int ans = 0; while (nx) { for (int i = 1; i <= n - 2; i++) { if (arr[i] < arr[i + 2] && arr[i + 2] < arr[i + 1] || arr[i] > arr[i + 2] && arr[i + 2] > arr[i + 1]) { sw(arr + i); anss[++ans] = i; } if (arr[i] < arr[i + 1] && arr[i] > arr[i + 2] || arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]) { sw(arr + i); nx -= 2; anss[++ans] = i; } } } printf( %d n , ans); for (int i = 1; i <= ans; i++) printf( %d , anss[i]); printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string test; cin >> test; for (int i = 0; i < m; i++) { int l, r; char c1, c2; cin >> l >> r >> c1 >> c2; l -= 1; r -= 1; for (; l <= r; l++) { if (test[l] == c1) { test[l] = c2; } } } cout << test << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; const long long mod = 1e9 + 7; char s[maxn]; int num[55]; long long dp[maxn]; long long tmp[maxn]; long long quesnum[55][55]; long long ques[55][55]; long long inv[maxn], fac[maxn], invf[maxn]; void init(int hf) { inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = ((mod - mod / i) * inv[mod % i]) % mod; fac[0] = 1; for (int i = 1; i < maxn; i++) fac[i] = (fac[i - 1] * (long long)i) % mod; invf[0] = 1; for (int i = 1; i < maxn; i++) invf[i] = (invf[i - 1] * inv[i]) % mod; memset(dp, 0, sizeof(dp)); memset(ques, -1, sizeof(ques)); dp[0] = 1; for (int i = 0; i < 55; i++) { if (!num[i]) continue; for (int j = hf; j >= num[i]; j--) { dp[j] = (dp[j] + dp[j - num[i]]) % mod; } } for (int i = 0; i < 52; i++) { for (int j = i; j < 52; j++) { for (int k = 0; k <= hf; k++) tmp[k] = dp[k]; for (int k = num[i]; k <= hf; k++) tmp[k] = (tmp[k] - tmp[k - num[i]] + mod) % mod; if (i != j) for (int k = num[j]; k <= hf; k++) tmp[k] = (tmp[k] - tmp[k - num[j]] + mod) % mod; quesnum[i][j] = quesnum[j][i] = tmp[hf]; } } } int main() { scanf( %s , s); memset(num, 0, sizeof(num)); int len = strlen(s); int hf = len / 2; for (int i = 0; i < len; i++) { if (s[i] >= a && s[i] <= z ) num[s[i] - a ]++; else num[s[i] - A + 26]++; } init(hf); int q; scanf( %d , &q); int x, y; while (q--) { scanf( %d%d , &x, &y); x--; y--; int idx, idy; if (s[x] >= a && s[x] <= z ) idx = s[x] - a ; else idx = s[x] - A + 26; if (s[y] >= a && s[y] <= z ) idy = s[y] - a ; else idy = s[y] - A + 26; if (ques[idx][idy] != -1) { printf( %I64d n , ques[idx][idy]); continue; } long long ans; if (idx == idy) { if (num[idx] > hf) { printf( 0 n ); ques[idx][idx] = 0; continue; } long long cc = ((fac[hf] * invf[num[idx]]) % mod * invf[hf - num[idx]]) % mod; ans = ((cc * fac[hf - num[idx]]) % mod * fac[hf]) % mod; } else { if (num[idx] + num[idy] > hf) { ques[idx][idy] = ques[idy][idx] = 0; printf( 0 n ); continue; } int numx = num[idx], numy = num[idy]; int num_hfx = hf - numx; long long cc1 = ((fac[hf] * invf[numx]) % mod * invf[hf - numx]) % mod; long long cc2 = ((fac[num_hfx] * invf[numy]) % mod * invf[num_hfx - numy]) % mod; ans = (((cc1 * cc2) % mod * fac[hf]) % mod * fac[hf - numx - numy]) % mod; } for (int i = 0; i < 55; i++) { if (i == idx || i == idy) continue; ans = (ans * invf[num[i]]) % mod; } ans = (ans * quesnum[idx][idy]) % mod; ans = (ans * 2) % mod; ques[idx][idy] = ques[idy][idx] = ans; printf( %I64d n , ans); } } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 10; int a[MAXN]; int n, m, ans, tp, tot; int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %d , a + i); ans = n; sort(a + 1, a + 1 + n); for (int i = n / 2; i >= 1; --i) if (a[ans] >= a[i] * 2) --ans; printf( %d n , ans); } |
#include <bits/stdc++.h> using namespace std; char a[500001 + 10], b[500001 + 10], s[(500001 << 1) + 10]; int n, m; struct BIT { long long c[500001 << 1]; long long query(int x) { if (x <= 0) return 0; long long ans = 0; for (int i = x; i; i -= (i & -i)) ans += c[i]; return ans; } void add(int x, long long p) { if (x <= 0) x = 1; for (int i = x; i <= 1000000; i += (i & -i)) c[i] += p; } } b1, b2; int za[500001 * 3], zb[500001 * 3]; char mod[500001 * 3]; void zfunc(char *s, int n, int *z) { int l = 0, r = 0; z[1] = n; int pt; for (int i = 2; i <= n; ++i) { if (r < i) { for (pt = i; pt <= n && s[pt] == s[pt - i + 1]; ++pt) ; --pt; z[i] = pt - i + 1; if (z[i]) l = i, r = pt; } else { int b = r - i + 1, i_ = i - l + 1; if (z[i_] < b) z[i] = z[i_]; else { for (pt = r + 1; pt <= n && s[pt] == s[pt - i + 1]; ++pt) ; --pt; z[i] = pt - i + 1; l = i, r = pt; } } } } int main() { scanf( %d%d , &n, &m); scanf( %s%s%s , a + 1, b + 1, s + 1); memcpy(mod + 1, s + 1, m - 1); mod[m] = $ ; memcpy(mod + m + 1, a + 1, n); zfunc(mod, n + m, za); for (int i = 1; i <= n; ++i) za[i] = za[i + m]; reverse(s + 1, s + 1 + m); reverse(b + 1, b + 1 + n); memcpy(mod + 1, s + 1, m - 1); mod[m] = $ ; memcpy(mod + m + 1, b + 1, n); zfunc(mod, n + m, zb); for (int i = 1; i <= n; ++i) zb[i] = zb[i + m]; reverse(zb + 1, zb + 1 + n); int cur = 1; long long ans = 0; for (int i = 1; i <= n; ++i) { while (cur <= min(i + m - 2, n)) b1.add(m - 1 - zb[cur], 1), b2.add(m - 1 - zb[cur], zb[cur]), ++cur; ans += 1ll * b1.query(za[i]) * (za[i] - m + 1) + b2.query(za[i]); b1.add(m - 1 - zb[i], -1), b2.add(m - 1 - zb[i], -zb[i]); } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; long long int dec(string s, int n) { long long int r = 0, p = 0, l = s.size() - 1, tmp; while (l >= 0) { if (isalpha(s[l])) tmp = s[l] - 55; else tmp = s[l] - 48; int prod = 1; for (int k = 0; k < p; k++) prod *= n; r += prod * tmp; p++; l--; } return r; } int main() { string s, a, b; int mx = 0; cin >> s; int i = 0; while (s[i] != : ) { a.push_back(s[i]); if (mx < s[i]) mx = s[i]; i++; } i++; while (s[i] != 0 ) { b.push_back(s[i]); if (mx < s[i]) mx = s[i]; i++; } int bg; bg = mx < 65 ? mx - 48 : mx - 55; int beg = max(2, bg + 1); if (dec(a, beg) >= 24 || dec(b, beg) >= 60) { cout << 0 << endl; return 0; } if (dec(a, 100) < 24 && dec(b, 100) < 60) { cout << -1 << endl; return 0; } for (i = beg;; i++) { if (dec(a, i) < 24 && dec(b, i) < 60) { cout << i << ; } else break; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = (int)2e9; const long long INFL = (long long)9e18; const int MAXINT = ((~0) ^ (1 << 31)); const long long MAXLL = ((~0) ^ ((long long)1 << 63)); template <class T> inline T pow2(T a) { return a * a; } template <class T> inline bool mineq(T& a, T b) { return (a > b) ? (a = b, true) : false; } template <class T> inline bool maxeq(T& a, T b) { return (a < b) ? (a = b, true) : false; } const int maxn = 2e5; long long n, k, x, cnt; pair<long long, long long> p[maxn]; int main() { ios_base::sync_with_stdio(0); cin >> n >> k; for (int i = 1; i <= n; i++) p[i] = {1e10, 0}; for (int i = 1; i <= k; i++) { cin >> x; p[x].first = min(p[x].first, (long long)i); p[x].second = max(p[x].second, (long long)i); } if (p[1].first > p[2].second && n != 1) { cnt++; } if (p[n].first > p[n - 1].second && n != 1) { cnt++; } if (p[1].second == 0) cnt++; if (p[n].second == 0 && n != 1) cnt++; for (int i = 2; i <= n - 1; i++) { if (p[i].second == 0) cnt++; if (p[i].first > p[i + 1].second) { cnt++; } if (p[i].first > p[i - 1].second) { cnt++; } } cout << cnt; } |
#include <bits/stdc++.h> using namespace std; long long n, ti[2005], co[2005], dp[2005][2005], i, j; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> ti[i] >> co[i]; ti[i]++; } for (i = 0; i <= n; i++) { for (j = 1; j <= n; j++) dp[i][j] = 1e16; } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0ll, j - ti[i])] + co[i]); } } cout << dp[n][n]; return 0; } |
#include <bits/stdc++.h> using namespace std; struct pen { int color; int diameter; }; int n, m, u, v; pen marker[100000], cap[100000]; struct match { vector<pen> marker; vector<pen> cap; }; match singleMatch[1000 + 1]; match doubleMatch[1000 + 1][1000 + 1]; int main(int argc, char *argv[]) { u = 0, v = 0; cin >> n >> m; for (int i = 0; i < n; i++) { int color, diameter; cin >> color >> diameter; marker[i].color = color; marker[i].diameter = diameter; singleMatch[diameter].marker.push_back(marker[i]); doubleMatch[diameter][color].marker.push_back(marker[i]); } for (int i = 0; i < m; i++) { int color, diameter; cin >> color >> diameter; cap[i].color = color; cap[i].diameter = diameter; singleMatch[diameter].cap.push_back(marker[i]); doubleMatch[diameter][color].cap.push_back(cap[i]); } for (int i = 1; i < 1000 + 1; i++) { vector<pen> tempM = singleMatch[i].marker; vector<pen> tempC = singleMatch[i].cap; int temp1 = (int)tempM.size(); int temp2 = (int)tempC.size(); if (temp1 > 0 && temp2 > 0) u += min(temp1, temp2); } for (int i = 1; i < 1000 + 1; i++) for (int j = 1; j < 1000 + 1; j++) { vector<pen> tempM = doubleMatch[i][j].marker; vector<pen> tempC = doubleMatch[i][j].cap; int temp1 = (int)tempM.size(); int temp2 = (int)tempC.size(); if (temp1 > 0 && temp2 > 0) v += min(temp1, temp2); } cout << u << << v << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 109; const int mod = 1000000007; long long cnt[inf]; long long dp[inf]; long long temp[inf][inf]; long long sol[inf][inf]; long long bs[inf][inf]; int main() { int n, x, a; scanf( %d%d , &n, &x); for (int i = 0; i < n; ++i) { scanf( %d , &a); cnt[a]++; } dp[0] = 1; int all = 0; for (int i = 1; i <= 100; ++i) { for (int j = 1; j <= i; ++j) dp[i] = (dp[i] + dp[i - j] * cnt[j]) % mod; all = (all + dp[i]) % mod; } dp[0] = all; for (int i = 1; i <= 50; ++i) swap(dp[i], dp[101 - i]); for (int i = 1; i <= 100; ++i) sol[i][i] = 1, bs[0][i] = bs[1][i] = cnt[i]; sol[0][0] = bs[0][0] = 1; for (int i = 2; i <= 100; ++i) bs[i][i - 1] = 1; if (x <= 100) { int ad = 0; for (int i = 100; i > 0 && x > 0; --i, --x) ad = (ad + dp[i]) % mod; printf( %d n , ad + 1); return 0; } x -= 100; while (x) { if (x & 1) { for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; ++j) { temp[i][j] = 0; for (int u = 0; u <= 100; ++u) temp[i][j] = (temp[i][j] + sol[i][u] * bs[u][j]) % mod; } for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; ++j) sol[i][j] = temp[i][j]; } x = x >> 1; for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; ++j) { temp[i][j] = 0; for (int u = 0; u <= 100; ++u) temp[i][j] = (temp[i][j] + bs[i][u] * bs[u][j]) % mod; } for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; ++j) bs[i][j] = temp[i][j]; } int ans = 0; for (int i = 0; i <= 100; ++i) ans = (ans + sol[0][i] * dp[i]) % mod; printf( %d n , (ans + 1) % mod); } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 4e5 + 10; const int M = 22; class SegmentTree { public: int ql, qr, qx; int xorv[N]; int sumv[N]; void init() { memset((xorv), (0), sizeof(xorv)); memset((sumv), (0), sizeof(sumv)); } void set(int ql, int qr, int qx) { this->ql = ql; this->qr = qr; this->qx = qx; } void pushdown(int o, int len) { if (xorv[o]) { xorv[o << 1] ^= 1; xorv[o << 1 | 1] ^= 1; sumv[o << 1] = (len - len / 2) - sumv[o << 1]; sumv[o << 1 | 1] = len / 2 - sumv[o << 1 | 1]; xorv[o] = 0; } } void maintain(int o) { sumv[o] = sumv[o << 1] + sumv[o << 1 | 1]; } int query(int l, int r, int o = 1) { if (ql <= l && qr >= r) { return sumv[o]; } int mid = (l + r) >> 1; pushdown(o, r - l + 1); int ans = 0; if (ql <= mid) ans += query(l, mid, o << 1); if (qr > mid) ans += query(mid + 1, r, o << 1 | 1); return ans; } void update(int l, int r, int o = 1) { if (ql <= l && qr >= r) { xorv[o] ^= 1; sumv[o] = r - l + 1 - sumv[o]; return; } int mid = (l + r) >> 1; pushdown(o, r - l + 1); if (ql <= mid) update(l, mid, o << 1); if (qr > mid) update(mid + 1, r, o << 1 | 1); maintain(o); } } st[M]; int main() { for (int i = 0; i < M; i++) st[i].init(); int n; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; for (int j = 0; j < M; j++) { if (x >> j & 1) st[j].set(i, i, 1), st[j].update(1, n); } } int q; cin >> q; while (q--) { int op; cin >> op; if (op == 1) { int l, r; cin >> l >> r; long long ans = 0; for (int i = 0; i < M; i++) { st[i].set(l, r, 0); ans += (1LL << i) * st[i].query(1, n); } cout << ans << n ; } else { int l, r, x; cin >> l >> r >> x; for (int i = 0; i < M; i++) { if (x >> i & 1) st[i].set(l, r, 1), st[i].update(1, n); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct data { int s, m, r, t; int operator<(const data &b) const { return t < b.t; } void get() { scanf( %d , &s); scanf( %d , &m); scanf( %d , &r); t = r ? (m / r + (m % r > 0)) : 2e9; } int calc(int o) { return min(1ll * m, s + 1ll * r * o); } } a[20][100005]; int T[100005 << 2]; long long R[20][100005], sum[20][100005], ans; int n, Q, t, ql, qr; void Build(int x, int l, int r, int d) { T[x] = -2; if (l == r) { a[d][l].get(); R[d][l] = R[d][l - 1] + a[d][l].r; sum[d][l] = sum[d][l - 1] + a[d][l].m; return; } int mid = (l + r) >> 1; Build(x << 1, l, mid, d + 1); Build(x << 1 | 1, mid + 1, r, d + 1); for (int i = l; i <= r; i++) a[d][i] = a[d + 1][i]; sort(a[d] + l, a[d] + r + 1); for (int i = l; i <= r; i++) R[d][i] = R[d][i - 1] + a[d][i].r; for (int i = l; i <= r; i++) sum[d][i] = sum[d][i - 1] + a[d][i].m; } void Find(int x, int l, int r, int d) { if (ql <= l && r <= qr && T[x] != -1) { if (T[x] == -2) { for (int i = l; i <= r; i++) ans += a[d][i].calc(t); T[x] = t; return; } data tmp = (data){0, 0, 0, t - T[x]}; int id = upper_bound(a[d] + l, a[d] + r + 1, tmp) - a[d]; ans += 1ll * (R[d][r] - R[d][id - 1]) * (t - T[x]) + sum[d][id - 1] - sum[d][l - 1]; T[x] = t; return; } if (T[x] >= 0) T[x << 1] = T[x << 1 | 1] = T[x]; int mid = (l + r) >> 1; if (ql <= mid) Find(x << 1, l, mid, d + 1); if (qr > mid) Find(x << 1 | 1, mid + 1, r, d + 1); T[x] = (T[x << 1] == T[x << 1 | 1]) ? T[x << 1] : -1; } int main() { scanf( %d , &n); Build(1, 1, n, 0); scanf( %d , &Q); while (Q--) { scanf( %d , &t); scanf( %d , &ql); scanf( %d , &qr); ans = 0; Find(1, 1, n, 0); printf( %I64d n , ans); } } |
#include <bits/stdc++.h> using namespace std; struct Edge { int v; Edge *link; } edge[400010 * 2], *adj[200010], *tree[200010 * 2]; int totE, con, root, currT, top, top1; int totCut, totBi; int dep[200010], deps[200010], num[200010], dfn[200010], lowlink[200010], cut[200010], bi[200010], color[200010]; int temp[200010], st1[200010], st2[200010], st3[200010], st4[200010], f[200010 * 2]; int used[200010 * 2], odd[200010 * 2]; int fa[200010][20]; int LOG[200010]; void addEdge(int u, int v) { Edge *p = &edge[totE++]; p->v = v; p->link = adj[u]; adj[u] = p; } void addTree(int u, int v) { Edge *p = &edge[totE++]; p->v = v; p->link = tree[u]; tree[u] = p; } void dfs(int u, int pre) { dep[u] = dep[pre] + 1; dfn[u] = lowlink[u] = ++currT; num[u] = con; Edge *p = adj[u]; int cnt = 0; bool flag = false; while (p) { if (!dfn[p->v]) { ++cnt; dfs(p->v, u); lowlink[u] = min(lowlink[u], lowlink[p->v]); if (lowlink[p->v] >= dfn[u]) flag = true; } else if (p->v != pre) lowlink[u] = min(lowlink[u], dfn[p->v]); p = p->link; } if (flag && (u != root || cnt > 1)) cut[u] = ++totCut; } bool dfs2(int u) { Edge *p = adj[u]; while (p) { if (bi[p->v] == totBi) { if (color[p->v] == -1) { color[p->v] = !color[u]; if (!dfs2(p->v)) return false; } else if (color[p->v] == color[u]) return false; } p = p->link; } return true; } void solve() { ++totBi; int cnt = 0; for (int i = 0; i < top1; ++i) { int u = st3[i], v = st4[i]; bi[u] = totBi; bi[v] = totBi; color[u] = color[v] = -1; if (cut[u]) temp[cnt++] = cut[u]; if (cut[v]) temp[cnt++] = cut[v]; } sort(temp, temp + cnt); for (int i = 0; i < cnt; ++i) if (i == 0 || temp[i] != temp[i - 1]) { addTree(temp[i], totCut + totBi); addTree(totCut + totBi, temp[i]); } color[st3[0]] = 0; odd[totBi] = !dfs2(st3[0]); } void dfs1(int u, int pre) { dfn[u] = lowlink[u] = ++currT; Edge *p = adj[u]; while (p) { if (!dfn[p->v]) { st1[top] = u; st2[top++] = p->v; dfs1(p->v, u); lowlink[u] = min(lowlink[u], lowlink[p->v]); if (lowlink[p->v] >= dfn[u]) { top1 = 0; while (true) { --top; st3[top1] = st1[top]; st4[top1++] = st2[top]; if (st1[top] == u && st2[top] == p->v) break; } solve(); } } else { if (dfn[u] > dfn[p->v] && p->v != pre) { st1[top] = u; st2[top++] = p->v; } if (p->v != pre) lowlink[u] = min(lowlink[u], dfn[p->v]); } p = p->link; } } void dfs3(int u, int pre) { deps[u] = deps[pre] + 1; used[u] = true; fa[u][0] = pre; if (u <= totCut) f[u] = 0; else f[u] = odd[u - totCut]; f[u] += f[pre]; Edge *p = tree[u]; while (p) { if (!used[p->v]) dfs3(p->v, u); p = p->link; } } int LCA(int u, int v) { if (deps[u] < deps[v]) swap(u, v); int diff = deps[u] - deps[v]; for (int i = 0; diff; ++i, diff >>= 1) if (diff & 1) u = fa[u][i]; if (u == v) return u; int t = 19; for (int i = t; i >= 0; --i) { if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } int main() { LOG[1] = 0; for (int i = 2; i < 200010; ++i) LOG[i] = (LOG[i] >> 1) + 1; int n, m; scanf( %d%d , &n, &m); for (int i = 0; i < m; ++i) { int u, v; scanf( %d%d , &u, &v); addEdge(u, v); addEdge(v, u); } dep[0] = -1; for (int i = 1; i <= n; ++i) if (!dfn[i]) { root = i; dfs(i, 0); ++con; } currT = 0; memset(dfn, 0, sizeof(dfn)); for (int i = 1; i <= n; ++i) if (!dfn[i]) dfs1(i, 0); for (int i = 1; i <= totCut + totBi; ++i) if (!used[i]) dfs3(i, 0); for (int l = 1; (1 << l) <= totCut + totBi; ++l) for (int i = 1; i <= totCut + totBi; ++i) if (fa[i][l - 1] != 0) fa[i][l] = fa[fa[i][l - 1]][l - 1]; int q; scanf( %d , &q); for (int i = 0; i < q; ++i) { int u, v; scanf( %d%d , &u, &v); if (u == v || num[u] != num[v]) puts( No ); else { int x, y; if (cut[u]) x = cut[u]; else x = totCut + bi[u]; if (cut[v]) y = cut[v]; else y = totCut + bi[v]; int lca = LCA(x, y); int sum = f[x] + f[y] - 2 * f[fa[lca][0]]; if (sum > 0) puts( Yes ); else if ((dep[u] + dep[v]) % 2 == 0) puts( No ); else puts( Yes ); } } return 0; } |
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); } using namespace std; int t; int pri[4001000], pcnt; bool depri[35001000]; inline void init() { depri[1] = true; const int up = 3.5e7; for (int i = 2; i <= up; ++i) { if (!depri[i]) pri[++pcnt] = i; for (int j = 1; j <= pcnt && i * pri[j] <= up; ++j) { depri[i * pri[j]] = true; if (i % pri[j] == 0) break; } } } struct TC { int id; long long n, k; inline bool operator<(const TC &a) const { return k < a.k; } } tc[10100]; bool ans[10100]; long long p[100]; int ptot; inline void Div(long long x) { ptot = 0; for (int i = 1; 1ll * pri[i] * pri[i] <= x; ++i) { if (x % pri[i] == 0) { p[++ptot] = pri[i]; while (x % pri[i] == 0) x /= pri[i]; } } if (x != 1) p[++ptot] = x; } struct node { int cur; long long val; inline bool operator<(const node &a) const { return val > a.val; } }; priority_queue<node> q; long long dis[100100]; bool vis[100100]; inline void Update(long long k) { Div(k); if (ptot <= 2) return; memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[0] = 0; q.push((node){0, 0}); while (!q.empty()) { int cur = (q.top()).cur; q.pop(); if (vis[cur]) continue; vis[cur] = true; for (int i = 2; i <= ptot; ++i) { int to = (cur + p[i]) % p[1]; long long val = (cur + p[i]) / p[1]; if (dis[to] <= dis[cur] + val) continue; dis[to] = dis[cur] + val; q.push((node){to, dis[to]}); } } } inline long long quickmul(long long x, long long k, long long P) { x = (x % P + P) % P; k = (k % P + P) % P; long long res = 0; while (k) { if (k & 1) res = (res + x) % P; x = (x + x) % P; k >>= 1; } return res; } long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } long long g = exgcd(b, a % b, x, y); long long tmp = x; x = y; y = tmp - a / b * y; return g; } inline bool get_ans(long long n) { if (!ptot) return false; if (ptot == 1) return n % p[1] == 0; if (ptot == 2) { long long x = 0, y = 1; exgcd(p[1], p[2], x, y); x = quickmul(x, n, p[2]); if (1.0 * x * p[1] > 2e18) return false; return n >= x * p[1]; } if (1.0 * dis[n % p[1]] * p[1] > 2e18) return false; return dis[n % p[1]] * p[1] + n % p[1] <= n; } signed main() { init(); read(t); for (int i = 1; i <= t; ++i) { read(tc[i].n), read(tc[i].k); tc[i].id = i; } sort(tc + 1, tc + 1 + t); for (int i = 1; i <= t; ++i) { if (tc[i].k != tc[i - 1].k) Update(tc[i].k); ans[tc[i].id] = get_ans(tc[i].n); } for (int i = 1; i <= t; ++i) puts(ans[i] ? YES : NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; std::vector<long long> factorial; void precompute_factorial(int ma) { factorial.push_back(1); for (int i = 1; i <= ma; i++) factorial.push_back((factorial.back() * i) % MOD); } long long power(long long base, int e) { long long result = 1; while (e) { if (e & 1) result = (result * base) % MOD; base = (base * base) % MOD; e >>= 1; } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; precompute_factorial(n + 1); vector<long long> inv_factorial(n + 1, 1); for (int i = 2; i < n + 1; i++) inv_factorial[i] = power(factorial[i], MOD - 2); vector<long long> inv(n + 2); for (int i = 1; i < n + 2; i++) inv[i] = power(i, MOD - 2); if (k == 0) { cout << ((factorial[n] - factorial[n - 1] + MOD) % MOD) << endl; return 0; } vector<long long> dp2(n + 1, 1); vector<long long> xxx(n + 1, 1); long long sum = 0; for (int i = 1; i < n + 1; i++) { sum = (sum + xxx[i - 1]) % MOD; if (i - 1 - k >= 0) { sum = (sum + MOD - xxx[i - 1 - k]) % MOD; } dp2[i] = (sum * factorial[i - 1]) % MOD; xxx[i] = (dp2[i] * inv_factorial[i]) % MOD; } vector<long long> dp(n + 1, 0); long long last = 0; long long total = 0; for (int i = k + 2; i <= n; i++) { long long y = i - k - 2; long long tt = dp2[y]; tt = (tt + MOD) % MOD; tt = (tt * inv_factorial[y]) % MOD; tt = (tt * inv[i - 1]) % MOD; last += tt; last %= MOD; total += last; total %= MOD; dp[i] = (factorial[i - 1] * total) % MOD; } cout << dp.back() << endl; } |
#include <bits/stdc++.h> using namespace std; const int N = 105; const int M = 505; const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const double PI = acos(-1.0); const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; template <class T> inline T Min(T a, T b) { return a < b ? a : b; } template <class T> inline T Max(T a, T b) { return a > b ? a : b; } template <class T> inline T Min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T Max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } int main() { int a, b; while (scanf( %d%d , &a, &b) != EOF) { int i, tot = (a * (a - 1)) / 2; if (tot <= b) { printf( no solution n ); } else { for (i = 0; i < a; i++) { printf( %d %d n , 0, i); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, q; int A[600006]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } map<pair<int, int>, int> M; int L[600006], R[600006], lst[600006], cn; struct que { int l, r, idx; } Q[600006]; vector<int> bc[600006]; int T[600006]; void ad(int x, int c) { while (x <= cn) T[x] += c, x += (x & -x); } int sum(int x) { int ret = 0; while (x > 0) ret += T[x], x -= (x & -x); return ret; } int ans[600006]; void solve() { cin >> n; for (int i = (1), iend = (n); i <= iend; ++i) { int k; scanf( %d , &k); L[i] = cn + 1; int px, py, tx, ty, x, y, sx, sy; scanf( %d%d , &px, &py); sx = px, sy = py; for (int j = (1), jend = (k); j <= jend; ++j) { if (j != k) scanf( %d%d , &x, &y); else x = sx, y = sy; tx = x, ty = y; x -= px, y -= py; int g = gcd(abs(x), abs(y)); x /= g, y /= g; ++cn; lst[cn] = M[make_pair(x, y)]; bc[lst[cn]].push_back(cn); M[make_pair(x, y)] = cn; px = tx, py = ty; } R[i] = cn; } cin >> q; for (int i = (1), iend = (q); i <= iend; ++i) { int l, r; scanf( %d%d , &l, &r); l = L[l], r = R[r]; Q[i] = (que){l, r, i}; } sort(Q + 1, Q + 1 + q, [](que a, que b) { return a.l < b.l; }); int cur = 1; for (int i = (0), iend = (cn); i <= iend; ++i) { while (cur <= q && Q[cur].l == i) { ans[Q[cur].idx] = sum(Q[cur].r) - sum(Q[cur].l - 1); ++cur; } for (int x : bc[i]) ad(x, 1); } for (int i = (1), iend = (q); i <= iend; ++i) printf( %d n , ans[i]); } signed main() { solve(); } |
#include <bits/stdc++.h> using namespace std; const long long remi = 1000000007; const long long remi2 = 998244353; const long long inf = 1e18 + 1e17 + 1e16 + 1e15 + 1e14; long long Pow(long long x, long long y, long long m) { if (y == 0) return 1; long long p = Pow(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, k; long long n, m; cin >> n >> m; vector<long long> v(m); for (i = 0; i < m; i++) { cin >> v[i]; } sort(v.begin(), v.end()); bool ans = true; if (m == 0) { cout << YES ; exit(0); } if (v[0] == 1 or v[m - 1] == n) ans = false; for (i = 2; i < m; i++) { long long s0 = v[i - 2]; long long s1 = v[i - 1]; long long s2 = v[i]; if ((s0 == s2 - 2) and (s1 == s2 - 1)) ans = false; } if (ans) cout << YES ; else cout << NO ; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0); int t; cin >> t; while (t--) { map<int, int>L, R; int bal = 0; string s; cin >> s; for (char &c : s) { if (c == 1 ) { R[bal]++; bal++; } else { L[bal]++; bal--; } } int cur = 0; while (L[cur] > 0 || R[cur] > 0) { if (L[cur] > 0 && R[cur - 1] > 0 || (!R[cur] && !L[cur + 1])) { L[cur]--; cur--; cout << 0; } else { R[cur]--; cur++; cout << 1; } } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int tree[100010 << 2]; bool flag; struct node { int row, col, pos; friend bool operator<(node a, node b) { if (a.row == b.row) return a.col < b.col; return a.row < b.row; } }; node p[100010]; inline void pushUp(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); } void update(int x, int y, int val, int L, int R, int rt) { if (flag) return; if (L == R) { if (tree[rt] == 0) { flag = true; tree[rt] = val; } return; } else if (x <= L && R <= y) { int mid = (L + R) >> 1; if (tree[rt << 1] == 0) update(x, y, val, L, mid, rt << 1); else if (tree[rt << 1 | 1] == 0) update(x, y, val, mid + 1, R, rt << 1 | 1); } else { int mid = (L + R) >> 1; if (x <= mid) update(x, y, val, L, mid, rt << 1); if (y > mid) update(x, y, val, mid + 1, R, rt << 1 | 1); } pushUp(rt); } void out(int L, int R, int rt) { if (L == R) { if (tree[rt]) printf( %d , tree[rt]); } else { int mid = (L + R) >> 1; out(L, mid, rt << 1); out(mid + 1, R, rt << 1 | 1); } } int main() { int i; int n, m, cnt; while (~scanf( %d%d , &n, &m)) { memset(tree, 0, sizeof(tree)); for (i = 0; i < m; i++) { scanf( %d%d , &p[i].row, &p[i].col); p[i].pos = i + 1; } sort(p, p + m); for (cnt = i = 0; i < m; i++) { flag = false; update(n - p[i].col + 1, p[i].row, p[i].pos, 1, n, 1); if (flag) cnt++; } printf( %d n , cnt); out(1, n, 1); putchar( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int n, t, sz[200010], cen; vector<int> g[200010], v; bool mark[200010]; int dfs(int pos, int par) { sz[pos] = (int)mark[pos]; bool ok = true; for (int i = 0; i < g[pos].size(); i++) if (g[pos][i] != par) { sz[pos] += dfs(g[pos][i], pos); if (sz[g[pos][i]] > t) ok = false; } if (ok && sz[pos] >= t) cen = pos; return sz[pos]; } void dfs2(int pos, int par) { if (mark[pos]) v.push_back(pos); for (int i = 0; i < g[pos].size(); i++) if (g[pos][i] != par) dfs2(g[pos][i], pos); } int main() { cin >> n >> t; int x, y; for (int i = 0; i < n - 1; i++) { scanf( %d%d , &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < t + t; i++) { scanf( %d , &x); mark[x] = true; } dfs(1, 0); for (int i = 0; i < g[cen].size(); i++) dfs2(g[cen][i], cen); if (mark[cen]) v.push_back(cen); cout << 1 << endl << cen << endl; for (int i = 0; i < t; i++) printf( %d %d %d n , v[i], v[i + t], cen); return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline int countbit(T n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } template <class T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } const double EPS = 1e-9; const double PI = acos(-1.0); int n, k, h; int ind[100005]; int vel[100005]; int mass[100005]; int ret[100005]; bool can(double mid) { int cnt = 0; double step = k; for (int j = n - 1; j >= 0 && cnt <= k; j--) { double tempo = step * h / ((double)vel[ind[j]]); if (tempo < mid - EPS || fabs(tempo - mid) < EPS) cnt++, step--; } return cnt >= k; } bool cmp(int i, int j) { if (mass[i] != mass[j]) return mass[i] < mass[j]; return vel[i] < vel[j]; } int main(void) { scanf( %d %d %d , &n, &k, &h); for (int i = (0); i < int(n); i++) scanf( %d , &mass[i]); for (int i = (0); i < int(n); i++) scanf( %d , &vel[i]); for (int i = (0); i < int(n); i++) ind[i] = i; sort(ind, ind + n, cmp); double l = 0, r = 10E9 + 100; for (int i = (0); i < int(100); i++) { double mid = (l + r) / 2; if (can(mid)) r = mid; else l = mid; } double mid = r, step = k; int p = k - 1; for (int j = n - 1; j >= 0 && p >= 0; j--) { double tempo = step * h / ((double)vel[ind[j]]); if (tempo < mid - EPS || fabs(tempo - mid) < EPS) { step--; ret[p--] = ind[j] + 1; } } for (int i = (0); i < int(k); i++) { if (i) printf( ); printf( %d , ret[i]); } printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; map<int, int> mym; void kaisa() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; queue<int> q; for (int i = 1; i <= n; i++) { int x; cin >> x; mym[x] = 0; q.push(x); } long long res = 0; vector<int> ans; while (!q.empty()) { int x = q.front(); q.pop(); if (mym[x] != 0) { res += mym[x]; ans.push_back(x); if (ans.size() == m) break; } if (!mym.count(x + 1)) { mym[x + 1] = mym[x] + 1; q.push(x + 1); } if (!mym.count(x - 1)) { mym[x - 1] = mym[x] + 1; q.push(x - 1); } } cout << res << n ; for (int i : ans) { cout << i << ; } cout << n ; } int main() { kaisa(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long q, x, y; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> q; while (q--) { cin >> x >> y; if (x - y > 1) cout << YES << n ; else cout << NO << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, a, b, x; cin >> t; while (t--) { cin >> a >> b; if (a < b) { x = b - a; x % 2 == 0 ? cout << 2 : cout << 1; } else if (a > b) { x = a - b; x % 2 == 0 ? cout << 1 : cout << 2; } else cout << 0; cout << endl; } } |
#include <bits/stdc++.h> using namespace std; int gcd_(int a, int b) { if (b == 0) return a; else return gcd_(b, a % b); } int main() { int n, i, x; vector<int> v; cin >> n; for (i = 0; i < n; i++) { cin >> x; v.push_back(x); } int max1 = v[0]; x = v[0]; for (i = 1; i < n; i++) { x = gcd_(v[i], x); if (v[i] > max1) max1 = v[i]; } int moves = ((max1 / x) - n); if (moves % 2 != 0) cout << Alice << endl; else cout << Bob << endl; return 0; } |
#include <bits/stdc++.h> std::vector<int> ReadVector(size_t size) { std::vector<int> vector(size); for (auto& element : vector) { std::cin >> element; } return vector; } bool is_pal(const std::string& a) { for (size_t i = 0; i < a.size() / 2; ++i) { if (a[i] != a[a.size() - i - 1]) { return false; } } return true; } class Solver { public: std::vector<std::vector<int>> g; Solver() {} void read() {} void solve() { size_t n; std::cin >> n; g.resize(n); for (size_t i = 0; i < n - 1; ++i) { int a, b; std::cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } int root = -1; int begin = -1; int end = -1; for (int v = 0; v < n; ++v) { if (g[v].size() > 2) { if (root != -1) { std::cout << No << std::endl; return; } else { root = v; } } if (g[v].size() == 1) { if (begin == -1) { begin = v; } else { end = v; } } } std::cout << Yes n ; if (root == -1) { std::cout << 1 << std::endl; std::cout << begin + 1 << << end + 1 << std::endl; return; } std::cout << g[root].size() << std::endl; for (auto ch : g[root]) { std::cout << root + 1 << ; int tmp = ch; int parent = root; while (g[tmp].size() == 2) { if (g[tmp][0] == parent) { parent = tmp; tmp = g[tmp][1]; } else { parent = tmp; tmp = g[tmp][0]; } } std::cout << tmp + 1 << std::endl; } } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); Solver solver; solver.solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long INF = 1e9; const int maxn = 1e6 + 5; int a[maxn]; int tpr = 0; vector<int> adj[maxn]; void dfs(int root) { if (adj[root].size() > 0) { if (adj[root][0] == (root + 1)) { dfs(root + 1); tpr++; } else return; } } int main() { int n, m, t, i, j, k; int flag = 0, flag2; int ans = 0; cin >> n >> k; for (int i = 0; i < k; i++) { cin >> m; ans += m - 1; int y; int prev = -1; for (int j = 0; j < m; j++) { cin >> y; if (y == 1) { flag2 = m - 1; } if ((j == 0) && (y == 1)) { flag = 1; } if (prev != -1) adj[prev].push_back(y); prev = y; } } if (flag == 0) { ans += n - 1; cout << ans << endl; return 0; } dfs(1); ans -= tpr; ans += (n - tpr - 1); cout << ans << endl; return 0; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( sse4 ) using namespace std; using ii = pair<int, int>; using ll = long long; const int N = 3000 + 5; const int mod = 1e9 + 7; string source[N], target[N]; int n; ii ranges[N]; vector<int> FAIL(string pat) { int m = pat.size(); vector<int> F(m + 1); int i = 0, j = -1; F[0] = -1; while (i < m) { while (j >= 0 && pat[i] != pat[j]) j = F[j]; i++, j++; F[i] = j; } return F; } bool check(string a, string b, string s, string t) { vector<int> F = FAIL(s); int i = 0, j = 0; int n = a.size(); int m = s.size(); while (i < n) { while (j >= 0 && a[i] != s[j]) j = F[j]; i++, j++; if (j == m) { int idx = i - j; for (int k = 0; k < m; k++) { a[k + idx] = t[k]; } break; } } return a == b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> source[i]; for (int i = 0; i < n; i++) cin >> target[i]; string s, t; string prefix, suffix; for (int i = 0; i < n; i++) { if (source[i] != target[i]) { int l = -1, r = -1; for (int j = 0; j < source[i].size(); j++) { if (source[i][j] != target[i][j]) r = j; if (source[i][j] != target[i][j] && l == -1) l = j; } ranges[i] = {l, r}; if (s.empty()) { s = source[i].substr(l, r - l + 1); t = target[i].substr(l, r - l + 1); prefix = source[i].substr(0, l); suffix = source[i].substr(r + 1); reverse(prefix.begin(), prefix.end()); } else { if (s != source[i].substr(l, r - l + 1) || t != target[i].substr(l, r - l + 1)) { cout << NO n ; return 0; } string cprefix = source[i].substr(0, l); string csuffix = source[i].substr(r + 1); reverse(cprefix.begin(), cprefix.end()); string p, ss; for (int j = 0; j < min(cprefix.size(), prefix.size()) && cprefix[j] == prefix[j]; j++) { p.push_back(prefix[j]); } for (int j = 0; j < min(csuffix.size(), suffix.size()) && csuffix[j] == suffix[j]; j++) { ss.push_back(suffix[j]); } prefix = p; suffix = ss; } } } reverse(prefix.begin(), prefix.end()); s = prefix + s + suffix; t = prefix + t + suffix; for (int i = 0; i < n; i++) { if (!check(source[i], target[i], s, t)) { cout << NO n ; return 0; } } cout << YES n ; cout << s << n ; cout << t << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool inrange(T a, T l, T r) { if (l > r) swap(l, r); return (a >= l and a <= r); } template <typename T, typename U> inline bool exist(T val, U &any) { return (any.find(val) != any.end()); } template <typename T> inline void inp(T &any) { cin >> any; } template <typename T, typename... U> inline void inp(T &a, U &...b) { cin >> a; inp(b...); } template <typename T> inline istream &operator>>(istream &in, vector<T> &a) { for (auto &x : a) in >> x; return in; } template <typename T, typename U> inline istream &operator>>(istream &in, pair<T, U> &a) { in >> a.first >> a.second; return in; } void solve(long long int &T) { long long int n; std::cin >> n; vector<long long int> a(n), b(n); std::cin >> a >> b; vector<long long int> sol(n); for (long long int i = (0); i < (n); i++) sol[a[i] - 1] = b[i]; for (auto &X : sol) std::cout << X << ; std::cout << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; for (long long int i = (1); i < (t + 1); i++) { cerr << Case # << i << n ; solve(i); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> v; for (int i = 0; i < (n); ++i) { int l; cin >> l; vector<int> temp; for (int j = 0; j < (l); ++j) { int ele; cin >> ele; temp.push_back(ele); } v.push_back(temp); } vector<int> maxi; int cnt_no_ascent = 0; bool ascent[n]; int mini_arr[n]; int maxi_arr[n]; for (int i = 0; i < (n); ++i) ascent[i] = false; for (int i = 0; i < (n); ++i) { int flag = 0; int maxi_el = INT_MIN; int mini_el = INT_MAX; for (int j = 0; j < (v[i].size()); ++j) { if (v[i][j] > maxi_el) { maxi_el = v[i][j]; } if (v[i][j] < mini_el) { mini_el = v[i][j]; } if (j != 0 && v[i][j - 1] < v[i][j]) { ++cnt_no_ascent; flag = 1; ascent[i] = true; break; } } if (flag == 0) { mini_arr[i] = mini_el; maxi.push_back(maxi_el); maxi_arr[i] = maxi_el; } } long long ans = ((long long)cnt_no_ascent * (long long)n); sort(maxi.begin(), maxi.end()); for (int i = 0; i < (n); ++i) { if (ascent[i] == true) continue; auto it = upper_bound(maxi.begin(), maxi.end(), mini_arr[i]); ans = ans + (long long)(maxi.end() - it); ans = ans + cnt_no_ascent; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 6; int main() { int n; cin >> n; string str = ; while (n) { str += ((n % 2) + 0 ); n /= 2; } reverse(str.begin(), str.end()); while (str.size() < 6) str = 0 + str; string ss = ; ss += str[0]; ss += str[5]; ss += str[3]; ss += str[2]; ss += str[4]; ss += str[1]; int ret = 0; for (auto x : ss) ret = ret * 2 + (x - 0 ); cout << ret << n ; } |
#include <bits/stdc++.h> using namespace std; int getint() { unsigned int c; int x = 0; while (((c = getchar()) - 0 ) >= 10) { if (c == - ) return -getint(); if (!~c) exit(0); } do { x = (x << 3) + (x << 1) + (c - 0 ); } while (((c = getchar()) - 0 ) < 10); return x; } int n, nn, sum; int as[20]; int c[10][10]; bool used[20]; int num[20], have[20], ns; int perm[20][20][20]; void dfs(int row, int col, int rows, int cols, int dir, int cnt) { int i, j, k; if (cnt == nn) { int tmp1 = 0, tmp2 = 0; for (i = 0; i < n; i++) { tmp1 += c[i][i]; tmp2 += c[i][n - 1 - i]; } if (tmp1 != sum) return; if (tmp2 != sum) return; tmp1 = tmp2 = 0; for (i = 0; i < n; i++) { tmp1 += c[n - 1][i]; tmp2 += c[i][n - 1]; } if (tmp1 != sum) return; if (tmp2 != sum) return; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j) putchar( ); printf( %d , c[i][j]); } puts( ); } exit(0); } if (row == n or col == n) { int tmp = 0; if (dir == 0) { for (i = 0; i < n; i++) tmp += c[row][i]; if (tmp != sum) return; } else { for (i = 0; i < n; i++) tmp += c[i][col]; if (tmp != sum) return; } if (dir == 0) { dfs(rows + 1, cols, rows + 1, cols, 1, cnt); } else { dfs(rows, cols + 1, rows, cols + 1, 0, cnt); } } for (i = 0; i < ns; i++) { int d = perm[row][col][i]; if (have[d]) { have[d]--; c[row][col] = num[d]; if (dir == 0) dfs(row, col + 1, rows, cols, dir, cnt + 1); if (dir == 1) dfs(row + 1, col, rows, cols, dir, cnt + 1); have[d]++; } } } int main() { int i, j, tcc, tc = 1 << 28; for (tcc = 0; tcc < tc; tcc++) { n = getint(), nn = n * n, sum = 0; for (i = 0; i < nn; i++) as[i] = getint(), sum += as[i]; sum /= n; for (i = 0; i < nn; i++) num[i] = as[i]; sort(num, num + nn); ns = unique(num, num + nn) - num; for (i = 0; i < ns; i++) { for (j = 0; j < nn; j++) if (num[i] == as[j]) have[i]++; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { for (int k = 0; k < ns; k++) perm[i][j][k] = k; random_shuffle(perm[i][j], perm[i][j] + ns); } printf( %d n , sum); dfs(0, 0, 0, 0, 0, 0); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MXX = 3e5 + 5; int n, m; int a[MXX], ans[MXX]; bool ok(int x) { int mx = m - 1; for (int i = n - 1; i >= 0; i--) { if (a[i] <= mx) { if (a[i] + x >= mx) { ans[i] = mx; } else { ans[i] = a[i] + x; } } else { if (a[i] + x >= mx + m) ans[i] = mx; else ans[i] = (a[i] + x) % m; } mx = ans[i]; } for (int i = 0; i < n - 1; i++) { if (ans[i] > ans[i + 1]) { return false; } } return true; } bool ok2() { for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } if (ok2()) { return cout << 0 << endl, 0; } int lo = 0, hi = m - 1, mid, ans = -1; while (lo <= hi) { mid = (lo + hi) / 2; if (ok(mid)) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b); void print(long long arr[], long long n); void printv(vector<long long> arr); signed main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long k; cin >> k; vector<long long> arr(n); unordered_map<long long, long long> m; for (long long i = 0; i < n; i++) { cin >> arr[i]; m[arr[i]]++; } vector<long long> tem = arr; sort(tem.begin(), tem.end()); set<long long> a, b; if (arr[0] * 2 != k) a.insert(arr[0]); for (long long i = 1; i < n; i++) { if (arr[i] * 2 == k) continue; if (a.find(k - arr[i]) == a.end()) a.insert(arr[i]); else b.insert(arr[i]); } long long ii = 0; for (long long i = 0; i < n; i++) { if (a.find(arr[i]) != a.end()) cout << 1 << ; else if (b.find(arr[i]) != b.end()) cout << 0 << ; else { cout << ii << ; ii = (ii + 1) % 2; } } cout << n ; } } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } void print(long long arr[], long long n) { for (long long i = 0; i < n; i++) cout << arr[i] << ; cout << n ; } void printv(vector<long long> arr) { for (long long i = 0; i < arr.size(); i++) cout << arr[i] << ; cout << n ; } |
#include <bits/stdc++.h> using namespace std; int par[300005]; int vis[300005]; unordered_map<int, int> edge[300005]; int d[300005]; vector<int> ans; int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); } void join(int u, int v) { u = find(u); v = find(v); if (u > v) swap(u, v); par[v] = u; } void dfs(int u, int pr) { if (vis[u]) return; vis[u] = 1; for (auto i : edge[u]) { dfs(i.first, u); } if (d[u] == 1) { ans.push_back(edge[u][pr]); d[pr]++; d[pr] %= 2; return; } } int main() { ios_base::sync_with_stdio(false); memset(vis, 0, sizeof vis); int n, k, u, v; cin >> n >> k; for (int i = 1; i <= n; i++) { edge[i].clear(); par[i] = i; cin >> d[i]; } for (int i = 1; i <= k; i++) { cin >> u >> v; if (find(u) != find(v)) { edge[u][v] = i; edge[v][u] = i; join(u, v); } } int sum = 0; int avail = 0; for (int i = 1; i <= n; i++) { if (d[i] == -1) { avail++; } else sum += d[i]; } if (sum % 2 && !avail) { cout << -1 << endl; return 0; } for (int i = 1; i <= n; i++) { if (d[i] == -1) { if (sum % 2 == 1) { d[i] = 1; sum++; } else d[i] = 0; } } dfs(1, 0); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e6 + 100; const long long mod = 1e9 + 7; const long long inf = 1e18 + 12; vector<long long> arr(MAX); map<long long, long long> mm; int main() { ios_base::sync_with_stdio(false); cout.tie(NULL); cout.tie(NULL); ; long long tt = 1; while (tt--) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long w = 0, b = 0; long long i = 0; while (i < n) { if (i % 2 == 0) { w += (a[i] + 1) / 2; b += (a[i]) / 2; } else { w += a[i] / 2; b += (a[i] + 1) / 2; } i++; } cout << min(w, b); } } |
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 200005; const double PI = 4 * atan(1); const long long MAX = 9223372036854775807; int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } bool isprime(long long u) { for (long long i = 2; i <= (int)sqrt(u); i++) { if (u % i == 0) return 0; } return 1; } long long mod(long long to_mod, long long MOD) { to_mod %= MOD; while (to_mod < 0) to_mod += MOD; return to_mod % MOD; } long long moduloMultiplication(long long a, long long b, long long mod) { long long res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; a = (2 * a) % mod; b >>= 1; } return res; } long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long n, m, x, y, t, k, z, w, l, r, ans; bool vis[N]; long long a[N]; long long lvl[N]; map<long long, vector<long long> > adj; map<long long, long long> maa; void bfs(long long src) { queue<long long> q; q.push(src); lvl[src] = 1; while (!q.empty()) { long long ras = q.front(); q.pop(); if (lvl[ras] % 2 == 0) { vis[ras] = 1; continue; } for (auto t : adj[ras]) { if (vis[t]) continue; lvl[t] = lvl[ras] + 1; q.push(t); } } } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; maa[a[i]] = i; } for (int j = 0; j < n; j++) { k = a[j]; for (int i = j; i < n; i += k) { if (a[i] > k) adj[i].push_back(j); } for (int i = j; i >= 0; i -= k) { if (a[i] > k) adj[i].push_back(j); } } for (int i = n; i >= 1; i--) { if (vis[maa[i]]) continue; bfs(maa[i]); } for (int i = 0; i < n; i++) { if (vis[i]) cout << A ; else cout << B ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; int n, lim, a[N], mn[N], mx[N], suf[N]; long long ans; int main() { scanf( %d%d , &n, &lim); for (int i = 1; i <= lim; i++) mn[i] = n + 1; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); mn[a[i]] = min(mn[a[i]], i); mx[a[i]] = max(mx[a[i]], i); } int L = lim, R = 1, pre = mx[1]; for (int i = 1; i < lim; i++) { pre = max(pre, mx[i]); if (pre > mn[i + 1]) { L = i; break; } } if (L == lim) { cout << 1ll * lim * (lim + 1) / 2; return 0; } pre = mn[lim]; for (int i = lim; i > 1; i--) { pre = min(pre, mn[i]); if (mx[i - 1] > pre) { R = i; break; } } ans = lim - R + 2; suf[lim] = mn[lim]; for (int i = lim - 1; i >= R; i--) suf[i] = min(suf[i + 1], mn[i]); pre = mx[1]; for (int i = 1, j = R; i <= L; i++) { pre = max(pre, mx[i]); while (j <= lim && pre > suf[j]) j++; ans += lim - j + 2; if (j == i + 1) ans--; } cout << ans; } |
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n, m; int a[300000]; int b[300000]; vector<int> edge[300000]; int ngood; int child[300000]; int good[300000]; int d[300000]; int ans; void back(int now, int parent) { child[now] = 1; good[now] = b[now]; for (auto e : edge[now]) if (e != parent) { back(e, now); child[now] += child[e], good[now] += good[e]; } } vector<pair<int, int> > v; void back2(int now, int parent) { for (auto e : edge[now]) if (e != parent) { back2(e, now); } if (b[now] == 0) { d[now] = 0; return; } d[now] = 1; int xgood = 0; for (auto e : edge[now]) if (e != parent) { if (child[e] == good[e]) xgood += child[e]; } d[now] = 1 + xgood; for (auto e : edge[now]) if (e != parent) { d[now] = max(d[now], 1 + d[e] + xgood - (child[e] == good[e] ? child[e] : 0)); } ans = max(ans, d[now]); int u = 1 + (n - child[now] == ngood - good[now] ? n - child[now] : 0); v.clear(); for (auto e : edge[now]) if (e != parent) { v.emplace_back(d[e], e); } sort((v).begin(), (v).end(), greater<pair<int, int> >()); for (int i = 0; i < 8 && i < v.size(); i++) { for (int j = i + 1; j < 8 && j < v.size(); j++) { ans = max( ans, u + v[i].first + v[j].first + xgood - (child[v[i].second] == good[v[i].second] ? child[v[i].second] : 0) - (child[v[j].second] == good[v[j].second] ? child[v[j].second] : 0)); } } } int param(int x) { int i, j, k; for (i = 0; i < n; i++) b[i] = a[i] >= x; ngood = count(b, b + n, 1); back(0, -1); ans = 0; back2(0, -1); return ans >= m; } int main() { int i, j, k; cin >> n >> m; for (i = 0; i < n; i++) scanf( %d , &a[i]); for (i = 0; i < n - 1; i++) { int x, y; scanf( %d%d , &x, &y), x--, y--; edge[x].push_back(y), edge[y].push_back(x); } int low = 0, high = 1000001, mid; while (low <= high) { mid = (low + high) / 2; if (param(mid)) low = mid + 1; else high = mid - 1; } cout << high; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5e4 + 10, max_node = N * 200; int n, m, q, C, c[N], a[N], size[N], heavy[N], dfn[N], dfn_t, top[N], father[N], tt, lo[max_node], ro[max_node], addv[max_node]; long long sum[max_node], sum2[max_node]; vector<int> graph[N], nodes[N]; void dfs1(int u) { size[u] = 1; for (auto v : graph[u]) { dfs1(v); size[u] += size[v]; if (size[v] > size[heavy[u]]) { heavy[u] = v; } } } void dfs2(int u, int t) { top[u] = t; dfn[u] = ++dfn_t; if (heavy[u]) { dfs2(heavy[u], t); for (auto v : graph[u]) { if (v != heavy[u]) { dfs2(v, v); } } } } void add(int l, int r, int o, int v) { addv[o] += v; sum2[o] += ((sum[o] << 1) + (r - l + 1) * v) * v; sum[o] += (r - l + 1) * v; } void push_down(int l, int r, int o) { if (addv[o]) { int mid = l + r >> 1; if (!lo[o]) { lo[o] = ++tt; } if (!ro[o]) { ro[o] = ++tt; } add(l, mid, lo[o], addv[o]); add(mid + 1, r, ro[o], addv[o]); addv[o] = 0; } } void push_up(int o) { sum[o] = sum[lo[o]] + sum[ro[o]]; sum2[o] = sum2[lo[o]] + sum2[ro[o]]; } void modify(int l, int r, int& o, int ql, int qr, int v) { if (!o) { o = ++tt; } if (ql <= l && r <= qr) { return add(l, r, o, v); } else { int mid = l + r >> 1; push_down(l, r, o); if (ql <= mid) { modify(l, mid, lo[o], ql, qr, v); } if (qr > mid) { modify(mid + 1, r, ro[o], ql, qr, v); } push_up(o); } } struct info_t { int root; void init(vector<int>& nodes) { for (auto v : nodes) { for (int x = v; x; x = father[top[x]]) { modify(1, n, root, dfn[top[x]], dfn[x], 1); } } } void update(int u, int type) { for (int x = u; x; x = father[top[x]]) { modify(1, n, root, dfn[top[x]], dfn[x], type); } } double get_answer(int c) { double result = (double)C * C; result -= 2.0 * C * c * sum[root] / n; result += 1.0 * c * c * sum2[root] / n; return result; } } info[N]; int main() { scanf( %d%d%d%d , &n, &m, &q, &C); for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); nodes[a[i]].push_back(i); } for (int i = 2; i <= n; ++i) { scanf( %d , &father[i]); graph[father[i]].push_back(i); } for (int i = 1; i <= m; ++i) { scanf( %d , &c[i]); } dfs1(1); dfs2(1, 1); for (int i = 1; i <= m; ++i) { info[i].init(nodes[i]); } while (q--) { int type, x, w; scanf( %d , &type); if (type == 1) { scanf( %d%d , &x, &w); info[a[x]].update(x, -1); a[x] = w; info[a[x]].update(x, 1); } else { scanf( %d , &x); printf( %.12lf n , info[x].get_answer(c[x])); } } return 0; } |
#include <bits/stdc++.h> const long long int mod = 1000000007; const long long int AM = 2e6 + 5; using namespace std; long long int powm(long long int a, long long int b, long long int n) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % n; } y = (y * y) % n; b /= 2; } return x % n; } long long int pow(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; } long long int fact(long long int n) { long long int ans = 1; for (long long int i = 1; i <= n; i++) ans *= i; return ans; } long long int nCr(long long int n, long long int r) { if (r > n - r) r = n - r; long long int ans = 1, i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans; } bool isPrime(long long int n) { if (n <= 1) return false; for (long long int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int n; cin >> n; long long int s = sqrt(n), f1, f2; if (s * s >= n) { f1 = s; f2 = s; } else if (s * (s + 1) >= n) { f1 = s; f2 = s + 1; } else { f1 = s + 1; f2 = s + 1; } cout << f1 + f2; } |
#include <bits/stdc++.h> using namespace std; int f[305][305], rf[305][305], g[305][305], rg[305][305]; int a[305][305]; int n, m, s, t1, t2, t3; int ans1, ans2, ans3, ans4, Min = 1999999999; set<pair<int, int> > S; int C(int first, int second) { if (first < second) return t2; if (first == second) return t1; return t3; } int G(int i, int j, int k, int l) { int ans = (f[i][l] - f[i][k]) + (rf[j][l] - rf[j][k]); ans += (g[l][j] - g[l][i]); return ans; } int main() { scanf( %d %d %d , &n, &m, &s); scanf( %d %d %d , &t1, &t2, &t3); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); for (int i = 1; i <= n; i++) { f[i][1] = 0; for (int j = 2; j <= m; j++) f[i][j] = f[i][j - 1] + C(a[i][j - 1], a[i][j]); rf[i][1] = 0; for (int j = 2; j <= m; j++) rf[i][j] = rf[i][j - 1] + C(a[i][j], a[i][j - 1]); } for (int i = 1; i <= m; i++) { g[i][1] = 0; for (int j = 2; j <= n; j++) g[i][j] = g[i][j - 1] + C(a[j - 1][i], a[j][i]); rg[i][1] = 0; for (int j = 2; j <= n; j++) rg[i][j] = rg[i][j - 1] + C(a[j][i], a[j - 1][i]); } for (int i = 1; i <= n; i++) for (int j = i + 2; j <= n; j++) { S.clear(); for (int k = m; k >= 1; k--) { if (k <= m - 2) S.insert(make_pair(G(i, j, 1, k + 2), k + 2)); if (S.empty()) continue; int tmp = s + f[i][k] + rf[j][k] - rg[k][j] + rg[k][i]; set<pair<int, int> >::iterator it = S.upper_bound(make_pair(tmp, 0)); if (it != S.end()) { pair<int, int> tmpg = *it; if (abs(tmpg.first - tmp) < Min) { Min = abs(tmpg.first - tmp); ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second; } } if (it == S.begin()) continue; it--; pair<int, int> tmpg = *it; if (abs(tmpg.first - tmp) < Min) { Min = abs(tmpg.first - tmp); ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second; } } } printf( %d %d %d %d n , ans1, ans3, ans2, ans4); return 0; } |
#include <bits/stdc++.h> template <class T> T abs(T x) { return x > 0 ? x : -x; } const int MAXINT = 1111 * 1111 * 1111; const long long MAXLINT = MAXINT * 1ll * MAXINT; const long double EPS = 1e-10; using namespace std; int n, m, x, y, z, p; int a, b; void reverse_left() { int aa = a, bb = b; a = m - b + 1; b = aa; swap(n, m); } void reverse_middle() { b = m - b + 1; } void reverse_right() { int aa = a, bb = b; b = n - a + 1; a = bb; swap(n, m); } void solve() { cin >> n >> m >> x >> y >> z >> p; int N = n, M = m; x %= 4; z %= 4; y %= 2; while (p--) { n = N; m = M; cin >> a >> b; for (int i = 0; i < x; i++) reverse_right(); for (int i = 0; i < y; i++) reverse_middle(); for (int i = 0; i < z; i++) reverse_left(); cout << a << << b << endl; } } int main() { solve(); } |
#include <bits/stdc++.h> using namespace std; set<long long> s; map<long long, long long> m; vector<long long> v; long long n, a[100010], b[100010], dp[100010], mx = 0; bool mark[100010]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (i == 1) dp[i] = a[i]; else dp[i] = dp[i - 1] + a[i]; } for (long long i = 1; i <= n; i++) cin >> b[i]; for (long long i = n; i > 0; i--) { v.push_back(mx); long long r, l; mark[b[i]] = 1; if (b[i] < n) { if (mark[b[i] + 1] == 1) { set<long long>::iterator it = s.lower_bound(b[i]); r = *it; } else r = b[i]; } else r = b[i]; s.insert(r); if (b[i] > 1) { if (mark[b[i] - 1] == 1) { s.erase(b[i] - 1); } } m[r] = m[b[i] - 1] + dp[r] - dp[b[i] - 1]; if (m[r] > mx) mx = m[r]; } for (long long i = v.size() - 1; i >= 0; i--) cout << v[i] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(vector<vector<T>> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << { ; _print(p.first); cerr << , ; _print(p.second); cerr << } ; } template <class T> void _print(vector<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(vector<vector<T>> v) { cerr << ==> << endl; for (vector<T> vec : v) { for (T i : vec) { _print(i); cerr << ; } cerr << endl; } } template <class T> void _print(set<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(multiset<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T, class V> void _print(map<T, V> v) { cerr << [ ; for (auto i : v) { _print(i); cerr << ; } cerr << ] ; } mt19937_64 rang( chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; long long mod_exp(long long a, long long b) { a %= mod; if (a == 0) return 0LL; long long res = 1LL; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } long long mod_inv(long long a) { return mod_exp(a, mod - 2); } long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); } void solve() { long long a1, a2, a3, a4, a5, a6; cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; cout << ((a1 + a2 + a3) * (a1 + a2 + a3)) - (a1 * a1) - (a3 * a3) - (a5 * a5); } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; char s[1005]; char t[1000005]; int main() { scanf( %s , s); scanf( %s , t); int len1 = strlen(s); int len2 = strlen(t); int le = 0x3f3f3f3f; int ri = 0x3f3f3f3f; for (int i = 0, j = 0; j < len2; j++) { if (s[i] == t[j]) i++; if (i == len1) { le = j; break; } } for (int i = len1 - 1, j = len2 - 1; j >= 0; j--) { if (s[i] == t[j]) i--; if (i == -1) { ri = j; break; } } if ((le - ri) == (len1 - 1)) cout << 0 << endl; else { int ans = ri - le; cout << ans << endl; } } |
#include <bits/stdc++.h> int N, K; std::string S; int main() { std::cin >> N >> K; std::cin >> S; std::string t = S; std::string s = ; for (int l = 1; l < N; ++l) { if (S.substr(0, l) == S.substr(N - l, l)) { t = S.substr(0, N - l); s = S.substr(N - l, l); } } while (K--) std::cout << t; std::cout << s << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> candies(n); for (int i = 0; i < n; i++) cin >> candies[i]; int i = 0, j = n - 1; int store_a = 0, store_b = 0; int turn = 0, sum_a = 0, sum_b = 0; while (i <= j) { if (turn % 2 == 0) { int str_temp = 0; while ((str_temp <= store_b) && (i <= j)) { str_temp += candies[i]; i++; } store_a = str_temp; sum_a += store_a; } else { int str_tem = 0; while ((str_tem <= store_a) && (i <= j)) { str_tem += candies[j]; j--; } store_b = str_tem; sum_b += store_b; } turn++; } cout << turn << << sum_a << << sum_b << n ; } int main() { int t; cin >> t; while (t) { solve(); t--; } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast , unroll-loops , no-stack-protector ) using namespace std; using ll = long long; using ld = double; using pii = pair<int, int>; using uint = unsigned int; using ull = unsigned long long; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); default_random_engine generator; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << setprecision(16) << fixed; cerr << setprecision(16) << fixed; int tests = 1; for (int test = 1; test <= tests; ++test) { solve(); } } const int INF = 1e9; struct Point { int x, y; }; void solve() { int n; cin >> n; vector<Point> p(n); for (int i = 0; i < n; ++i) { string s; cin >> s; int a = 0, b = 0; for (char c : s) { a += c == B ; b += c == N ; } p[i] = {a, b}; } int minx = INF, maxx = -INF; int miny = INF, maxy = -INF; int l = INF, r = -INF; for (auto [x, y] : p) { minx = min(minx, x); maxx = max(maxx, x); miny = min(miny, y); maxy = max(maxy, y); l = min(l, x - y); r = max(r, x - y); } int res = 0; res = max(res, (maxx - minx + 1) / 2); res = max(res, (maxy - miny + 1) / 2); res = max(res, (minx - miny) - l); res = max(res, r - (minx - miny)); Point best = {minx + res, miny + res}; for (int op = 0; op < 2; ++op) { int xx = miny + l; int yy = miny; int cur = 0; cur = max(cur, (maxx - xx + 2) / 3); cur = max(cur, (maxy - yy + 1) / 2); cur = max(cur, (r - l + 1) / 2); if (cur < res && cur <= minx - xx) { res = cur; best = {xx + 2 * res, yy + res}; if (op) { swap(best.x, best.y); } } for (int i = 0; i < n; ++i) { swap(p[i].x, p[i].y); } swap(minx, miny); swap(maxx, maxy); r *= -1; l *= -1; swap(l, r); } cout << res << endl; string ans; for (int i = 0; i < best.x; ++i) { ans += B ; } for (int i = 0; i < best.y; ++i) { ans += N ; } cout << ans << endl; } |