solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts("-1"); exit(0); } long double dp[2][10025], p[2][10025]; void fmain(int tid) { scanf("%d", &n); long double C; scanf("%Lf", &C); C /= 1e6; for (int i = 0; i <= n; i++) { scanf("%Lf", &p[0][i]); p[0][i] /= 1e6; dp[0][i] = i; } int t = 0; for (int i = n - 1; ~i; i--) { int cur = t ^ 1, pre = t; t ^= 1; for (int red = 0; red <= i; red++) { long double p1 = p[pre][red + 1] * (red + 1) / (i + 1); long double p0 = p[pre][red] * (i + 1 - red) / (i + 1); p[cur][red] = p0 + p1; if (p[cur][red] == 0) continue; long double P = p1 / (p1 + p0); long double tmp = P * dp[pre][red + 1] + (1 - P) * dp[pre][red] - C; dp[cur][red] = max(tmp, (long double)red); } } printf("%.10Lf\n", dp[t][0]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
10
#include <bits/stdc++.h> using namespace std; string s; string s1 = "ACTG"; int m(int y) { int ans = 0; for (int i = y; i < y + 4; i++) { int mm = min((s1[i - y] - 'A') + 26 - (s[i] - 'A'), abs((s[i] - 'A') - (s1[i - y] - 'A'))); ans += min(mm, 1 + (s[i] - 'A') + 25 - (s1[i - y] - 'A')); } return ans; } int main() { int n; cin >> n; int ans = 1000000; cin >> s; for (int i = 0; i < n - 3; i++) { ans = min(ans, m(i)); } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int MOD = 1E9 + 7; long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } long long int gcd_ex(long long int a, long long int b, long long int& x, long long int& y) { if (b == 0) { x = 1; y = 0; return a; } long long int x1, y1; long long int g = gcd_ex(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return g; } long long int multi_mod_inv(long long int a, long long int m) { long long int x, y; long long int g = gcd_ex(a, m, x, y); if (g != 1) { return -1; } x = (x % m + m) % m; return x; } long long int mod_div(long long int a, long long int b, long long int m) { if (multi_mod_inv(b, m) != -1) { return (a % m * multi_mod_inv(b, m) % m) % m; } else { return -1; } } long long int lcm(long long int a, long long int b) { return a / gcd(a, b) * b; } long long int power(long long int a, long long int n) { if (n == 0) return 1; long long int p = power(a, n / 2); p = (p * p) % MOD; if (n % 2) return (p * a) % MOD; else return p % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long int test_case = 1; for (long long int test = 1; test <= test_case; test++) { long long int n; cin >> n; vector<long long int> a(n + 1), b(n + 1); for (long long int i = 1; i <= n; i++) { cin >> a[i]; } for (long long int i = 1; i <= n; i++) { cin >> b[i]; } a[0] = b[0] = 0; vector<long long int> dist(n + 1, 1E9); dist[n] = 0; vector<long long int> parent(n + 1, -1); vector<long long int> depth(n + 1, -1); set<long long int> s; for (long long int i = 0; i <= n; i++) { s.insert(i); } queue<long long int> q; q.push(n); while (!q.empty()) { long long int v = q.front(); q.pop(); long long int d = dist[v]; while (true) { auto it = s.lower_bound(v - a[v]); if (it == s.end() || *it > v) { break; } long long int x = (*it) + b[*it]; if (dist[x] == 1E9) { dist[x] = d + 1; parent[x] = v; depth[x] = *it; q.push(x); } s.erase(it); } } if (dist[0] != 1E9) { cout << dist[0] << '\n'; long long int cur = 0; vector<long long int> ans; while (cur != n) { ans.push_back(depth[cur]); cur = parent[cur]; } reverse(ans.begin(), ans.end()); for (auto x : ans) { cout << x << " "; } cout << '\n'; } else { cout << -1 << '\n'; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long cd, s, r, fact[10001], sf[10001], invmod[10001]; void inp() { cin >> cd >> s >> r; } long long bigmod(long long base, long long hat) { if (hat == 0) return 1; long long k = bigmod(base, hat / 2); if (hat % 2 == 0) return k * k % mod; return k * k % mod * base % mod; } void create() { for (long long i = 0; i <= 10000; i++) { if (i == 0) fact[i] = 1; else fact[i] = fact[i - 1] * i % mod; sf[i] = bigmod(fact[i], mod - 2); invmod[i] = bigmod(i, mod - 2); } } long long C(long long k, long long n) { if (k > n || k < 0 || n < 0) return 0; return fact[n] * sf[k] % mod * sf[n - k] % mod; } void do_it() { create(); long long all = 0, kq = 0; for (long long i = r; i <= s; i++) { all += C(cd - 2, s - i + cd - 1 - 1); all %= mod; if (cd * i == s) kq += invmod[cd], kq %= mod; for (long long e = 1; e < cd; e++) { if (e * i > s) break; long long remain = s - e * i, inv = 0; remain += cd - e; long long space = C(e - 1, cd - 1) * C(cd - e - 1, remain - 1) % mod; for (long long f = 1; f <= cd - e; f++) { long long t = remain - i * f; if (t < 0 || cd - e - 1 > t - 1) break; if (f % 2 == 1) inv += C(cd - e - 1, t - 1) * C(f, cd - e) % mod; else inv -= C(cd - e - 1, t - 1) * C(f, cd - e) % mod; inv += mod * 5, inv %= mod; } inv *= C(cd - e, cd - 1); space -= inv, space += mod * 5, space %= mod; kq += space * invmod[e] % mod, kq %= mod; } } if (cd == 1) kq = 1, all = 1; cout << kq * bigmod(all, mod - 2) % mod << endl; } int main() { inp(); do_it(); }
8
#include <bits/stdc++.h> using namespace std; bool cmp(const vector<int> &a, const vector<int> &b, int shift) { for (int i = 0; i < a.size(); i++) if (a[i] != b[(i + shift) % a.size()]) return false; return true; } int main() { int n, L; cin >> n >> L; vector<int> a(n), b(n); for (auto &el : a) cin >> el; for (auto &el : b) cin >> el; vector<int> d1(n), d2(n); for (int i = 0; i + 1 < n; i++) { d1[i] = a[i + 1] - a[i]; d2[i] = b[i + 1] - b[i]; } d1[n - 1] = a[0] - a[n - 1] + L; d2[n - 1] = b[0] - b[n - 1] + L; bool eq = false; for (int shift = 0; shift < n; shift++) if (cmp(d1, d2, shift)) { eq = true; break; } cout << (eq ? "YES" : "NO") << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-9; const int MOD = 1e9 + 7; const int INF = 1e9 + 10000; const long long INFLL = 4e18; const int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1}; const int dc[] = {0, 1, 0, -1, 1, 1, -1, -1}; long long pref[100005]; long long sleep[100005]; long long a[100005]; long long t[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; long long ans = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> t[i]; } for (int i = 1; i <= n; i++) { if (t[i] == 0) { sleep[i] += a[i]; } else { ans += a[i]; } } for (int i = 1; i <= n; i++) { pref[i] += sleep[i]; if (i > 1) pref[i] += pref[i - 1]; } long long add = 0; for (int i = k; i <= n; i++) { add = max(add, pref[i] - pref[i - k]); } printf("%lld\n", ans + add); return 0; }
2
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } void __print(long long first) { cerr << first; } void __print(long first) { cerr << first; } void __print(unsigned first) { cerr << first; } void __print(unsigned long first) { cerr << first; } void __print(unsigned long long first) { cerr << first; } void __print(float first) { cerr << first; } void __print(double first) { cerr << first; } void __print(long double first) { cerr << first; } void __print(char first) { cerr << '\'' << first << '\''; } void __print(const char *first) { cerr << '\"' << first << '\"'; } void __print(const string &first) { cerr << '\"' << first << '\"'; } void __print(bool first) { cerr << (first ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &first) { cerr << '{'; __print(first.first); cerr << ','; __print(first.second); cerr << '}'; } template <typename T> void __print(const T &first) { long long f = 0; cerr << '{'; for (auto &i : first) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } long long dx[] = {0, 0, 1, -1}; long long dy[] = {1, -1, 0, 0}; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, s, l; cin >> n >> s >> l; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; if (l > n) { cout << -1 << endl; return 0; } vector<long long> res(n + 1, 2e9); res[0] = 0; multiset<long long> L; multiset<long long> value; long long t = 0; for (long long i = 0; i < l - 1; i++) value.insert(a[i]); for (long long i = l - 1; i < n; i++) { L.insert(res[i - l + 1]); value.insert(a[i]); while (L.size() && (*(value.rbegin()) - (*(value.begin())) > s)) { L.erase(L.find(res[t])); value.erase(value.find(a[t])); t++; } if (L.size()) res[i + 1] = 1 + *(L.begin()); } if (res[n] >= 2e9) res[n] = -1; cout << res[n] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int m = 1000000; int n; bool c[m]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; c[x - 1] = true; } vector<int> ans; vector<pair<int, int> > gg; int q = 0; for (int i = 0; i < m; ++i) { if (c[i]) { if (!c[m - i - 1]) ans.push_back(m - i - 1); else ++q; } else { if (!c[m - i - 1]) gg.push_back(make_pair(i, m - i - 1)); } } q /= 2; for (int i = 0; i < q; ++i) { ans.push_back(gg[i].first); ans.push_back(gg[i].second); } sort(ans.begin(), ans.end()); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) cout << ans[i] + 1 << ' '; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = (long long)1e6 + 1; const int inf = (long long)1e9 + 1; long long n, k, pa[N], pb[N], mxa, mxb; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> s; pa[0] = 'b' - s[0]; for (long long i = 1; i < s.size(); i++) { pa[i] = pa[i - 1] + 'b' - s[i]; } pb[0] = abs('a' - s[0]); for (long long i = 1; i < s.size(); i++) { pb[i] = pb[i - 1] + abs('a' - s[i]); } long long l = 0, r = 0; while (r != n - 1) { r++; while ((r - l + 1) - (pa[r] - pa[l - 1]) > k) { l++; } mxa = max(mxa, (r - l + 1)); } l = 0, r = 0; while (r != n - 1) { r++; while ((r - l + 1) - (pb[r] - pb[l - 1]) > k) { l++; } mxb = max(mxb, (r - l + 1)); } if (s.size() == 1) { cout << 1; return 0; } cout << max(mxb, mxa); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long h, c; const double eps = 1e-8; double getT(long long k) { double x = 1.0; return x * ((h + c) * k - c) / (2 * k - 1); } int main() { int T; long long t; scanf("%d", &T); while (T--) { scanf("%lld%lld%lld", &h, &c, &t); if (t == h) { puts("1"); continue; } if (h + c >= 2 * t) { puts("2"); continue; } long long l = 1, r = 1e18; long long ans1, ans2, ans; while (l <= r) { long long mid = (l + r) / 2; if (getT(mid) >= t) { ans1 = mid; l = mid + 1; } else r = mid - 1; } l = 1, r = 1e18; while (l <= r) { long long mid = (l + r) / 2; if (getT(mid) <= t) { ans2 = mid; r = mid - 1; } else l = mid + 1; } if (fabs(getT(ans1) - t) <= fabs(getT(ans2) - t)) ans = ans1; else ans = ans2; printf("%lld\n", ans * 2 - 1); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, mxm = INT_MIN, mnm = 1; cin >> n; unordered_map<long long, long long> m, m2; vector<long long> xxx(n), l(n, 0), r(n, 0); for (long long j = 0; j < n; j++) { cin >> xxx[j]; } for (long long j = 0; j < n; j++) { mxm = max(mxm, xxx[j]); if (m.find(xxx[j]) == m.end()) { m[xxx[j]] = 1; if (mxm - mnm + 1 == m.size()) { l[j] = m.size(); } } else { break; } } mnm = 1, mxm = INT_MIN; for (long long j = n - 1; j >= 0; j--) { mxm = max(mxm, xxx[j]); if (m2.find(xxx[j]) == m2.end()) { m2[xxx[j]] = 1; if (mxm - mnm + 1 == m2.size()) { r[j] = m2.size(); } } else { break; } } long long count = 0; for (long long j = 0; j < n - 1; j++) { if ((l[j] != 0) && (r[j + 1] != 0)) { count++; } } cout << count << "\n"; for (long long j = 0; j < n - 1; j++) { if ((l[j] != 0) && (r[j + 1] != 0)) { cout << l[j] << " " << r[j + 1] << "\n"; } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; int a, b; int t[50][50]; int sol = 9999999; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) scanf("%d", t[i] + j); scanf("%d %d", &a, &b); for (int i = 0; i <= n - a; ++i) for (int j = 0; j <= m - b; ++j) { int sum = 0; for (int k = 0; k < a; ++k) for (int l = 0; l < b; ++l) sum += t[i + k][j + l]; sol = min(sol, sum); } swap(a, b); for (int i = 0; i <= n - a; ++i) for (int j = 0; j <= m - b; ++j) { int sum = 0; for (int k = 0; k < a; ++k) for (int l = 0; l < b; ++l) sum += t[i + k][j + l]; sol = min(sol, sum); } printf("%d\n", sol); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 5; map<long long int, int> mp; queue<int> Q; int vis[M]; long long int dk[M]; vector<long long int> step; long long int a[M]; int rch[M]; struct TREE { int lc; int rc; long long int l; long long int r; long long int idx; long long int val; } tree[20 * M]; int tot = 1; void update(long long int l, long long int r, int node, long long int val, long long int idx) { if (l == r) { tree[node].val += val; tree[node].l = l; tree[node].r = r; tree[node].idx = idx; tree[node].lc = node; tree[node].rc = node; return; } long long int m = (l + r) / 2; if (tree[node].lc == 0) { tot++; tree[node].lc = tot; } if (tree[node].rc == 0) { tot++; tree[node].rc = tot; } if (idx <= m) { update(l, m, tree[node].lc, val, idx); } else { update(m + 1, r, tree[node].rc, val, idx); } int lc = tree[node].lc; int rc = tree[node].rc; if (tree[lc].val < tree[rc].val) { tree[node].idx = tree[rc].idx; tree[node].val = tree[rc].val; } else { tree[node].idx = tree[lc].idx; tree[node].val = tree[lc].val; } return; } int main() { time_t t_start, t_end; t_start = clock(); long long int h; cin >> h; int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { long long int ai; int ci; cin >> ai >> ci; a[i + 1] = ai; mp[ai] = ci; if (ai % k == 1) { rch[i + 1] = 1; update(1, M, 1, ci, i + 1); } } step.push_back(k); for (int q = 0; q < m; q++) { int ty; cin >> ty; if (ty == 1) { long long int x; cin >> x; step.push_back(x); for (int i = 0; i < M; i++) { vis[i] = 0; dk[i] = -1; } Q.push(1); dk[1] = 0; while (!Q.empty()) { int u = Q.front(); vis[u] = 0; Q.pop(); for (int i = 0; i < step.size(); i++) { long long int dx = step[i]; int nxtu = (u + dx) % k; long long int nxty = (u + dk[u] * k + dx) / k; if (dk[nxtu] == -1 || dk[nxtu] > nxty) { dk[nxtu] = nxty; if (vis[nxtu] == 0) { Q.push(nxtu); vis[nxtu] = 1; } } } } for (int i = 1; i <= n; i++) { long long int ai = a[i]; int u = ai % k; if (dk[u] != -1) { long long int yy = dk[u] * k + u; if (ai >= yy && rch[i] == 0) { rch[i] = 1; update(1, M, 1, mp[ai], i); } } } } if (ty == 2) { int x; long long int y; cin >> x >> y; long long int ai = a[x]; mp[ai] = mp[ai] - y; if (rch[x] == 1) { update(1, M, 1, -y, x); } } if (ty == 3) { long long int res = tree[1].val; cout << res << endl; update(1, M, 1, -res, tree[1].idx); } } t_end = clock(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, k, l; char tt[61]; int nt[61]; void maken() { int now = 0; for (int i = 2; i <= l; i++) { while (now > 0 && tt[now + 1] != tt[i]) now = nt[now]; if (tt[now + 1] == tt[i]) now++; nt[i] = now; } } int main() { scanf("%d%d", &n, &k); scanf("%s", tt + 1); printf("%s", tt + 1); if (k == 1) { return 0; } l = strlen(tt + 1); maken(); for (int i = 1; i < k; i++) { for (int j = nt[l] + 1; j <= l; j++) printf("%c", tt[j]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MX = (1 << 20), MOD = 1e9 + 7; bool lucky(int x) { while (x > 0) { if (x % 10 != 4 && x % 10 != 7) return false; x /= 10; } return true; } vector<pair<int, int> > v[MX]; int n; int cnt[MX]; long long ans = 0; void Pdfs(int x, int p) { cnt[x] = 1; for (auto pp : v[x]) { int nxt = pp.first, C = pp.second; if (nxt == p) continue; Pdfs(nxt, x); if (C == 0) cnt[x] += cnt[nxt]; } } void Add(int fakss) { long long valid = n - 1 - fakss; valid *= (valid - 1); ans += valid; } void dfs(int x, int p, int load) { int fakss = load + cnt[x] - 1; Add(fakss); int nxt, C, nload = load; for (auto pp : v[x]) { nxt = pp.first; C = pp.second; if (nxt == p) continue; if (C) nload = 0; else nload = load + cnt[x] - cnt[nxt]; dfs(nxt, x, nload); } } int main() { scanf("%d", &n); for (int j = 1; j < n; j++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); c = lucky(c); v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } Pdfs(1, 0); dfs(1, 0, 0); cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; const int N = int(500 * 1000 + 7); long long n, k; vector<pair<long long, long long> > adj[N]; long long dp[N][2]; void dfs(int v, int p = -1) { vector<long long> x; long long curr = 0; for (auto it : adj[v]) { long long to = it.first; long long w = it.second; if (to == p) continue; dfs(to, v); curr += dp[to][0]; x.push_back(dp[to][1] + w - dp[to][0]); } sort(x.begin(), x.end(), greater<long long>()); for (long long i = 0; i < min((long long)x.size(), k); i++) { if (x[i] > 0) curr += x[i]; } dp[v][0] = dp[v][1] = curr; if (k <= (long long)x.size() && x[k - 1] > 0) dp[v][1] -= x[k - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long q; cin >> q; while (q--) { cin >> n >> k; for (long long i = 0; i < n; i++) adj[i].clear(); for (long long i = 0; i < n - 1; i++) { long long u, v, w; cin >> u >> v >> w; u--; v--; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } dfs(0, -1); cout << dp[0][0] << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<int, int>; using vi = vc<int>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream& operator<<(ostream& os, const array<t, n>& first) { return os << vc<t>(first.begin(), first.end()); } template <int i, class T> void print_tuple(ostream&, const T&) {} template <int i, class T, class H, class... Args> void print_tuple(ostream& os, const T& t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, int suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(int n, int off = 0) { vi v(n); for (int i = int(0); i < int(n); i++) v[i] = read() + off; return v; } template <class T> void print(const vector<T>& v, int suc = 1) { for (int i = int(0); i < int(v.size()); i++) print(v[i], i == int(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T& t) { return t * t; } void yes(bool ex = true) { cout << "YES" << "\n"; if (ex) exit(0); } void no(bool ex = true) { cout << "NO" << "\n"; if (ex) exit(0); } void possible(bool ex = true) { cout << "POSSIBLE" << "\n"; if (ex) exit(0); } void impossible(bool ex = true) { cout << "IMPOSSIBLE" << "\n"; if (ex) exit(0); } constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; const int inf = INT_MAX / 2 - 100; int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } int botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } int popcount(signed t) { return __builtin_popcount(t); } int popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } ll mask(int i) { return (ll(1) << i) - 1; } bool inc(int first, int second, int c) { return first <= second && second <= c; } template <class t> void mkuni(vc<t>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } ll rand_int(ll l, ll r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t>& first) { for (int i = int(0); i < int(int(first.size())); i++) swap(first[i], first[rand_int(0, i)]); } template <class t> int lwb(const vc<t>& v, const t& first) { return lower_bound(v.begin(), v.end(), first) - v.begin(); } const int S = 2010; const int nmax = 16; using B = bitset<S>; B dp[1 << nmax]; signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int n, k; cin >> n >> k; vi first = readvi(n); dp[0][0] = 1; for (int bit = int(0); bit < int(1 << n); bit++) { for (int i = (S - 1) / k * k; i >= k; i -= k) if (dp[bit][i]) { dp[bit][i / k] = 1; } for (int i = int(0); i < int(n); i++) if (!(bit & 1 << i)) dp[bit | 1 << i] |= dp[bit] << first[i]; } if (dp[mask(n)][1]) { yes(0); vi u(n); int lv = 0, rem = mask(n), cur = 1; while (cur) { if (cur * k < S && dp[rem][cur * k]) { cur *= k; lv++; } else { bool f = false; for (int i = int(0); i < int(n); i++) if (rem & 1 << i) { if (cur >= first[i] && dp[rem ^ 1 << i][cur - first[i]]) { u[i] = lv; rem ^= 1 << i; cur -= first[i]; f = true; break; } } assert(f); } } for (int _ = int(0); _ < int(n - 1); _++) { int mx = -1; int x = -1, y = -1; for (int i = int(0); i < int(n); i++) { if (mx < u[i]) { mx = u[i]; x = -1; y = -1; } if (mx == u[i]) { if (x == -1) x = i; else y = i; } } assert(y != -1); cout << first[x] << " " << first[y] << endl; first[x] += first[y]; u[y] = -1; while (first[x] % k == 0) { u[x]--; first[x] /= k; } } for (int i = int(0); i < int(n); i++) { if (u[i] == 0) { assert(first[i] == 1); } else { assert(u[i] == -1); } } } else no(); }
11
#include <bits/stdc++.h> using namespace std; const int CN = 510; int read() { int s = 0, ne = 1; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') ne = -1; for (; c >= '0' && c <= '9'; c = getchar()) s = (s << 1) + (s << 3) + c - '0'; return s * ne; } int n, m; bitset<CN> f[61][2][CN], s, t; int main() { n = read(), m = read(); for (int i = 1; i <= m; i++) { int u = read(), v = read(), tp = read(); f[0][tp][u][v] = 1; } for (int e = 1; e <= 60; e++) for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) { if (f[e - 1][0][i][k]) f[e][0][i] |= f[e - 1][1][k]; if (f[e - 1][1][i][k]) f[e][1][i] |= f[e - 1][0][k]; } s[1] = 1; int p = 0; long long ans = 0; for (int k = 60; k + 1; k--) { t.reset(); for (int i = 1; i <= n; i++) if (s[i]) t |= f[k][p][i]; if (t.any()) { ans += 1ll << k, s = t, p ^= 1; } } if (ans > (long long)1e18) puts("-1"); else printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); long long inf = 1e18, MOD = 1e9 + 7; long long n, m, k, ar[1000005], br[1000005]; string s, s1; vector<int> gen(vector<int> p) { vector<int> ret(n); for (int i = 0; i < n; i++) { ret[p[i]] = i; } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int ti = 1; while (ti--) { cin >> n; vector<int> xor_b0(n), xor_p0(n); for (int i = 0; i < n; i++) { cout << "? " << 0 << " " << i << endl; cin >> xor_p0[i]; cout << "? " << i << " " << 0 << endl; cin >> xor_b0[i]; } vector<int> ans; int cnt = 0; for (int b0 = 0; b0 < n; b0++) { vector<int> p(n); for (int i = 0; i < n; i++) { p[i] = xor_b0[i] ^ b0; } vector<int> comp(n), comp2(n); comp2 = p; sort(comp2.begin(), comp2.end()); iota(comp.begin(), comp.end(), 0); if (comp != comp2) continue; vector<int> b = gen(p); if (b0 != b[0]) continue; bool ok = true; for (int i = 0; i < n; i++) { if (xor_p0[i] != (p[0] ^ b[i])) ok = false; } if (!ok) continue; cnt++; ans = p; } cout << "!" << endl; cout << cnt << endl; for (auto i : ans) cout << i << " "; cout << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first >> p.second; return in; } template <class T> ostream &operator<<(ostream &out, pair<T, T> &p) { out << p.first << ' ' << p.second; return out; } const long long MOD = 1e9 + 7; int N; long long A[1000005]; int main() { ios_base::sync_with_stdio(0); cin >> N; long long sum = 0, ans = 0; for (int i = 1; i <= N; i++) { cin >> A[i]; A[i]--; if (i > 1) { sum = sum * min({A[i - 2], A[i - 1], A[i]}) + min(A[i - 1], A[i]); sum %= MOD; } ans += A[i] + sum * min(A[i - 1], A[i]); ans %= MOD; } cout << "\n"; cout << ans % MOD << "\n"; }
7
#include <bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; long long int power(long long int a, long long int b) { long long int res = 1; a = a % MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; b--; } a = (a * a) % MOD; b >>= 1; } return res; } long long int fermat_inv(long long int y) { return power(y, MOD - 2); } long long int gcd(long long int a, long long int b) { return (b == 0) ? a : gcd(b, a % b); } long long int min(long long int a, long long int b) { return (a > b) ? b : a; } long long int max(long long int a, long long int b) { return (a > b) ? a : b; } bool prime[1000001]; vector<int> primes; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p < 1000001; p++) if (prime[p]) primes.push_back(p); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int c[32] = {0}; for (int i = 0; i < 32; i++) { for (int j = 0; j < n; j++) { c[i] += 1 & (a[j] >> i); } } int i = 31; while (i >= 0 && !(c[i] % 2)) i--; if (i < 0) cout << "DRAW\n"; else if (c[i] % 4 == 3 && (n - c[i]) % 2 == 0) cout << "LOSE\n"; else cout << "WIN\n"; } }
5
#include <bits/stdc++.h> using namespace std; string s; int d[300004], k, i; int main() { cin >> s >> i; while (i--) { cin >> k; d[--k]++, d[s.size() - k]--; } for (k = i = 0; i < s.size(); i++) { k += d[i]; cout << s[(k % 2 ? s.size() - 1 - i : i)]; } return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> void print(vector<T> a) { int n = a.size(); for (long long i = 0; i < n; ++i) { cout << a[i] << (i == n - 1 ? "\n" : " "); } } int sum_vector(vector<int> v) { return accumulate(v.begin(), v.end(), 0); } void sort_vector(vector<int> &v) { sort(v.begin(), v.end()); } void sort_comp(vector<int> &v, bool func(int, int)) { sort(v.begin(), v.end(), func); } bool comp(int a, int b) { return a < b; } struct CustomCompare { bool operator()(pair<int, int> lhs, pair<int, int> rhs) { return lhs.first > rhs.first; } }; long long gcd(long long a, long long b) { a = abs(a); b = abs(b); while (a) { long long temp = a; a = b % a; b = temp; } return abs(b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } string binary(long long num) { string ans = ""; do { ans = to_string(num % 2) + ans; num /= 2; } while (num); return ans; } const int mxn = 5e5 + 7; const int d = 18; const int mill = 1e6 + 3; const long long mod = 998244353; long long pwr(long long num, long long p) { long long res = 1; while (p > 0) { if (p & 1) res = (res * num) % mod; num = (num * num) % mod; p /= 2; } return res; } long long inverse(long long num) { return pwr(num, mod - 2); } long long get_rand(long long n) { return ((rand() << 15) + rand()) % n; } void solve() { int n, q; const int step = 400; cin >> n >> q; vector<int> p(n + 1); vector<int> wtf(n + 1); vector<int> pws(n + 1); for (int i = 1; i <= n; i++) { cin >> p[i]; wtf[p[i]] = i; } int temp; for (int i = 1; i <= n; i++) { temp = i; for (int s = 0; s < step; s++) { temp = p[temp]; } pws[i] = temp; } int type; int x, y; int idx, k; vector<int> helpx(step + 1); vector<int> helpy(step + 1); for (int qq = 0; qq < q; qq++) { cin >> type; if (type == 1) { cin >> x >> y; temp = p[x]; p[x] = p[y]; p[y] = temp; wtf[p[x]] = x; wtf[p[y]] = y; helpx[0] = x; helpy[0] = y; for (int s = 1; s <= step; s++) { helpx[s] = p[helpx[s - 1]]; helpy[s] = p[helpy[s - 1]]; } pws[x] = helpx[step]; pws[y] = helpy[step]; for (int s = 1; s <= step; s++) { x = wtf[x]; y = wtf[y]; pws[x] = helpx[step - s]; pws[y] = helpy[step - s]; } } else { cin >> idx >> k; while (k >= step) { idx = pws[idx]; k -= step; } for (int s = 0; s < k; s++) { idx = p[idx]; } cout << idx << "\n"; } } } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t = 1; for (int ii = 1; ii <= t; ii++) { solve(); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double PI = acos(-1.0); const long long int mod = 1e9 + 7; const int MAXN = 1e6 + 5; struct node { int t, candies, prevcolour, preveaten, pos; node() {} node(int t, int candies, int prevcolour, int preveaten, int pos) : t(t), candies(candies), prevcolour(prevcolour), pos(pos), preveaten(preveaten) {} const bool operator<(const node &nd) const { if (t == nd.t) return candies < nd.candies; else return t > nd.t; } }; bool vis[1300][55][1300]; void cp() { int n, s, k; string colour; cin >> n >> s >> k; vector<int> arr(n); for (int &x : arr) cin >> x; cin >> colour; vector<vector<int>> adj(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[j] > arr[i] && colour[j] != colour[i]) adj[i].push_back(j); } } long long int ans = -1; priority_queue<node> pq; for (int i = 0; i < n; i++) { int d = abs(i - (s - 1)); pq.push(node(d, arr[i], colour[i], arr[i], i)); } while (!pq.empty()) { node u = pq.top(); pq.pop(); vis[u.candies][u.pos][u.t] = 1; if (u.candies >= k) { ans = u.t; break; } for (int i : adj[u.pos]) { node v = u; v.candies += arr[i]; v.preveaten = arr[i]; v.prevcolour = colour[i]; v.t += abs(i - u.pos); v.pos = i; if (v.candies < 1300 && !vis[v.candies][v.pos][v.t]) pq.push(v), vis[v.candies][v.pos][v.t] = 1; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; while (t--) { cp(); } return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks") #pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx") using namespace std; long long read() { char ch = getchar(); long long f = 1, x = 0; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } const long long maxn = 1e5 + 10; long long n, ex[maxn << 1], a[maxn], p; map<long long, long long> t; vector<long long> ans; long long mod(long long x, long long p) { return (x + p) % p; } signed main() { n = read(); p = read(); for (long long i = 1; i <= n; i++) { a[i] = read(); } sort(a + 1, a + n + 1); a[n + 1] = a[n] + 1; for (long long i = a[n] - n + 1; i <= a[n]; i++) { long long q = upper_bound(a + 1, a + n + 1, i) - a - 1; ex[mod(i - q, p)]++; } long long k = max(0LL, a[n] - n + 1); for (long long x = k; x <= a[n]; x++) { if (!ex[x % p]) { ans.push_back(x); } ex[mod(x - (upper_bound(a + 1, a + n + 1, x) - a - 1), p)]--; ex[mod(x + n - (upper_bound(a + 1, a + n + 1, x + n) - a - 1), p)]++; } cout << ans.size() << endl; long long sz = ans.size(); for (long long i = 0; i < sz; i++) { cout << ans[i] << " "; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int inf; const double eps = 1e-8; const double pi = acos(-1.0); template <class T> int chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> int chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <class T> T sqr(T a) { return a * a; } template <class T> T mmin(T a, T b) { return a < b ? a : b; } template <class T> T mmax(T a, T b) { return a > b ? a : b; } template <class T> T aabs(T a) { return a < 0 ? -a : a; } template <class T> int dcmp(T a, T b) { return a > b; } template <int *a> int cmp_a(int first, int second) { return a[first] < a[second]; } struct __INIT__ { __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } } __INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char first) { *oS++ = first; if (oS == oT) flush(); } inline bool read(signed &first) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; else if (c == EOF) return 0; for (first = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) first = first * 10 + (c & 15); first *= f; return 1; } inline bool read(long long &first) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; else if (c == EOF) return 0; for (first = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) first = first * 10 + (c & 15); first *= f; return 1; } inline bool read(char &first) { first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); return first != EOF; } inline bool read(char *first) { while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == '\n' || *first == ' ' || *first == '\r') if (*first == EOF) return 0; while (!(*first == '\n' || *first == ' ' || *first == '\r')) *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); *first = 0; return 1; } template <typename A, typename... B> inline bool read(A &first, B &...second) { return read(first) && read(second...); } inline bool write(signed first) { if (!first) putc('0'); if (first < 0) putc('-'), first = -first; while (first) qu[++qr] = first % 10 + '0', first /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(long long first) { if (!first) putc('0'); if (first < 0) putc('-'), first = -first; while (first) qu[++qr] = first % 10 + '0', first /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char first) { putc(first); return 0; } inline bool write(const char *first) { while (*first) { putc(*first); ++first; } return 0; } inline bool write(char *first) { while (*first) { putc(*first); ++first; } return 0; } template <typename A, typename... B> inline bool write(A first, B... second) { return write(first) || write(second...); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; int n, m; vector<int> ans, to[1 << 17 | 5]; int ok, is; int siz[1 << 17 | 5]; int cnt[1 << 17 | 5]; bool isbinary(int dp, int first, int f) { if (!dp) return 0; if (dp == 1) return to[first].size() == 1; if (to[first].size() != 3) return 0; for (auto i : to[first]) if (i != f) if (!isbinary(dp - 1, i, first)) return 0; return 1; } void getsize(int first, int f) { cnt[first] = 1; for (auto i : to[first]) if (i != f) { getsize(i, first); cnt[first] += cnt[i]; } } void dfs(int dp, int first, int f, bool ntrt = 0) { if (!ok) return; if (dp == 1) { if (to[first].size() != ntrt) { ok = 0; } return; } if (to[first].size() - ntrt > 3) { ok = 0; return; } if (to[first].size() - ntrt == 3) { if (~is) { ok = 0; return; } int mx = 0, xs = 0, at = 0; for (auto i : to[first]) if (i != f) { if (chkmax(mx, cnt[i])) at = i; xs ^= cnt[i]; } if (!ntrt) { if (xs) { ok = 0; return; } for (auto i : to[first]) if (i != f) if (!isbinary(dp - 2, i, first)) { ok = 0; return; } is = first; return; } else { if (mx != xs) { ok = 0; return; } for (auto i : to[first]) if (i != f) { if (!isbinary(dp - 2 + (i == at), i, first)) { ok = 0; return; } } is = first; return; } } if (to[first].size() - ntrt == 1) { if (dp == 2) { for (auto i : to[first]) if (i != f) { if (cnt[i] != 1) { ok = 0; return; } } is = first; return; } else { ok = 0; return; } } for (auto i : to[first]) if (i != f) dfs(dp - 1, i, first, 1); } void check(int rt, int mxs) { getsize(rt, 0); if (!isbinary(n - 1, mxs, rt)) return; ok = 1; is = -1; dfs(n, rt, mxs); if (!ok || !~is) return; ans.push_back(is); } void dfs(int first, int f) { siz[first] = 1; for (auto i : to[first]) if (i != f) { dfs(i, first); if (siz[i] << 1 == m) { check(first, i); check(i, first); } siz[first] += siz[i]; } } signed main() { read(n); if (n == 2) return write("2\n1 2\n"); m = (1 << n) - 2; for (int i = 1; i < m; ++i) { int u, v; read(u, v); to[u].push_back(v); to[v].push_back(u); } dfs(1, 0); sort(ans.begin(), ans.end()); write((int)ans.size(), '\n'); for (auto i : ans) write(i, ' '); write('\n'); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long a[100000], b[100000], n, t, r; void y() { partial_sum(a, a + n, b); r |= binary_search(b, b + n, t); for (int i = 0; i < n && b[i] - a[i] < t; i++) r |= binary_search(b, b + n, t + a[i]); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i], t += a[i]; if (t % 2 == 0) t /= 2, y(), reverse(a, a + n), y(); cout << (r ? "YES" : "NO"); }
5
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline int ty() { int a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 300007; int n, val[_] = {0}, fe[_] = {0}, fo[_] = {0}, f[_] = {0}; int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(); for (int i = 1; i <= n; i++) val[i] = ty(); for (int i = 1; i < n; i++) fe[min(i, n - i)] = max(fe[min(i, n - i)], max(val[i], val[i + 1])); for (int i = 2; i < n; i++) fo[min(i - 1, n - i)] = max(fo[min(i - 1, n - i)], max(min(val[i - 1], val[i]), min(val[i], val[i + 1]))); for (int i = 1; i <= n; i++) f[1] = max(f[1], val[i]); for (int i = n / 2; i >= 1; i--) f[i + i] = max(f[i + i + 2], fe[i]), f[i + i + 1] = max(f[i + i + 3], fo[i]); for (int i = 0; i < n; i++) cout << f[n - i] << " \n"[i == n - 1]; return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int a[N], maxf[1 << 21], maxs[1 << 21], bit[25], n; inline void update(int u, int d) { if (maxf[u] < d) { maxs[u] = maxf[u]; maxf[u] = d; } else if (maxs[u] < d) maxs[u] = d; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 2; i <= n; i++) { if (maxf[a[i]]) { maxs[a[i]] = maxf[a[i]]; maxf[a[i]] = i; } maxf[a[i]] = i; } bit[0] = 1; for (int i = 1; i <= 21; i++) bit[i] = bit[i - 1] * 2; for (int i = 0; i < 21; i++) { for (int s = 0; s < bit[21]; s++) { if (s & bit[i]) { update(s ^ bit[i], maxf[s]); update(s ^ bit[i], maxs[s]); } } } int ans = 0; for (int i = 1; i <= n - 2; i++) { int u = 0; for (int k = 20; k >= 0; k--) { if (a[i] & bit[k]) continue; if (maxs[u | bit[k]] > i) u |= bit[k]; } ans = max(ans, a[i] | u); } printf("%d\n", ans); }
9
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } long long n, w, b, x, c[1005], cost[1005], f[1005][10005], C, a[10005], tot; long long cmp(long long x, long long y) { return x > y; } signed main() { n = read(); w = read(); b = read(); x = read(); for (long long i = 1; i <= n; i++) c[i] = read(), C += c[i]; for (long long i = 1; i <= n; i++) cost[i] = read(); memset(f, -0x3f, sizeof(f)); f[0][0] = w; for (long long i = 1; i <= n; i++) { if (i != 1) for (long long j = 0; j <= C; j++) f[i][j] = min(f[i - 1][j] + x, w + b * j); else for (long long j = 0; j <= C; j++) f[i][j] = f[i - 1][j]; tot = 0; for (long long k = 1; k <= c[i]; k *= 2) { a[++tot] = k; c[i] -= k; } if (c[i]) a[++tot] = c[i]; sort(a + 1, a + tot + 1, cmp); for (long long k = 1; k <= tot; k++) { for (long long j = C; j >= a[k]; j--) { if (min(f[i][j - a[k]], w + b * (j - a[k])) >= cost[i] * a[k]) f[i][j] = max(f[i][j], min(f[i][j - a[k]], w + b * (j - a[k])) - cost[i] * a[k]); } } } long long ans = 0; for (long long i = C; i >= 0; i--) { if (f[n][i] >= 0) { ans = i; break; } } cout << ans << endl; return 0; ; }
7
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000001 int n,k; vector<vector<int>> E; vector<int> a; vector<vector<int>> ans; void dfs0(int cur,int p){ ans[cur][0] = a[cur]; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; dfs0(to,cur); auto t = ans[to]; t.insert(t.begin(),t.back()); t.pop_back(); rep(j,2*k){ ans[cur][j] ^= t[j]; } } } void dfs1(int cur,int p,vector<int> X){ X.insert(X.begin(),X.back()); X.pop_back(); rep(i,2*k){ ans[cur][i] ^= X[i]; } X[0] ^= a[cur]; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; vector<int> t = ans[to]; t.insert(t.begin(),t.back()); t.pop_back(); rep(j,2*k)X[j] ^= t[j]; } rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; vector<int> t = ans[to]; t.insert(t.begin(),t.back()); t.pop_back(); rep(j,2*k)X[j] ^= t[j]; dfs1(to,cur,X); rep(j,2*k)X[j] ^= t[j]; } } int main(){ cin>>n>>k; E.resize(n); rep(i,n-1){ int u,v; scanf("%d %d",&u,&v); u--;v--; E[u].push_back(v); E[v].push_back(u); } a.resize(n); rep(i,n){ scanf("%d",&a[i]); } ans.resize(n,vector<int>(2*k,0)); dfs0(0,-1); vector<int> X(2*k,0); dfs1(0,-1,X); rep(i,n){ if(i!=0)printf(" "); int temp = 0; for(int j=k;j<=2*k-1;j++)temp ^= ans[i][j]; if(temp==0)printf("0"); else printf("1"); } cout<<endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long INFL = 9223372036854775807LL; const double EPSILON = 0.00000001; const long long MOD = 1000000007; int vals[25][3]; map<pair<int, int>, pair<int, string>> poss_sums; void solve1(string asgn, int i, int a, int b, int c, int n) { if (i == n / 2) { pair<int, int> key = {b - a, c - b}; if (poss_sums.find(key) == poss_sums.end() or poss_sums[key].first < a) { poss_sums[key] = {a, asgn}; } } else { solve1(asgn + "LM\n", i + 1, a + vals[i][0], b + vals[i][1], c, n); solve1(asgn + "MW\n", i + 1, a, b + vals[i][1], c + vals[i][2], n); solve1(asgn + "LW\n", i + 1, a + vals[i][0], b, c + vals[i][2], n); } } string ans = ""; int best = -INF; void solve2(string asgn, int i, int a, int b, int c, int n) { if (i == n) { pair<int, int> target = {a - b, b - c}; if (poss_sums.find(target) != poss_sums.end()) { int val = a + poss_sums[target].first; if (val > best) { best = val; ans = poss_sums[target].second + asgn; } } } else { solve2(asgn + "LM\n", i + 1, a + vals[i][0], b + vals[i][1], c, n); solve2(asgn + "MW\n", i + 1, a, b + vals[i][1], c + vals[i][2], n); solve2(asgn + "LW\n", i + 1, a + vals[i][0], b, c + vals[i][2], n); } } int32_t main() { int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) scanf("%d", &vals[i][j]); ; } solve1("", 0, 0, 0, 0, n); solve2("", n / 2, 0, 0, 0, n); if (ans.length() == 0) { cout << "Impossible" << endl; } else cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; long long binpow(long long a, long long b) { a %= 1000000007; long long res = 1; while (b > 0) { if (b & 1) res = res * a % 1000000007; a = a * a % 1000000007; b >>= 1; } return res; } vector<vector<long long>> mult(vector<vector<long long>> a, vector<vector<long long>> b) { vector<vector<long long>> c; for (long long i = 0; i < a.size(); i++) { vector<long long> v; for (long long j = 0; j < a[i].size(); j++) { long long x = 0; for (long long k = 0; k < b[j].size(); k++) { x += (a[i][k] * b[k][j]) % 1000000007; x %= 1000000007; } v.push_back(x); } c.push_back(v); } return c; } bool isPrime(long long n) { if (n <= 1) return false; for (long long i = 2; i < n; i++) if (n % i == 0) return false; return true; } vector<long long> get_Prime(long long n) { vector<long long> primes; for (long long i = 2; i <= n; i++) { if (isPrime(i)) primes.push_back(i); } return primes; } int32_t main() { long long t; ios_base::sync_with_stdio(false); cin.tie(NULL); t = 1; while (t--) { long long n; cin >> n; vector<long long> primes = get_Prime(n); vector<long long> ans; for (long long i = 0; i < primes.size(); i++) { long long x = primes[i]; while (1) { if (x > n) { break; } ans.push_back(x); x *= primes[i]; } } cout << ans.size() << '\n'; for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct BITree { int t[N]; int Lowbit(int x) { return x & -x; } void Add(int x) { for (; x < N; x += Lowbit(x)) t[x]++; } int Query(int x) { int ret = 0; for (; x; x -= Lowbit(x)) ret += t[x]; return ret; } }; BITree bt; int BSearch(int L, int R, int x) { while (L < R) { int M = (L + R) / 2; if (bt.Query(M) < x) L = M + 1; else R = M; } return L; } int b[N]; pair<int, int> a[N]; struct Qry { int len, pos, id, ans; }; Qry qry[N]; int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; a[i] = make_pair(b[i], i); } sort(a + 1, a + n + 1, [](pair<int, int> &x, pair<int, int> &y) { if (x.first == y.first) return x.second < y.second; else return x.first > y.first; }); int q; cin >> q; for (int i = 1; i <= q; i++) { cin >> qry[i].len >> qry[i].pos; qry[i].id = i; } sort(qry + 1, qry + q + 1, [](Qry &x, Qry &y) { return x.len < y.len; }); int now = 1; for (int i = 1; i <= n; i++) { bt.Add(a[i].second); while (now <= q && qry[now].len == i) { qry[now].ans = b[BSearch(1, n + 1, qry[now].pos)]; now++; } } sort(qry + 1, qry + q + 1, [](Qry &x, Qry &y) { return x.id < y.id; }); for (int i = 1; i <= q; i++) cout << qry[i].ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); int t; cin>>t; while(t--) { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; int h[101]; for(int i=0;i<=100;i++) h[i]=0; for(int i=0;i<n;i++) { h[a[i]]++; } vector<int> v; for(int i=0;i<=100;i++) { if(h[i]>=1) { v.push_back(i); h[i]--; } } for(int i=0;i<=100;i++) { while(h[i]>0) { v.push_back(i); h[i]--; } } for(int i=0;i<v.size();i++) cout<<v[i]<<" "; cout<<endl; } }
0
#include <bits/stdc++.h> using namespace std; template <typename T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } template <typename T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); } template <typename T> inline void umin(T &a, T b) { a = a < b ? a : b; } template <typename T> inline void umax(T &a, T b) { a = a > b ? a : b; } template <typename T, typename W> inline void Int(T &x, W &y) { Int(x), Int(y); } template <typename T, typename W, typename Q> inline void Int(T &x, W &y, Q &z) { Int(x, y), Int(z); } const int N = 1e5 + 7; const int inf = 1e9 + 7; vector<pair<long long, int>> Sort(vector<pair<long long, int>> bucket_1, vector<pair<long long, int>> bucket_2) { vector<pair<long long, int>> sorted_bucket; int i = 0, j = 0; while (i < bucket_1.size() || j < bucket_2.size()) { if (bucket_1[i].first == -1) { ++i; continue; } if (i == bucket_1.size()) { sorted_bucket.push_back(bucket_2[j++]); } else if (j == bucket_2.size()) { sorted_bucket.push_back(bucket_1[i++]); } else { if (bucket_1[i].first == bucket_2[j].first) { if (bucket_1[i].second < bucket_2[j].second) { sorted_bucket.push_back(bucket_1[i++]); } else { sorted_bucket.push_back(bucket_2[j++]); } } else if (bucket_1[i].first < bucket_2[j].first) { sorted_bucket.push_back(bucket_1[i++]); } else { sorted_bucket.push_back(bucket_2[j++]); } } } return sorted_bucket; } void update_block(vector<pair<long long, int>> &bucket, int l, int r, long long x, long long propagation_value) { for (int i = 0; propagation_value > 0 && i < (int)bucket.size(); ++i) { bucket[i].first += propagation_value; } vector<pair<long long, int>> new_bucket; for (int i = 0; i < (int)bucket.size(); ++i) { if (l <= bucket[i].second && bucket[i].second <= r) { bucket[i].first += x; new_bucket.push_back(bucket[i]); bucket[i].first = -1; } } bucket = Sort(bucket, new_bucket); } int solve() { int n, q; Int(n, q); vector<long long> a(n); for (long long &x : a) { Int(x); } int block = 1500; vector<vector<pair<long long, int>>> bucket(n / block + 3); for (int i = 0; i < n; ++i) { bucket[i / block].push_back({a[i], i}); } for (int b = 0; b < bucket.size(); ++b) { sort(bucket[b].begin(), bucket[b].end()); } vector<long long> propagation(n / block + 3); while (q--) { int t; Int(t); if (t == 1) { int l, r; Int(l, r); long long x; Int(x); --l, --r; int st = l / block; int en = r / block; update_block(bucket[st], l, r, x, propagation[st]); propagation[st] = 0; if (st == en) { continue; } for (int b = st + 1; b < en; ++b) { propagation[b] += x; } update_block(bucket[en], l, r, x, propagation[en]); propagation[en] = 0; } else { long long y; Int(y); int left_index = -1, right_bucket = -1; for (int b = 0; b < (int)bucket.size(); ++b) { if (bucket[b].empty() || propagation[b] >= y) { continue; } pair<long long, int> val = make_pair(y - propagation[b], -1); auto ptr = lower_bound(bucket[b].begin(), bucket[b].end(), val); if (ptr != bucket[b].end() && ptr->first == y - propagation[b]) { if (left_index == -1) { left_index = ptr->second; } right_bucket = b; } } if (left_index == -1) { printf("-1\n"); continue; } int right_index = left_index; for (int i = 0; i < (int)bucket[right_bucket].size(); ++i) { if (bucket[right_bucket][i].first == y - propagation[right_bucket]) { right_index = bucket[right_bucket][i].second; } } printf("%d\n", right_index - left_index); } } return 0; } int main() { int tests = 1, CaseNo = 0; while (tests--) { solve(); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, k; int bin(int l, int r) { if (l == r) return l; int m = (l + r) / 2; cout << "1 " << m << " " << m + 1 << endl; string s; cin >> s; if (s == "TAK") return bin(l, m); else return bin(m + 1, r); } int main() { srand(time(NULL)); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; int x = bin(1, n); int y, z; if (x != 1) y = bin(1, x - 1); if (x != n) z = bin(x + 1, n); if (x == 1) cout << "2 " << x << " " << z << endl; else if (x == n) { cout << "2 " << x << " " << y << endl; } else { cout << "1 " << y << " " << z << endl; string s; cin >> s; if (s == "TAK") cout << "2 " << x << " " << y << endl; else cout << "2 " << x << " " << z << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MX = 10002; bitset<MX> tree[MX * 4], ans, tt; int n, QN, st, en, V; vector<int> v[MX * 4]; void update(int x, int a, int b) { if (st > b || en < a) return; if (a >= st && b <= en) { v[x].push_back(V); return; } update(x * 2, a, (a + b) / 2); update(x * 2 + 1, (a + b) / 2 + 1, b); } void dfs(int x, int a, int b) { if (a == b) { for (auto pp : v[x]) tree[x] |= (tree[x] << pp); return; } dfs(x * 2, a, (a + b) / 2); dfs(x * 2 + 1, (a + b) / 2 + 1, b); tree[x] = tree[x * 2] | tree[x * 2 + 1]; for (auto pp : v[x]) { tree[x] |= (tree[x] << pp); } } int main() { for (int j = 0; j < MX * 4; j++) { tree[j].reset(); tree[j][0] = 1; } cin >> n >> QN; while (QN--) { scanf("%d %d %d", &st, &en, &V); if (V > n) continue; update(1, 1, n); } dfs(1, 1, n); vector<int> sol; for (int j = 1; j <= n; j++) if (tree[1][j]) sol.push_back(j); cout << sol.size() << endl; for (auto pp : sol) cout << pp << ' '; }
7
#include <bits/stdc++.h> using namespace std; long long T; int nowx, nowy, a[10005], s; int ansx_1[10005], ansy_1[10005], ansx_2[10005], ansy_2[10005], K; int S[10][10] = {{1, 1, 1, 1}, {1, 0, 1, 1}, {1, 0, 1, 0}, {0, 1, 1, 1}, {0, 0, 1, 1}, {0, 0, 1, 0}}; int dx[10][2] = {{1, 1}, {2, 2}, {-1, 0}, {-1, 0}}; int dy[10][2] = {{-1, 0}, {-1, 0}, {1, 1}, {2, 2}}; void add(int X1, int Y1, int X2, int Y2) { if (X1 && Y1 && X2 && Y2 && X1 <= 50 && X2 <= 50 && Y1 <= 50 && Y2 <= 50) ansx_1[++K] = X1, ansy_1[K] = Y1, ansx_2[K] = X2, ansy_2[K] = Y2; return; } int main() { scanf("%lld", &T); for (register long long i = T; i; i /= 6) a[++s] = i % 6; if (a[s] == 1) add(3, 3, 4, 3), add(3, 4, 4, 4); if (a[s] == 2) add(3, 3, 4, 3), add(3, 3, 3, 4); if (a[s] == 3) add(2, 4, 3, 4), add(3, 4, 4, 4); if (a[s] == 4) add(3, 3, 3, 4); if (a[s] == 5) add(2, 4, 3, 4); add(1, 2, 2, 2), add(1, 3, 2, 3), add(1, 4, 2, 4), add(2, 4, 2, 5), add(3, 4, 3, 5); add(3, 1, 3, 2), add(4, 1, 4, 2), add(4, 2, 5, 2), add(4, 3, 5, 3); nowx = nowy = 4; for (register int i = s - 1; i; i--, nowx += 2, nowy += 2) { add(nowx - 3, nowy + 1, nowx - 3, nowy + 2), add(nowx - 2, nowy + 1, nowx - 2, nowy + 2); add(nowx + 1, nowy - 4, nowx + 1, nowy - 3), add(nowx + 2, nowy - 2, nowx + 2, nowy - 1); add(nowx + 2, nowy, nowx + 3, nowy), add(nowx + 2, nowy + 1, nowx + 3, nowy + 1); add(nowx, nowy + 2, nowx, nowy + 3), add(nowx + 1, nowy + 2, nowx + 1, nowy + 3); for (register int j = 0; j < 4; j++) if (S[a[i]][j]) add(nowx + dx[j][0], nowy + dy[j][0], nowx + dx[j][1], nowy + dy[j][1]); } add(nowx, nowy, nowx + 1, nowy); add(nowx - 1, nowy + 1, nowx, nowy + 1), add(nowx + 1, nowy, nowx + 1, nowy + 1); printf("%d %d\n%d\n", min(50, nowx + 1), min(50, nowy + 1), K); for (register int i = 1; i <= K; i++) printf("%d %d %d %d\n", ansx_1[i], ansy_1[i], ansx_2[i], ansy_2[i]); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; void solve() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<int> sum; for (int i = 0, j = 2 * (n - k) - 1; i < j; i++, j--) { sum.push_back(v[i] + v[j]); } sort((sum).begin(), (sum).end()); if (sum.size() == 0) cout << v[n - 1]; else { cout << max(sum[sum.size() - 1], v[v.size() - 1]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); }
3
#include <bits/stdc++.h> using namespace std; const int N = 55; char str[N][N]; int f[N][N][N][N], n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", str[i] + 1); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { f[i][j][i][j] = (str[i][j] == '#'); } } for (int lenx = 1; lenx <= n; lenx++) { for (int leny = 1; leny <= n; leny++) { for (int x1 = 1; x1 + lenx - 1 <= n; x1++) { for (int y1 = 1; y1 + leny - 1 <= n; y1++) { int x2 = x1 + lenx - 1, y2 = y1 + leny - 1; if (lenx == 1 && leny == 1) { continue; } f[x1][y1][x2][y2] = max(abs(x1 - x2), abs(y1 - y2)) + 1; for (int k = x1; k < x2; k++) { f[x1][y1][x2][y2] = min(f[x1][y1][x2][y2], f[x1][y1][k][y2] + f[k + 1][y1][x2][y2]); } for (int k = y1; k < y2; k++) { f[x1][y1][x2][y2] = min(f[x1][y1][x2][y2], f[x1][y1][x2][k] + f[x1][k + 1][x2][y2]); } } } } } printf("%d\n", f[1][1][n][n]); return 0; }
7
#include <bits/stdc++.h> using namespace std; int hsy[10005]; struct node { int id, x, y; } a[200005]; struct node1 { int a, r; } c[10005]; bool cmp(node a, node b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d%d", &c[i].a, &c[i].r); int m; cin >> m; for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i].x, &a[i].y); a[i].id = i; } sort(a + 1, a + 1 + m, cmp); int num = 0; for (int i = 1; i <= n; i++) { int l = 0, r = m + 1, mid, f = 0; while (l + 1 < r) { int mid = (l + r) / 2; if (a[mid].x < c[i].a - c[i].r) l = mid; else r = mid; } if (l - 10 <= 0) l = 11; hsy[i] = 0x3f3f3f3f; for (int j = l - 10; c[i].a + c[i].r >= a[j].x && j <= m; j++) { if ((a[j].x - c[i].a) * (a[j].x - c[i].a) + a[j].y * a[j].y <= c[i].r * c[i].r) { hsy[i] = min(hsy[i], a[j].id); f = 1; } } num += f; if (!f) hsy[i] = -1; } cout << num << endl; for (int i = 1; i <= n; i++) cout << hsy[i] << " "; return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long int n, k; long long int arr[100]; long long int csum[100]; bool way(long long int mask, long long int targ) { long long int dp[n + 5][k + 10]; memset(dp, false, sizeof dp); dp[0][0] = true; for (long long int i = 1; i <= n; i++) { for (long long int j = 0; j < i; j++) { if (((csum[i] - csum[j]) & mask & targ) == targ) { for (long long int p = 0; p <= k; p++) { if (dp[j][p]) { dp[i][p + 1] = true; } } } } } return dp[n][k]; } int main() { cin >> n >> k; csum[0] = 0; for (long long int i = 1; i <= n; i++) { cin >> arr[i]; csum[i] = csum[i - 1] + arr[i]; } long long int ans = 0ll; for (long long int i = 55; i >= 0; i--) { if (way(~((1ll << i) - 1), ans | (1ll << i))) { ans |= (1ll << i); } } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7; int n, deg[N], vis[N], cnt[N], k; vector<int> e[N]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { deg[i] = 0; e[i].clear(); } for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); e[u].push_back(v); e[v].push_back(u); deg[u]++; deg[v]++; } queue<int> q; for (int i = 1; i <= n; i++) { vis[i] = cnt[i] = 0; if (deg[i] == 1) q.push(i); } int ans = 0; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 1; for (auto v : e[u]) { if (vis[v]) continue; deg[v]--; cnt[v]++; if (cnt[v] % k == 0) { ans++; if (deg[v] == 1) q.push(v); } } } printf("%d\n", ans); } return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-Ofast") using namespace std; const int MAX = 1e7; const int MOD = 1e9 + 7; const int MAAX = 1e18; priority_queue<int> pq, er; map<int, int> mp; int arr[100010]; int main() { int tc = 1; while (tc--) { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int x; cin >> x; arr[i] = x; if (i >= m - 1) { mp[x]++; if (mp[x] == 1) { pq.push(x); } if (mp[x] == 2) { er.push(x); } if (i > m - 1) { mp[arr[i - m]]--; if (mp[arr[i - m]] == 1) { pq.push(arr[i - m]); } if (mp[arr[i - m]] == 0) { er.push(arr[i - m]); } } while (pq.size() && er.size() && pq.top() == er.top()) { pq.pop(); er.pop(); } if (pq.size()) { cout << pq.top() << "\n"; } else { cout << "Nothing\n"; } } else { mp[x]++; if (mp[x] == 1) { pq.push(x); } if (mp[x] == 2) { er.push(x); } } } } return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<long long, vector<long long>> func(vector<long long> v) { if (v.size() == 1) { return {v[0], v}; } long long mini = 1e10; long long ind = -1; for (long long i = 0; i < v.size(); i++) { if (v[i] < mini) { mini = v[i]; ind = i; } } if (ind == 0) { vector<long long> yy(v.begin() + ind + 1, v.end()); pair<long long, vector<long long>> y = func(yy); vector<long long> temp = {v[ind]}; for (auto c : y.second) { temp.push_back(c); } return {v[ind] + y.first, temp}; } else if (ind == v.size() - 1) { vector<long long> xx(v.begin(), v.begin() + ind); pair<long long, vector<long long>> x = func(xx); vector<long long> temp = x.second; temp.push_back(v[ind]); return {v[ind] + x.first, temp}; } vector<long long> xx(v.begin(), v.begin() + ind); vector<long long> yy(v.begin() + ind + 1, v.end()); pair<long long, vector<long long>> x = func(xx); pair<long long, vector<long long>> y = func(yy); if (x.first + (v.size() - xx.size()) * mini > y.first + (v.size() - yy.size()) * mini) { vector<long long> temp = x.second; for (long long i = 0; i < (v.size() - xx.size()); i++) { temp.push_back(mini); } return {x.first + (v.size() - xx.size()) * mini, temp}; } else { vector<long long> temp; for (long long i = 0; i < (v.size() - yy.size()); i++) { temp.push_back(mini); } for (auto c : y.second) { temp.push_back(c); } return {y.first + (v.size() - yy.size()) * mini, temp}; } } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; vector<long long> ans = func(v).second; for (auto c : ans) { cout << c << " "; } cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; char str[1000010][10]; long long option[1000010]; const long long cn = 1e9; int main() { priority_queue<int, vector<int>, greater<int> > pq; int n, i, temp, j = 0; char str1[10]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", str1); if (str1[0] == 'i') { scanf("%d", &temp); pq.push(temp); strcpy(str[j], "insert "), option[j++] = temp; } else if (str1[0] == 'g') { scanf("%d", &temp); if (pq.empty()) { pq.push(temp); strcpy(str[j], "insert "), option[j++] = temp; strcpy(str[j], "getMin "), option[j++] = temp; } else if (pq.top() == temp) { strcpy(str[j], "getMin "), option[j++] = temp; } else { if (pq.top() > temp) { pq.push(temp); strcpy(str[j], "insert "), option[j++] = temp; } else { while (!pq.empty()) { if (pq.top() < temp) { pq.pop(); strcpy(str[j], "removeMin"), option[j++] = 0; } else if (pq.top() == temp) { break; } else { pq.push(temp); strcpy(str[j], "insert "), option[j++] = temp; break; } } if (pq.empty()) { pq.push(temp); strcpy(str[j], "insert "), option[j++] = temp; } } strcpy(str[j], "getMin "), option[j++] = temp; } } else { if (pq.empty()) { pq.push(cn); strcpy(str[j], "insert "), option[j++] = cn; pq.pop(); strcpy(str[j], "removeMin"), option[j++] = 0; } else { pq.pop(); strcpy(str[j], "removeMin"), option[j++] = 0; } } } printf("%d\n", j); for (i = 0; i < j; i++) { printf("%s", str[i]); if (str[i][0] != 'r') printf("%lld", option[i]); printf("\n"); } return 0; }
4
#include <bits/stdc++.h> int main() { int numsum, a, b, i; scanf("%d", &numsum); int vector[numsum]; for (i = 0; i < numsum; i++) { scanf("%d", &a); scanf("%d", &b); vector[i] = (a + b); } for (i = 0; i < numsum; i++) printf("%d\n", vector[i]); return 0; }
0
#include <bits/stdc++.h> using namespace std; map<long long, int> ma, Ma; char str[3]; int n, i, a[100], b[100], c[100], Ans, A1, A2, ans[100]; void dfs(long long x, long long y, long long z, int d, int k) { if (d > n / 2) { long long tmp = (y - x) * 300007 + (z - x); if ((ma.find(tmp) == ma.end()) || (x > ma[tmp])) { ma[tmp] = x; Ma[tmp] = k; } return; } dfs(x + a[d], y + b[d], z, d + 1, k * 3); dfs(x, y + b[d], z + c[d], d + 1, k * 3 + 1); dfs(x + a[d], y, z + c[d], d + 1, k * 3 + 2); } void Dfs(long long x, long long y, long long z, int d, int k) { if (d > n) { long long tmp = (x - y) * 300007 + (x - z); if (ma.find(tmp) != ma.end()) { if (ma[tmp] + x > Ans) { Ans = ma[tmp] + x; A1 = Ma[tmp]; A2 = k; } } return; } Dfs(x + a[d], y + b[d], z, d + 1, k * 3); Dfs(x, y + b[d], z + c[d], d + 1, k * 3 + 1); Dfs(x + a[d], y, z + c[d], d + 1, k * 3 + 2); } int main() { scanf("%d", &n); Ans = -0x37373737; int q = 0; for (i = 1; i <= n; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); } dfs(0, 0, 0, 1, 0); Dfs(0, 0, 0, n / 2 + 1, 0); if (Ans == -0x37373737) printf("Impossible"); else { for (i = n; i > n / 2; i--) { ans[i] = A2 % 3; A2 = A2 / 3; } for (i = n / 2; i >= 1; i--) { ans[i] = A1 % 3; A1 = A1 / 3; } for (i = 1; i <= n; i++) if (ans[i] == 0) printf("LM\n"); else if (ans[i] == 1) printf("MW\n"); else if (ans[i] == 2) printf("LW\n"); } }
7
#include <bits/stdc++.h> const int kMaxN = 1 + 100; int N; std::vector<std::vector<std::pair<int, int> > > graph; void ReadInput() { scanf("%d", &N); graph.resize(N + 1); for (int i = 1; i < N; i++) { int u, v; scanf("%d%d", &u, &v); graph[u].push_back({v, i}); graph[v].push_back({u, i}); } } void DFS(int node, int father, double time) { if (time >= 2) { time -= 2; } int cnt = 0; for (auto edge : graph[node]) { if (edge.first != father) { cnt++; double pointTime = time + (double)cnt * 2 / (double)graph[node].size(); while (pointTime >= 2) { pointTime -= 2; } if (pointTime <= 1.0) { printf("1 %d %d %d %.10f\n", edge.second, edge.first, node, pointTime); } else { printf("1 %d %d %d %.10f\n", edge.second, node, edge.first, pointTime - 1.0); } DFS(edge.first, node, pointTime + 1); } } } int main() { ReadInput(); printf("%d\n", N - 1); DFS(1, 0, 0.0); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<pair<int, int> > v(n); for (int i = 0; i < n; ++i) cin >> v[i].first >> v[i].second; double l = 0, r = 2e9; for (int it = 0; it < 80; ++it) { double mid = (l + r) / 2; double cur = mid; for (int i = 0; i < n; ++i) { double remove = (m + cur) / v[i].first; cur -= remove; remove = (m + cur) / v[(i + 1) % n].second; cur -= remove; } if (cur < 0) l = mid; else r = mid; } cout.precision(10); if (abs(l - 2e9) <= 1e-9) cout << -1 << endl; else cout << l << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> void mxi(T& a, const T& b) { a = max(a, b); } template <class T> void mni(T& a, const T& b) { a = min(a, b); } long double EPS = 1e-9; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); const int p70[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67}; int bexp(int a, int n) { if (n == 0) return 1; int k = bexp(a, n >> 1); k = (1LL * k * k) % 1000000007; if (n & 1) k = (1LL * a * k) % 1000000007; return k; } int rnk, basis[21]; void InsertVector(int e) { for (int i = 0; i < 21; i++) { if (e & (1 << i)) { if (!basis[i]) { basis[i] = e; rnk++; return; } e ^= basis[i]; } } } void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; int candidate_vector = 0; for (int j = 0; j < 19; j++) { int cnt = 0; while ((x % p70[j]) == 0) { x /= p70[j]; ++cnt; } if (cnt & 1) candidate_vector |= (1 << j); } InsertVector(candidate_vector); } cout << (bexp(2, n - rnk) - 1 + 1000000007) % 1000000007 << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, tab; t = 1; tab = t; while (t--) { solve(); } cerr << ((double)clock() / CLOCKS_PER_SEC); }
6
#include <bits/stdc++.h> int main() { int a, b, i; char ss[110]; int xx[110], yy[110], sx, sy; scanf("%d%d", &a, &b); scanf("%s", ss); xx[0] = 0; yy[0] = 0; if (a == 0 && b == 0) { printf("Yes\n"); return 0; } for (i = 0; i < strlen(ss); i++) { if (ss[i] == 'U') { xx[i + 1] = xx[i]; yy[i + 1] = yy[i] + 1; } if (ss[i] == 'D') { xx[i + 1] = xx[i]; yy[i + 1] = yy[i] - 1; } if (ss[i] == 'L') { xx[i + 1] = xx[i] - 1; yy[i + 1] = yy[i]; } if (ss[i] == 'R') { xx[i + 1] = xx[i] + 1; yy[i + 1] = yy[i]; } } sx = xx[strlen(ss)] - xx[0]; sy = yy[strlen(ss)] - yy[0]; for (i = 0; i < strlen(ss); i++) { if (sx == 0 && sy == 0) { if (xx[i] == a && yy[i] == b) { printf("Yes\n"); return 0; } else continue; } if (sx == 0) { if ((a == xx[i]) && ((b - yy[i]) % sy == 0) && ((b - yy[i]) / sy >= 0)) { printf("Yes\n"); return 0; } else continue; } if (sy == 0) { if ((b == yy[i]) && ((a - xx[i]) % sx == 0) && ((a - xx[i]) / sx >= 0)) { printf("Yes\n"); return 0; } else continue; } if (((a - xx[i]) % sx == 0) && ((b - yy[i]) % sy == 0) && ((a - xx[i]) / sx == (b - yy[i]) / sy) && ((a - xx[i]) / sx >= 0)) { printf("Yes\n"); return 0; } } printf("No\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; void input(vector<long long int> &arr) { for (int i = 0; i < arr.size(); i++) cin >> arr[i]; } void output(vector<long long int> arr) { for (int i = 0; i < arr.size(); i++) cout << arr[i] << " "; cout << "\n"; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int power(long long int a, long long int b) { if (b == 0) return 1; if (b == 1) return a; long long int smallans = power(a, b / 2); long long int myans = (smallans * smallans) % 1000000007; if ((b & 1)) myans = (myans * a) % 1000000007; return myans; } long long int multiply(long long int a, long long int b) { long long int ans = ((a % 1000000007) * (b % 1000000007)) % 1000000007; return ans; } long long int add(long long int a, long long int b) { long long int ans = ((a % 1000000007) + (b % 1000000007)) % 1000000007; return ans; } long long int subtract(long long int a, long long int b) { long long int ans = ((a % 1000000007) - (b % 1000000007)) % 1000000007; if (ans < 0) ans += 1000000007; return ans; } long long int divide(long long int a, long long int b) { return multiply(a, power(b, 1000000007 - 2)); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; void update(vector<long long int> &tree, long long int index, long long int val) { index++; while (index < tree.size()) { tree[index] = (tree[index] + val) % 1000000007; index += index & (-index); } } long long int calculate(vector<long long int> &tree, long long int index) { long long int sum = 0; index++; while (index > 0) { sum = (sum + tree[index]) % 1000000007; index -= index & (-index); } return sum; } long long int findpar(long long int p, vector<long long int> &parent) { if (parent[p] == p) return p; parent[p] = findpar(parent[p], parent); return parent[p]; } void merge(long long int a, long long int b, vector<long long int> &parent, vector<long long int> &size_) { a = findpar(a, parent); b = findpar(b, parent); if (a != b) { if (size_[a] < size_[b]) swap(a, b); parent[b] = a; size_[a] += size_[b]; } } void solve() { long long int n; cin >> n; string s; cin >> s; long long int zero = 0; long long int one = 0; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1] && s[i - 1] == '1') one++; if (s[i] == s[i - 1] && s[i - 1] == '0') zero++; } cout << max(zero, one) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { string tb, tmp; int testok = 0; cin >> tb; for (int i = 0; i < 5; ++i) { cin >> tmp; if (tmp[0] == tb[0] || tmp[1] == tb[1]) testok = 1; } if (testok) cout << "YES"; else cout << "NO"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = numeric_limits<int>::max(); const long long int linf = numeric_limits<long long int>::max(); const long long int MOD = (long long int)1e9 + 7; long long int fast_mod(long long int abcd, int expp) { long long int res = 1; while (expp > 0) { if (expp % 2 == 1) res = (res * abcd) % MOD; abcd = (abcd * abcd) % MOD; expp /= 2; } return res % MOD; } long long int gcd(long long int a, long long int b) { long long int temppp = max(a, b); b = min(a, b); a = temppp; if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); clock_t begin123 = clock(); map<int, int> m; int n, temp; cin >> n; for (int i = 0; i < (n); ++i) { cin >> temp; m[temp]++; } if (m.size() > 3) { cout << "-1"; } else if (m.size() == 2) { auto it = m.begin(); auto it1 = it; it1++; int teemp = it1->first - it->first; if (teemp & 1) cout << teemp << "\n"; else cout << teemp / 2 << "\n"; } else if (m.size() == 1) { cout << "0" << "\n"; } else if (m.size() == 3) { auto it = m.begin(); auto it1 = it; it1++; auto it2 = it1; it2++; if (it2->first - it1->first == it1->first - it->first) cout << it1->first - it->first << "\n"; else cout << "-1" << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((a[i][j] + i + j) % 2 == 0) a[i][j]++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << a[i][j] << " "; } cout << "\n"; } } }
6
#include <bits/stdc++.h> using namespace std; bool used[3][10]; bool check(int n) { vector<int> d; while (n > 0) { d.push_back(n % 10); n /= 10; } if (d.size() == 1) { for (int i = 0; i < 3; ++i) if (used[i][d[0]]) return true; return false; } if (d.size() == 2) { for (int i1 = 0; i1 < 3; ++i1) for (int i2 = 0; i2 < 3; ++i2) { if (i1 != i2) { if (used[i1][d[0]] && used[i2][d[1]]) return true; } } return false; } if (d.size() == 2) { for (int i1 = 0; i1 < 3; ++i1) for (int i2 = 0; i2 < 3; ++i2) for (int i3 = 0; i3 < 3; ++i3) { if (i1 != i2 && i1 != i3 && i2 != i3) { if (used[i1][d[0]] && used[i2][d[1]] && used[i3][d[2]]) return true; } } return false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < 6; ++j) { int cur; cin >> cur; if (!used[i][cur]) { used[i][cur] = 1; } } } int ans = 0; for (int i = 1;; ++i) { if (!check(i)) { break; } ans = i; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> T mabs(const T &a) { return a < 0 ? -a : a; } struct point { long long x, y; point(long long x0 = 0, long long y0 = 0) { x = x0; y = y0; } point operator*(const long long a) const { return point(a * x, a * y); } point operator+(const point &a) const { return point(x + a.x, y + a.y); } point operator-(const point &a) const { return point(x - a.x, y - a.y); } long long operator^(const point &a) const { return x * a.y - y * a.x; } double norm() const { return sqrt(x * x + y * y); } }; int sign(long long a) { if (a == 0) return 0; return a < 0 ? -1 : 1; } int checkProj(long long a, long long b, long long c, long long d) { long long sta = min(a, b), enda = max(a, b); long long stc = min(c, d), endc = max(c, d); return sign(min(enda, endc) - max(sta, stc)); } bool intersect(const point &a, const point &b, const point &c, const point &d) { long long pr1 = sign((b - a) ^ (c - a)); long long pr2 = sign((b - a) ^ (d - a)); long long pr3 = sign((d - c) ^ (b - c)); long long pr4 = sign((d - c) ^ (a - c)); if (pr1 == 0 && pr2 == 0 && pr3 == 0 && pr4 == 0) { int s1 = checkProj(a.x, b.x, c.x, d.x); int s2 = checkProj(a.y, b.y, c.y, d.y); return s1 >= 0 && s2 >= 0 && (s1 != 0 || s2 != 0); } long long s1 = pr1 * pr2; long long s2 = pr3 * pr4; return s1 <= 0 && s2 <= 0 && (s1 != 0 || s2 != 0); } const int mod = 1e9 + 7; int D[202][202]; bool Can[201][201] = {0}; int dyn(int a, int b) { if (D[a][b] != -1) return D[a][b]; int &res = D[a][b]; if (b - a + 1 <= 3) return res = 1; res = 0; if (Can[a + 1][b]) { res = (res + dyn(a + 1, b)) % mod; } for (int mid = (a + 2), emid = (b); mid < emid; mid++) { if (Can[a][mid] && Can[mid][b]) { long long p1 = dyn(a, mid); long long p2 = dyn(mid, b); res = (res + p1 * p2) % mod; } } return res; } const double PI = acos(-1.0); bool pointInside(point *P, int n, const point &a) { double sum = 0; const double eps = 1e-8; for (int i = (0), ei = (n); i < ei; i++) { point da = P[i] * 2 - a; point db = P[i + 1] * 2 - a; double cs = (da.x * db.x + da.y * db.y) / (da.norm() * db.norm()); if (cs < -1) cs = -1; if (cs > 1) cs = 1; double v = acos(cs); if (sign(da ^ db) < 0) sum += v; else sum -= v; } sum = fabs(sum); return fabs(sum - 2 * PI) < eps; } void run() { memset(D, -1, sizeof(D)); point P[202]; int n; cin >> n; for (int i = (0), ei = (n); i < ei; i++) { int x, y; scanf("%d%d", &x, &y); P[i] = point(x, y); } P[n] = P[0]; for (int i = (0), ei = (n); i < ei; i++) for (int j = (i + 1), ej = (n); j < ej; j++) { bool ok = true; for (int k = (0), ek = (n); k < ek; k++) if (intersect(P[i], P[j], P[k], P[k + 1])) ok = false; if (ok) { if (!pointInside(P, n, P[i] + P[j])) ok = false; } if (ok) { Can[i][j] = Can[j][i] = 1; } } for (int i = (0), ei = (n); i < ei; i++) Can[i][(i + 1) % n] = Can[(i + 1) % n][i] = 1; int res = dyn(0, n - 1); cout << res << endl; } int main() { run(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int SET(int n, int pos) { return n = n | (1 << pos); } int RESET(int n, int pos) { return n = n & ~(1 << pos); } int CHECK(int n, int pos) { return (bool)(n & (1 << pos)); } int str2int(string s) { stringstream ss(s); int x; ss >> x; return x; } string int2str(int a) { stringstream ss; ss << a; string str = ss.str(); return str; } string char2str(char a) { stringstream ss; ss << a; string str = ss.str(); return str; } long long int bigMod(long long int n, long long int power, long long int MOD) { if (power == 0) return 1; if (power % 2 == 0) { long long int ret = bigMod(n, power / 2, MOD); return ((ret % MOD) * (ret % MOD)) % MOD; } else return ((n % MOD) * (bigMod(n, power - 1, MOD) % MOD)) % MOD; } long long int modInverse(long long int n, long long int MOD) { return bigMod(n, MOD - 2, MOD); } int POW(int x, int y) { int res = 1; for (; y;) { if ((y & 1)) { res *= x; } x *= x; y >>= 1; } return res; } int inverse(int x) { double p = ((double)1.0) / x; return (p) + 1e-9; } int gcd(int a, int b) { while (b) b ^= a ^= b ^= a %= b; return a; } int nC2(int n) { return n * (n - 1) / 2; } long long int MOD(long long int n, long long int mod) { if (n >= 0) return n % mod; else if (-n == mod) return 0; else return mod + (n % mod); } int n, data[105], Mask[65], dp[105][(1 << 18) + 1], path[105][(1 << 18) + 1]; bool isPrime[65]; vector<int> prime; void seive_N_logN(int N) { memset(isPrime, true, sizeof isPrime); prime.clear(); isPrime[1] = false; for (int i = 4; i <= N; i = i + 2) isPrime[i] = false; for (int i = 3; i * i <= N; i = i + 2) { if (isPrime[i]) { for (int j = i * i; j <= N; j += i) isPrime[j] = false; } } for (int i = 1; i < N; i++) if (isPrime[i]) prime.push_back(i); for (int i = 1; i <= 60; i++) { for (int j = 0; j < prime.size(); j++) { if (i % prime[j] == 0) Mask[i] = SET(Mask[i], j); } } } int call(int i, int mask) { if (i >= n) { return 0; } int &ret = dp[i][mask]; if (ret != -1) return ret; ret = 1000000000; for (int j = 1; j <= 60; j++) { if (mask & Mask[j]) continue; int xx = call(i + 1, mask | Mask[j]) + abs(data[i] - j); if (xx < ret) { ret = xx; path[i][mask] = j; } } return ret; } int main() { seive_N_logN(60); int t, cas = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &data[i]); } memset(dp, -1, sizeof(dp)); call(0, 0); int mask = 0; for (int i = 0; i < n; i++) { int ans = path[i][mask]; printf("%d ", ans); mask |= Mask[ans]; } printf("\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long pw(long long x, long long y) { if (y == 0) return 1; if (y == 1) return x; long long v = pw(x, y / 2); if (y & 1) { return v * v % 1000000007 * x % 1000000007; } return v * v % 1000000007; } long long f(int p, long long n) { long long cnt = 0; while (n >= p) { cnt += n / p; n /= p; } return pw(p, cnt); } int main() { int x; long long n; cin >> x >> n; long long an = 1; for (int i = 2; i * i <= x; i++) { if (x % i == 0) { while (x % i == 0) { x /= i; } an = an * f(i, n) % 1000000007; } } if (x != 1) an = an * f(x, n) % 1000000007; cout << an << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; const int DIRX[] = {-1, 0, 0, 1, -1, -1, 1, 1}, DIRY[] = {0, -1, 1, 0, -1, 1, -1, 1}; const double ERR = 1e-9, PI = (2 * acos(0.0)); 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 ((b < a) ? a : b); } template <class T> inline void checkMIN(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkMAX(T& a, T b) { if (a < b) a = b; } template <class T> inline T sqr(T x) { return (x * x); } template <class T> inline T gcd(T a, T b) { if (!b) return a; else return gcd(b, a % b); } template <class T> void BubbleSort(vector<T>& VT) { for (int i = (int)VT.size(); i > 0; i--) for (int j = 1; j < i; j++) if (VT[j] < VT[j - 1]) swap(VT[j], VT[j - 1]); } template <class T> T fastPow(T Base, T Power) { T Result = (T)1; while (Power > 0) { if (Power & (T)1) Result *= Base; Power >>= 1; Base *= Base; } return Result; } template <class T> T fastPow(T Base, T Power, T Mod) { T Result = (T)1; while (Power > 0) { if (Power & (T)1) Result = (Result * Base) % Mod; Power >>= 1; Base = (Base * Base) % Mod; } return Result; } int compareDouble(double x, double y) { double d = x - y; if (d > ERR) return 1; if (d < -ERR) return -1; return 0; } int compareDouble(double x, double y, double err) { double d = x - y; if (d > err) return 1; if (d < -err) return -1; return 0; } int mat[102][102]; bool valid(int x, int y) { if (x == 1 && (2 <= y && y <= 3)) return false; if (x == 2 && y == 2) return false; return true; } int main() { int i, j, k, r, c; cin >> r >> c; if (valid(r, c) && valid(c, r)) { k = 0; for (i = 0; i < r; i++) for (j = (i + 1) % 2; j < c; j += 2) mat[i][j] = ++k; for (i = 0; i < r; i++) for (j = i % 2; j < c; j += 2) mat[i][j] = ++k; for (i = 0; i < r; cout << endl, i++) for (j = 0; j < c; cout << ' ', j++) cout << mat[i][j]; } else cout << -1 << endl; return 0; }
5
#include <bits/stdc++.h> long long int add(long long int a, long long int b) { return a + b - (a + b >= 1000000007) * 1000000007; } long long int sub(long long int a, long long int b) { return a - b + (a - b < 0) * 1000000007; } long long int mult(long long int a, long long int b) { return (1LL * a * b) % 1000000007; } inline long long int gcd(long long int a, long long int b) { long long int t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } using namespace std; vector<long long int> v1[1001]; bool visited[1001]; void DFS(long long int node, long long int par) { if (visited[node] == true) { return; } visited[node] = true; long long int sz = v1[node].size(); for (long long int i = 0; i < sz; i++) { if (v1[node][i] == par) { continue; } else { DFS(v1[node][i], node); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); memset(visited, 0, sizeof(visited)); long long int n, m; cin >> n >> m; if (m != n - 1) { cout << "no"; return 0; } for (long long int j = 0; j < m; j++) { long long int a, b; cin >> a >> b; a--; b--; v1[a].push_back(b); v1[b].push_back(a); } DFS(0, -1); for (long long int i = 0; i < n; i++) { if (visited[i] == false) { cout << "no"; return 0; } } cout << "yes"; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long pre[105][105]; void preCalc() { for (int i = 1; i <= 100; i++) { long long temp = i; for (int j = 2; j <= temp; j++) { long long x = 0; while (temp % j == 0) { x++; temp /= j; } pre[i][j] = x; } } } int main() { preCalc(); long long a, b, c; cin >> a >> b >> c; long long ans = 0; for (int i = 1; i <= a; i++) { for (int j = 1; j <= b; j++) { for (int k = 1; k <= c; k++) { long long here = 1; for (int l = 2; l <= 100; l++) { long long temp = pre[i][l] + pre[j][l] + pre[k][l]; here = (here * (temp + 1)) & ((1LL << 30) - 1); } ans = (ans + here) & ((1LL << 30) - 1); } } } cout << ans << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { int r, h[2], mi, ma; } a[100005]; int n, m, s[100005], w, ww, root, r; long long sum[100005], cnt[1000005]; int ref(int z) { int mi = 0, mid, ma = ww - 1; while (mi <= ma) { mid = (mi + ma) >> 1; if (s[mid] == z) return mid; if (z < s[mid]) ma = mid - 1; else mi = mid + 1; } } void dfs(int z, int x, int y) { a[z].r = ref(a[z].r); if (a[z].h[0] == -1) { a[z].mi = a[z].ma = s[a[z].r]; } else { if (a[a[z].h[0]].r > a[a[z].h[1]].r) swap(a[z].h[0], a[z].h[1]); dfs(a[z].h[0], x, a[z].r); dfs(a[z].h[1], a[z].r, y); a[z].mi = a[a[z].h[0]].mi; a[z].ma = a[a[z].h[1]].ma; sum[x] += a[a[z].h[1]].mi; cnt[x]++; sum[a[z].r] -= a[a[z].h[1]].mi; cnt[a[z].r]--; sum[a[z].r] += a[a[z].h[0]].ma; cnt[a[z].r]++; sum[y] -= a[a[z].h[0]].ma; cnt[y]--; } } int sea(int z) { int mi = 0, mid, ma = ww - 1; while (mi < ma) { mid = (mi + ma + 1) >> 1; if (s[mid] < z) mi = mid; else ma = mid - 1; } return mi; } int main() { scanf("%d", &n); w = 0; s[w++] = 0; s[w++] = 1000000001; for (int i = 1; i <= n; i++) a[i].h[0] = a[i].h[1] = -1; for (int i = 1; i <= n; i++) { scanf("%d%d", &m, &a[i].r); if (m == -1) { root = i; } else { if (a[m].h[0] == -1) a[m].h[0] = i; else a[m].h[1] = i; } s[w++] = a[i].r; } sort(s, s + w); ww = 1; for (int i = 1; i < w; i++) { if (s[i] != s[i - 1]) { s[ww++] = s[i]; } } memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); dfs(root, 0, ww - 1); for (int i = 1; i < ww; i++) { sum[i] += sum[i - 1]; cnt[i] += cnt[i - 1]; } scanf("%d", &m); while (m--) { scanf("%d", &r); r = sea(r); printf("%.10f\n", sum[r] / (double)cnt[r]); } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<pair<pair<int, int>, int> > edges; int u[100007]; int v[100007]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { pair<pair<int, int>, int> t; cin >> t.first.first; cin >> t.first.second; t.first.second ^= 1; t.second = i; edges.push_back(t); } sort(edges.begin(), edges.end()); if (edges[0].first.second == 1) { cout << "-1" << endl; return 0; } u[edges[0].second] = 1; v[edges[0].second] = 2; int formed = 2; int p = 2; int q = 3; for (int i = 1; i < m; i++) { if (edges[i].first.second == 1) { if (q > formed) { cout << "-1\n"; return 0; } else { u[edges[i].second] = p; v[edges[i].second] = q; p++; if (p == q) { p = 2; q++; } } } else { u[edges[i].second] = 1; formed++; v[edges[i].second] = formed; } } for (int i = 0; i < m; i++) cout << u[i] << " " << v[i] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n; long long int c; vector<long long int> v; vector<long long int> vv; vector<long long int> have; deque<pair<long long int, int> > d; long long int im[200002]; long long int gt(int idx) { if (idx < 0) { return 0; } return im[idx]; } priority_queue<pair<long long int, int> > q; int main() { cin >> n; cin >> c; long long int ans = 0; for (int i = 0; i < 2 * n - 1; i++) { int a; scanf("%d", &a); if (i % 2 == 0) { v.push_back(a); } else { vv.push_back(a); } } have.assign(v.size(), 0); have[0] = min(c, v[0]); d.push_back(make_pair(0, 0)); q.push(make_pair(0, 0)); im[0] = have[0]; long long int sum = have[0]; for (int i = 1; i < v.size(); i++) { while (d.size() && sum - d.front().first >= c) { d.pop_front(); } sum += vv[i - 1]; d.push_back(make_pair(sum, i)); q.push(make_pair(-sum + gt(i - 1), i)); sum += v[i]; im[i] = im[i - 1]; long long int mx = 0; while (d.size() && sum - d.front().first >= c) { mx = max(mx, c - (im[i - 1] - gt(d.front().second - 1))); d.pop_front(); } while (!q.empty()) { if (d.size() == 0 || q.top().second < d.front().second) { q.pop(); continue; } long long int mm = q.top().first + sum - im[i - 1]; mx = max(mx, mm); break; } mx = min(mx, (long long int)c); have[i] = mx; im[i] += have[i]; } long long int sum2 = 0; for (int i = 0; i < n; i++) { sum2 += have[i]; } printf("%lld\n", sum2); return 0; }
10
#include <bits/stdc++.h> using namespace std; string st, z; long s, q; long find(char a) { q = 0; while (z[q] != a) ++q; return q + 8; } int main() { ios_base::sync_with_stdio(0); z = "><+-.,[]"; cin >> st; for (int i = 0; i < st.size(); ++i) s = s * 16 + find(st[i]), s %= 1000003; cout << s << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = (int)2e9 + 10; const int mod = (int)1e9 + 7; const int N = (int)1e6 + 10; const long long LLINF = (long long)8e18 + 10; const double pi = acos(-1.0); template <typename T1, typename T2> bool umin(T1 &x, const T2 &y) { if (x > y) return x = y, true; return false; } template <typename T1, typename T2> bool umax(T1 &x, const T2 &y) { if (x < y) return x = y, true; return false; } template <typename T> T getint() { char c = getchar(); T p = 1, x = 0; while (c == ' ' || c == '\n') c = getchar(); if (c == '-') p = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * p; } int add[40]; int d[300][300]; long long ans[300][300]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; char a[300][300]; int n, m, q, p; void bfs(int x, int y, int val) { add[0] = val; for (int i = 1; i < 27; ++i) { add[i] = add[i - 1] / 2; } for (int i = 1; i <= n; ++i) { int del = 31 - abs(x - i); for (int j = max(1, y - del); j <= min(y + del, m); ++j) { d[i][j] = INF; } } d[x][y] = 0; queue<int> qx, qy; qx.push(x); qy.push(y); while (qx.size()) { int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); if (d[x][y] > 26) continue; for (int i = 0; i < 4; ++i) { if (x + dx[i] < 1 || x + dx[i] > n) continue; if (y + dy[i] < 1 || y + dy[i] > m) continue; if (a[x + dx[i]][y + dy[i]] == '*') continue; if (umin(d[x + dx[i]][y + dy[i]], d[x][y] + 1)) qx.push(x + dx[i]), qy.push(y + dy[i]); } } for (int i = 1; i <= n; ++i) { int del = 31 - abs(x - i); for (int j = max(1, y - del); j <= min(y + del, m); ++j) { if (d[i][j] < 27) ans[i][j] += add[d[i][j]]; } } } int main() { srand(time(NULL)); n = getint<int>(); m = getint<int>(); q = getint<int>(); p = getint<int>(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = getchar(); } getchar(); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] == '.' || a[i][j] == '*') continue; bfs(i, j, (a[i][j] - 'A' + 1) * q); } } int res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (ans[i][j] > p) ++res; } } printf("%d\n", res); }
5
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INFMEM = 63; const int INF = 1061109567; const long long LINF = 4557430888798830399LL; const double DINF = numeric_limits<double>::infinity(); const long long MOD = 1000000007; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; const double PI = 3.141592653589793; inline void open(string a) { freopen((a + ".in").c_str(), "r", stdin); freopen((a + ".out").c_str(), "w", stdout); } inline void fasterios() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } inline void fastll(long long &input_number) { input_number = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '9') { input_number = (input_number << 3) + (input_number << 1) + ch - '0'; ch = getchar_unlocked(); } input_number *= sign; } long long n; long long fact[25]; long long comb(long long a, long long b) { if (a <= 0 || b <= 0 || a < b) return 1; return (fact[a] / fact[a - b]) / fact[b]; } long long cnt[25]; int main() { long long t; fact[0] = 1; for (int i = 1; i <= 20; i++) fact[i] = i * fact[i - 1]; fastll(t); while (t--) { fastll(n); long long all = 0; memset(cnt, 0, sizeof(cnt)); for (int i = 2; i <= INF; i++) { if (n == 0) break; cnt[n % i]++; n /= i; all++; } n = all; long long ans = 1; long long used = 0; bool flag = 1; for (int i = n; i >= 0; i--) { if (i != 0) used++; ans *= comb(used, cnt[i]); used -= cnt[i]; if (i != 0 && used == 0) flag = 0; } if (flag) { long long ansb = 1; cnt[0]--; used = 0; for (int i = n - 1; i >= 0; i--) { if (i != 0) used++; ansb *= comb(used, cnt[i]); used -= cnt[i]; } ans -= ansb; } cout << ans - 1 << '\n'; } return 0; }
6
#include <bits/stdc++.h> using namespace std; using longs = long long; using uint = unsigned; template <class T> inline T nexT() { T x = 0; int f = 0, ch = getchar(); while (!isdigit(ch)) ch == '-' && (f = !f), ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } namespace In { template <class T> inline void read(T &x) { x = 0; int ch = getchar(), f = 1; while (!isdigit(ch)) ch == '-' && (f = !f), ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x = f ? -x : x; } template <class T, class... Ts> inline void read(T &x, Ts &...y) { return read(x), read(y...); } } // namespace In const int N = 2060, mod = 1e9 + 7; struct snake { int x, y, b; } p[N]; bitset<N> g[N]; int pre[N][N]; namespace FastC { const longs mod = ::mod; const int N = ::N * 2; longs fact[N], inv[N]; longs fastPow(longs a, uint b) { longs ans = 1; while (b) { if (b & 1u) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1u; } return ans % mod; } void init(int p = N - 1) { fact[0] = inv[0] = 1; for (int i = 1; i <= p; i++) fact[i] = fact[i - 1] * i % mod; inv[p] = fastPow(fact[p], mod - 2); for (int i = p - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; } longs $C(longs n, longs m) { if (m > n || m < 0) return 0; return fact[n] * inv[m] % mod * inv[n - m] % mod; } } // namespace FastC int getW(int x1, int y1, int x2, int y2) { x1 = max(x1, 1), x2 = min(1000, x2); y1 = max(y1, 1), y2 = min(1000, y2); if (x1 > x2 || y1 > y2) return 0; --x1, --y1; return pre[x2][y2] + pre[x1][y1] - pre[x1][y2] - pre[x2][y1]; } int getSquare(int i, int r) { const auto x = p[i].x, y = p[i].y; return getW(x - r, y - r, x + r, y + r); } int getInter(int i, int j, int r) { int uu = max(p[i].x, p[j].x) - r, dd = min(p[i].x, p[j].x) + r, ll = max(p[i].y, p[j].y) - r, rr = min(p[i].y, p[j].y) + r; return getW(uu, ll, dd, rr); } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); using FastC::$C; int n = nexT<int>(), m = nexT<int>(), r = nexT<int>(); FastC::init(); for (int i = 1; i <= n; ++i) { p[i] = {nexT<int>(), nexT<int>(), nexT<int>()}; assert(!g[p[i].x][p[i].y]); g[p[i].x][p[i].y] = true; } for (int i = 1; i < N; ++i) for (int j = 1; j < N; ++j) pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + g[i][j]; longs ans = 0; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) { auto w = getInter(i, j, r), nn = n - w; longs cnt = $C(n, m) - $C(n - w, m); auto u = getSquare(i, r) - w, v = getSquare(j, r) - w; cnt += $C(nn, m) - $C(nn - u, m) - $C(nn - v, m) + $C(nn - v - u, m); while (cnt < 0) cnt += mod; cnt = cnt * (i == j ? 1 : 2) % mod; ans = (ans + cnt * p[i].b % mod * p[j].b % mod) % mod; } printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int d; cin >> d; string s; cin >> s; string ans1 = "", ans2 = ""; for (int j = 0; j < s.length(); j++) { if (s[j] == '2') { if (ans1 > ans2) { ans1 += '0'; ans2 += '2'; } else { ans1 += '1'; ans2 += '1'; } } if (s[j] == '1') { if (ans1 <= ans2) { ans1 += '1'; ans2 += '0'; } else { ans1 += '0'; ans2 += '1'; } } if (s[j] == '0') { ans1 += '0'; ans2 += '0'; } } cout << ans1 << endl << ans2 << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void solve() { long long n, l, r; cin >> n >> l >> r; if (n == 1) { cout << r - l + 1 << endl; return; } else if (n == 2) { cout << (r - l + 1) * (r - l) << endl; return; } if (n > 30) { cout << 0 << endl; return; } long long ans = 0; for (long long i = 1; i < 4000; i++) { for (long long j = 1; j < 4000; j++) { if (i != 1 && j != 1 && gcd(i, j) != 1) continue; if (i == j) continue; bool bad = false; long long p1 = 1, p2 = 1; for (long long k = 0; k < n - 1; k++) { p1 *= i; p2 *= j; if (p1 > (long long)(1e10) || p2 > (long long)(1e10)) { bad = true; break; } } if (bad) break; long long g1 = max(p1, p2), g2 = min(p1, p2); long long lower = (l + g2 - 1) / g2; long long upper = r / g1; if (lower > upper) continue; ans += upper - lower + 1; } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
8
#include <bits/stdc++.h> using namespace std; int n, l[1010], r[1010], k[1010], ans[1010], ansk = 0; bool ok[1010]; bool con(int a, int b) { return r[a] < r[b]; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; if (l[i] > r[i]) { k[i] = l[i]; l[i] = r[i]; r[i] = k[i]; } k[i] = i; } sort(k, k + n, con); for (int i = 0; i < n; i++) if (!ok[k[i]]) { ans[ansk] = r[k[i]]; ansk++; for (int j = 0; j < n; j++) if ((r[k[i]] >= l[j]) and (r[k[i]] <= r[j])) ok[j] = 1; } cout << ansk << endl; for (int i = 0; i < ansk; i++) cout << ans[i] << " "; }
5
#include <bits/stdc++.h> using namespace std; int n, q; int A[500006]; vector<pair<int, int> > que[500006]; int ans[500006]; int T[500006 << 2], lz[500006 << 2]; void pu(int rt) { T[rt] = min(T[rt << 1], T[rt << 1 | 1]); } void pd(int rt) { if (lz[rt]) { T[rt << 1] += lz[rt], T[rt << 1 | 1] += lz[rt]; lz[rt << 1] += lz[rt], lz[rt << 1 | 1] += lz[rt]; lz[rt] = 0; } } void mdfy(int rt, int l, int r, int L, int R) { if (L <= l && R >= r) { T[rt]--; lz[rt]--; return; } pd(rt); int m = l + r >> 1; if (L <= m) mdfy(rt << 1, l, m, L, R); if (R > m) mdfy(rt << 1 | 1, m + 1, r, L, R); pu(rt); } int wkr(int rt, int l, int r) { if (l == r) { T[rt] = 0x3f3f3f3f; return l; } pd(rt); int m = l + r >> 1, ret = 0; if (!T[rt << 1 | 1]) ret = wkr(rt << 1 | 1, m + 1, r); else ret = wkr(rt << 1, l, m); pu(rt); return ret; } void lsj(int rt, int l, int r, int p, int c) { if (l == r) { T[rt] = c; return; } pd(rt); int m = l + r >> 1; if (p <= m) lsj(rt << 1, l, m, p, c); else lsj(rt << 1 | 1, m + 1, r, p, c); pu(rt); } int S[500006]; void add(int x, int c) { while (x <= n) S[x] += c, x += (x & -x); } int sum(int x) { int ret = 0; while (x > 0) ret += S[x], x -= (x & -x); return ret; } void solve() { cin >> n >> q; for (int i = (1), iend = (n); i <= iend; ++i) scanf("%d", A + i); for (int i = (1), iend = (q); i <= iend; ++i) { static int l, r; scanf("%d%d", &l, &r); r = n - r; if (l + 1 > r) ans[i] = 0; else que[l + 1].emplace_back(make_pair(r, i)); } memset(T, 0x3f, sizeof T); for (int i = (n), iend = (1); i >= iend; --i) { if (A[i] <= i) lsj(1, 1, n, i, i - A[i]); while (!T[1]) { int x = wkr(1, 1, n); mdfy(1, 1, n, x, n); add(x, 1); } for (auto t : que[i]) ans[t.second] = sum(t.first) - sum(i - 1); } for (int i = (1), iend = (q); i <= iend; ++i) printf("%d\n", ans[i]); } signed main() { solve(); }
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; sort(a.begin(), a.end()); int g, r; cin >> g >> r; vector<vector<bool> > was(m, vector<bool>(g + 1, 0)); vector<vector<int> > wait(m, vector<int>(g + 1, 0)); was[0][0] = 1; deque<pair<int, int> > dq; dq.push_front(make_pair(0, 0)); int i; long long t, ans = -1, pot, lim, newt; while (!dq.empty()) { i = dq.front().first; t = dq.front().second; dq.pop_front(); if (i == m - 1) { pot = t + wait[i][t] * (g + r); if (ans == -1 || pot < ans) ans = pot; continue; } if (t == g) { if (was[i][0] == false) { wait[i][0] = wait[i][t] + 1; was[i][0] = true; dq.push_back(make_pair(i, 0)); } } if (i > 0) { newt = t + a[i] - a[i - 1]; if (g >= newt && was[i - 1][newt] == false) { wait[i - 1][newt] = wait[i][t]; was[i - 1][newt] = true; dq.push_front(make_pair(i - 1, newt)); } } if (i < m - 1) { newt = t + a[i + 1] - a[i]; if (g >= newt && was[i + 1][newt] == false) { wait[i + 1][newt] = wait[i][t]; was[i + 1][newt] = true; dq.push_front(make_pair(i + 1, newt)); } } } cout << ans; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5, K = 109, inf = 1e9; const long double pi = acos(-1); int n, k; int x[N], y[N]; bool solve(long double r) { vector<pair<long double, int> > ranges; int cnt = 0; for (int i = 1; i <= n; i++) { long double theta = fmod(atan2(y[i], x[i]) + 2 * pi, 2 * pi); if (1ll * x[i] * x[i] + 1ll * y[i] * y[i] > 2 * r * (x[i] * cos(theta) + y[i] * sin(theta))) continue; long double low = acos(sqrt(1ll * x[i] * x[i] + 1ll * y[i] * y[i]) / (2 * r)); long double L = theta - low, R = theta + low; R = fmod(R + 2 * pi, 2 * pi); if (L < 0) { L += 2 * pi; } if (L <= R) { ranges.push_back({L, 1}); ranges.push_back({R, 2}); } else { ranges.push_back({L, 1}); ranges.push_back({2 * pi, 2}); ranges.push_back({0, 1}); ranges.push_back({R, 2}); } } sort(ranges.begin(), ranges.end()); cnt = 0; for (auto i : ranges) { if (i.second == 1) cnt++; else cnt--; if (cnt >= k) return true; } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; long double low = 0, high = 2e6; for (int i = 0; i < 50; i++) { long double m = (low + high) / 2; if (solve(m)) high = m; else low = m; } if (low > 1.5e6) return cout << -1, 0; cout << fixed << low; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s1, s2, s3; map<char, long long int> mp1, mp2; cin >> s1 >> s2 >> s3; set<char> st; for (auto i = 0; i < s1.size(); i++) { mp1[s1[i]]++; st.insert(s1[i]); } for (auto i = 0; i < s2.size(); i++) { mp1[s2[i]]++; st.insert(s2[i]); } for (auto i = 0; i < s3.size(); i++) { mp2[s3[i]]++; } long long int blb = 0; for (auto i : st) { if (mp1[i] != mp2[i]) { blb = 1; break; } } if (s3.size() > s1.size() + s2.size()) blb = 1; if (blb) cout << "NO\n"; else cout << "YES\n"; return 0; }
0
#include <bits/stdc++.h> int main() { long long l3, l4, l5; std::cin >> l3 >> l4 >> l5; long double pi = 3.141592653589793238462643383279; long double v3 = 1.0 / 3.0 * l3 * l3 * sqrtl(3.0) / 4.0 * l3 * sqrtl(2.0 / 3); long double v4 = 1.0 / 3.0 * l4 * l4 * l4 * sqrtl(2) / 2; long double v5 = 1.0 / 3.0 * l5 * l5 * sqrtl(25 + 10 * sqrtl(5)) / 4 * l5 * sqrtl(4 * sinl(pi / 5) * sinl(pi / 5) - 1) / (2 * sinl(pi / 5)); std::cout.precision(10); return std::cout << std::fixed << v3 + v4 + v5, 0; }
4
#include <bits/stdc++.h> using namespace std; bool sortbyFrst(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } bool sortbyScnd(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } bool perfectDec(const pair<int, int> &a, const pair<int, int> &b) { return ((a.first != b.first) ? a.first > b.first : a.second < b.second); } int drX[] = {1, 1, 0, -1, -1, -1, 0, 1}; int drY[] = {0, 1, 1, 1, 0, -1, -1, -1}; int main() { ios::sync_with_stdio(false); int n; cin >> n; printf("0 0 %d\n", n); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, M = 205, mod = 1e9 + 7; int rd() { int x = 0, w = 1; char ch = 0; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch ^ 48); ch = getchar(); } return x * w; } int to[N << 1], nt[N << 1], hd[N], tot = 1; void adde(int x, int y) { ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot; ++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot; } void ad(int &x, int y) { x += y, x -= x >= mod ? mod : 0; } int fpow(int a, int b) { int an = 1; while (b) { if (b & 1) an = 1ll * an * a % mod; a = 1ll * a * a % mod, b >>= 1; } return an; } int ginv(int a) { return fpow(a, mod - 2); } int n, m, fac[M], iac[M], s2[M][M], ss[N], sz[N], f[N][M], g[3][M], h[3][M], fx[N], ans; void dfs(int x, int ffa) { ss[x] = 1; for (int i = hd[x]; i; i = nt[i]) { int y = to[i]; if (y == ffa) continue; dfs(y, x), ss[x] += ss[y]; } sz[x] = 1; for (int l = 0; l <= 2; ++l) memset(g[l], 0, sizeof(int) * (m + 1)); g[0][0] = 1; for (int i = hd[x]; i; i = nt[i]) { int y = to[i]; if (y == ffa) continue; int nsz = min(m, sz[x] + sz[y]); for (int l = 0; l <= 2; ++l) memset(h[l], 0, sizeof(int) * (nsz + 1)); for (int j = 0; j <= sz[x]; ++j) for (int k = 0; k <= sz[y] && j + k <= nsz; ++k) for (int l = 0; l <= 2; ++l) ad(h[min(l + (bool)k, 2)][j + k], 1ll * g[l][j] * f[y][k] % mod); for (int l = 0; l <= 2; ++l) memcpy(g[l], h[l], sizeof(int) * (nsz + 1)); sz[x] = nsz; } for (int j = 1; j <= m; ++j) ad(ans, 1ll * g[2][j] * s2[m][j] % mod * fac[j] % mod); for (int j = 1; j <= m; ++j) for (int l = 0; l <= 2; ++l) ad(f[x][j], g[l][j]); if (ffa) { ad(ans, 1ll * fx[ss[x]] * fx[n - ss[x]] % mod * s2[m][1] % mod); for (int j = 1; j < m; ++j) for (int l = 0; l <= 2; ++l) { ad(ans, 1ll * g[l][j] * fx[n - ss[x]] % mod * s2[m][j + 1] % mod * fac[j + 1] % mod); if (l) ad(f[x][j + 1], g[l][j]); } } f[x][0] = 1; ad(f[x][1], fx[ss[x]]); } int main() { n = rd(), m = rd(); fx[0] = 1; for (int i = 1; i <= n; ++i) fx[i] = 1ll * (fpow(2, i) - 1) * ginv(fpow(2, i)) % mod; for (int i = 1; i < n; ++i) adde(rd(), rd()); fac[0] = 1; for (int i = 1; i <= m; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; s2[0][0] = 1; for (int i = 1; i <= m; ++i) for (int j = 1; j <= i; ++j) s2[i][j] = (s2[i - 1][j - 1] + 1ll * j * s2[i - 1][j] % mod) % mod; dfs(1, 0); ans = 1ll * ans * fpow(2, n) % mod; printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> using namespace std; int t, l, r, ll, a[200005][30], sum; string s; int main() { cin >> s; ll = s.length(); for (int i = 0; i < ll; i++) { for (int j = 0; j < 26; j++) a[i + 1][j] = a[i][j]; a[i + 1][s[i] - 'a']++; } cin >> t; while (t--) { scanf("%d%d", &l, &r); if (l == r) { puts("Yes"); continue; } sum = 0; for (int i = 0; i < 26; i++) if (a[r][i] - a[l - 1][i]) sum++; l--; r--; if (s[l] == s[r] && sum <= 2) puts("No"); else puts("Yes"); } }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; vector<vector<int> > mult(vector<vector<int> > A, vector<vector<int> > B) { vector<vector<int> > ans(A.size(), vector<int>(B[0].size())); for (int i = 0; i < A.size(); i++) for (int j = 0; j < B[0].size(); j++) { for (int k = 0; k < A[0].size(); k++) ans[i][j] = (ans[i][j] + (long long)A[i][k] * B[k][j]) % MOD; } return ans; } vector<vector<int> > pow(vector<vector<int> > cur, int p) { vector<vector<int> > ans(cur.size(), vector<int>(cur.size())); for (int i = 0; i < cur.size(); i++) ans[i][i] = 1; while (p) { if (p % 2) ans = mult(ans, cur); cur = mult(cur, cur); p /= 2; } return ans; } int n, m, q; bool bad[20][20]; vector<vector<int> > cur; vector<vector<int> > adj; void makeAdj() { adj = vector<vector<int> >(n * m, vector<int>(n * m)); for (int i = 0; i < n * m; i++) if (!bad[i / m][i % m]) { adj[i][i] = 1; if (i / m > 0 && !bad[i / m - 1][i % m]) adj[i][i - m] = 1; if (i / m < n - 1 && !bad[i / m + 1][i % m]) adj[i][i + m] = 1; if (i % m > 0 && !bad[i / m][i % m - 1]) adj[i][i - 1] = 1; if (i % m < m - 1 && !bad[i / m][i % m + 1]) adj[i][i + 1] = 1; } } int main() { scanf("%d%d%d", &n, &m, &q); cur = vector<vector<int> >(1, vector<int>(n * m)); cur[0][0] = 1; int prevT = 1; for (int Q = 0; Q < q; Q++) { int type, x, y, t; scanf("%d%d%d%d", &type, &x, &y, &t); x -= 1; y -= 1; makeAdj(); cur = mult(cur, pow(adj, t - prevT)); if (type == 1) printf("%d\n", cur[0][x * m + y]); else bad[x][y] ^= 1; prevT = t; } return 0; }
8
#include <bits/stdc++.h> int cnt[200000]; int freq[200000]; using namespace std; int main(void) { int n, num, res = 0, m = 0; scanf("%d", &n); int nn = -1; int maxi = -1; for (int i = 0; i < n; i++) { scanf("%d", &num); if (freq[num] == 0) { if (m == 0) nn = num; m++; } else { cnt[freq[num]]--; } freq[num]++; cnt[freq[num]]++; maxi = max(maxi, freq[num]); if (m == i + 1) { res = max(res, i + 1); } if (cnt[maxi] * maxi == i) { res = max(res, i + 1); } if (cnt[freq[num]] == 1 && cnt[freq[num] - 1] == m - 1) { res = max(res, i + 1); } if (cnt[freq[num] + 1] == 1 && cnt[freq[num]] == m - 1) { res = max(res, i + 1); } } cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } const int N = (int)2e5 + 77; const long long mod = (long long)1e9 + 7; long long n, m, k, p; long long a[1111], b[2111]; long long check(long long lim) { long long now = 1; for (int i = 1; i <= (int)(k); ++i) if (abs(b[i] - a[now]) + abs(b[i] - p) <= lim) { now++; } return now > n; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k >> p; for (int i = 1; i <= (int)(n); ++i) cin >> a[i]; for (int i = 1; i <= (int)(k); ++i) cin >> b[i]; sort(a + 1, a + n + 1); sort(b + 1, b + k + 1); long long l = 0, r = (long long)2e9 + 77, ans = (long long)2e9 + 77; while (l <= r) { long long mid = l + r >> 1; if (check(mid)) r = mid - 1, ans = min(ans, mid); else l = mid + 1; } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; unsigned long long fact( unsigned long long n, unsigned long long m = numeric_limits<unsigned long long>::max()) { return n ? n * fact(n - 1, m) % m : 1; } unsigned long long gcd(unsigned long long a, unsigned long long b) { return b ? gcd(b, a % b) : a; } unsigned long long lcm(unsigned long long a, unsigned long long b) { return a / gcd(a, b) * b; } unsigned long long qpow( unsigned long long a, unsigned long long b, unsigned long long m = numeric_limits<unsigned long long>::max()) { return b ? (b % 2 ? a : 1) * qpow(a * a % m, b / 2, m) % m : 1; } bool isPow2(unsigned long long x) { return !(x & (x - 1)); } static const bool PARALLEL_MULTITEST = true; static const bool MULTILINE_OUTPUT = false; static const bool LAUNCH_STRESSTEST = false; static const string INPUT_FILE = ""; static const string OUTPUT_FILE = ""; static const string DBG_INPUT_FILE = "input.txt"; static const string DBG_OUTPUT_FILE = "output.txt"; static const string DBG_ERROR_FILE = "error.txt"; struct Solver { long long n, kx, ky; struct Test { long long x, y; char t; }; struct Ray { long long dist; char type; Ray() { dist = numeric_limits<long long>::max(); type = 'N'; } }; vector<Test> tests; void readData(istream &cin) { cin >> n >> kx >> ky; tests.resize(n); for (auto &t : tests) { cin >> t.t >> t.x >> t.y; } } bool test_slon(long long x, long long y) { return abs(x - kx) == abs(y - ky); } bool test_lad(long long x, long long y) { return x == kx || y == ky; } pair<int, long long> get_ray(long long x, long long y) { if (test_slon(x, y)) { return make_pair<int, long long>(2 * (x < kx) + (y < ky), abs(x - kx)); } else if (test_lad(x, y)) { long long dist = max(abs(x - kx), abs(y - ky)); return make_pair(4 + 2 * (x == kx) + (x < kx || y < ky), dist); } else { return make_pair<int, long long>(-1, -1); } } void solve(ostream &cout, ostream &cerr) { bool res = false; Ray rays[8]; for (const auto &t : tests) { auto ray = get_ray(t.x, t.y); if (ray.first == -1) { continue; } if (rays[ray.first].dist < ray.second) { continue; } rays[ray.first].type = t.t; rays[ray.first].dist = ray.second; } for (int i = (0); i <= (3); ++i) { if (rays[i].type == 'Q' || rays[i].type == 'B') { res = true; } } for (int i = (4); i <= (7); ++i) { if (rays[i].type == 'Q' || rays[i].type == 'R') { res = true; } } cout << (res ? "YES" : "NO") << endl; } }; int main() { std::ios::sync_with_stdio(false); if (!INPUT_FILE.empty()) freopen(INPUT_FILE.c_str(), "rt", stdin); if (!OUTPUT_FILE.empty()) freopen(OUTPUT_FILE.c_str(), "wt", stdout); auto solver = Solver(); solver.readData(cin); solver.solve(cout, cerr); return 0; }
4
#include <bits/stdc++.h> using namespace std; int onbit(int x, int pos) { x |= (1 << pos); return x; } int offbit(int x, int pos) { x &= ~(1 << pos); return x; } int test(int x, int pos) { return x & (1 << pos); } int flip(int x, int pos) { x ^= (1 << pos); return x; } int lsb(int x) { return x & (-x); } int onbitall(int x, int pos) { x = (1 << pos) - 1; return x; } const double EPS = 1e-9; const double PI = 2 * acos(0.0); const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int dx1[] = {0, 1, 0, -1}; const int dy1[] = {1, 0, -1, 0}; const int dx2[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int dy2[] = {-1, 0, 1, -1, 1, -1, 0, 1}; int n, m; int a[123]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i + 1); scanf("%d", &m); for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); int til = a[x]; for (int j = 1; j < y; j++) { a[x - 1]++; a[x]--; } for (int j = y; j < til; j++) { a[x + 1]++; a[x]--; } a[x]--; } for (int i = 1; i < n + 1; i++) { printf("%d\n", a[i]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long ar[n]; for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> ar[i]; long long pre[n], suf[n]; long long flag = 0; pre[0] = 1; suf[n - 1] = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { pre[i] = (pre[i - 1] && (ar[i] >= i)); } for (long long i = n - 2; i >= 0; i--) suf[i] = (suf[i + 1] && (ar[i] >= (n - 1 - i))); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { if (pre[i] == 1 && suf[i] == 1) { cout << "Yes" << '\n'; flag = 1; break; } } if (flag == 0) { cout << "No" << '\n'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int m; cin >> m; int n = m; int l = log10(m) + 1; int sum = 0; for (int i = 0; i <= l; i++) { int r; r = n % 10; sum += r; n = n / 10; } if (sum % 4 == 0) cout << m; else { for (int k = m;; k++) { int t = k; int sum1 = 0; int l1 = log10(t); for (int j = 0; j <= l1; j++) { int r1; r1 = t % 10; sum1 += r1; t = t / 10; } if (sum1 % 4 == 0) { cout << k; break; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[110], b[110]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < k; i++) { cin >> b[i]; } sort(b, b + k); reverse(b, b + k); int c = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { a[i] = b[c]; c++; } } for (int i = 1; i < n; i++) { if (a[i] <= a[i - 1]) { cout << "Yes"; return 0; } } cout << "No"; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long min(long long a, long long b) { return (a < b) ? a : b; } long long max(long long a, long long b) { return (a > b) ? a : b; } long long fp(long long a, long long b) { if (b == 0) return 1; long long x = fp(a, b / 2); x = (x * x) % mod; if (b & 1) x = (x * a) % mod; return x; } const long long N = 2e5 + 5; long long a[N]; long long n; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; long long cnt[60] = {0}; for (long long i = 1; i <= n; i++) { cin >> a[i]; for (long long b = 0; b < 20; b++) { if ((a[i] >> b) & 1) { cnt[b]++; } } } long long ans = 0; for (long long i = 1; i <= n; i++) { long long x = 0; for (long long b = 0; b < 20; b++) { if (cnt[b]) { cnt[b]--; x |= (1ll << b); } } ans += x * x; } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; struct node { node *par, *go[26]; int num; long long sum; int val; } * root, *tail, que[200010], *top[200010]; int tot; char str[200010 >> 1]; void add(int c, int l) { node *p = tail, *np = &que[tot++]; np->val = l; while (p && p->go[c] == NULL) p->go[c] = np, p = p->par; if (p == NULL) np->par = root; else { node *q = p->go[c]; if (p->val + 1 == q->val) np->par = q; else { node *nq = &que[tot++]; *nq = *q; nq->val = p->val + 1; np->par = q->par = nq; while (p && p->go[c] == q) p->go[c] = nq, p = p->par; } } tail = np; } int c[200010], len; void init() { memset(que, 0, sizeof(que)); tot = 0; len = 1; root = tail = &que[tot++]; } void solve() { int i, j; memset(c, 0, sizeof(c)); for (i = 0; i < tot; i++) c[que[i].val]++; for (i = 1; i < len; i++) c[i] += c[i - 1]; for (i = 0; i < tot; i++) top[--c[que[i].val]] = &que[i]; for (node *p = root;; p = p->go[str[p->val] - 'a']) { p->num = 1; if (p->val == len - 1) break; } for (i = tot - 1; i >= 0; i--) { node *p = top[i]; if (p->par) { node *q = p->par; q->num += p->num; } for (j = 0; j < 26; j++) { if (p->go[j]) p->sum += p->go[j]->num + p->go[j]->sum; } } } int ans[200010]; void dfs(int k) { node *p = root; if (k > p->sum) { printf("No such line.\n"); return; } int x; int i; while (1) { for (i = 0; i < 26; i++) { if (p->go[i]) { node *q = p->go[i]; if (k > q->sum + q->num) { k -= q->sum + q->num; } else { x = i; break; } } } printf("%c", x + 'a'); p = p->go[x]; k -= p->num; if (k <= 0) break; } printf("\n"); } int main() { scanf("%s", str); int i, l = strlen(str), k; init(); for (i = 0; i < l; i++) add(str[i] - 'a', len++); solve(); scanf("%d", &k); dfs(k); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323846; const long long M = 1e18 + 7; const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const int mod = 998244353; void solve() { int n; cin >> n; vector<long long> v(n), pre(n); pre[0] = 0; for (long long i = 0; i < (n); ++i) { cin >> v[i]; if (i == 0) pre[i] = v[i]; else if (i % 2) { pre[i] = pre[i - 1] - v[i]; } else pre[i] = pre[i - 1] + v[i]; } long long mn; long long ans = 0; for (int i = 0; i < n; i += 2) { mn = 0; long long a = 0, b = 0; for (int j = i + 1; j < n; j++) { if (j % 2) { if (v[i] >= mn && v[j] >= a + mn) { int b = a + mn; ans += min(v[i] - mn + (mn != 0 ? 1 : 0), v[j] - (a + mn) + (b != 0 ? 1 : 0)); } if (v[j] > a) { mn = max(mn, v[j] - a); a -= v[j]; } else { a -= v[j]; } } else { a += v[j]; } } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int SIZE = 400 + 1; const long long int mod = 1e9 + 7; int N, M, u, v; int x; int arr[SIZE]; int minDist[SIZE]; int hasrailpath[SIZE][SIZE]; int conn[SIZE][SIZE]; bool vis[SIZE]; bool contains(int N) { if (N < 0) N = -N; while (N) { if (N % 10 == 8) return 1; N /= 10; } return 0; } bool comp(const pair<int, int>& p1, const pair<int, int>& p2) { if (p1.second != p2.second) return p1.second < p2.second; return p1.first < p2.first; } int main() { long long int a, b, c, tb, tc; vector<pair<int, int> > orders; scanf("%d", &N); while (N--) scanf("%d%d", &u, &v), orders.push_back(make_pair(u, v)); sort(orders.begin(), orders.end(), comp); int ans = 1; int prev = orders[0].second; int sz = orders.size(); for (int i = 1; i < sz; i++) if (orders[i].first > prev) ans++, prev = orders[i].second; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const char nl = '\n'; const int E = 1e5 + 10; struct dsu { vector<int> par; dsu(int N) { par.resize(N + 1); iota(par.begin(), par.end(), 0); } int find(int x) { return (par[x] == x) ? (x) : (par[x] = find(par[x])); } bool check(int u, int v) { u = find(u); v = find(v); if (u == v) return false; return true; } bool merge(int u, int v) { u = find(u); v = find(v); if (u == v) return false; if (u > v) swap(u, v); par[v] = u; return true; } }; int main() { int N, n, m; cin >> N >> n >> m; dsu Di(N), Mo(N); for (int i = 0; i < n; i++) { int u, v; cin >> u >> v; Di.merge(u, v); } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; Mo.merge(u, v); } vector<pair<int, int>> res; vector<int> v1, v2; for (int i = 1; i <= 1; i++) { for (int j = i + 1; j <= N; j++) { if (Di.check(i, j) && Mo.check(i, j)) { res.push_back({i, j}); Di.merge(i, j); Mo.merge(i, j); } if (Di.find(j) != 1) v1.push_back(j); if (Mo.find(j) != 1) v2.push_back(j); } } cout << N - max(n, m) - 1 << nl; while (!v1.empty() && !v2.empty()) { if (Di.find(v1.back()) == 1 && Mo.find(v1.back()) == 1) { v1.pop_back(); continue; } if (Di.find(v2.back()) == 1 && Mo.find(v2.back()) == 1) { v2.pop_back(); continue; } res.push_back({v1.back(), v2.back()}); Di.merge(v1.back(), v2.back()); Mo.merge(v1.back(), v2.back()); } for (auto &[x, y] : res) cout << x << ' ' << y << nl; }
8
#include <bits/stdc++.h> using namespace std; int box[45]; int out[1005], len; int main() { int n, m; while (scanf("%d %d", &n, &m) != EOF) { int i, j; for (i = 1; i <= m; ++i) { scanf("%d", &box[i]); } out[0] = -1; memset(out, 0, sizeof(out)); int choice; for (i = 1; i <= n; ++i) { if (i == n) out[i + 1] = out[1]; choice = 0; for (j = 1; j <= m; ++j) { if (j == out[i - 1] || j == out[i + 1]) continue; if (!choice || box[j] > box[choice] || (box[choice] == box[j] && j == out[1])) choice = j; } if (box[choice] < 1) { printf("-1\n"); break; } out[i] = choice; --box[choice]; } if (i <= n) continue; for (i = 1; i <= n; ++i) { printf("%d", out[i]); if (i == n) printf("\n"); else printf(" "); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 100100; int n, m, aux[MAXN], f[MAXN], ans; char s[MAXN], t[MAXN]; int main() { scanf("%s %s", s + 1, t + 1); n = strlen(s + 1), m = strlen(t + 1); for (int i = 1; i <= n; ++i) { aux[i] = aux[i - 1] + (s[i] == t[aux[i - 1] + 1]); if (!f[aux[i]]) f[aux[i]] = i; } f[0] = 0; for (int i = n + 1, j = m + 1; i >= 1; --i) { if (s[i] == t[j - 1]) j--; ans = max(ans, i - f[j - 1] - 1); } printf("%d\n", ans); }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long n, x; cin >> n >> x; long double amount = 0, sum = 0; long double C = 0, H = 0; vector<long long> A(n), T(n); vector<pair<long long, long long>> Cold, Hot; for (int i = 0; i < n; i++) cin >> A[i]; for (int i = 0; i < n; i++) { cin >> T[i]; sum += T[i] * A[i]; amount += A[i]; if (T[i] == x) continue; else if (T[i] > x) Hot.push_back({T[i], A[i]}), H += A[i]; else Cold.push_back({T[i], A[i]}), C += A[i]; } sort(Hot.rbegin(), Hot.rend()); sort(Cold.begin(), Cold.end()); if (sum / amount == x) return cout << setprecision(15) << fixed << amount, 0; else if (sum / amount > x) { long double L = 0.0, R = H + 1; for (int i = 0; i < 200; i++) { long double M = (L + R) / 2; long double left = M; long double amount2 = amount; long double sum2 = sum; for (int i = 0; i < Hot.size(); i++) { long double lose = min(left, (long double)Hot[i].second); left -= lose; sum2 -= Hot[i].first * lose; amount2 -= lose; } if (sum2 / amount2 <= x) R = M; else L = M; } if (R > H + 0.5) return cout << 0, 0; cout << setprecision(15) << fixed << amount - R << "\n"; } else if (sum / amount < x) { long double L = 0.0, R = C + 1; for (int i = 0; i < 200; i++) { long double M = (L + R) / 2; long double left = M; long double amount2 = amount; long double sum2 = sum; for (int i = 0; i < Cold.size(); i++) { long double lose = min(left, (long double)Cold[i].second); left -= lose; sum2 -= Cold[i].first * lose; amount2 -= lose; } if (sum2 / amount2 >= x) R = M; else L = M; } if (R > C + 0.5) return cout << 0, 0; cout << setprecision(15) << fixed << amount - R << "\n"; } }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m, j, i; int ax, ay, bx, by, cx, cy; cin >> n; cin >> ax >> ay; cin >> bx >> by; cin >> cx >> cy; if (bx < ax && cx < ax) { if (by < ay && cy < ay) { cout << "YES"; return 0; } if (by > ay && cy > ay) { cout << "YES"; return 0; } } if (bx > ax && cx > ax) { if (by < ay && cy < ay) { cout << "YES"; return 0; } if (by > ay && cy > ay) { cout << "YES"; return 0; } } cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, m, l, r, x, p[N], a[N / 5]; void init() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 2; i <= n; i++) { if (a[i] == a[i - 1]) p[i] = p[i - 1]; else p[i] = i - 1; } while (m--) { scanf("%d%d%d", &l, &r, &x); if (a[r] != x) printf("%d\n", r); else if (p[r] >= l) printf("%d\n", p[r]); else printf("-1\n"); } } void fread() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int main() { init(); return 0; }
4