solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int b, d, is[9]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int main() { scanf("%d%d", &b, &d); int k = 0; for (; gcd(b, d) > 1; k++) d /= gcd(b, d); if (d == 1) { printf("2-type\n%d", k); } else if (k == 0 && b % d == 1) { printf("3-type"); } else if (k == 0 && b % d == d - 1) { printf("11-type"); } else if ((b * b - 1) / (b % 2 + 1) % d == 0) { printf("6-type"); } else printf("7-type"); }
7
#include <bits/stdc++.h> using namespace std; const int next_x[8] = {0, 1, -1, 0, 1, 1, -1, -1}; const int next_y[8] = {1, 0, 0, -1, 1, -1, -1, 1}; const int inf = 1e9 + 5; const long long linf = 1e18; const double PI = acos(-1.0); const int MAXN = 2e5 + 5; const int N = 1e6 + 5; int n, m, k, cnt = 0; int mx[N * 4], add[N * 4]; set<pair<int, int> > s; multiset<int> t; void doadd(int cnt, int v) { add[cnt] += v, mx[cnt] += v; } void up(int cnt) { mx[cnt] = max(mx[((cnt << 1))], mx[((cnt << 1 | 1))]); } void down(int cnt) { if (add[cnt]) { doadd(((cnt << 1)), add[cnt]); doadd(((cnt << 1 | 1)), add[cnt]); add[cnt] = 0; } } void update(int l, int r, int nl, int nr, int v, int cnt) { if (l == nl && r == nr) { doadd(cnt, v); return; } down(cnt); if (nr <= ((l + r) / 2)) update(l, ((l + r) / 2), nl, nr, v, ((cnt << 1))); else if (nl > ((l + r) / 2)) update(((l + r) / 2) + 1, r, nl, nr, v, ((cnt << 1 | 1))); else update(l, ((l + r) / 2), nl, ((l + r) / 2), v, ((cnt << 1))), update(((l + r) / 2) + 1, r, ((l + r) / 2) + 1, nr, v, ((cnt << 1 | 1))); up(cnt); } void build(int l, int r, int cnt) { if (l == r) { mx[cnt] = l - 1; return; } build(l, ((l + r) / 2), ((cnt << 1))), build(((l + r) / 2) + 1, r, ((cnt << 1 | 1))), up(cnt); } int query(int l, int r, int nl, int nr, int cnt) { if (l == nl && r == nr) return mx[cnt]; down(cnt); if (nr <= ((l + r) / 2)) return query(l, ((l + r) / 2), nl, nr, ((cnt << 1))); if (nl > ((l + r) / 2)) return query(((l + r) / 2) + 1, r, nl, nr, ((cnt << 1 | 1))); return max( query(l, ((l + r) / 2), nl, ((l + r) / 2), ((cnt << 1))), query(((l + r) / 2) + 1, r, ((l + r) / 2) + 1, nr, ((cnt << 1 | 1)))); } int work() { scanf("%d%d%d", &n, &k, &m), build(1, N, 1); while (m--) { pair<int, int> p; scanf("%d%d", &p.first, &p.second); int pos = abs(k - p.first) + p.second, f; if (s.find(p) == s.end()) s.insert(p), f = 1, t.insert(pos); else s.erase(p), f = -1, t.erase(t.find(pos)); update(1, N, 1, pos, f, 1); if (t.empty()) { printf("0\n"); continue; } int l = max(n, *t.rbegin()), r = N, ans = r; while (l <= r) { if (query(1, N, 1, ((l + r) / 2), 1) <= ((l + r) / 2)) ans = ((l + r) / 2), r = ((l + r) / 2) - 1; else l = ((l + r) / 2) + 1; } printf("%d\n", max(0, ans - n)); } return 0; } int main() { work(); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 1000010, D = N * 22; int n, px[N], py[N], a[N], root[N]; int sum[D], l[D], r[D], dn; int m, key, ans; int insert(int d, int lt, int rt, int w) { int md = (lt + rt) >> 1; int nd = ++dn; sum[nd] = sum[d] + 1; if (lt != rt) { if (w <= md) { l[nd] = insert(l[d], lt, md, w); r[nd] = r[d]; } else { r[nd] = insert(r[d], md + 1, rt, w); l[nd] = l[d]; } } return nd; } int query(int d, int lt, int rt, int lq, int rq) { if (!d) { return 0; } if (lq <= lt && rt <= rq) { return sum[d]; } int md = (lt + rt) >> 1; int ret = 0; if (lq <= md) { ret += query(l[d], lt, md, lq, rq); } if (md < rq) { ret += query(r[d], md + 1, rt, lq, rq); } return ret; } int init() { int x; scanf("%d", &x); return (x - 1 + key) % n + 1; } int main() { scanf("%d", &n); for (int i = 1, v; i <= n; ++i) { scanf("%d", &v), px[v] = i; } for (int i = 1, v; i <= n; ++i) { scanf("%d", &v), py[v] = i; } for (int v = 1; v <= n; ++v) { a[px[v]] = v; } root[0] = ++dn; for (int i = 1; i <= n; ++i) { root[i] = insert(root[i - 1], 1, n, py[a[i]]); } scanf("%d", &m); ans = -1; for (int im = 1; im <= m; ++im) { key = ans + 1; int l1 = init(), r1 = init(), l2 = init(), r2 = init(); if (l1 > r1) swap(l1, r1); if (l2 > r2) swap(l2, r2); ans = query(root[r1], 1, n, l2, r2) - query(root[l1 - 1], 1, n, l2, r2); printf("%d\n", ans); } }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); printf("%d\n", (n / 2) - 2); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); string a, b; cin >> a; b = a; reverse(b.begin(), b.end()); cout << a + b; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; set<pair<char, char> > arr; int ans = 0; vector<bool> us(30, false); for (int i = 0; i < s.size(); i++) { if (s[i] != t[i]) { char a1, a2, a3, a4; a1 = max(s[i], t[i]); a2 = min(s[i], t[i]); arr.insert({a1, a2}); } else { us[s[i] - 'a'] = true; } } vector<int> cnt(30, 0); for (set<pair<char, char> >::iterator it = arr.begin(); it != arr.end(); it++) { char a1, a2; a1 = it->first; a2 = it->second; cnt[a1 - 'a']++; cnt[a2 - 'a']++; } bool wr = false; for (int i = 0; i < 30; i++) { if (cnt[i] > 1 || (cnt[i] == 1 && us[i])) { wr = true; } } if (wr) { cout << "-1"; } else { cout << arr.size() << endl; for (set<pair<char, char> >::iterator it = arr.begin(); it != arr.end(); it++) { char a1, a2; a1 = it->first; a2 = it->second; cout << a1 << " " << a2 << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; char str[1024]; inline void scr(int x, int OK) { if (!x) return; scr(x / 1000, false); if (x / 1000 > 0) { long long cx = x % 1000; if (!(cx / 100)) printf("0"); if (!(cx / 10)) printf("0"); } printf("%d", x % 1000); if (!OK) printf("."); } inline long long establish(double sum) { double x = sum * 1000.0; long long xx = floor(x); long long c = xx % 10LL; if (c >= 5LL) xx += 10LL; xx /= 10LL; return xx; } int main() { gets(str + 1); int n = strlen(str + 1); double sum = 0.0, nr = 0.0; int dec = 0, lg = 0; bool dot = true; for (int i = 1; i <= n; ++i) { if ('a' <= str[i] && str[i] <= 'z') { if (lg) { if (dot && lg == 2) nr += 1.0 * dec / 100.0; else nr = nr * 1000.0 + dec; sum += nr; nr = 0.0; } dot = false; lg = dec = 0; continue; } if (str[i] == '.') { dot = true; nr = nr * 1000.0 + dec; dec = lg = 0; continue; } ++lg; dec = dec * 10 + str[i] - 48; } if (dot && lg == 2) nr += 1.0 * dec / 100.0; else nr = nr * 1000.0 + dec; sum += nr; nr = 0.0; long long xx = establish(sum); int ss = (int)(xx / 100LL); if (!ss) printf("0"); scr(ss, true); ss = (int)(xx % 100LL); if (ss) { printf("."); if (!(ss / 10)) printf("0"); printf("%d", ss); } return 0; }
4
#include <bits/stdc++.h> const int N = 7000000; int primes[N], pal[N]; bool sieve[N]; int main() { int p, q, ans = 0; scanf("%d%d", &p, &q); for (int i = 2; i < N; ++i) { sieve[i] = true; } for (int i = 2; i * i <= N; ++i) { if (sieve[i]) { for (int j = i * i; j <= N; j += i) { sieve[j] = false; } } } for (int i = 2; i < N; ++i) { if (sieve[i]) { ++primes[i]; } primes[i] += primes[i - 1]; } for (int i = 1; i < N; ++i) { pal[i] += pal[i - 1]; int rev = 0, j = i; while (j > 0) { rev *= 10; rev += j % 10; j /= 10; } if (rev == i) { ++pal[i]; } } for (int i = 0; i < N; ++i) { if (q * 1ll * primes[i] <= p * 1ll * pal[i]) { ans = i; } } printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; bool isprime(int a) { for (int i = 2; i <= sqrt(a); i++) { if (a % i == 0) return false; } return true; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int a, int b) { return (a * b) / (gcd(a, b)); } int fact(int n) { int s; for (int i = 1, s = 1; i <= n; i++) { s *= i; } return s; } int comb(int a, int b) { return (fact(a) / (fact(a - b) * fact(b))); } int check(vector<char> v, string s) { int j = 0; for (int i = 0; i < s.length() && j < v.size(); i++) { if (v[j] == s[i]) j++; } if (j == v.size()) return 1; else return 0; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int a, k; cin >> a >> k; int n = a; string s = ""; int i; for (i = 0; i < n - 2; i++) s += "a"; s += "bb"; int j = n - 1; int g = n + 1; int b[g]; b[0] = 1; for (i = 1; i < g; i++) { b[i] = i + 1 + b[i - 1]; } int f1 = 0, f2 = 0; for (i = 0; i < g; i++) { if (k - b[i] <= 0) { f1 = i; break; } } if (f1 != 0) { f2 = k - b[f1 - 1]; swap(s[n - 2], s[n - 2 - f1]); swap(s[n - 1], s[n - f2]); } cout << s << endl; } }
2
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& os, vector<T> v) { os << '['; string sep; for (const T& el : v) os << sep << el, sep = ", "; return os << ']'; } template <typename S, typename T> ostream& operator<<(ostream& os, pair<S, T> p) { return os << '(' << p.first << ',' << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } void player_2(int n) { cout << "Second" << endl; vector<vector<int> > edgelist(n); vector<pair<int, int> > raw_numbers(n, make_pair(-1, -1)); vector<pair<int, int> > pairs_with_mod(n, make_pair(-1, -1)); for (int i = 1; i < 2 * n + 1; i++) { int p; cin >> p; p--; if (raw_numbers[p].first < 0) { raw_numbers[p].first = i; } else { raw_numbers[p].second = i; } if (pairs_with_mod[i % n].first < 0) { pairs_with_mod[i % n].first = p; } else { pairs_with_mod[i % n].second = p; } }; vector<vector<int> > cycles; vector<bool> used_mods(n, false); for (int i = 0; i < n; i++) { if (used_mods[i]) continue; vector<int> cyc; int tail_pair = pairs_with_mod[i].first; int head_pair = pairs_with_mod[i].second; int curr_mod = i; while (true) { ; cyc.push_back(head_pair); if (raw_numbers[head_pair].second % n == curr_mod) raw_numbers[head_pair] = make_pair(raw_numbers[head_pair].second, raw_numbers[head_pair].first); if (head_pair == tail_pair) break; curr_mod = raw_numbers[head_pair].second % n; used_mods[curr_mod] = true; pair<int, int>& mod_pairs = pairs_with_mod[curr_mod]; if (mod_pairs.second == head_pair) mod_pairs = make_pair(mod_pairs.second, mod_pairs.first); head_pair = mod_pairs.second; } cycles.push_back(cyc); }; ; int mod2 = 0; for (int i = 0; i < n; i++) { mod2 = (mod2 + raw_numbers[i].first) % 2; } if (mod2) { for (vector<int>& cyc : cycles) { if (cyc.size() % 2) { for (int p : cyc) { raw_numbers[p] = make_pair(raw_numbers[p].second, raw_numbers[p].first); } break; } } } for (int i = 0; i < n; i++) { cout << raw_numbers[i].first << ' '; } cout << endl; } void player_1(int n) { cout << "First" << endl; for (int j = 0; j < 2; j++) { for (int i = 1; i < n + 1; i++) { cout << i << ' '; } } cout << '\n'; } void run() { int n; cin >> n; if (n % 2) { player_2(n); } else { player_1(n); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); run(); }
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; long long sum = 0, a; for (long long i = 0; i < n; i++) { cin >> a; sum += a; } if (sum % n == 0) cout << n << "\n"; else cout << n - 1 << "\n"; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; struct note { int x, id; } q[maxn]; int st[maxn][20], st1[maxn][20], a[maxn], n, dp[maxn]; int head, tail; void init(int n) { for (int i = 1; i <= n; i++) st1[i][0] = st[i][0] = a[i]; for (int j = 1; j < 20; j++) for (int i = 1; i <= n; i++) if (i + (1 << (j - 1)) <= n) st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]), st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]); } int query(int l, int r) { int len = log2(r - l + 1); return max(st1[l][len], st1[r - (1 << len) + 1][len]) - min(st[l][len], st[r - (1 << len) + 1][len]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int s, len; cin >> n >> s >> len; for (int i = 1; i <= n; i++) cin >> a[i]; init(n); int l, r, ans; if (len > n || query(1, len) > s) return cout << -1 << endl, 0; memset(dp, 0x3f3f3f3f, sizeof(dp)); dp[0] = 0; q[tail].id = 0, q[tail].x = 0, tail++; dp[len] = 1; for (int i = len + 1; i <= n; i++) { l = 1, r = i - len + 1, ans = -1; while (l <= r) { int mid = (l + r) >> 1; if (query(mid, i) <= s) r = mid - 1, ans = mid; else l = mid + 1; } while (head < tail && q[tail - 1].x >= dp[i - len]) tail--; q[tail].x = dp[i - len], q[tail].id = i - len, tail++; if (ans != -1) { while (head < tail && q[head].id < ans - 1) head++; if (head < tail) dp[i] = min(dp[i], q[head].x + 1); } } if (dp[n] == 0x3f3f3f3f) dp[n] = -1; cout << dp[n] << endl; }
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, ans = INT_MAX, a_, b_, c_; cin >> a >> b >> c; for (int A = 1; A <= (c << 1); A++) for (int B = A; B <= (c << 1); B += A) for (int C = B; C <= (c << 1); C += B) { int res = abs(A - a) + abs(B - b) + abs(C - c); if (res < ans) { ans = res; a_ = A, b_ = B, c_ = C; } } cout << ans << '\n' << a_ << ' ' << b_ << ' ' << c_ << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 20; const long long inf = 1e10; long long h[N]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { long long n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) cin >> h[i]; bool ans = true; for (int i = 0; i < n - 1 && ans; i++) { long long need = max(0ll, h[i + 1] - k); if ((h[i] + m) < need) ans = false; else { if (h[i] >= need) m += h[i] - need; else m -= need - h[i]; } } if (ans) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int s[105]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; int ned = 0; for (int i = 1; i <= n; i++) ned += s[i] % 2; cout << (ned == 0 || ned == n ? "YES" : "NO") << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long MOD = 1000000007; const int N = 505; int a[N][N]; bool v[N][N]; vector<char> road; char ty[] = {'R', 'L', 'D', 'U'}; int X[] = {0, 0, 1, -1}; int Y[] = {1, -1, 0, 0}; int n, xx0, yy0; int num; void dfs(int x1, int y1) { v[x1][y1] = true; if (a[x1][y1] == 1) ++num; if (a[x1][y1] == 0) { a[x1][y1] = 1; road.push_back('1'); } for (int i = 0; i < 4; ++i) { int x = x1 + X[i]; int y = y1 + Y[i]; bool flag = false; int x2 = x, y2 = y; while (x2 >= 1 && x2 <= n && y2 >= 1 && y2 <= n && !v[x2][y2]) { if (a[x2][y2] == 1) { flag = true; break; } x2 = x2 + X[i]; y2 = y2 + Y[i]; } if (flag) { road.push_back(ty[i]); dfs(x, y); road.push_back(ty[i ^ 1]); } } a[x1][y1] = 0; road.push_back('2'); } int main() { while (scanf("%d %d %d", &n, &xx0, &yy0) != EOF) { int m = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { scanf("%d", &a[i][j]); if (a[i][j] == 1) ++m; } road.clear(); memset(v, false, sizeof(v)); num = 0; dfs(xx0, yy0); if (m != num) printf("NO\n"); else { printf("YES\n"); for (unsigned int i = 0; i < road.size(); ++i) printf("%c", road[i]); printf("\n"); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 5e5 + 10; long long a[maxn], b[maxn], c[maxn], ans = 0; int n; void addmod(long long &a, long long b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } void add(int u, int val) { for (; u <= n; u += u & -u) addmod(c[u], val); } long long sum(int u) { long long ret = 0; for (; u > 0; u -= u & -u) addmod(ret, c[u]); return ret; } int id(int x) { return lower_bound(b, b + n, x) - b + 1; } long long solve() { for (int i = 1; i <= n; ++i) c[i] = 0; long long ans = 0; for (int i = 0; i < n; ++i) { long long curr = sum(id(a[i])); addmod(ans, (a[i] * curr % mod) * (n - i) % mod); add(id(a[i]), i + 1); } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]), b[i] = a[i]; sort(b, b + n); for (int i = 0; i < n; ++i) addmod(ans, a[i] * (i + 1) % mod * (n - i) % mod); addmod(ans, solve()); reverse(a, a + n); addmod(ans, solve()); printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); srand(time(NULL)); long long int t; cin >> t; while (t--) { long long int a, b, n; cin >> a >> b >> n; if (n % 3 == 0) cout << a << "\n"; else if (n % 3 == 1) cout << b << "\n"; else cout << (a ^ b) << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; int n; int t[5][10], num[10]; int f(int x, int y) { if ((long long)2 * x > y) return 500; if ((long long)4 * x > y) return 1000; if ((long long)8 * x > y) return 1500; if ((long long)16 * x > y) return 2000; if ((long long)32 * x > y) return 2500; return 3000; } bool check(int x) { int s = 0, sb = 0; for (int i = 1; i <= 5; i++) { if (t[1][i] < t[2][i] && t[1][i]) s += f(num[i] + x, n + x) * (t[1][i] - t[2][i]); else s += f(num[i], n + x) * (t[1][i] - t[2][i]); } return s > 0; } int q[10] = {1, 2, 4, 8, 16, 32}; int main() { scanf("%d", &n); memset(num, 0, sizeof(num)); for (int i = 1; i <= 2; i++) for (int j = 1; j <= 5; j++) { scanf("%d", &t[i][j]); if (t[i][j] == -1) t[i][j] = 0; else num[j]++, t[i][j] = 250 - t[i][j]; } for (int i = 3; i <= n; i++) for (int j = 1; j <= 5; j++) { int x; scanf("%d", &x); if (x != -1) num[j]++; } for (int i = 0; i <= 4000; i++) if (check(i)) { printf("%d\n", i); return 0; } printf("-1\n"); }
6
#include <bits/stdc++.h> using namespace std; const int N = 25; int a[N][N]; char s[N][N]; int rev[1 << N]; int dp[N]; int cost[N][N], st[N][N]; int lowbit(int x) { return x & (-x); } void update(int &x, int y) { if (x == -1) x = y; else x = min(x, y); } int main() { for (int i = 0; i < N; i++) rev[1 << i] = i; int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", s[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int mx = 0; for (int k = 0; k < n; k++) { if (s[i][j] == s[k][j]) { mx = max(mx, a[k][j]); st[i][j] |= (1 << k); cost[i][j] += a[k][j]; } } cost[i][j] -= mx; } for (int i = 1; i < (1 << n); i++) dp[i] = 1000000000; for (int i = 0; i < (1 << n) - 1; i++) { if (dp[i] == -1) continue; for (int pos = 0; pos < n; pos++) { for (int j = 0; j < m; j++) { update(dp[i | st[pos][j]], dp[i] + cost[pos][j]); update(dp[i | (1 << pos)], dp[i] + a[pos][j]); } } } cout << dp[(1 << n) - 1] << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100 + 10, MAX_M = 20, UND = ~0U >> 1; int n, m, s[MAX_N]; char cmd[MAX_M]; int who[MAX_M]; int dp[MAX_M + 1][1 << MAX_M]; bool saved[MAX_M + 1][1 << MAX_M]; void update(int& r, int w, int d) { if (r == UND) r = d; else { if (w == 1) r = max(r, d); else r = min(r, d); } } int calc(int i, int cant) { if (i == m) return 0; int& ret = dp[i][cant]; if (saved[i][cant]) return ret; saved[i][cant] = true; ret = UND; if (cmd[i] == 'p') { for (int k = 0; k < m; ++k) if (~cant >> k & 1) { update(ret, who[i], (who[i] == 1 ? s[k] : -s[k]) + calc(i + 1, cant ^ (1 << k))); } } else { update(ret, who[i], calc(i + 1, cant)); for (int k = 0; k < m; ++k) if (~cant >> k & 1) { update(ret, who[i], +calc(i + 1, cant ^ (1 << k))); } } return ret; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> s[i]; } cin >> m; for (int i = 0; i < m; ++i) { scanf(" "); scanf("%c", &cmd[i]); scanf("%d", who + i); } sort(s, s + n); reverse(s, s + n); for (int i = 0; i < (1 << m); ++i) { dp[m][i] = 0; } for (int i = m - 1; i >= 0; --i) { for (int j = 0; j < (1 << m); ++j) { int& ret = dp[i][j] = UND; int cant = j; if (cmd[i] == 'p') { for (int k = 0; k < m; ++k) if (~cant >> k & 1) { update(ret, who[i], (who[i] == 1 ? s[k] : -s[k]) + dp[i + 1][cant ^ (1 << k)]); } } else { update(ret, who[i], dp[i + 1][cant]); for (int k = 0; k < m; ++k) if (~cant >> k & 1) { update(ret, who[i], dp[i + 1][cant ^ (1 << k)]); } } } } cout << dp[0][0] << endl; }
7
#include <bits/stdc++.h> using namespace std; int n, ans, ans2; int a[111111]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 25) { ans++; } else if (a[i] == 50) { if (ans > 0) { ans--; ans2++; } else { cout << "NO"; return 0; } } else { if (ans2 > 0 && ans > 0) { ans2--; ans--; } else if (ans2 == 0 && ans >= 3) { ans -= 3; } else { cout << "NO"; return 0; } } } cout << "YES"; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool operator<(const pair<int, long long>& e1, const pair<int, long long>& e2) { if (e1.first != e2.first) return e1.first < e2.first; return e1.second < e2.second; } pair<int, long long> operator+(const pair<int, long long>& e1, const pair<int, long long>& e2) { return {e1.first + e2.first, e1.second + e2.second}; } int t, n, m; vector<pair<int, long long> > DP[3010]; vector<int> e[3010]; vector<pair<int, long long> > step(const vector<pair<int, long long> >& fa, const vector<pair<int, long long> >& fb) { vector<pair<int, long long> > ret(fa.size() + fb.size(), {-1, 0}); for (int a = 0; a < (int)fa.size(); ++a) { for (int b = 0; b < (int)fb.size(); ++b) { ret[a + b] = max(ret[a + b], fa[a] + fb[b]); pair<int, long long> bvar = fb[b]; if (bvar.second > 0) ++bvar.first; bvar.second = 0; ret[a + b + 1] = max(ret[a + b + 1], fa[a] + bvar); } } return ret; } void dfs(int node, int p) { for (int ch : e[node]) { if (ch == p) continue; dfs(ch, node); DP[node] = step(DP[node], DP[ch]); } } void solve() { cin >> n >> m; for (int i = 0; i < n; ++i) { DP[i] = {{0, 0}}; e[i].clear(); int x; cin >> x; DP[i][0].second = -x; } for (int i = 0; i < n; ++i) { int x; cin >> x; DP[i][0].second += x; } for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; --x, --y; e[x].push_back(y), e[y].push_back(x); } dfs(0, -1); int ans = DP[0][m - 1].first; if (DP[0][m - 1].second > 0) ++ans; cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin >> t; while (t--) solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; const long long inf = 1000000000000000; int a[maxn], b[maxn], c[maxn], cc[maxn], e[maxn], sta[maxn]; int h[maxn], go[maxn * 2], nxt[maxn * 2], fa[maxn], d[maxn]; int i, j, k, l, r, t, n, m, u, v, w, z, tot, top, S, T, ansu, ansv; long long num, sum, ans; bool bz[maxn]; bool czy; int read() { int 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 * 10 + ch - '0'; ch = getchar(); } return x * f; } void add(int x, int y) { go[++tot] = y; nxt[tot] = h[x]; h[x] = tot; } void dfs(int x, int y) { d[x] = d[y] + 1; fa[x] = y; int t = h[x]; while (t) { if (go[t] != y) dfs(go[t], x); t = nxt[t]; } } bool pd() { int i; for (i = 1; i <= n; i++) if (a[i] != b[i]) return 0; return 1; } void re() { int i; for (i = 1; i <= n; i++) { a[i] = c[i]; b[i] = cc[i]; } num = 0; } int lca(int x, int y) { while (x != y) { if (d[x] > d[y]) x = fa[x]; else y = fa[y]; } return x; } int main() { n = read(); for (i = 1; i <= n; i++) { c[i] = a[i] = read(); if (!a[i]) S = i; } for (i = 1; i <= n; i++) { cc[i] = b[i] = read(); if (!b[i]) T = i; } for (i = 1; i <= n - 1; i++) { j = read(); k = read(); add(j, k); add(k, j); } d[0] = -1; dfs(S, 0); k = T; while (k) { bz[k] = 1; sta[++top] = k; k = fa[k]; } for (i = 1; i <= top - 1; i++) b[sta[i]] = b[sta[i + 1]]; b[sta[top]] = 0; if (pd()) { printf("0 %d\n", d[T]); return 0; } ans = inf; num = d[T]; u = 0; v = 0; for (i = 1; i <= n; i++) if (a[i] != b[i]) { czy = 1; t = h[i]; while (t) { if (go[t] != fa[i] && a[go[t]] != b[go[t]]) { czy = 0; break; } t = nxt[t]; } if (!czy) continue; if (!u) u = i; else v = i; } if (!v) { k = u; top = 0; while (a[k] != b[k]) { sta[++top] = k; k = fa[k]; } l = k; while (!bz[l]) l = fa[l]; num += 2 * (d[k] - d[l]); for (i = 1; i <= top + 1; i++) if (i > top || b[sta[i]] == a[u]) break; if (i <= top) { for (j = 1; j <= top; j++) e[j] = a[sta[j]]; t = 0; for (j = i; j <= top; j++) a[sta[j]] = e[++t]; for (j = 1; j <= i - 1; j++) a[sta[j]] = e[++t]; if (pd()) { sum = num + (long long)(top + 1) * (i - 1); if (sum < ans) { ansu = k; ansv = u; ans = sum; } r = lca(u, T); sum = num + (long long)(top + 1) * (top - i + 1); if (d[r] > d[k]) sum -= (long long)2 * (d[r] - d[k]); if (sum < ans) { ansu = k; ansv = u; ans = sum; } } } } else { w = lca(u, v); l = w; while (!bz[l]) l = fa[l]; num += 2 * (d[w] - d[l]); top = 0; k = u; while (k != w) { if (fa[k] == w) r = k; sta[++top] = k; k = fa[k]; } reverse(sta + 1, sta + top + 1); k = v; while (k != w) { sta[++top] = k; k = fa[k]; } for (i = 1; i <= top + 1; i++) if (i > top || b[sta[i]] == a[r]) break; if (i <= top) { for (j = 1; j <= top; j++) e[j] = a[sta[j]]; t = 0; for (j = i; j <= top; j++) a[sta[j]] = e[++t]; for (j = 1; j <= i - 1; j++) a[sta[j]] = e[++t]; if (pd()) { r = lca(u, T); sum = num + (long long)(top + 1) * (i - 1); if (d[r] > d[w]) sum -= (long long)2 * (d[r] - d[w]); if (sum < ans) { ansu = u; ansv = v; ans = sum; } r = lca(v, T); sum = num + (long long)(top + 1) * (top - i + 1); if (d[r] > d[w]) sum -= (long long)2 * (d[r] - d[w]); if (sum < ans) { ansu = u; ansv = v; ans = sum; } } } } if (ans == inf) printf("-1\n"); else { if (ansu > ansv) swap(ansu, ansv); printf("%d %d %lld\n", ansu, ansv, ans); } }
13
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int count = 0; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'H' || s[i] == 'Q' || s[i] == '9') { count++; } } if (count >= 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct node { int to; long long val; }; struct no { int x, y; long long val; } bi[maxn]; struct cmp1 { bool operator()(const node &a, const node &b) { return a.val > b.val; } }; bool cmp(const no &a, const no &b) { return a.val < b.val; } int n, m, k, p[maxn], bcj[maxn], cnt, ne[maxn]; long long dist[maxn], inf; bool cs[maxn], us[maxn]; vector<node> q[maxn]; priority_queue<node, vector<node>, cmp1> p1; int find(int x) { return x == bcj[x] ? x : bcj[x] = find(bcj[x]); } void dijkstra() { while (!p1.empty()) { node t = p1.top(); p1.pop(); int u = t.to; int len = q[u].size(); for (int j = 0; j < len; j++) { int to = q[u][j].to; long long val = q[u][j].val; if (dist[to] > dist[u] + val) { dist[to] = dist[u] + val; ne[to] = ne[u]; p1.push((node){to, dist[to]}); } } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; long long v; scanf("%d%d%lld", &x, &y, &v); q[x].push_back((node){y, v}); q[y].push_back((node){x, v}); bi[i] = (no){x, y, v}; } scanf("%d", &k); for (int i = 1; i <= k; i++) { int x; scanf("%d", &x); p[i] = x; cs[x] = 1; } memset(dist, 0x3f, sizeof(dist)); inf = dist[0]; memset(us, 0, sizeof(us)); memset(ne, 0, sizeof(ne)); dist[1] = 0; p1.push((node){1, 0}); dijkstra(); while (!p1.empty()) p1.pop(); long long ans = inf; for (int i = 1; i <= k; i++) ans = min(ans, dist[p[i]]); memset(dist, 0x3f, sizeof(dist)); for (int i = 1; i <= k; i++) { p1.push((node){p[i], 0}); dist[p[i]] = 0; ne[p[i]] = p[i]; } dijkstra(); for (int i = 1; i <= n; i++) bcj[i] = i; for (int i = 1; i <= m; i++) { bi[i].val += dist[bi[i].x] + dist[bi[i].y]; bi[i].x = ne[bi[i].x], bi[i].y = ne[bi[i].y]; } sort(bi + 1, bi + m + 1, cmp); int co = 0; for (int i = 1; i <= m; i++) { int x = find(bi[i].x), y = find(bi[i].y); if (x == y) continue; co++; ans += bi[i].val; bcj[x] = y; if (co == n - 1) break; } printf("%lld\n", ans); return 0; }
9
#include <bits/stdc++.h> const int MAXN = 511; const int MOD = 1000000007; inline int sum(const int &a, const int &b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); } inline int mul(const int &a, const int &b) { return (int)((1LL * a * b) % (long long)(MOD)); } inline void add(int &f, const int &v) { f += v; if (f >= MOD) f -= MOD; } inline int inv(const int &a); int pow(int a, int k) { if (k < 0) return pow(inv(a), -k); int r = 1; while (k) { if (k & 1) r = mul(r, a); a = mul(a, a); k >>= 1; } return r; } inline int inv(const int &a) { return pow(a, MOD - 2); } int N, K; int C[MAXN][MAXN]; struct Vert { int FE; int Size; int F[MAXN], G[MAXN]; } V[MAXN]; struct Edge { int y, next; }; Edge E[MAXN << 1]; int Ecnt; void addE(int a, int b) { ++Ecnt; E[Ecnt].y = b; E[Ecnt].next = V[a].FE; V[a].FE = Ecnt; } void DFS(int at, int f = 0) { V[at].Size = 1; V[at].F[0] = V[at].G[0] = 1; for (int k = V[at].FE, to; k; k = E[k].next) { to = E[k].y; if (to == f) continue; DFS(to, at); for (int i = V[at].Size - 1; i >= 0; --i) { for (int j = 1; j < V[to].Size; ++j) { add(V[at].F[i + j + 1], mul(V[at].F[i], V[to].F[j])); add(V[at].G[i + j + 1], mul(V[at].G[i], V[to].F[j])); add(V[at].F[i + j], sum(mul(V[at].F[i], V[to].G[j]), mul(V[at].G[i], V[to].F[j]))); add(V[at].G[i + j], mul(V[at].G[i], V[to].G[j])); } add(V[at].F[i + 1], mul(V[at].F[i], V[to].F[0])); add(V[at].G[i + 1], mul(V[at].G[i], V[to].F[0])); V[at].F[i] = sum(mul(V[at].F[i], V[to].G[0]), mul(V[at].G[i], V[to].F[0])); V[at].G[i] = mul(V[at].G[i], V[to].G[0]); } V[at].Size += V[to].Size; } } int Ans[MAXN], ANS[MAXN]; int main() { scanf("%d", &N); for (int i = 0; i <= N; ++i) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; ++j) { C[i][j] = sum(C[i - 1][j], C[i - 1][j - 1]); } } for (int i = 1, a, b; i < N; ++i) { scanf("%d%d", &a, &b); addE(a, b); addE(b, a); } DFS(1); for (int i = 0; i < N; ++i) Ans[i] = mul(pow(N, N - i - 2), V[1].F[N - 1 - i]); for (int k = 0; k < N; ++k) { for (int i = k, t = 1; i < N; ++i, t = MOD - t) { add(ANS[k], mul(mul(t, C[i][k]), Ans[i])); } } for (int i = 0; i < N; ++i) printf("%d ", ANS[i]); puts(""); return 0; }
9
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, s; cin >> n >> s; s--; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int t = 0; if (a[s] != 0) { a[s] = 0; t++; } sort(a.begin(), a.end()); int cnt = 0; int it = 0; vector<bool> used(n, 0); int ans = 1e9; int cnt1 = 0; for (int h = 0; h < n; h++) { cnt++; while (it < n && a[it] <= h) { if (!used[a[it]]) { cnt--; used[a[it]] = 1; } else if (a[it] == 0) { cnt1++; } it++; } if (h != 0 || cnt1 == 0) { ans = min(ans, max(cnt1 + n - it, cnt)); } } cout << ans + t; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool vis[150002]; int cnt[150002]; vector<int> edge[150002]; int nd, ed; int main() { string str[100]; char j = 'A', k; for (int i = 1; i <= 26; i++, j++) str[i] = j; j = 'a', k = 'A'; for (int i = 27; i <= 52; i++, j++, k++) { str[i] = k; str[i] += j; } double L, H, W; int n, m; while (cin >> n >> m) { string st; vector<string> ans; int k = 1; for (int i = 0; i < n - m + 1; i++) { cin >> st; if (i == 0) { if (st == "YES") { for (int j = i; j < i + m; j++) { ans.push_back(str[k++]); } } else { for (int j = i; j < i + m - 1; j++) { ans.push_back(str[k++]); } ans.push_back(str[1]); } } else { if (st == "YES") { ans.push_back(str[k++]); } else { string ss = ans[i]; ans.push_back(ss); } } } for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' '; cout << endl; } return 0; }
3
#include <bits/stdc++.h> int caseno = 1; using namespace std; typedef unsigned long long ULL; typedef unsigned long long uint64; typedef long long LL; typedef long long int64; template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline T MAX(T a, T b) { return (a > b) ? a : b; } template <class T> inline T MIN(T a, T b) { return (a < b) ? a : b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T fABS(T a) { return a < 0 ? a * (-1) : a; } template <class T> inline void SWAP(T &a, T &b) { T t = a; a = b; b = t; } inline LL POW(LL base, LL power) { LL I, res = base; if (power == 0) return 1; for (I = 0; I < power - 1; I++) res *= base; return res; } bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; } bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; } bool isVowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); } bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; } bool isDigit(char c) { return c >= '0' && c <= '9'; } char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; } char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } double toDouble(string s) { double r = 0; istringstream sin(s); sin >> r; return r; } string toString(int n) { string s; stringstream convert; convert << n; s = convert.str(); return s; } const double PI = 2 * acos(0.0); const double EPS = 1e-11; const int SIZE = 1e6; string S; int f[30]; int main() { std::ios_base::sync_with_stdio(0); int tcases, I, J, K, N, n, m, cnt = 0, len; cin >> len >> S; for (I = 0; I < len; I++) { S[I] = toLowerCase(S[I]); f[S[I] - 'a']++; } for (I = 0; I < 26; I++) if (f[I] == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; return 0; }
0
#include <bits/stdc++.h> const double eps = 1e-7, PI = 3.1415926; const long long N = 2e5 + 10; using namespace std; long long n, q, m, k, x, y, a[N], mx = -1, mn = 1e9, sum; char c[N]; string s, s1, s2; map<long long, long long> mp; vector<long long> vec; int32_t main() { cin >> q; while (q--) { cin >> x >> y >> k; sum = k + y * k - 1; sum = (sum + x - 2) / (x - 1); y = 0; cout << sum + k << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int dp[maxn], n; int main() { ios_base::sync_with_stdio(false); cin.tie(0); dp[0] = 0; for (int i = 1; i < maxn; i++) { int a = i; dp[i] = maxn; while (a) { (dp[i]) = min((dp[i]), (dp[i - (a % 10)] + 1)); a /= 10; } } cin >> n; cout << dp[n] << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; t = 1; while (t--) { long long n, k; cin >> n >> k; string s; cin >> s; long long sum = 0; vector<long long> data(200, 0); for (int i = 0; i < k; i++) { char temp; cin >> temp; data[int(temp)] = 1; } long long l_sofar = 0; for (int i = 0; i < n; i++) { if (data[int(s[i])] == 1) { l_sofar++; } else { sum += (l_sofar * (l_sofar + 1)) / 2; l_sofar = 0; } } sum += (l_sofar * (l_sofar + 1)) / 2; cout << sum; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, r, d; cin >> n >> k; if (n < k) x = k; else if (n == k) x = n * 2; else { r = n % k; d = k - r; x = n + d; } cout << x << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 1; long long n, ans = 1e18 + 7, num, t, a[maxn]; long long cal(long long x) { long long res = 0, sum = 0; for (long long i = 1; i <= n; ++i) sum = (sum + a[i]) % x, res += min(sum, x - sum); return res; } int main() { scanf("%lld", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); num += a[i]; } if (num == 0) { puts("0"); return 0; } if (num == 1) { puts("-1"); return 0; } t = sqrt(num); for (long long i = 2; i <= t; ++i) { if (num % i == 0) { ans = min(ans, cal(i)); while (num % i == 0) num /= i; } } if (num > 1) ans = min(ans, cal(num)); printf("%lld\n", ans); }
6
#include <bits/stdc++.h> using namespace std; long long ans; map<int, int> s; int main() { int n, m; scanf("%d", &n); for (int i = 1; i <= n; ++i) { int x, y; scanf("%d%d", &x, &y); s[x] = y; } scanf("%d", &m); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); if (s.count(x)) s[x] = max(s[x], y); else s[x] = y; } for (map<int, int>::iterator it = s.begin(); it != s.end(); ++it) ans += (long long)it->second; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int read() { char ch; while (!isdigit(ch = getchar())) ; int x = ch - '0'; while (isdigit(ch = getchar())) x = x * 10 + ch - '0'; return x; } const int MAXN = 2e5 + 5; const int INF = 1e9; int n, a[MAXN], S, f[MAXN], sz[MAXN], rt, ans; bool vis[MAXN]; vector<int> P[MAXN], G[MAXN]; void get_root(int u, int fa_u) { sz[u] = 1; f[u] = 0; for (int i = 0; i < (int)G[u].size(); ++i) { int v = G[u][i]; if (v == fa_u || vis[v]) continue; get_root(v, u); sz[u] += sz[v]; f[u] = max(f[u], sz[v]); } f[u] = max(f[u], S - sz[u]); if (f[u] < f[rt]) rt = u; } int dfs(int u, int fa_u, int g) { if (a[u] % g) return 0; int k = 1; for (int i = 0; i < (int)G[u].size(); ++i) { int v = G[u][i]; if (v == fa_u || vis[v]) continue; k = max(k, dfs(v, u, g) + 1); } return k; } void dfz(int u, int fa_u) { sz[u] = 1; for (int i = 0; i < (int)G[u].size(); ++i) { int v = G[u][i]; if (v == fa_u || vis[v]) continue; dfz(v, u); sz[u] += sz[v]; } } void work(int u) { vis[u] = 1; dfz(u, 0); for (int i = 0; i < (int)P[a[u]].size(); ++i) { int t1 = 0, t2 = 0; for (int j = 0; j < (int)G[u].size(); ++j) { if (vis[G[u][j]]) continue; int k = dfs(G[u][j], u, P[a[u]][i]); if (k > t1) t2 = t1, t1 = k; else if (k > t2) t2 = k; } ans = max(ans, t1 + t2 + 1); } for (int i = 0; i < (int)G[u].size(); ++i) { int v = G[u][i]; if (vis[v]) continue; if (sz[v] < ans) continue; f[rt = 0] = INF; S = sz[v]; get_root(v, 0); work(rt); } } int main() { ios::sync_with_stdio(0); for (int i = 2; i <= 200000; ++i) { if (!vis[i]) { for (int j = 1; j * i <= 200000; ++j) { vis[j * i] = 1; P[j * i].push_back(i); } } } memset(vis, 0, sizeof(vis)); n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1; i < n; ++i) { int u = read(), v = read(); G[u].push_back(v); G[v].push_back(u); } f[rt = 0] = INF; S = n; get_root(1, 0); work(rt); cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int moves[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool within(pair<int, int> p, int n, int m) { int x = p.first, y = p.second; if (x < 0 || x >= n || y < 0 || y >= m) return false; else return true; } vector<pair<int, int> > neighbours(pair<int, int> point, vector<string>& M, int n, int m) { vector<pair<int, int> > res; pair<int, int> p; for (int i = 0; i < 4; i++) { p = make_pair(point.first + moves[i][0], point.second + moves[i][1]); if (within(p, n, m)) { char c = M[p.first][p.second]; if ((c >= '0' && c <= '9') || c == 'S' || c == 'E') res.push_back(p); } } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<string> maze(n); pair<int, int> start, finish; map<pair<int, int>, int> breeders; for (int i = 0; i < maze.size(); i++) { cin >> maze[i]; for (int j = 0; j < maze[i].size(); j++) { if (maze[i][j] == 'S') start = make_pair(i, j); else if (maze[i][j] == 'E') finish = make_pair(i, j); else if (maze[i][j] >= '1' && maze[i][j] <= '9') breeders[make_pair(i, j)] = maze[i][j] - '0'; } } queue<pair<int, int> > cola; vector<vector<int> > moves(n, vector<int>(m, 1e9)); moves[finish.first][finish.second] = 0; cola.push(finish); while (!cola.empty()) { pair<int, int> v = cola.front(); cola.pop(); vector<pair<int, int> > vecinos = neighbours(v, maze, n, m); for (int i = 0; i < vecinos.size(); i++) { pair<int, int> n = vecinos[i]; if (moves[n.first][n.second] > moves[v.first][v.second] + 1) { moves[n.first][n.second] = moves[v.first][v.second] + 1; cola.push(n); } } } int mymin = moves[start.first][start.second], res = 0; for (auto& breeder : breeders) { if (moves[breeder.first.first][breeder.first.second] <= mymin) res += breeder.second; } cout << res << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int res = 0; int bit_shift = 0; while (n != 0) { if (n % 10 == 4) { res += 1 * (1 << bit_shift); bit_shift++; n /= 10; } if (n % 10 == 7) { res += 2 * (1 << bit_shift); bit_shift++; n /= 10; } } cout << res << endl; }
1
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 2e5 + 5; struct ele { int l, r; bool operator<(const ele A) const { return l < A.l; } } t[N * 4]; int a[N], b[N]; int n, k, tot; long long sum; int main() { read(n); read(k); for (int i = 1; i <= k; i++) { read(a[i]); read(b[i]); sum += b[i]; } if (sum > n) { puts("-1"); return 0; } if (sum < n) { puts("1"); return 0; } sum = 0; for (int i = 1; i <= k; i++) sum = (sum + 1ll * a[i] * b[i]) % n; if (sum == (1ll * n * (n + 1) / 2) % n) puts("1"); else puts("-1"); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 1000100, M = 100100, K = 1000100, mod = 1000000007; int n, m, k, l = N, r = 0, tot = 0, p[K] = {1}; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1, u, v; i <= m; ++i) { scanf("%d%d", &u, &v); if (v - u > 1) { if (v - u != k + 1) { puts("0"); return 0; } l = min(l, u); r = max(r, u); ++tot; } } for (int i = 1; i <= k; ++i) p[i] = p[i - 1] * 2 % mod; if (l == N && r == 0) { int ans = 1; for (int i = 1; i <= n - k + 1; ++i) ans = (ans + p[min(k, n - i - k - 1)]) % mod; printf("%d\n", ans); } else { int ans = 0; for (int i = 1; i <= n - k + 1; ++i) if (i <= l && i + k >= r) ans = (ans + p[min(k, n - i - k - 1) - tot + (i == l)]) % mod; printf("%d\n", ans); } return 0; }
7
#include <bits/stdc++.h> struct { inline operator int() { int x; return scanf("%d", &x), x; } } read; const int maxn = 100005; std::vector<int> factor[maxn]; bool has[maxn]; int stack[maxn], sp; int tot[maxn]; int mu[maxn], minp[maxn]; int query(int x) { int res = 0; for (int d : factor[x]) res += mu[d] * tot[d]; return res; } void insert(int x) { for (int d : factor[x]) ++tot[d]; } void erase(int x) { for (int d : factor[x]) --tot[d]; } int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { int n = 0; for (int i = read; i; i--) { int x = read; has[x] = 1; n = std::max(n, x); } mu[1] = 1; minp[1] = 1; for (int i = 1; i <= n; i++) { if (!minp[i]) minp[i] = i; if (minp[i] != minp[i / minp[i]]) mu[i] = -mu[i / minp[i]]; for (int j = i; j <= n; j += i) { factor[j].push_back(i); has[i] |= has[j]; if (!minp[j] and i > 1) minp[j] = i; } } long long ans = n; for (int x = n; x; x--) if (has[x]) { while (sp and query(x) - (gcd(x, stack[sp]) == 1) > 0) erase(stack[sp--]); if (sp) ans = std::max(ans, 1ll * x * stack[sp] / gcd(x, stack[sp])); insert(stack[++sp] = x); } printf("%lld\n", ans); }
10
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); void solve() { long long n, a, q; cin >> n >> q; set<long long> s; multiset<long long> diff; vector<long long> v(n); long long maxi = 0, mini = INT_MAX; for (long long i = 0; i < n; i++) { cin >> v[i]; s.insert(v[i]); maxi = max(maxi, v[i]); mini = min(mini, v[i]); } sort(v.begin(), v.end()); for (long long i = 1; i < n; i++) diff.insert(v[i] - v[i - 1]); long long x; if (n == 1) { cout << 0 << "\n"; } else { cout << maxi - mini - *prev(diff.end()) << endl; } while (q--) { cin >> a >> x; if (a == 0) { auto it = s.find(x); if (s.size() == 1) { s.clear(); cout << 0 << "\n"; diff.clear(); continue; } if (s.size() == 2) { s.erase(x); diff.clear(); cout << 0 << "\n"; continue; } if (it == s.begin()) { diff.erase(diff.find(*next(it) - *it)); } else if (it == prev(s.end())) { diff.erase(diff.find(*it - *prev(it))); } else { diff.erase(diff.find(*next(it) - *it)); diff.erase(diff.find(*it - *prev(it))); diff.insert(*next(it) - *prev(it)); } s.erase(it); cout << *prev(s.end()) - *s.begin() - *prev(diff.end()) << "\n"; } if (a == 1) { s.insert(x); auto it = s.find(x); if (s.size() == 1) { cout << 0 << "\n"; continue; } if (s.size() == 2) { cout << 0 << "\n"; diff.clear(); diff.insert(*prev(s.end()) - *s.begin()); continue; } if (it == s.begin()) { diff.insert(*next(it) - *it); } else if (it == prev(s.end())) { diff.insert(*it - *prev(it)); } else { diff.erase(diff.find(*next(it) - *prev(it))); diff.insert(*it - *prev(it)); diff.insert(*next(it) - *it); } cout << *prev(s.end()) - *s.begin() - *prev(diff.end()) << "\n"; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } }
6
#include <bits/stdc++.h> const int MAXAi = 1000005; const int GCD_LIM = 1000000; const int MOD = 1000000007; int N, cnt[MAXAi] = {0}, sum[MAXAi] = {0}; inline void plus(int& x, int d) { x = (x + d) % MOD; } inline void subtrac(int& x, int d) { x = (x + MOD - d) % MOD; } inline int pow(int bs, int ex) { if (ex <= 0) return 1; int res = 1; for (; ex; ex >>= 1, bs = (long long)bs * bs % MOD) if (ex & 1) res = (long long)res * bs % MOD; return res; } int main() { scanf("%d", &N); int i, j, Ai, res = 0; for (i = 0; i < N; i++) { scanf("%d", &Ai); ++cnt[Ai]; } for (i = GCD_LIM; i > 1; i--) { int tot = 0; for (j = i; j <= GCD_LIM; j += i) { subtrac(sum[i], sum[j]); tot += cnt[j]; } plus(sum[i], (long long)tot * pow(2, tot - 1) % MOD); plus(res, (long long)i * sum[i] % MOD); } printf("%d\n", res); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; long long arr[n + m], ta[n + m], mx[n + m], l = -1, s = -1, ff[n + m], in1 = -1, in2 = -1; for (int i = 0; i < n + m; i++) { cin >> arr[i]; } for (int i = 0; i < n + m; i++) { cin >> ta[i]; if (ta[i] && l == -1) { l = i; s = i; } else if (ta[i]) s = i; } for (int i = 0; i < n + m; i++) { if (!ta[i]) { mx[i] = abs(arr[l] - arr[i]); ff[i] = l; } else l = i; } for (int i = n + m - 1; i >= 0; i--) { if (!ta[i]) { if (mx[i] > abs(arr[s] - arr[i])) { mx[i] = abs(arr[s] - arr[i]); ff[i] = s; } } else s = i; } int c = 0, ans[m + n]; memset(ans, 0, sizeof ans); for (int i = 0; i < n + m; i++) { if (!ta[i]) { ans[ff[i]]++; } } for (int i = 0; i < m + n; i++) if (ta[i]) cout << ans[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, pos; vector<int> b; vector<vector<int>> a, c, tmp; vector<pair<int, int>> ans; void app(int tp, int ind) { ans.push_back({tp, ind}); if (tp) for (int j = 0; j < n; j++) tmp[ind][j] ^= b[j]; else for (int j = 0; j < n; j++) tmp[j][ind] ^= b[j]; } void f() { for (int i = 0; i < n; i++) { if (tmp[i][pos] == c[i][pos]) continue; app(1, i); } for (int i = 0; i < n; i++) { if (i == pos) continue; bool tr = 0; for (int j = 0; j < n; j++) if (tmp[j][i] != c[j][i]) tr = 1; if (tr) app(0, i); } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (tmp[i][j] != c[i][j]) return; cout << (int)ans.size() << '\n'; for (auto i : ans) { if (i.first) cout << "row "; else cout << "col "; cout << i.second << '\n'; } exit(0); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; a.resize(n), c.resize(n); for (int i = 0; i < n; i++) { a[i].resize(n); for (int j = 0; j < n; j++) { char t; cin >> t; if (t == '0') a[i][j] = 0; else a[i][j] = 1; } } for (int i = 0; i < n; i++) { c[i].resize(n); for (int j = 0; j < n; j++) { char t; cin >> t; if (t == '0') c[i][j] = 0; else c[i][j] = 1; } } b.resize(n); pos = -1; for (int i = 0; i < n; i++) { char t; cin >> t; if (t == '0') b[i] = 0; else b[i] = 1, pos = i; } if (pos == -1) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (a[i][j] != c[i][j]) return cout << -1, 0; return cout << 0, 0; } tmp = a; f(); ans.clear(); tmp = a; app(0, pos); f(); cout << -1; }
6
#include <bits/stdc++.h> using namespace std; const int hashP = 239017; const int N = 1e5 + 10; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int, int> > a(n); for (auto& now : a) cin >> now.first; int q; cin >> q; vector<int> d(q + 11); int mx = 0, last = 0; for (int i = 0; i < q; i++) { int ch = 0; cin >> ch; if (ch == 1) { int x, p; cin >> p >> x; a[p - 1].first = x; a[p - 1].second = i + 1; } else { int x; cin >> x; d[i + 1] = x; } } for (int i = q - 1; i >= 0; i--) d[i] = max(d[i], d[i + 1]); for (auto now : a) { cout << max(now.first, d[now.second]) << " "; } return 0; }
4
#include <bits/stdc++.h> int n, m, q, map[2][2][100001], f[100001][2]; char Getchar() { char ch = getchar(); putchar(ch); putchar('\n'); return ch; } int read() { char ch = getchar(); while (ch != 'R' && ch != 'B') ch = getchar(); return (ch == 'B'); } struct solve { int cnt[2][100001], S, sum[800001]; bool lazy[800001][2]; struct info { int f[2][2]; info() { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) f[i][j] = 10000000; } } se[800001][2][2]; info merge(info a, info b) { info c; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) for (int l = 0; l < 2; l++) c.f[i][l] = std::min(c.f[i][l], a.f[i][j] + b.f[k][l] + (k != j) * S); return c; } void pushdown(int x, int ind) { if (ind) { for (int i = 0; i < 2; i++) std::swap(se[x][i][ind], se[x][i][ind ^ 1]); } else { for (int i = 0; i < 2; i++) std::swap(se[x][ind][i], se[x][ind ^ 1][i]); } lazy[x][ind] ^= 1; } void spread(int root) { for (int i = 0; i < 2; i++) { if (lazy[root][i]) pushdown(root << 1, i), pushdown(root << 1 | 1, i); lazy[root][i] = 0; } } void pushup(int x) { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) se[x][i][j] = merge(se[x << 1][i][j], se[x << 1 | 1][i][j]); } void build(int root, int l, int r, int *a, int *b) { if (l == r) { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) { se[root][i][j].f[k][k] = cnt[k][l]; if (i) se[root][i][j].f[k][k] += (b[l] == k) ? -1 : 1; if (j) se[root][i][j].f[k][k] += (a[l] == k) ? -1 : 1; } return; } build(root << 1, l, (l + r) >> 1, a, b); build(root << 1 | 1, ((l + r) >> 1) + 1, r, a, b); pushup(root); } int getans() { int cnt = 0x7f7f7f7f; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cnt = std::min(cnt, se[1][0][0].f[i][j]); return cnt; } void update(int root, int l, int r, int el, int er, int ind) { if (el > r || er < l) return; if (el <= l && er >= r) { pushdown(root, ind); return; } spread(root); update(root << 1, l, (l + r) >> 1, el, er, ind); update(root << 1 | 1, ((l + r) >> 1) + 1, r, el, er, ind); pushup(root); } void upd(int root, int l, int r, int e, int y) { spread(root); if (l == r) { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) se[root][i][j].f[0][0] -= y, se[root][i][j].f[1][1] += y; return; } if ((l + r) >> 1 >= e) upd(root << 1, l, (l + r) >> 1, e, y); else upd(root << 1 | 1, ((l + r) >> 1) + 1, r, e, y); pushup(root); } } N, M; struct segtree { int se[800001], sz[800001]; bool lazy[800001]; void pushdown(int x) { se[x] = sz[x] - se[x]; lazy[x] ^= 1; } void spread(int x) { if (lazy[x]) pushdown(x << 1), pushdown(x << 1 | 1); lazy[x] = 0; } void pushup(int x) { se[x] = se[x << 1] + se[x << 1 | 1]; } void build(int root, int l, int r, int *a) { if (l == r) { se[root] = a[l]; sz[root] = 1; return; } build(root << 1, l, (l + r) >> 1, a); build(root << 1 | 1, ((l + r) >> 1) + 1, r, a); sz[root] = sz[root << 1] + sz[root << 1 | 1]; pushup(root); } int update(int root, int l, int r, int el, int er) { if (el > r || er < l) return 0; if (el <= l && er >= r) { pushdown(root); return se[root]; } spread(root); int cnt = 0; cnt += update(root << 1, l, (l + r) >> 1, el, er); cnt += update(root << 1 | 1, ((l + r) >> 1) + 1, r, el, er); pushup(root); return cnt; } } se[2][2]; int main() { scanf("%d%d%d", &n, &m, &q); N.S = m; M.S = n; for (int j = 0; j < 2; j++) for (int i = 1; i <= n; i++) { map[0][j][i] = read(); ++N.cnt[map[0][j][i]][i]; ++M.cnt[map[0][j][i]][(j == 0) ? 1 : m]; } for (int j = 0; j < 2; j++) for (int i = 1; i <= m; i++) { map[1][j][i] = read(); ++M.cnt[map[1][j][i]][i]; ++N.cnt[map[1][j][i]][(j == 0) ? 1 : n]; } N.build(1, 1, n, map[0][1], map[0][0]); M.build(1, 1, m, map[1][1], map[1][0]); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) se[i][j].build(1, 1, i ? m : n, map[i][j]); for (int i = 0; i <= q; i++) { printf("%d\n", std::min((n + m) << 1, std::min(N.getans(), M.getans()))); if (i < q) { char ch = getchar(); while (ch < 'A' || ch > 'Z') ch = getchar(); int p1, p2, L, R; if (ch == 'L') p1 = p2 = 0; else if (ch == 'R') p1 = 0, p2 = 1; else if (ch == 'U') p1 = 1, p2 = 0; else p1 = p2 = 1; scanf("%d%d", &L, &R); int u = se[p1][p2].update(1, 1, p1 ? m : n, L, R); u = (u << 1) - (R - L + 1); if (!p1) { N.update(1, 1, n, L, R, p2); M.upd(1, 1, m, (p2 == 0) ? 1 : m, u); } else { M.update(1, 1, m, L, R, p2); N.upd(1, 1, n, (p2 == 0) ? 1 : n, u); } } } }
12
#include <bits/stdc++.h> using namespace std; int Scan() { int res = 0, flag = 0; char ch; if ((ch = getchar()) == '-') { flag = 1; } else if (ch >= '0' && ch <= '9') { res = ch - '0'; } while ((ch = getchar()) >= '0' && ch <= '9') { res = res * 10 + (ch - '0'); } return flag ? -res : res; } void Out(int a) { if (a < 0) { putchar('-'); a = -a; } if (a >= 10) { Out(a / 10); } putchar(a % 10 + '0'); } void test1() { for (int i = 0; i < 100; ++i) ; } void test2() { for (int i = 0; i < 1000; ++i) ; } void checksta() {} int x, y, p, q; int n, m; long long a[1000005]; long long bin[100]; int flag[100]; int num[1000005]; int maxx[1000005]; vector<long long> ha; vector<int> ans; int main() { scanf("%d", &n); bin[0] = 1; for (int i = 1; i <= 60; i++) bin[i] = bin[i - 1] * 2ll; memset(flag, 0, sizeof flag); memset(num, 0, sizeof num); int sum = 0; ha.clear(); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); int f = 0; for (int j = 0; j <= 60; j++) { if (bin[j] == a[i]) flag[j]++, sum++, f = 1; } if (f == 0) ha.push_back(a[i]); } sum = n - sum; int cun = 0; while (flag[0] > 0) { cun++; int cur = 0; while (flag[cur] > 0) flag[cur]--, cur++, num[cun]++; maxx[cun] = cur; } for (int i = 0; i <= 60; i++) { sum += flag[i]; while (flag[i] > 0) flag[i]--, ha.push_back(bin[i]); } sort(ha.begin(), ha.end()); if (cun == 0) { cout << -1 << endl; return 0; } int j = 1; for (int i = (int)ha.size() - 1; i >= 0; i--, j++) if (j > cun || bin[maxx[j]] < ha[i]) { cout << -1 << endl; return 0; } ans.clear(); while (cun >= sum) { ans.push_back(cun); sum += num[cun]; cun--; } sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); puts(""); return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 100; long long mark[N], a[N], b[N]; vector<long long> g[N], res; void dfs(long long v, long long x, long long y) { mark[v] = 1; if (a[v] ^ x != b[v]) { res.push_back(v); x ^= 1; } for (long long u : g[v]) { if (!mark[u]) dfs(u, y, x); } return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, u, v, x = 0, y = 0; cin >> n; for (long long i = 1; i < n; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (long long i = 1; i <= n; i++) { cin >> a[i]; } for (long long i = 1; i <= n; i++) { cin >> b[i]; } dfs(1, x, y); cout << res.size() << "\n"; for (long long i = 0; i < res.size(); i++) cout << res[i] << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000000000000000LL; const long long NMAX = 1e6 + 4; const long long mod = 1e9 + 7; const double eps = 1e-10; const double PI = acos(-1); vector<pair<long long, long long> > pairs1, pairs2; long long N, M; set<long long> solns1[14]; set<long long> solns2[14]; long long match(pair<long long, long long> a, pair<long long, long long> b) { if (a == b) return -1; if (a.first == b.first || a.first == b.second) return a.first; if (a.second == b.first || a.second == b.second) return a.second; return -1; } signed main() { ios::sync_with_stdio(false); cin >> N >> M; pairs1.assign(N, {0, 0}); pairs2.assign(M, {0, 0}); for (long long i = 0; i < N; i++) { long long a, b; cin >> a >> b; pairs1[i].first = min(a, b); pairs1[i].second = max(a, b); } for (long long i = 0; i < M; i++) { long long a, b; cin >> a >> b; pairs2[i].first = min(a, b); pairs2[i].second = max(a, b); } for (long long i = 0; i < N; i++) { for (long long j = 0; j < M; j++) { long long k = match(pairs1[i], pairs2[j]); if (k != -1) solns1[i].insert(k); } } for (long long i = 0; i < M; i++) { for (long long j = 0; j < N; j++) { long long k = match(pairs2[i], pairs1[j]); if (k != -1) solns2[i].insert(k); } } for (long long i = 0; i < N; i++) { if (solns1[i].size() > 1) { cout << -1 << endl; return 0; } } for (long long i = 0; i < M; i++) { if (solns2[i].size() > 1) { cout << -1 << endl; return 0; } } long long soln = -1; for (long long i = 0; i < N; i++) { for (long long k : solns1[i]) { if (soln != -1 and k != soln) { cout << 0 << endl; return 0; } soln = k; } } for (long long i = 0; i < M; i++) { for (long long k : solns2[i]) { if (soln != -1 and k != soln) { cout << 0 << endl; return 0; } soln = k; } } cout << soln << endl; }
5
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) using namespace std; const int N = 2e5 + 5; pair<double, double> f[N]; double sum[N]; int main() { int n, q, l, r; double s, a, b, x, y; scanf("%lf %lf %lf", &s, &a, &b); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf %lf", &f[i].first, &f[i].second); sum[0] = 0; for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + f[i].second - f[i].first; } scanf("%d", &q); while (q--) { scanf("%lf %lf", &x, &y); double c1 = (a * y - s * x) / (y - s); double c2 = (b * y - s * x) / (y - s); int t = lower_bound(f + 1, f + n + 1, pair<double, double>(c1, 0)) - f; double ans = 0; if (t == 1) l = 1; else { l = t; if (c1 < f[t - 1].second) ans += f[t - 1].second - c1; } int tt = lower_bound(f + 1, f + n + 1, pair<double, double>(c2, 0)) - f; if (tt == 1) r = tt - 1; else { r = tt - 1; if (c2 < f[tt - 1].second) ans -= f[tt - 1].second - c2; } if (r >= l) ans += sum[r] - sum[l - 1]; printf("%.10f\n", ans * (y - s) / y); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5; map<int, pair<int, int> > dp; int a[N], par[N]; int main() { int n; scanf("%d", &n); int mx = 0, start; for (int i = 0, x; i < n; ++i) { scanf("%d", &a[i]); x = a[i]; if (!dp.count(x - 1)) { dp[x] = {1, i}; par[i] = i; } else { if (dp.count(x)) { if (dp[x].first < dp[x - 1].first + 1) dp[x] = {dp[x - 1].first + 1, i}; } else dp[x] = {dp[x - 1].first + 1, i}; par[i] = dp[x - 1].second; } if (dp[x].first > mx) { mx = dp[x].first; start = i; } } int id = start; vector<int> out; while (id != par[id]) { out.push_back(id); id = par[id]; } out.push_back(id); reverse(out.begin(), out.end()); printf("%d\n", (int)out.size()); for (int id : out) printf("%d ", id + 1); }
4
using namespace std; #include <iostream> #define MAXN 100005 #define MOD 1000000007 int N,K; long long fact[MAXN],invf[MAXN]; long long inv(long long a,long long b = MOD) { return 1 < a ? b - inv(b % a,a) * b / a : 1; } long long nCr(long long a,long long b) { return (fact[a] * invf[b] % MOD) * invf[a - b] % MOD; } long long inCr(long long a,long long b) { return (invf[a] * fact[b] % MOD) * fact[a - b] % MOD; } void solve() { cin >> N >> K; int ans = 1; for(int i = 1;i <= N && i + (i - 1) * (K - 1) <= N;++i) { ans += nCr(N - (i - 1) * (K - 1),i) * inCr(N,i) % MOD; if(ans >= MOD) ans -= MOD; } cout << ans << endl; return; } int main() { fact[0] = invf[0] = 1; for(int i = 1;i < MAXN;++i) { fact[i] = (i * fact[i - 1]) % MOD; invf[i] = (inv(i) * invf[i - 1]) % MOD; } int T; cin >> T; while(T--) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std ; using LL = long long ; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int test_cases ; cin >> test_cases ; for(int tc = 1 ; tc <= test_cases ; tc++){ int n, m ; char ch ; cin >> n >> m ; cin.ignore() ; vector<int> ar(n + 2) ; for(int i = 1 ; i <= n ; i++){ cin >> ch ; ar[i] = ch == '+' ? 1 : -1 ; } vector<pair<int, int> > pre (n + 2, make_pair(0, 0)) , suf (n + 2, make_pair(0, 0)) ; for(int i = n ; i >= 1 ; i--){ suf[i].first = max(0, suf[i + 1].first + ar[i]) ; suf[i].second = min(0, suf[i + 1].second + ar[i]) ; } for(int i = 1 ; i <= n ; i++){ ar[i] += ar[i-1] ; pre[i].first = max(pre[i-1].first, ar[i]) ; pre[i].second = min(pre[i-1].second, ar[i]) ; } int l, r, a, b, lo, hi ; while(m--){ cin >> l >> r ; l--, r++ ; hi = pre[l].first ; lo = pre[l].second ; hi = max(hi, ar[l] + suf[r].first) ; lo = min(lo, ar[l] + suf[r].second) ; cout << (hi - lo + 1) << endl ; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; string orig, s2; int n; bool check(string x, string y) { int a = 0, b = 0, n = x.length(); for (int i = 0; i < n; ++i) { if (x[i] == '+') ++a; else --a; if (y[i] == '+') ++b; else --b; } return (a == b); } int f(int i, string l) { if (i == n) return (check(orig, l)) ? 1 : 0; if (s2[i] == '?') { string l1 = l, l2 = l; l1[i] = '+'; l2[i] = '-'; return f(i + 1, l1) + f(i + 1, l2); } return f(i + 1, l); } int main(int argc, char const* argv[]) { cin.sync_with_stdio(0); cin.tie(0); cin >> orig >> s2; int q = 0; n = s2.length(); for (int i = 0; i < n; ++i) if (s2[i] == '?') ++q; int t = 1 << q; int l = f(0, s2); double sol = l * 1.0 / t; printf("%.9f\n", sol); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; const int MOD = 1e9 + 7; const int N = 2e5 + 10; int n, m, f[N] = {0}; void solve() { int h = 0; while (((h + 2) * (h + 1)) / 2 <= n + m) h++; int block = (h * (h + 1)) / 2; f[0] = 1; for (int i = 1; i <= h; i++) for (int j = n; j >= i; j--) f[j] = (f[j] + f[j - i]) % MOD; long long ans = 0; for (int i = max(block - m, 0); i <= n; i++) ans = (ans + f[i]) % MOD; cout << ans; } int main() { cin >> n >> m; if (n > m) swap(n, m); solve(); }
6
#include <bits/stdc++.h> using namespace std; bool ask(long long mid) { cout << mid << " " << 2 << endl << flush; string s; cin >> s; if (s == "black") return false; return true; } int main() { long long n; cin >> n; bool flg = 0; cout << "0 2" << endl << flush; string s; cin >> s; if (s == "black") flg = true; long long l = 0, r = (long long)1e9, ans = 0; for (int i = 1; i < n && l < r; i++) { long long mid = (l + r) / 2; if (mid == 0) break; if (ask(mid)) { if (!flg) { ans = mid; l = mid; } else { r = mid; } } else { if (!flg) { r = mid; } else { ans = mid; l = mid; } } } cout << ans << " " << 3 << " " << ans + 1 << " " << 1 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 3e6 + 10; int n; char s[N]; int f[10][10][10]; int main() { scanf("%s", s + 1); n = strlen(s + 1); memset(f, 0x3f, sizeof f); for (int i = 0; i <= 9; ++i) for (int j = 0; j <= 9; ++j) { for (int ii = 0; ii <= 9; ++ii) for (int jj = 0; jj <= 9; ++jj) if (!i || !j || ii || jj) { f[i][j][(i * ii + j * jj) % 10] = min(f[i][j][(i * ii + j * jj) % 10], ii + jj); } } for (int i = 0; i <= 9; ++i) { for (int j = 0; j <= 9; ++j) { int now = 0, ans = 0; for (int k = 1; k <= n; ++k) { if (k == 1 && s[k] == '0') continue; else if (k == 1) ++ans; int delta = (s[k] - '0' - now) % 10; if (delta < 0) delta += 10; if (s[k] == s[k - 1]) ans += max(0, f[i][j][0] - 1); else ans += max(0, f[i][j][delta] - 1); now = s[k] - '0'; if (ans >= INF - 10) break; } if (ans >= INF - 10) ans = -1; printf("%d ", ans); } putchar(10); } }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, x; cin >> n; x = 2; k = 1; for (int i = 0; i < n; i++) { cout << ((k * (k + 1) * (k + 1))) - (x / k) << endl; x = k * (k + 1); k++; } return 0; }
4
#include <bits/stdc++.h> char s[10010]; int main() { int i, j; gets(s); int len = strlen(s); for (i = 0; i < len; i++) { if (s[i] == ' ') { if (s[i + 1] == ' ') continue; else if (s[i + 1] == '.' || s[i + 1] == ',' || s[i + 1] == '!' || s[i + 1] == '?') continue; else printf("%c", s[i]); } else if (s[i] == '.' || s[i] == ',' || s[i] == '!' || s[i] == '?') { printf("%c", s[i]); if (s[i + 1] != ' ') printf(" "); } else printf("%c", s[i]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long ans = 0; priority_queue<long long, vector<long long>, greater<long long> > pq; long long temp; cin >> temp; pq.push(temp); for (int i = 1; i < n; i++) { cin >> temp; if (pq.top() < temp) { ans += temp - pq.top(); pq.pop(); pq.push(temp); pq.push(temp); } else pq.push(temp); } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 42; int a[maxn], b[maxn], k[maxn], p[maxn]; int n, u, r; long long ANS = -1e18; void go(int step, int x) { long long ans = 0; for (int i = 0; i < n; i++) { ans += (a[i] * 1LL * k[i]); } if ((u - step) % 2 == 0) ANS = max(ANS, ans); if (step == u) return; int c[33]; for (int i = 0; i < n; i++) c[i] = a[i]; if (x == 0) { for (int i = 0; i < n; i++) { a[i] = a[i] ^ b[i]; } go(step + 1, 1); } for (int i = 0; i < n; i++) { a[i] = c[p[i] - 1] + r; } go(step + 1, 0); return; } int main() { scanf("%d%d%d", &n, &u, &r); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); for (int i = 0; i < n; i++) scanf("%d", &k[i]); for (int i = 0; i < n; i++) scanf("%d", &p[i]); go(0, 0); cout << ANS; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, k; string s; long long solveB[1000013], solveW[1000013]; long long almostComplete[1000013], junk[1000013]; void solve(char targetChar, long long targetArray[1000013], long long almost[1000013]) { queue<long long> dpvals; long long pow = 1; long long zeroans = 1; long long dpans = 0; for (int i = 0; i < n; i++) { if (s[i] == 'X') { long long olddpans = dpans; dpans += zeroans; dpans %= 1000000007LL; dpvals.push(zeroans); if (dpvals.size() > k - 1) { dpans -= dpvals.front(); dpvals.pop(); dpans = ((dpans % 1000000007LL) + 1000000007LL) % 1000000007LL; } zeroans += olddpans; zeroans %= 1000000007LL; pow *= 2; pow %= 1000000007LL; } else if (s[i] == targetChar) { dpans += zeroans; dpans %= 1000000007LL; dpvals.push(zeroans); if (dpvals.size() > k - 1) { dpans -= dpvals.front(); dpvals.pop(); dpans = ((dpans % 1000000007LL) + 1000000007LL) % 1000000007LL; } zeroans = 0; } else { zeroans += dpans; zeroans %= 1000000007LL; dpans = 0; dpvals = queue<long long>(); } long long thisAns = pow - (zeroans + dpans); thisAns = ((thisAns % 1000000007LL) + 1000000007LL) % 1000000007LL; targetArray[i] = thisAns; almost[i] = (k == 1 ? zeroans : (dpvals.size() == k - 1 ? dpvals.front() : 0)); } } int main() { if (fopen("FILENAME.in", "r")) { freopen("FILENAME.in", "r", stdin); freopen("FILENAME.out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; cin >> s; solve('B', solveB, almostComplete); reverse(s.begin(), s.end()); solve('W', solveW, junk); reverse(s.begin(), s.end()); long long answer = 0; for (int i = 0; i < n - 1; i++) { if (s[i] != 'X' && s[i] != 'B') continue; long long lways = (i > 0 ? almostComplete[i - 1] : (k == 1 ? 1 : 0)); long long rways = solveW[n - i - 2]; answer += lways * rways; answer %= 1000000007LL; } cout << answer << endl; }
8
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { char c; bool nega = 0; while ((!isdigit(c = getchar())) && (c != '-')) ; if (c == '-') { nega = 1; c = getchar(); } x = c - 48; while (isdigit(c = getchar())) x = x * 10 + c - 48; if (nega) x = -x; } template <typename T> inline void writep(T x) { if (x > 9) writep(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } writep(x); } template <typename T> inline void writeln(T x) { write(x); putchar('\n'); } int a, b, c; int x, y, z; int main() { read(a); read(b); read(c); read(x); read(y); read(z); int temp = 0; if (a >= x) temp += (a - x) / 2; else temp -= x - a; if (b >= y) temp += (b - y) / 2; else temp -= y - b; if (c >= z) temp += (c - z) / 2; else temp -= z - c; if (temp >= 0) puts("Yes"); else puts("No"); }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const long long mod = 1e9 + 7; const double eps = 1e-6; const double PI = acos(-1); const double R = 0.57721566490153286060651209; template <typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } namespace FastIO { char gc(void) { const int S = 1 << 17; static char buf[S], *s = buf, *t = buf; if (s == t) t = buf + fread(s = buf, 1, S, stdin); if (s == t) return EOF; return *s++; } int read(void) { int a = 0, b = 1, c = gc(); for (; !isdigit(c); c = gc()) b ^= (c == '-'); for (; isdigit(c); c = gc()) a = a * 10 + c - '0'; return b ? a : -a; } } // namespace FastIO using namespace FastIO; long long quick_pow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans % mod; } namespace Comb { const int N = 1e6 + 10; long long F[N], invF[N], inv[N]; void init() { F[0] = F[1] = invF[0] = invF[1] = inv[0] = inv[1] = 1; for (int i = 2; i < N; i++) { F[i] = F[i - 1] * i % mod; inv[i] = (mod - mod / i) * inv[mod % i] % mod; invF[i] = invF[i - 1] * inv[i] % mod; } } long long C(long long m, long long n) { if (m < 0 || n < 0 || n > m) return 0; long long ans = F[m]; ans = ans * invF[n] % mod; ans = ans * invF[m - n] % mod; return ans; } long long Lucas(long long m, long long n) { return n ? Lucas(m / mod, n / mod) * C(m % mod, n % mod) % mod : 1; } } // namespace Comb long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } void solve() { int _; cin >> _; while (_--) { long long s, n, k; cin >> s >> n >> k; if (s == k) cout << "YES" << "\n"; else { long long ans = s / (k * 2) * k; long long ys = s % (k * 2) + 1; ans += min(k, ys); cout << (n + 1 > ans ? "YES" : "NO") << "\n"; } } } signed main() { ios_base::sync_with_stdio(false); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1111111, M = 11111111; int s[M], c[N], a[N], b[N]; bool cmp(int i, int j) { return a[i] < a[j]; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { int j; scanf("%d", &j); s[j]++; } for (int i = 10000000; i; i--) if (s[i] > k) { s[i - 1] += s[i] - k; s[i] = k; } if (s[0] > k) { printf("-1\n"); return 0; } for (int i = 0; i < m; i++) { scanf("%d", &a[i]); b[i] = i; } sort(b, b + m, cmp); int ans = 0; for (int i = 0, j = 0; i < m; i++) { for (; s[j] == k; j++) ; if (a[b[i]] >= j) { s[j]++; c[ans++] = b[i]; } } printf("%d\n", ans); for (int i = 0; i < ans; i++) { printf("%d ", c[i] + 1); } puts(""); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int r1, r2, c1, c2, d1, d2; set<int> ani; cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; int a11 = (r1 + c1 - d2) / 2; int a22 = d1 - a11; int a12 = r1 - a11; int a21 = c1 - a11; ani.insert(a11); ani.insert(a12); ani.insert(a21); ani.insert(a22); if (ani.size() == 4 && a11 >= 1 && a11 <= 9 && a12 >= 1 && a12 <= 9 && a21 >= 1 && a21 <= 9 && a22 >= 1 && a22 <= 9 && a21 + a22 == r2 && a12 + a22 == c2 && a12 + a21 == d2) { cout << a11 << " " << a12 << endl; cout << a21 << " " << a22 << endl; } else cout << "-1" << endl; return 0; }
1
#include <bits/stdc++.h> typedef struct link { int elemento; struct link *next; } Link; typedef struct list { Link *head; Link *tail; Link *curr; int cnt; } List; Link *create_link(int it, Link *nextval) { Link *n = (Link *)malloc(1 * sizeof(Link)); n->elemento = it; n->next = nextval; return n; } Link *create_link_nulo(Link *nextval) { Link *n = (Link *)malloc(1 * sizeof(Link)); n->next = nextval; return n; } List *create_list() { List *l = (List *)malloc(1 * sizeof(List)); l->curr = l->tail = l->head = create_link_nulo(NULL); l->cnt = 0; return l; } void insert(List *l, int it) { l->curr->next = create_link(it, l->curr->next); if (l->tail == l->curr) l->tail = l->curr->next; l->cnt++; } void append(List *l, int it) { l->tail->next = create_link(it, NULL); l->tail = l->tail->next; l->cnt++; } void moveToStart(List *l) { l->curr = l->head; } void moveToEnd(List *l) { l->curr = l->tail; } void prev(List *l) { if (l->curr != l->head) { Link *temp = l->head; while (temp->next != l->curr) { temp = temp->next; } l->curr = temp; } } void next(List *l) { if (l->curr != l->tail) l->curr = l->curr->next; } int length(List *l) { return l->cnt; } int currPos(List *l) { Link *temp = l->head; int i = 0; while (l->curr != temp) { temp = temp->next; i++; } return i; } void moveToPos(List *l, int pos) { if (pos >= 0 && pos < l->cnt) { l->curr = l->head; int i = 0; while (i < pos) { l->curr = l->curr->next; i++; } } } int getValue(List *l) { if (l->curr->next != NULL) return l->curr->next->elemento; else return 0; } int remover(List *l) { if (l->curr->next == NULL) return 0; Link *aux = l->curr->next; int it = l->curr->next->elemento; if (l->tail == l->curr->next) l->tail = l->curr; l->curr->next = l->curr->next->next; free(aux); l->cnt--; return it; } void clear(List *l) { int tam = l->cnt; int i; Link *aux; l->curr = l->head; for (i = 0; i < tam; i++) { remover(l); } l->cnt = 0; l->head->next = NULL; l->curr = l->head; l->tail = l->head; } int main(void) { int n, q, aux, max = 0, indexMax = 0; scanf("%d %d", &n, &q); List *a = create_list(); for (int i = 0; i < n; i++) { scanf("%d", &aux); moveToEnd(a); insert(a, aux); if (aux > max) { max = aux; indexMax = i; } } long long int *m = (long long int *)malloc((q + 1) * sizeof(long long int)); for (int i = 0; i < q; i++) { scanf("%lld", &m[i]); } long long int **ans = (long long int **)malloc((2) * sizeof(long long int *)); ans[0] = (long long int *)malloc((indexMax + 1) * sizeof(long long int)); ans[1] = (long long int *)malloc((indexMax + 1) * sizeof(long long int)); for (int i = 0; i < indexMax; i++) { int A, B; moveToStart(a); A = remover(a); B = remover(a); ans[0][i] = A; ans[1][i] = B; if (A > B) { insert(a, A); moveToEnd(a); insert(a, B); } else { insert(a, B); moveToEnd(a); insert(a, A); } } moveToStart(a); next(a); int *b = (int *)malloc(n * sizeof(int)); for (int i = 0; i < n - 1; i++) { b[i] = a->curr->next->elemento; next(a); } printf("\n"); for (int i = 0; i < q; i++) { if (m[i] <= indexMax) { printf("%lld %lld\n", ans[0][m[i] - 1], ans[1][m[i] - 1]); } else { long long int index = (m[i] - indexMax - 1) % (n - 1); printf("%d %d\n", max, b[index]); } } clear(a); free(a->curr); free(a); free(m); free(ans[0]); free(ans[1]); free(ans); free(b); return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; template <typename T> inline T Abs(T a) { return a > 0 ? a : -a; } template <typename T> inline T sqr(T a) { return a * a; } template <typename T> inline void relaxMin(T &a, T b) { if (b < a) a = b; } template <typename T> inline void relaxMax(T &a, T b) { if (b > a) a = b; } const int INF = (int)1E9; const long double EPS = 1E-12; const long double PI = 3.1415926535897932384626433832795; long long n, k, p; long long q[1000]; void ok() { putchar('X'); } void fail() { putchar('.'); } void calc(long long n0, long long qq, long long k0) { if (k0 == 0) { fail(); return; } if (qq & 1) { long long pos = (qq - 1) / 2; long long ost = k0 - n0 / 2; if (ost <= 0) fail(); else { if (pos < ost) ok(); else fail(); } } else { if (qq < k0 * 2) ok(); else fail(); } } void solve() { cin >> n >> k >> p; for (int i = 0, _n(p); i < _n; i++) cin >> q[i]; for (int i = 0, _n(p); i < _n; i++) q[i] = n - q[i]; if (k == 0) { for (int i = 0, _n(p); i < _n; i++) fail(); return; } if (n & 1) { for (int i = 0, _n(p); i < _n; i++) { if (q[i] == 0) ok(); else { calc(n - 1, q[i] - 1, k - 1); } } } else { for (int i = 0, _n(p); i < _n; i++) { calc(n, q[i], k); } } } int main() { solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int a[n], o[n], e[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) { e[i] = 0; o[i] = a[i]; } else { e[i] = a[i]; o[i] = 0; } } long long int so[n], se[n]; so[0] = o[0]; se[0] = e[0]; for (int i = 0; i < n - 1; i++) { so[i + 1] = o[i + 1] + so[i]; se[i + 1] = e[i + 1] + se[i]; } int cnt = 0; for (int i = 0; i < n; i++) { long long int x = 0, y = 1; if (i % 2 == 0) { x = so[n - 1] - so[i] + se[i]; y = se[n - 1] - se[i] + so[i] - a[i]; } else { x = se[n - 1] - se[i] + so[i]; y = so[n - 1] - so[i] + se[i] - a[i]; } if (x == y) cnt++; } cout << cnt; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1010, mod = 1e9 + 7, INF = 0x3f3f3f3f; const double eps = 1e-6; int n, m, k; long long g[N][N]; vector<int> id; struct Edge { int a, b, w; bool operator<(const Edge& W) const { return w < W.w; } } edge[N * N * 4]; int find(int x) { return lower_bound(id.begin(), id.end(), x) - id.begin(); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); edge[i] = {a, b, c}; } sort(edge + 1, edge + 1 + m); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) g[i][j] = 1e18; for (int i = 1; i <= min(m, k); i++) { int a = edge[i].a, b = edge[i].b, w = edge[i].w; id.push_back(a); id.push_back(b); } sort(id.begin(), id.end()); id.erase(unique(id.begin(), id.end()), id.end()); for (int i = 1; i <= min(m, k); i++) { int a = edge[i].a, b = edge[i].b, w = edge[i].w; a = find(a); b = find(b); g[a][b] = g[b][a] = min(g[a][b], 1ll * w); } n = id.size(); vector<long long> v; for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (g[i][j] != 1e18) v.push_back(g[i][j]); sort(v.begin(), v.end()); printf("%lld\n", v[k - 1]); return 0; }
7
#include <bits/stdc++.h> template <class T> inline T read() { T r = 0, f = 0; char c; while (!isdigit(c = getchar())) f |= (c == '-'); while (isdigit(c)) r = (r << 1) + (r << 3) + (c ^ 48), c = getchar(); return f ? -r : r; } inline long long qpow(long long a, int b) { long long ans = 1; for (; b; b >>= 1) { if (b & 1) (ans *= a) %= 1000000007; (a *= a) %= 1000000007; } return ans; } template <class T> inline T max(T a, T b) { return a > b ? a : b; } int n, Max, a[101101]; long long s, f[101101]; int main() { n = read<int>(); for (int i = 1; i <= n; i++) { a[i] = read<int>(); Max = max(Max, a[i]); s += a[i]; } f[1] = (s - 1) * (s - 1) % 1000000007 * qpow(s, 1000000007 - 2) % 1000000007; for (int i = 2; i <= Max; i++) { long long inv = qpow(s - i + 1, 1000000007 - 2); f[i] = (1000000007 + (2 * f[i - 1] - f[i - 2] - (s - 1) * inv) % 1000000007) % 1000000007; } long long ans = 0; for (int i = 1; i <= n; i++) ans += f[a[i]]; printf("%lld\n", ans % 1000000007); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; const int INF = 1e9 + 5; vector<int> lisRES, lisDP, lisPadre, LIS; void lisrec_increasing(int i, vector<int> &A) { if (i == -1) return; lisRES.push_back(A[i]); lisrec_increasing(lisPadre[i], A); } int lis(vector<int> &A) { int tam = (int)(A).size(); LIS.assign(tam, 0); lisPadre.assign(tam, -1); lisDP.assign(tam, INF); int M = 0; for (int i = (0); i < (tam); i++) { int j = upper_bound(lisDP.begin(), lisDP.begin() + M + 1, A[i]) - lisDP.begin(); lisDP[j] = A[i]; LIS[i] = j + 1; lisPadre[i] = (j == 0 ? -1 : LIS[j - 1]); M = max(M, j); } int maxv = 0; int maxi = 0; for (int i = tam - 1; i >= 0; i--) { if (LIS[i] > maxv) { maxv = LIS[i]; maxi = i; } } return maxv; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<int> diffarray(m + 1, 0); for (int i = (0); i < (n); i++) { int a, b; cin >> a >> b; diffarray[a - 1]++; diffarray[b]--; } vector<int> cant(m, 0); for (int i = (0); i < (m); i++) { cant[i] = (i != 0 ? cant[i - 1] : 0) + diffarray[i]; } int lisc = lis(cant); vector<int> creciente = LIS; reverse(cant.begin(), cant.end()); int lisd = lis(cant); vector<int> decreciente = LIS; reverse(decreciente.begin(), decreciente.end()); int maxUnimodal = 0; for (int i = (0); i < (m); i++) { maxUnimodal = max(maxUnimodal, creciente[i] + decreciente[i] - 1); } cout << maxUnimodal << "\n"; }
5
#include <bits/stdc++.h> using namespace std; const int maxx = 40 + 7; const int N = (1 << 7); int DP[maxx][N][N]; int ans = INT_MAX; int n, m; bool valid(int a, int b, int c) { if (!(((b >> 0) & 1) || ((b >> 1) & 1) || ((c >> 0) & 1) || ((a >> 0) & 1))) return false; for (int i = 1; i < m; i++) if (!(((b >> i) & 1) || ((b >> (i + 1)) & 1) || ((b >> (i - 1)) & 1) || ((a >> i) & 1) || ((c >> i) & 1))) return false; return true; } int main() { ios::sync_with_stdio(false); cin >> n >> m; if (n == 1 && m == 1) { cout << "0" << "\n"; return 0; } if (n < m) swap(n, m); for (int i = 0; i < (1 << m); i++) for (int j = 0; j < (1 << m); j++) { if (valid(i, j, 0)) DP[1][i][j] = __builtin_popcount(i) + __builtin_popcount(j); else DP[1][i][j] = -1; } for (int i = 2; i <= n; i++) { for (int j = 0; j < (1 << m); j++) for (int k = 0; k < (1 << m); k++) { DP[i][j][k] = INT_MAX; for (int l = 0; l < (1 << m); l++) { if (valid(j, k, l) && DP[i - 1][k][l] != -1) DP[i][j][k] = min(DP[i][j][k], DP[i - 1][k][l] + __builtin_popcount(j)); } } } for (int i = 0; i < (1 << m); i++) ans = min(DP[n][0][i], ans); cout << (n * m) - ans << "\n"; }
6
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, x1, x2, y1, y2; cin >> n >> m >> x1 >> y1 >> x2 >> y2; long long ma = max(abs(x2 - x1), abs(y2 - y1)), mi = min(abs(x2 - x1), abs(y2 - y1)); if (ma >= 5 or (ma == 4 and mi >= 3)) cout << "Second"; else cout << "First"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { long long n, m; long long ans = 0, c = 0, x = 0; cin >> n >> m; long long mm = m; for (int i = 1; i <= n; i++) { mm = m; x = 5 - (i % 5); mm -= x; if (mm >= 0) ans++; ans += mm / 5; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int m; cin >> m; long long int x; cin >> x; long long int y; cin >> y; char arr[n][m]; bool visited[n][m]; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { visited[i][j] = false; } } long long int countb = 0; long long int countw = 0; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { char c; cin >> c; arr[i][j] = c; if (c == '*') { countb++; } else { countw++; } } } if (m == 1) { cout << (x * countw) << endl; } else if (x * 2 <= y) { cout << ((countw) * (x)) << endl; } else { long long int ans = 0; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m - 1;) { if (arr[i][j] == '.' && arr[i][j + 1] == '.') { ans = ans + y; visited[i][j] = true; visited[i][j + 1] = true; j = j + 2; } else { j = j + 1; } } } for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { if (arr[i][j] == '.' && visited[i][j] == false) { ans = ans + x; } } } cout << ans << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int MAXN = 200010; int F[MAXN], G[MAXN], H[MAXN], A[MAXN], B[MAXN]; int count(int[], int, int); int main() { int N, K; cin >> N >> K; F[1] = K - 2; G[1] = K - 1; F[2] = ((long long)(K - 2) * (K - 2) + (K - 1)) % MOD; G[2] = (long long)(K - 1) * (K - 2) % MOD; H[1] = K - 1; H[2] = (long long)(K - 1) * (K - 1) % MOD; for (int i = 3; i <= N; ++i) { F[i] = ((long long)F[i - 1] * (K - 2) + (long long)F[i - 2] * (K - 1)) % MOD; G[i] = ((long long)G[i - 1] * (K - 2) + (long long)G[i - 2] * (K - 1)) % MOD; H[i] = (long long)(H[i - 1]) * (K - 1) % MOD; } int N1 = 0, N2 = 0; for (int i = 0; i < N; ++i) { int x; cin >> x; if (i % 2 == 0) { A[N1++] = x; } else { B[N2++] = x; } } cout << (long long)count(A, N1, K) * count(B, N2, K) % MOD << endl; return 0; } int count(int A[], int N, int K) { int cnt = 1; for (int i = 0, j = 0; i < N; i = j) { for (; j < N && A[j] == A[i]; ++j) ; int len = j - i; if (A[i] != -1) { if (len > 1) { return 0; } else { continue; } } if (len == N) { cnt = (long long)cnt * K % MOD; if (N > 1) { cnt = (long long)cnt * H[N - 1] % MOD; } } else if (i == 0 || j == N) { cnt = (long long)cnt * H[len] % MOD; } else if (A[i - 1] == A[j]) { cnt = (long long)cnt * G[len] % MOD; } else { cnt = (long long)cnt * F[len] % MOD; } } return cnt; }
7
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } double pi = 3.141592653589; void solve() { long long n; cin >> n; vector<bool> used(n); vector<bool> married(n); for (long long i = (0); i < (long long)n; ++i) { long long k; cin >> k; vector<long long> list(k); for (long long j = (0); j < (long long)k; ++j) { cin >> list[j]; list[j]--; } for (long long x : list) { if (!used[x]) { used[x] = true; married[i] = true; break; } } } long long x = 0; while (x < n and married[x]) x++; if (x == n) cout << "OPTIMAL\n"; else { long long y = 0; while (used[y]) y++; cout << "IMPROVE\n" << x + 1 << " " << y + 1 << "\n"; } } int32_t main() { long long x; cin >> x; while (x--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int a, m; cin >> a >> m; int s = 0; while (a--) { int b; cin >> b; s += b; } cout << min(s, m) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int ans = INT_MAX; int a[410], n; int G(int x) { int res = 0; for (int i = 1; i <= n; i++) { res += x / a[i]; x %= a[i]; } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 2; i <= n; i++) { for (int j = i, x = a[i - 1] - 1, y = x, m = 1; j <= n; j++) { int t = y / a[j]; m += t, y -= t * a[j]; if (m < G(x - y + a[j])) ans = min(ans, x - y + a[j]); } } printf("%d\n", ans > 2 * a[1] ? -1 : ans); }
9
#include <bits/stdc++.h> using namespace std; int n, e, d; int gcd(int m, int n) { int r = m % n; while (r) { m = n; n = r; r = m % n; } return n; } int main() { scanf("%d%d%d", &n, &d, &e); e *= 5; int ans = n % d; n -= e; for (int i = 1; n >= 0; i++) { ans = min(ans, n % d); n -= e; } printf("%d", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& s, vector<T>& v) { s << '{'; for (int i = 0; i < int(v.size()); i++) s << (i ? "," : "") << v[i]; return s << '}'; } template <typename S, typename T> ostream& operator<<(ostream& s, pair<S, T> const& p) { return s << '(' << p.first << ',' << p.second << ')'; } set<string> ans; string s; int N; bool dp[10000][2]; void sv(int n, int l) { if (dp[n][l]) return; dp[n][l] = true; string t; if (l == 0 && n >= 6) { t = s.substr(n - 1, 2); } else if (l == 1 && n >= 7) { t = s.substr(n - 2, 3); } else { return; } ans.insert(t); if (s.substr(n + 1 - 2 * t.size(), t.size()) != t) sv(n - t.size(), t.size() - 2); sv(n - t.size(), !(t.size() - 2)); } int main() { cin >> s; N = (int)s.size(); sv(N - 1, 0); sv(N - 1, 1); printf("%d\n", (int)ans.size()); for (auto const& x : ans) puts(x.c_str()); }
5
#include <bits/stdc++.h> using namespace std; template <class T, class U> ostream& operator<<(ostream& o, const pair<T, U>& p) { o << "(" << p.first << "," << p.second << ")"; return o; } template <class T> ostream& operator<<(ostream& o, const vector<T>& v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } long long mod_pow(long long x, long long n, long long p) { long long res = 1; x %= p; while (n > 0) { if (n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res; } long long mod_inv(long long x, long long p) { return mod_pow(x % p, p - 2, p); } long long mod_sqrt(long long x, long long p) { x %= p; if (x == 0) return 0; if (p == 2) return x; auto is_square = [&](long long a) { return mod_pow(a, (p - 1) / 2, p) == 1; }; if (!is_square(x)) return -1; long long y = 2; while (is_square((y * y - x + p) % p)) y++; long long r = (y * y - x + p) % p; auto mul = [&](pair<long long, long long> u, pair<long long, long long> v) { long long s = (u.first * v.first + u.second * v.second % p * r) % p; long long t = (u.second * v.first + u.first * v.second) % p; return make_pair((s), (t)); }; long long e = (p + 1) / 2; auto ret = make_pair((1LL), (0LL)); auto v = make_pair((y), (1LL)); while (e > 0) { if (e & 1) ret = mul(ret, v); v = mul(v, v); e /= 2; } assert(r == 0 || ret.second == 0); return ret.first; } int main() { long long m, n; cin >> m >> n; vector<long long> vec(n); for (int i = (int)(0); i < (int)(n); i++) scanf("%d", &vec[i]); set<long long> st; for (int i = (int)(0); i < (int)(n); i++) st.insert(vec[i]); if (n == 1) { cout << vec[0] << " " << 0; return 0; } if (m < n) { cout << -1 << endl; return 0; } if (m == n) { cout << vec[0] << " " << 1 << endl; return 0; } if (n == m - 1) { for (int i = (int)(0); i < (int)(m); i++) if (st.find(i) == st.end()) { cout << (i + 1) % m << " " << 1 << endl; return 0; } } long long s = accumulate((vec).begin(), (vec).end(), 0LL); long long avg = s % m * mod_inv(n, m) % m; long long v = 0; for (int i = (int)(0); i < (int)(n); i++) v += (vec[i] - avg) * (vec[i] - avg) % m; v = v % m * mod_inv(n, m) % m; long long d = mod_sqrt(12 * v % m * mod_inv(n * n - 1, m) % m, m); if (d <= 0) { cout << -1 << endl; return 0; } int tmp = vec[0]; int cnt = 1; while (cnt < n && st.find((tmp + d) % m) != st.end()) { tmp = (tmp + d) % m; cnt++; } tmp = vec[0]; while (cnt < n && st.find((tmp - d + m) % m) != st.end()) { tmp = (tmp - d + m) % m; cnt++; } if (cnt == n) { cout << tmp << " " << d << endl; return 0; } cout << -1 << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = 0; for (int i = 0; i < s.size();) { if (s[i + 1] != '/') { for (int j = 0; j < n / 2; j++) { cout << " "; } cout << s[i] << s[i + 1] << s[i + 2] << endl; i += 3; n += 2; } else { n -= 2; for (int j = 0; j < n / 2; j++) { cout << " "; } cout << s[i] << s[i + 1] << s[i + 2] << s[i + 3] << endl; i += 4; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 1e5; int pila[MAXN + 2]; int cubeta[MAXN + 2]; int obten_sig(int n) { if (cubeta[1] != 0) return 1; int pos = 1; while (pila[pos] != 1) pos++; int ini = pos - 1; for (int i = 1; i <= n - ini; i++) { if (pila[ini + i] != i) return 1; } return n - ini + 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { int a; cin >> a; cubeta[a] = 1; } for (int i = 1; i <= n; i++) { cin >> pila[i]; } int idx = 1; int pasos = 0; int sig = obten_sig(n); int menor = 1; while (menor <= n && cubeta[menor] != 0) menor++; while (sig <= n) { if (cubeta[sig] != 0) { sig++; } else { sig = menor; } if (idx <= n) { cubeta[pila[idx]] = 1; idx++; } if (menor <= n && cubeta[menor] != 0) menor++; pasos++; } cout << pasos << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int p = (1 << 20); int d[2 * p]; int pref[p]; int pref_b[p]; int pref_e[p]; const int INF = 1000000000; void wstaw(int pos, int x) { pos += p; d[pos] = x; pos >>= 1; while (pos > 0) { d[pos] = min(d[pos * 2], d[2 * pos + 1]); pos >>= 1; } } int query(int l, int r) { l += p; r += p; int wyn = min(d[l], d[r]); while (l < r) { if (l & 1) wyn = min(wyn, d[l++]); if (!(r & 1)) wyn = min(wyn, d[r--]); l >>= 1; r >>= 1; } if (l == r) wyn = min(wyn, d[l]); return wyn; } int main() { for (int i = 0; i < 2 * p; i++) d[i] = INF; cin.tie(0); ios::sync_with_stdio(false); string s; cin >> s; pref[0] = 0; pref_b[0] = 0; pref_e[0] = 0; int n = s.size(); for (int i = 1; i <= n; i++) { if (s[i - 1] == '(') { pref[i] = pref[i - 1] + 1; pref_b[i] = pref_b[i - 1] + 1; pref_e[i] = pref_e[i - 1]; wstaw(i, pref[i]); } else { pref[i] = pref[i - 1] - 1; pref_b[i] = pref_b[i - 1]; pref_e[i] = pref_e[i - 1] + 1; wstaw(i, pref[i]); } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int mini = query(l, r); int nadmiar = max(0, (pref[l - 1] - mini)); int naw = max(0, pref_e[r] - pref_e[l - 1] - nadmiar); cout << 2 * naw << "\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long maxn = 100100; long long n, m, k; set<long long> R[maxn], C[maxn]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; long long s = n * m - k; long long u = 0, d = n + 1, l = 0, r = m + 1; for (long long i = 0; i < k; i++) { long long x, y; cin >> x >> y; R[x].insert(y); C[y].insert(x); } long long x = 1, y = 0; long long t = 0; while (true) { long long nx, ny; if (t == 0) { auto it = R[x].lower_bound(y); nx = x; ny = r - 1; if (it != R[x].end()) ny = min(ny, *it - 1); if (ny <= y) break; u = x; } if (t == 1) { auto it = C[y].lower_bound(x); nx = d - 1; ny = y; if (it != C[y].end()) nx = min(nx, *it - 1); if (nx <= x) break; r = y; } if (t == 2) { auto it = R[x].lower_bound(y); nx = x; ny = l + 1; if (R[x].size() and it != R[x].begin()) ny = max(ny, *(--it) + 1); if (ny >= y) break; d = x; } if (t == 3) { auto it = C[y].lower_bound(x); nx = u + 1; ny = y; if (C[y].size() and it != C[y].begin()) nx = max(nx, *(--it) + 1); if (nx >= x) break; l = y; } t = (t + 1) % 4; s -= abs(nx - x) + abs(ny - y); x = nx, y = ny; } if (s) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, m; int col[N + 2]; vector<int> G[N + 2]; void solve() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) col[i] = 0, G[i].clear(); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); G[y].push_back(x); } for (int i = 1; i <= n; i++) for (auto j : G[i]) { if (col[j] == 0 && col[i] != 2) col[i] = 1; if (col[j] == 1) col[i] = 2; } int cnt = 0; for (int i = 1; i <= n; i++) if (col[i] == 2) cnt++; printf("%d\n", cnt); for (int i = 1; i <= n; i++) if (col[i] == 2) printf("%d ", i); puts(""); } int main() { int t; scanf("%d", &t); while (t--) solve(); }
8
#include <bits/stdc++.h> using namespace std; int n, a, b; int c[105][105]; int main() { cin >> n >> a >> b; if (n > a * b) { cout << -1 << endl; return 0; } for (int i = 1, k = 0; i <= a; i++) for (int jj = 1; jj <= b; jj++) { int j; if (i & 1) j = jj; else j = b - jj + 1; if (k < n) c[i][j] = ++k; else break; } for (int i = 1; i <= a; i++) for (int j = 1; j <= b; j++) printf("%d%c", c[i][j], " \n"[j == b]); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; long long a[MAXN]; long long b[MAXN]; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long pos = 0; long long kk1 = 1; long long kk2 = 2; long long ans = 0; long long num1 = 0; long long num2 = 0; long long ans1 = 0; long long ans2 = 0; for (int i = 1; i <= n; ++i) { cin >> a[i], num1 += a[i] & 1, num2 += 1 - a[i] & 1; if (a[i] & 1) { ans1 += abs(kk1 - i), kk1 += 2; ans2 += abs(kk2 - i), kk2 += 2; } } if (abs(num1 - num2) > 1) { cout << -1 << endl; continue; } if (n % 2 == 1) { if (num1 > num2) { cout << ans1 << endl; } else cout << ans2 << endl; continue; } cout << min(ans1, ans2) << endl; } }
2
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using vi = vector<int>; using ll = long long; vi prefix_function(string &s) { int n = int((s).size()), j = 0; vi pre(n); for (int i = int(1); i < int(n); i++) { while (j > 0 && s[i] != s[j]) j = pre[j - 1]; pre[i] = s[i] == s[j] ? ++j : j; } return pre; } vector<vi> kmp_automaton(string &s) { s += '#'; int n = int((s).size()); vi pre = prefix_function(s); vector<vi> aut(n, vi(26)); for (int i = int(0); i < int(n); i++) for (int c = int(0); c < int(26); c++) { if (i > 0 && 'a' + c != s[i]) aut[i][c] = aut[pre[i - 1]][c]; else aut[i][c] = i + ('a' + c == s[i]); } return aut; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string t, s; cin >> t >> s; int m = int((t).size()), n = int((s).size()); vector<vi> aut = kmp_automaton(s), dp(m + 1, vi(n + 1)); for (int i = int(m - 1); i >= int(0); i--) for (int j = int(0); j < int(n + 1); j++) { int &x = dp[i][j]; if (t[i] == '?') { for (int c = int(0); c < int(26); c++) { int v = aut[j][c]; x = max(x, dp[i + 1][v] + (v == n)); } } else { int v = aut[j][int(t[i] - 'a')]; x = dp[i + 1][v] + (v == n); } } cout << dp[0][0] << '\n'; return 0; }
7
#include <bits/stdc++.h> using namespace std; string A; bool isPalindrome(int left, int right) { while (left < right) { if (A[left] != A[right]) return false; left++, right--; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> A; int N = A.size(); int bonus = 0; int ret = 0; if (N % 2 == 1) bonus = 1; N /= 2; while (N > 0) { if (isPalindrome(0, N - 1)) { if (N % 2 == 1) { N--; bonus = 1; } else N /= 2; } else { ret = 1 + bonus; break; } } if (ret == 0) cout << "Impossible"; else cout << ret; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int S = 400; const int N = 2e5 + 7; int n, m; vector<int> G[N]; int u[N]; int w[N]; unsigned long long ans, A, B, C; unsigned long long sum(int a, int b) { if (a > b) return 0; if (a == 0) ++a; return (unsigned long long)b * (b + 1) / 2 - (unsigned long long)a * (a - 1) / 2; } bitset<N> edge[3 * S + 7]; bitset<N> curr; int cnt = 0; int gdzie[N]; inline void trian(int a, int b, int c) { if (b > c) swap(b, c); if (a > b) return; ans -= A * a + B * b + C * c; } void trojkaty() { for (int v = 0; v < n; ++v) { if (G[v].size() > S) { gdzie[v] = ++cnt; for (auto it : G[v]) edge[cnt][it] = 1; } } for (int v = 0; v < n; ++v) { if (G[v].size() > S) { for (int i = 1; i <= m; ++i) { if (edge[gdzie[v]][u[i]] && edge[gdzie[v]][w[i]]) trian(v, u[i], w[i]); } } else { for (int i = 0; i < G[v].size(); ++i) { if (G[G[v][i]].size() > S) { for (int j = i + 1; j < G[v].size(); ++j) { if (edge[gdzie[G[v][i]]][G[v][j]]) trian(v, G[v][i], G[v][j]); } } else { for (auto it : G[G[v][i]]) curr[it] = 1; for (int j = i + 1; j < G[v].size(); ++j) { if (curr[G[v][j]]) trian(v, G[v][i], G[v][j]); } for (auto it : G[G[v][i]]) curr[it] = 0; } } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> A >> B >> C; for (int i = 1; i <= m; ++i) { cin >> u[i] >> w[i]; G[u[i]].push_back(w[i]); G[w[i]].push_back(u[i]); } for (int i = 0; i < n; ++i) { sort((G[i]).begin(), (G[i]).end()); ans += A * (n - i - 1) * (n - i - 2) / 2 * i; ans += B * i * (n - i - 1) * i; ans += C * i * (i - 1) / 2 * i; unsigned long long l = 0, u = 0, sl = 0, su = 0, kl = 0, ku = 0; for (auto it : G[i]) { if (it < i) { ans -= (A * it + B * i) * (n - i - 1) + C * sum(i + 1, n - 1); ++l; sl += it; kl += (unsigned long long)it * it; } else { ans -= (C * it + B * i) * i + A * sum(0, i - 1); ++u; su += it; ku += (unsigned long long)it * it; ans -= A * i * (it - i - 1) + C * it * (it - i - 1) + B * sum(i + 1, it - 1); } } ans += A * sl * u + B * i * u * l + C * su * l; ans += C * i * l * (l - 1) / 2; int cnt = 0; for (auto it : G[i]) { if (it < i) { ans += A * it * (l - cnt - 1); ans += B * it * cnt; ++cnt; } } ans += A * i * u * (u - 1) / 2; cnt = 0; for (auto it : G[i]) { if (it > i) { ans += B * it * (u - cnt - 1); ans += C * it * cnt; ++cnt; } } } trojkaty(); cout << ans << '\n'; }
9
#include <bits/stdc++.h> using namespace std; struct student { int i; int t, x; }; bool myless(student &st1, student &st2) { if (st1.x < st2.x) return true; return false; } int main() { int n, m; cin >> n >> m; vector<int> res(n); vector<student> v(n); for (int i = 0; i < n; i++) { student st; st.i = i; cin >> st.t >> st.x; v[i] = st; } int t = 0; int n_size = n; int ind = 0; while (n_size > 0) { int x = 0; int imaxt = 0; vector<student> car; if (n_size - m >= 0) { car.resize(m); for (int i = ind; i < ind + m; i++) { imaxt = max(imaxt, v[i].t); car[i - ind] = v[i]; } ind += m; } else { car.resize(n_size); for (int i = ind; i < ind + n_size; i++) { imaxt = max(imaxt, v[i].t); car[i - ind] = v[i]; } ind += n_size; } sort(car.begin(), car.end(), myless); n_size -= m; t = max(t, imaxt); int carsize = car.size(); for (int i = 0; i < carsize; i++) { int cnt = 1; int ibeg = i; while ((i + 1) < carsize && car[i].x == car[i + 1].x) { cnt++; i++; } t += (car[i].x - x); x = car[i].x; for (int j = ibeg; j < ibeg + cnt; j++) res[car[j].i] = t; t += 1 + cnt / 2; } t += car[carsize - 1].x; } for (int i = 0; i < n; i++) cout << res[i] << ' '; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long c, hr, hb, wr, wb; scanf("%I64d%I64d%I64d%I64d%I64d", &c, &hr, &hb, &wr, &wb); long long ans = 0; for (int i = 0; i < 1000000; i++) { if (wr * i < c) ans = max(ans, hr * i + ((c - wr * i) / wb) * hb); if (wb * i < c) ans = max(ans, hb * i + ((c - wb * i) / wr) * hr); } printf("%I64d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int a[100001], b[100001]; vector<int> roots[100001]; vector<int> edges[100001]; bool flag[100001] = {0}; int main() { int n; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { scanf("%d %d", &a[i], &b[i]); roots[a[i]].push_back(i + 1); roots[b[i]].push_back(i + 1); } printf("%d\n", n - 1); for (int i = 0; i < n - 1; ++i) { printf("2 %d %d\n", a[i], b[i]); } for (int i = 1; i <= n; ++i) { for (size_t j = 0; j + 1 < roots[i].size(); ++j) { edges[roots[i][j]].push_back(roots[i][j + 1]); edges[roots[i][j + 1]].push_back(roots[i][j]); } } flag[1] = true; queue<int> q; q.push(1); while (!q.empty()) { int t = q.front(); q.pop(); for (size_t j = 0; j < edges[t].size(); ++j) { if (!flag[edges[t][j]]) { printf("%d %d\n", t, edges[t][j]); q.push(edges[t][j]); flag[edges[t][j]] = true; } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; inline int in() { int32_t x; scanf("%d", &x); return x; } inline long long lin() { long long x; scanf("%lld", &x); return x; } char ch[4000010]; inline string get() { scanf("%s", ch); return string(ch); } inline void read(int *a, int n) { for (int i = 0; i < n; i++) a[i] = in(); } template <class blank> inline void out(blank x) { cout << x << "\n"; exit(0); } template <class blank, class blank2> inline void smin(blank &a, blank2 b) { a = a <= b ? a : b; } template <class blank, class blank2> inline void smax(blank &a, blank2 b) { a = a >= b ? a : b; } const int maxn = 3e5 + 10; const int maxm = 4e6 + 10; const int maxlg = 20; const int base = 29; const int mod = 1e9 + 7; const int inf = 2e18 + 10; const double eps = 1e-9; string code[maxn]; int correct[maxn]; int mark[maxn]; int n, m; int res; inline void bt(int pos = 0, int cnt = 0) { if (cnt > correct[0] || n - pos + cnt < correct[0]) return; if (pos == n) { string cc = code[0]; for (int i = 0; i < n; i++) { if (mark[i] == 0) { cc[i] ^= '1' ^ '0'; } } bool fl = true; for (int i = 0; i < m; i++) { int diff = 0; for (int j = 0; j < n; j++) diff += code[i][j] != cc[j]; if (diff != n - correct[i]) fl = false; } res += fl; return; } bt(pos + 1, cnt); mark[pos] = true; bt(pos + 1, cnt + 1); mark[pos] = false; } int32_t main() { n = in(), m = in(); for (int i = 0; i < m; i++) { code[i] = get(), correct[i] = in(); } bt(0); cout << res << "\n"; }
7
#include <bits/stdc++.h> using namespace std; class DSU { private: int N; vector<int> path; public: DSU() : N(2e5), path(N + 25) { iota(path.begin(), path.end(), 0); } DSU(int n) : N(n), path(N + 25) { iota(path.begin(), path.end(), 0); } void init(int n) { N = n; path.resize(N + 25); iota(path.begin(), path.end(), 0); } int findUnion(int x) { if (path[x] == x) return x; return path[x] = findUnion(path[x]); } void mergeUnion(int x, int y) { path[findUnion(x)] = findUnion(y); } bool sameSet(int x, int y) { return findUnion(x) == findUnion(y); } }; int n, m, d; set<int> an; vector<vector<int> > adj; DSU dsu(200005), dsu1(200005); vector<int> degree; void addEdge(int u, int v) { adj[u].emplace_back(v); adj[v].emplace_back(u); degree[u]++, degree[v]++; } set<int> st; void dfs(int node, map<int, vector<int> >& mp, set<int>& vis, int par = -1, int depth = 0) { vis.insert(node); if (an.find(node) != an.end()) { mp[depth].push_back(node); } for (auto it : adj[node]) { if (it == 1 || st.find(it) == st.end() || it == par || vis.find(it) != vis.end()) continue; dfs(it, mp, vis, node, depth + 1); } } vector<pair<int, int> > ans; void dfs1(int node, int par = -1) { for (auto it : adj[node]) { if (it == 1 || st.find(it) == st.end() || it == par) continue; if (dsu1.sameSet(node, it)) continue; ans.emplace_back(node, it); dsu1.mergeUnion(node, it); dfs1(it, node); } } void solve() { cin >> n >> m >> d; adj.resize(n + 1); degree.assign(n + 1, 0); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; addEdge(u, v); } int cnt = 0; for (int it : adj[1]) { an.insert(it); if (degree[it] == 1) cnt++; } if (degree[1] < d || cnt > d) { cout << "NO\n"; return; } set<int> x; if (degree[1] == d) { for (int it : adj[1]) { ans.emplace_back(1, it); dsu.mergeUnion(1, it); } for (int i = 2; i <= n; i++) { for (int it : adj[i]) { if (it == 1) continue; if (dsu.sameSet(i, it)) continue; ans.emplace_back(i, it); dsu.mergeUnion(i, it); } } cout << "YES\n"; for (auto it : ans) { cout << it.first << " " << it.second << "\n"; } return; } for (int i = 2; i <= n; i++) { for (int it : adj[i]) { if (it == 1 || dsu.sameSet(it, i)) continue; dsu.mergeUnion(i, it); st.insert(i); st.insert(it); } } for (int it : adj[1]) { if (degree[it] == 1) st.insert(it); } map<int, vector<int> > mp, mp1; for (int i : st) { mp[dsu.findUnion(i)].push_back(i); } if (mp.size() > d) { cout << "NO\n"; return; } int sz = mp.size(); for (auto& it : mp) { if (sz == d) { for (int jt : it.second) { if (an.find(jt) == an.end()) continue; sz--, d--; ans.emplace_back(1, jt); dsu1.mergeUnion(1, jt); x.insert(jt); break; } continue; } else { mp1.clear(); set<int> vis; dfs(it.second[0], mp1, vis); while (d > sz && mp1.size()) { ans.emplace_back(1, mp1.rbegin()->second.back()); dsu1.mergeUnion(1, mp1.rbegin()->second.back()); st.erase(mp1.rbegin()->second.back()); x.insert(mp1.rbegin()->second.back()); d--; mp1.rbegin()->second.pop_back(); if (mp1.rbegin()->second.size() == 0) mp1.erase(mp1.rbegin()->first); } if (mp1.empty()) sz--; if (sz == d && mp1.size()) { sz--, d--; ans.emplace_back(1, mp1.begin()->second[0]); x.insert(mp1.begin()->second[0]); dsu1.mergeUnion(1, mp1.begin()->second[0]); } } } for (int it : x) { dfs1(it); } cout << "YES\n"; for (auto it : ans) { cout << it.first << " " << it.second << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tc = 1; while (tc--) { solve(); } return 0; }
4