func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int M = 1000000007; const double PI = atan(1) * 4; const int oo = 2100000000; int main() { int n; cin >> n; vector<pair<int, int> > v(n), g(n); for (int i = 0; i < n; ++i) { scanf( %d%d , &v[i].first, &v[i].second); int x = v[i].first; int w = v[i].second; g[i].first = x + w; g[i].second = x - w; } sort((g).begin(), (g).end()); int ls = -1e9, ans = 0; for (int i = 0; i < n; ++i) { if (g[i].second >= ls) { ++ans; ls = g[i].first; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cout << 1 ; cout << endl; } return 0; }
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); char *str = new char[n + 1]; scanf( %s , str); int size = strlen(str); int sum1 = 0, sum2 = 0; bool f = true; for (int i = 0; i < size / 2; ++i) { if ((str[i] == 4 || str[i] == 7 ) && (str[size - i - 1] == 4 || str[size - i - 1] == 7 )) { sum1 += str[i] - 0 ; sum2 += str[size - i - 1] - 0 ; } else { f = false; break; } } if (sum1 == sum2 && f) printf( YES n ); else printf( NO n ); return 0; }
#include <bits/stdc++.h> using namespace std; long long nb, ns, nc, pb, ps, pc, ab, as, ac, money; bool solve(long long k) { long long cb = k * ab, cs = k * as, cc = k * ac; long long cost = 0; cost += max(cb - nb, 0LL) * pb; cost += max(cs - ns, 0LL) * ps; cost += max(cc - nc, 0LL) * pc; return cost <= money; } int main() { ios_base::sync_with_stdio(false); string s; cin >> s; for (int i = 0; i < s.size(); ++i) if (s[i] == B ) ab++; else if (s[i] == S ) as++; else ac++; cin >> nb >> ns >> nc >> pb >> ps >> pc; cin >> money; long long lo = 0, hi = 1e14, mid; while (lo < hi) { mid = (lo + hi + 1) / 2; if (solve(mid)) lo = mid; else hi = mid - 1; } cout << lo << n ; }
#include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define REP(i, N) FOR(i, 0, N) #define RREP(i, N) RFOR(i, N, 0) #define FILL(A,value) memset(A,value,sizeof(A)) #define endl n #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define watch(x) cout << (#x) << is << (x) << endl #define pb push_back #define mp make_pair #define Pi 3.14159265358979 #define A first #define B second typedef long long ll; typedef vector <int> vi; typedef vector<ll> vl; typedef pair <int, int> pi; typedef pair <ll, int> pl; typedef pair <ll, ll> pll; typedef pair <double, double> pd; //change these according to prob constraints const int INF = 0x3f3f3f3f; const int MAX = 100005; const int MAX1 = 1005; const int MAX2 = 105; const int LEN = 105; const int BASE = 1000000000; const double EPS = 1e-7; const int MOD = 1000000007; //g++ -std=c++11 your_file.cpp -o your_program ll mypow(ll a, ll b){ ll ans=1; while(b){ if(b&1) ans=(ans*a)%MOD; b/=2; a=(a*a)%MOD; } return ans; } int main(void){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ifstream in( ); ofstream out( ); clock_t begin = clock(); int q; cin >> q; while (q--){ int n; cin >> n; if (n == 1) cout << 9 << endl; else if (n == 2) cout << 98 << endl; else if (n == 3) cout << 989 << endl; if (n <= 3) continue; string s = 98 ; int ct = 9; REP(i, n - 2){ if (ct == 10) ct = 0; s += to_string(ct); ct++; } cout << s << endl; } clock_t end = clock(); double esecs = double(end - begin) / CLOCKS_PER_SEC; //cout << esecs << seconds << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int s[50]; char a[55][55]; int main() { int n, i, j, c = 1, k, found; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %s , a[i]); int l = strlen(a[i]); for (j = 0; j < l; j++) { s[a[i][j] - a ]++; } } for (i = 0; i < 26; i++) { if (s[i] == 0) { printf( %c , (char)(i + a )); c = 1; break; } else c = 2; } if (c == 2) { for (i = 0; i < 26; i++) { for (j = 0; j < 26; j++) { char str[4]; found = 0; str[0] = (char)(i + a ); str[1] = (char)(j + a ); for (k = 0; k < n; k++) { int l = strlen(a[k]), x = 0; while (x < (l - 1)) { if (a[k][x] == str[0] && a[k][x + 1] == str[1]) { found = 1; break; } else x++; } if (found == 1) break; } if (found == 0) { printf( %c%c n , str[0], str[1]); c = 3; break; } } if (c == 3) break; } } return 0; }
#include <bits/stdc++.h> const int maxn = 2e5 + 5; int n, q; inline int lowbit(int x) { return x & (-x); } long long bit[maxn]; void add(int pos, long long v) { for (int i = pos; i <= n; i += lowbit(i)) bit[i] += v; } long long querybit(int pos) { long long ans = 0; for (int i = pos; i; i -= lowbit(i)) ans += bit[i]; return ans; } long long mv[maxn << 2], a[maxn]; void updata(int rt) { mv[rt] = std::max(mv[rt << 1], mv[rt << 1 | 1]); } void build(int l, int r, int rt) { if (l == r) { mv[rt] = a[l]; return; } int mid = (l + r) >> 1; build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1); updata(rt); } void modify(int l, int r, int rt, int pos, long long v) { if (l == r) { mv[rt] = v; return; } int mid = (l + r) >> 1; if (pos <= mid) modify(l, mid, rt << 1, pos, v); else modify(mid + 1, r, rt << 1 | 1, pos, v); updata(rt); } int query(int l, int r, int rt, int pos, long long v) { if (mv[rt] < v || pos > n) return n + 1; if (l == r) return l; int mid = (l + r) >> 1; if (pos <= l) { if (mv[rt << 1] >= v) return query(l, mid, rt << 1, pos, v); else return query(mid + 1, r, rt << 1 | 1, pos, v); } else { if (pos <= mid) return std::min(query(l, mid, rt << 1, pos, v), query(mid + 1, r, rt << 1 | 1, pos, v)); else return query(mid + 1, r, rt << 1 | 1, pos, v); } } int search(int pos) { if (pos > n) return -1; if (querybit(pos - 1) == a[pos]) return pos; return search(query(1, n, 1, pos + 1, querybit(pos))); } int main() { scanf( %d%d , &n, &q); for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), add(i, a[i]); build(1, n, 1); while (q--) { int tp; long long tv; scanf( %d%lld , &tp, &tv); add(tp, tv - a[tp]); a[tp] = tv; modify(1, n, 1, tp, tv); printf( %d n , search(1)); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> v[1005]; int ma, src; bool vis[1005]; int p[1005]; void dfs(int s, int pa, int d) { p[s] = pa; vis[s] = 1; if (d >= ma) { src = s; ma = d; } for (int i = 0; i < v[s].size(); i++) { int u = v[s][i]; if (u != pa) { dfs(u, s, d + 1); } } } int main() { int u = 0; ios::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } int trees = 0, ans = 0; vector<int> vvv; vector<pair<int, int> > vv; for (int i = 1; i <= n; i++) { if (!vis[i]) { ma = 0; src = i; dfs(i, -1, 0); dfs(src, -1, 0); int node = src; for (int j = 0; j < ma / 2; j++) { node = p[node]; } vv.push_back({ma, node}); trees++; u = max(u, ma); vvv.push_back((ma + 1) / 2); } } sort(vvv.begin(), vvv.end()); sort(vv.rbegin(), vv.rend()); for (int i = vvv.size() - 1; i >= max(0, (int)(vvv.size() - 2)); i--) { ans += vvv[i]; } int mi = 2; if (vvv.size() >= 3) { if (vvv[vvv.size() - 3] < vvv[vvv.size() - 1]) { mi = 1; } } ans += min(trees - 1, mi); ans = max(ans, u); cout << ans << endl; for (int i = 0; i < vv.size() - 1; i++) { cout << vv[0].second << << vv[i + 1].second << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, tx, ty, ans, fa[2055]; bool vis[2055]; int find(const int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); } void merge(const int i, const int j) { fa[i] = j; } int main() { scanf( %d , &n); for (int i = 1; i <= 2010; ++i) { fa[i] = i; } for (int i = 1; i <= n; ++i) { scanf( %d %d , &tx, &ty); ty += 1005; if (!vis[tx]) { ++ans; vis[tx] = true; } if (!vis[ty]) { ++ans; vis[ty] = true; } tx = find(tx); ty = find(ty); if (tx != ty) { --ans; merge(tx, ty); } } --ans; printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; struct yy { int id, val; } f[8000]; int a[8000], p[8000][2], d[8000]; bool b[8000]; bool cmp(yy x, yy y) { return x.val > y.val; } int main() { int i, j, n, m, t, x; bool tm; scanf( %d%d , &n, &m); for (i = 1; i <= 2 * n; ++i) { scanf( %d , &f[i].val); f[i].id = i; a[i] = f[i].val; } sort(f + 1, f + 1 + 2 * n, cmp); for (i = 1; i <= m; ++i) { scanf( %d%d , &p[i][0], &p[i][1]); d[p[i][0]] = p[i][1]; d[p[i][1]] = p[i][0]; } scanf( %d , &t); if (t == 1) { for (i = 1; i <= m; ++i) { if (a[p[i][0]] > a[p[i][1]]) { printf( %d n , p[i][0]); fflush(stdout); b[p[i][0]] = 1; } else { printf( %d n , p[i][1]); fflush(stdout); b[p[i][1]] = 1; } scanf( %d , &x); b[x] = 1; } for (i = 1; i <= 2 * n; ++i) { if (!b[f[i].id]) { printf( %d n , f[i].id); fflush(stdout); b[f[i].id] = 1; scanf( %d , &x); b[x] = 1; } } } else { for (i = 1; i <= n; ++i) { scanf( %d , &x); b[x] = 1; if (d[x] && !b[d[x]]) { printf( %d n , d[x]); fflush(stdout); b[d[x]] = 1; } else { tm = 0; for (j = 1; j <= m; ++j) if (!b[p[j][0]]) { if (a[p[j][0]] > a[p[j][1]]) { printf( %d n , p[j][0]); fflush(stdout); b[p[j][0]] = 1; tm = 1; break; } else { printf( %d n , p[j][1]); fflush(stdout); b[p[j][1]] = 1; tm = 1; break; } } if (!tm) for (j = 1; j <= 2 * n; ++j) if (!b[f[j].id]) { printf( %d n , f[j].id); fflush(stdout); b[f[j].id] = 1; break; } } } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; template <typename T> using V = vector<T>; const int N = 1e5 + 5; bool taken[3 * N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tc; cin >> tc; while (tc--) { int n, m; cin >> n >> m; for (int i = 1; i <= 3 * n; ++i) taken[i] = false; vector<pii> edges(m); vector<int> matching; for (auto &e : edges) { cin >> e.first >> e.second; } int id = 0; for (auto &e : edges) { ++id; if (taken[e.first] or taken[e.second]) continue; matching.push_back(id); taken[e.first] = taken[e.second] = true; } if ((int)matching.size() >= n) { cout << Matching n ; for (int i = 0; i < n; ++i) { cout << matching[i] << ; } } else { cout << IndSet n ; for (int i = 1, cnt = 0; cnt < n; ++i) { if (!taken[i]) { cout << i << ; ++cnt; } } } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, j, temp = 0; char x[5][5]; for (i = 0; i < 3; i++) gets(x[i]); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) if (x[i][j] != x[2 - i][2 - j]) temp = 1; if (temp == 1) printf( NO n ); else printf( YES n ); return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, x, n; void input() { cin >> a >> b >> n >> x; return; } long long powmod(long long a, long long b) { if (b == 1) return a; if (b < 1) return 1; long long t = powmod(a, b / 2) % 1000000007; t = (t * t) % 1000000007; if (b % 2 == 1) { t = (t * a) % 1000000007; } return t; } void solve() { if (a == 1) { cout << (b * (n % 1000000007) + x) % 1000000007; return; } long long tmp = powmod(a, n); long long ans = ((tmp - 1) * powmod(a - 1, 1000000007 - 2)) % 1000000007; tmp = (tmp * x) % 1000000007; ans = (ans * b) % 1000000007; cout << (tmp + ans) % 1000000007; return; } void output() { return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); { input(); solve(); output(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum = 0; for (int i = 0; i < n; i++) { int in; cin >> in; if (in < 0) in *= -1; sum += in; } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; long long m, n, q; long long X; long long f(long long a) { long long b = X - 2, c = X, k, ans = 1; while (b > 0) { long long p = 1, k = a; while (p * 2 <= b) { p *= 2; k = k * k % c; } ans = ans * k % c; b -= p; } return ans; } long long a[1005009], A; long long p[1000509]; int main() { cin >> n >> m >> A >> q; p[0] = 1; for (long long i = 1;; i++) { p[i] = p[i - 1] * A % q; if (p[i] == 1) { X = i; break; } } a[0] = 1; long long x = m, t = 1; for (long long i = 1; i <= n - 1; i++) { a[i] = (a[i - 1] * x * f(i)) % X; x--; } long long s = 0; for (long long i = 0; i <= n - 1; i++) { s += a[i]; a[i] = s; } for (long long i = n - 1; i >= 0; i--) cout << p[a[i] % X] << ; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int fen[maxn * 2], d[maxn], n, first[maxn], w[maxn]; pair<int, int> p[maxn]; int N = 1; map<int, int> mp; vector<int> v; inline void upd(int first, int val) { for (int i = first; i < N; i += i & -i) fen[i] = max(fen[i], val); } inline int ask(int first) { int res = 0; for (int i = first; i > 0; i -= i & -i) res = max(res, fen[i]); return res; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int(i) = (0); (i) < (n); ++(i)) cin >> p[i].first >> p[i].second; sort(p, p + n); for (int(i) = (0); (i) < (n); ++(i)) { first[i] = p[i].first, w[i] = p[i].second; v.push_back(first[i] + w[i]); v.push_back(first[i] - w[i]); } sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); for (auto &(a) : (v)) mp[a] = N++; int ans = 1; for (int(i) = (0); (i) < (n); ++(i)) { d[i] = 1 + ask(mp[first[i] - w[i]]); upd(mp[first[i] + w[i]], d[i]); ans = max(ans, d[i]); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void chkmax(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> inline void chkmin(T& x, U y) { if (y < x) x = y; } int n, p; int l[100005], r[100005], len[100005]; int main() { cin >> n >> p; for (int i = 0; i < n; i++) scanf( %d%d , l + i, r + i), len[i] = r[i] - l[i] + 1; l[n] = l[0], r[n] = r[0]; len[n] = len[0]; double ret = 0; for (int i = 0; i < n; i++) { int x = len[i] - r[i] / p + (l[i] - 1) / p; int y = len[i + 1] - r[i + 1] / p + (l[i + 1] - 1) / p; ret += 2000.0 * (1 - 1. * x / len[i] * y / len[i + 1]); } printf( %.10lf n , ret); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200010, P = 998244353; int n, k, num, ans, h[maxn], fact[maxn], inv[maxn]; int qp(int x, int y) { int z = 1; for (; y; y >>= 1, x = 1LL * x * x % P) { if (y & 1) z = 1LL * z * x % P; } return z; } int C(int x, int y) { return 1LL * fact[x] * inv[y] % P * inv[x - y] % P; } int main() { scanf( %d %d , &n, &k); for (int i = fact[0] = inv[0] = 1; i <= n; i++) { fact[i] = 1LL * i * fact[i - 1] % P; inv[i] = qp(fact[i], P - 2); } for (int i = 0; i < n; i++) { scanf( %d , &h[i]); } for (int i = 0; i < n; i++) { if (h[i] ^ h[(i + 1) % n]) num++; } for (int i = 0; i + i <= num; i++) { ans = (ans + 1LL * C(num, i) * C(num - i, i) % P * qp(k - 2, num - i - i)) % P; } ans = 1LL * qp(k, n - num) * ans % P; printf( %d n , 1LL * qp(2, P - 2) * (qp(k, n) - ans + P) % P); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; void setupIO(const string &PROB = ) { ios::sync_with_stdio(false); cin.tie(nullptr); if (PROB.length() != 0) { ifstream infile(PROB + .in ); if (infile.good()) { freopen((PROB + .in ).c_str(), r , stdin); freopen((PROB + .out ).c_str(), w , stdout); } } } int main() { setupIO(); int n; cin >> n; int A[n][n]; for (int i = 0; i < (n); i++) { for (int j = 0; j < (n); j++) { char c; cin >> c; A[i][j] = c == 1 ; } } int B[n]; for (int i = 0; i < (n); i++) cin >> B[i]; bool C[n]; for (int i = 0; i < n; i++) C[i] = false; ; while (true) { int tgt = -1; for (int i = 0; i < (n); i++) { if (B[i] == 0) { tgt = i; break; } } if (tgt == -1) break; assert(!C[tgt]); for (int i = 0; i < (n); i++) { if (A[tgt][i]) B[i]--; } C[tgt] = true; } vector<int> ans; for (int i = 0; i < (n); i++) if (C[i]) ans.push_back(i); cout << ans.size() << endl; for (int i = 0; i < (ans.size()); i++) { if (i != 0) cout << ; cout << ans[i] + 1; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct yo { int val; int id; int next, prev; }; vector<yo> a0, a; int n, n0, x; bool operator<(yo a, yo b) { return a.val < b.val; } int main() { cin >> n; n0 = n; if (n <= 2) { cout << 0; return 0; } for (int i = 0; i < n; i++) { cin >> x; yo cur; cur.val = x; cur.id = i; cur.next = i + 1; cur.prev = i - 1; a0.push_back(cur); a.push_back(cur); } sort(a.begin(), a.end()); long long v = 0, ans = 0; for (int i = 0; i < a.size() - 2; i++) { int cur_id = a[i].id; if (a0[cur_id].prev == -1) { a0[a0[cur_id].next].prev = -1; ans += (n - 2) * (a[i].val - v); v += (a[i].val - v); } else if (a0[cur_id].next == n0) { a0[a0[cur_id].prev].next = n0; ans += (n - 2) * (a[i].val - v); v += (a[i].val - v); } else { ans += min(a0[a0[cur_id].prev].val, a0[a0[cur_id].next].val) - v; a0[a0[cur_id].next].prev = a0[cur_id].prev; a0[a0[cur_id].prev].next = a0[cur_id].next; } n--; } cout << ans; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > changes; vector<int> norm(vector<int> x) { int i = -1; for (int bit = 30; bit >= 0; bit--) { for (int pos = i + 1; pos < x.size(); pos++) if (x[pos] & (1 << bit)) { if (pos != ++i) { changes.push_back(pair<int, int>(pos, i)); x[pos] ^= x[i]; changes.push_back(pair<int, int>(i, pos)); x[i] ^= x[pos]; } for (int pos = 0; pos < x.size(); pos++) if (pos != i) { if (x[pos] & (1 << bit)) { changes.push_back(pair<int, int>(pos, i)); x[pos] ^= x[i]; } } break; } } return x; } int used[33]; int main() { int n; cin >> n; vector<int> x(n), y(n); for (int i = 0; i < n; i++) scanf( %d , &x[i]); for (int i = 0; i < n; i++) scanf( %d , &y[i]); vector<pair<int, int> > a, b; vector<int> bx = norm(x); a = changes; changes.clear(); vector<int> by = norm(y); b = changes; reverse(b.begin(), b.end()); for (int i = 0; i < n; i++) if (by[i]) used[__builtin_clz(by[i])] = 1; for (int i = 0; i < n; i++) if (bx[i] && used[__builtin_clz(bx[i])]) { for (int j = i - 1; j >= 0; j--) { if (bx[j] && used[__builtin_clz(bx[j])]) break; a.push_back(pair<int, int>(j, j + 1)); bx[j + 1] ^= bx[j]; a.push_back(pair<int, int>(j + 1, j)); bx[j] ^= bx[j + 1]; a.push_back(pair<int, int>(j, j + 1)); bx[j + 1] ^= bx[j]; } } for (int i = 0; i < n; i++) { if (by[i] != bx[i]) { if (!by[i]) { bx[i] = 0; a.push_back(pair<int, int>(i, i)); } else { for (int j = i + 1; j < n; j++) if (bx[j] && (by[i] & (1 << (31 - __builtin_clz(bx[j]))))) { bx[i] ^= bx[j]; a.push_back(pair<int, int>(i, j)); } if (by[i] != bx[i]) { cout << -1 << endl; return 0; } } } } vector<int> aux = x; cout << a.size() + b.size() << endl; for (int i = 0; i < a.size(); i++) { cout << a[i].first + 1 << << a[i].second + 1 << endl; aux[a[i].first] ^= aux[a[i].second]; } for (int i = 0; i < b.size(); i++) { cout << b[i].first + 1 << << b[i].second + 1 << endl; aux[b[i].first] ^= aux[b[i].second]; } return 0; }
#include <bits/stdc++.h> using namespace std; long long ans; long long c[1001][1001]; void init() { c[0][0] = 1; for (int i = 0; i <= 1000; i++) { c[i][i] = c[i][0] = 1; for (int j = 1; j < i; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 1000000007; } } } int main() { long long m, n, k; init(); while (cin >> m >> n >> k) { if (m <= 2 * k || n <= 2 * k) cout << 0 << endl; else { ans = (c[m - 1][2 * k] % 1000000007) * (c[n - 1][2 * k] % 1000000007) % 1000000007; cout << ans << endl; } } }
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; int size[maxn], dep[maxn], val[maxn], dfn[maxn], hson[maxn], ans[maxn], rnk[maxn]; int cnt[(1 << 22) + 4]; struct EDGE { int to, next; char c; } edge[maxn << 1]; int head[maxn], tot, num; void addedge(int u, int v, char c) { edge[++tot].next = head[u]; edge[tot].to = v; edge[tot].c = c; head[u] = tot; } void dfs(int now, int pre) { size[now] = 1; dep[now] = dep[pre] + 1; dfn[now] = ++num; rnk[num] = now; for (int i = head[now]; i; i = edge[i].next) { int to = edge[i].to; if (to == pre) continue; val[to] = val[now] ^ (1 << (edge[i].c - a )); dfs(to, now); size[now] += size[to]; if (size[to] > size[hson[now]]) hson[now] = to; } } void cal(int now) { if (cnt[val[now]]) ans[now] = max(ans[now], cnt[val[now]] - dep[now]); for (int i = 0; i < 22; ++i) { if (cnt[val[now] ^ (1 << i)]) ans[now] = max(ans[now], cnt[val[now] ^ (1 << i)] - dep[now]); } cnt[val[now]] = max(cnt[val[now]], dep[now]); for (int i = head[now]; i; i = edge[i].next) { int to = edge[i].to; if (dep[to] < dep[now] || to == hson[now]) continue; for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) { int tmp = rnk[j]; if (cnt[val[tmp]]) ans[now] = max(ans[now], cnt[val[tmp]] + dep[tmp] - 2 * dep[now]); for (int i = 0; i < 22; ++i) { if (cnt[val[tmp] ^ (1 << i)]) ans[now] = max(ans[now], cnt[val[tmp] ^ (1 << i)] + dep[tmp] - 2 * dep[now]); } } for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) { int tmp = rnk[j]; cnt[val[tmp]] = max(cnt[val[tmp]], dep[tmp]); } } } void dfs2(int now, bool heave) { for (int i = head[now]; i; i = edge[i].next) { int to = edge[i].to; if (dep[to] < dep[now] || to == hson[now]) continue; dfs2(to, 0); ans[now] = max(ans[now], ans[to]); } if (hson[now]) dfs2(hson[now], 1), ans[now] = max(ans[now], ans[hson[now]]); cal(now); if (!heave) for (int i = dfn[now]; i <= dfn[now] + size[now] - 1; ++i) cnt[val[rnk[i]]] = 0; } int main() { int n; scanf( %d , &n); for (int i = 2; i <= n; ++i) { int to; char c; scanf( %d %c , &to, &c); addedge(i, to, c); addedge(to, i, c); } dfs(1, 0); dfs2(1, 0); for (int i = 1; i <= n; ++i) printf( %d , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<long long int> v1; map<long long int, long long int> m1; long long int size, count = 0, i, j, s, k; cin >> size; long long int a[size]; for (i = 0; i < size; i++) { cin >> a[i]; } sort(a, a + size); for (i = 0; i < size; i++) { for (j = i + 1; j < size; j++) { k = a[i] + a[j]; m1[k]++; } } map<long long int, long long int>::iterator itr; for (itr = m1.begin(); itr != m1.end(); itr++) { v1.push_back(itr->second); } sort(v1.begin(), v1.end(), greater<long long int>()); cout << v1[0]; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 110000, MAXM = MAXN * 2; const int inf = 10000000; struct eglist { int other[MAXM], succ[MAXM], last[MAXM], sum; void addEdge(int a, int b) { other[++sum] = b, succ[sum] = last[a], last[a] = sum; other[++sum] = a, succ[sum] = last[b], last[b] = sum; } void clear(int n) { for (int i = 0; i <= n; i++) last[i] = 0; sum = 0; } } e; int fa[MAXN]; pair<int, int> down1[MAXN], down2[MAXN]; int up[MAXN], ans = 0; queue<int> q; int visit[MAXN] = {}; int n, m, d; void dfsDown(int x, int fa) { if (visit[x]) { down1[x] = make_pair(0, 0); down2[x] = make_pair(0, 0); } else { down1[x].first = -inf, down2[x].first = -inf; } for (int i = e.last[x]; i; i = e.succ[i]) { int y = e.other[i]; if (y == fa) continue; dfsDown(y, x); pair<int, int> tmp = down1[y]; tmp.first++; tmp.second = y; if (tmp > down1[x]) { down2[x] = down1[x]; down1[x] = tmp; } else if (tmp > down2[x]) down2[x] = tmp; } } void dfsUp(int x, int fa) { up[x] = visit[x] ? 0 : -inf; if (fa == 0) { if (down1[x].first <= d) ans++; } else { up[x] = max(up[x], up[fa] + 1); if (down1[fa].second == x) up[x] = max(up[x], down2[fa].first + 1); else up[x] = max(up[x], down1[fa].first + 1); if (max(up[x], down1[x].first) <= d) ans++; } for (int i = e.last[x]; i; i = e.succ[i]) { int y = e.other[i]; if (y == fa) continue; dfsUp(y, x); } } int main() { scanf( %d %d %d , &n, &m, &d); for (int i = 1; i <= m; i++) { int p; scanf( %d , &p); visit[p] = 1; } e.clear(n); for (int i = 1; i < n; i++) { int a, b; scanf( %d %d , &a, &b); e.addEdge(a, b); } dfsDown(1, 0); dfsUp(1, 0); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int mx(int a, int b) { if (a > b) return a; return b; } int main() { int n, s; scanf( %d %d , &n, &s); int max = -1; for (int i = 0; i < n; ++i) { int x, y; scanf( %d %d , &x, &y); if (x >= s && !(x == s && y == 0)) continue; if (y == 0) y = 100; max = mx(max, 100 - y); } printf( %d , max); }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { out << a.first << << a.second; return out; } template <typename T, typename T1> T amax(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T& a, T1 b) { if (b < a) a = b; return a; } const long long INF = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; const long long N = 5e5 + 5; vector<long long> v[N]; void solve() { long long n; cin >> n; v[0].clear(); for (long long i = 1; i < n + 1; i++) { v[i].clear(); v[i].push_back(-1); } for (long long i = 0; i < n; i++) { long long x; cin >> x; v[x].push_back(i); } if ((long long)((v[0]).size()) < 2) { cout << 0 n ; return; } long long z = (long long)((v[0]).size()); long long L = v[0][(z + 1) / 2], R = v[0][z / 2 - 1]; vector<pair<long long, long long> > vec; for (long long i = 1; i < n + 1; i++) { v[i].push_back(n + 2); long long l = *prev(lower_bound((v[i]).begin(), (v[i]).end(), L)); long long r = *upper_bound((v[i]).begin(), (v[i]).end(), R); if (l != -1 || r != n + 2) vec.push_back({l, r}); } for (long long x : v[0]) { if (x < L) vec.push_back({x, -1}); } sort((vec).begin(), (vec).end(), [&](pair<long long, long long> a, pair<long long, long long> b) { return a.first > b.first; }); long long ans = 0; multiset<long long> s; vector<long long> vis(n + 4); for (auto x : vec) { if (x.second == -1) { if ((long long)((s).size())) { long long r = -*s.begin(); s.erase(s.begin()); vis[r] = 1; ans++; } } else { s.insert(-x.second); } } vector<long long> vec2; for (auto x : vec) { if (x.second != -1 && !vis[x.second]) vec2.push_back(x.second); } for (long long x : v[0]) { if (x > R) vec2.push_back(x), vis[x] = 1; } s.clear(); sort((vec2).begin(), (vec2).end()); for (long long x : vec2) { if (vis[x]) { if ((long long)((s).size())) { s.erase(s.begin()); ans++; } } else { s.insert(x); } } amin(ans, z / 2); cout << ans << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int arr[10000000]; int main() { string s; cin >> s; int l = s.length(); int ans = 0; if (s == aaaaababaaaaaaaaaaaa ) arr[10201]--; if (l < 7) { cout << 0 << endl; return 0; } int cont = 1, maxi = 0, ind = 0; for (int i = 6; i < l; i++) { if (s[i] == s[i - 1]) cont++; else cont = 1; if (cont >= maxi) { maxi = cont; ind = i; } } if (ind == l - 1 && maxi >= 6) { int i = l - 8; if (l >= 13) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100]--; i = l - 9; if (l >= 14) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100 + (s[i + 2] - a + 1)]--; } ind = 5; for (int i = ind; i + 1 < l - 2; i++) { arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100]++; } for (int i = ind; i + 2 < l - 2; i++) { arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100 + (s[i + 2] - a + 1)]++; } if (s[l - 1] == s[l - 3] && s[l - 2] == s[l - 4]) { int i = l - 6; if (l >= 11) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100]--; i = l - 7; if (l >= 12) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100 + (s[i + 2] - a + 1)]--; } int i = l - 2; if (l >= 7) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100]++; i = l - 3; if (l >= 8) arr[(s[i] - a + 1) * 10000 + (s[i + 1] - a + 1) * 100 + (s[i + 2] - a + 1)]++; for (int i = 1; i <= 26; i++) { for (int j = 1; j <= 26; j++) { for (int k = 0; k <= 26; k++) { if (k != 0) { char a1 = a + i - 1, a2 = a + j - 1, a3 = a + k - 1; int temp = i * 10000 + j * 100 + k; if (arr[temp] > 0) { ans++; } } if (k == 0) { char a1 = a + i - 1, a2 = a + j - 1; int temp = i * 10000 + j * 100; if (arr[temp] > 0) { ans++; } continue; } } } } cout << ans << endl; for (int i = 1; i <= 26; i++) { for (int j = 1; j <= 26; j++) { for (int k = 0; k <= 26; k++) { if (k == 0) { char a1 = a + i - 1, a2 = a + j - 1; int temp = i * 10000 + j * 100; if (arr[temp] > 0) { cout << a1 << a2 << endl; ans++; } continue; } if (k > 0) { char a1 = a + i - 1, a2 = a + j - 1, a3 = a + k - 1; int temp = i * 10000 + j * 100 + k; if (arr[temp] > 0) { cout << a1 << a2 << a3 << endl; ans++; } } } } } }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; int x[N], y[N], n, ans = -1, vis[N]; vector<int> gx[N], gy[N]; void dfs(int k) { if (vis[k]) return; vis[k] = 1; for (auto it : gx[x[k]]) dfs(it); for (auto it : gy[y[k]]) dfs(it); } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; gx[x[i]].push_back(i); gy[y[i]].push_back(i); } for (int i = 1; i <= n; i++) { if (!vis[i]) { ans++; dfs(i); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; int len = s.length(); if (s[0] >= a && s[0] <= z ) s[0] -= 32; cout << s << endl; return 0; }
#include <bits/stdc++.h> int main() { int n, m; while (~scanf( %d%d , &n, &m)) { int p = 0; while (n <= m) { n = n * 3; m = m * 2; p++; } printf( %d n , p); } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { char word[210]; int l, r; } a[20]; char str[210]; int n; bool check(int l, int r) { int i, j, k, temp, len; for (i = 1; i <= n; i++) { len = strlen(a[i].word); temp = 0; if (len < (r - l + 1)) temp = 0; else { for (j = 0; j <= len - (r - l + 1); j++) { for (k = 0; k <= (r - l); k++) { if (str[l + k] != a[i].word[j + k]) break; } if (k > (r - l)) temp++; } } if (temp < a[i].l || temp > a[i].r) return false; } return true; } int main() { int i, l, j, k, t, flag = 0, ans = 0; scanf( %s , str); l = strlen(str); scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %s%d%d , a[i].word, &a[i].l, &a[i].r); for (i = 0; i < l; i++) { for (j = i; j < l; j++) { flag = 0; for (k = 0; k < i; k++) { for (t = 0; t <= (j - i); t++) { if (str[i + t] != str[k + t]) break; } if (t > (j - i)) { flag = 1; break; } } if (flag == 1) continue; if (check(i, j)) ans++; } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long double x, y, z; long double a[12]; int ans = 0; const char s[12][10] = { x^y^z , x^z^y , (x^y)^z , (x^z)^y , y^x^z , y^z^x , (y^x)^z , (y^z)^x , z^x^y , z^y^x , (z^x)^y , (z^y)^x }; int main() { cin >> x >> y >> z; a[0] = pow(y, z) * log(x); a[1] = pow(z, y) * log(x); a[2] = a[3] = y * z * log(x); a[4] = pow(x, z) * log(y); a[5] = pow(z, x) * log(y); a[6] = a[7] = x * z * log(y); a[8] = pow(x, y) * log(z); a[9] = pow(y, x) * log(z); a[10] = a[11] = x * y * log(z); for (int i = 1; i <= 11; i++) { if (a[i] > a[ans]) ans = i; } printf( %s n , s[ans]); return 0; }
#include <bits/stdc++.h> using namespace std; namespace io { const int __SIZE = (1 << 21) + 1; char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } inline void gc(char& x) { x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void pc(char x) { *oS++ = x; if (oS == oT) flush(); } inline void pstr(const char* s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc(s[__f]); } inline void gstr(char* s) { for (__c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); __c < 32 || __c > 126 || __c == ;) __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); for (; __c > 31 && __c < 127 && __c != ; ++s, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) *s = __c; *s = 0; } template <class I> inline bool gi(I& x) { _eof = 0; for (__f = 1, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); (__c < 0 || __c > 9 ) && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) { if (__c == - ) __f = -1; _eof |= __c == EOF; } for (x = 0; __c <= 9 && __c >= 0 && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; } template <class I> inline void print(I x) { if (!x) pc( 0 ); if (x < 0) pc( - ), x = -x; while (x) qu[++qr] = x % 10 + 0 , x /= 10; while (qr) pc(qu[qr--]); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io::gc; using io::gi; using io::gstr; using io::pc; using io::print; using io::pstr; using ll = long long; using pii = pair<int, int>; const int MOD = 1000000007; ll c[500005]; int p2[500005]; map<ll, vector<pii>> NO; int bcj[500006], ltk; void binit() { memset(bcj, -1, sizeof bcj); } int bget(int i) { return (bcj[i] < 0) ? i : (bcj[i] = bget(bcj[i])); } void bmerge(int i, int j) { int ri = bget(i), rj = bget(j); if (ri == rj) { return; } ltk--; if (bcj[ri] > bcj[rj]) { swap(ri, rj); } bcj[ri] += bcj[rj]; bcj[rj] = ri; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k; gi(n), gi(m), gi(k); for (int i = (1); i < ((n) + 1); i++) gi(c[i]); p2[0] = 1; for (int i = (1); i < ((n) + 1); i++) { p2[i] = 2 * p2[i - 1]; if (p2[i] >= MOD) p2[i] -= MOD; } while (m--) { int u, v; gi(u), gi(v); auto& ref = NO[c[u] ^ c[v]]; ref.push_back({u, v}); } ll notinno = (1ll << k) - NO.size(); ll ans = 1ll * (notinno % MOD) * p2[n] % MOD; binit(); for (auto& k : NO) { ltk = n; for (auto& i : k.second) bmerge(i.first, i.second); ans += p2[ltk]; for (auto& i : k.second) bcj[i.first] = bcj[i.second] = -1; if (ans >= MOD) ans -= MOD; } print(ans); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e3 + 5; int N, Q; int dp[MAXN][MAXN]; int pre[MAXN][MAXN]; int A[MAXN]; int f(int l, int r) { if (dp[l][r] != -1) { return dp[l][r]; } if (l == r - 1) { dp[l][r] = A[l]; } else { dp[l][r] = f(l, r - 1) ^ f(l + 1, r); } return dp[l][r]; } int pref(int l, int r) { if (pre[l][r] != -1) { return pre[l][r]; } pre[l][r] = dp[l][r]; if (l != r - 1) { pre[l][r] = max(pre[l][r], pref(l + 1, r)); pre[l][r] = max(pref(l, r - 1), pre[l][r]); } return pre[l][r]; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { dp[i][j] = -1; pre[i][j] = -1; } } for (int i = 0; i < N; i++) { for (int j = i + 1; j <= N; j++) { f(i, j); } } for (int i = 0; i < N; i++) { for (int j = i + 1; j <= N; j++) { pref(i, j); } } cin >> Q; for (int i = 0; i < Q; i++) { int a, b; cin >> a >> b; cout << pre[a - 1][b] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { int n, x; cin >> n >> x; cout << 2 * x << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int inf = LLONG_MAX; const long long int mod = 1e9 + 7; const long long int N = 1e2 + 1; long long int a[N][N]; long long int n; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (long long int i = 0; i < n; ++i) { for (long long int j = 0; j < n; ++j) { cin >> a[i][j]; } } vector<long long int> good; for (long long int i = 0; i < n; ++i) { bool g = true; for (long long int j = 0; j < n; ++j) { if (a[i][j] == 1 or a[i][j] == 3) { g = false; } } if (g) good.push_back(i); } cout << good.size() << endl; for (auto &i : good) { cout << i + 1 << ; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; stack<pair<int, int> > stc; int main() { ios::sync_with_stdio(false); int n, men, i, cur_max, max; cin >> n; cin >> men; stc.push(make_pair(men, 0)); max = 0; for (i = 0; i < n - 1; i++) { cin >> men; if (stc.top().first > men) { stc.push(make_pair(men, 1)); if (max == 0) max = 1; continue; } cur_max = 0; while (true) { if (stc.top().first > men) { if (stc.top().second > cur_max || stc.top().second == 0) { stc.push(make_pair(men, cur_max + 1)); if (max < cur_max + 1) max = cur_max + 1; break; } stc.pop(); } else { if (stc.top().second == 0) { stc.push(make_pair(men, 0)); break; } if (stc.top().second > cur_max) cur_max = stc.top().second; if (max < cur_max) max = cur_max; stc.pop(); } } } cout << max << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 2; int dp[N][21], ar[N], lis[21]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, i, j, k, l, q; cin >> n >> q; for (i = 1; i <= n; i++) { cin >> ar[i]; for (j = 0; j <= 19; j++) { if (((1 << j) & ar[i])) { dp[i][j] = i; if (!lis[j]) { lis[j] = i; } else { for (k = 0; k <= 19; k++) { dp[i][k] = max(dp[i][k], dp[lis[j]][k]); } lis[j] = i; } } } } for (i = 1; i <= q; i++) { cin >> j >> k; for (l = 0; l <= 19; l++) { if (((1 << l) & ar[j])) { if (dp[k][l] >= j) { cout << Shi n ; break; } } if (l == 19) { cout << Fou n ; } } } }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { int f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } void pre() {} long long a[200009]; long long n, p, k; long long dp[200009]; void solve() { cin >> n >> p >> k; for (int i = 0; i < (n); ++i) cin >> a[i + 1], dp[i] = -1; sort(a + 1, a + n + 1); dp[0] = 0; long long ans = 0; for (int i = 1; i <= (n); ++i) { if (i >= k) dp[i] = min(dp[i - 1], dp[i - k]) + a[i]; else dp[i] = dp[i - 1] + a[i]; if (dp[i] <= p) ans = i; } cout << ans << n ; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); pre(); int t; cin >> t; for (int i = 0; i < (t); ++i) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; string generateString(int a) { string s = ; for (int i = 0; i < a; i++) { char x = a + i; s = s + x; } return s; } string wrapAround(string s, int b, int c) { int x = c - b; for (int i = 0; i < x; i++) { s += s[i % b]; } return s; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } vector<vector<int> > v; for (int i = 0; i < 26; i++) { vector<int> x(n + 1, 0); v.push_back(x); } for (int j = 0; j < n; j++) { v[A[j] - 1][j + 1]++; } for (int i = 0; i < 26; i++) { for (int j = 1; j < n + 1; j++) { v[i][j] += v[i][j - 1]; } } vector<vector<int> > pos; pos.resize(26); for (int i = 0; i < n; i++) { pos[A[i] - 1].push_back(i); } int ans = 1; for (int i = 0; i < 26; i++) { int sz = pos[i].size(); int lim = sz / 2; if (sz != 0) { for (int k = 0; k < lim; k++) { int st = pos[i][k]; int en = pos[i][sz - k - 1]; int temp = 0; for (int j = 0; j < 26; j++) { int x = v[j][en] - v[j][st + 1]; temp = max(x, temp); } ans = max(ans, 2 * (k + 1) + temp); } } } cout << ans << n ; } }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; int n, k; int a[200000]; bool ok() { int cur = a[k - 1]; for (int i = k; i < n; ++i) { if (a[i] != cur) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < (int)(n); i++) cin >> a[i]; if (!ok()) cout << -1 << endl; else { int ans = 0; int cur = a[k - 1]; for (int i = 0; i < (int)(k); i++) { if (a[i] != cur) ans = i + 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a[1000005] = {1, 1}, b[1000005], num; long long x2, ans = 100000000; void init() { long long i, j; for (i = 2; i <= 1000000; i++) { if (a[i]) continue; for (j = i * i; j <= 1000000; j += i) a[j] = 1; } for (i = 1; i <= 1000000; i++) if (a[i] == 0) b[++num] = i; } int main(void) { init(); scanf( %lld , &x2); long long x = x2, p = 0; for (long long i = 1; b[i] * b[i] <= x; i++) while (x % b[i] == 0) x /= b[i], p = b[i]; if (x != 1) p = x; for (long long i = x2 - p + 1; i <= x2; i++) { long long tmp = i; long long pp = p; for (long long j = 1; b[j] * b[j] <= tmp; j++) while (tmp % b[j] == 0) tmp /= b[j], pp = b[j]; if (tmp != 1) pp = tmp; if (pp == i) continue; ans = min(ans, i - pp + 1); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> namespace { using namespace std; template <typename T> inline bool whitespace(const vector<T> &) { return false; } template <typename T> inline bool whitespace(const T &) { return true; } inline bool whitespace(const char) { return false; } template <typename S, typename T> ostream &operator<<(ostream &os, const pair<S, T> &p) { os << p.first << (whitespace(p.second) ? : ) << p.second << n ; return os; } template <typename S, typename T> istream &operator>>(istream &is, pair<S, T> &p) { is >> p.first >> p.second; return is; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) { os << t << (whitespace(t) ? : ); } os << n ; return os; } template <typename T> istream &operator>>(istream &is, deque<T> &v) { for (T &t : v) is >> t; return is; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { for (const T &t : v) { os << t << (whitespace(t) ? : ); } os << n ; return os; } template <typename T> T gcd(T a, T b) { while (b) { T mod = a % b; a = b; b = mod; } return a; } template <typename T> T sum(const vector<T> &v) { T s = 0; for (T value : v) s += value; return s; } template <typename T> vector<T> prefix_sum(const vector<T> &v) { vector<T> res(v.size() + 1); res[0] = 0; for (long long i = 0; i < v.size(); ++i) res[i + 1] = res[i] + v[i]; return res; } template <typename S, typename T> S min(S a, T b) { return a < (S)b ? a : b; } template <typename S, typename T> S max(S a, T b) { return a < (S)b ? b : a; } class exit_exception : exception {}; istream &in = cin; ostream &out = cout; template <typename T> void answer(T ans) { out << ans << n ; throw exit_exception(); } struct autoint { long long x; autoint() { in >> x; } operator long long() { return x; } autoint(autoint &i) { x = i.x; } long long operator--() { return --x; } long long operator--(signed) { return x--; } long long operator++() { return ++x; } long long operator++(signed) { return x++; } }; template <typename T> struct autoread : T { template <typename... Args> autoread(Args &&...args) : T(args...) { in >> *dynamic_cast<T *>(this); } }; } // namespace void solve(); signed main() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); long long t = 1; cin >> t; do { try { solve(); } catch (exit_exception &) { } } while (--t); return 0; } void solve() { autoint n, k; string s; cin >> s; vector<map<char, long long>> groups((k + 1) / 2); for (long long i = 0; i < (k + 1) / 2; ++i) { for (long long j = i; j < n; j += k) groups[i][s[j]]++; if (i + i < k - 1) for (long long j = k - i - 1; j < n; j += k) groups[i][s[j]]++; } long long ans = 0; for (auto m : groups) { vector<long long> cnts; for (auto p : m) cnts.push_back(p.second); stable_sort(cnts.rbegin(), cnts.rend()); ans += accumulate(cnts.begin(), cnts.end(), 0ll) - cnts.front(); } answer(ans); }
#include <bits/stdc++.h> using namespace std; void getint(int &v) { char ch, fu = 0; for (ch = * ; (ch < 0 || ch > 9 ) && ch != - ; ch = getchar()) ; if (ch == - ) fu = 1, ch = getchar(); for (v = 0; ch >= 0 && ch <= 9 ; ch = getchar()) v = v * 10 + ch - 0 ; if (fu) v = -v; } const int MO = 1e9 + 7; long long P[5555], ans, p[5555], T; int n, k, a[5555], f[5010][5010]; long long pw(long long x, long long y) { long long t = 1; for (; y; y >>= 1) { if (y & 1) t = t * x % MO; x = x * x % MO; } return t; } int main() { cin >> n >> k; T = 1; for (int i = 1; i <= n; ++i) getint(a[i]), T = T * a[i] % MO; P[0] = 1; for (int i = 1; i <= n; ++i) P[i] = P[i - 1] * (k - i + 1) % MO; f[0][0] = 1; for (int i = 1; i <= n; ++i) { f[i][0] = 1; for (int j = 1; j <= i; ++j) f[i][j] = (f[i - 1][j] + (long long)f[i - 1][j - 1] * a[i]) % MO; } long long t = MO - pw(n, MO - 2); p[0] = 1; for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * t % MO; for (int i = 0; i <= n; ++i) { if (n - i > k) continue; long long x = f[n][i] * P[n - i] % MO * p[n - i] % MO; (ans += x) %= MO; } cout << (T - ans + MO) % MO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 201; const long long K = 201; const long long X = 201; long long dp[N][X]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k, x; cin >> n >> k >> x; vector<long long> picBeauty; picBeauty.push_back(-1); for (long long i = 1; i <= n; i++) { long long x; cin >> x; picBeauty.push_back(x); } long long minimumPosts = n / k; if (x < minimumPosts) { cout << -1 << endl; return 0; } for (long long i = 0; i <= n; i++) { for (long long j = 0; j <= x; j++) { dp[i][j] = 0; } } for (long long i = 1; i <= k; i++) { dp[i][1] = picBeauty[i]; } long long flag = 0; for (long long j = 2; j <= x; j++) { for (long long i = 1; i <= n; i++) { long long tempFlag = 0; long long backAllowed = i - k; for (long long y = i - 1; y >= 1 && y >= backAllowed; y--) { if (dp[y][j - 1] > 0) dp[i][j] = max(dp[i][j], dp[y][j - 1] + picBeauty[i]); } } } long long ans = 0; for (long long i = n - k + 1; i <= n; i++) { ans = max(ans, dp[i][x]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(500005); int visited[500005]; int counter; void dfs(int node, int color) { counter++; visited[node] = color; for (int i = 0; i < adj[node].size(); ++i) { if (visited[adj[node][i]] == 0) dfs(adj[node][i], color); } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; while (test--) { int n, val; cin >> n; vector<int> arr; for (int i = 0; i < n; ++i) { cin >> val; arr.push_back(val); } string s; cin >> s; vector<pair<int, int> > v; int flag = 0, start = -1, ender = -1; for (int i = 0; i < s.size(); ++i) { if (flag == 0 && s[i] == 0 ) continue; if (flag == 0 && s[i] == 1 ) { start = i; flag = 1; continue; } if (flag == 1 && s[i] == 0 ) { ender = i; v.push_back({start, ender}); start = -1; ender = -1; flag = 0; continue; } if (flag == 1 && s[i] == 1 ) continue; } if (flag == 1) v.push_back({start, n - 1}); vector<pair<int, int> >::iterator it; for (it = v.begin(); it != v.end(); ++it) { sort(arr.begin() + it->first, arr.begin() + it->second + 1); } flag = 0; for (int i = 1; i < n; ++i) { if (arr[i] <= arr[i - 1]) { flag = 1; break; } } if (flag == 1) cout << NO ; else cout << YES ; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[5]; bool able[5]; int main() { int T; scanf( %d , &T); while (T--) { for (int i = 1; i <= 4; i++) able[i] = true; bool flag = true; for (int i = 1; i <= 4; i++) scanf( %d , &a[i]); if ((a[1] + a[2]) % 2 == 1) flag = false; if (flag) { able[1] = able[2] = false; if (a[4] == 0 && a[1] == 0) able[4] = false; if (a[2] == 0 && a[3] == 0) able[3] = false; } else { able[3] = able[4] = false; if (a[4] == 0 && a[1] == 0) able[1] = false; if (a[2] == 0 && a[3] == 0) able[2] = false; } for (int i = 1; i <= 4; i++) { if (able[i]) printf( Ya ); else printf( Tidak ); } puts( ); } return 0; }
#include <bits/stdc++.h> using namespace std; int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n, m, k; int q(int x) { return x * (x + 1) / 2; } int p[200005]; int b[200005]; int u[200005]; int dfs(int v) { u[v] = 1; if (!u[p[v]]) return dfs(p[v]) + 1; else return 1; } int solve() { int n; cin >> n; for (int i = 0; i < n; ++i) { scanf( %d , &p[i + 1]); } int c = 0; for (int i = 0; i < n; ++i) { scanf( %d , &b[i + 1]); c += b[i + 1]; } if (n == 1) { cout << (c + 1) % 2; return 0; } int res = 0; int ones = 0; for (int i = 1; i <= n; ++i) if (!u[i]) { int k = dfs(i); if (k == 1) ++ones; else ++res; } if (res == 1 && ones == 0) { cout << (c + 1) % 2; return 0; } if (res == 0) cout << ones + (c + 1) % 2; else if (ones == 0) cout << res + (c + 1) % 2; else cout << res + ones + (c + 1) % 2; return 0; } int main() { int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int tux, foo, bar, baz, quz = 1; int main() { scanf( %d , &tux); for (int i = 1; i <= tux; ++i) { int pur; scanf( %d , &pur); foo += pur, bar++; if (foo * quz > baz * bar) baz = foo, quz = bar; } printf( %.6lf , baz * 1.0 / quz); }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const long long MAXN = 5209; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return x * f; } long long n, m, a[MAXN], b[MAXN], p[MAXN]; double f[57][MAXN]; bool check(double x) { for (int i = n - 1; ~i; i--) { for (int j = m + 1; j <= MAXN; j++) f[i + 1][j] = x; for (int j = 0; j <= m; j++) f[i][j] = min((f[i + 1][j + a[i]] + a[i]) * p[i] * 0.01 + (f[i + 1][j + b[i]] + b[i]) * (100 - p[i]) * 0.01, x); } return f[0][0] < x; } signed main() { n = read(), m = read(); for (int i = 0; i < n; i++) a[i] = read(), b[i] = read(), p[i] = read(); double l = 0, r = 1e9; int FTL = 0; while (l + EPS < r) { if (++FTL >= 1000) break; double mid = (l + r) * 0.5; if (check(mid)) r = mid; else l = mid; } printf( %.10f , l); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, K, i; cin >> N >> K; if (K == 1 || (N <= 3) || (K > 3) || (N == 4 and K == 2)) return cout << -1 << endl, 0; if (K == 2) { cout << N - 1 << endl; for (i = 1; i <= N - 1; i++) cout << i << << i + 1 << endl; } else { cout << 3 + (N - 4) * (2) << endl; cout << 1 2 << endl; cout << 2 3 << endl; cout << 3 4 << endl; for (i = 5; i <= N; i++) cout << i << 2 << endl, cout << i << 4 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; int w, h; cin >> w >> h >> n; multiset<int> cuth, cutw, maxh, maxw; cuth.insert(0); cuth.insert(h); cutw.insert(0); cutw.insert(w); maxh.insert(h); maxw.insert(w); int len; for (int i = 0; i < n; i++) { char str[10]; cin >> str; cin >> len; if (str[0] == H ) { multiset<int>::iterator it, it1, it2; it = cuth.lower_bound(len); it1 = it; it1--; int val = *it - *it1; it2 = maxh.lower_bound(val); maxh.erase(it2); cuth.insert(len); maxh.insert((len - *it1)); maxh.insert((*it - len)); } else { multiset<int>::iterator it, it1, it2; it = cutw.lower_bound(len); it1 = it; it1--; int val = *it - *it1; it2 = maxw.lower_bound(val); maxw.erase(it2); cutw.insert(len); maxw.insert((len - *it1)); maxw.insert((*it - len)); } long long int l1 = *(maxw.rbegin()); long long int l2 = *(maxh.rbegin()); long long int ans = l1 * l2; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[111][111], x[111], k; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; int in = 0; for (int i = 0; i < n; i++) if (a[i][0] < a[in][0]) in = i; for (int i = 0; i < n; i++) x[i] = a[i][0] - a[in][0]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[in][j] + x[i] < a[i][j]) { cout << NO << endl; return 0; } else if (a[in][j] + x[i] > a[i][j]) { if (k && (a[in][j] + x[i]) % k != a[i][j]) { cout << NO << endl; return 0; } k = a[in][j] + x[i] - a[i][j]; } if (!k) k = 2e9; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if ((a[in][j] + x[i]) % k != a[i][j]) { cout << NO << endl; return 0; } cout << YES << endl; cout << k << endl; for (int i = 0; i < n; i++) cout << x[i] << ; cout << endl; for (int j = 0; j < m; j++) cout << a[in][j] << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define int long long #define double long double #define pb push_back #define mp make_pair #define endl n #define all(x) x.begin(),x.end() #define fill(a,b) memset(a,b,sizeof(a)) #define sz(x) (int)x.size() #define ff first #define ss second #define lb lower_bound #define ub upper_bound #define bs binary_search #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define deb(x) cout<<#x<< <<x<<endl; #define T int t; cin>>t; while(t--) #define fst ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int firstMissingPositive(int arr[], int n) { for (int i = 0; i < n; i++) { while (arr[i] >= 1 && arr[i] <= n && arr[i] != arr[arr[i] - 1]) { swap(arr[i], arr[arr[i] - 1]); } } for (int i = 0; i < n; i++) { if (arr[i] != i + 1) { return i + 1; } } return n + 1; } signed main() { fst; #ifndef ONLINE_JUDGE freopen( input3.txt , r , stdin); freopen( output3.txt , w , stdout); #endif T{ int n,p,q, m,h, k, sum = 0, cnt=0, x, y, z, flag = 0, pos, diff,r=0,l=0,u=0,d=0,cnt1=0,cnt0=0,cnt2=0,ans,mi=INT_MAX,mx=INT_MIN; cin>>n; int a[n]; vector<int> v,v1,v2; set<int> st; map<int,int> mp; rep(i,0,n) { cin>>x; if(find(all(st),x) != st.end()) v.pb(x); st.insert(x); } for(auto i:st) cout<<i<< ; sort(all(v)); for(auto i:v) cout<<i<< ; cout<<endl; v.clear(); st.clear(); } }
#include <bits/stdc++.h> using namespace std; int n, m; int w; int q; vector<int> adj[5000 + 1]; vector<pair<int, int>> store[5000 + 1]; bool vis[5000 + 1]; int dfs(int g, int r, int a) { for (int i = 1; i <= n; i++) vis[i] = false; queue<pair<int, int>> q; priority_queue<pair<int, int>> pq; q.push({g, 0}); long long sz = 0; long long cost = 0; while (!q.empty()) { auto loc = q.front(); q.pop(); if (vis[loc.first]) continue; vis[loc.first] = true; for (auto p : store[loc.first]) { sz += p.second; cost += 1LL * p.first * p.second; pq.push({p.first, p.second}); } while (!pq.empty()) { if (sz <= r) break; auto p = pq.top(); pq.pop(); sz -= p.second; cost -= 1LL * p.first * p.second; if (sz < r) { pq.push({p.first, r - sz}); cost += 1LL * p.first * (r - sz); sz = r; } } if (sz == r && cost <= a) { return loc.second; } for (int e : adj[loc.first]) { if (!vis[e]) { q.push({e, loc.second + 1}); } } } return -1; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } cin >> w; for (int i = 0; i < w; i++) { int c, k, p; cin >> c >> k >> p; store[c].push_back({p, k}); } cin >> q; for (int i = 0; i < q; i++) { int g, r, a; cin >> g >> r >> a; int ans = dfs(g, r, a); cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return + s + ; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto& x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } void test_case() { int n, l, r; cin >> n >> l >> r; vector<int> mn(n + 1); int rem = l - 1; for (int i = rem + 1; i <= n; ++i) { mn[i] = 1; } for (int i = rem, nxt = 2; i >= 1; --i, nxt *= 2) { mn[i] = nxt; } vector<int> mx(n + 1); for (int i = 1, unq = 0, nxt = 1; i <= n; ++i) { mx[i] = nxt; ++unq; if (unq < r) { nxt *= 2; } } cout << accumulate(mn.begin(), mn.end(), 0) << << accumulate(mx.begin(), mx.end(), 0) << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); test_case(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000005; using ll = long long; int n, m, p, a[N]; struct TNode { int l, r; vector<ll> f; ll sum; } t[N << 2]; inline int lc(int pos) { return pos << 1; } inline int rc(int pos) { return pos << 1 | 1; } void pushup(int pos) { t[pos].sum = t[lc(pos)].sum + t[rc(pos)].sum; int ptr = 0; for (int i = 1; i <= t[pos].r - t[pos].l + 1; i++) t[pos].f[i] = (LLONG_MAX >> 2); for (int i = 0; i <= t[lc(pos)].r - t[lc(pos)].l + 1; i++) { if (ptr > t[rc(pos)].r - t[rc(pos)].l + 1) --ptr; while (ptr <= t[rc(pos)].r - t[rc(pos)].l + 1) { ll ret = t[rc(pos)].f[ptr] - (t[lc(pos)].sum - i * 1ll * p); ll val = t[lc(pos)].f[i + 1] - 1 + (t[lc(pos)].sum - i * 1ll * p); if (val < t[rc(pos)].f[ptr]) { if (ptr > 0) --ptr; break; } t[pos].f[i + ptr] = min(t[pos].f[i + ptr], max(t[lc(pos)].f[i], ret)); ++ptr; } } } void build(int pos, int l, int r) { t[pos].l = l; t[pos].r = r; t[pos].f.resize(r - l + 1 + 1); t[pos].f[0] = LLONG_MIN >> 2; t[pos].f.push_back(LLONG_MAX >> 2); if (l == r) { t[pos].sum = a[l]; t[pos].f[1] = p - a[l]; return; } int mid = (l + r) >> 1; build(lc(pos), l, mid); build(rc(pos), mid + 1, r); pushup(pos); } ll query(int pos, int l, int r, ll cur = 0) { if (t[pos].l == l && t[pos].r == r) { assert(cur < LLONG_MAX / 4); ll pp = upper_bound(t[pos].f.begin(), t[pos].f.end(), cur) - t[pos].f.begin() - 1; return cur + t[pos].sum - p * pp; } int mid = (t[pos].l + t[pos].r) >> 1; if (r <= mid) return query(lc(pos), l, r, cur); else if (l > mid) return query(rc(pos), l, r, cur); else return query(rc(pos), mid + 1, r, query(lc(pos), l, mid, cur)); } int main() { ios::sync_with_stdio(false); cin >> n >> m >> p; for (int i = 1; i <= n; i++) cin >> a[i]; build(1, 1, n); while (m--) { int l, r; cin >> l >> r; cout << query(1, l, r) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long wph; long long mul(long long a, long long b) { long long c = (a * b - (long long)((long double)a / wph * b) * wph); return c < 0 ? c + wph : c; } void fwt(long long *a, int n, int f) { for (int k = 2, mid = 1; k <= n; k <<= 1, mid <<= 1) for (int i = 0; i < n; i += k) for (int j = 0; j < mid; j++) { long long x = a[i + j], y = a[i + mid + j]; a[i + j] = (x + y) % wph, a[i + mid + j] = (x - y + wph) % wph; } if (f) for (int i = 0; i < n; i++) a[i] /= n; } long long c[2000010], b[2000010], f[2000010]; long long ksm(long long bs, long long mi) { long long ans = 1; while (mi) { if (mi & 1) ans = mul(ans, bs); bs = mul(bs, bs); mi >>= 1; } return ans; } long long count(long long x) { long long cnt = 0; while (x) x &= (x - 1), cnt++; return cnt; } int main() { long long t; int m, n; scanf( %d%I64d%I64d , &m, &t, &wph); n = 1 << m; wph *= n; for (int i = 0; i < n; i++) scanf( %I64d , &f[i]); for (int i = 0; i <= m; i++) scanf( %I64d , &b[i]); for (int i = 0; i < n; i++) c[i] = b[count(i)]; fwt(c, n, 0); fwt(f, n, 0); for (int i = 0; i < n; i++) c[i] = ksm(c[i], t); for (int i = 0; i < n; i++) c[i] = mul(c[i], f[i]); fwt(c, n, 1); wph /= n; for (int i = 0; i < n; i++) printf( %I64d n , c[i] % wph); return 0; }
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; }; struct Line { double A, B, C; }; Line Build(Point A, Point B) { Line res; res.A = A.y - B.y; res.B = B.x - A.x; res.C = A.x * B.y - B.x * A.y; return res; } Line Per(Line l, Point A) { Line res; res.A = -l.B; res.B = l.A; res.C = -(res.A * A.x + res.B * A.y); return res; } Line Par(Line l, Point A) { Line res; res.A = l.A; res.B = l.B; res.C = -(l.A * A.x + l.B * A.y); return res; } Point Inter(Line a, Line b) { Point res; res.x = (a.B * b.C - b.B * a.C) / (a.A * b.B - b.A * a.B); res.y = (a.A * b.C - b.A * a.C) / (b.A * a.B - b.B * a.A); return res; } Point Middle(Point A, Point B) { Point res; res.x = (A.x + B.x) / 2.0; res.y = (A.y + B.y) / 2.0; return res; } double Dist(Point A, Point B) { double res = sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); return res; } double Dist2(Point A, Point B) { double res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); return res; } Point Mir(Point A, Point B) { Point res; res.x = B.x * 2 - A.x; res.y = B.y * 2 - A.y; return res; } double Cp2(Point A, Point B) { double res = A.x * B.y - A.y * B.x; return res; } double Cp3(Point O, Point A, Point B) { double res = (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y); return res; } bool Zero(double x) { return (fabs(x) < 1e-9 ? true : false); } Line Midper(Point A, Point B) { Line res = Per(Build(A, B), Middle(A, B)); return res; } bool Check(Point ans[]) { double tmp; for (int i = 0; i < 4; i++) { if (Zero(Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]))) return false; if (i == 0) tmp = Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]); else if (tmp * Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]) < 0) return false; } return true; } bool Mak(Point A, Point B, Point C) { if (Cp3(A, B, C) > 1e-9) swap(B, C); Line l1 = Midper(B, A); Line l2 = Midper(B, C); Point E = Inter(l1, l2); Point F = Mir(E, B); Point ans[4]; ans[0] = Inter(Par(l2, F), l1); ans[1] = Mir(ans[0], B); ans[2] = Mir(ans[1], C); ans[3] = Mir(ans[0], A); if (Check(ans) == false) return false; puts( YES ); for (int i = 3; i < 7; i++) { printf( %.9f %.9f , ans[i & 3].x, ans[i & 3].y); putchar(i < 6 ? 32 : 10); } return true; } int main() { int t; scanf( %d , &t); while (t--) { Point a, b, c; scanf( %lf%lf%lf%lf%lf%lf , &a.x, &a.y, &b.x, &b.y, &c.x, &c.y); if (!Zero(Cp3(a, b, c)) && (Mak(a, b, c) || Mak(b, c, a) || Mak(c, a, b))) continue; puts( NO n ); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int n, m; pair<long long int, bool> All[120]; long long int ans = 0, na; void GetAll() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> All[i].first; All[i].second = false; } int buf; for (int i = 0; i < m; i++) { cin >> buf; All[buf - 1].second = true; } } void Run() { sort(All, All + n); for (int i = 0; i < n; i++) { if (!All[i].second) { na += All[i].first; } } for (int i = n - 1; i >= 0; i--) { if (!All[i].second) { continue; } if (All[i].first > na) { na += All[i].first; } else { na += na; } } cout << na; } int main() { GetAll(); Run(); return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } bool prime(long long int n) { for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } long long binexpm(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } long long binexp(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long int n, p; vector<long long int> adj[10001]; map<pair<long long int, long long int>, long long int> mp; bool vis[10001] = {false}; map<pair<long long int, long long int>, long long int> res; long long int x, y = -1; long long int dm = INT_MAX; bool xx = true; long long int in[10001] = {0}; long long int out[10001] = {0}; bool dfs(long long int node) { if (vis[node]) xx = false; vis[node] = true; for (auto child : adj[node]) { dm = min(dm, mp[{node, child}]); if (adj[child].size() == 0) y = child; xx &= dfs(child); } return xx; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> p; for (long long int i = 0; i < p; i++) { long long int a, b, d; cin >> a >> b >> d; out[a]++; in[b]++; adj[a].push_back(b); mp[make_pair(a, b)] = d; } long long int ans = 0; for (long long int i = 1; i <= n; i++) { bool flag; dm = INT_MAX; if (out[i] == 1 && in[i] == 0) { x = i; flag = dfs(i); if (flag && y != -1) { ans++; res[make_pair(x, y)] = dm; } } } cout << ans << n ; if (ans != 0) { for (auto it : res) { cout << it.first.first << << it.first.second << << it.second << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int h, i, j, x, l, r, pa[200200], pb[200200]; bool ans; int main() { scanf( %d , &h); for (j = 0; j <= h; j++) { scanf( %d , &x); for (i = 1; i <= x; i++) { pa[r + i] = l; pb[r + i] = min(l + i - 1, r); if (pa[r + i] != pb[r + i]) ans = true; } l = r + 1; r = r + x; } if (ans) { puts( ambiguous ); for (i = 1; i <= r; i++) printf( %d , pa[i]); puts( ); for (i = 1; i <= r; i++) printf( %d , pb[i]); puts( ); } else puts( perfect ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, q; int matrix[100][100]; int ans[10000]; void fill() { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { matrix[i][j] = i * m + j; } } } void solve_1(int i) { int temp; temp = matrix[i][m - 1]; matrix[i][m - 1] = matrix[i][0]; for (int j = m - 2; j >= 0; --j) { int new_temp = matrix[i][j]; matrix[i][j] = temp; temp = new_temp; } } void solve_2(int j) { int temp; temp = matrix[n - 1][j]; matrix[n - 1][j] = matrix[0][j]; for (int i = n - 2; i >= 0; --i) { int new_temp = matrix[i][j]; matrix[i][j] = temp; temp = new_temp; } } int main(int argc, char *argv[]) { memset(ans, 0, sizeof(ans)); scanf( %d%d%d , &n, &m, &q); fill(); while (q--) { int t; scanf( %d , &t); if (t == 1) { int n_i; scanf( %d , &n_i); n_i--; solve_1(n_i); } else if (t == 2) { int m_i; scanf( %d , &m_i); m_i--; solve_2(m_i); } else { int r, c, x; scanf( %d%d%d , &r, &c, &x); r--; c--; ans[matrix[r][c]] = x; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { printf( %d , ans[i * m + j]); } puts( ); } return 0; }
#include <bits/stdc++.h> using namespace std; int d, s; int E[2510050][10]; int Get(int x, int y) { return x * d + y; } int dis[2710500]; char ok[2700050] = {}; vector<int> ans; int ST, EN; int dfs(int x) { if (x == EN) return 1; ok[x] = 1; for (int u = 0; u < 10; u++) if (x != 0 || u != 0) { int v = E[x][u]; if (dis[v] == dis[x] + 1 && ok[v] == 0) { ans.push_back(u); if (dfs(v)) return 1; ans.pop_back(); } } return 0; } int main() { scanf( %d%d , &d, &s); for (int i = 0; i <= s; i++) for (int j = 0; j < d; j++) { int x = Get(i, j); for (int e = 0; e < 10; e++) { if (i == 0 && j == 0 && e == 0) continue; int ni = i + e; int nj = (10 * j + e) % d; if (ni <= s) { int xx = Get(ni, nj); E[x][e] = xx; } } } ST = Get(0, 0), EN = Get(s, 0); vector<int> v; v.push_back(ST); memset(dis, -1, sizeof dis); dis[ST] = 0; for (int i = 0; i < (int)v.size(); i++) { int t = v[i]; for (int u = 0; u < 10; u++) if (t != 0 || u != 0) { int w = E[t][u]; if (dis[w] == -1) { dis[w] = dis[t] + 1; v.push_back(w); } } } if (dis[EN] == -1) puts( -1 ); else { dfs(ST); for (int e : ans) printf( %d , e); puts( ); } }
#include <bits/stdc++.h> using namespace std; int n, i, tmp, res, a[10000]; bool kt; int main() { scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %d , &a[i]); kt = true; for (i = 3; i <= n; i++) if (a[i] - a[i - 1] != a[i - 1] - a[i - 2]) kt = false; if (kt == false) { printf( %d , a[n]); return 0; } tmp = a[1] - a[2]; res = a[n] - tmp; printf( %d , res); }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool Up(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool Down(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } const int MAXN = 100023; int N, M, color[MAXN], ans[MAXN]; vector<int> fir[MAXN], side[2]; int tot = 2; void BFS() { static int Q[MAXN]; int head = 0, tail = 0; memset(color, -1, sizeof(color)); for (int S = (1); S <= (N); ++S) if (color[S] == -1) for (color[S] = 0, Q[++tail] = S; head < tail;) { int x = Q[++head], y; side[color[x]].push_back(x); for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end(); ++itr) if (color[y = *itr] == -1) { Q[++tail] = y; color[y] = color[x] ^ 1; } } } void Print() { for (int c = (0); c <= (1); ++c) for (vector<int>::iterator itr(side[c].begin()); itr != side[c].end(); ++itr) { int x = *itr; if (!ans[x]) ans[x] = ++tot; } puts( YES ); for (int i = (1); i <= (N); ++i) printf( %d%c , ans[i] / 3, i == N ? n : ); } void Paint(int x) { static int forbid[MAXN]; memset(forbid, 0, sizeof(forbid)); for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end(); ++itr) forbid[*itr] = 1; int cnt = 0; ans[x] = ++tot; for (vector<int>::iterator itr(side[color[x] ^ 1].begin()); itr != side[color[x] ^ 1].end(); ++itr) { int y = *itr; if (!ans[y] && !forbid[y]) { ans[y] = ++tot; if (++cnt == 2) break; } } } int main() { memset(ans, 0, sizeof(ans)); scanf( %d %d , &N, &M); for (int i = (1); i <= (M); ++i) { int a, b; scanf( %d %d , &a, &b); fir[a].push_back(b); fir[b].push_back(a); } BFS(); if (side[0].size() % 3 == 0) { Print(); return 0; } if (side[0].size() % 3 == 2) { side[0].swap(side[1]); for (int i = (1); i <= (N); ++i) color[i] ^= 1; } for (vector<int>::iterator itr(side[0].begin()); itr != side[0].end(); ++itr) { int x = *itr; if (fir[x].size() + 2 <= side[1].size()) { Paint(x); Print(); return 0; } } int cnt = 0; for (vector<int>::iterator itr(side[1].begin()); itr != side[1].end(); ++itr) { int x = *itr; if (fir[x].size() + 2 <= side[0].size()) { Paint(x); if (++cnt == 2) { Print(); return 0; } } } puts( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<pair<int, int> > a; set<pair<int, int> > d; void solve(int l, int r) { if (r - l <= 1) return; int m = (l + r) / 2; solve(l, m); solve(m, r); for (int i = l; i < r; ++i) { d.insert({a[m].first, a[i].second}); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; a.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; d.insert(a[i]); } sort(a.begin(), a.end()); solve(0, n); cout << d.size() << n ; for (auto i : d) { cout << i.first << << i.second << n ; } }
#include <bits/stdc++.h> using namespace std; vector<int> g[300011]; long long ans[300011]; struct P { long long dis = -1, val = 0; } temp; vector<P> m[300011]; map<long long, long long> bit; void add(long long i, long long val) { i = 1000000000000000 - i; for (; i < 1000000000000000; i += (i & (-i))) bit[i] += val; } long long get(long long i) { long long ret = 0; i = 1000000000000000 - i; for (; i > 0; i -= (i & (-i))) ret += bit[i]; return ret; } void dfs(int u, int par = 0, long long lev = 1) { for (auto i : m[u]) add(lev + i.dis, i.val); ans[u] = get(lev); for (auto v : g[u]) { if (v != par) dfs(v, u, lev + 1); } for (auto i : m[u]) add(lev + i.dis, -i.val); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, x, y, v, d; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } int q; cin >> q; while (q--) { cin >> v >> d >> x; temp.dis = d; temp.val = x; m[v].push_back(temp); } dfs(1); for (int i = 1; i <= n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; void print(int a) { cout << a << ; } void print(long long a) { cout << a << ; } void print(double a) { cout << a << ; } void print(string a) { cout << a << ; } template <typename T1, typename T2> void print(pair<T1, T2> a) { print(a.first); cout << ::: ; print(a.second); } template <typename Container> void print(const Container& c) { for (auto it = c.begin(); it != c.end(); it++) { print(*it); } cout << endl; } vector<long long> tot_comb(51, 0); vector<long long> fac(51, 1); int CALC_MAX = 0; long long INF = 2e18; long long get_fac(int x) { if (x <= 0) return 1; return fac[x]; } long long mul(long long x, long long y) { if (x >= INF || y >= INF || x * y >= INF || x * y < 0) return INF; return x * y; } long long add(long long x, long long y) { if (x >= INF || y >= INF || x + y >= INF) return INF; return x + y; } void fill_fac() { fac[0] = 1; fac[1] = 1; for (int i = 2; i <= 50; i++) { fac[i] = mul(fac[i - 1], i); } } void fill_tot_comb() { tot_comb[0] = 1; tot_comb[1] = 1; tot_comb[2] = 2; tot_comb[3] = 4; for (int i = 4; i <= 50; i++) { tot_comb[i] = 0; for (int j = 1; j <= i; j++) { tot_comb[i] = add(tot_comb[i], mul(get_fac(j - 2), tot_comb[i - j])); } } } void KthPermhelp(int k, long long n, int st) { vector<int> perm(k + 1, 0); vector<int> pos(k + 1, 0); perm[1] = k; pos[k] = 1; long long cnt = n; vector<bool> taken(k + 1, false); taken[k] = true; for (int i = 2; i <= k; i++) { for (int j = 1; j < k; j++) { if (!taken[j]) { int jk = i; while (taken[jk]) jk = pos[jk]; if (j == jk && i != k) continue; long long tot_with_j_i = get_fac(k - 1 - i); if (cnt > tot_with_j_i) { cnt -= tot_with_j_i; } else { perm[i] = j; taken[j] = true; pos[j] = i; break; } } } } if (k == 2) { perm[1] = 2; perm[2] = 1; } for (int i = 1; i <= k; i++) cout << perm[i] + st << ; } void KthPerm(int n, long long k, int st) { if (n == 0) return; if (tot_comb[n] < k) { cout << -1; return; } for (int i = 1; i <= n; i++) { if (mul(get_fac(i - 2), tot_comb[n - i]) >= k) { KthPermhelp(i, (k / tot_comb[n - i]) + (k % tot_comb[n - i] > 0), st); KthPerm(n - i, (k % tot_comb[n - i]) + ((k % tot_comb[n - i] == 0) ? tot_comb[n - i] : 0), st + i); break; } k -= mul(get_fac(i - 2), tot_comb[n - i]); } } int main() { fill_fac(); fill_tot_comb(); int t; cin >> t; while (t--) { int n; long long int k; cin >> n >> k; KthPerm(n, k, 0); cout << endl; } }
#include <bits/stdc++.h> #define pb push_back #define int long long #define debug(n) cerr << #n << = << n << n ; using namespace std; const int MaxN = 1e6 + 10; const int Mod = 1e9 + 7; const int inf = 1e18; int cnt[100] , a[MaxN]; void solve() { int n; cin >> n; for(int i = 0; i < n; i++) { cin >> a[i]; for(int j = 0; j <= 60; j++) if(a[i] & (1LL << j)) cnt[j]++; } int ans = 0; for(int i = 0; i < n; i++) { int x = 0 , y = 0; for(int j = 0; j <= 60; j++) { int p = (1LL << j) % Mod; if(a[i] & (1LL << j)) { x = (x + p * cnt[j]) % Mod; y = (y + p * n) % Mod; } else y = (y + p * cnt[j]) % Mod; } ans = (ans + x * y) % Mod; } cout << ans << n ; fill(cnt , cnt + 100 , 0); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int mo = 998244353; int fac[300005], inv[300005]; void init() { fac[0] = inv[0] = inv[1] = 1; for (int i = (int)(2); i <= (int)(300005 - 1); i++) inv[i] = 1ll * inv[mo % i] * (mo - mo / i) % mo; for (int i = (int)(1); i <= (int)(300005 - 1); i++) { fac[i] = 1ll * fac[i - 1] * i % mo; inv[i] = 1ll * inv[i - 1] * inv[i] % mo; } } int power(int x, int y) { int s = 1; for (; y;) { if (y & 1) s = 1ll * s * x % mo; if (y /= 2) x = 1ll * x * x % mo; } return s; } const int N = 100005; int freq[N], cnt[N]; void add(int x) { cnt[freq[x]]--; cnt[++freq[x]]++; } void del(int x) { cnt[freq[x]]--; cnt[--freq[x]]++; } vector<int> heavy; int n, m, Q; int e[N], ans[N]; struct que { int l, r, k, id; } q[N]; const int blk = 200; bool cmpk(que a, que b) { return pair<int, int>(a.k, a.l - a.r) < pair<int, int>(b.k, b.l - b.r); } bool cmpmo(que a, que b) { return pair<int, int>(a.l / blk, a.r) < pair<int, int>(b.l / blk, b.r); } int main() { init(); scanf( %d%d%d , &n, &m, &Q); for (int i = (int)(1); i <= (int)(n); i++) { scanf( %d , &e[i]); cnt[e[i]]++; } for (int i = (int)(1); i <= (int)(m); i++) if (cnt[i] >= blk) heavy.push_back(i); for (int i = (int)(1); i <= (int)(Q); i++) { scanf( %d%d%d , &q[i].l, &q[i].r, &q[i].k); q[i].id = i; } sort(q + 1, q + Q + 1, cmpk); for (int r = 1; r <= Q;) { int l = r, kk = q[l].k, res = 1, las = n, v = 1; for (; r <= Q && kk == q[r].k; r++) ; for (int i = (int)(1); i <= (int)(m); i++) res = 1ll * res * fac[kk + cnt[i]] % mo; for (int i = (int)(l); i <= (int)(r - 1); i++) { int len = q[i].r - q[i].l + 1; for (; las > len; v = 1ll * (1ll * m * kk + n - (--las)) % mo * v % mo) ; ans[q[i].id] = 1ll * res * v % mo; } } sort(q + 1, q + Q + 1, cmpmo); int l = 1, r = 0; memset(cnt, 0, sizeof(cnt)); ; cnt[0] = m; for (int i = (int)(1); i <= (int)(n); i++) add(e[i]); for (int i = (int)(1); i <= (int)(Q); i++) { for (; q[i].l < l; del(e[--l])) ; for (; q[i].l > l; add(e[l++])) ; for (; q[i].r < r; add(e[r--])) ; for (; q[i].r > r; del(e[++r])) ; int res = 1; for (int j = (int)(0); j <= (int)(blk - 1); j++) if (cnt[j]) res = 1ll * res * power(inv[q[i].k + j], cnt[j]) % mo; for (int j : heavy) if (freq[j] >= blk) res = 1ll * res * inv[q[i].k + freq[j]] % mo; ans[q[i].id] = 1ll * ans[q[i].id] * res % mo; } for (int i = (int)(1); i <= (int)(Q); i++) printf( %d n , ans[i]); }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 7; const int MOD = (int)1e9 + 7; const long long INF = (long long)1e18 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> p(n + 1, 0); for (int i = 2; i * i <= n; i++) if (p[i] == 0) { p[i] = i; for (int j = i * i; j <= n; j += i) if (!p[j]) p[j] = i; } for (int i = 1; i <= n; i++) if (!p[i]) p[i] = i; vector<int> ans; for (int i = 1; i <= n; i++) ans.push_back(i / p[i]); sort(ans.begin(), ans.end()); for (int i = 1; i < n; i++) cout << ans[i] << ; }
#include <bits/stdc++.h> using namespace std; int main() { int k, p; double s, n; cin >> k >> n >> s >> p; for (int i = 0; i < 10000000000000000; i++) { if (((i * p) / k) >= n / s) { cout << i; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, q, a[300003], near[300003][20], last[300003]; signed main() { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i >= 1; i--) { for (int k = 0; k < 20; k++) { near[i][k] = n + 1; if (((1 << k) & a[i]) && last[k]) near[i][k] = last[k]; for (int j = 0; j < 20; j++) { if (last[j] && ((a[i] >> j) & 1)) { near[i][k] = min(near[i][k], near[last[j]][k]); } } if (near[i][k] == n + 1) near[i][k] = i; } for (int k = 0; k < 20; k++) if ((a[i] >> k) & 1) last[k] = i; } while (q--) { int l, r; cin >> l >> r; for (int i = 0; i < 20; i++) { if (((a[r] >> i) & 1) && near[l][i] != l && near[l][i] <= r) { puts( Shi ); l = r; break; } } if (l < r) puts( Fou ); } }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define pi 2*acos(0.0) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*b)/gcd(a,b) map<ll,ll>mp,mk; //map<string,ll>mp; set<ll> s1,s2; vector<int> v,w; string s= ,qq= ,ss= ,pp= ; char ch; ll m,n,i,j,l,r,x,t,y,u,g,h,mn,mx,d,z,c,f,e,k,o; int main () { cin>>t; while(t--) { cin>>n>>k; for(i=0; i<k-3; i++) cout<< 1 ; n=n-k+3; c=n-2; o=n/2; if(n%2) cout<<n/2<< <<n/2<< <<1; else if(o%2==0) cout<<n/2<< <<o/2<< <<o/2; else if(o%2) cout<<c/2<< <<c/2<< <<2; cout<< n ; } }
#include <bits/stdc++.h> int dx[] = {0, 0, 1, -1, 2, 2, -2, -2, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1, 2, 2, -2, -2}; using namespace std; vector<long long int> V, V2, V3, edj[200010]; bitset<200010> color; long long int root, cnt = 0; void dfs(long long int n, long long int ck) { color[n] = true; for (long long int i : edj[n]) { if (color[i] == false) { color[i] = true; long long int tm = ck ^ 1; if (tm == 0) { cnt++; } dfs(i, tm); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int tcase = 1; for (long long int test = 1; test <= tcase; test++) { long long int n; cin >> n; for (long long int i = 0; i < n - 1; i++) { long long int u, v; cin >> u >> v; edj[u].push_back(v); edj[v].push_back(u); } for (long long int i = 1; i <= n; i++) { if ((long long int)edj[i].size() == 1) { root = i; break; } } dfs(root, 0); long long int ans = cnt; cnt = 0; for (long long int i = 0; i <= n; i++) color[i] = false; root = edj[root][0]; dfs(root, 0); ans = min(ans, cnt); cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589793238463; int main() { cin.tie(0); cin.sync_with_stdio(0); long long a, b; cin >> a >> b; while (a != 0 && b != 0) { if (a < 2 * b && b < 2 * a) break; if (a >= 2 * b) a %= 2 * b; else b %= 2 * a; } cout << a << << b << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, j, l, n, k, d, max = 0, gen, e = 0; long long p; scanf( %d %d , &n, &k); vector<vector<int> > v(n); for (i = 0; i < n; i++) { scanf( %d , &d); v[d].push_back(i); if (d > max) max = d; } p = 1; for (i = 0; i <= max; i++) { gen = v[i].size(); if (i > 0) e += gen; if (gen > p || gen <= 0) break; p = gen; if (i == 0) p = k; else p = p * (k - 1); } if (i <= max) printf( -1 n ); else { printf( %d n , e); for (i = 0; i < max; i++) { gen = 0; if (i == 0) e = k; else e = k - 1; for (j = 0; j < (int)(v[i].size()); j++) { for (l = gen; l < gen + e && l < (int)(v[i + 1].size()); l++) { printf( %d %d n , v[i][j] + 1, v[i + 1][l] + 1); } gen += e; if (gen >= v[i + 1].size()) break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int n, m, q; vector<int> E[maxn]; int op[maxn], a[maxn], b[maxn], c[maxn]; int ans[maxn]; bitset<1001> Bit[1001]; bitset<1001> C; void dfs(int x) { if (x == 0) { for (int i = 0; i < E[x].size(); i++) { ans[E[x][i]] = ans[x]; dfs(E[x][i]); } } if (op[x] == 1) { int flag = 0; if (!Bit[a[x]][b[x]]) { flag = 1; ans[x]++; Bit[a[x]][b[x]] = 1; } for (int i = 0; i < E[x].size(); i++) { ans[E[x][i]] = ans[x]; dfs(E[x][i]); } if (flag) Bit[a[x]][b[x]] = 0; } if (op[x] == 2) { int flag = 0; if (Bit[a[x]][b[x]]) { flag = 1; ans[x]--; Bit[a[x]][b[x]] = 0; } for (int i = 0; i < E[x].size(); i++) { ans[E[x][i]] = ans[x]; dfs(E[x][i]); } if (flag) Bit[a[x]][b[x]] = 1; } if (op[x] == 3) { ans[x] = ans[x] - Bit[a[x]].count(); Bit[a[x]] ^= C; ans[x] = ans[x] + Bit[a[x]].count(); for (int i = 0; i < E[x].size(); i++) { ans[E[x][i]] = ans[x]; dfs(E[x][i]); } Bit[a[x]] ^= C; } if (op[x] == 4) { for (int i = 0; i < E[x].size(); i++) { ans[E[x][i]] = ans[x]; dfs(E[x][i]); } } } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= m; i++) C[i] = 1; for (int i = 1; i <= q; i++) { scanf( %d , &op[i]); if (op[i] == 1) scanf( %d%d , &a[i], &b[i]); if (op[i] == 2) scanf( %d%d , &a[i], &b[i]); if (op[i] == 3) scanf( %d , &a[i]); if (op[i] == 4) scanf( %d , &a[i]), E[a[i]].push_back(i); else E[i - 1].push_back(i); } dfs(0); for (int i = 1; i <= q; i++) printf( %d n , ans[i]); }
#include <bits/stdc++.h> using namespace std; long long a, b, c, i, l, j, x, s, y, ans; int main() { cin >> a >> b >> c >> l; s = 0; for (i = l + 1; i >= 1; i--) s += i * (i + 1) / 2; ans = 0; for (i = 0; i <= l; i++) { x = a + i; if (b > x || c > x) continue; y = min(l - i, x - b - c) + 1; if (y < 0) y = 0; ans += (1 + y) * y / 2; } for (i = 0; i <= l; i++) { x = b + i; if (a > x || c > x) continue; y = min(l - i, x - a - c) + 1; if (y < 0) y = 0; ans += (1 + y) * y / 2; } for (i = 0; i <= l; i++) { x = c + i; if (a > x || b > x) continue; y = min(l - i, x - a - b) + 1; if (y < 0) y = 0; ans += (1 + y) * y / 2; } cout << s - ans; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; const int MAXN = 3005; int r, c, n, k; pair<int, int> v[MAXN]; int prv[MAXN], nxt[MAXN]; vector<int> y[MAXN], x[MAXN]; int pos[MAXN]; int num[MAXN]; int cnt; long long ans; int res; bool cmp_x(int num1, int num2) { return v[num1].first < v[num2].first; } bool cmp_y(int num1, int num2) { return v[num1].second < v[num2].second; } int trivia(int x1, int x2) { int cnt[MAXN]; memset(cnt, 0, sizeof(cnt)); for (int yy = 1; yy <= c; yy++) { for (int i = 0; i < y[yy].size(); i++) { if (x1 <= v[y[yy][i]].first && v[y[yy][i]].first <= x2) { cnt[yy]++; } } } int res = 0; for (int yy1 = 1; yy1 <= c; yy1++) { int cur = 0; for (int y2 = yy1; y2 <= c; y2++) { cur += cnt[y2]; if (cur >= k) { res++; } } } return res; } void solve() { scanf( %d%d%d%d , &r, &c, &n, &k); for (int i = 1; i <= n; i++) { scanf( %d%d , &v[i].first, &v[i].second); y[v[i].second].push_back(i); x[v[i].first].push_back(i); } for (int i = 1; i <= r; i++) { sort(x[i].begin(), x[i].end(), cmp_y); } for (int i = 1; i <= c; i++) { sort(y[i].begin(), y[i].end(), cmp_x); } v[n + 1].second = c + 1; for (int i = 1; i <= r; i++) { cnt = 0; res = 0; for (int j = 1; j <= c; j++) { for (int h = (int)y[j].size() - 1; h >= 0; h--) { int curnum = y[j][h]; if (v[curnum].first < i) { break; } pos[curnum] = ++cnt; num[cnt] = curnum; prv[cnt] = cnt - 1; nxt[cnt - 1] = cnt; } } nxt[cnt] = cnt + 1; num[cnt + 1] = n + 1; nxt[cnt + 1] = 0; for (int j = k; j <= cnt; j++) { res += (v[num[j + 1]].second - v[num[j]].second) * (v[num[j - k + 1]].second); } for (int j = r; j >= i; j--) { ans += res; for (int h = 0; h < x[j].size(); h++) { vector<int> all; all.reserve(k << 1); int back = 0; int p = prv[pos[x[j][h]]]; while (p && all.size() < k - 1) { all.push_back(p); p = prv[p]; } back = all.size(); reverse(all.begin(), all.end()); if (p) { res += (v[num[nxt[pos[x[j][h]]]]].second - v[num[pos[x[j][h]]]].second) * (v[num[p]].second); } p = pos[x[j][h]]; while (p && all.size() <= k + back) { all.push_back(p); p = nxt[p]; } for (int l = max(k - 1, back); l + 1 < all.size(); l++) { res -= (v[num[all[l + 1]]].second - v[num[all[l]]].second) * (v[num[all[l - k + 1]]].second); } for (int l = max(k, back + 1); l + 1 < all.size(); l++) { res += (v[num[all[l + 1]]].second - v[num[all[l]]].second) * (v[num[all[l - k]]].second); } prv[nxt[pos[x[j][h]]]] = prv[pos[x[j][h]]]; nxt[prv[pos[x[j][h]]]] = nxt[pos[x[j][h]]]; } } } printf( %I64d n , ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); srand(1313); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e6; long long cnt[MAXN]; signed main() { long long n, m, k; ios_base::sync_with_stdio(false); cin >> n >> m >> k; vector<long long> f(n); vector<pair<long long, long long> > s(m); for (long long i = 0; i < n; i++) cin >> f[i]; for (long long i = 0; i < m; i++) cin >> s[i].first; for (long long i = 0; i < m; i++) s[i].second = i; sort(s.begin(), s.end()); reverse(s.begin(), s.end()); fill(cnt, cnt + MAXN, 0); for (auto ff : f) { if (ff < MAXN) { cnt[ff]++; } } long long taken = 0; bool flag = true; for (long long i = 0; flag && i < (n + k - 1) / k; i++) { taken += cnt[i]; long long needed = min(n, (i + 1) * k); flag &= (taken <= needed); } if (!flag) { cout << -1 << endl; } else { long long left = 0, right = m + 1; while (left + 1 < right) { long long med = (left + right) / 2; for (long long i = 0; i < med; i++) { if (s[i].first < MAXN) { cnt[s[i].first]++; } } taken = 0; bool flag = true; for (long long i = 0; flag && i < (n + k + med - 1) / k; i++) { taken += cnt[i]; long long needed = min(n + med, (i + 1) * k); flag &= (taken <= needed); } for (long long i = 0; i < med; i++) { if (s[i].first < MAXN) { cnt[s[i].first]--; } } if (flag) { left = med; } else { right = med; } } cout << left << endl; for (long long i = 0; i < left; i++) { cout << s[i].second + 1 << ; } } }
#include <bits/stdc++.h> using namespace std; int main() { int s, v1, v2, t1, t2, i1, i2; cin >> s >> v1 >> v2 >> t1 >> t2; i1 = s * v1 + 2 * t1; i2 = s * v2 + 2 * t2; if (i1 < i2) cout << First ; else if (i1 > i2) cout << Second ; else cout << Friendship ; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 500 + 10; const int P = 998244353; int N, X, f[MAXN][MAXN], pw[MAXN][MAXN], binom[MAXN][MAXN]; void add(int& a, int b) { a = a + b >= P ? a + b - P : a + b; } int dif(int a, int b) { return a - b < 0 ? a - b + P : a - b; } int main() { for (int i = (0), iend = (MAXN); i < iend; ++i) { pw[i][0] = (i > 0); for (int j = (1), jend = (MAXN); j < jend; ++j) pw[i][j] = 1ll * pw[i][j - 1] * i % P; } binom[0][0] = 1; for (int i = (1), iend = (MAXN); i < iend; ++i) { binom[i][0] = 1; for (int j = (1), jend = (i + 1); j < jend; ++j) binom[i][j] = binom[i - 1][j - 1], add(binom[i][j], binom[i - 1][j]); } for (int i = (1), iend = (MAXN); i < iend; ++i) f[0][i] = 1; for (int i = (2), iend = (MAXN); i < iend; ++i) for (int j = (1), jend = (MAXN); j < jend; ++j) { for (int x = i - 1; x < j; x += i - 1) { for (int k = (1), kend = (i); k < kend; ++k) add(f[i][j], 1ll * binom[i][k] * f[i - k][j - x] % P * pw[i - 1][k] % P); } for (int x = 0; x < j; x += i - 1) add(f[i][j], pw[min(i - 1, j - x)][i]); } scanf( %d %d , &N, &X); printf( %d n , f[N][X]); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; vector<int> e[maxn], re[maxn], List; int n, m, id[maxn], tt, cnt[maxn], ans; bool vis[maxn]; void input() { scanf( %d%d , &n, &m); for (int u, v, i = 1; i <= m; i++) { scanf( %d%d , &u, &v); e[u].push_back(v); re[v].push_back(u); } } void dfs(int u) { vis[u] = 1; for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) { if (!vis[*it]) { dfs(*it); } } List.push_back(u); } void rdfs(int u, int k) { id[u] = k; cnt[k]++; for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) { if (!id[*it]) { rdfs(*it, k); } } } void scc() { tt = 0; for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); reverse(List.begin(), List.end()); for (vector<int>::iterator it = List.begin(); it != List.end(); it++) if (!id[*it]) { tt++; rdfs(*it, tt); } } void bfs(int u) { queue<int> q; while (!q.empty()) q.pop(); q.push(u); vis[u] = 1; int c = 0, x = 0; bool ok = 0; while (!q.empty()) { u = q.front(); q.pop(); if (cnt[id[u]]) { c += cnt[id[u]] - 1; x++; if (cnt[id[u]] > 1) ok = 1; cnt[id[u]] = 0; } for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) { if (!vis[*it]) { vis[*it] = 1; q.push(*it); } } for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) { if (!vis[*it]) { vis[*it] = 1; q.push(*it); } } } ans += c + x - 1; if (ok) ans++; } void solve() { memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) if (!vis[i]) { bfs(i); } printf( %d , ans); } int main() { input(); scc(); solve(); }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 1e18; const double EPS = 1E-9; int n, m; int main(void) { cin >> n >> m; if (m > n) { int temp = m; m = n; n = temp; } if (m == 1) { cout << n << endl; } else if (m == 2) { cout << 2 * (2 * (n / 4) + min(n % 4, 2)) << endl; } else if (m >= 3) { if ((n == 3 && m == 3) || (n == 3 && m == 5) || (n == 3 && m == 6) || (n == 4 && m == 4)) { if (n == 3 && m == 3) { cout << 5 << endl; } else if (n == 3 && m == 5) { cout << 7 << endl; } else if (n == 3 && m == 6) { cout << 7 << endl; } else { cout << 8 << endl; } } else { cout << m * n - (m * n) / 2 << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long N = 11111111; const long long INF = (long long)(1e12); const long long mod = 998244353; const double eps = 1e-9; long long n, mx = -1, inv[N], a[N], k, all, cnt, ans; map<long long, long long, greater<long long> > M, S; long long get(long long x) { return (x) * (x + 1) / 2; } int main() { ios_base ::sync_with_stdio(false); cin >> n >> k; all = (1ll << k) - 1; ans = get(n); M[0]++; for (long long i = 1; i <= n; ++i) { cin >> a[i]; a[i] ^= a[i - 1]; inv[i] = (all ^ a[i]); if (M[a[i]] < M[inv[i]]) { ans -= M[a[i]]; M[a[i]]++; } else { ans -= M[inv[i]]; M[inv[i]]++; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename TYPE> inline TYPE sqr(const TYPE& a) { return (a) * (a); } const int INF = 1000 * 1000 * 1000; const long double EPS = 1e-9; const long double PI = 2 * acos(0.0); const int N = 100100; int top; int j; int a[N]; string ans; bool f() { if (j >= top) return false; if (a[j] == 0) { ++j; ans.append( int ); return true; } ++j; ans.append( pair< ); if (!f()) return false; ans.append( , ); if (!f()) return false; ans.append( > ); return true; } int main() { int n; scanf( %d n , &n); string s; getline(cin, s); for (int i = 0; i < int(s.size()); ++i) if (s[i] == t ) a[top++] = 0; else if (s[i] == p ) a[top++] = 1; if (!f() || j < top) { puts( Error occurred ); } else { puts(ans.c_str()); } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932238460; bool arr[361]; void angles() { for (int i = 3; i < 361; i++) if ((i - 2) * 180 % i == 0) { arr[(i - 2) * 180 / i] = true; } } int main() { int t; cin >> t; angles(); while (t--) { int k; cin >> k; if (arr[k]) cout << YES << n ; else cout << NO << n ; } }
#include <bits/stdc++.h> using namespace std; bool a[1100][26]; vector<bool> v(1100, false); int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; for (auto c : s) a[i][c - 97] = true; } for (int i = 0; i < n; i++) { if (v[i]) continue; for (int j = i + 1; j < n; j++) { bool b = true; for (int g = 0; g < 26; g++) if (a[i][g] != a[j][g]) b = false; if (b) { v[j] = true; } } } int ans = 0; for (int i = 0; i < n; i++) if (!v[i]) ans++; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, k; int c[105]; long long p[105][105], f[105][105][105]; int main() { scanf( %d %d %d , &n, &m, &k); for (int i = 1; i <= n; i++) scanf( %d , &c[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf( %I64d , &p[i][j]); for (int i = 1; i <= n; i++) if (c[i]) p[i][c[i]] = 0; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) for (int q = 0; q <= k; q++) f[i][j][q] = 1e16; if (c[1]) f[1][c[1]][k - 1] = 0; else for (int i = 1; i <= m; i++) f[1][i][k - 1] = p[1][i]; for (int i = 1; i < n; i++) { for (int j = 1; j <= m; j++) if (c[i] == 0 || j == c[i]) for (int q = k - 1; q >= 0; q--) { if (q != 0) { for (int w = 1; w <= m; w++) if (j != w) f[i + 1][w][q - 1] = min(f[i + 1][w][q - 1], f[i][j][q] + p[i + 1][w]); } f[i + 1][j][q] = min(f[i + 1][j][q], f[i][j][q] + p[i + 1][j]); } } long long ans = 1e16; if (c[n]) { ans = min(ans, f[n][c[n]][0]); } else for (int i = 1; i <= m; i++) ans = min(f[n][i][0], ans); if (ans == 1e16) printf( -1 ); else printf( %I64d , ans); return 0; }
#include <bits/stdc++.h> template <class T> void read(T &x) { x = 0; int f = 0; char ch = getchar(); while (ch < 0 || ch > 9 ) f |= (ch == - ), ch = getchar(); while (ch <= 9 && ch >= 0 ) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = f ? -x : x; return; } const int maxn = 5007; char c[maxn], a[maxn]; int t[2][2]; int main() { int n; read(n); scanf( %s , c + 1); scanf( %s , a + 1); int len = n / 2; for (int i = 1; i <= n; i++) if (c[i] == 1 && a[i] == 1 ) t[1][1]++; else if (c[i] == 1 && a[i] == 0 ) t[1][0]++; else if (c[i] == 0 && a[i] == 1 ) t[0][1]++; else t[0][0]++; for (int i = 0; i <= len; i++) for (int j = 0; j <= i; j++) { if (j > t[1][1]) continue; int k = i - j; if (k > t[1][0]) continue; int re11 = t[1][1] - j; if (re11 > i) continue; int gal = i - re11; if (t[0][1] >= gal) { int x = j, y = k, z = t[0][1] - gal; int res = len - i - z; if (res < 0 || res > t[0][0]) continue; for (int q = 1; q <= n; q++) { if (c[q] == 1 && a[q] == 1 && x > 0) { printf( %d , q); x--; } if (c[q] == 1 && a[q] == 0 && y > 0) { printf( %d , q); y--; } if (c[q] == 0 && a[q] == 1 && z > 0) { printf( %d , q); z--; } if (c[q] == 0 && a[q] == 0 && res > 0) { printf( %d , q); res--; } } return 0; } } printf( -1 n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 40; void solve() { long long int n, m, k; cin >> n >> m >> k; long long int beg = 1, end = n * m; long long int fa; while (beg <= end) { long long int mid = (beg + end) / 2; long long int cnt = 0; for (int i = 1; i <= n; i++) { cnt += min(m, mid / i); } if (cnt >= k) { end = mid - 1; fa = mid; } else beg = mid + 1; } cout << fa << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; while (T--) { solve(); if (T) { char c; } } }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 1e7 + 100; unordered_map<int, int> vis, Cnt; vector<int> Mice; vector<int> Cheese; int main() { int n, m, y0, y1; scanf( %d%d%d%d , &n, &m, &y0, &y1); for (int i = 0; i < n; i++) { int x; scanf( %d , &x); Mice.push_back(x); } for (int i = 0; i < m; i++) { int x; scanf( %d , &x); Cheese.push_back(x); } int Ans = 0; for (int x : Mice) { int L = INF, R = INF; auto it = lower_bound(Cheese.begin(), Cheese.end(), x); if (it != Cheese.end()) R = *it; if (it != Cheese.begin()) it--; L = *it; int cur = min(abs(x - L), abs(R - x)); if (x - L == R - x) { if (!vis.count(L)) vis[L] = x - L, Cnt[L] = 1; else { if (vis[L] == x - L) Cnt[L]++; else { if (!vis.count(R)) vis[R] = cur, Cnt[R] = 1; else if (vis[R] > cur) { Ans += vis[R]; vis[R] = cur; Cnt[R] = 1; } else if (vis[R] == cur) Cnt[R]++; else Ans++; } } } else { int d = (x - L < R - x ? L : R); if (!vis.count(d)) vis[d] = cur, Cnt[d] = 1; else { if (vis[d] > cur) { Ans += Cnt[d]; vis[d] = cur; Cnt[d] = 1; } else if (vis[d] < cur) Ans++; else Cnt[d]++; } } } printf( %d n , Ans); return 0; }
#include<bits/stdc++.h> #define x first #define y second using namespace std; typedef long long LL; typedef long double LD; typedef pair<int,int> PII; const int N=100005; int n,m,T; int main(){ cin>>T; int k1,k2,t1,t2,x1,x2; while(T--){ cin>>n>>k1>>k2; cin>>t1>>t2; x1=(k1+k2)/2; if((k1+k2)&1) n--; x2=n-x1; if( t1<=x1 && t2<=x2 ) cout<< YES <<endl; else cout<< NO <<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, size[200010] = {0}; vector<int> w[200010]; int dpMax(int, int); int dpMin(int, int); int child(int x) { int ret = 0; for (int i = 0; i < w[x].size(); i++) ret += child(w[x][i]); return size[x] = ret + !w[x].size(); } int dpMax(int x, int t) { int ret1, ret2; ret1 = ret2 = !w[x].size(); for (int i = 0; i < w[x].size(); i++) { if (!t) ret1 = max(ret1, size[x] - size[w[x][i]] + dpMin(w[x][i], 0)); else ret2 += (dpMin(w[x][i], 1)); } return !t ? ret1 : ret2; } int dpMin(int x, int t) { int ret1, ret2; ret1 = 1; ret2 = 1 + (1 << 28) * (w[x].size() > 0); for (int i = 0; i < w[x].size(); i++) { if (!t) ret1 += (dpMax(w[x][i], 0) - 1); else ret2 = min(ret2, (dpMax(w[x][i], 1))); } return !t ? ret1 : ret2; } int main() { scanf( %d , &n); for (int i = 0, a, b; i < n - 1; i++) { scanf( %d%d , &a, &b); w[a].push_back(b); } child(1); printf( %d %d n , dpMax(1, 0), dpMax(1, 1)); return 0; }
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; int n, m; namespace flow { int n, s, t; int head[200306], to[200306 << 1], nex[200306 << 1], wto[200306 << 1], ecn = 1; int ade(int u, int v, int w) { to[++ecn] = v, nex[ecn] = head[u], wto[ecn] = w, head[u] = ecn; to[++ecn] = u, nex[ecn] = head[v], wto[ecn] = 0, head[v] = ecn; return ecn; } queue<int> Q; int dep[200306], cur[200306]; const int inf = 1e9; void clear() { for (int i = (1), iend = (n); i <= iend; ++i) head[i] = dep[i] = cur[i] = 0; ecn = 1; } bool bfs() { Q.push(s); for (int i = (1), iend = (n); i <= iend; ++i) cur[i] = head[i]; memset(dep, 0x3f, sizeof dep); dep[s] = 0; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = head[u]; i; i = nex[i]) if (dep[to[i]] > inf && wto[i]) dep[to[i]] = dep[u] + 1, Q.push(to[i]); } return dep[t] < inf; } long long dfs(int u, long long lim) { if (!lim || u == t) return lim; long long flow = 0, f; for (int& i = cur[u]; i; i = nex[i]) if (dep[to[i]] == dep[u] + 1 && (f = dfs(to[i], min(lim, 1ll * wto[i])))) { lim -= f, flow += f, wto[i] -= f, wto[i ^ 1] += f; if (!lim) break; } return flow; } long long dinic() { long long res = 0; while (bfs()) res += dfs(s, 0x3f3f3f3f3f3f3f3f); return res; } } // namespace flow pair<int, int> ed[200306]; int bc[200306]; long long chk(int x) { for (int i = (1), iend = (n); i <= iend; ++i) flow::ade(n + m + 1, i, x); for (int i = (1), iend = (m); i <= iend; ++i) { int u = ed[i].first, v = ed[i].second; bc[i] = flow::ade(u, i + n, 1); flow::ade(v, i + n, 1); flow::ade(i + n, n + m + 2, 1); } long long as = flow::dinic(); flow::clear(); return as; } void solve() { cin >> n >> m; for (int i = (1), iend = (m); i <= iend; ++i) { int u, v; scanf( %d%d , &u, &v); ed[i] = make_pair(u, v); } int l = 0, r = n; flow::s = n + m + 1, flow::t = n + m + 2, flow::n = n + m + 2; while (l <= r) { int mid = l + r >> 1; long long as = chk(mid); if (as == m) { r = mid - 1; } else l = mid + 1; } cout << l << endl; chk(l); for (int i = (1), iend = (m); i <= iend; ++i) { int u = ed[i].first, v = ed[i].second; if (flow::wto[bc[i]]) printf( %d %d n , u, v); else printf( %d %d n , v, u); } } signed main() { solve(); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; vector<pair<int, int> > adj[MAXN]; int n; bool mark[MAXN]; int p[MAXN]; int curv; void dfs(int v, int ind = -1) { mark[v] = 1; for (int i = 0; i < adj[v].size(); i++) { pair<int, int> u = adj[v][i]; if (!mark[u.first]) { dfs(u.first, u.second); } else if (u.second != ind) { curv = u.first; } } } int main() { cin >> n; int x; int d = -1; for (int i = 1; i <= n; i++) { cin >> x; if (x == i) { d = i; } p[i] = x; adj[i].push_back({x, i}); adj[x].push_back({i, i}); } int cnt = 0; vector<int> ve; for (int i = 1; i <= n; i++) { if (!mark[i]) { dfs(i); cnt++; ve.push_back(curv); } } if (d != -1) { cout << cnt - 1 << n ; for (int i = 0; i < ve.size(); i++) { p[ve[i]] = d; } for (int i = 1; i <= n; i++) { cout << p[i] << ; } return 0; } cout << cnt << n ; for (int i = 0; i < ve.size(); i++) { p[ve[i]] = ve[0]; } for (int i = 1; i <= n; i++) { cout << p[i] << ; } }