solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 10; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; long long qp(long long base, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * base) % mod; base = (base * base) % mod; n >>= 1; } return res; } int n, q; int from[maxn], to[maxn], f[maxn][11][11][11][11]; int dis(int a, int b) { return abs(a - b); } int dfs(int i, int cur, int a, int b, int c) { if (f[i][cur][a][b][c] != -1) return f[i][cur][a][b][c]; int ret = inf; if (i > n) { if (a == 0 && b == 0 && c == 0) return 0; if (a) ret = min(ret, dfs(i, a, 0, b, c) + dis(cur, a) + 1); if (b) ret = min(ret, dfs(i, b, a, 0, c) + dis(cur, b) + 1); if (c) ret = min(ret, dfs(i, c, a, b, 0) + dis(cur, c) + 1); return f[i][cur][a][b][c] = ret; } if (a) ret = min(ret, dfs(i, a, 0, b, c) + dis(cur, a) + 1); if (b) ret = min(ret, dfs(i, b, a, 0, c) + dis(cur, b) + 1); if (c) ret = min(ret, dfs(i, c, a, b, 0) + dis(cur, c) + 1); if (a && b && c) { ret = min(ret, dfs(i + 1, to[i], a, b, c) + dis(cur, from[i]) + 1 + dis(from[i], to[i]) + 1); ret = min(ret, dfs(i + 1, a, to[i], b, c) + dis(cur, from[i]) + 1 + dis(from[i], a) + 1); ret = min(ret, dfs(i + 1, b, a, to[i], c) + dis(cur, from[i]) + 1 + dis(from[i], b) + 1); ret = min(ret, dfs(i + 1, c, a, b, to[i]) + dis(cur, from[i]) + 1 + dis(from[i], c) + 1); } else { if (!a) ret = min(ret, dfs(i + 1, from[i], to[i], b, c) + dis(cur, from[i]) + 1); else if (!b) ret = min(ret, dfs(i + 1, from[i], a, to[i], c) + dis(cur, from[i]) + 1); else if (!c) ret = min(ret, dfs(i + 1, from[i], a, b, to[i]) + dis(cur, from[i]) + 1); } return f[i][cur][a][b][c] = ret; } int main() { while (~scanf("%d", &n)) { memset(f, -1, sizeof(f)); for (int i = 1; i <= n; i++) scanf("%d%d", &from[i], &to[i]); int ans = dfs(1, 1, 0, 0, 0); printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int a[int(1e5 + 10)], t = 0, k; long long int fun(int v1, int v2, int p1, int p2, int it, int f, int i) { long long int t1 = 0; if (v1 <= it) { if (v2 > a[i - 1]) { if (p2 >= f) { if (p1 < a[i + k]) { t1 = (long long int)(min(v2, it) - max(a[i - 1] + 1, v1) + 1) * (long long int)(min(a[i + k] - 1, p2) - max(f, p1) + 1); } } } } return t1; } int main() { int prv = 1; a[++t] = 4; a[++t] = 7; for (int i = 2; i <= 9; ++i) { int f = t; for (int j = prv; j <= f; ++j) { a[++t] = a[j] * 10 + 4; a[++t] = a[j] * 10 + 7; } prv = f + 1; } a[t + 1] = 2e9; int p1, p2, v1, v2; cin >> p1 >> p2 >> v1 >> v2 >> k; long long int ans = 0; a[0] = 0; for (int i = 1; i <= t - k + 1; i++) { int it = a[i], f = a[i + k - 1]; long long int t1, t2; t1 = fun(p1, p2, v1, v2, it, f, i); t2 = fun(v1, v2, p1, p2, it, f, i); ans += t1 + t2; if (k == 1 && t1 && t2) ans--; } double fans = ans, g = p2 - p1 + 1; g *= v2 - v1 + 1; fans /= g; printf("%.10lf\n", fans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int ans = 0; vector<char> fans; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == s[i + 1]) { fans.push_back(s[i]); ans++; i++; if (i + 1 <= s.length() - 1) { if (s[i - 1] == 'R' && s[i + 1] == 'R') { fans.push_back('G'); } else if (s[i - 1] == 'G' && s[i + 1] == 'G') { fans.push_back('R'); } else if (s[i - 1] == 'B' && s[i + 1] == 'B') { fans.push_back('R'); } else if (s[i - 1] == 'R' && s[i + 1] == 'G') { fans.push_back('B'); } else if (s[i - 1] == 'R' && s[i + 1] == 'B') { fans.push_back('G'); } else if (s[i - 1] == 'G' && s[i + 1] == 'R') { fans.push_back('B'); } else if (s[i - 1] == 'G' && s[i + 1] == 'B') { fans.push_back('R'); } else if (s[i - 1] == 'B' && s[i + 1] == 'R') { fans.push_back('G'); } else if (s[i - 1] == 'B' && s[i + 1] == 'G') { fans.push_back('R'); } } else { if (s[i - 1] == 'R') { fans.push_back('B'); } else if (s[i - 1] == 'B') { fans.push_back('R'); } else { fans.push_back('R'); } } } else { fans.push_back(s[i]); } } cout << ans << "\n"; for (int i = 0; i < fans.size(); i++) { cout << fans[i]; } if (fans.size() != s.length()) { cout << s[s.length() - 1]; } }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int N = 100005; const double PI = 4 * atan(1); set<char> vowel = {'A', 'O', 'Y', 'E', 'U', 'I', 'a', 'o', 'y', 'e', 'u', 'i'}; int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } bool isprime(long long u) { for (long long i = 2; i <= (int)sqrt(u); i++) { if (u % i == 0) return 0; } return 1; } long long mod(long long to_mod) { to_mod %= MOD; while (to_mod < 0) to_mod += MOD; return to_mod % MOD; } long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long n, m, k, a, l, r, b, t, ans = 0, res = 0, x, y, z, dfsroot, xmax, xmin; vector<long long> vv, v; vector<pair<string, long long> > troll; double db; vector<pair<long long, long long> > vvv; priority_queue<pair<long long, long long> > pq; map<long long, vector<long long> > adj; char c; bool vis[105]; map<string, long long> points; map<long long, long long> maa; string ch, ch1, ch2; long long dist[105][105]; unordered_set<string> ss; void add(long long v1, long long v2) { v1--; v2--; adj[v1].push_back(v2); adj[v2].push_back(v1); } void dfs(long long src, long long a) { if (vis[src]) return; vis[src] = 1; if (v[src] == dfsroot) ans = a; if (vis[v[src]]) return; dfs(v[src], a + 1); } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> x; x--; v.push_back(x); } res = 1; for (long long i = 0; i < n; i++) { memset(vis, 0, sizeof(vis)); ans = 1e9; dfsroot = i; dist[i][i] = 0; dfs(i, 1); if (ans >= 1e9) { cout << -1; return 0; } if (ans % 2 == 0) res = lcm(res, ans / 2); else res = lcm(res, ans); } cout << res; return 0; }
1
#include<bits/stdc++.h> using namespace std; int main() { int n; while(cin>>n&&n!=-1){ int sum=3130,num[3]={125,140,160}; for(int i=0;i<3;i++){ n-=10; if(n>10&&i!=2)sum-=num[i]*10; else if(n>0)sum-=num[i]*n; } cout<<sum<<endl; } }
0
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int mn = 1e5 + 10; const long long inf = 0x3f3f3f3f3f3f3f3f; int bit[mn]; void upd(int a, int b) { for (a++; a < mn; a += a & -a) bit[a] += b; } int qu(int a) { int b = 0; for (a++; a; a -= a & -a) b += bit[a]; return b; } long long a[mn]; vector<int> g[mn]; int main() { cin.tie(0); cin.sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i], a[i] = abs(a[i]), g[a[i]].push_back(i); long long ans = 0; for (int i = 0; i < n; i++) upd(i, 1); for (int i = mn - 1; i >= 0; i--) { for (int j : g[i]) { upd(j, -1); } for (int j : g[i]) { int aa = qu(j - 1), bb = qu(mn - 2) - qu(j); ans += min(aa, bb); } } printf("%lld", ans); }
5
#include <bits/stdc++.h> using namespace std; void __print(long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(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) { long long f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } void sol() { long long n; cin >> n; vector<long long> arr(n); for (long long i = 0; i < arr.size(); i++) { cin >> arr[i]; } for (long long i = 0; i < arr.size(); i++) { long long b; cin >> b; arr[i] = arr[i] - b; } long long result = 0; sort(arr.begin(), arr.end()); long long l = 0, r = n - 1; while (l < r) { if (arr[l] + arr[r] > 0) { result += r - l; r--; } else { l++; } } cout << result << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); sol(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5; int n, arr[8000], ans, sum; vector<bool> b(8001); void solve() { cin >> n; for (long long int i = 0; i < n; i++) { cin >> arr[i]; b[i] = 0; } b[n] = 0; for (int i = 0; i < n - 1; i++) { sum = arr[i] + arr[i + 1]; if (sum <= n) b[sum] = 1; for (int j = i + 2; j < n; j++) { sum += arr[j]; if (sum > n) break; b[sum] = 1; } } ans = 0; for (long long int i = 0; i < n; i++) ans += (b[arr[i]]); cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(0); int T; cin >> T; ; while (T--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T> inline void checkmin(T &a, T b) { if (b < a) { a = b; } } template <typename T> inline void checkmax(T &a, T b) { if (b > a) { a = b; } } const int maxn = (int)1e5 + 10; const long long inf = (long long)1e18; struct Node { long long w; int id, pos, segpos; long long add; Node(int id, int pos, int segpos, long long w, long long add) : id(id), pos(pos), segpos(segpos), w(w), add(add) {} Node() : id(0), pos(0), segpos(0), w(inf), add(0) {} bool operator<(const Node &o) const { return w == o.w ? pos < o.pos : w < o.w; } }; inline ostream &operator<<(ostream &out, const Node &a) { out << a.id << ' ' << a.pos << ' ' << a.w; return out; } vector<Node> segs[maxn]; vector<int> partitions[maxn]; int tail[maxn]; int size[maxn], dep[maxn], fa[maxn], id[maxn], pos[maxn], idCnt; int enter[maxn], leave[maxn], cnt; long long totadd[maxn << 1]; vector<int> adj[maxn]; vector<Node> girls[maxn]; inline void getSummary(Node &a, Node &l, Node &r) { if (l < r) { a = Node(l.id, l.pos, l.segpos, l.w, a.add); } else { a = Node(r.id, r.pos, r.segpos, r.w, a.add); } } inline void push(Node &a, Node &l, Node &r) { if (a.add) { l.w += a.add; l.add += a.add; r.w += a.add; r.add += a.add; a.add = 0; } } void build(vector<Node> &seg, vector<int> &partition, int i, int l, int r) { if (l < r) { int mid = (l + r) / 2; build(seg, partition, i * 2, l, mid); build(seg, partition, i * 2 + 1, mid + 1, r); getSummary(seg[i], seg[i * 2], seg[i * 2 + 1]); } else { int p = partition[l]; if (girls[p].size()) { seg[i] = girls[p].back(); seg[i].segpos = l; } else { seg[i] = Node(); } } } void remove(vector<Node> &seg, int i, int l, int r, int x) { if (l == r) { int p = seg[i].pos; long long k = seg[i].add; girls[p].pop_back(); if (girls[p].size()) { seg[i] = girls[p].back(); seg[i].w += k; seg[i].add = k; seg[i].segpos = l; } else { seg[i] = Node(); } } else { int mid = (l + r) / 2; push(seg[i], seg[i * 2], seg[i * 2 + 1]); if (x <= mid) { remove(seg, i * 2, l, mid, x); } else { remove(seg, i * 2 + 1, mid + 1, r, x); } getSummary(seg[i], seg[i * 2], seg[i * 2 + 1]); } } void add(vector<Node> &seg, int i, int l, int r, int x, int y, int k) { if (x > r || y < l) { return; } if (x <= l && y >= r) { seg[i].w += k; seg[i].add += k; } else { int mid = (l + r) / 2; push(seg[i], seg[i * 2], seg[i * 2 + 1]); add(seg, i * 2, l, mid, x, y, k); add(seg, i * 2 + 1, mid + 1, r, x, y, k); getSummary(seg[i], seg[i * 2], seg[i * 2 + 1]); } } bool flag = false; Node getBest(vector<Node> &seg, int i, int l, int r, int x, int y) { if (x > r || y < l) { return Node(); } if (x <= l && y >= r) { return seg[i]; } else { int mid = (l + r) / 2; push(seg[i], seg[i * 2], seg[i * 2 + 1]); return min(getBest(seg, i * 2, l, mid, x, y), getBest(seg, i * 2 + 1, mid + 1, r, x, y)); } } void dfs(int u, int v) { fa[v] = u; dep[v] = dep[u] + 1; enter[v] = ++cnt; size[v] = 1; pair<int, int> prefer(0, 0); for (int w : adj[v]) { if (w != u) { dfs(v, w); size[v] += size[w]; prefer = max(prefer, make_pair(size[w], w)); } } int child = prefer.second; if (child != 0 && size[child] * 2 > size[v]) { id[v] = id[child]; } else { id[v] = idCnt++; } pos[v] = partitions[id[v]].size(); partitions[id[v]].emplace_back(v); leave[v] = ++cnt; } inline void add(int pos, int v) { while (pos <= cnt) { totadd[pos] += v; pos += pos & -pos; } } inline long long getsum(int pos) { long long res = 0; while (pos > 0) { res += totadd[pos]; pos -= pos & -pos; } return res; } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < n - 1; ++i) { int u, v; scanf("%d%d", &u, &v); adj[u].emplace_back(v); adj[v].emplace_back(u); } for (int i = 1; i <= m; ++i) { int c; scanf("%d", &c); girls[c].emplace_back(i, c, 0, i, 0); } for (int i = 1; i <= n; ++i) { sort((girls[i]).begin(), (girls[i]).end()); reverse((girls[i]).begin(), (girls[i]).end()); } dfs(0, 1); for (int i = 0; i < idCnt; ++i) { segs[i].resize(partitions[i].size() * 4); build(segs[i], partitions[i], 1, 0, partitions[i].size() - 1); } memset(totadd, 0, sizeof totadd); for (int i = 0; i < q; ++i) { int type, v, u, k; scanf("%d", &type); if (type == 1) { scanf("%d%d%d", &v, &u, &k); int _v = v; int _u = u; vector<int> res; for (int j = 0; j < k; ++j) { Node best, cur; v = _v; u = _u; while (true) { if (dep[partitions[id[v]].back()] < dep[partitions[id[u]].back()]) { swap(v, u); } if (id[v] == id[u]) { if (pos[v] > pos[u]) { swap(v, u); } cur = getBest(segs[id[v]], 1, 0, partitions[id[v]].size() - 1, pos[v], pos[u]); cur.w += getsum(enter[fa[partitions[id[v]].back()]]); best = min(best, cur); break; } else { cur = getBest(segs[id[v]], 1, 0, partitions[id[v]].size() - 1, pos[v], partitions[id[v]].size() - 1); cur.w += getsum(enter[fa[partitions[id[v]].back()]]); best = min(best, cur); } v = fa[partitions[id[v]].back()]; } if (best.w >= inf) { break; } res.emplace_back(best.id); remove(segs[id[best.pos]], 1, 0, partitions[id[best.pos]].size() - 1, best.segpos); } printf("%d", res.size()); for (int w : res) { printf(" %d", w); } puts(""); } else { scanf("%d%d", &v, &k); add(enter[v], k); add(leave[v] + 1, -k); add(segs[id[v]], 1, 0, partitions[id[v]].size() - 1, 0, pos[v], k); } } }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int N, k; cin >> N >> k; vector<int> eu(k), ev(k), ew(k), all; for (int i = 0; i < k; ++i) cin >> eu[i] >> ev[i] >> ew[i], --ew[i], all.push_back(eu[i]), all.push_back(ev[i] + N); sort(all.begin(), all.end()); all.resize(unique(all.begin(), all.end()) - all.begin()); vector<int> par(all.size()); for (int i = 0; i < par.size(); ++i) par[i] = i; function<int(int)> F = [&](int u) { return par[u] == u ? u : par[u] = F(par[u]); }; for (int i = 0; i < k; ++i) { eu[i] = lower_bound(all.begin(), all.end(), eu[i]) - all.begin(); ev[i] = lower_bound(all.begin(), all.end(), ev[i] + N) - all.begin(); par[F(eu[i])] = F(ev[i]); } static constexpr int mod = 1e9 + 7; vector<int> dp(1, 1), rem(par.size()); auto upd = [&](int &x, const int &y) { ((x += y) >= mod) ? x -= mod : x; }; auto pow_mod = [&](int x, int n) { int y = 1; while (n) { if (n & 1) y = (long long)y * x % mod; n >>= 1; if (n) x = (long long)x * x % mod; } return y; }; vector<int> ifac(k + 1, 1); for (int i = 1; i <= k; ++i) ifac[i] = (long long)ifac[i - 1] * pow_mod(i, mod - 2) % mod; for (int rt = 0; rt < par.size(); ++rt) if (F(rt) == rt) { auto conv = [&](vector<int> dp, vector<int> pd) { vector<int> f(dp.size() + pd.size() - 1, 0); for (int i = 0; i < dp.size(); ++i) for (int j = 0; j < pd.size(); ++j) upd(f[i + j], (long long)dp[i] * pd[j] % mod); return f; }; vector<int> v, e, pd; for (int u = 0; u < par.size(); ++u) if (F(u) == rt) rem[u] = v.size(), v.push_back(u); for (int i = 0; i < k; ++i) if (F(eu[i]) == rt) e.push_back(i); int n = 0; while (all[v[n]] <= N) ++n; if (v.size() / 2 <= e.size() - (v.size() - 1)) { auto work = [&]() { int S = 1 << n; vector<int> dp(S, 0); vector<vector<int> > g(v.size()); dp[0] = 1; for (int i : e) g[rem[ev[i]]].push_back(i); for (int u = n; u < v.size(); ++u) { auto pd = dp; for (int t, s = 0; s < S; ++s) if (t = dp[s]) { for (int i : g[u]) { int v = rem[eu[i]], r = 1 << v; if ((s & r) == 0) upd(pd[r | s], (long long)t * ew[i] % mod); } } dp = pd; } for (int s = 0; s < S; ++s) { int i = __builtin_popcount(s); if (pd.size() <= i) pd.resize(i + 1, 0); upd(pd[i], dp[s]); } return; }; work(); } else { auto work = [&]() { vector<int> par(v.size()); for (int i = 0; i < v.size(); ++i) par[i] = i; function<int(int)> F = [&](int u) { return par[u] == u ? u : par[u] = F(par[u]); }; vector<vector<int> > g(v.size()); vector<int> e2; for (int i : e) { int x = rem[eu[i]], y = rem[ev[i]]; if (F(x) != F(y)) { par[F(x)] = F(y); g[x].push_back(i); g[y].push_back(i); } else e2.push_back(i); } int S = 1 << e2.size(); for (int s = 0; s < S; ++s) { vector<bool> vis(v.size(), false); bool flag = true; int coef = 1, cnt = 0; for (int _ = 0; _ < e2.size(); ++_) if ((s >> _) & 1) { int i = e2[_]; int x = rem[eu[i]], y = rem[ev[i]]; if (vis[x] || vis[y]) { flag = false; break; } ++cnt; vis[x] = vis[y] = 1; coef = (long long)coef * ew[i] % mod; } if (flag) { vector<array<vector<int>, 2> > dp(v.size()); function<void(int, int)> dfs = [&](int u, int p) { dp[u][0] = vector<int>{1}; array<vector<int>, 2> pd; for (int i : g[u]) { int v = rem[eu[i]] + rem[ev[i]] - u; if (v != p) { dfs(v, u); vector<int> t(max(dp[v][0].size(), dp[v][1].size()), 0); for (int i = 0; i < dp[v][0].size(); ++i) upd(t[i], dp[v][0][i]); for (int i = 0; i < dp[v][1].size(); ++i) upd(t[i], dp[v][1][i]); pd[0] = conv(dp[u][0], t); pd[1] = conv(dp[u][1], t); if (!vis[u] && !vis[v]) { auto t = conv(dp[u][0], dp[v][0]); for (int x, j = 0; j < t.size(); ++j) if (x = t[j]) { if (pd[1].size() <= j + 1) pd[1].resize(j + 2, 0); upd(pd[1][j + 1], (long long)x * ew[i] % mod); } } dp[u] = pd; } } return; }; dfs(0, -1); for (int _ = 0; _ < 2; ++_) for (int x, i = 0; i < dp[0][_].size(); ++i) if (x = dp[0][_][i]) { int j = i + cnt; if (pd.size() <= j) pd.resize(j + 1, 0); upd(pd[j], (long long)coef * x % mod); } } } }; work(); } dp = conv(dp, pd); } vector<int> fac(N + 1, 1); for (int i = 1; i <= N; ++i) fac[i] = (long long)fac[i - 1] * i % mod; int ans = 0; for (int i = 0; i < dp.size(); ++i) upd(ans, (long long)dp[i] * fac[N - i] % mod); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n, m, ans = 0; cin >> n >> m; vector<long long> a(n + 1); vector<long long> c(n + 1); priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { cin >> c[i]; q.push(make_pair(c[i], i)); } for (i = 1; i <= m; i++) { long long t, d; cin >> t >> d; if (a[t] >= d) { a[t] -= d; cout << c[t] * d << '\n'; } else { ans = a[t] * c[t]; d -= a[t]; a[t] = 0; bool pass = false; while (!q.empty()) { while ((!q.empty()) && (a[q.top().second] == 0)) { q.pop(); } if (q.empty()) { break; } pair<long long, long long> p = q.top(); if (a[p.second] >= d) { a[p.second] -= d; ans += d * p.first; pass = true; cout << ans << '\n'; break; } else { ans += a[p.second] * p.first; d -= a[p.second]; a[p.second] = 0; q.pop(); } } if (!pass) { cout << 0 << '\n'; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = (1LL) << 53; long long a[200010], dp[200010][2]; int n; long long dfs(int x, int flag) { if (x <= 0 || x > n) return 0; if (dp[x][flag] != -INF) return dp[x][flag]; dp[x][flag] = INF; int next = flag ? x - a[x] : x + a[x]; return dp[x][flag] = dfs(next, !flag) + a[x]; } int main() { while (~scanf("%d", &n)) { for (int i = 2; i <= n; i++) scanf("%I64d", &a[i]); for (int i = 0; i <= n; i++) { dp[i][0] = dp[i][1] = -INF; } for (int i = 1; i < n; i++) { long long ans = dfs(i + 1, 1); if (ans > INF) printf("-1\n"); else printf("%I64d\n", ans + i); } } return 0; }
2
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <utility> #include <cstring> #include <iomanip> #include <numeric> #include <cmath> #include <queue> #include <map> using namespace std; typedef long long ll; const int INF = 1<<30; const int MOD = 1e9 + 7; bool check(string s) { if(s.size() >= 2 && s[0] == 'X') return true; return false; } void get_str(string &s) { string tmp = ""; for(int i = s.size(); i <= 130; i++) tmp += '0'; s = tmp + s; } int to_int(char ch) { return ch - '0'; } int main() { cin.tie(0); ios::sync_with_stdio(false); string s; while(cin >> s) { int p = s.find('+'), q = s.find('='); string a = s.substr(0, p), b = s.substr(p + 1, q - p - 1), c = s.substr(q + 1); int ans = -1; bool flag = false; if(check(a) || check(b) || check(c)) flag = true; for(char k = (flag ? '1' : '0'); k <= '9'; k++) { string ta, tb, tc; ta = tb = tc = ""; for(int i = 0; i < a.size(); i++) ta += (a[i] == 'X' ? k : a[i]); for(int i = 0; i < b.size(); i++) tb += (b[i] == 'X' ? k : b[i]); for(int i = 0; i < c.size(); i++) tc += (c[i] == 'X' ? k : c[i]); get_str(ta); get_str(tb); get_str(tc); int inc = 0; bool ok = true; for(int i = ta.size() - 1; i >= 0; i--) { if(ok == false) break; int left = to_int(ta[i]) + to_int(tb[i]) + inc; int right = to_int(tc[i]); if(left >= 10) { left %= 10; inc = 1; } else inc = 0; if(left != right) ok = false; } if(ok) ans = k - '0'; } if(ans == -1) cout << "NA" << endl; else cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a[3]; int main() { ios::sync_with_stdio(0); cin >> a[0] >> a[1] >> a[2]; long long k = min(a[0], min(a[1], a[2])); a[0] -= k; a[1] -= k; a[2] -= k; if (!a[0] && !a[1] && !a[2]) { cout << "0\n"; return 0; } if (a[0] && !a[1] && !a[2]) { cout << 2 * a[0] - 2 << "\n"; return 0; } if (!a[0] && a[1] && !a[2]) { cout << 2 * a[1] - 2 << "\n"; return 0; } if (!a[0] && !a[1] && a[2]) { cout << 2 * a[2] - 2 << "\n"; return 0; } if (!a[0]) { if (a[1] == a[2]) cout << a[1] - 1 << "\n"; else if (a[1] > a[2]) cout << 2 * a[1] - a[2] - 2 << "\n"; else cout << 2 * a[2] - a[1] - 2 << "\n"; return 0; } if (!a[1]) { if (a[0] == a[2]) cout << a[0] - 1 << "\n"; else if (a[0] > a[2]) cout << 2 * a[0] - a[2] - 2 << "\n"; else cout << 2 * a[2] - a[0] - 2 << "\n"; return 0; } if (!a[2]) { if (a[0] == a[1]) cout << a[0] - 1 << "\n"; else if (a[0] > a[1]) cout << 2 * a[0] - a[1] - 2 << "\n"; else cout << 2 * a[1] - a[0] - 2 << "\n"; return 0; } return 0; }
3
#include <bits/stdc++.h> using namespace std; double DP[2005][2005]; int main() { int i, j, k, l, m, n, t; double ans, p; scanf("%d %lf %d", &n, &p, &t); DP[1][1] = p; DP[1][0] = 1 - p; for (i = 2; i <= t; i++) { DP[i][0] = DP[i - 1][0] * (1 - p); for (j = 1; j <= i && j <= n; j++) { if (j < n) DP[i][j] += DP[i - 1][j] * (1 - p); else DP[i][j] += DP[i - 1][j]; DP[i][j] += DP[i - 1][j - 1] * p; } } for (i = 1, ans = 0.0; i <= n; i++) { ans += i * DP[t][i]; } printf("%lf\n", ans); return 0; }
4
#include <stdio.h> int main(){ char a[8]; scanf("%s", a); (a[2] == a[3] && a[4] == a[5])? puts("Yes") : puts("No"); return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N = 100005; ll n, l, t, a[N], b[N], c[3]; vector<ll> ans; int main() { scanf("%lld%lld%lld",&n,&l,&t); for(ll i=0;i<n;i++) { scanf("%lld%lld",&a[i],&b[i]); if(b[i] == 1) { if(l - a[i] <= t % l) c[1]++; ans.push_back((a[i] + t) % l); } else { if(a[i] < t % l) c[2]++; ans.push_back(((a[i] - t) % l + l) % l); } c[b[i]] += t / l; } sort(ans.begin(), ans.end()); for(ll i=0;i<n;i++) { printf("%lld\n",ans[((c[1] - c[2]) % n + n + i) % n]); } }
0
#include <bits/stdc++.h> using namespace std; vector<int> arr[100010]; bool isprime[100010]; bool geh[100010]; set<int> seet; set<int>::iterator it; int bta3men[100010]; void sevie() { isprime[1] = 1; isprime[0] = 1; for (int i = 2; i <= 100000; ++i) { if (!isprime[i]) { arr[i].push_back(i); for (int j = i * 2; j <= 100000; j += i) { isprime[j] = 1; arr[j].push_back(i); } } } } int main() { sevie(); int n, m, x; char c; scanf("%d", &n); scanf("%d", &m); while (m--) { cin >> c; scanf("%d", &x); if (c == '+') { if (geh[x]) printf("Already on\n"); else { bool flag = false; int temp = seet.size(); for (int i = 0; i < arr[x].size(); ++i) { it = seet.find(arr[x][i]); if (it != seet.end()) { printf("Conflict with "); printf("%d\n", bta3men[*it]); flag = true; break; } } if (!flag) { for (int i = 0; i < arr[x].size(); ++i) { seet.insert(arr[x][i]); bta3men[arr[x][i]] = x; } geh[x] = 1; printf("Success\n"); } } } else { if (!geh[x]) printf("Already off\n"); else { for (int i = 0; i < arr[x].size(); ++i) { it = seet.find(arr[x][i]); bta3men[*it] = 0; seet.erase(it); } geh[x] = 0; printf("Success\n"); } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, maxs = 28, maxl = 8e5 + 100; int n; char s[maxl + 5]; int ch[maxl + 1][maxs + 1], d[maxl + 1], name[maxl + 1], ps[maxl + 1], tt = 0; vector<int> N[maxl + 1], P[maxl + 1]; int mat[maxn + 1]; void newn(int &x) { x = ++tt; d[x] = name[x] = ps[x] = 0; } void inser(char *s, int *a, int mark, int t) { int l = strlen(s); int x = 1; for (int i = 0; i <= l - 1; i++) { if (ch[x][s[i] - 'a' + 1] == 0) newn(ch[x][s[i] - 'a' + 1]); x = ch[x][s[i] - 'a' + 1]; } a[x]++; if (t == 1) { N[x].push_back(mark); } else P[x].push_back(mark); } void read() { scanf("%d\n", &n); tt = 1; for (int i = 1; i <= n; i++) { scanf("%s\n", s); inser(s, name, i, 1); } for (int i = 1; i <= n; i++) { scanf("%s\n", s); inser(s, ps, i, 2); } } int ans = 0; void dfs(int x, int d) { for (int i = 1; i <= 26; i++) { if (ch[x][i]) dfs(ch[x][i], d + 1); } int a = 0, b = 0; for (int i = 1; i <= 26; i++) { if (ch[x][i] == 0) continue; a += name[ch[x][i]]; b += ps[ch[x][i]]; if (name[ch[x][i]]) { int k = ch[x][i]; for (int i = N[k].size() - name[k]; i < N[k].size(); i++) { N[x].push_back(N[k][i]); } } if (ps[ch[x][i]]) { int k = ch[x][i]; for (int i = P[k].size() - ps[k]; i < P[k].size(); i++) { P[x].push_back(P[k][i]); } } } a += name[x]; b += ps[x]; if (a >= b) { ans += d * b; name[x] = a - b; ps[x] = 0; for (int i = 0; i <= b - 1; i++) { mat[N[x][i]] = P[x][i]; } } else { ans += d * a; name[x] = 0; ps[x] = b - a; for (int i = 0; i <= a - 1; i++) { mat[N[x][i]] = P[x][i]; } } } void work() { dfs(1, 0); printf("%d\n", ans); for (int i = 1; i <= n; i++) { printf("%d %d\n", i, mat[i]); } } int main() { read(); work(); return 0; }
1
#include<iostream> using namespace std; int main(){ int n,q; cin >> n >> q; string s; cin >> s; int sum[n]; for(int i=0;i<n;i++){ if(i==0) sum[i]=0; else if(s[i-1]=='A' && s[i]=='C') sum[i]=sum[i-1]+1; else sum[i]=sum[i-1]; } for(int i=0;i<q;i++){ int l,r; cin >> l >> r; l--,r--; cout << sum[r]-sum[l] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ long long a,b,c,s,ss; while(1){ cin >> a >> b >> c; if(a<0 or b<0 or c<0){ return 0; } s=7200-a*3600-b*60-c; ss=(7200-a*3600-b*60-c)*3; cout << 0 << s/3600 << ":"; s%=3600; cout << ((s<600)?"0":"") << s/60 << ":"; s%=60; cout << ((s<10)?"0":"") << s << endl; cout << 0 << ss/3600 << ":"; ss%=3600; cout << ((ss<600)?"0":"") << ss/60 << ":"; ss%=60; cout << ((ss<10)?"0":"") << ss << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int V = 2100; int x[V], n, K; int main() { while (~scanf("%d%d", &n, &K)) { int ct = 0; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) ct++; if (K >= ct) { puts("no solution"); continue; } x[0] = 0; for (int i = 1; i < n; i++) x[i] = x[i - 1] + n - i; for (int i = 0; i < n; i++) printf("0 %d\n", x[i]); } }
1
#include <bits/stdc++.h> using namespace std; int main() { vector<int> left, right; int n, m, s, l, r; string kata, q; cin >> n >> m >> s; cin >> kata >> q; size_t pos = kata.find(q, 0); while (pos != string::npos) { left.push_back(pos + 1); right.push_back(pos + m); pos = kata.find(q, pos + 1); } vector<int>::iterator low, up; for (int i = 0; i < s; i++) { cin >> l >> r; int lef = lower_bound(left.begin(), left.end(), l) - left.begin(); int righ = lower_bound(right.begin(), right.end(), r + 1) - right.begin(); if (righ - lef >= 0) cout << righ - lef << endl; else cout << 0 << endl; } return 0; }
2
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } struct SegTree { int n; struct Node { int64_t sum; int64_t add; int l, r; Node operator * (const Node &rhs) const { return {sum + rhs.sum, 0, l, rhs.r}; } }; std::vector<Node> nodes; SegTree (int n_) { for (n = 1; n < n_; n <<= 1); nodes.resize(n << 1); for (int i = 0; i < n; i++) nodes[i + n] = {0, 0, i, i + 1}; for (int i = n; --i; ) nodes[i] = nodes[i << 1] * nodes[i << 1 | 1]; } void flush(int node) { if (nodes[node].add) { nodes[node].sum += nodes[node].add * (nodes[node].r - nodes[node].l); if (node < n) { nodes[node << 1].add += nodes[node].add; nodes[node << 1 | 1].add += nodes[node].add; } nodes[node].add = 0; } } void add(int l, int r, int val, int node = 1) { flush(node); if (l >= nodes[node].r || r <= nodes[node].l) return; if (l <= nodes[node].l && r >= nodes[node].r) { nodes[node].add += val; flush(node); } else { add(l, r, val, node << 1); add(l, r, val, node << 1 | 1); nodes[node] = nodes[node << 1] * nodes[node << 1 | 1]; } } int64_t sum(int l, int r, int node = 1) { flush(node); if (l >= nodes[node].r || r <= nodes[node].l) return 0; if (l <= nodes[node].l && r >= nodes[node].r) return nodes[node].sum; int64_t res = sum(l, r, node << 1) + sum(l, r, node << 1 | 1); nodes[node] = nodes[node << 1] * nodes[node << 1 | 1]; return res; } }; std::vector<std::vector<int> > hens; std::vector<int> size; std::vector<int> parent; void dfs1(int i) { if (parent[i] != -1) hens[i].erase(std::find(hens[i].begin(), hens[i].end(), parent[i])); for (auto &j : hens[i]) { parent[j] = i; dfs1(j); size[i] += size[j]; if (size[j] > size[hens[i][0]]) std::swap(j, hens[i][0]); } } std::vector<int> in, out, top; int cnt = 0; void dfs2(int i) { in[i] = cnt++; for (auto j : hens[i]) { top[j] = j == hens[i][0] ? top[i] : j; dfs2(j); } out[i] = cnt; } int lca(int i, int j) { while (1) { if (in[i] > in[j]) std::swap(i, j); if (top[i] == top[j]) return i; j = parent[top[j]]; } } std::vector<std::pair<int, int> > descend(int high, int low) { std::vector<std::pair<int, int> > res; while (low != high) { int next = top[low]; if (in[next] < in[high]) next = high; res.push_back({in[next] + (next == high), in[low] + 1}); if (next == high) break; low = parent[next]; } return res; } int main() { int n = ri(), q = ri(); hens.resize(n); for (int i = 1; i < n; i++) { int a = ri(); int b = ri(); hens[a].push_back(b); hens[b].push_back(a); } parent.resize(n, -1); size.resize(n, 1); dfs1(0); in.resize(n, -1); out.resize(n, -1); top.resize(n); dfs2(0); SegTree tree(n); for (int i = 0; i < q; i++) { if (!ri()) { int v = ri(), x = ri(); int lc = lca(v, x); int64_t res = 0; for (auto i : descend(lc, v)) res += tree.sum(i.first, i.second); for (auto i : descend(lc, x)) res += tree.sum(i.first, i.second); std::cout << res << std::endl; } else { int v = ri(), x = ri(); tree.add(in[v] + 1, out[v], x); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<pair<long long, char> > s, t, p; vector<int> z; int n, m; long long ans = 0ll; char myget() { char res = '!'; while (!(res >= 'a' && res <= 'z')) res = getc(stdin); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); scanf("%d%d", &m, &n); s.resize(n); t.resize(m); int last = -1; for (int i = 0; i < m; i++) { int cnt; scanf("%d", &cnt); char c = myget(); if (last != -1 && t[last].second == c) { t[last].first += cnt; } else { last++; t[last] = make_pair(cnt, c); } } m = last + 1; last = -1; for (int i = 0; i < n; i++) { int cnt; scanf("%d", &cnt); char c = myget(); if (last != -1 && s[last].second == c) { s[last].first += cnt; } else { last++; s[last] = make_pair(cnt, c); } } n = last + 1; if (n == 1) { for (int i = 0; i < m; i++) { if (s[0].second == t[i].second) { ans += max(0ll, t[i].first - s[0].first + 1ll); } } cout << ans << endl; return 0; } if (n == 2) { for (int i = 1; i < m; i++) { if (s[0].second == t[i - 1].second && s[1].second == t[i].second && s[0].first <= t[i - 1].first && s[1].first <= t[i].first) ans++; } cout << ans << endl; return 0; } p.resize(n - 2 + 1 + m); z.resize(n - 2 + 1 + m); int cp = 0; for (int i = 1; i < n - 1; i++) { p[cp] = s[i]; cp++; } pair<long long, char> dvr = make_pair(0, '#'); p[cp] = dvr; cp++; for (int i = 0; i < m; i++) { p[cp] = t[i]; cp++; } for (int i = 1, l = 0, r = 0; i < n - 2 + 1 + m; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n - 2 + 1 + m && p[z[i]] == p[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } for (int i = n - 2 + 1; i < n - 2 + 1 + m; i++) { if (z[i] < n - 2) continue; if (i == 0 || p[i - 1].second != s[0].second || p[i - 1].first < s[0].first) continue; if (i + n - 2 >= n - 2 + 1 + m || p[i + n - 2].second != s[n - 1].second || p[i + n - 2].first < s[n - 1].first) continue; ans++; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long ncr(long long n, long long r) { if (n < r) return 0; else if (n == r) return 1; r = min(r, n - r); long long ans = 1; for (int i = 1; i <= r; i++) { ans *= (long long)(n - i + 1); ans /= i; ans %= 1000000007; } return ans % 1000000007; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; map<long long, vector<int>, greater<long long> > mp; vector<long long> v(n), pre(n, 0); for (long long int i = 0; i < n; i++) { cin >> v[i]; if (v[i] > 0) pre[i] = v[i]; if (i) pre[i] += pre[i - 1]; } for (long long int i = 0; i < n; i++) { if (!mp.count(v[i])) mp[v[i]].push_back(i); } for (long long i = n - 1; i >= 0; i--) { if (mp.count(v[i]) && mp[v[i]].size() == 1 && i > mp[v[i]].front()) mp[v[i]].push_back(i); } for (long long int i = 0; i < n; i++) { if (mp.count(v[i]) && mp[v[i]].size() == 1) mp.erase(v[i]); } auto it = mp.begin(); long long S = pre[((it)->second).back()] - ((((it)->second).front()) == 0 ? 0 : pre[((mp.begin())->second).front() - 1]); if (it->first < 0) S += 2 * (it->first); int r = ((it)->second).back(), l = ((it)->second).front(); it++; for (; it != mp.end(); it++) { long long S1 = pre[((it)->second).back()] - ((((it)->second).front()) == 0 ? 0 : pre[((it)->second).front() - 1]); if (it->first < 0) S1 += 2 * (it->first); if (S < S1) { S = S1; r = ((it)->second).back(); l = ((it)->second).front(); } } cout << S << " "; pre.clear(); for (long long int i = 0; i < l; i++) pre.push_back(i + 1); for (long long i = l + 1; i < r; i++) { if (v[i] < 0) pre.push_back(i + 1); } for (long long i = r + 1; i < n; i++) pre.push_back(i + 1); cout << pre.size() << "\n"; for (auto x : pre) cout << x << " "; cout << "\n"; }
1
#include<bits/stdc++.h> using namespace std; unordered_set<int>a[200003]; bool vis[200003]; int dfs(int k){ vis[k]=true; int sz=1; for(auto c:a[k]){ if(!vis[c]){ vis[c]=true; sz+=dfs(c);} } return sz; } int main(){ int n,m; cin>>n>>m; while(m--){ int x,y; cin>>x>>y; a[x].insert(y); a[y].insert(x); } int sz,ans=0; for(int i=1;i<=n;i++){ if(!vis[i]){ sz=dfs(i); ans=max(ans,sz); } } cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; int n, k; pair<long long, long long> a[100007]; int r[100007]; int c[100007]; int used[100007]; long long ans = -1; bool cmp1(int x, int y) { return (a[x].first < a[y].first); } bool cmp2(int x, int y) { return (a[x].second < a[y].second); } void input() { cin >> n >> k; int i; long long xx1, yy1, xx2, yy2; for (i = 1; i <= n; i++) { cin >> xx1 >> yy1 >> xx2 >> yy2; xx1 *= 2; yy1 *= 2; xx2 *= 2; yy2 *= 2; a[i].first = (xx1 + xx2) / 2; a[i].second = (yy1 + yy2) / 2; r[i] = c[i] = i; } } void solve() { sort(r + 1, r + n + 1, cmp1); sort(c + 1, c + n + 1, cmp2); int i, j; int mask; int lim = (2 << (2 * k)); vector<int> v; for (mask = 0; mask < lim; mask++) { int u = mask; v.clear(); for (j = 0; j < k; j++) { int ost = (u % 4); u /= 4; if (ost == 0) { for (i = 1; i <= n; i++) { if (used[r[i]] == 1) { continue; } used[r[i]] = 1; v.push_back(r[i]); break; } } else if (ost == 1) { for (i = n; i >= 1; i--) { if (used[r[i]] == 1) { continue; } used[r[i]] = 1; v.push_back(r[i]); break; } } else if (ost == 2) { for (i = 1; i <= n; i++) { if (used[c[i]] == 1) { continue; } used[c[i]] = 1; v.push_back(c[i]); break; } } else { for (i = n; i >= 1; i--) { if (used[c[i]] == 1) { continue; } used[c[i]] = 1; v.push_back(c[i]); break; } } } long long xx1, yy1, xx2, yy2; xx1 = xx2 = yy1 = yy2 = 0; for (i = 1; i <= n; i++) { if (used[r[i]] == 0) { xx1 = a[r[i]].first; break; } } for (i = n; i >= 1; i--) { if (used[r[i]] == 0) { xx2 = a[r[i]].first; break; } } for (i = 1; i <= n; i++) { if (used[c[i]] == 0) { yy1 = a[c[i]].second; break; } } for (i = n; i >= 1; i--) { if (used[c[i]] == 0) { yy2 = a[c[i]].second; break; } } if (xx1 == xx2) { if (k != n) { xx2++; } } if (yy1 == yy2) { if (k != n) { yy2++; } } long long s = (xx2 - xx1 + 1) / 2; s *= ((yy2 - yy1 + 1) / 2); if (ans == -1 || ans > s) { ans = s; } for (i = 0; i < k; i++) { used[v[i]] = 0; } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } int main() { long long n, m, k; cin >> n >> m >> k; if ((2 * m * n) % k != 0) return cout << "NO", 0; long long a, n1, m1, k1; a = gcd(n, k); n1 = n / a; k1 = k / a; a = gcd(m, k1); m1 = m / a; k1 = k / a; if (n1 * m1 != (n * m * 2) / k) if (n1 * 2 <= n) n1 *= 2; else m1 *= 2; cout << "YES" << endl; cout << 0 << " " << 0 << endl; cout << n1 << " " << 0 << endl; cout << 0 << " " << m1; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 1; long long d[maxn << 1], inf; int n, Q, s; vector<int> to[maxn << 1], w[maxn << 1]; void addEdge(int x, int y, int z) { to[x].push_back(y); w[x].push_back(z); return; } struct node { int i; long long dis; }; bool operator<(node a, node b) { return a.dis > b.dis; } priority_queue<node> q; void buildtree(int k1, int l, int r) { if (l == r) { addEdge(k1 + n, l, 0); addEdge(l, k1 + maxn + n, 0); return; } int mid = l + r >> 1, lc = k1 << 1, rc = lc | 1; buildtree(lc, l, mid); buildtree(rc, mid + 1, r); addEdge(k1 + n, lc + n, 0); addEdge(lc + maxn + n, k1 + maxn + n, 0); addEdge(k1 + n, rc + n, 0); addEdge(rc + maxn + n, k1 + maxn + n, 0); return; } int op, x, y, z, v; void add(int k1, int l, int r) { if (x <= l && r <= y) { if (op == 2) addEdge(v, k1 + n, z); else addEdge(k1 + n + maxn, v, z); return; } if (l > y || r < x) return; int mid = l + r >> 1, lc = k1 << 1, rc = lc | 1; add(lc, l, mid); add(rc, mid + 1, r); return; } void dijkstra() { for (int i = 1; i <= maxn * 2; i++) d[i] = inf; q.push((node){s, 0}); d[s] = 0; while (!q.empty()) { node t = q.top(); x = t.i; q.pop(); if (d[t.i] != t.dis) continue; int siz = to[t.i].size(); for (int i = 0; i < siz; i++) { int v = to[x][i]; if (d[v] > d[x] + w[x][i]) { d[v] = d[x] + w[x][i]; q.push((node){v, d[v]}); } } } } int main() { inf = 1, inf <<= 60; scanf("%d%d%d", &n, &Q, &s); buildtree(1, 1, n); for (int i = 1; i <= Q; i++) { scanf("%d", &op); if (op == 1) { scanf("%d%d%d", &x, &y, &z); addEdge(x, y, z); } else { scanf("%d%d%d%d", &v, &x, &y, &z); add(1, 1, n); } } dijkstra(); for (int i = 1; i <= n; i++) { if (d[i] == inf) printf("-1 "); else printf("%I64d ", d[i]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, x, y, z, sub[111111], rt, cnt, a[111111], pos[111111], wh, Rt[111111], sz, tree[11111111], ls[11111111], rs[11111111]; vector<pair<int, int> > g[111111]; int l, r, mid, pp; long long ans; set<int> s[111111]; set<pair<int, int> > ss; set<pair<int, int> >::iterator it; void dfs(int i, int fa) { sub[i] = 1; for (int j = 0; j < g[i].size(); j++) { int to = g[i][j].first, val = g[i][j].second; if (to == fa) continue; dfs(to, i); sub[i] += sub[to]; ans += (2ll * val * min(sub[to], n - sub[to])); } } pair<int, int> findgrav(int i, int fa) { pair<int, int> res = make_pair(1e9, 1e9); int mx = 0; for (int j = 0; j < g[i].size(); j++) { int to = g[i][j].first; if (to == fa) continue; res = min(res, findgrav(to, i)); mx = max(mx, sub[to]); } mx = max(mx, n - sub[i]); res = min(res, make_pair(mx, i)); return res; } void dft(int i, int fa) { s[cnt].insert(i); a[cnt] += 2; pos[i] = cnt; for (int j = 0; j < g[i].size(); j++) { int to = g[i][j].first; if (to == fa) continue; dft(to, i); } } void updt(int x) { ss.erase(ss.find(make_pair(a[x], x))); a[x]--; ss.insert(make_pair(a[x], x)); } void upd(int x, int l, int r, int &nw, int y) { if (!nw) { nw = ++sz; } tree[nw] += y; if (l == r) return; int mid = (l + r) >> 1; if (x > mid) upd(x, mid + 1, r, rs[nw], y); else upd(x, l, mid, ls[nw], y); } int query(int beg, int en, int l, int r, int node) { if (l > en || r < beg) return 0; if (!node) return 0; if (l >= beg && r <= en) return tree[node]; int mid = (l + r) >> 1; return query(beg, en, l, mid, ls[node]) + query(beg, en, mid + 1, r, rs[node]); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d%d", &x, &y, &z); g[x].push_back(make_pair(y, z)); g[y].push_back(make_pair(x, z)); } dfs(1, 0); rt = findgrav(1, 0).second; for (int i = 0; i < g[rt].size(); i++) { int to = g[rt][i].first; ++cnt; dft(to, rt); } ++cnt; s[cnt].insert(rt); a[cnt] = 0; pos[rt] = cnt; printf("%lld\n", ans); for (int i = 1; i <= cnt; i++) ss.insert(make_pair(a[i], i)); for (int i = 1; i <= n; i++) { upd(i, 1, n, Rt[0], 1); upd(i, 1, n, Rt[pos[i]], 1); } for (int i = 1; i <= n; i++) { wh = pos[i]; updt(wh); it = ss.end(); it--; if ((it->first) == n - i + 1) { int ff = it->second; updt(ff); printf("%d ", *s[ff].begin()); upd(*s[ff].begin(), 1, n, Rt[0], -1); upd(*s[ff].begin(), 1, n, Rt[ff], -1); s[ff].erase(s[ff].begin()); } else { l = 1; r = n; while (l <= r) { mid = (l + r) >> 1; if (query(1, mid, 1, n, Rt[0]) > query(1, mid, 1, n, Rt[wh])) r = mid - 1; else l = mid + 1; } if (i == rt) { l = min(l, i); } printf("%d ", l); updt(pos[l]); s[pos[l]].erase(l); upd(l, 1, n, Rt[0], -1); upd(l, 1, n, Rt[pos[l]], -1); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct info { int pr, af; } arr[2000 + 5]; int main(void) { int n; scanf("%d", &n); bool ch = false; for (int i = 1; i <= n; i++) { scanf("%d %d", &arr[i].pr, &arr[i].af); if (arr[i].pr != arr[i].af) { ch = true; } } if (ch) { printf("rated\n"); } else { for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (arr[i].af < arr[j].af) { ch = true; } } } if (ch) { printf("unrated\n"); } else { printf("maybe\n"); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 1; vector<int> p[N]; int main() { for (int i = 2; i < N; ++i) { if (p[i].empty()) { for (int j = i + i; j < N; j += i) { p[j].emplace_back(i); } } } int n; scanf("%d", &n); int ans = 1 << 20; for (int i = n - p[n].back() + 1; i <= n; ++i) { if (p[i].empty()) { continue; } ans = min(ans, i - p[i].back() + 1); } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int p4[] = {-1, 1, 0, 0}; long long int q4[] = {0, 0, 1, -1}; long long int p8[] = {-1, 1, 0, 0, -1, 1, -1, 1}; long long int q8[] = {0, 0, 1, -1, 1, 1, -1, -1}; long long int gcd(long long int a, long long int b) { if (b > a) gcd(b, a); return ((b == 0) ? a : gcd(b, a % b)); } long long int LCM(long long int a, long long int b) { long long int p = (a * b) / (gcd(a, b)); return p; } long long int BigMod(long long int a, long long int b, long long int m) { if (!b) return 1; long long int ans = BigMod(a, b / 2, m); ans = (ans * ans) % m; if (b & 1) ans = (ans * a) % m; return ans; } long long int Mod_inv(long long int a) { return BigMod(a, 1000000007LL - 2, 1000000007LL); } long long int arra[78502]; bool arre[1000002]; long long int pl = 0; void prime_seive() { memset(arre, true, sizeof(arre)); arre[0] = arre[1] = false; for (int i = 4; i <= 1000000; i += 2) arre[i] = false; for (int i = 3; i * i <= 1000000; i += 2) if (arre[i]) for (int j = i * i; j <= 1000000; j += i + i) arre[j] = false; arra[pl++] = 2; for (int i = 3; i <= 1000000; i += 2) if (arre[i]) arra[pl++] = i; } long long int prime(long long int a) { prime_seive(); if (a == 1) return 1; else if (a == 2) return 0; else if (a % 2 == 0) return 1; else for (int i = 1; i < pl && (arra[i] * arra[i] <= a); i++) if (a % arra[i] == 0) return 1; return 0; } long long int a[1000005], b[1000005]; vector<long long int> v, v1, v2; map<long long int, long long int> mp; int main() { long long int i, j, k, l, m, n, c, d, p, q; long long int cs = 0, tc, t = 1, flg, f, sm; string s, s1, s2; while (cin >> n) { c = 0; for (i = 0; i < n; i++) { cin >> a[i] >> b[i]; if (a[i] == b[i]) c++; } if (c == n) { f = 0; for (i = 1; i < n; i++) { if (b[i - 1] < b[i]) f = 1; } if (f) cout << "unrated" << endl; else cout << "maybe" << endl; } else { cout << "rated" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } int main() { string s; cin >> s; long long f = 0, k = 0; long long n = s.size(); for (int i = 0; i < n; i++) { if (s[i] == '4') f++; if (s[i] == '7') k++; } if (k > f) cout << 7; else if (k <= f && f != 0) cout << 4; else cout << -1; }
2
#include <bits/stdc++.h> using namespace std; int n, m; set<int> myset; int main() { scanf("%d", &n); m = 2 * n * n; myset.insert(1); set<int>::iterator it; for (int i = 2; myset.size() < n; i++) { it = myset.begin(); while (it != myset.end() && (*it) * i <= m) { myset.insert((*it) * i); it++; } } it = myset.end(); while (it != myset.begin() && n) { it--; printf("%d ", *it); n--; } printf("\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int A,B,C;cin>>A>>B>>C; string ans="NO"; for(int i=1; i<B; i++){ if((i*A)%B==C)ans="YES"; } cout<<ans<<endl; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define PLL pair<ll,ll> #define mod 1000000007 #define mx 200005 #define FILE freopen("input.txt","r",stdin) #define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); ll a[mx]; int main() { fastio ll t,n,p,x,id; cin>>t; while(t--){ cin>>n>>p; vector<PLL>v; for(ll i = 1;i<=n;i++){ cin>>x; a[i] = x; v.push_back({x, i}); } sort(v.begin(), v.end()); map<ll,ll>mp; ll need = n-1; ll ans = 0; for(ll i = 0;i<v.size() && v[i].first<=p;i++){ if(mp[v[i].second]) continue; mp[v[i].second] = 1; id = v[i].second + 1; while(a[id]%v[i].first==0 && !mp[id] && id<=n){ need--; ans+=v[i].first; //cout<<ans<<endl; mp[id] = 1; id++; } if(id-1!=v[i].second) mp[id-1] = 0; id = v[i].second - 1; while(a[id]%v[i].first==0 && !mp[id] && id>=1){ need--; ans+=v[i].first; //cout<<ans<<endl; mp[id] = 1; id--; } if(id+1!=v[i].second) mp[id+1] = 0; } ans+=(need*p); cout<<ans<<endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) int main() { int N, C; while(cin >> N >> C && N && C) { int panel[N]; memset(panel, 0, sizeof panel); rep(i, N) rep(j, 16) { int x; cin >> x; if(x) { panel[i] += 1<<j; } } int jubi[C]; memset(jubi, 0, sizeof jubi); rep(i, C) rep(j, 16) { int x; cin >> x; if(x) { jubi[i] += 1<<j; } } int dp[2][1<<16]; memset(dp, -1, sizeof dp); dp[0][0] = 0; int const mask = (1<<16)-1; rep(i, N) rep(S, 1<<16) { if(dp[i&1][S] == -1) continue; dp[(i+1)&1][panel[i]|S] = max(dp[(i+1)&1][panel[i]|S], dp[i&1][S]); rep(j, C) { int nS = (panel[i]|S)&(mask-jubi[j]); int score = __builtin_popcount(panel[i]|S) - __builtin_popcount(nS); dp[(i+1)&1][nS] = max(dp[(i+1)&1][nS], dp[i&1][S] + score); } } cout << *max_element(dp[N&1], dp[N&1]+(1<<16)) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int A; int B; cin>>A; cin>>B; for (int i=1; i<1010; i++){ if ((int)(0.08*i)==A && (int)(0.1*i)==B){ cout<<i; exit(0); } } cout<<-1; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t) { t--; int n; cin >> n; int t[n]; cin >> t[0]; int ost = t[0]; int ile = 1; vector<int> V; for (int i = 1; i < n; i++) { cin >> t[i]; if (t[i] != ost) { V.push_back(ile); ile = 1; ost = t[i]; } else ile++; } V.push_back(ile); vector<int> W; int sumax = 0; int j = 0; int m = V.size(); while (sumax <= (n / 2) && j <= (m - 1)) { if ((sumax + V[j]) > (n / 2)) break; sumax += V[j]; W.push_back(V[j]); j++; } n = W.size(); if (n <= 2) { cout << "0 0 0\n"; continue; } int g = W[0]; int suma = 0; j = 1; while (suma <= g && j < n) { suma += W[j]; j++; } if (suma <= g) { cout << "0 0 0\n"; continue; } int s = suma; suma = 0; while (j < n) { suma += W[j]; j++; } if (suma <= g) { cout << "0 0 0\n"; continue; } int b = suma; cout << g << " " << s << " " << b << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = 1e9 + 7; const double eps = 1e-9; struct segtree { int n; pair<int, int> t[2400005], lz[2400005]; const pair<int, int> neutral = {0, -1}; void init(int n) { this->n = n; fill(t + 1, t + 4 * n + 5, neutral); fill(lz + 1, lz + 4 * n + 5, neutral); } pair<int, int> comb(pair<int, int> u, pair<int, int> v) { return max(u, v); } void pull(int u) { t[u] = comb(t[2 * u], t[2 * u + 1]); } void push(int u, int tl, int tr) { if (lz[u] != neutral) { t[u] = max(t[u], lz[u]); if (tl != tr) { lz[2 * u] = max(lz[2 * u], lz[u]); lz[2 * u + 1] = max(lz[2 * u + 1], lz[u]); } lz[u] = neutral; } } void upd(int u, int tl, int tr, int l, int r, pair<int, int> val) { push(u, tl, tr); if (l > r) return; else if (l == tl && r == tr) { lz[u] = max(lz[u], val); push(u, tl, tr); } else { int tm = (tl + tr) / 2; upd(2 * u, tl, tm, l, min(r, tm), val); upd(2 * u + 1, tm + 1, tr, max(l, tm + 1), r, val); pull(u); } } void upd(int l, int r, pair<int, int> val) { upd(1, 1, n, l, r, val); } pair<int, int> query(int u, int tl, int tr, int l, int r) { if (l > r) return neutral; push(u, tl, tr); if (l == tl && r == tr) return t[u]; else { int tm = (tl + tr) / 2; return comb(query(2 * u, tl, tm, l, min(r, tm)), query(2 * u + 1, tm + 1, tr, max(l, tm + 1), r)); } } pair<int, int> query(int l, int r) { return query(1, 1, n, l, r); } } ds; int n, m; vector<pair<int, int> > r[300005]; vector<int> comp; int p[300005]; bool vis[300005]; int ans, cur; inline int compress(int x) { return upper_bound(comp.begin(), comp.end(), x) - comp.begin(); } void solve() { cin >> n >> m; for (int i = 1, row, le, ri; i <= m; ++i) { cin >> row >> le >> ri; r[row].push_back({le, ri}); comp.push_back(le); comp.push_back(ri); } sort(comp.begin(), comp.end()); comp.resize(unique(comp.begin(), comp.end()) - comp.begin()); for (int i = 1; i <= n; ++i) for (auto &x : r[i]) x = {compress(x.first), compress(x.second)}; ds.init(comp.size()); for (int i = 1; i <= n; ++i) { pair<int, int> mx = {0, -1}; for (auto x : r[i]) mx = max(mx, ds.query(x.first, x.second)); p[i] = mx.second; for (auto x : r[i]) ds.upd(x.first, x.second, {mx.first + 1, i}); } tie(ans, cur) = ds.query(1, comp.size()); memset(vis, 0, sizeof(vis)); for (; cur != -1; cur = p[cur]) vis[cur] = 1; cout << n - ans << "\n"; for (int i = 1; i <= n; ++i) if (!vis[i]) cout << i << " "; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; for (int tc = 1; tc <= test; ++tc) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int mul(long long a, long long b) { a %= mod; b %= mod; return a * b % mod; } int add(long long a, long long b) { a %= mod; b %= mod; return (a + b) % mod; } int sum(long long k) { return mul(mul(k, add(k, 1)), (mod + 1) / 2); } int main() { long long n, m; cin >> n >> m; long long ans = 0; if (m <= 3162300) { for (int i = 1; i <= m; ++i) ans = add(ans, n % i); return cout << ans << endl, 0; } for (long long i = 1; i * i <= n; ++i) { long long k = n / i; long long delta = n % i; long long dt = delta / k; long long h = (k - delta - 1) / (i + 1); long long lans = add(mul(i, dt + 1), sum(dt)); lans = mul(k, lans); ans = add(ans, lans); if (i < k) { bool ok = 1; if (m < k) { if (n / m == n / k) { k = m; delta = n % m; h = (m - delta - 1) / (i + 1); } else ok = 0; } if (ok) { lans = add(mul(k, h + 1), mod - sum(h)); lans = mul(i, lans); ans = add(ans, lans); } } i += dt; } ans = add(mul(n, m), mod - ans); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; void input(vector<int>& a) { for (int i = 0; i < a.size(); i++) cin >> a[i]; } void inputp(vector<pair<int, int> >& a) { for (int i = 0; i < a.size(); i++) cin >> a[i].first >> a[i].second; } void output(vector<int>& a) { for (int i = 0; i < a.size(); i++) cout << a[i] << ' '; } void outputp(vector<pair<int, int> >& a) { for (int i = 0; i < a.size(); i++) cout << a[i].first << ' ' << a[i].second << endl; } const long long mod = 1000000009; long long power(long long a, long long b) { long long ans = 1LL; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b /= 2.0; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, a, b, k; cin >> n >> a >> b >> k; long long pa = power(a, n); long long push_back = 1LL; long long ipa = power(a, mod - 2LL); string s; cin >> s; long long ans = 0; long long count = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == '+') { ans += (pa * push_back) % mod; ans %= mod; count++; } else { ans = (ans + (mod - (pa * push_back) % mod) % mod) % mod; count--; } pa = (pa * ipa) % mod; push_back = (push_back * b) % mod; } long long temp = ((long long)power(b, k) * (long long)power(ipa, k)) % mod; if (temp == 1) { ans = (ans * (n + 1LL) / k) % mod; } else { long long t = (power(temp, (n + 1LL) / k) + mod - 1LL) % mod; long long y = power((temp + mod - 1LL) % mod, mod - 2LL); t = (t * y) % mod; ans = (ans * t) % mod; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> edges[100005]; int hei[100005], par[100005]; int mxd = 0, mxu; void dfs(int u, int p) { int mxv = 0, smxv = 0; hei[u] = 1; for (auto v : edges[u]) { if (v == p) continue; par[v] = u; dfs(v, u); hei[u] = max(hei[u], hei[v] + 1); if (hei[v] > hei[mxv]) smxv = mxv, mxv = v; else if (hei[v] > hei[smxv]) smxv = v; } if (hei[mxv] + hei[smxv] + 1 > mxd) mxd = hei[mxv] + hei[smxv] + 1, mxu = u; } vector<int> findDiameter() { int u = mxu; int mxv = 0, smxv = 0; for (auto v : edges[u]) { if (v == par[u]) continue; if (hei[v] > hei[mxv]) smxv = mxv, mxv = v; else if (hei[v] > hei[smxv]) smxv = v; } vector<int> tmp, tmp2; if (mxv) { u = mxv; tmp.push_back(u); while (edges[u].size() > 1) { int mxv = 0; for (auto v : edges[u]) { if (v == par[u]) continue; if (hei[v] > hei[mxv]) mxv = v; } tmp.push_back(mxv); u = mxv; } } if (smxv) { u = smxv; tmp2.push_back(u); while (edges[u].size() > 1) { int mxv = 0; for (auto v : edges[u]) { if (v == par[u]) continue; if (hei[v] > hei[mxv]) mxv = v; } tmp2.push_back(mxv); u = mxv; } } reverse(tmp.begin(), tmp.end()); tmp.push_back(mxu); for (auto u : tmp2) tmp.push_back(u); return tmp; } bool chain[100005]; void checkchain(int u, int p) { chain[u] = (edges[u].size() <= 2); hei[u] = 1; for (auto v : edges[u]) { if (v == p) continue; checkchain(v, u); chain[u] &= chain[v]; hei[u] = max(hei[u], hei[v] + 1); } } int rt = 0; void findroot(int u) { int v1 = 0, v2 = 0; bool in1 = 0, in2 = 0; for (auto v : edges[u]) { if (!v1) v1 = v; else if (hei[v] == hei[v1] && chain[v] == chain[v1]) in1 = 1; else if (!v2) v2 = v; else if (hei[v] != hei[v1] || chain[v] != chain[v1]) in2 = 1; else { rt = -1; return; } } if (!v2) { rt = u; return; } if (in1 && in2 || in1 && !chain[v2] || in2 && !chain[v1]) { rt = -1; return; } int st = 0, pre = u; if (!in1 && chain[v1]) st = v1; else st = v2; while (edges[st].size() >= 2) { for (auto v : edges[st]) if (v != pre) { pre = st; st = v; break; } } rt = st; } int deg[100005]; void check(int u, int p, int d) { if (!deg[d]) deg[d] = edges[u].size(); else if (deg[d] != edges[u].size()) { rt = -1; return; } for (auto v : edges[u]) { if (v == p) continue; check(v, u, d + 1); if (rt == -1) return; } } int main() { int n; scanf("%d", &n); for (int i = (1); i <= (n - 1); i++) { int u, v; scanf("%d %d", &u, &v); edges[u].push_back(v); edges[v].push_back(u); } dfs(1, -1); auto dia = findDiameter(); int d = dia.size(); int cen = dia[d >> 1]; checkchain(cen, -1); findroot(cen); if (rt >= 1) check(rt, -1, 1); printf("%d\n", rt); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100005, mod = 1000000007; long long ncr(long long n, long long r) { long long sum = 1; long long f = 2; for (long long i = n - r + 1; i <= n; i++) { sum *= i; if (sum % f == 0 && r >= f) sum /= f++; } if (r >= f) { for (long long i = f; i <= r; i++) sum /= i; } return sum; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, f = 0000000000000; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x; cin >> x; if (x == 1) { if (i == 0 || j == 0 || i == n - 1 || j == m - 1) f = 1; } } } if (f) cout << 2; else cout << 4; }
1
#include <bits/stdc++.h> int n, i, x, ind[210], o = 0, e = 0; char a[200001], arr[200010]; int main() { scanf("%s", a); n = strlen(a); if (n == 1) { printf("%s\n", a); return 0; } for (int i = 0; i < n; i++) { ind[a[i]]++; } for (int i = 'a'; i <= 'z'; i++) { if (ind[i] > 0) { ind[i] % 2 == 0 ? e++ : o++; } } for (int i = 'z'; i >= 'a'; i--) { if (ind[i] % 2 != 0) { for (int x = 'a'; x < i; x++) { if (ind[x] % 2 != 0) { ind[x]++, ind[i]--; break; } } } } int j = 0; for (int i = 'a'; i <= 'z'; i++) { if (ind[i] > 0) { for (int x = 0; x < ind[i] / 2; x++) { printf("%c", i); arr[j++] = i; } } } if (n % 2 != 0) { for (int i = 'a'; i <= 'z'; i++) { if (ind[i] % 2 == 1) { printf("%c", i); break; } } } for (int i = j - 1; i >= 0; i--) { printf("%c", arr[i]); } }
3
#include <bits/stdc++.h> using namespace std; template <long long mod> struct modular { long long value; modular(long long x = 0) { value = x % mod; if (value < 0) value += mod; } modular& operator+=(const modular& other) { if ((value += other.value) >= mod) value -= mod; return *this; } modular& operator-=(const modular& other) { if ((value -= other.value) < 0) value += mod; return *this; } modular& operator*=(const modular& other) { value = value * other.value % mod; return *this; } modular& operator/=(const modular& other) { long long a = 0, b = 1, c = other.value, m = mod; while (c != 0) { long long t = m / c; m -= t * c; swap(c, m); a -= t * b; swap(a, b); } a %= mod; if (a < 0) a += mod; value = value * a % mod; return *this; } modular operator+(const modular& rhs) const { return modular(*this) += rhs; } modular operator-(const modular& rhs) const { return modular(*this) -= rhs; } modular operator*(const modular& rhs) const { return modular(*this) *= rhs; } modular operator/(const modular& rhs) const { return modular(*this) /= rhs; } modular& operator++() { return *this += 1; } modular& operator--() { return *this -= 1; } modular operator++(int) { modular res(*this); *this += 1; return res; } modular operator--(int) { modular res(*this); *this -= 1; return res; } modular operator-() const { return modular(-value); } bool operator==(const modular& rhs) const { return value == rhs.value; } bool operator!=(const modular& rhs) const { return value != rhs.value; } bool operator<(const modular& rhs) const { return value < rhs.value; } }; template <long long mod> string to_string(const modular<mod>& x) { return to_string(x.value); } template <long long mod> ostream& operator<<(ostream& stream, const modular<mod>& x) { return stream << x.value; } template <long long mod> istream& operator>>(istream& stream, modular<mod>& x) { stream >> x.value; x.value %= mod; if (x.value < 0) x.value += mod; return stream; } constexpr long long mod = 998244353LL; using mint = modular<mod>; mint power(mint a, long long n) { mint res = 1; while (n > 0) { if (n & 1) { res *= a; } a *= a; n >>= 1; } return res; } vector<mint> fact(1, 1); vector<mint> finv(1, 1); mint C(int n, int k) { if (n < k || k < 0) { return mint(0); } while ((int)fact.size() < n + 1) { fact.emplace_back(fact.back() * (int)fact.size()); finv.emplace_back(mint(1) / fact.back()); } return fact[n] * finv[k] * finv[n - k]; } int main() { ios::sync_with_stdio(false); cin.tie(0); const int MAX = 100001; vector<int> primes; vector<int> spf(MAX); vector<bool> is_prime(MAX, true); is_prime[0] = is_prime[1] = false; spf[0] = spf[1] = 1; for (int i = 2; i < MAX; i++) { if (!is_prime[i]) { continue; } primes.emplace_back(i); spf[i] = i; if (1LL * i * i >= MAX) { continue; } for (int j = i * i; j < MAX; j += i) { if (is_prime[j]) { is_prime[j] = false; spf[j] = i; } } } int n, m; cin >> n >> m; vector<int> l(n), r(n); for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; } vector<mint> a(m + 1); vector<vector<mint>> dp(m + 1); for (int i = 1; i <= m; i++) { dp[i] = vector<mint>(m / i + 1); dp[i][0] = 1; } for (int k = 0; k < n; k++) { for (int i = 1; i <= m; i++) { int x = (l[k] + i - 1) / i; int y = r[k] / i; vector<mint> new_dp(m / i + 1); for (int j = 0; j <= m / i; j++) { if (j != 0) { new_dp[j] = new_dp[j - 1]; } if (j >= x) { new_dp[j] += dp[i][j - x]; } if (j >= y + 1) { new_dp[j] -= dp[i][j - y - 1]; } } swap(dp[i], new_dp); } } for (int i = 1; i <= m; i++) { a[i] = accumulate(dp[i].begin(), dp[i].end(), mint(0)); } for (int i = m; i > 1; i--) { if (!is_prime[i]) { continue; } for (int j = 1; i * j <= m; j++) { a[j] -= a[i * j]; } } cout << a[1] << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n{0}, m{0}; cin >> n >> m; vector<vector<int> > adj(n, vector<int>()); for (int i = 0; i < m; i++) { int x{0}, y{0}; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } int max_degree{0}, max_vertex{-1}; for (int i = 0; i < n; i++) { if (adj[i].size() > max_degree) { max_degree = adj[i].size(); max_vertex = i; } } queue<int> bfs; bfs.push(max_vertex); vector<bool> visited(n, false); visited[max_vertex] = true; while (!bfs.empty()) { int source = bfs.front(); bfs.pop(); for (int adjacents : adj[source]) { if (!visited[adjacents]) { cout << source + 1 << " " << adjacents + 1 << endl; visited[adjacents] = true; bfs.push(adjacents); } } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const long long INF = 0x3f3f3f3f3f3f3f3fll; const int inf = 0x3f3f3f3f; const int maxn = 50 + 5; const int maxm = 20000 + 10; const int mod = 998244353; int ch[maxn][maxn]; int num[4], flag[4]; vector<int> ff; int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); a--, b--; for (int i = 1; i <= 50; i++) for (int j = 1; j <= 50; j++) ch[i][j] = -1; for (int i = 25; i >= 1; i -= 2) for (int j = 1; j <= 50; j++) ch[i][j] = 0; for (int i = 25; i >= 1; i--) for (int j = 2; j <= 50; j += 2) ch[i][j] = 0; for (int i = 26; i <= 50; i += 2) for (int j = 1; j <= 50; j++) ch[i][j] = 1; for (int i = 26; i <= 50; i++) for (int j = 2; j <= 50; j += 2) ch[i][j] = 1; for (int i = 1; i <= 50; i++) { for (int j = 1; j <= 50; j++) { if (ch[i][j] == -1) { if (c > 0) { ch[i][j] = 2; c--; } else if (d > 0) { ch[i][j] = 3; d--; } } } } for (int i = 26; i <= 50; i++) { for (int j = 1; j <= 50; j++) { if (ch[i][j] == -1) { if (a > 0) { ch[i][j] = 0; a--; } else { ch[i][j] = 1; } } } } for (int i = 1; i <= 25; i++) { for (int j = 1; j <= 50; j++) { if (ch[i][j] == -1) { if (b > 0) { ch[i][j] = 1; b--; } else { ch[i][j] = 0; } } } } printf("50 50\n"); for (int i = 1; i <= 50; i++) { for (int j = 1; j <= 50; j++) printf("%c", 'A' + ch[i][j]); puts(""); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 4; const int inf = 0x3f3f3f3f; int main() { int n; char s[1010]; while (cin >> n >> s) { bool flag = true; int ans = 0, now = 0; for (int i = 0; i < n; i++) { if (!flag) flag = true; now += (s[i] != '0'); if (flag && s[i] == '0') { flag = false; ans += now; ans *= 10; now = 0; } } if (flag) ans += now; cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } void transfer(int& now, int add) { if (add < 0) add = add % MOD + MOD; now = (now + add) % MOD; } int main() { int n, m; ios::sync_with_stdio(false); while (cin >> n >> m) { vector<pair<int, int> > bus(m); for (__typeof(bus.begin()) i = bus.begin(); i != bus.end(); ++i) cin >> i->first >> i->second; sort(bus.begin(), bus.end(), cmp); vector<int> con(m + 2); con.push_back(-1); con.push_back(n); for (__typeof(bus.begin()) i = bus.begin(); i != bus.end(); ++i) con.push_back(i->second); sort(con.begin(), con.end()); con.erase(unique(con.begin(), con.end()), con.end()); int m = con.size(); vector<int> dp(m); vector<int> sum(m); fill(dp.begin(), dp.end(), 0); fill(sum.begin(), sum.end(), 0); dp[0] = sum[0] = 0; dp[1] = sum[1] = 1; int pre = -1; for (__typeof(bus.begin()) i = bus.begin(); i != bus.end(); ++i) { int end = lower_bound(con.begin(), con.end(), i->second) - con.begin(); int beg = lower_bound(con.begin(), con.end(), i->first) - con.begin(); if (pre != i->second) transfer(sum[end], sum[end - 1]); transfer(dp[end], sum[end - 1] - sum[beg - 1]); transfer(sum[end], sum[end - 1] - sum[beg - 1]); pre = i->second; } printf("%d\n", dp[m - 1]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; string s; int n, m; int str[3][50005]; int main() { while (cin >> s) { str[0][0] = 0; str[1][0] = 0; str[2][0] = 0; int l = s.length(); for (int i = 0; i < l; i++) { str[0][i + 1] = str[0][i] + (s[i] == 'a'); str[1][i + 1] = max(str[0][i], str[1][i]) + (s[i] == 'b'); str[2][i + 1] = max(max(str[0][i], str[1][i]), str[2][i]) + (s[i] == 'a'); } cout << max(max(str[0][l], str[1][l]), str[2][l]) << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int gi() { int x = 0, o = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * o; } int n, l, r, a[N], dp[N]; vector<int> key, oth; int main() { cin >> n >> l >> r; int sum = 0; for (int i = 1; i <= n; i++) a[i] = gi(), sum += a[i]; l = sum - l; r = sum - r; swap(l, r); if (r < 0) return cout << 0, 0; for (int i = 1; i <= n; i++) (gi() ? key : oth).push_back(a[i]); memset(dp, 0xc0, sizeof(dp)); dp[0] = 0; for (auto x : oth) for (int i = r - x; i >= 0; i--) dp[i + x] = max(dp[i + x], dp[i]); sort(key.begin(), key.end(), greater<int>()); for (auto x : key) for (int i = r - x; i >= 0; i--) dp[i + x] = max(dp[i + x], dp[i] + (i + x >= l)); cout << *max_element(dp, dp + r + 1); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, k; const int MAXN = 200005; const long long inf = 1e9; struct segTree { struct Node { long long pre, suf, mx, sum; Node() {} Node(long long pre, long long suf, long long mx, long long sum) { this->pre = pre; this->suf = suf; this->mx = mx; this->sum = sum; } }; vector<Node> tree; void init() { tree.clear(); tree.resize(4 * n + 1, Node(-inf, -inf, -inf, -inf)); } Node mrg(Node &L, Node &R) { Node ret = Node(-inf, -inf, -inf, -inf); ret.mx = max({L.mx, R.mx, L.suf + R.pre}); ret.sum = L.sum + R.sum; ret.pre = max({L.pre, L.sum + R.pre}); ret.suf = max({R.suf, R.sum + L.suf}); return ret; } void update(int node, int st, int ed, int pos) { if (pos > ed || pos < st) return; if (st == ed) { tree[node] = Node(1, 1, 1, 1); return; } update(node * 2, st, (st + ed) / 2, pos); update(node * 2 + 1, (st + ed) / 2 + 1, ed, pos); tree[node] = mrg(tree[node * 2], tree[node * 2 + 1]); } Node query(int node, int st, int ed, int l, int r) { if (st > r || ed < l) return Node(-inf, -inf, -inf, -inf); if (st >= l && ed <= r) return tree[node]; auto L = query(node * 2, st, (st + ed) / 2, l, r); auto R = query(node * 2 + 1, (st + ed) / 2 + 1, ed, l, r); return mrg(L, R); } } seg; int lo[MAXN], hi[MAXN], l[MAXN], r[MAXN], w[MAXN], h[MAXN]; vector<int> query[MAXN]; vector<pair<int, int> > bag; int pbs() { seg.init(); int update = 0; for (int i = 0; i < MAXN; i++) query[i].clear(); for (int i = 1; i <= m; i++) { if (lo[i] <= hi[i]) { query[(lo[i] + hi[i]) / 2].push_back(i); update = 1; } } for (int i = (int)bag.size() - 1; i >= 0; i--) { seg.update(1, 1, n, bag[i].second); for (auto x : query[i]) { if (seg.query(1, 1, n, l[x], r[x]).mx >= w[x]) lo[x] = i + 1; else hi[x] = i - 1; } } return update; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> h[i]; bag.push_back({h[i], i}); } sort(bag.begin(), bag.end()); cin >> m; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> w[i]; lo[i] = 0; hi[i] = (int)bag.size() - 1; } while (pbs()) ; for (int i = 1; i <= m; i++) { cout << bag[lo[i] - 1].first << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } long long int s = 0; set<long long int> nums; bool res = false; for (int i = 0; i < n; i++) { s += a[i]; long long int t = sum - s; nums.insert(a[i]); if (s > t && (s - t) % 2 == 0 && nums.count((s - t) / 2) == 1) { res = true; } } s = 0; if (sum % 2 == 0 && nums.count(sum / 2) == 1) { res = true; } nums.clear(); for (int i = n - 1; i >= 0; i--) { s += a[i]; long long int t = sum - s; nums.insert(a[i]); if (s > t && (s - t) % 2 == 0 && nums.count((s - t) / 2) == 1) { res = true; } } if (res) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int dx[5] = {1, -1, 0, 0}; const int dy[5] = {0, 0, 1, -1}; int r, c, sx, sy, ex, ey, dis[1010][1010], vis[1010][1010]; char grid[1010][1010]; int ans = 0; queue<pair<int, int> > q; inline bool is_le(int x, int y) { if (vis[x][y] || x >= r || x < 0 || y >= c || y < 0 || grid[x][y] == 'T') return false; return true; } void bfs() { q.push(make_pair(ex, ey)); vis[ex][ey] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; ++i) { int a = x + dx[i], b = y + dy[i]; if (!is_le(a, b)) continue; dis[a][b] = dis[x][y] + 1; q.push(make_pair(a, b)); vis[a][b] = 1; } } return; } int main() { scanf("%d%d", &r, &c); for (int i = 0; i < r; ++i) scanf("%s", grid[i]); for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { if (grid[i][j] == 'S') sx = i, sy = j; if (grid[i][j] == 'E') ex = i, ey = j; } } bfs(); for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { if (isdigit(grid[i][j]) && dis[i][j] <= dis[sx][sy] && dis[i][j] != 0 && grid[i][j] != '0') ans += (grid[i][j] - '0'); } } printf("%d", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 5; int n, t; double p, dp[N][N]; signed main() { scanf("%d %lf %d", &n, &p, &t); dp[0][0] = 1; for (int i = 1; i <= t; ++i) { dp[i][0] = dp[i - 1][0] * (1 - p); for (int j = 1; j <= n; ++j) { if (j == n) dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j]; else dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1 - p); } } double ans = 0; for (int i = 0; i <= n; ++i) ans += dp[t][i] * (double)i; printf("%.12lf\n", ans); return 0; }
4
#include<bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define sz(x) ( (int)(x).size() ) using LL = long long; template<class T> inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; } template<class T> inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; } const LL infLL = 1e18; mt19937 rng( (int)chrono::steady_clock::now().time_since_epoch().count() ); struct Bit { vector<LL> mn; bool rev; Bit(int nNode, int _rev) : rev(_rev) { mn.assign(nNode, infLL); } void upd(int pos, LL val) { if (rev) pos = sz(mn) - 1 - pos; for (int i = pos; i < sz(mn); i |= i + 1) asMn(mn[i], val); } LL getMn(int pos) { if (rev) pos = sz(mn) - 1 - pos; LL ret = infLL; for (int i = pos; ~i; i = (i & (i + 1) ) - 1) asMn(ret, mn[i]); return ret; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef FourLeafClover freopen("input", "r", stdin); #endif // FourLeafClover int n, q, a, b; cin >> n >> q >> a >> b; --a; --b; vector<int> x(q); for (auto &i : x) cin >> i, --i; if (q == 1) return cout << min(abs(a - x[0]), abs(b - x[0]) ), 0; vector<LL> pref(sz(x) ); for (int i = 1; i < sz(x); ++i) pref[i] = pref[i - 1] + abs(x[i] - x[i - 1]); vector<LL> f(q, infLL); Bit smaller(n, false), bigger(n, true); for (int i = 0; i + 1 < sz(x); ++i) { asMn(f[i], pref[i] + abs(a - x[0]) + abs(b - x[i + 1]) ); asMn(f[i], pref[i] + abs(b - x[0]) + abs(a - x[i + 1]) ); asMn(f[i], pref[i] + x[i + 1] + smaller.getMn(x[i + 1]) ); asMn(f[i], pref[i] + bigger.getMn(x[i + 1]) - x[i + 1]); smaller.upd(x[i], f[i] - pref[i + 1] - x[i]); bigger.upd(x[i], f[i] - pref[i + 1] + x[i]); } LL ans = infLL; for (int i = 0; i + 1 < sz(f); ++i) asMn(ans, f[i] + pref.back() - pref[i + 1]); cout << ans << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main (){ int x,y,z,n; cin >> x >> y>> z; n=(x-z)/(y+z); cout << n; }
0
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } using ll = int64_t; using ull = uint64_t; using ld = long double; const int N = 1e5 + 5, INF = 1e9 + 5; int n, a[N]; ll dp[N]; int t[4 * N]; void build(int v, int tl, int tr) { if (tl == tr) { t[v] = tl; return; } int tm = (tl + tr) >> 1; int vl = v << 1, vr = vl | 1; build(vl, tl, tm); build(vr, tm + 1, tr); t[v] = (a[t[vl]] >= a[t[vr]]) ? t[vl] : t[vr]; } int query(int v, int tl, int tr, int l, int r) { if (r < tl || tr < l) { return n; } if (l <= tl && tr <= r) { return t[v]; } int tm = (tl + tr) >> 1; int vl = v << 1, vr = vl | 1; int ql = query(vl, tl, tm, l, r); int qr = query(vr, tm + 1, tr, l, r); return (a[ql] >= a[qr]) ? ql : qr; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin.exceptions(cin.failbit); ; cin >> n; for (int i = 0; i < n - 1; ++i) { cin >> a[i]; --a[i]; } a[n - 1] = n - 1; a[n] = -INF; build(1, 0, n - 1); ll ans = 0; for (int i = n - 2; i >= 0; --i) { int k = query(1, 0, n - 1, i + 1, a[i]); dp[i] = dp[k] + (k - i) + (n - a[i] - 1); ans += dp[i]; } cout << ans << '\n'; }
5
#include <bits/stdc++.h> const int mod = 1e9 + 7; using namespace std; vector<string> v; int MI(int n) { string s = to_string(n); int mi = 100; string s2 = s; for (int i = 0; i < (55); i++) { int OP = 0; s = s2; for (int j = 0; j < min(v[i].size(), s.size()); j++) { if (v[i][j] != s[j]) { s.erase(j, 1); j--; OP++; } } int k = v[i].size() - s.size(); if (k < 0) k *= -1; mi = min(mi, OP + k); } return mi; } void solve() { int n, ans = 0; cin >> n; cout << MI(n) << "\n"; } int main() { ios::sync_with_stdio(0), cin.tie(0); int t = 1; cin >> t; for (int i = 0; i < (55); i++) { long long ans = (1LL) << i; string s = to_string(ans); v.push_back(s); } while (t--) solve(); }
4
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <stack> #include <queue> using namespace std; typedef long long LL; typedef pair<int, int> PII; static const double EPS = 1e-5; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) vector< vector<int> > rot( vector< vector<int> > &ary){ int n = (int)ary.size(); vector< vector<int> > ret(n,vector<int>(n)); REP(y,n)REP(x,n)ret[y][x]=ary[n-1-x][y]; return ret; } int main(void){ int m1,m2; while(cin>>m1>>m2){ if(m1==0) break; vector< vector<int> > view(m1,vector<int>(m1)); REP(y,m1)REP(x,m1) cin>>view[y][x]; vector< vector<int> > pict(m2,vector<int>(m2)); REP(y,m2)REP(x,m2) cin>>pict[y][x]; vector<PII> ans; REP(k,4){ for(int sy=0; sy+m2-1<m1; sy++){ for(int sx=0; sx+m2-1<m1; sx++){ bool f = true; REP(dy,m2)REP(dx,m2){ if(pict[dy][dx]!=-1 && pict[dy][dx]!=view[sy+dy][sx+dx]){ f=false; goto READ; } } READ: if(f){ REP(y,m2)REP(x,m2){ if(pict[y][x]!=-1){ ans.push_back(PII(sy+y,sx+x)); goto END; } } } END:; } } pict = rot(pict); } if(ans.empty()){ cout<<"NA"<<endl; }else{ sort(ans.begin(),ans.end()); printf("%d %d\n",ans[0].second+1,ans[0].first+1); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const double EPS = 1e-9; const int INF = 2e9; const long long LINF = 1e18; template <class T> const T sqr(const T &x) { return x * x; } template <class T> const T &min(const T &a, const T &b, const T &c) { return min(min(a, b), c); } template <class T> const T &max(const T &a, const T &b, const T &c) { return max(max(a, b), c); } struct DbgStream { template <class T> DbgStream &operator<<(const T &x) { return *this; } } z_dbg; int n, ans, q; int main() { ios_base::sync_with_stdio(0); scanf("%d", &n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int x; scanf("%d", &x); if (i == j) ans ^= x; } } scanf("%d", &q); for (int i = 0; i < q; ++i) { int t; scanf("%d", &t); if (t == 3) { putchar('0' + ans); } else { scanf("%d", &t); ans ^= 1; } } putchar('\n'); return 0; }
1
#include <bits/stdc++.h> int64_t B, D; std::string A, C; int64_t next[25][105]; int main() { std::cin >> B >> D >> A >> C; for (int64_t i = 0; i < C.size(); i++) { int64_t nxt = 0; for (char c : A) { if (c == C[(i + nxt) % C.size()]) { nxt++; } } next[0][i] = nxt; } for (int64_t k = 1; k < 25; k++) { for (int64_t i = 0; i < C.size(); i++) { next[k][i] = next[k - 1][i] + next[k - 1][(i + next[k - 1][i]) % C.size()]; } } int64_t i = 0; int64_t cnt = 0; for (int64_t k = 25 - 1; k >= 0; k--) { if (B & (1 << k)) { i += next[k][i]; cnt += i / C.size(); i %= C.size(); } } std::cout << cnt / D << std::endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long n = 0; cin >> n; long ca = 0, cb = 0; long a[100000] = {0}; long b[100000] = {0}; while (n--) { cin >> a[n] >> b[n]; if (a[n] > 0) { ca++; } else if (a[n] < 0) { cb++; } } if ((cb == 1 || cb == 0) || (ca == 1 || ca == 0)) { cout << "Yes"; } else { cout << "No"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> f; vector<vector<int> > g, q; vector<char> used1, used2; void DFS1(int u) { used1[u] = 1; for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (!used1[v]) DFS1(v); } } void DFS2(int u) { used2[u] = 1; if (f[u] == 1) return; for (int i = 0; i < q[u].size(); ++i) { int v = q[u][i]; if (!used2[v]) DFS2(v); } } int main() { cin >> n >> m; f.resize(n); g.resize(n); q.resize(n); used1.resize(n, 0); used2.resize(n, 0); for (int u = 0; u < n; ++u) { cin >> f[u]; } for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); q[v].push_back(u); } for (int u = 0; u < n; ++u) { if (f[u] == 1 && !used1[u]) DFS1(u); if (f[u] == 2 && !used2[u]) DFS2(u); } for (int u = 0; u < n; ++u) { cout << (used1[u] && used2[u]) << endl; } return 0; }
1
#include <cstdio> int N, a[100001]; long long S, C; int main() { scanf("%d", &N); if (N == 1) { puts("YES"); return 0; } for (int i = 1; i <= N; i++) { scanf("%d", a + i); S += a[i]; } if (S % ((long long)N * (N + 1) / 2)) { puts("NO"); return 0; } S /= (long long)N * (N + 1) / 2; for (int i = 1; i <= N; i++) { long long D = S - a[i % N + 1] + a[i]; if (D < 0 || D % N) { puts("NO"); return 0; } C += D / N; } puts(S == C ? "YES" : "NO"); return 0; }
0
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") long long mul(long long a, long long b) { return (a * b) % (1000000007); } long long add(long long a, long long b) { return (a + b) % (1000000007); } long long sub(long long a, long long b) { return ((a - b) % (1000000007) + (1000000007)) % (1000000007); } void upd(long long &a, long long b) { a = (a % (1000000007) + b % (1000000007)) % (1000000007); } inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long a[123333]; int n; long long Abs(long long x) { return max(-x, x); } bool check(long long dis) { set<long long> S; S.insert(a[1]); S.insert(a[2]); for (int i = 3; i <= n; i++) { while (!S.empty() && Abs((*S.begin()) - a[i]) > dis) { S.erase(S.begin()); } while (!S.empty() && Abs((*S.rbegin()) - a[i]) > dis) { S.erase(--S.end()); } if (S.empty()) return 0; S.insert(a[i]); } return 1; } int main() { ios::sync_with_stdio(false); n = read() + 2; for (int i = 1; i <= n; i++) a[i] = read(); long long l = Abs(a[1] - a[2]), r = 1e12, ans = 0; while (l <= r) { long long m = l + r >> 1; if (check(m)) ans = m, r = m - 1; else l = m + 1; } cout << ans << endl; return 0; }
5
#include <iostream> using namespace std; int main() { string s; cin >> s; int len = s.length(); cout << (len % 2 == 0 && s == string("hihihihihi", len) ? "Yes" : "No") << endl; }
0
#include<bits/stdc++.h> #define pb emplace_back #define pf first #define ps second #define all(x) x.begin(),x.end() #define allr(x) x.rbegin(),x.rend() #define endl '\n' #define mod 1000000007 #define INF 1000000001 #define forn(i,n) for(ll i=0;i<n;i++) #define MAX 200001 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); using namespace std; typedef long long int ll; ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } //////////////////////////////////// JAI SHREE RAM ///////////////////////////////////// void JAISHREERAM() { ll n,i,k,j,ans=0; cin>>n>>k; vector<ll> a(n); forn(i,n) cin>>a[i]; ll sum=a[0]; for(i=1;i<n;i++) { ll l=0,r=1e17,mn=r; while(l<=r) { ll m=(l+r)/2; ll x=a[i]*100-(k*(m+sum+ans)); if(x<=0) mn=m,r=m-1; else l=m+1; } ans+=mn; sum+=a[i]; } cout<<ans<<endl; } int main() { fast; int T=1; cin>>T; for(int t=1;t<=T;t++) { JAISHREERAM(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using Pa = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; stack<Pa<ll, ll>> st; st.push({A[0], 1}); for (int i = 1; i < N; i++) { Pa<ll, ll> now = {A[i], 1}; while (!st.empty()) { Pa<ll, ll> pre = st.top(); if (pre.first * now.second > now.first * pre.second) { st.pop(); now.first += pre.first; now.second += pre.second; } else break; } st.push(now); } vec<double> ans; while (!st.empty()) { Pa<ll, ll> now = st.top(); st.pop(); for (int i = 0; i < now.second; i++) ans.push_back(1.0 * now.first / now.second); } reverse(ans.begin(), ans.end()); cout << fixed << setprecision(10); for (auto& x : ans) cout << x << "\n"; }
3
#include <bits/stdc++.h> using namespace std; #define ll long long #define dbg puts("It's Just Begining.Wait For The End!!!!!\n") #define CHECK(x) cout << (#x) << " is " << (x) <<"\n"; #define endl printf("\n") #define pi acos(-1) typedef pair<ll,ll> pii; #define pb push_back #define ms(a,b) memset(a, b, sizeof(a)) #define in(x) scanf("%lld",&x) #define out(x) printf("%lld",x) #define X first #define Y second #define sps printf(" ") #define dou(x) cout<<fixed<<setprecision(10)<<x #define all(v) v.begin(),v.end() #define dis(it,it1) distance(it,it1)+1 #define MAX 200005 #define INF 9223372036854775807LL #define MOD 1000000007 #define print(cs) printf("Case %lld: ",cs) #define Yesno(ok) if(ok)puts("YES");else puts("NO") #define yesno(ok) if(ok)puts("Yes");else puts("No") int main() { ll test=1LL; in(test); while(test--) { ll i,j,k,l,n,m,r,u,v; in(n); ll arr[n+5],prop[n+5]; for(i=1;i<=n;i++)in(arr[i]); ll ans=0; ms(prop,0LL); for(i=1;i<=n;i++) { prop[i]+=prop[i-1]; v = min( arr[i]-1 , prop[i] ); // cout<<v;endl; if(v&&arr[i]-v+1+i<=n) { prop[max(i+2,i+arr[i]-v+1)]++; prop[min(i+arr[i],n)+1]--; // cout<<max(i+2,min(i+arr[i]-v+1,n))<<" "<<min(i+arr[i],n)+1;endl; } arr[i]-=v; if(arr[i]==1) { prop[i+1]+=prop[i]-v; prop[i+2]-=prop[i]-v; continue; } ans+=arr[i]-1; // CHECK(ans); prop[i+2]++; prop[min(i+arr[i],n)+1]--; } out(ans);endl; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; template <typename T> void read(T &x) { int p = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') p = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); } x *= p; } int vis[101]; struct asd { int x, y; } a[101]; bool comp(asd q, asd p) { return q.x < p.x; } int main() { int n, sum = 0, ans = 1, tot = 0; read(n); for (int i = 1; i <= n; i++) read(a[i].x), sum += a[i].x, a[i].y = i; sort(a + 2, a + n + 1, comp); vis[1] = 1; tot = a[1].x; for (int i = 2; i <= n; i++) if (a[1].x >= a[i].x * 2) ++ans, vis[i] = true, tot += a[i].x; if (tot < (sum / 2 + 1)) { puts("0"); return 0; } if (ans == n) { cout << ans - 1 << "\n"; cout << "1 "; for (int i = 3; i <= n; i++) cout << i << " "; return 0; } cout << ans << "\n"; for (int i = 1; i <= n; i++) if (vis[i]) cout << a[i].y << " "; return 0; }
1
#include <bits/stdc++.h> using namespace std; int ar[505]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> ar[i]; } int ans = 0; for (int i = 2; i <= n; i++) { if (ar[i] + ar[i - 1] < k) { int add = k - (ar[i] + ar[i - 1]); ans += add; ar[i] += add; } } cout << ans << "\n"; for (int i = 1; i <= n; i++) cout << ar[i] << " "; puts(""); }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int prime = 999983; const int INF = 0x3f3f3f3f; const long long INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-8; const long long mod = (long long)1e9 + 7; const unsigned long long mx = 133333331; const int ma = 1e9; inline void RI(int &x) { char c; while ((c = getchar()) < '0' || c > '9') ; x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; } long long a[200010], b[200010]; int main() { long long n, k; cin >> n >> k; for (int i = 0; i <= n; i++) { cin >> a[i]; b[i] = a[i]; } for (int i = 1; i <= n; i++) { a[i] += (a[i - 1] / 2); a[i - 1] %= 2; if (a[i - 1] < 0) { a[i - 1] += 2; a[i]--; } } int beg = 0; for (int i = 0; i <= n; i++) { if (a[i]) { beg = i; break; } } long long sum = a[n]; int ans = 0; for (int i = n; i >= 0; i--) { if (i > beg) { if (i == n) continue; sum = sum * 2 + a[i]; if (abs(sum) >= 4 * k) break; } else { if (i != n) sum = 2 * sum + a[i]; if ((abs(b[i] - sum) <= k && i != n) || (i == n && a[n] != b[n])) { ans++; } if (abs(sum) >= 4 * k) { break; } } } cout << ans << endl; return 0; }
4
#include <cstdio> #include <algorithm> using namespace std; int g[300000],h[300000]; int i,n; int main() { scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&g[i]); for (i=1;i<=n;i++) h[i]=g[i]; sort(h+1,h+n+1); for (i=1;i<=n;i++) if (g[i]<=h[n/2]) printf("%d\n",h[n/2+1]); else printf("%d\n",h[n/2]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int H,R; cin >> H >> R; if (H + R == 0) { cout << 0 << endl; } else if (H + R > 0) { cout << 1 << endl; } else { cout << -1 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; double ex[2][100005]; double acc[100005]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; ex[1][0] = m - 1; acc[0] = 0; acc[1] = m - 1; for (int j = 2; j <= n * m + 1; ++j) acc[j] = ex[1][j - 1] + acc[j - 1]; int score = 0; bool b; for (int i = 0; i < n; ++i) { b = i & 1; int x; cin >> x; score += x; for (int j = n * m; j >= 0; --j) { ex[b][j] = (acc[j] - acc[max(0, j - m)]) / (m - 1); if (j >= x) ex[b][j] -= ex[!b][j - x] / (m - 1); } acc[1] = ex[b][0]; for (int j = 2; j <= n * m + 1; ++j) acc[j] = ex[b][j - 1] + acc[j - 1]; memset(ex[!b], 0, sizeof ex[!b]); } double sm = accumulate(ex[b] + n, ex[b] + score, 1.0); cout << fixed << setprecision(10) << sm; return 0; }
3
#include <bits/stdc++.h> const int maxn = 1e5 + 5; using namespace std; int k, ans; char a[maxn]; int A[maxn]; int powmod(int a, long long b) { int tmp = 1; while (b) { if (b & 1) tmp = (long long)tmp * a % 1000000007; a = (long long)a * a % 1000000007; b >>= 1; } return tmp; } int main() { while (cin >> a >> k) { int len = strlen(a), ans = 0; int j = 0; for (int i = 0; i < len; i++) if (a[i] == '5' || a[i] == '0') A[j++] = i; int a1 = 0; for (int i = 0; i < j; i++) { a1 = (long long)(a1 + powmod(2, A[i])) % 1000000007; } int Q2 = powmod(2, len); int Q1 = (powmod(Q2, k) - 1 + 1000000007) % 1000000007; Q2 = (Q2 - 1 + 1000000007) % 1000000007; Q1 = (long long)Q1 * powmod(Q2, 1000000007 - 2) % 1000000007; ans = (long long)a1 * Q1 % 1000000007; cout << ans << endl; } return 0; }
3
#include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; string f; getline(cin,f); while(n--) { string s; getline(cin,s); while(1) { int i=s.find("Hoshino"); if(i==-1) break; s=s.substr(0,i)+"Hoshina"+s.substr(i+7); } cout << s << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int maxm = 1e6 + 10; const int maxc = 26; const int inf = 0x3f3f3f3f; const long long mod = 1e9 + 7; const long long INF = 0x3f3f3f3f3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); } return f * x; } long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long poww(long long x, long long y, long long p) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % p; x = x * x % p; y >>= 1; } return ans; } bool is_prime(long long x) { if (x == 1) return 0; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) return 0; } return 1; } int t; long long a, b, p; string s; vector<pair<int, int> > pos; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { pos.clear(); cin >> a >> b >> p >> s; int ans = 1; int bus = 0, tram = 0; int now = s[0] - 'A'; if (!now) bus++; else tram++; pos.push_back(make_pair(0, now)); for (int i = 1; i < s.size(); i++) { int cur = s[i] - 'A'; if (i == s.size() - 1) { pos.push_back(make_pair(i, -1)); continue; } if (cur != now) { if (!cur) bus++; else tram++; now = cur; pos.push_back(make_pair(i, now)); } } for (int i = 0; i < pos.size(); i++) { int id = pos[i].first, k = pos[i].second; if (a * bus + b * tram <= p) { cout << pos[i].first + 1 << endl; break; } else { if (!k) bus--; else tram--; } } } }
2
#include<bits/stdc++.h> using namespace std; int A,B,C; int main() { cin>>A>>B>>C; if(B-A==C-B)cout<<"YES\n"; else cout<<"NO\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<int> st; bool vis[10000]; vector<string> vec; bool cmp(string &a, string &b) { return a.size() < b.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { string x; cin >> x; vec.push_back(x); } sort(vec.begin(), vec.end(), cmp); for (int i = n - 1; i >= 1; i--) { string f = vec[i]; string s = vec[i - 1]; if (f.find(s) != std::string::npos) { continue; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 0; i < vec.size(); i++) { cout << vec[i] << endl; } return 0; }
2
#include <bits/stdc++.h> long long S, N, K, x, F[2005], W[2005]; int main() { scanf("%I64d%I64d", &N, &K); for (int i = 1; i <= N; i++) for (int j = i + 1; j <= N; j++) { scanf("%I64d", &x); if (x >= 0) { F[i] += x, F[j] += x, W[i]++, W[j]++; } } for (int i = 1; i <= N; i++) S += K == 2 ? F[i] * (W[i] - 1) : F[i]; printf("%I64d", K == 2 ? 2 * S / N / (N - 1) : S / N); }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 610000; char A[maxn]; int main() { scanf("%s", A); int n = strlen(A); vector<pair<int, int> > vec; for (int i = 0; i < n; ++i) { int ch = A[i]; if (vec.empty() || ch != vec.back().first) vec.emplace_back(ch, 1); else vec.back().second += 1; } int m = vec.size() / 2; if (vec.size() % 2 == 0) { puts("0"); return 0; } if (vec[m].second < 2) { puts("0"); return 0; } bool ok = true; for (int i = 1; i <= m; ++i) { if (vec[m + i].first != vec[m - i].first) ok = false; if (vec[m + i].second + vec[m - i].second < 3) { ok = false; } } if (!ok) { puts("0"); return 0; } printf("%d\n", vec[m].second + 1); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, k; cin >> n; long d[8] = {0}, c; for (i = 1; i < 8; i++) { d[i] = 1000000; } for (i = 0; i < n; i++) { int s = 0; string st; cin >> c >> st; for (j = 0; j < st.length(); j++) s |= (1 << (st[j] - 'A')); for (j = 0; j < 8; j++) d[s | j] = min(d[s | j], d[j] + c); } if (d[7] >= 1000000) cout << "-1"; else cout << d[7]; }
2
#include <bits/stdc++.h> using namespace std; const long long int INF = 100000000000000000; const long long int MAXN = 1100; long long int a[MAXN][MAXN]; vector<long long int> col, row; multiset<long long int> cc, rr; multiset<long long int>::iterator itlow; long long int r[MAXN * MAXN], c[MAXN * MAXN]; int main() { long long int n, m, k, p; cin >> n >> m >> k >> p; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { cin >> a[i][j]; } } for (long long int i = 0; i < n; i++) { long long int sum = 0ll; for (long long int j = 0; j < m; j++) { sum += a[i][j]; } rr.insert(sum); } for (int i = 0; i <= 1000000; i++) { itlow = rr.end(); itlow--; long long int here = *itlow; rr.erase(itlow); row.push_back(here); here -= p * m; rr.insert(here); } sort(row.begin(), row.end()); reverse(row.begin(), row.end()); r[0] = 0ll; r[1] = row[0]; for (long long int i = 1; i < row.size(); i++) { r[i + 1] = row[i] + r[i]; } for (long long int j = 0; j < m; j++) { long long int sum = 0ll; for (long long int i = 0; i < n; i++) { sum += a[i][j]; } cc.insert(sum); } for (int i = 0; i <= 1000000; i++) { itlow = cc.end(); itlow--; long long int here = *itlow; cc.erase(itlow); col.push_back(here); here -= p * n; cc.insert(here); } sort(col.begin(), col.end()); reverse(col.begin(), col.end()); c[0] = 0ll; c[1] = col[0]; for (long long int i = 1; i < col.size(); i++) { c[i + 1] = col[i] + c[i]; } long long int answer = -INF; for (long long int i = 0; i <= k; i++) { long long int cur = 0ll; cur += r[i]; cur += c[k - i] - (p * i * (k - i)); answer = max(answer, cur); } cout << answer << endl; }
2
#include<cstdio> #define min(a,b) (a<b?a:b) long long n,k; int main(){ scanf("%lld%lld",&n,&k); printf("%lld",min(n%k,k-n%k)); }
0
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using vpii = vector<pii>; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<long long>; template <class T> istream &operator>>(istream &, vector<T> &); template <class T> ostream &operator<<(ostream &, const vector<T> &); template <class T, class U> istream &operator>>(istream &, pair<T, U> &); template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &); constexpr int INF = 0x3f3f3f3f; constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL; int solve() { int n; cin >> n; vector<ll> s(n), t(n); cin >> s >> t; vector<ll> ps(n), pt(n); iota(ps.begin(), ps.end(), 0); iota(pt.begin(), pt.end(), 0); sort(ps.begin(), ps.end(), [&](int l, int r) { return s[l] < s[r]; }); sort(pt.begin(), pt.end(), [&](int l, int r) { return t[l] < t[r]; }); vector<pair<pii, ll>> ans; vll fix(n); for (int i = 0; i < n; i++) fix[ps[i]] = t[pt[i]] - s[ps[i]]; int r = 0; for (int l = 0; l < n; l++) { if (fix[ps[l]] < 0) { cout << "NO" << endl; return 0; } while (r <= l) r++; while (r < n && fix[ps[l]] > 0) { ll lpos = s[ps[l]]; ll rpos = s[ps[r]]; if (lpos <= rpos) { ll can = (rpos - lpos) / 2; ll gogo = min(fix[ps[l]], can); s[ps[l]] += gogo; fix[ps[l]] -= gogo; s[ps[r]] -= gogo; fix[ps[r]] += gogo; if (gogo > 0) ans.emplace_back(pii(ps[l], ps[r]), gogo); else r++; } else { r++; } } if (fix[ps[l]] > 0) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; cout << ans.size() << endl; for (auto [l, r] : ans) cout << l.first + 1 << ' ' << l.second + 1 << ' ' << r << '\n'; return 0; } int main() { ios::sync_with_stdio(0); solve(); return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto it = v.begin(); it != v.end(); ++it) is >> *it; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end();) os << *it, os << " \n"[++it == v.end()]; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T, class U> inline void Max(T &a, U b) { if (a < b) a = b; } template <class T, class U> inline void Min(T &a, U b) { if (a > b) a = b; } inline void add(int &a, int b) { a += b; while (a >= 998244353) a -= 998244353; } int pow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = ans * (long long)a % 998244353; a = (long long)a * a % 998244353; b >>= 1; } return ans; } char s[1000010]; vector<long long> f[2]; int p[1000010][2]; int main() { int T, i, j, k, ca = 0, n, m = 0, K; scanf("%d%s", &n, s + 1); f[1].push_back(0); f[0].push_back(0); for (int i = 1; i < n + 1; i++) { k = s[i] == 'U'; f[k].push_back(i); for (int j = 0; j < 2; j++) p[i][j] = (int)(f[j].size()) - 1; } for (int k = 0; k < 2; k++) { for (int j = 1; j < f[k].size(); j++) { f[k][j] += f[k][j - 1]; } } long long ans; for (int i = 1; i < n + 1; i++) { if (s[i] == 'U') { int first = p[i][1], second = p[n][0] - p[i][0]; if (second == 0) { ans = n + 1 - i; } else { k = min(first, second); j = p[i][0]; ans = 2 * (f[0][j + k] - f[0][j]) - 2 * (f[1][first] - f[1][first - k]) + i; if (first > k) ans += n + 1 - 2 * (f[1][first - k] - f[1][first - k - 1]); } } else { int first = p[i][1], second = p[n][0] - p[i - 1][0]; if (first == 0) { ans = i; } else { k = min(first, second); j = p[i - 1][0]; ans = 2 * (f[0][j + k] - f[0][j]) - 2 * (f[1][first] - f[1][first - k]) - i; if (second == k) ans += n + 1; else ans += 2 * (f[0][j + k + 1] - f[0][j + k]); } } printf("%I64d ", ans); } return 0; }
5
#include<bits/stdc++.h> using namespace std; int f[1000005][4],inv6,n,g[1000005][4]; const int mod=998244353; const int inv2=(mod+1)/2; int M(int x){return x>=mod?x-mod:x;} int mypow(int a,int b) { int ans=1; while(b) { if(b&1)ans=1ll*ans*a%mod; a=1ll*a*a%mod; b>>=1; } return ans; } int main() { g[0][0]=1; f[0][0]=1; inv6=mypow(6,mod-2); for(int i=1;i<=1000000;i++) { f[i][0]=1; f[i][1]=M(M(f[i-1][0]+f[i-1][1])+f[i-1][2]); f[i][2]=1ll*f[i][1]*(f[i][1]+1)%mod*inv2%mod; f[i][3]=1ll*f[i][1]*(f[i][1]-1+mod)%mod*(f[i][1]-2+mod)%mod*inv6%mod; f[i][3]=(1ll*f[i][3]+1ll*f[i][1]*(f[i][1]-1+mod)%mod+1ll*f[i][1])%mod; for(int j=0;j<=3;j++)g[i][j]=M(mod+f[i][j]-f[i-1][j]); } //for(int i=0;i<=2;i++)for(int j=0;j<=3;j++)printf("%d %d %d %d\n",i,j,f[i][j],g[i][j]); scanf("%d",&n); int ans=0; for(int i=2;i<=3;i++)ans=M(ans+g[n][i]); ans=2ll*ans%mod; for(int i=1;i<=n;i++)ans=(1ll*ans+1ll*g[i][2]*g[n-i][1])%mod; ans=M(ans+g[n][1]); printf("%d\n",ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; void parr(int arr[], int n, string name) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << "\n"; } long long int fn(int arr[], int n) { int i; long long int pre[n]; sort(arr, arr + n, greater<int>()); pre[0] = arr[0]; for (i = 1; i < n; i++) pre[i] = pre[i - 1] + arr[i]; cout << "\n"; long long int ans = 0; for (i = 0; i < n; i++) ans += pre[i]; for (i = 1; i < n; i++) ans += arr[i]; return ans; } int main() { int n; cin >> n; int arr[n]; int i; for (i = 0; i < n; i++) cin >> arr[i]; long long int ans = fn(arr, n); cout << ans; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int n, i, j, p[3000010]; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) p[i] = i; for (i = 2; i <= n; i++) { int cur = p[i - 1], ncur; for (j = i - 1; j <= n + i - 2; j += i) { ncur = p[j + i]; if (j + i <= n + i - 2) p[j + i] = cur; else p[n + i - 1] = cur; cur = ncur; } } for (i = 1; i <= n; i++) printf("%d ", p[n - 1 + i]); putchar('\n'); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long maxn = 3e5 + 100; const long long mod = 1e9 + 7; const long long base = 1e15; vector<long long> adj[maxn]; vector<long long> adj1[maxn]; bool dd[maxn]; long long siz[maxn]; long long cntnw = 0; long long dinh; long long anc[maxn][20]; long long dep[maxn]; long long ans[maxn]; void dfs1(long long u, long long par) { cntnw++; siz[u] = 1; for (auto to : adj[u]) { if (dd[to] || to == par) continue; dfs1(to, u); siz[u] += siz[to]; } } void dfs2(long long u, long long par) { long long mx = cntnw - siz[u]; for (auto to : adj[u]) { if (dd[to] || to == par) continue; dfs2(to, u); mx = max(mx, siz[to]); } if (mx <= cntnw / 2) { dinh = u; } } long long par[maxn]; long long dosth(long long u) { cntnw = 0; dfs1(u, -1); dfs2(u, -1); if (cntnw == 1) { return dinh; } dd[dinh] = 1; long long p = dinh; for (auto to : adj[dinh]) { if (dd[to]) continue; long long h = dosth(to); par[h] = p; } return p; } void dfs(long long u, long long par) { anc[u][0] = par; for (int i = 1; i <= 18; i++) { anc[u][i] = anc[anc[u][i - 1]][i - 1]; } for (auto to : adj[u]) { if (to == par) continue; dep[to] = dep[u] + 1; dfs(to, u); } } long long lca(long long x, long long y) { if (dep[x] < dep[y]) swap(x, y); long long kc = dep[x] - dep[y]; for (int i = 18; i >= 0; i--) { if (kc & (1ll << i)) { x = anc[x][i]; } } if (x == y) return x; for (int i = 18; i >= 0; i--) { if (anc[x][i] != anc[y][i]) { x = anc[x][i]; y = anc[y][i]; } } return anc[x][0]; } long long kc(long long x, long long y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; } void update(long long u, long long rt) { ans[u] = min(ans[u], kc(u, rt)); if (par[u] == 0) return; update(par[u], rt); } long long get(long long u, long long rt) { if (par[u] == 0) return (ans[u] + kc(u, rt)); return min(ans[u] + kc(u, rt), get(par[u], rt)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("", "r")) { freopen(" ", "r", stdin); freopen(" ", "w", stdout); } long long n; cin >> n; long long q; cin >> q; for (int i = 1; i <= n - 1; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } dfs(1, 0); dosth(1); for (int i = 1; i <= n; i++) { ans[i] = 1e15; } update(1, 1); while (q--) { long long t; cin >> t; if (t == 1) { long long v; cin >> v; update(v, v); } else { long long x; cin >> x; cout << get(x, x) << "\n"; } } }
5
#include <bits/stdc++.h> using namespace std; int a[105], b[105]; int main() { string str, str1; int n, x[105], y[105], i; char ch[105], ch1[105]; cin >> str >> str1; cin >> n; for (i = 1; i <= n; i++) { cin >> x[i] >> ch[i] >> y[i] >> ch1[i]; } for (i = 1; i <= n; i++) { if (ch[i] == 'h') { if (ch1[i] == 'y') { a[y[i]]++; } else a[y[i]] += 2; if (a[y[i]] >= 2) { cout << str << " " << y[i] << " " << x[i] << endl; a[y[i]] = -1000; } } else { if (ch1[i] == 'y') { b[y[i]]++; } else b[y[i]] += 2; if (b[y[i]] >= 2) { cout << str1 << " " << y[i] << " " << x[i] << endl; b[y[i]] = -1000; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, s, x, ans = 0x3f3f3f3f; int v[100010], nx[100010]; std::default_random_engine generator(time(NULL)); std::uniform_int_distribution<int> dis(0, 2e9); auto dice = std::bind(dis, generator); void Init() { scanf("%d %d %d", &n, &s, &x); } void Work() { int i, j, l; for (i = 1; i <= 500; ++i) { j = i == 1 ? s : nx[l]; if (j == -1) return; printf("? %d\n", j); fflush(stdout); scanf("%d %d", &v[j], &nx[j]); if (v[j] >= x) ans = min(ans, v[j]); l = j; } srand(time(NULL)); for (; i <= 1500; ++i) { j = dice() % n + 1; printf("? %d\n", j); fflush(stdout); scanf("%d %d", &v[j], &nx[j]); if (v[j] >= x) ans = min(ans, v[j]); else if (v[j] > v[l]) l = j; } for (; i <= 1990; ++i) { j = nx[l]; if (j == -1) return; printf("? %d\n", j); fflush(stdout); scanf("%d %d", &v[j], &nx[j]); if (v[j] >= x) ans = min(ans, v[j]); l = j; } } int main() { Init(); Work(); printf("! %d\n", ans == 0x3f3f3f3f ? -1 : ans); return 0; }
2
#include <iostream> #include <cmath> using namespace std; void koch(double x0, double y0, double x1, double y1, int n) { if (n == 0) { cout << x0 << " " << y0 << endl; } else { double c1x,c1y,c2x,c2y,c3x,c3y; double dx = x1 - x0; double dy = y1 - y0; //double L = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); c1x = x0 + dx / 3; c1y = y0 + dy / 3; c2x = (x0 + x1) / 2 - (dy * sqrt(3) / 6); c2y = (y0 + y1) / 2 + (dx * sqrt(3) / 6); c3x = x0 + dx * 2 / 3; c3y = y0 + dy * 2 / 3; koch(x0,y0,c1x,c1y,n-1); koch(c1x,c1y,c2x,c2y,n-1); koch(c2x,c2y,c3x,c3y,n-1); koch(c3x,c3y,x1,y1,n-1); } } int main() { int n; cin >> n; koch(0,0,100,0,n); cout << 100 << " " << 0 << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ans; } long long inverse(long long a) { return qpow(a, MOD - 2) % MOD; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int extend_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } else { int r = extend_gcd(b, a % b, y, x); y -= x * (a / b); return r; } } int main() { long long x1, x2, y1, y2, dy, ans; cin >> x1 >> y1 >> x2 >> y2; ans = ((y2 - y1) / 2) * ((x2 - x1) / 2) + ((x2 - x1) / 2 + 1) * ((y2 - y1) / 2 + 1); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll N = 2e6; vector<ll> g[N]; vector<ll> e[N]; ll col[N]; vector<ll> comp[N]; ll sum[N]; void dfs(ll v) { comp[col[v]].push_back(v + 1); sum[col[v]] += v + 1; for (ll u : g[v]) { if (col[u] == -1) { col[u] = col[v] ^ 1; dfs(u); } } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); ll n; cin >> n; if (n % 2 == 0) { cout << "First" << endl; for (ll i = 0; i < 2 * n; ++i) { cout << i % n + 1 << " "; } cout << endl; } else { cout << "Second" << endl; for (ll x, i = 0; i < 2 * n; ++i) { cin >> x; e[--x].push_back(i); } for (ll i = 0; i < n; ++i) { assert(e[i].size() == 2); ll v = e[i][0], u = e[i][1]; g[v].push_back(u); g[u].push_back(v); g[i].push_back(i + n); g[i + n].push_back(i); } fill_n(col, N, -1); for (ll i = 0; i < 2 * n; ++i) { if (col[i] == -1) { col[i] = 0; dfs(i); } } if (sum[0] % (2 * n)) { swap(sum[0], sum[1]); swap(comp[0], comp[1]); } for (ll i : comp[0]) { cout << i << " "; } cout << endl; } return 0; }
4