func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < 0) a = -a; if (b < 0) b = -b; return b == 0 ? a : gcd(b, a % b); } template <typename T> void read(T &res) { bool flag = false; char ch; while (!isdigit(ch = getchar())) (ch == - ) && (flag = true); for (res = ch - 48; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + ch - 48) ; flag && (res = -res); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long qp(long long a, long long b, long long mod) { long long ans = 1; if (b == 0) { return ans % mod; } while (b) { if (b % 2 == 1) { b--; ans = ans * a % mod; } a = a * a % mod; b = b / 2; } return ans % mod; } long long qpn(long long a, long long b, long long p) { long long ans = 1; a %= p; while (b) { if (b & 1) { ans = (ans * a) % p; --b; } a = (a * a) % p; b >>= 1; } return ans % p; } set<long long> s; signed main() { long long t; read(t); while (t--) { long long n; read(n); long long po = 0; s.insert(po); for (long long i = 1; i * i <= n; i++) { s.insert(n / i); s.insert(n / (n / i)); } printf( %d n , s.size()); for (auto x : s) { printf( %lld , x); } s.clear(); printf( n ); } } |
#include <bits/stdc++.h> #pragma GCC optimize( O2 ) 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...); } const long long N = 2e5 + 10; const long long mod = 1e9 + 7; struct Trie { Trie *child[2]; long long cnt; }; Trie *createNode() { Trie *node = (Trie *)malloc(sizeof(Trie)); memset(node->child, NULL, sizeof node->child); node->cnt = 0; return node; } void insert(Trie *root, long long x) { for (long long i = 30; i >= 0; --i) { long long val = (1LL << i); long long bit = 0; if (val & x) bit = 1; if (!root->child[bit]) root->child[bit] = createNode(); root = root->child[bit]; root->cnt++; } } void erase(Trie *root, long long x) { for (long long i = 30; i >= 0; --i) { long long val = (1LL << i); long long bit = 0; if (val & x) bit = 1; if (!root->child[bit]) root->child[bit] = createNode(); root = root->child[bit]; root->cnt--; } } long long query(Trie *root, long long x) { long long ans = 0; for (long long i = 30; i >= 0; --i) { long long val = (1 << i); long long bit = 0; if (val & x) bit = 1; if (root->child[bit] && root->child[bit]->cnt > 0) { root = root->child[bit]; } else { assert(root->child[bit ^ 1] && root->child[bit ^ 1]->cnt > 0); ans += val; root = root->child[bit ^ 1]; } } return ans; } void solve() { long long n; cin >> n; long long a[n], b[n]; for (long long i = 0; i < n; ++i) cin >> a[i]; for (long long i = 0; i < n; ++i) cin >> b[i]; Trie *root = createNode(); for (long long i = 0; i < n; ++i) { insert(root, b[i]); } vector<long long> ans; for (long long i = 0; i < n; ++i) { long long x = query(root, a[i]); ans.push_back(x); erase(root, a[i] ^ x); } for (long long i = 0; i < n; ++i) cout << ans[i] << ; cout << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); ; long long t = 1; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline int read(T &a) { a = 0; char c = getchar(), f = 1; while (c > 9 || c < 0 ) { if (c == - ) f = (~f) + 1; if (c == -1) return -1; c = getchar(); } while (c <= 9 && c >= 0 ) a = (a << 1) + (a << 3) + (c ^ 48), c = getchar(); a *= f; return 1; } template <typename T> inline int write(T a) { if (a < 0) putchar( - ), a = (~a) + 1; if (a / 10) return write(a / 10) & putchar(a % 10 | 48); return putchar(a | 48); } template <typename T> inline int write(T a, char c) { return write(a) + putchar(c); } int n, m; int w[200001]; int x[200001], y[200001]; int need[200001]; bool used[200001]; vector<int> v[200001]; bool ok[200001]; queue<int> q; int ans[200001], cnt; int main() { read(n), read(m); for (int i = 1; i <= n; i++) read(w[i]); for (int i = 1; i <= m; i++) { read(x[i]), read(y[i]), need[x[i]]++, need[y[i]]++; v[x[i]].push_back(i); v[y[i]].push_back(i); } for (int i = 1; i <= n; i++) if (need[i] <= w[i]) { used[i] = 1; for (auto j : v[i]) if (!ok[j]) { ok[j] = 1; q.push(j); } } while (!q.empty()) { int t = q.front(); q.pop(); ans[++cnt] = t; need[x[t]]--, need[y[t]]--; int i = y[t]; if (need[i] <= w[i] && !used[i]) { used[i] = 1; for (auto j : v[i]) if (!ok[j]) { ok[j] = 1; q.push(j); } } i = x[t]; if (need[i] <= w[i] && !used[i]) { used[i] = 1; for (auto j : v[i]) if (!ok[j]) { ok[j] = 1; q.push(j); } } } if (cnt != m) return puts( DEAD ) & 0; puts( ALIVE ); for (int i = cnt; i >= 1; i--) write(ans[i], ); puts( ); } |
#include <bits/stdc++.h> int b[12][12]; int main() { int a; int i, j; int sum; scanf( %d , &a); memset(b, 0, sizeof(b)); for (i = 1; i <= 10; i++) for (j = 1; j <= 10; j++) { b[1][j] = b[i][1] = 1; b[i][j] = b[i - 1][j] + b[i][j - 1]; } printf( %d , b[a][a]); } |
#include <bits/stdc++.h> using namespace std; int n, Cnt = 0, Num = 0; int l[200000 + 10], r[200000 + 10]; map<int, int> mp; int Di[200000 + 10]; vector<int> f[200000 + 10], No[200000 + 10]; int cur[200000 + 10]; bool b[200000 + 10]; int Ans[200000 + 10]; int Check() { int Now = -1, ODD = 0; for (int i = 1; i <= Cnt; i++) if (f[i].size() % 2 == 1) { ODD++; Now = i; } if (ODD == 2) return Now; if (ODD == 0) return 1; return -1; } void Euler(int x) { for (int i = cur[x]; i < f[x].size(); i = cur[x]) { cur[x] = i + 1; if (b[No[x][i]]) continue; b[No[x][i]] = true; Euler(f[x][i]); } Ans[++Ans[0]] = x; } int main() { scanf( %d , &n); n--; for (int i = 1; i <= n; i++) scanf( %d , &l[i]); for (int i = 1; i <= n; i++) scanf( %d , &r[i]); int x, y; for (int i = 1; i <= n; i++) { if (l[i] > r[i]) return printf( -1 ), 0; if (!mp.count(l[i])) x = mp[l[i]] = ++Cnt, Di[Cnt] = l[i]; else x = mp[l[i]]; if (!mp.count(r[i])) y = mp[r[i]] = ++Cnt, Di[Cnt] = r[i]; else y = mp[r[i]]; Num++; f[x].push_back(y); No[x].push_back(Num); f[y].push_back(x); No[y].push_back(Num); } int Begin = Check(); if (Begin == -1) return printf( -1 ), 0; Euler(Begin); if (Ans[0] != n + 1) return printf( -1 ), 0; for (int i = n + 1; i; i--) printf( %d , Di[Ans[i]]); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k, a[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); cin >> n >> k; if (k < n / 2 || (n == 1 && k)) { cout << -1; return 0; } if (n == 1 && k == 0) { cout << 1; return 0; } k = k - (n - 2) / 2; cout << k << << k * 2 << ; k = k * 2 + 1; for (int i = 0; i <= n - 3; i++) cout << k + i << ; return 0; } |
#include <bits/stdc++.h> using namespace std; void qmax(long long &x, int y) { if (x < y) x = y; } void qmin(long long &x, long long y) { if (x > y) x = y; } inline long long read() { char s; long long k = 0, base = 1; while ((s = getchar()) != - && s != EOF && !(isdigit(s))) ; if (s == EOF) exit(0); if (s == - ) base = -1, s = getchar(); while (isdigit(s)) { k = k * 10 + (s ^ 0 ); s = getchar(); } return k * base; } inline void write(int x) { static char cnt, num[15]; cnt = 0; if (!x) { printf( 0 ); return; } for (; x; x /= 10) num[++cnt] = x % 10; for (; cnt; putchar(num[cnt--] + 48)) ; } const int maxn = 1e5 + 100; int n, m, k, s, X, Y; int a[maxn]; int po[maxn], ne[maxn * 2], to[maxn * 2], id; void add(int x, int y) { id++; to[id] = y; ne[id] = po[x]; po[x] = id; } int dis[maxn], vis[maxn]; queue<int> q; int c[maxn][101]; void spfa(int x) { memset(vis, 0, sizeof(vis)); memset(dis, 0x3f3f3f3f, sizeof(dis)); int inf = dis[0]; while (!q.empty()) q.pop(); for (int i = 1; i <= n; i++) if (a[i] == x) { dis[i] = 0; vis[i] = 1; q.push(i); } while (!q.empty()) { int u = q.front(), v; q.pop(); vis[u] = 0; for (int i = po[u]; i; i = ne[i]) { v = to[i]; if (dis[v] > dis[u] + 1) { dis[v] = dis[u] + 1; if (!vis[v]) { vis[v] = 1; q.push(v); } } } } for (int i = 1; i <= n; i++) c[i][x] = dis[i]; } int main() { n = read(); m = read(); k = read(); s = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) { X = read(); Y = read(); add(X, Y); add(Y, X); } for (int i = 1; i <= k; i++) spfa(i); for (int i = 1; i <= n; i++) { long long sum = 0; sort(c[i] + 1, c[i] + k + 1); for (int j = 1; j <= s; j++) sum += c[i][j]; printf( %lld , sum); } } |
#include <bits/stdc++.h> using namespace std; string s; int pos[1000]; int n, a, b; int main() { cin >> n; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == x ) a++; else b++; } cout << abs(n / 2 - a) << endl; int t = abs(n / 2 - a); if (a > b) { for (int i = 0; i < s.size(); i++) if (s[i] == x && t) { s[i] = X ; t--; } } else if (a < b) { for (int i = 0; i < s.size(); i++) if (s[i] == X && t) { s[i] = x ; t--; } } cout << s << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { int ans = 0; cin >> n; cout << n / 2 << endl; } } |
#include <bits/stdc++.h> using namespace std; void checkDefine(); long long int a, b, c, d; int main() { cin >> a >> b >> c >> d; long long int t1 = max(3 * a / 10, a - (a * c) / 250); long long int t2 = max(3 * b / 10, b - (b * d) / 250); if (t1 > t2) { cout << Misha ; } else if (t1 < t2) { cout << Vasya ; } else { cout << Tie ; } return 0; } void checkDefine() { long long int n, a[200005]; map<long long int, long long int> m; cin >> n; for (int i = (0), _b = (n - 1); i <= _b; i++) { cin >> a[i]; m[a[i]]++; } string s; cin >> s; { cout << s << = ; cout << (s) << endl; }; { cout << a << = ; for (int _ = 0, _a = (n); _ < _a; _++) cout << a[_] << ; cout << endl; }; { cout << ------------ << = ; cout << ( ------------ ) << endl; }; for (__typeof(m.begin()) it = m.begin(); it != m.end(); ++it) { cout << it->first << << it->second << endl; } } |
#include <bits/stdc++.h> using namespace std; long long n, k, cnt[200005], x, y, ans; vector<long long> v[200005]; void dfs(long long pos, long long pre) { long long len = v[pos].size(); for (long long i = 0; i < len; i++) { long long xx = v[pos][i]; if (xx != pre) { dfs(xx, pos); cnt[pos] += cnt[xx]; ans += min(cnt[xx], 2 * k - cnt[xx]); } } } int main() { scanf( %lld%lld , &n, &k); for (long long i = 0; i < 2 * k; i++) { scanf( %lld , &x); cnt[x] = 1; } for (long long i = 0; i < n - 1; i++) { scanf( %lld%lld , &x, &y); v[x].push_back(y); v[y].push_back(x); } dfs(1, 0); printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; bool cmp(pair<long long, int> l, pair<long long, int> r) { return l.first < r.first; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, A, cf, cm, money; cin >> n >> A >> cf >> cm >> money; vector<pair<long long, int> > v(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i].first; v[i].second = i; } sort(v.begin(), v.end()); vector<long long> pf(n + 1, 0); long long c = 0; for (int i = 1; i <= n; i++) pf[i] = v[i].first + pf[i - 1]; vector<long long> ps(n + 2, 0); for (int i = n; i >= 1; i--) ps[i] = ps[i + 1] + v[i].first; long long ans = 0; long long no = 0; long long mn = 0; for (long long i = 0; i <= n; i++) { long long m = money; m = m - max((long long)0, (A * i - ps[n - i + 1])); if (m < 0) continue; long long pnts = cf * i; int lo = 0; int hi = A; while (lo < hi) { long long mid = (lo + hi + 1) >> (long long)1; vector<pair<long long, int> >::iterator it = lower_bound( v.begin(), v.begin() + (n - i + 1), make_pair(mid, 0), cmp); if (it == v.begin()) { lo = mid; continue; } else it--; long long reqd = ((long long)(it - v.begin())) * mid - pf[it - v.begin()]; if (reqd <= m) lo = mid; else hi = mid - 1; } pnts += lo * cm; if (ans < pnts) { ans = pnts; no = i; mn = lo; } ans = max(ans, pnts); } cout << ans << n ; ; vector<long long> re(n + 1, 0); for (int i = n; i > n - no; i--) re[(int)v[i].second] = A; for (int i = 1; i <= n - no; i++) re[v[i].second] = max(v[i].first, mn); for (int i = 1; i <= n; i++) cout << re[i] << ; return 0; } |
#include <bits/stdc++.h> int nextInt() { int x; scanf( %d , &x); return x; } double nextDouble() { double x; scanf( %lf , &x); return x; } long long nextLong() { long long x; scanf( %I64d , &x); return x; } char nextChar() { char x; scanf( %c , &x); return x; } void newLine() { printf( n ); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long powM(long long a, long long b, long long MOD) { long long x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); if (x > MOD) x %= MOD; } y = (y * y); if (y > MOD) y %= MOD; b /= 2; } return x; } long long ModExp(long long n, long long k, long long MOD) { long long ans = 1; while (k > 0) { if (k & 1) ans = (ans * n) % MOD; n = (n * n) % MOD; k /= 2; } return ans; } long long Exp(long long n, long long k) { long long ans = 1; while (k > 0) { if (k & 1) ans = (ans * n); n = (n * n); k /= 2; } return ans; } int countSetBits(long long n) { int ans = 0; while (n != 0) { n -= (n & -n); ++ans; } return ans; } const int N = 100100; const int M = 12; const int INF = 1e6; const double EPS = 1e-6; const long double PI = 2 * acos(0); using namespace std; const int LOG = 18; long long mod = 1000000009; int par[N]; int getPar(int x) { return x == par[x] ? x : par[x] = getPar(par[x]); }; bool same(int x, int y) { return getPar(x) == getPar(y); } void merge(int x, int y) { int xx = getPar(x); int yy = getPar(y); if (xx != yy) par[xx] = yy; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) par[i] = i; long long ans = 1; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; if (same(x, y)) { ans = (ans * 2ll) % mod; } else { merge(x, y); } cout << (ans + mod - 1) % mod << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { set<int> co[2]; set<pair<int, int> > delta[2]; int w, h, n; scanf( %d%d%d , &w, &h, &n); co[0].insert(0); co[0].insert(w); delta[0].insert(make_pair(w, 0)); co[1].insert(0); co[1].insert(h); delta[1].insert(make_pair(h, 0)); for (int i = (0), _end_ = (n); i < _end_; ++i) { static char s[2]; static int first; scanf( %s%d , s, &first); int ty = s[0] == H ; co[ty].insert(first); auto it = co[ty].lower_bound(first); auto tmp = it; --tmp; ++it; delta[ty].erase(make_pair(*it - *tmp, *tmp)); delta[ty].insert(make_pair(first - *tmp, *tmp)); delta[ty].insert(make_pair(*it - first, first)); printf( %I64d n , (long long)delta[0].rbegin()->first * delta[1].rbegin()->first); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int l, r, x, y; }; vector<node> p[500010]; int a[500010], b[500010], c[500010]; int main() { int i, j, k, n, m, s; node e; scanf( %d%d , &n, &m); for (i = 1; i <= n; i++) scanf( %d , &a[i]); b[0] = 0; for (i = 1; i <= n + 1; i++) { b[i] = b[i - 1]; if (a[i] == m) b[i]++; } a[n + 1] = 0; j = 0; k = 0; for (i = 1; i <= n + 1; i++) { if (a[i] == a[i - 1]) k++; else { e.l = j; e.r = k; e.x = k - j + 1; p[a[i - 1]].push_back(e); j = i; k = i; } } s = 0; for (i = 1; i <= 500010 - 10; i++) { k = p[i].size(); if (k == 0) continue; e.l = n + 1; e.r = n + 1; e.x = 0; p[i].push_back(e); p[i][0].y = p[i][0].x + b[n] - b[p[i][0].r] + b[p[i][0].l - 1]; s = max(s, p[i][0].y); c[p[i][0].l] = b[p[i][0].l - 1] + p[i][0].x; for (j = 1; j < k; j++) { p[i][j].y = p[i][j].x + max(c[p[i][j - 1].l], b[p[i][j].l - 1]) + b[n] - b[p[i][j].r]; s = max(s, p[i][j].y); c[p[i][j].l] = max(c[p[i][j - 1].l] + p[i][j].x, p[i][j].x + b[p[i][j].l - 1]); } } printf( %d n , s); return 0; } |
#include <bits/stdc++.h> using namespace std; void func() { string s; cin >> s; long long n = s.length(); long long i = 1, len = 0, pre[n]; pre[0] = 0; while (i < n) { if (s[i] == s[len]) { len++; pre[i++] = len; } else { if (len == 0) { pre[i++] = 0; } else { len = pre[len - 1]; } } } if (pre[n - 1] == 0) { cout << Just a legend << n ; return; } for (long long i = 0; i < n - 1; ++i) { if (pre[i] == pre[n - 1]) { cout << s.substr(0, pre[n - 1]) << n ; return; } } if (pre[pre[n - 1] - 1] == 0) { cout << Just a legend << n ; return; } cout << s.substr(0, pre[pre[n - 1] - 1]) << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { func(); } } |
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct item { long long t, d, p, idx; }; item sh[200005]; bool cmp(item x, item y) { return x.d < y.d; } long long n; long long dp[105][2005]; long long sol(long long i, long long cur) { if (i == n) return 0; if (~dp[i][cur]) return dp[i][cur]; if (sh[i].t + cur >= sh[i].d) return dp[i][cur] = sol(i + 1, cur); return dp[i][cur] = max(sh[i].p + sol(i + 1, cur + sh[i].t), sol(i + 1, cur)); } vector<long long> ans; void print(long long i, long long cur) { if (i == n) return; if (sh[i].t + cur >= sh[i].d) { print(i + 1, cur); } else if (sh[i].p + sol(i + 1, cur + sh[i].t) > sol(i + 1, cur)) { ans.push_back(sh[i].idx); print(i + 1, cur + sh[i].t); } else print(i + 1, cur); } void solve() { long long i, j, k, l, r, m, a, b, c, d, x, y, z, t; cin >> n; memset(dp, -1, sizeof dp); for (i = 0; i < n; i++) { cin >> sh[i].t >> sh[i].d >> sh[i].p; sh[i].idx = i + 1; } sort(sh, sh + n, cmp); cout << sol(0, 0) << endl; print(0, 0); cout << ans.size() << endl; for (auto &cv : ans) { cout << cv << ; } cout << endl; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long t = 1; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> int n, m, i, j, k, l, a, b, c; char s[200][200]; bool bo; void fail() { bo = false; } bool work(int x, int y, int o, int p, int a, int b, int c) { int i, j; if (x > o) { return true; } if ((a < 0) || (b < 0) || (c < 0)) return false; if (x == o) { j = 0; for (i = 1; i <= m; i += 2) { s[1][i] = s[1][i + 1] = a + j; j = (j ^ 1); } return true; } else if (y == p) { j = 0; for (i = 1; i <= n; i += 2) { s[i][1] = s[i + 1][1] = a + j; j = (j ^ 1); } return true; } else { bool bo[29]; for (i = 0; i < 26; i++) { bo[i] = false; } if (s[x - 1][y] != 0) { bo[s[x - 1][y] - a ] = true; } if (s[x - 1][y + 1] != 0) { bo[s[x - 1][y + 1] - a ] = true; } if (s[x][y - 1] != 0) { bo[s[x][y - 1] - a ] = true; } if (s[x + 1][y - 1] != 0) { bo[s[x + 1][y - 1] - a ] = true; } if (c >= 1) { char ch; for (i = 0; i < 26; i++) if (!bo[i]) { break; } ch = a + i; s[x][y] = s[x][y + 1] = s[x + 1][y] = s[x + 1][y + 1] = ch; c--; } else if (a >= 2) { char ch[2]; int k; k = 0; for (i = 0; i < 26; i++) if (!bo[i]) { ch[k] = a + i; k++; if (k == 2) { break; } } s[x][y] = s[x][y + 1] = ch[0]; s[x + 1][y] = s[x + 1][y + 1] = ch[1]; a -= 2; } else if (b >= 2) { char ch[2]; int k; k = 0; for (i = 0; i < 26; i++) if (!bo[i]) { ch[k] = a + i; k++; if (k == 2) { break; } } s[x][y] = s[x + 1][y] = ch[0]; s[x][y + 1] = s[x + 1][y + 1] = ch[1]; b -= 2; } else { return false; } y += 2; if (y > m) { x += 2; y = 1; if ((m & 1) == 1) { y = 2; } } } return work(x, y, o, p, a, b, c); } void rect(int x, int y, int o, int p, int a, int b, int c) { if (!work(x, y, o, p, a, b, c)) { fail(); } } int main() { bo = true; scanf( %d%d%d%d%d , &n, &m, &a, &b, &c); for (i = 0; i <= n; i++) for (j = 0; j <= m; j++) { s[i][j] = 0; } if ((((n * m) & 1) == 1) || (a * 2 + b * 2 + c * 4 < n * m)) { fail(); } else if ((n & 1) == 1) { rect(1, 1, 1, m, m / 2, 0, 0); if (n > 1) { rect(2, 1, n, m, a - m / 2, b, c); } else { if (a < m / 2) { fail(); } } } else if ((m & 1) == 1) { rect(1, 1, n, 1, 0, n / 2, 0); if (m > 1) { rect(1, 2, n, m, a, b - n / 2, c); } else { if (b < n / 2) { fail(); } } } else { rect(1, 1, n, m, a, b, c); } if (bo) { for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { printf( %c , s[i][j]); } printf( n ); } } else { printf( IMPOSSIBLE n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 4e18; const long long MAX = 1e5; const long long MV = 1e18; const long double PI = 3.14159265358979323846; long long MA = -INF, MI = INF; long long PO(long long x, long long y) { if (y == 0) return 1; long long ans = PO(x, y / 2); ans *= ans, ans %= MOD; if (y % 2 == 1) ans *= x, ans %= MOD; return ans % MOD; } void RE(long long x) { x %= MOD; if (x < 0) x += MOD; } long long fib[100], dp1[100], dp2[100], siz = 2; void cal() { fib[1] = 1, fib[2] = 2; while (true) { long long temp = fib[siz - 1] + fib[siz]; if (temp > MV) break; else fib[++siz] = temp; } } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(10); cal(); long long t; cin >> t; deque<long long> rep; while (t--) { long long n; cin >> n; for (long long i = siz; i >= 1; i--) { if (n >= fib[i]) rep.push_back(i), n -= fib[i]; } reverse(rep.begin(), rep.end()); rep.push_front(0); long long len = rep.size(); dp1[0] = 1; dp2[0] = 0; for (long long i = 1; i < len; i++) { dp1[i] = dp1[i - 1] + dp2[i - 1]; dp2[i] = (dp1[i - 1] * ((rep[i] - rep[i - 1] - 1) / 2)) + (dp2[i - 1] * ((rep[i] - rep[i - 1]) / 2)); } cout << dp1[len - 1] + dp2[len - 1] << n ; rep.clear(); } return 0; } |
#include <bits/stdc++.h> using namespace std; static long long mod = 998244353; long long pow(long long x, long long y) { x = x % mod; int res = 1; while (y > 0) { if (y & 1) { res = (res * x) % mod; } y = y >> 1; x = (x * x) % mod; } return res; } long long modInverse(long long n, long long mod) { return pow(n, mod - 2); } long long nCrmodFermat(long long n, long long r) { if (r == 0) return 1; long long fact[n + 1]; fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = ((fact[i - 1] % mod) * (i % mod)) % mod; } return (fact[n] * modInverse(fact[r], mod) % mod * modInverse(fact[n - r], mod) % mod) % mod; } int main() { long long n; cin >> n; vector<long long> v(2 * n); long long sum = 0; for (int i = 0; i < 2 * n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int i = 0; int j = (2 * n) - 1; while (i < n && j >= n) { sum = (sum + abs(v[i] - v[j])) % mod; i++; j--; } cout << ((sum % mod) * (nCrmodFermat((2 * n), n) % mod)) % mod << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[100010]; vector<pair<long long, long long> > people; long long Free[100010]; int main() { long long ts, tf, t; cin >> ts >> tf >> t; int n; scanf( %d , &n); if (n == 0) { cout << ts << endl; return 0; } for (int i = 1; i <= n; i++) { scanf( %I64d , &a[i]); } people.push_back(make_pair(0, 0)); long long cnt = 1; for (int i = 1; i < n; i++) { if (a[i] != a[i + 1]) { people.push_back(make_pair(a[i], cnt)); cnt = 1; } else cnt++; } people.push_back(make_pair(a[n], cnt)); long long ans, wait; ans = a[1] - 1; wait = ts - (a[1] - 1); if (wait < 0) { wait = 0; ans = ts; } long long time = max(people[1].first, ts); for (int i = 1; i + 1 < people.size(); i++) { if (time + t > tf) break; time = time + people[i].second * t; if (time + t > tf) break; long long come_time = people[i + 1].first - 1; long long new_wait = time - come_time; if (new_wait < 0) { new_wait = 0; come_time = time; } if (new_wait < wait) { wait = new_wait; ans = come_time; } time = max(time, people[i + 1].first); } time = time + people.back().second * t; if (time + t <= tf) { ans = time; wait = 0; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long int co = 0, i, n, y, m, j, k, t, x = 0; cin >> n; int a[n + 1]; for (i = 1; i <= n; i++) cin >> a[i]; co = 0; int c = 1; j = 0; vector<int> v; map<int, int> mp; for (i = 1; i <= n; i++) { mp[a[i]]++; co += a[i]; j++; if (a[i] < 0) { if (mp[-1 * a[i]] != 1) { co = -1; break; } } if (mp[a[i]] > 1) { c = 0; break; } if (co == 0) { v.push_back(j); mp.clear(); j = 0; co = 0; } } if (!co) { if (c) { cout << v.size() << n ; for (int i = 0; i < v.size(); i++) { cout << v[i] << ; } } else cout << -1 << n ; } else cout << -1 << n ; } |
#include <bits/stdc++.h> using namespace std; int n, tot; long long ans, sum; char s[100010], t[100010]; void print(int x, int opt) { tot++; printf( %d %d n , x, opt); if (tot == 100000) exit(0); } bool check(int i, int opt) { if (i == n) return 0; if (opt == -1) { if (s[i + 1] > 0 ) return s[i]--, s[i + 1]--, print(i, -1), 1; if (check(i + 1, 1)) return s[i]--, s[i + 1]--, print(i, -1), 1; return 0; } if (opt == 1) { if (s[i + 1] < 9 ) return s[i]++, s[i + 1]++, print(i, 1), 1; if (check(i + 1, -1)) return s[i]++, s[i + 1]++, print(i, 1), 1; return 0; } } int main() { scanf( %d%s%s , &n, s + 1, t + 1); for (int i = 1; i < n; i++) { int S = sum + s[i] - a , T = t[i] - a ; ans += abs(S - T), sum = T - S; } if (sum + s[n] - a != t[n] - a ) return puts( -1 ), 0; printf( %lld n , ans); for (int i = 1; i < n; i++) { while (s[i] > t[i]) if (!check(i, -1)) return puts( -1 ), 0; while (s[i] < t[i]) if (!check(i, 1)) return puts( -1 ), 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; char s[100005]; int c[100005], N, full; int LG; void read() { scanf( %d n , &N); for (int line = 1; line <= N; ++line) { scanf( %s , s + 1); s[0] = # ; int len = strlen(s) - 1; LG = len; for (int i = 1; i <= len; ++i) { if (s[i] == ? ) continue; if (c[i] == 0) { c[i] = s[i] - a + 1; continue; } if (c[i] == s[i] - a + 1) continue; else { if (c[i] != -1) { c[i] = -1; full++; } if (full == len) return; } } } } int main() { read(); for (int i = 1; i <= LG; ++i) if (c[i] == -1) printf( ? ); else if (c[i] == 0) printf( x ); else printf( %c , c[i] + a - 1); return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = (int)2e9; const double EPS = 1e-6; const double PI = 3.1415926535; const int MOD = 1000003; int last[100000]; int main() { int a, b, m, now, i = 2; cin >> a >> b >> m >> now; last[now] = 1; while (1) { now = (a * now + b) % m; if (last[now]) { cout << i - last[now]; return 0; } else last[now] = i++; } } |
#include <bits/stdc++.h> using namespace std; struct Expr { char a[1010], b[1010]; int v; int op; }; Expr exps[5010]; char ansMin[1010], ansMax[1010]; char buf[10010]; map<string, int> varIndex; int calc(int a, int b, int op) { if (op == 1) return a & b; else if (op == 2) return a | b; else if (op == 3) return a ^ b; } int work(int bit, int n, int v) { int ans = 0; for (int i = 0; i < n; i++) { if (exps[i].op) { int a, b; if (isdigit(exps[i].a[0])) a = exps[i].a[bit] - 0 ; else if (exps[i].a[0] == ? ) a = v; else { string _var; _var.assign(exps[i].a); a = varIndex[_var]; a = exps[a].v; } if (isdigit(exps[i].b[0])) b = exps[i].b[bit] - 0 ; else if (exps[i].b[0] == ? ) b = v; else { string _var; _var.assign(exps[i].b); b = varIndex[_var]; b = exps[b].v; } exps[i].v = calc(a, b, exps[i].op); ans += exps[i].v; } else { exps[i].v = exps[i].a[bit] - 0 ; ans += exps[i].v; } } return ans; } int main() { int n, m; char op[16], var[16]; scanf( %d%d , &n, &m); getchar(); for (int i = 0; i < n; i++) { gets(buf); if (sscanf(buf, %s := %s %s %s , var, exps[i].a, op, exps[i].b) == 4) { if (op[0] == A ) exps[i].op = 1; else if (op[0] == O ) exps[i].op = 2; else if (op[0] == X ) exps[i].op = 3; } else { sscanf(buf, %s := %s , var, exps[i].a); exps[i].op = 0; } string _var; _var.assign(var); varIndex[var] = i; } for (int i = 0; i < m; i++) { int a = work(i, n, 0); int b = work(i, n, 1); if (a > b) ansMin[i] = 1 , ansMax[i] = 0 ; else if (a < b) ansMin[i] = 0 , ansMax[i] = 1 ; else ansMin[i] = 0 , ansMax[i] = 0 ; } puts(ansMin); puts(ansMax); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; map<int, vector<int>> mappu; cin >> n >> m; for (long long i = (0); i < (m); i++) { int a, b; cin >> a >> b; mappu[a].push_back(b); mappu[b].push_back(a); } for (long long i = (1); i < (n + 1); i++) { mappu[i].push_back(i); sort(mappu[i].begin(), mappu[i].end()); } for (long long i = (1); i < (n + 1); i++) { for (long long j = (0); j < (mappu[i].size()); j++) { if (mappu[i] != mappu[mappu[i][j]]) { cout << NO ; return 0; } } } cout << YES ; } |
#include <bits/stdc++.h> using namespace std; int a[105]; struct Fenwick_Tree { int sz, c[305]; void init(int n) { sz = n; for (int i = 1; i <= n; i++) c[i] = 0; } int lowbit(int x) { return x & (-x); } void update(int p, int v) { for (int i = p; i <= sz; i += lowbit(i)) c[i] = max(c[i], v); } int query(int p) { int res = 0; for (int i = p; i > 0; i -= lowbit(i)) res = max(res, c[i]); return res; } } BIT; int b[200005], cnt[305]; int pre[305], suf[305]; int main() { int n, T; scanf( %d%d , &n, &T); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); if (T <= 2000) { for (int i = 0; i < T; i++) for (int j = 1; j <= n; j++) b[i * n + j] = a[j]; int ans = 0; BIT.init(300); for (int i = 1; i <= n * T; i++) { int len = BIT.query(b[i]) + 1; BIT.update(b[i], len); ans = max(ans, len); } printf( %d n , ans); } else { for (int i = 1; i <= n; i++) cnt[a[i]]++; for (int i = 0; i < 2000; i++) for (int j = 1; j <= n; j++) b[i * n + j] = a[j]; BIT.init(300); for (int i = 1; i <= 1000 * n; i++) { pre[b[i]] = BIT.query(b[i]) + 1; BIT.update(b[i], pre[b[i]]); } BIT.init(300); for (int i = 2000 * n; i > 1000 * n; i--) { suf[b[i]] = BIT.query(301 - b[i]) + 1; BIT.update(301 - b[i], suf[b[i]]); } int ans = 0; for (int i = 1; i <= 300; i++) ans = max(ans, pre[i] + suf[i] + cnt[i] * (T - 2000)); printf( %d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1000011; const long long INF = (1 << 29) + 123; const long long MOD = 1000000007; const long double PI = 4 * atan((long double)1); int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, w; cin >> n >> w; int l[n]; int x; long long ans[w], pf[w + 1]; for (int i = 0; i < w + 1; i++) ans[i] = pf[i] = 0; for (int i = 0; i < n; i++) { cin >> l[i]; deque<pair<long long, int>> store; int window = w - l[i] + 1; int best = -INF; for (int j = 0; j < w; j++) { if (j < l[i]) { cin >> x; while (!store.empty() && x >= store.back().first) store.pop_back(); store.push_back(make_pair(x, j)); best = max(best, x); } else if (j == l[i] && j < window) { j = window; if (best >= 0) { pf[l[i]] += best; pf[window] -= best; } else { if (window - 1 < w - window + 1) { pf[window - 1] += best; pf[w - window + 1] -= best; } } } if (j == w) break; while (store.front().second <= j - window) store.pop_front(); if (store.front().first > 0 || (j >= window - 1 && j <= w - window)) { ans[j] += 1LL * store.front().first; } } } long long total = 0; for (int i = 0; i < w; i++) { total += pf[i]; cout << ans[i] + total << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( sse4 ) using namespace std; constexpr long long mod = 1000000007; const long long INF = mod * mod; const double eps = 1e-12; const double pi = acosl(-1.0); long long mod_pow(long long x, long long n, long long m = mod) { long long res = 1; while (n) { if (n & 1) res = res * x % m; x = x * x % m; n >>= 1; } return res; } struct modint { long long n; modint() : n(0) { ; } modint(long long m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } modint operator*=(modint& a, modint b) { a.n = ((long long)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, long long n) { if (n == 0) return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } long long inv(long long a, long long p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } const int max_n = 1 << 10; modint fact[max_n], factinv[max_n]; void init_f() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b) return 0; return fact[a] * factinv[b] * factinv[a - b]; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); while (b) { long long r = a % b; a = b; b = r; } return a; } void solve() { long long a, b; cin >> a >> b; modint ans = 0; for (long long i = 1; i < b; i++) { long long num = a; ans += num * i; modint cr = num * (num + 1) / 2; cr *= i; ans += cr * (modint)b; } cout << ans << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int a, b, t, n, m, f[100010], ans, trl; struct node { int next, ar, flow, b; } k[100010 * 20]; int first[100010], dis[100010], len; int X[100010], y[100010], v[100010], e[100010]; int t1[100010], t2[100010], tl1, tl2, ANS; void add(int a, int b, int t) { len++; k[len].ar = b; k[len].next = first[a]; first[a] = len; k[len].flow = t; } int head, tail, d[100010]; bool bfs() { head = 0, tail = 1; d[0] = a; for (register int i = 1; i <= n; i++) dis[i] = 0; dis[a] = 1; while (head < tail) { t = d[head]; head++; for (register int i = first[t]; i; i = k[i].next) { if (dis[k[i].ar] == 0 && k[i].flow > 0) { dis[k[i].ar] = dis[t] + 1; if (k[i].ar == b) return true; d[tail] = k[i].ar; tail++; } } } return false; } int dfs(int xx, int flow) { if (xx == b) return flow; if (flow == 0) return 0; if (dis[xx] >= dis[b]) return 0; int h, s = 0; for (register int i = first[xx]; i > 1; i = k[i].next) { if (flow == 0) { dis[xx] = 0; break; } if (dis[k[i].ar] == dis[xx] + 1 && k[i].flow > 0) { h = dfs(k[i].ar, min(flow, k[i].flow)); s += h; flow -= h; k[i].flow -= h; k[i ^ 1].flow += h; if (h == 0) dis[k[i].ar] = 0; } } return s; } void dinic() { for (register int i = 1; i <= n; i++) first[i] = 0; len = 1, ans = 0; for (register int i = 1; i <= m; i++) add(X[i], y[i], f[i]), add(y[i], X[i], f[i]); while (bfs()) ans += dfs(a, 0x7ffffff); } struct gGomory_Hu_Tree { node tr[100010 * 2], trt[2][100010]; int trl, first[100010]; void adtr(int a, int b, int t) { trl++; tr[trl].ar = b; tr[trl].b = a; tr[trl].flow = t; tr[trl].next = first[a]; first[a] = trl; ANS += t; } void dfs(int x, int y, int &w) { for (int i = first[x]; i; i = tr[i].next) { if (tr[i].flow >= 0 && tr[i].ar != y) { if (tr[i].flow < tr[w].flow) w = i; dfs(tr[i].ar, x, w); } } } void work(int x) { int t = 1; dfs(x, 0, t); if (t == 1) { printf( %d , x); return; } tr[t].flow = tr[t ^ 1].flow = -1; work(tr[t].ar); work(tr[t].b); } } ght; void build(int l, int r) { if (l >= r) return; a = v[l]; b = v[l + 1]; dinic(); ght.adtr(b, a, ans); ght.adtr(a, b, ans); int tl1 = 0, tl2 = 0; for (register int i = l; i <= r; i++) { if (dis[v[i]]) tl1++, t1[tl1] = v[i], dis[v[i]] = 0; else tl2++, t2[tl2] = v[i]; } for (register int i = 1; i <= tl1; i++) v[i + l - 1] = t1[i]; for (register int i = 1; i <= tl2; i++) v[l + tl1 + i - 1] = t2[i]; build(l, tl1 + l - 1); build(l + tl1, r); } int main() { int T; len = 1; scanf( %d%d , &n, &m); for (register int i = 1; i <= m; i++) { scanf( %d%d%d , &a, &b, &t); X[i] = a, y[i] = b, f[i] = t; } ght.trl = 1; ght.tr[1].flow = 0x3f3f3f3f; for (register int i = 1; i <= n; i++) v[i] = i; build(1, n); printf( %d n , ANS / 2); ght.work(1); return 0; } |
#include <bits/stdc++.h> using namespace std; int a[10005], cnt[305], dp[10005], inf = 1e8; int main() { int n, t; scanf( %d %d , &n, &t); int max_num = 0, len = n * min(n / 2, t); if (len <= 0) len = n; for (int i = 1; i <= n; ++i) { scanf( %d , a + i); max_num = max(max_num, ++cnt[a[i]]); } for (int i = n + 1; i <= len; ++i) { a[i] = a[i - n]; } int ans = 0; for (int i = 1; i <= len; ++i) { dp[i] = 1; for (int j = i - 1; j >= max(i - n, 1); --j) { if (a[i] >= a[j]) { dp[i] = max(dp[i], dp[j] + 1); } } ans = max(ans, dp[i]); } if (n / 2 == 0) n++; if (t <= (n / 2)) { printf( %d n , ans); } else { printf( %d n , ans + max_num * (t - (n / 2))); } return 0; } |
#include <bits/stdc++.h> const int MAXN = 1e5 * 5; const int INF = 1e9 + 7; const int N = 5000; using namespace std; int n, m, h[N], c[N], cnt; char a[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { cin >> a[i][j]; if (a[i][j] == * ) ++h[i], ++c[j], ++cnt; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (h[i] + c[j] - (a[i][j] == * ) >= cnt) { cout << YES << n ; cout << i << << j; return 0; } cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1e9, MAX = 4000001, MOD = 1000000007; int t, n, k, ans[200001]; vector<int> cnt[200001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> k; int sum = 0; fill(ans, ans + n + 1, 0); for (int i = 0; i < n + 1; i++) cnt[i].clear(); for (int i = 0; i < n; i++) { int a; cin >> a; if (cnt[a].size() < k) cnt[a].push_back(i); } int curr = 1, aa = 0; for (int i = 1; i <= n; i++) { aa += cnt[i].size(); } aa -= aa % k; for (int i = 1; i <= n; i++) { int s = min(k, (int)cnt[i].size()); for (int j = 0; j < s; j++) { ans[cnt[i][j]] = curr; curr = curr % k + 1; aa--; if (aa == 0) break; } if (aa == 0) break; } for (int i = 0; i < n; i++) { cout << ans[i] << ; } cout << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { string aba; cin >> aba; int ab = 0; int ba = 0; if (aba[0] != aba[aba.size() - 1]) { aba[0] = aba[aba.size() - 1]; for (int i = 0; i < aba.size() - 1; i++) { if (aba[i] == a && aba[i + 1] == b ) { ab += 1; } else if (aba[i] == b && aba[i + 1] == a ) { ba += 1; } } if (ab == ba) { for (char c : aba) { cout << c; } } else { aba[aba.size() - 1] = aba[0]; for (char c : aba) { cout << c; } } } else { for (char c : aba) { cout << c; } } cout << n ; } } |
#include <bits/stdc++.h> using namespace std; const long double INF = 1e9; vector<tuple<int, int, int> > v; map<pair<long long, tuple<int, int, int> >, long double> mp; vector<int> e[31][4]; long double pb[] = {1.0 / 6, 1.0 / 6, 1.0 / 3, 1.0 / 3}; int get_pos(vector<tuple<int, int, int> > v, tuple<int, int, int> p) { return int(lower_bound(v.begin(), v.end(), p) - v.begin()); } tuple<int, int, int> good_tp(int a, int b, int c) { if (a > c) swap(a, c); return tuple<int, int, int>(a, b, c); } long long to_num(int a[]) { int b[8]; for (int i = 0; i < 8; i++) b[i] = a[i]; sort(b, b + 8); long long r = 0; for (int i = 0; i < 8; i++) { r *= 22; r += b[i]; } return r; } long double ask(pair<long long, tuple<int, int, int> > x) { if (mp.find(x) != mp.end()) return mp[x]; else { long long p = x.first; int a = get<0>(x.second); int b = get<1>(x.second); int c = get<2>(x.second); int id = get_pos(v, x.second); int A = a, B = b, C = c; if (A && B && C) A = B = C = 0; int num[8]; for (int i = 7; i >= 0; i--) num[i] = p % 22, p /= 22; long double unchanged = pb[0]; bool OK = false; long double sum = 1; for (int i = 1; i <= 3; i++) { bool flag = false; long double mn = INF; for (int j = 0; j < 8; j++) { int t = num[j]; for (int x : e[t][i]) { flag = true; OK = true; num[j] = x; if (A != a) num[0] = id; long long next_num = to_num(num); if (A != a) num[0] = 0; tuple<int, int, int> next_tp; if (!A) { next_tp = good_tp(i, B, C); mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, next_tp))); } if (!B) { next_tp = good_tp(A, i, C); mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, next_tp))); } if (!C) { next_tp = good_tp(A, B, i); mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, next_tp))); } num[j] = t; } } if (!flag) unchanged += pb[i]; else { sum += pb[i] * mn; } } if (!OK) return mp[x] = 0; else { return mp[x] = sum / (1 - unchanged); puts( oao ); } } } int char_to_int(char c) { if (c == R ) return 1; else if (c == G ) return 2; else return 3; } int main() { for (int i = 0; i <= 3; i++) { v.emplace_back(0, 0, i); if (i) e[i][i].push_back(0); } int m = 4; for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) for (int k = i; k <= 3; k++) { v.emplace_back(i, j, k); e[m][j].push_back(0); e[m][i].push_back(k); if (i != k) e[m][k].push_back(i); m++; } int n; scanf( %d , &n); string second; int num[8] = {}; for (int ni = 0; ni < n - 1; ni++) { cin >> second; int a = char_to_int(second[0]); int b = char_to_int(second[1]); int c = char_to_int(second[2]); tuple<int, int, int> t = good_tp(a, b, c); num[ni] = get_pos(v, t); } cin >> second; int a = char_to_int(second[0]); int b = char_to_int(second[1]); int c = char_to_int(second[2]); tuple<int, int, int> next_tp = good_tp(a, b, c); printf( %.9f n , (double)ask(pair<long long, tuple<int, int, int> >( to_num(num), next_tp))); } |
#include <bits/stdc++.h> using namespace std; int n, T; vector<int> intp; vector<int> t; vector<double> p; vector<vector<long double> > dp; vector<vector<long double> > pows; long double ans; void addAns(const vector<long double>& prob, int index) { for (int j = 1; j <= T; j++) { if (index == n - 1) { ans += prob[j] * n; continue; } if (j + t[index + 1] > T) { ans += prob[j] * (index + 1) * pows[100 - intp[index + 1]][T - j]; } } } int main() { cin >> n >> T; intp = vector<int>(n); p = vector<double>(n); t = vector<int>(n); for (int i = 0; i < (int)(n); i++) { cin >> intp[i] >> t[i]; p[i] = intp[i] / 100.0; } pows = vector<vector<long double> >(101, vector<long double>(T + 1)); for (int i = 0; i <= 100; i++) { pows[i][0] = 1; for (int j = 1; j <= T; j++) { pows[i][j] = pows[i][j - 1] * i / 100.0; if (pows[i][j] < 1e-100) pows[i][j] = 0; } } dp = vector<vector<long double> >(2, vector<long double>(T + 1)); long double curp = 1; for (int j = 1; j < t[0]; j++) { dp[0][j] = curp * p[0]; curp *= (1 - p[0]); } dp[0][t[0]] = curp; addAns(dp[0], 0); for (int i = 1; i < n; i++) { int ci = i & 1; int pi = ci ^ 1; long double sum = 0; for (int j = 1; j <= T; j++) { sum = sum * (1 - p[i]) + dp[pi][j - 1]; if (j >= t[i]) { sum -= dp[pi][j - t[i]] * pows[100 - intp[i]][t[i] - 1]; } dp[ci][j] = sum * p[i]; if (j >= t[i]) { dp[ci][j] += dp[pi][j - t[i]] * pows[100 - intp[i]][t[i] - 1]; } } addAns(dp[ci], i); } cout.precision(16); cout << fixed << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << ( << p.first << , << p.second << ) ; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << { ; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? , : ) << *it; return out << } ; } void dump() { cerr << n ; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << , ; dump(x...); } template <class T> int size(T &&a) { return a.size(); } using LL = long long; using PII = pair<int, int>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; auto ask = [&](int len) { cout << ? 1 << len << endl; int k = len * (len + 1) / 2; vector<vector<string>> ret(len + 1); for (int i = 0; i < k; i++) { string str; cin >> str; ret[size(str)].emplace_back(str); } return ret; }; if (n <= 2) { auto x = ask(1); if (n == 1) cout << ! << x[1][0]; if (n == 2) { auto y = ask(2); cout << ! << x[1][0] << (y[1][0] == x[1][0] ? y[1][1] : y[1][0]); } cout << endl; return 0; } const int A = 26; auto count = [&](vector<string> vec) { vector<int> ret(A); for (auto &str : vec) for (char c : str) ret[c - a ]++; return ret; }; auto process = [&](int len, vector<vector<string>> all) { vector<vector<int>> ret(len + 1); auto ref = count(all[1]); auto used = ref; for (int i = 1; i <= (len - 1) / 2; i++) { auto a = count(all[i + 1]); auto b = ref; for (int &x : b) x *= (i + 1); for (int j = 1; j <= i - 1; j++) { for (int &x : ret[j]) a[x] += 1 + i - j; } for (int j = 0; j < A; j++) for (int k = 0; k < b[j] - a[j]; k++) { ret[i].emplace_back(j); used[j]--; } } for (int j = 0; j < A; j++) for (int k = 0; k < used[j]; k++) ret[(len + 1) / 2].emplace_back(j); return ret; }; int l = (n + 1) / 2; auto h1 = ask(l); auto h2 = ask(l - 1); auto e1 = process(l, h1); auto e2 = process(l - 1, h2); false; false; vector<int> ans(n + 1); auto c1 = count(h1[1]); auto c2 = count(h2[1]); for (int j = 0; j < A; j++) if (c1[j] != c2[j]) ans[l] = j; false; auto find = [&](vector<int> a, int b) { return (a[0] == b ? a[1] : a[0]); }; for (int i = 1; i <= l / 2; i++) { ans[i] = find(e1[i], ans[l + 1 - i]); if (size(e2[i]) == 2) ans[l - i] = find(e2[i], ans[i]); } false; auto h = ask(n); auto e = process(n, h); false; for (int i = 1; i <= n / 2; i++) { ans[n + 1 - i] = find(e[i], ans[i]); false; } cout << ! ; for (int i = 1; i <= n; i++) cout << char(ans[i] + a ); cout << endl; } |
#include <bits/stdc++.h> using namespace std; char getc() { char c = getchar(); while ((c < A || c > Z ) && (c < a || c > z ) && (c < 0 || c > 9 )) c = getchar(); return c; } long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); } long long read() { long long x = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, m; long long a[300010], b[300010]; signed main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) b[i] = read(); sort(a + 1, a + n + 1); long long u = 0; for (int i = 2; i <= n; i++) u = gcd(u, a[i] - a[i - 1]); for (int i = 1; i <= m; i++) if (u % b[i] == 0) { cout << YES << endl; cout << a[1] << << i; return 0; } cout << NO ; return 0; } |
#include <bits/stdc++.h> int main() { long long a, b, k = 0; bool flag = true; std::stack<long long> st; std::cin >> a >> b; st.push(b); while (b > a && flag) { if (b % 10 == 1) { b = b / 10; st.push(b); k++; } else if (b % 2 == 0) { b = b / 2; st.push(b); k++; } else { flag = false; } } if (!flag || b != a) { std::cout << NO ; } else { std::cout << YES << std::endl << k + 1 << std::endl; while (!st.empty()) { std::cout << st.top() << ; st.pop(); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int fin[100005]; int main() { int n; while (cin >> n) { int l, r; int flag = 1; int temp; for (int i = 0; i < n; i++) { cin >> l >> r; if (i == 0) fin[0] = l > r ? l : r; else { temp = l > r ? l : r; if (temp <= fin[i - 1]) fin[i] = temp; else fin[i] = l < r ? l : r; if (fin[i] > fin[i - 1]) flag = 0; } } if (!flag) cout << NO << endl; if (flag) cout << YES << endl; } } |
#include <bits/stdc++.h> using namespace std; const int N = 3005; int n, m, k, p[N]; bool vis[N]; inline void mk(int v) { for (int i = v; !vis[i]; i = p[i]) vis[i] = 1; } int main() { while (~scanf( %d , &n)) { for (int i = 0; i < n; ++i) scanf( %d , &p[i]), --p[i]; scanf( %d , &m); m = n - m; for (int i = 0; i < n; ++i) if (!vis[i]) k++, mk(i); for (int i = 0; i < n; ++i) vis[i] = false; printf( %d n , (int)abs(k - m)); if (k > m) { mk(0); for (int i = 1; i < n && k > m; ++i) { if (!vis[i]) { printf( %d %d , 1, i + 1); k--; mk(i); } } } else if (k < m) { for (int i = 0; i < n && k < m; ++i) { vector<int> pos(n, -1); int cur = 0; for (int j = p[i]; j != i; j = p[j]) pos[j] = ++cur; pos[i] = cur; cur = 0; for (int j = i + 1; j < n && k < m; ++j) if (pos[j] > cur) { printf( %d %d , i + 1, j + 1); k++; cur = pos[j]; swap(p[i], p[j]); } } } puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct crew { string name; string pos; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<crew> a(n); for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].pos; for (int i = 0; i < n; i++) { if (a[i].pos == rat ) cout << a[i].name << n ; } for (int i = 0; i < n; i++) { if (a[i].pos == child || a[i].pos == woman ) cout << a[i].name << n ; } for (int i = 0; i < n; i++) { if (a[i].pos == man ) cout << a[i].name << n ; } for (int i = 0; i < n; i++) { if (a[i].pos == captain ) cout << a[i].name << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int m, n, t, k; long long a[10]; long long x[10]; int cnt; bool ok(int n) { for (int i = 2; i <= n; i++) if (a[i] == i) return 0; for (int i = 2; i <= n; i++) { int t = a[i]; int cnt = 0; while (t != 1) { cnt++; t = a[t]; if (cnt > n) return 0; } } return 1; } void fun(int n, int cur) { if (cur == n + 1) { if (ok(n)) cnt++; return; } for (int i = 1; i <= n; ++i) { a[cur] = i; fun(n, cur + 1); } } int main() { for (int i = 1; i <= 8; i++) { cnt = 0; fun(i, 2); x[i] = cnt; } scanf( %d%d , &n, &k); long long s = n - k; long long ans = (x[k] * k) % 1000000007; for (int i = 0; i < s; ++i) ans = (ans * s) % 1000000007; printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { char s[5000], a[5000]; char b[] = bear ; int count = 0, j = 0; scanf( %s , &s); int i = 0; int n = strlen(s); printf( %s , a); for (i = 0; i < n; i++) { for (j = i; j < n; j++) { if (s[j] == b && s[j + 1] == e && s[j + 2] == a && s[j + 3] == r ) { count = count + n - j - 3; count = count + j - i; count = count + (n - j - 4) * (j - i); i = j + 1; } } } printf( %d , count); return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; bool odd(ll num) { return ((num & 1) == 1); } bool even(ll num) { return ((num & 1) == 0); } ll sum(ll n) { return ((n * (n + 1)) / 2); } ll ceil(ll a, ll b) { return ((a + (b - 1)) / b); } void InputOutput() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll calculation(vector<ll> &v, ll k) { ll dis, cnt, len, index; dis = cnt = 0; len = (ll)v.size(); len -= k; index = len - 1; for (ll i = len - 1; i >= 0; i--) { cnt++; if (cnt == k) { dis += (v[index] * 2); cnt = 0; index = i - 1; } } if (cnt) dis += (v[index] * 2); return dis; } void solve() { ll n, k; cin >> n >> k; vector<ll> pos, neg; ll temp, dis; for (ll i = 0; i < n; i++) { cin >> temp; if (temp < 0) neg.push_back(abs(temp)); else if (temp > 0) pos.push_back(temp); } if (pos.empty() and neg.empty()) { cout << 0 << n ; return; } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); dis = 0; dis += calculation(pos, k); dis += calculation(neg, k); if (pos.empty()) dis += neg.back(); else if (neg.empty()) dis += pos.back(); else { if (pos.back() < neg.back()) { dis += (pos.back() * 2); dis += neg.back(); } else { dis += (neg.back() * 2); dis += pos.back(); } } cout << dis << n ; } int main() { InputOutput(); int t = 1; cin >> t; for (int test_case = 1; test_case <= t; test_case++) { solve(); } return 0; } |
#include <bits/stdc++.h> int main() { int n, m, i, j, b = 0; scanf( %d %d , &n, &m); int pass[n]; int finger[m]; int print[m]; for (i = 0; i < n; i++) { scanf( %d , &pass[i]); } for (i = 0; i < m; i++) { scanf( %d , &finger[i]); } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (pass[i] == finger[j]) { print[b] = pass[i]; b++; } } } for (i = 0; i < b; i++) { printf( %d , print[i]); } return 0; } |
#include <bits/stdc++.h> using namespace std; void in(int &r) { static char c; r = 0; while (c = getchar(), !isdigit(c)) ; do r = (r << 1) + (r << 3) + (c ^ 48); while (c = getchar(), isdigit(c)); } const int mn = 100005; int head[mn], ne[mn << 1], to[mn << 1], cnt1; int fa[mn]; int n, m; namespace part5 { struct segment_tree { bool mark[mn << 2]; int maxv[mn << 2], sum[mn << 2]; void up(int o) { sum[o] = sum[o << 1] + sum[o << 1 | 1]; maxv[o] = max(maxv[o << 1] + sum[o << 1 | 1], maxv[o << 1 | 1]); } int L[mn << 2], R[mn << 2]; void build(int o, int l, int r) { L[o] = l, R[o] = r; if (l == r) { sum[o] = -1; maxv[o] = -1; return; } int mid = l + r >> 1; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); up(o); } int y_1, y_2; void down(int o) { if (mark[o]) { mark[o << 1] = 1; sum[o << 1] = -(R[o << 1] - L[o << 1] + 1); maxv[o << 1] = -1; mark[o << 1 | 1] = 1; sum[o << 1 | 1] = -(R[o << 1 | 1] - L[o << 1 | 1] + 1); maxv[o << 1 | 1] = -1; mark[o] = 0; } } void mk(int o, int l, int r) { if (y_1 <= l && r <= y_2) { maxv[o] = -1, mark[o] = 1, sum[o] = -(r - l + 1); return; } down(o); int mid = l + r >> 1; if (y_1 <= mid) mk(o << 1, l, mid); if (y_2 > mid) mk(o << 1 | 1, mid + 1, r); up(o); } void mk(int l, int r) { y_1 = l, y_2 = r; mk(1, 1, n); } int ad_v; void add(int o, int l, int r) { if (l == r) { sum[o] += ad_v; maxv[o] += ad_v; return; } down(o); int mid = l + r >> 1; if (y_1 <= mid) add(o << 1, l, mid); else add(o << 1 | 1, mid + 1, r); up(o); } void add(int x, int v) { y_1 = x, ad_v = v; add(1, 1, n); } int S, Mx; void ask(int o, int l, int r) { if (y_1 <= l && r <= y_2) { Mx = max(Mx, maxv[o] + S); S += sum[o]; return; } down(o); int mid = l + r >> 1; if (y_2 > mid) ask(o << 1 | 1, mid + 1, r); if (y_1 <= mid) ask(o << 1, l, mid); } pair<int, int> ask(int l, int r) { y_1 = l, y_2 = r; S = 0, Mx = -1; ask(1, 1, n); return make_pair(S, Mx); } } an; int sz[mn], son[mn], fa[mn], H[mn], high; void dfs(int x, int f) { H[x] = ++high, sz[x] = 1, fa[x] = f; for (int q(head[x]); q; q = ne[q]) if (to[q] != f) { dfs(to[q], x); sz[x] += sz[to[q]]; if (sz[to[q]] > sz[son[x]]) son[x] = to[q]; } --high; } int dfn_l[mn], dfn_r[mn], ind, top[mn]; void redfs(int x, int f, int tp) { dfn_l[x] = ++ind, top[x] = tp; if (son[x]) redfs(son[x], x, tp); for (int q(head[x]); q; q = ne[q]) if (to[q] != f && to[q] != son[x]) redfs(to[q], x, to[q]); dfn_r[x] = ind; } pair<int, int> get(int x) { int S = 0, Mx = -1; while (x) { pair<int, int> now = an.ask(dfn_l[top[x]], dfn_l[x]); Mx = max(Mx, now.second + S); S += now.first; x = fa[top[x]]; } return make_pair(S, Mx); } void solve() { dfs(1, 0); redfs(1, 0, 1); an.build(1, 1, n); int ty, x; while (m--) { in(ty), in(x); if (ty == 1) an.add(dfn_l[x], 1); else if (ty == 2) { an.mk(dfn_l[x], dfn_r[x]); pair<int, int> now = get(x); if (now.second != -1) an.add(dfn_l[x], -(1 + now.second)); now = get(x); } else { pair<int, int> now = get(x); if (now.second != -1) puts( black ); else puts( white ); } } } } // namespace part5 int main() { in(n), in(m); int a; for (int q = 2, q_end_ = n; q <= q_end_; ++q) in(a), to[++cnt1] = a, ne[cnt1] = head[q], head[q] = cnt1, to[++cnt1] = q, ne[cnt1] = head[a], head[a] = cnt1; part5::solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf( %d%d , &n, &k); int z = k - 1; while (n % z) z--; int y = n / z; printf( %d n , y * k + z); } |
#include <bits/stdc++.h> using namespace std; unordered_map<string, vector<string> > m; set<string> q; string SS(char C) { string p = ; p += C; return p; } void dfs(string s, long n) { if (n == 0) { q.insert(s); return; } for (auto i : m[SS(s[0])]) dfs(i + s.substr(1, s.size() - 1), n - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); long n, t; cin >> n >> t; string s, p; for (long i = long(0); i < long(t); i++) { cin >> s >> p; m[p].push_back(s); } for (auto i : m[ a ]) dfs(i, n - 2); cout << q.size(); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 3700, maxd = 1850, mod = 998244353; int h, w, tr[maxn], tc[maxn], x, y, n; vector<int> rs, cs; long long f[maxd], c[maxn][maxn], dpr[maxn][maxd], dpc[maxn][maxd], ans; long long C(int a, int b) { if (a < 0 || b < 0 || a < b) return 0; return c[a][b]; } int main() { ios::sync_with_stdio(0); cin.tie(0); f[0] = 1; for (int i = 1; i < maxd; i++) f[i] = (f[i - 1] * i) % mod; for (int i = 0; i < maxn; i++) c[i][0] = 1; for (int i = 1; i < maxn; i++) { for (int j = 1; j < maxn; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; } cin >> h >> w >> n; for (int i = 0; i < 2 * n; i++) { cin >> x >> y; x--; y--; tr[x]++; tc[y]++; } dpr[0][0] = dpc[0][0] = 1; for (int i = 0; i < h; i++) { if (tr[i] == 0) { for (int j = 0; j <= ((((int)rs.size())) + 1) / 2; j++) { dpr[(((int)rs.size())) + 1][j] = dpr[(((int)rs.size()))][j]; if (!rs.empty() && rs.back() == i - 1 && j > 0) { dpr[(((int)rs.size())) + 1][j] += dpr[(((int)rs.size())) - 1][j - 1]; dpr[(((int)rs.size())) + 1][j] %= mod; } } rs.push_back(i); } } for (int i = 0; i < w; i++) { if (tc[i] == 0) { for (int j = 0; j <= ((((int)cs.size())) + 1) / 2; j++) { dpc[(((int)cs.size())) + 1][j] = dpc[(((int)cs.size()))][j]; if (!cs.empty() && cs.back() == i - 1 && j > 0) { dpc[(((int)cs.size())) + 1][j] += dpc[(((int)cs.size())) - 1][j - 1]; dpc[(((int)cs.size())) + 1][j] %= mod; } } cs.push_back(i); } } for (int i = 0; i <= (((int)rs.size())) / 2; i++) { for (int j = 0; j <= (((int)cs.size())) / 2; j++) { ans += ((((dpr[(((int)rs.size()))][i] * dpc[(((int)cs.size()))][j]) % mod) * ((C((((int)rs.size())) - 2 * i, j) * C((((int)cs.size())) - 2 * j, i)) % mod)) % mod) * ((f[i] * f[j]) % mod); ans %= mod; } } cout << ans; return 0; } |
#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 maxs(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T mins(T& a, T1 b) { if (b < a) a = b; return a; } const long long N = (1 << 21); const long long M = N - 1; pair<long long, long long> dp[N]; long long solve() { long long n; cin >> n; vector<long long> a(n + 1); auto add = [&](long long mask, long long j) { if (dp[mask].first == -1) dp[mask].first = j; else if (dp[mask].second == -1) { dp[mask].second = j; if (dp[mask].second < dp[mask].first) swap(dp[mask].first, dp[mask].second); } else if (dp[mask].second < j) { dp[mask].first = dp[mask].second; dp[mask].second = j; } else if (dp[mask].first < j && j != dp[mask].second) { dp[mask].first = j; } }; for (long long i = 1; i < n + 1; i++) { cin >> a[i]; add(a[i], i); } for (long long i = 0; i < 21; i++) { for (long long mask = 0; mask < N; mask++) { if ((mask) & (1 << i)) { if (dp[mask].first != -1) add(mask ^ (1 << i), dp[mask].first); if (dp[mask].second != -1) add(mask ^ (1 << i), dp[mask].second); } } } long long ans = 0; for (long long i = 1; i < n + 1; i++) { long long rev_mask = M - a[i]; long long best = 0; for (long long j = 20; j >= 0; j--) { if (rev_mask & (1 << j)) { if (dp[best ^ (1 << j)].second != -1 && dp[best ^ (1 << j)].first > i) { best ^= (1 << j); } } } if (i + 2 <= n) maxs(ans, a[i] + best); } cout << ans << n ; return 0; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; string a(n, 0 ); string b(n, 0 ); int flag = 0; for (int i = 0; i < n; i++) { if (s[i] == 2 ) { if (flag == 0) { a[i] = 1 ; b[i] = 1 ; } else { a[i] = 0 ; b[i] = 2 ; } } if (s[i] == 1 ) { if (flag == 0) { a[i] = 1 ; flag = 1; } else { b[i] = 1 ; } } } cout << a << n ; cout << b << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - 0 ; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 2e5 + 5; int parent[MAX], sz[MAX]; set<int> s[MAX]; int n, t[MAX], ans[MAX]; void init() { for (int i = 1; i < MAX; i++) { parent[i] = i, sz[i] = 1, t[i] = 1; s[i].insert(i); } } int root(int u) { while (parent[u] != u) u = parent[parent[u]]; return u; } void union_make(int u, int v) { int root_u = root(u); int root_v = root(v); if (root_u == root_v) return; if (sz[root_u] >= sz[root_v]) { parent[root_v] = parent[root_u]; sz[root_u] += sz[root_v]; } else { parent[root_u] = parent[root_v]; sz[root_v] += sz[root_u]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int x, y, i; cin >> n; init(); for (i = 1; i < n; ++i) { cin >> x >> y; int u = root(x); int v = root(y); if (sz[u] < sz[v]) swap(u, v); long long int add = sz[u]; for (auto it : s[v]) { s[u].insert(it); t[it] += add; } s[v].clear(); union_make(x, y); } for (i = 1; i <= n; ++i) { ans[t[i]] = i; } for (i = 1; i <= n; ++i) cout << ans[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 1e6; const long long MX = -1e7 - 3; const long long MN = 1e7 + 3; long long n, m, cnt, ans, x, y, mx = MX, mn = MN, a, b, c, d, T, l, r, k; float p = 3.14159; bool bl, bl1; string str = , s; map<char, long long> mp, mp1; vector<long long> v; deque<long long> q; bool used[N]; pair<long long, long long> pr[N]; pair<long long, char> pr1[N]; int main() { cin >> T; for (long long i = 0; i < T; i++) { cin >> n >> x >> a >> b; if (a > b) { c = a; a = b; b = c; } if (a - x >= 1) { a -= x; x = 0; } else { x -= a - 1; a = 1; } if (b + x <= n) { b += x; x = 0; } else b = n; v.push_back(b - a); } for (long long i = 0; i < v.size(); i++) cout << v[i] << n ; cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); long long ts, tf, t; cin >> ts >> tf >> t; int n; cin >> n; map<long long, int> T; for (int i = 0; i < n; ++i) { long long t; cin >> t; ++T[t]; } long long bt = 0, ans = ts + t; for (const auto &s : T) { if (ts + t > tf) break; if (ts < s.first) { bt = ts; break; } if (s.first > 0) { long long ta = s.first - 1; if (ts + t - ta < ans) { ans = ts + t - ta; bt = ta; } } ts += t * s.second; } if (ts + t <= tf) bt = ts; cout << bt << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 200010, INF = 1e9; int cnt[MAX]; int n, c, ans = INF; int mx[MAX], v[MAX]; bool f[MAX]; vector<int> a; bool ok(int x, int y) { if (x == 0) return 1; if (y == 0) return 0; x -= a[y] * min(cnt[a[y]], x / a[y]); if (mx[x] > y - 1) return ok(x, y - 1); else return f[x]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> c >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; cnt[x]++; } a.push_back(0); for (int i = 1; i <= c; i++) if (cnt[i]) a.push_back(i); a.push_back(c); int cur = c; for (int i = a.size() - 1; i >= 1; i--) cur -= a[i] * min(cnt[a[i]], cur / a[i]), v[i] = cur; memset(mx, -1, sizeof mx); for (int i = 0; i < a.size() - 1; i++) { for (int j = a[i]; j < a[i + 1]; j++) { mx[j] = i; f[j] = ok(j, i); } } for (int i = 0; i < a.size() - 1; i++) { for (int j = a[i]; j < a[i + 1]; j++) { if (v[i + 1] >= j && !ok(v[i + 1] - j, i)) ans = min(ans, j); } } if (ans != INF) cout << ans << endl; else cout << Greed is good << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; set<int> s; for (int i = 0; i < n; i++) { int x; cin >> x; s.insert(x); } set<int>::iterator it = s.begin(); int c = 0; while (it != s.end()) { if (*it == 0) c++; it++; } cout << s.size() - c << endl; } |
#include <bits/stdc++.h> inline int read() { int data = 0, w = 1; char ch = getchar(); while (ch != - && (ch < 0 || ch > 9 )) ch = getchar(); if (ch == - ) w = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) data = data * 10 + (ch ^ 48), ch = getchar(); return data * w; } const int N(205), LIM(20010), M(20000), INF(0x3f3f3f3f); struct edge { int next, to, cap; } e[N * N * 2]; int n, A[N], not_prime[LIM], head[N], e_num = -1, lev[N], cur[N], S, T; inline void Add(int from, int to, int cap) { e[++e_num] = (edge){head[from], to, cap}, head[from] = e_num; e[++e_num] = (edge){head[to], from, 0}, head[to] = e_num; } void Init() { not_prime[1] = 1; for (int i = 2; i <= M; i++) if (!not_prime[i]) for (int j = i * i; j <= M; j += i) not_prime[j] = 1; } int bfs() { std::queue<int> Q; Q.push(S); memset(lev, 0, (T + 1) << 2), lev[S] = 1; while (!Q.empty()) { int x = Q.front(); Q.pop(); for (int i = head[x]; ~i; i = e[i].next) { int to = e[i].to; if (!e[i].cap || lev[to]) continue; lev[to] = lev[x] + 1, Q.push(to); } } return lev[T]; } int dfs(int x, int f) { if (x == T || !f) return f; int ans = 0, cap; for (int i = head[x]; ~i; i = e[i].next) { int to = e[i].to; if (e[i].cap && lev[to] == lev[x] + 1) { cap = dfs(e[i].to, std::min(f - ans, e[i].cap)); e[i].cap -= cap, e[i ^ 1].cap += cap, ans += cap; if (ans == f) break; } } if (!ans) lev[x] = 0; return ans; } int Dinic() { int ans = 0; while (bfs()) memcpy(cur, head, (T + 1) << 2), ans += dfs(S, INF); return ans; } std::vector<int> cir[N]; int tot, vis[N]; void find(int x) { if (1 <= x && x <= n) cir[tot].push_back(x); vis[x] = 1; for (int i = head[x]; ~i; i = e[i].next) if (!vis[e[i].to] && e[i | 1].cap == 1) find(e[i].to); } int main() { memset(head, -1, sizeof head); Init(), n = read(), S = 0, T = n + 1; int p = 0, q = 0; for (int i = 1; i <= n; i++) A[i] = read(), ((A[i] & 1) ? ++p : ++q); if (p != q) return puts( Impossible ), 0; for (int i = 1; i <= n; i++) if (A[i] & 1) for (int j = (Add(S, i, 2), 1); j <= n; j++) { if (!not_prime[A[i] + A[j]]) Add(i, j, 1); } else Add(i, T, 2); if (Dinic() != n) return puts( Impossible ), 0; for (int i = 1; i <= n; i++) if (!vis[i]) ++tot, find(i); printf( %d n , tot); for (int i = 1; i <= tot; i++, puts( )) { printf( %lu , cir[i].size()); for (int j : cir[i]) printf( %d , j); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> vec(n); for (int i = 0; i < n; i++) { cin >> vec[i]; } if (n == 1) { if (vec[0] == 15) cout << DOWN << endl; else if (vec[0] == 0) cout << UP << endl; else cout << -1 << endl; } else { if (vec[n - 1] == 0 || (vec[n - 1] > vec[n - 2] && vec[n - 1] != 15)) cout << UP << endl; else cout << DOWN << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[100010], g, cnt; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &a[i]), g = g == 0 ? a[i] : gcd(a[i], g), cnt += a[i] & 1; printf( %d n , cnt > 1 ? 0 : g); string str, ans; for (int i = 0; i < n; i++) for (int j = 1; j <= a[i] / g / 2; j++) str = str + char( a + i); for (int i = 0; i < n; i++) if (a[i] / g & 1) str = str + char(i + a ); for (int i = n - 1; i >= 0; i--) for (int j = 1; j <= a[i] / g / 2; j++) str = str + char(i + a ); while (g--) ans = ans + str, reverse(str.begin(), str.end()); return cout << ans, 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> V; void dfs(long long x) { if (x < 1000000000) { V.push_back(x); dfs(x * 10 + 4); dfs(x * 10 + 7); } } int main() { dfs(0); V.push_back(INT_MAX); sort(V.begin(), V.end()); int pl, pr, vl, vr, k; cin >> pl >> pr >> vl >> vr >> k; double cnt = 0; double all = ((vr - vl) + 1.0) * ((pr - pl) + 1.0); for (int i = 1; i < V.size() - k; i++) { int hll = V[i - 1] + 1; int hl = V[i]; int hr = V[(i + k) - 1]; int hrr = V[(i + k)] - 1; if (pl <= hl && pr >= hll && vl <= hrr && vr >= hr) { cnt += (min(pr, hl) - max(pl, hll) + 1.0) * (min(vr, hrr) - max(vl, hr) + 1.0); } if (vl <= hl && vr >= hll && pl <= hrr && pr >= hr) { cnt += (min(vr, hl) - max(vl, hll) + 1.0) * (min(pr, hrr) - max(pl, hr) + 1.0); if (k == 1) cnt -= 1.0; } } printf( %.12f , cnt / all); exit: return (0); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int cnt = 0; string ans = 1 ; for (int i = 0; i < n; i++) { string str; cin >> str; while (str.size() && str.back() == 0 ) str.pop_back(), cnt++; if (str.size() == 0) { cout << 0 ; exit(0); } if (str.size() != 1 || str.back() != 1 ) ans = str; } cout << ans; for (int i = 0; i < cnt; i++) cout << 0 ; return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T, class L> bool smax(T& x, L y) { return x < y ? (x = y, 1) : 0; } template <class T, class L> bool smin(T& x, L y) { return x > y ? (x = y, 1) : 0; } const int maxn = 16; int n, a[maxn], b[maxn][maxn], ex; bool mark[maxn]; vector<vector<int> > all; void bt(int s = 0) { if (s == n) { for (int i = 0, sum = 0; i < n; i++, sum = 0) { for (int j = 0; j < n; j++) sum += b[j][i]; if (sum != ex) return; } { int sum = 0; for (int i = 0; i < n; i++) sum += b[i][i]; if (sum != ex) return; } { int sum = 0; for (int i = 0; i < n; i++) sum += b[i][n - i - 1]; if (sum != ex) return; } for (int i = 0; i < n; i++, cout << n ) for (int j = 0; j < n; j++) cout << b[i][j] << ; exit(0); } for (auto v : all) { bool reval = 1; for (auto x : v) if (mark[x]) { reval = 0; break; } if (!reval) continue; do { for (int i = 0; i < n; i++) b[s][i] = a[v[i]], mark[v[i]] = 1; bt(s + 1); for (int i = 0; i < n; i++) mark[v[i]] = 0; } while (next_permutation(v.begin(), v.end())); } } int main() { ios::sync_with_stdio(0), cin.tie(); cin >> n; for (int i = 0; i < n * n; i++) cin >> a[i], ex += a[i]; ex /= n; cout << ex << n ; for (int i = 0; i < 1 << n * n; i++) if (__builtin_popcount(i) == n) { int sum = 0; for (int j = 0; j < n * n; j++) sum += (i >> j & 1) * a[j]; if (sum == ex) { all.push_back({}); for (int j = 0; j < n * n; j++) if (i >> j & 1) all.back().push_back(j); sort(all.back().begin(), all.back().end()); } } bt(); return 0; } |
#include <bits/stdc++.h> int main() { char p[1000001]; int n, m, y2; int y1 = -1; long long ans = 1; scanf( %d %d , &n, &m); scanf( %s , p); int plen = strlen(p); for (int i = 0; i < m; ++i) { scanf( %d , &y2); if (-1 == y1) { n -= plen; } else if (y2 - y1 < plen) { if (p[y2 - y1] != p[0]) { printf( 0 n ); return 0; } n -= y2 - y1; } else { n -= plen; } y1 = y2; } for (int i = 0; i < n; ++i) { ans = ans * 26 % 1000000007; } printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> int main() { int tcase; for (scanf( %d , &tcase); tcase--;) { long long l, r; scanf( %lld%lld , &l, &r); if (l == r) { printf( %lld n , r); continue; } int p = 63 - __builtin_clzll(l ^ r); long long temp = 0; for (int i = 63; i > p; --i) if (l >> i & 1) temp |= 1LL << i; long long a = (1LL << p) - 1, b = (1LL << (p + 1)) - 1; a |= temp, b |= temp; if (l <= b && b <= r) a = b; printf( %lld n , a); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; struct dsu { vector<int> par, rnk, size; int c; dsu(int n) : par(n + 1), rnk(n + 1, 0), size(n + 1, 1), c(n) { for (int i = 1; i <= n; ++i) par[i] = i; } int find(int i) { return (par[i] == i ? i : (par[i] = find(par[i]))); } bool same(int i, int j) { return find(i) == find(j); } int get_size(int i) { return size[find(i)]; } int count() { return c; } int merge(int i, int j) { if ((i = find(i)) == (j = find(j))) return -1; else --c; if (rnk[i] > rnk[j]) swap(i, j); par[i] = j; size[j] += size[i]; if (rnk[i] == rnk[j]) rnk[j]++; return j; } }; const int N = 1e5 + 9; long long d[N]; int near[N], vis[N]; vector<pair<int, int>> g[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<array<long long, 3>> edges, ed; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); edges.push_back({u, v, w}); } for (int i = 1; i <= n; i++) { d[i] = inf; } int k; cin >> k; set<pair<long long, int>> se; for (int i = 1; i <= k; i++) { int u; cin >> u; se.insert({0, u}); near[u] = u; d[u] = 0; } while (!se.empty()) { auto x = *se.begin(); se.erase(se.begin()); int u = x.second; if (vis[u]) continue; vis[u] = 1; for (auto e : g[u]) { int v = e.first, w = e.second; if (d[u] + w < d[v]) { d[v] = d[u] + w; near[v] = near[u]; se.insert({d[v], v}); } } } for (auto e : edges) { if (near[e[0]] == near[e[1]]) continue; ed.push_back({d[e[0]] + d[e[1]] + e[2], near[e[0]], near[e[1]]}); } sort(ed.begin(), ed.end()); dsu D(n); long long ans = d[1]; for (auto e : ed) { int u = e[1], v = e[2]; long long w = e[0]; if (D.same(u, v)) continue; ans += w; D.merge(u, v); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; bool b[31]; struct trie { trie* nt[2]; int tot; trie() { nt[0] = nt[1] = NULL; tot = 0; } }; trie* root = new trie(); void insert(trie* cur, int idx) { if (idx == -1) return; else { int x = b[idx]; if (cur->nt[x] == NULL) cur->nt[x] = new trie(); cur->nt[x]->tot++; insert(cur->nt[x], idx - 1); } } void del(trie* cur, int idx) { if (idx == -1) return; else { int x = b[idx]; del(cur->nt[x], idx - 1); cur->nt[x]->tot--; if (cur->nt[x]->tot == 0) cur->nt[x] = NULL; } } int query(trie* cur, int idx) { if (idx == -1) return 0; else { int x = 1 - b[idx]; int add = 1 << idx; if (cur->nt[x] == NULL) x = 1 - x, add = 0; return query(cur->nt[x], idx - 1) + add; } } int main() { insert(root, 30); int q; scanf( %d , &q); while (q--) { char s[2]; int x; scanf( %s%d , s, &x); for (int i = 0; i <= 30; i++) { b[i] = x % 2; x /= 2; } if (s[0] == + ) insert(root, 30); else if (s[0] == - ) del(root, 30); else printf( %d n , query(root, 30)); } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 300000 + 100; int n, m, sz, l, r; int head[maxn], Next[maxn], to[maxn], in[maxn], out[maxn], deg[maxn], q[maxn]; void init() { sz = 0; memset(deg, 0, sizeof(deg)); memset(head, -1, sizeof(head)); } void add_edge(int a, int b) { ++sz; to[sz] = b; Next[sz] = head[a]; head[a] = sz; } struct Edge { int from, to; } edges[maxn]; int main() { init(); scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf( %d%d , &a, &b); edges[i].from = a; edges[i].to = b; add_edge(a, b); deg[b]++; } l = r = 0; for (int i = 1; i <= n; i++) { if (!deg[i]) { q[r++] = i; } } while (l < r) { int u = q[l++]; if (l == r) { out[u] = n - r; } else if (l + 1 == r) { int v = q[l]; int flag = 1; for (int i = head[v]; i != -1; i = Next[i]) { int vv = to[i]; if (deg[vv] == 1) { flag = 0; break; } } if (!flag) out[u] = -n; else out[u] = n - r; } else out[u] = -n; for (int i = head[u]; i != -1; i = Next[i]) { int v = to[i]; deg[v]--; if (!deg[v]) q[r++] = v; } } init(); for (int i = 1; i <= m; i++) { add_edge(edges[i].to, edges[i].from); deg[edges[i].from]++; } l = r = 0; for (int i = 1; i <= n; i++) { if (!deg[i]) { q[r++] = i; } } while (l < r) { int u = q[l++]; if (l == r) { in[u] = n - r; } else if (l + 1 == r) { int v = q[l]; int flag = 1; for (int i = head[v]; i != -1; i = Next[i]) { int vv = to[i]; if (deg[vv] == 1) { flag = 0; break; } } if (!flag) in[u] = -n; else in[u] = n - r; } else in[u] = -n; for (int i = head[u]; i != -1; i = Next[i]) { int vv = to[i]; deg[vv]--; if (!deg[vv]) q[r++] = vv; } } int ans = 0; for (int i = 1; i <= n; i++) { if (in[i] + out[i] >= n - 2) { ans++; } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n == 1 || n == 2 || n == 3) { cout << NO << endl; return 0; } cout << YES << endl; if (n % 2 == 0) { printf( 1 * 2 = 2 n ); printf( 2 * 3 = 6 n ); printf( 6 * 4 = 24 n ); } else { printf( 3 - 1 = 2 n ); printf( 5 - 2 = 3 n ); printf( 2 * 3 = 6 n ); printf( 6 * 4 = 24 n ); } if (n % 2 == 0) { for (int i = n; i >= 6; i -= 2) { printf( %d - %d = 1 n , i, i - 1); printf( 24 * 1 = 24 n ); } } else { for (int i = n; i >= 7; i -= 2) { printf( %d - %d = 1 n , i, i - 1); printf( 24 * 1 = 24 n ); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int r[105][105], d[105][105], vis[105][105]; int w, h, n; int bfs(int x, int y) { queue<pair<int, int> > q; q.push({x, y}); vis[x][y] = 1; int area = 0; while (!q.empty()) { int cx = q.front().first, cy = q.front().second; q.pop(); ++area; if (!d[cx][cy] && cx > 1 && !vis[cx - 1][cy]) q.push({cx - 1, cy}), vis[cx - 1][cy] = 1; if (!r[cx][cy] && cy < w && !vis[cx][cy + 1]) q.push({cx, cy + 1}), vis[cx][cy + 1] = 1; } return area; } int main() { cin >> w >> h >> n; int x1, x2, y1, y2; for (int i = 1; i <= n; ++i) { cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) for (int j = y1 + 1; j <= y2; ++j) r[j][x1] = 1; else for (int j = x1 + 1; j <= x2; ++j) d[y1 + 1][j] = 1; } vector<int> ans; for (int i = h; i >= 1; --i) for (int j = 1; j <= w; ++j) if (!vis[i][j]) ans.push_back(bfs(i, j)); sort(ans.begin(), ans.end()); for (auto& x : ans) cout << x << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; struct data { int points; string name; data(int p, string n) { points = p; name = n; } data() { points = -1; name = ; } }; inline bool cmp(data d1, data d2) { return (d1.points > d2.points); } int main() { int n, m, r, points; cin >> n >> m; string name; vector<vector<data>> reg(m); for (auto i(0); i < n; ++i) { cin >> name >> r >> points; r--; if (reg[r].size() < 3) { reg[r].push_back(data(points, name)); sort(reg[r].begin(), reg[r].end(), cmp); } else { reg[r].push_back(data(points, name)); sort(reg[r].begin(), reg[r].end(), cmp); reg[r].resize(3); } } for (auto i(0); i < m; ++i) { if (reg[i].size() == 2) { cout << reg[i][0].name << << reg[i][1].name << endl; ; } else if (reg[i][1].points == reg[i][2].points) { cout << ? << endl; } else { cout << reg[i][0].name << << reg[i][1].name << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, M = 1e6 + 5; long long T, l[N], dp[N], fen[M][2]; vector<int> adj[N]; int n, x[N], t[N]; void add(int p, int x) { for (long long y = 1LL * x * p; p < M; p += p & -p) fen[p][0] += x, fen[p][1] += y; } long long get(int p, int id) { long long ans = 0; for (; p; p -= p & -p) ans += fen[p][id]; return ans; } void dfs(int u) { add(t[u], x[u]); int L = 0, R = M; while (L + 1 < R) { int mid = L + R >> 1; (T < 2 * l[u] + get(mid, 1) ? R : L) = mid; } long long mx[2] = {}; for (int v : adj[u]) { l[v] += l[u], dfs(v); dp[v] > mx[1] ? mx[0] = mx[1], mx[1] = dp[v] : mx[0] = max(mx[0], dp[v]); } dp[u] = max(get(L, 0) + (R < M && T > 2 * l[u]) * (T - 2 * l[u] - get(L, 1)) / R, mx[!u]); add(t[u], -x[u]); } int main() { cin >> n >> T; for (int u = 0; u < n; u++) cin >> x[u]; for (int u = 0; u < n; u++) cin >> t[u]; for (int u = 1, v; u < n; u++) { cin >> v >> l[u]; adj[--v].push_back(u); } dfs(0), cout << dp[0]; } |
#include <bits/stdc++.h> using namespace std; long long f[(1 << 20) + 10], num[21], sum[21][21]; int main() { int n, m = 20; scanf( %d , &n); for (int i = 1, x; i <= n; i++) { scanf( %d , &x); x--, num[x]++; for (int j = 0; j < 20; j++) { sum[j][x] += num[j]; } } memset(f, 0x3f, sizeof f); f[0] = 0; for (int i = 1; i < 1 << m; i++) { for (int j = 0; j < m; j++) { if (i >> j & 1) { long long cur = 0; for (int k = 0; k < m; k++) { if ((i >> k & 1) && k != j) { cur += sum[k][j]; } } f[i] = min(f[i], f[i ^ (1 << j)] + cur); } } } printf( %lld n , f[(1 << m) - 1]); return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[7]; long long T, X, Y, ans; long long ABS(long long x) { return (x > 0) ? x : -x; } int main() { scanf( %lld , &T); while (T--) { scanf( %lld%lld , &X, &Y); for (int i = 1; i <= 6; i++) scanf( %lld , &a[i]); a[1] = min(a[1], a[6] + a[2]); a[6] = min(a[6], a[5] + a[1]); for (int i = 2; i <= 5; i++) { a[i] = min(a[i], a[i - 1] + a[i + 1]); } if (X <= Y && X >= 0) { X = ABS(X); Y = ABS(Y); ans = a[1] * X + a[2] * (Y - X); } else if (X < 0 && Y >= 0) { X = ABS(X); Y = ABS(Y); ans = a[2] * Y + a[3] * X; } else if (X < 0 && -X >= -Y && Y < 0) { X = ABS(X); Y = ABS(Y); ans = a[4] * Y + a[3] * (X - Y); } else if (X <= 0 && Y < 0 && -X < -Y) { X = ABS(X); Y = ABS(Y); ans = a[4] * X + a[5] * (Y - X); } else if (X > 0 && Y <= 0) { X = ABS(X); Y = ABS(Y); ans = a[6] * X + a[5] * Y; } else { X = ABS(X); Y = ABS(Y); ans = a[1] * Y + a[6] * (X - Y); } printf( %lld n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; bool check(int s) { return (int)sqrt(s) * (int)sqrt(s) == s; } int main() { int i, j, a, b, c, d, s1, s2, s3, s4; scanf( %d%d , &n, &m); for (a = 1; a <= 100; a++) for (b = 1; b <= 100; b++) for (c = 1; c <= 100; c++) for (d = 1; d <= 100; d++) { s1 = (m - 1) * a * a + b * b; s2 = (n - 1) * a * a + c * c; s3 = (n - 1) * b * b + d * d; s4 = (m - 1) * c * c + d * d; if (check(s1) && check(s2) && check(s3) && check(s4)) goto hhh; } hhh: for (i = 1; i < n; i++) { for (j = 1; j < m; j++) printf( %d , a); printf( %d n , b); } for (i = 1; i < m; i++) printf( %d , c); printf( %d n , d); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<float> coordinates; for (int i = 0; i < (int)(n); ++i) { float a, b; cin >> a >> b; coordinates.push_back(a); coordinates.push_back(b); } float sum = 0; std::cout << fixed; std::cout << setprecision(9); for (int i = 0; i < coordinates.size() - 2; i += 2) { sum += sqrt(pow(abs(coordinates[i] - coordinates[i + 2]), 2) + pow(abs(coordinates[i + 1] - coordinates[i + 3]), 2)) / 50; } cout << sum * k << n ; } |
#include <bits/stdc++.h> using namespace std; vector<int> v[300005], one[300005]; map<pair<int, int>, int> mp; int size[300005], ji[300005], xx[300005], yy[300005]; char ans[300005]; void dfs1(int p, int fa) { size[p] = 1; ji[p]++; for (int i = 0; i < v[p].size(); i++) { int to = v[p][i]; if (to == fa || ji[to]) continue; dfs1(to, p); size[p] += size[to]; } for (int i = 0; i < one[p].size(); i++) { int to = one[p][i]; if (to == fa) continue; mp[pair<int, int>(p, to)] = 1; if (ji[to]) continue; dfs1(to, p); size[p] += size[to]; } } void dfs2(int p, int fa) { size[p] = 1; ji[p]++; for (int i = 0; i < v[p].size(); i++) { int to = v[p][i]; if (to == fa || ji[to]) continue; dfs2(to, p); size[p] += size[to]; } for (int i = 0; i < one[p].size(); i++) { int to = one[p][i]; if (to == fa) continue; mp[pair<int, int>(to, p)] = 1; } } int main() { int n, m, s, tot = 0; scanf( %d%d%d , &n, &m, &s); for (int i = 0; i < m; i++) { int f, x, y; scanf( %d%d%d , &f, &x, &y); if (f == 1) v[x].push_back(y); else { one[x].push_back(y); one[y].push_back(x); xx[tot] = x; yy[tot++] = y; } } dfs1(s, 0); printf( %d n , size[s]); for (int i = 0; i < tot; i++) { if (mp[pair<int, int>(xx[i], yy[i])]) printf( + ); else printf( - ); } printf( n ); memset(ji, 0, sizeof(ji)); mp.clear(); dfs2(s, 0); printf( %d n , size[s]); for (int i = 0; i < tot; i++) { if (mp[pair<int, int>(xx[i], yy[i])]) printf( + ); else printf( - ); } printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<string> notes, names(55); for (int i = 0; i <= n - k; i++) { string m; cin >> m; notes.push_back(m); } for (int i = 0; i < 26; i++) { names[i] = (i + A ); } if (n >= 26) { for (int i = 26; i <= n; i++) { names[i] = names[i - 26] + a ; } } for (int i = 0; i <= n - k; i++) { if (notes[i] == NO ) names[i + k - 1] = names[i]; } for (int i = 0; i < n; i++) { cout << names[i] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); double d, h, v, e; cin >> d >> h >> v >> e; double ans = (3.14159265358979323846 * pow(d, 2) * h) / (4 * v - 3.14159265358979323846 * pow(d, 2) * e); if (ans < 0) cout << NO n ; else { cout << YES n ; cout << setprecision(10) << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n; bool taken[1001]; int a[1001], b[1001], ans[1001]; vector<int> problem; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; if (b[i] == a[i]) ans[i] = b[i], taken[a[i]] = true; else problem.push_back(i); } if (problem.size() == 1) { for (int i = 1; i <= n; ++i) { if (!taken[i]) { ans[problem[0]] = i; break; } } } else { vector<int> possible[2]; for (int i = 1; i <= n; ++i) { if (!taken[i]) { if (a[problem[0]] == i || b[problem[0]] == i) possible[0].push_back(i); if (a[problem[1]] == i || b[problem[1]] == i) possible[1].push_back(i); } } if (possible[0].size() == possible[1].size()) { if (possible[0].size() == 1) { ans[problem[0]] = possible[0][0]; ans[problem[1]] = possible[1][0]; } else { ans[problem[0]] = possible[0][0]; ans[problem[1]] = possible[1][1]; } } else { if (possible[0].size() == 1) { ans[problem[0]] = possible[0][0]; ans[problem[1]] = (possible[0][0] == possible[1][0] ? possible[1][1] : possible[1][0]); } else { ans[problem[0]] = (possible[1][0] == possible[0][0] ? possible[0][1] : possible[0][0]); ans[problem[1]] = possible[1][0]; } } } for (int i = 0; i < n; ++i) { cout << ans[i] << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); multiset<int> s; s.insert(0); int q; cin >> q; while (q--) { char c; int x; cin >> c >> x; if (c == + ) { s.insert(x); } else if (c == - ) { s.erase(s.find(x)); } else { int cur = 0; for (int i = 30; i >= 0; i--) { if (((1 << i) & x) == 0) { cur |= (1 << i); auto it = s.lower_bound(cur); if (it == s.end() || (cur >> i) != (*it >> i)) cur ^= (1 << i); } else { auto it = s.lower_bound(cur); if (it == s.end() || (cur >> i) != (*it >> i)) cur |= (1 << i); } } cout << (x ^ cur) << n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n; long long mmax = 0; long long sum = 0; long long num; cin >> n; for (int i = 1; i <= n; i++) { cin >> num; sum += num; mmax = max(mmax, num); } cout << 2 * mmax - sum + 1 << endl; } |
#include <bits/stdc++.h> template <int MOD> struct Integral { int v_ = 0; template <typename T> Integral(T v) : v_(norm(v)) { static_assert(std::is_integral<T>::value, input should be an integral. ); } Integral() = default; ~Integral() = default; template <typename T> T norm(T v) const { if (v >= MOD) v -= MOD; if (v < 0) v += MOD; if (v >= MOD || v < 0) v = (v % MOD + MOD) % MOD; return v; } int val() const { return v_; } Integral operator+(const Integral& rhs) const { return Integral(val() + rhs.val()); } Integral operator-(const Integral& rhs) const { return Integral(val() - rhs.val()); } Integral operator*(const Integral& rhs) const { return Integral(val() * 1LL * rhs.val()); } Integral operator/(const Integral& rhs) const { return *this * rhs.inv(); } Integral& operator+=(const Integral& rhs) { return *this = *this + rhs; } Integral& operator-=(const Integral& rhs) { return *this = *this - rhs; } Integral& operator*=(const Integral& rhs) { return *this = *this * rhs; } Integral& operator/=(const Integral& rhs) { return *this = *this / rhs; } bool operator==(const Integral& rhs) const { return val() == rhs.val(); } bool operator!=(const Integral& rhs) const { return !(*this == rhs); } const Integral operator-() const { return Integral(-val()); } const Integral operator++() { v_ = norm(v_ + 1); return *this; } const Integral operator++(int) { Integral ret = *this; ++(*this); return ret; } const Integral operator--() { v_ = norm(v_ - 1); return *this; } const Integral operator--(int) { Integral ret = *this; --(*this); return ret; } Integral power(long long b) const { long long ret = 1 % MOD, a = v_; for (; b; b >>= 1, a = a * a % MOD) if (b & 1) ret = ret * a % MOD; return ret; } Integral inv() const { return power(MOD - 2); } }; template <int MOD> std::string to_string(const Integral<MOD>& v) { return std::string( Integral{v= ) + std::to_string(v.val()) + } ; } template <int MOD, bool kAllowBruteForce = false> struct Binomial { std::vector<Integral<MOD>> factor, inv_factor; explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) { factor[0] = 1; for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i; inv_factor[n] = factor[n].inv(); for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i; } ~Binomial() = default; template <typename T> Integral<MOD> operator()(T a, T b) const { if (a < b || b < 0) return 0; if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b]; if constexpr (!kAllowBruteForce) { throw std::out_of_range( Binomial ); } else { b = std::min(b, a - b); Integral<MOD> ret = 1; for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i; return ret; } } }; template <int MOD> struct PowerTable { std::vector<Integral<MOD>> arr; PowerTable(int n, const Integral<MOD>& g) : arr(n + 1) { arr[0] = 1; arr[1] = g; for (int i = 2; i < arr.size(); ++i) arr[i] = arr[i - 1] * arr[1]; } template <typename T> Integral<MOD> power(T index) const { if (0 <= index && index < arr.size()) return arr[index]; return arr[1].power(index); } }; const int MOD = 1e9 + 7; using Mint = Integral<MOD>; using Binom = Binomial<MOD>; Binom binom(200); const int kN = 50 + 5; const int kM = 2500 + 5; Mint f[kN][kN][kM]; Mint g[kN][kN][kN][2]; Mint h[kN][kM]; Mint q[kN][kM]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::istream& reader = std::cin; int n, T; reader >> n >> T; std::vector<std::vector<int>> ts(3); for (int i = 0; i < n; ++i) { int t, g; reader >> t >> g; ts[g - 1].emplace_back(t); } int m[3] = {}; for (int i = 0; i < 3; ++i) m[i] = ts[i].size(); f[0][0][0] = 1; for (int t : ts[0]) { for (int i = m[0] - 1; i >= 0; --i) for (int k = T - t; k >= 0; --k) f[i + 1][0][k + t] += f[i][0][k]; } for (int t : ts[1]) { for (int j = m[1] - 1; j >= 0; --j) for (int i = m[0]; i >= 0; --i) for (int k = T - t; k >= 0; --k) f[i][j + 1][k + t] += f[i][j][k]; } g[0][0][0][0] = 1; for (int i = 0; i <= m[0]; ++i) { for (int j = 0; j <= m[1]; ++j) { for (int k = 0; k <= i + j; ++k) { for (int r = 0; r < 2; ++r) if (g[i][j][k][r].val()) { Mint val = g[i][j][k][r].val(); g[i + 1][j][k + 1][0] += val; g[i][j + 1][k + 1][1] += val; if (k > 0) { if (r == 0) g[i][j + 1][k][1] += val; if (r == 1) g[i + 1][j][k][0] += val; } } } } } for (int i = 0; i <= m[0]; ++i) { for (int j = 0; j <= m[1]; ++j) { for (int k = 0; k <= i + j; ++k) { for (int w = 0; w < 2; ++w) if (g[i][j][k][w].val()) { for (int r = 0; r <= T; ++r) if (f[i][j][r].val()) { q[k][r] += f[i][j][r] * g[i][j][k][w] * binom.factor[i] * binom.factor[j]; } } } } } h[0][0] = 1; for (int t : ts[2]) { for (int j = m[2] - 1; j >= 0; --j) for (int k = T - t; k >= 0; --k) h[j + 1][k + t] += h[j][k]; } Mint result = 0; for (int k = 0; k <= m[0] + m[1]; ++k) { for (int t = 0; t <= T; ++t) if (q[k][t].val()) { 1145141919810; for (int c = std::max(0, k - 1); c <= k + 1 && c <= m[2]; ++c) { if (!h[c][T - t].val()) continue; Mint coef = c == k ? 2 : 1; result += coef * q[k][t] * h[c][T - t] * binom.factor[c]; 1145141919810; } } } printf( %d n , result.val()); } |
#include <bits/stdc++.h> using namespace std; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; int nn = n - (n % 2 == 1); int mm = m - (m % 2 == 1); long long maxx = (nn / 2) * (mm / 2) * 2; if (n != nn) { maxx += m / 2; if (m / 2 > k || (k - m / 2) % 2 == 1) { cout << NO << endl; continue; } } else { if (k % 2 == 1) { cout << NO << endl; continue; } } if (maxx < k) { cout << NO << endl; continue; } vector<vector<char>> l = {{ a , b , c , d }, { x , y , z , w }}; vector<char> let = { m , n }; int q = 0; vector<vector<char>> ans(n, vector<char>(m)); if (n != nn) { for (int i = 0; i < m; i += 2) { k--; ans[n - 1][i] = let[q]; ans[n - 1][i + 1] = let[q]; q = (q + 1) % 2; } } q = 0; if (m != mm) { for (int i = 0; i < n; i += 2) { ans[i][m - 1] = let[q]; ans[i + 1][m - 1] = let[q]; q = (q + 1) % 2; } } q = 0; for (int i = 0; i < nn; i += 2) { int f = (i % 4 == 0); q = 0; for (int j = 0; j < mm; j += 2) { if (k > 0) { ans[i][j] = l[f][q]; ans[i][j + 1] = l[f][q]; q = (q + 1) % 4; ans[i + 1][j] = l[f][q]; ans[i + 1][j + 1] = l[f][q]; q = (q + 1) % 4; k -= 2; } else { ans[i][j] = l[f][q]; ans[i + 1][j] = l[f][q]; q = (q + 1) % 4; ans[i][j + 1] = l[f][q]; ans[i + 1][j + 1] = l[f][q]; q = (q + 1) % 4; } } } cout << YES << endl; for (int i = int(0); i < int(n); i++) { for (int j = int(0); j < int(m); j++) cout << ans[i][j]; cout << endl; } } } |
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<vector<pair<long long, long long> > > g; void fun() { cin >> n >> m >> k; g.resize(n); map<pair<int, int>, int> index; for (long long i = 0; i < m; i++) { long long x, y, z; cin >> x >> y >> z; x--; y--; g[x].push_back({y, z}); g[y].push_back({x, z}); index[{x, y}] = i + 1; index[{y, x}] = i + 1; } vector<long long> dis(n, LLONG_MAX), par(n); dis[0] = 0; set<pair<long long, long long> > s; s.insert({0, 0}); while (!s.empty()) { pair<long long, long long> d = *s.begin(); s.erase(s.begin()); for (auto j : g[d.second]) { if (dis[j.first] > dis[d.second] + j.second) { s.erase({dis[j.first], j.first}); dis[j.first] = dis[d.second] + j.second; par[j.first] = d.second; s.insert({dis[j.first], j.first}); } } } vector<pair<long long, int> > dissort; for (int i = 0; i < n; i++) dissort.push_back({dis[i], i}); sort((dissort).begin(), (dissort).end()); cout << min(n - 1, k) << n ; k = min(n - 1, k); int j = 1; while (k--) { int x = dissort[j].second; cout << index[{x, par[x]}] << ; j++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(0)); ; int t = 1; while (t--) fun(); } |
#include <bits/stdc++.h> using namespace std; int main() { int n; long long m, k; scanf( %d%lld%lld , &n, &m, &k); long long a[n], asum[n + 1]; asum[0] = 0; for (int((i)) = (0); ((i)) < ((n)); ++((i))) { scanf( %lld , &a[i]); asum[i + 1] = asum[i] + a[i]; } long long dp[m]; for (int((i)) = (0); ((i)) < ((m)); ++((i))) dp[i] = 1e18; long long ans = 0; for (int((i)) = (0); ((i)) < ((n)); ++((i))) { dp[i % m] = min(dp[i % m] + k, asum[i] + k); for (int((j)) = (0); ((j)) < ((m)); ++((j))) ans = max(ans, asum[i + 1] - dp[j]); } printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; const int E9 = 1e9; const int E8 = 1e8; const int E7 = 1e7; const int E6 = 1e6; const int E5 = 1e5; const int E4 = 1e4; const int E3 = 1e3; const int N = 5e5 + 7; const int MOD = 1e9 + 7; const long long INF = 1e18; int n; bool dp[101][51][2][201]; string s; void calc(int com, int kol, int dir, int pos) { if (dp[com][kol][dir][pos]) { return; } dp[com][kol][dir][pos] = 1; if (com == (int)s.size()) { return; } for (int i = 0; i <= n - kol; i++) { if (i % 2 == 0) { if (s[com] == F ) { calc(com + 1, kol + i, dir, pos + (dir == 0 ? 1 : -1)); } else { calc(com + 1, kol + i, (dir ^ 1), pos); } } else { if (s[com] == T ) { calc(com + 1, kol + i, dir, pos + (dir == 0 ? 1 : -1)); } else { calc(com + 1, kol + i, (dir ^ 1), pos); } } } } int main() { cin >> s >> n; calc(0, 0, 0, 100); int sz = (int)s.size(); int mx = 0; for (int i = 0; i <= 200; i++) { if (dp[sz][n][0][i] || dp[sz][n][1][i]) { mx = max(mx, abs(i - 100)); } } cout << mx; } |
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long n, sum = 0; cin >> n; if (n % 2 == 0) { if ((n / 2) % 2 == 0) cout << 0 << endl; else cout << 1 << endl; } else { if ((n / 2) % 2 == 0) cout << 1 << endl; else cout << 0 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long inf = 1e9 + 7; long long mod = 998244353; using namespace std; vector<long long> g[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T, N, i, j, k, L, l, m, n; long long s; cin >> N >> s; for (i = 0; i < N - 1; i++) { long long u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } long double ans = 0.0; if (N == 2) ans = (long double)s / 2.0; else { long long cnt = 0; for (i = 0; i < N; i++) { if (g[i].size() == 1) cnt++; } ans = (long double)s / (long double)cnt; } printf( %.12Lf n , 2 * ans); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<int> debt(n + 1, 0); for (int i = 0; i < m; i++) { int poor, rich, amt; cin >> poor >> rich >> amt; debt[poor] -= amt; debt[rich] += amt; } int ans = 0; for (int &it : debt) { if (it < 0) { ans += -it; } } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 10, INF = 1e9; double arr[maxn]; int main() { int n; double mina = 0, maxa = INF; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) { mina = max(mina, arr[i] * 10 / (i + 1)); maxa = min(maxa, (arr[i] + 1) * (10) / (i + 1)); } int k1, k2; k1 = (mina) * (n + 1) / 10; k2 = ceil((maxa) * (n + 1) - 1) / 10; if (k1 != k2) cout << not unique << endl; else { cout << unique << endl; cout << k1 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; const int INF = 1e9; vector<int> p[maxn]; bool vis[maxn]; int nmultiple[maxn], N, tag[maxn], a[maxn]; int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } int main() { for (int i = 2; i < maxn; ++i) { if (!vis[i]) { for (int j = i; j < maxn; j += i) { vis[j] = 1; p[j].push_back(i); } } } scanf( %d , &N); for (int i = 1; i <= N; ++i) { scanf( %d , &a[i]); int x = 1; for (int j : p[a[i]]) x *= j; a[i] = x; tag[a[i]] = 1; } if (tag[1]) { printf( 1 n ); return 0; } int d = a[1]; for (int i = 1; i <= N; ++i) d = gcd(d, a[i]); if (d > 1) { printf( -1 n ); return 0; } for (int i = 1; i < maxn; ++i) { for (int j = i; j < maxn; j += i) { nmultiple[i] += tag[j]; } } int ans = INF; for (int i = 1; i <= N; ++i) { int v = a[i], sz = p[v].size(), up = (1 << sz) - 1; vector<int> DP(up + 1, INF), mp(up + 1, 0); vector<vector<int>> ways(up + 1, vector<int>(sz + 1)); for (int mask = 0; mask <= up; ++mask) { int x = 1; for (int j = 0; j < sz; ++j) { if (mask >> j & 1) { x *= p[v][j]; } } mp[mask] = x; ways[mask][0] = nmultiple[x]; } for (int j = 0; j < sz; ++j) { for (int mask = 0; mask <= up; ++mask) { int pmask = mask ^ (1 << j); ways[pmask][j + 1] = ways[pmask][j]; if (mask >> j & 1) { ways[pmask][j + 1] -= ways[mask][j]; } } } DP[up] = 1; for (int mask = up; mask >= 0; --mask) { for (int pick = 0; pick <= up; ++pick) { if (ways[pick][sz]) { DP[mask & pick] = min(DP[mask & pick], DP[mask] + 1); } } } ans = min(ans, DP[0]); } printf( %d n , ans); } |
#include <bits/stdc++.h> using namespace std; long long f[203][203][2], g[203][203][2], A[203], B[203], C[203], SA[203], SB[203], N, T, K; int main() { cin >> N >> T >> K; for (int i = 1; i <= N; ++i) cin >> A[i] >> B[i] >> C[i]; ++N; A[N] = C[N] = 1e15; memset(f, 0x3f, sizeof(f)); memset(g, 0x3f, sizeof(g)); for (int i = 1; i <= N; ++i) { SA[i] = SA[i - 1] + A[i]; SB[i] = SB[i - 1] + B[i]; } memset(f[0], 0, sizeof(f[0])); memset(g[0], 0, sizeof(g[0])); for (int i = 1; i <= N; ++i) for (int j = 0; j <= T; ++j) for (int k = 0; k <= 1; ++k) { if (k * A[i] + B[i] * j <= C[i] && f[i - 1][j][k] <= 1e15) { f[i][j][k] = f[i - 1][j][k]; long long vl = (k * SA[i - 1] + j * SB[i - 1] + K - 1) / K; if (vl * K <= k * SA[i] + j * SB[i]) g[i][j][k] = vl; } for (int l = 0; l < j; ++l) { long long stp = g[i][l][k]; if (stp > 1e15) continue; long long lft = SA[i] * k + SB[i] * l - stp * K, need = max((lft + (j - l) * B[i] - C[i] + K - 1) / K, 0ll); if (need * K > lft) continue; if (f[i - 1][j - l][0] > 1e15) continue; f[i][j][k] = min(f[i][j][k], stp + need + f[i - 1][j - l][0]); long long vl = ((j - l) * SB[i - 1] + K - 1) / K; if (vl * K > lft - need * K + vl * SB[i]) continue; g[i][j][k] = min(g[i][j][k], stp + need + vl); } } cout << f[N][T][1] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int A[100000]; void solve() { int n; cin >> n; int prv = -1; for (int i = 0; i < (n); i++) cin >> A[i]; int res = 1; for (int i = 0; i < (n); i++) { int x = A[i]; ; ; if (x == 0) { if (prv == 0) { cout << -1 n ; return; } } else { if (prv != 1) { res++; } else { res += 5; } } prv = x; } cout << res << n ; } int main() { ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); ; int t; cin >> t; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, i, lim, cnt = 0, other = 0; string str; cin >> n >> str; lim = str.size(); int petya = (n - 11) / 2; for (i = 0; i < lim; i++) { if (str[i] == 8 ) cnt++; else other++; if (cnt > petya) break; } if (cnt > petya && other <= petya) cout << YES << endl; else cout << NO << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a, b; cin >> a >> b; int ans = a; while (a >= b) { int t = a / b; ans += t; a = t + (a - b * t); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, a, b; cin >> n >> a >> b; string s; cin >> s; if (s[a - 1] == s[b - 1]) cout << 0 << n ; else { cout << 1 << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long t, a, b, x; cin >> t; while (t--) { cin >> a >> b; if (a == b) { cout << 0 << endl; continue; } if (a < b) { x = (b / 10) - (a / 10); } else { x = (a / 10) - (b / 10); } if (a + x * 10 < b || b + x * 10 < a) cout << x + 1 << endl; else cout << x << endl; } } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 1007; const int MAXS = 107; const int MAXM = 13; const int MAXL = 13; const int BASE = 4; const int MOD = 1e9 + 9; int toid[128]; int n, m, kn, ml; char s[MAXM][MAXL]; int nxt[MAXS][BASE], fail[MAXS], q[MAXS]; int ok[MAXS]; int tag[MAXS]; int dp[MAXN][MAXL][MAXS]; void init(); void input(); void work(); void trie_add(char s[]); void buildac(); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); toid[ A ] = 0; toid[ C ] = 1; toid[ G ] = 2; toid[ T ] = 3; } void input() { scanf( %d%d , &n, &m); for (int i = 0; i < m; ++i) scanf( %s , s[i]); } void work() { kn = 1; memset(nxt, -1, sizeof(nxt)); memset(tag, 0, sizeof(tag)); for (int i = 0; i < m; ++i) trie_add(s[i]), ml = (((ml) > (strlen(s[i]))) ? (ml) : (strlen(s[i]))); buildac(); ++ml; memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; int v, nj, nl; for (int i = 0; i < n; ++i) { for (int j = 0; j < ml; ++j) { for (int k = 0; k < kn; ++k) { if (dp[i][j][k]) { for (int nc = 0; nc < BASE; ++nc) { v = nxt[k][nc]; nj = j + 1; if (tag[v] || ok[v]) { if (tag[v]) nl = tag[v]; else nl = tag[ok[v]]; if (nl >= nj) nj = 0; } dp[i + 1][nj][v] += dp[i][j][k]; dp[i + 1][nj][v] %= MOD; } } } } } int sum = 0; for (int i = 0; i < kn; ++i) { sum += dp[n][0][i]; sum %= MOD; } printf( %d n , sum); } void trie_add(char s[]) { int len = strlen(s), now = 0, nc; for (int i = 0; i < len; ++i) { nc = toid[s[i]]; if (nxt[now][nc] ^ -1) now = nxt[now][nc]; else now = nxt[now][nc] = kn++; } tag[now] = len; } void buildac() { int l = 0, r = 0, now, v, f; q[r++] = 0; fail[0] = 0; while (l < r) { now = q[l++]; for (int i = 0; i < BASE; ++i) { if (nxt[now][i] ^ -1) { v = nxt[now][i]; q[r++] = v; f = fail[now]; while (f && nxt[f][i] == -1) f = fail[f]; fail[v] = (now && nxt[f][i] ^ -1) ? nxt[f][i] : 0; } else nxt[now][i] = (nxt[fail[now]][i] ^ -1) ? nxt[fail[now]][i] : 0; } f = fail[now]; while (f && !tag[f]) f = ok[f]; ok[now] = f; } } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.