solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int M = 1e3 + 5; const int INF = 1e9 + 5; const int mod = 1e9 + 7; int n, m, a[N], dp[M]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; if (n > m) { cout << "YES\n"; return 0; } for (int i = 1; i <= n; i++) { vector<int> tmp(m); tmp[a[i] % m] = 1; for (int j = 0; j < m; j++) { tmp[j] |= dp[j]; tmp[(j + a[i]) % m] |= dp[j]; } for (int j = 0; j < m; j++) dp[j] |= tmp[j]; } cout << (dp[0] ? "YES\n" : "NO\n"); }
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n + 1, 0); int kl = 0, ans = 0; for (int i = 0; i < 2 * n; i++) { int x; cin >> x; if (a[x] > 0) { a[x]--; kl--; } else { a[x]++; kl++; } ans = max(ans, kl); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, z; cin >> n >> k; string s; cin >> s; for (int i = 0; i < n; i++) if (s[i] == 'G') x = i; else if (s[i] == 'T') z = i; if (z < x && (x - z) % k == 0) for (int j = x; j > z;) { j -= k; if (s[j] == '#') { cout << "NO\n"; exit(0); } if (s[j] == '.') continue; } else if (x < z && (z - x) % k == 0) for (int j = x; j < z;) { j += k; if (s[j] == '#') { cout << "NO\n"; exit(0); } if (s[j] == '.') continue; } else { cout << "NO\n"; exit(0); } cout << "YES\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; int a[maxn], b[maxn]; int toggle[maxn]; bool on[maxn]; int main(void) { int n; while (cin >> n) { string s; cin >> s; queue<int> q; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; toggle[i] = b[i]; if (s[i - 1] == '1') { on[i] = true; } else { on[i] = false; } } int res = 0; for (int t = 0; t <= 200; t++) { int cur = 0; for (int i = 1; i <= n; i++) { if (t == toggle[i]) { on[i] = 1 - on[i]; cur += on[i]; toggle[i] += a[i]; } else { cur += on[i]; } } res = max(res, cur); } cout << res << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; inline long long pow(long long a, long long b, long long mo) { if (!a) return 0; long long tmp = 1; for (; b; b >>= 1) { if (b & 1) tmp = (long long)tmp * a % mo; a = (long long)a * a % mo; } return tmp; } template <class T> inline void R(T &xx) { xx = 0; char ch = getchar(); bool F = 0; while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') F = 1, ch = getchar(); while (ch >= '0' && ch <= '9') xx = xx + xx + (xx << 3) + ch - 48, ch = getchar(); if (F) xx = -xx; } long long n, m; bool che(long long k) { long long s = 1, C = 1, lim = k * m; for (int i = (1); i <= (k); i++) { C = C * (k - i + 1) / i; if (C * i <= lim) lim -= C * i, s += C; else { s += lim / i; break; } if (s >= n) return 1; } return s >= n; } int main() { int _; R(_); while (_--) { R(n), R(m); long long l = 0, r = n, ans = 0, md; while (l <= r) { md = l + r >> 1; if (che(md)) r = md - 1, ans = md; else l = md + 1; } cout << ans << endl; } return 0; }
18
#include <bits/stdc++.h> using namespace std; int n, a[1000000], num[1000000], ban[1000000], tot, now, ret; int ac() { scanf("%d", &n), ret = n; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); if (n == 1) return 0; for (int i = 1; i <= n; ++i) { if (a[i] < 0) num[++tot] = i; else if (a[i] == 0) { if (now == 0) now = i, ban[now] = 1; else printf("1 %d %d\n", i, now), ban[i] = 1, ret--; } } if (tot & 1) { int hh, hhh = -1000000001; for (int i = 1; i <= tot; ++i) if (hhh < a[num[i]]) hhh = a[num[i]], hh = i; if (now) printf("1 %d %d\n", num[hh], now), ban[num[hh]] = 1, ret--; else printf("2 %d\n", num[hh]), ban[num[hh]] = 1, ret--; } if (now) { if (ret > 1) printf("2 %d\n", now); else return 0; } now = 0; for (int i = 1; i <= n; ++i) { if (ban[i]) continue; if (!now) now = i; else printf("1 %d %d\n", i, now); } return 0; } int main() { ac(); }
9
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long linf = 1ll * inf * inf; const int N = 300000 + 7; const int M = 19; const int multipleTest = 0; struct RMQ { int a[N]; int RMQ[N][20]; int n; int& operator[](int pos) { return a[pos]; } void build() { for (int i = (1), _b = (n + 1); i < _b; ++i) { RMQ[i][0] = a[i]; } for (int j = (1), _b = (20); j < _b; ++j) { int lt = 1 << j; int half = lt >> 1; for (int i = 1; i + lt - 1 <= n; ++i) { RMQ[i][j] = min(RMQ[i][j - 1], RMQ[i + half][j - 1]); } } } int get(int u, int v) { int k = log2(v - u + 1); return min(RMQ[u][k], RMQ[v - (1 << k) + 1][k]); } } rmq1, rmq2, rmq3; int n; int a[N]; int mx_left[N]; int get_left(int r) { int lo = 1, hi = r - 1, res = -1; while (lo <= hi) { int g = (lo + hi) >> 1; if (mx_left[g] > r) { res = g; hi = g - 1; } else lo = g + 1; } return res; } int get_right(int r) { int lo = r, hi = n, res = -1; while (lo <= hi) { int g = (lo + hi) >> 1; if (rmq1.get(r, g) < r) { res = g; hi = g - 1; } else lo = g + 1; } return res; } int calc(int r) { int j = get_left(r); if (j == -1) j = get_right(r); if (j == -1) return inf; int rhs = inf; if (j <= n) rhs = min(rhs, rmq2.get(max(r, j), n) - r); if (j < r) rhs = min(rhs, rmq3.get(j, r - 1) + r); int lhs = inf; --j; if (j > 0) { lhs = min(lhs, rmq3.get(1, min(j, r - 1)) + r); if (j >= r) lhs = min(lhs, rmq2.get(r, j) - r); } return max(lhs, rhs); } void solve() { cin >> n; rmq1.n = rmq2.n = rmq3.n = n; for (int i = (1), _b = (n + 1); i < _b; ++i) { scanf("%d", a + i); rmq1[i] = a[i]; rmq2[i] = a[i] + i; rmq3[i] = -i + a[i]; mx_left[i] = max(mx_left[i - 1], 2 * i - a[i]); } rmq1.build(); rmq2.build(); rmq3.build(); int res = inf; for (int i = (1), _b = (n + 1); i < _b; ++i) res = min(res, calc(i)); if (res >= inf) res = -1; cout << res; } int main() { int Test = 1; if (multipleTest) { cin >> Test; } for (int i = 0; i < Test; ++i) { solve(); } }
21
#include <bits/stdc++.h> using namespace std; const int inf_int = 1e9 + 100; const long long inf_ll = 1e18; const double pi = 3.1415926535898; template <class T1, class T2> std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) { out << "( " << rhs.first << " , " << rhs.second << " )"; return out; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A> string to_string(vector<vector<A>> v) { bool first = true; string res = "\n{"; for (const auto &x : v) { if (!first) { res += ",\n "; } first = false; res += to_string(x); } res += "}\n"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } bool debug = 0; const int MAXN = 1 << 18; const int LOG = 20; const int mod = 998244353; const int MX = (2e4 + 100); int parent[MAXN]; int asdasfsaf[MAXN]; int arr[64]; int top = 0; int get_parent(int v) { top = 0; while (parent[v] != v) { arr[top++] = v; v = parent[v]; } int res = v; for (int i = 0; i < top; ++i) { parent[arr[i]] = res; } return res; } bool union_set(int a, int b) { a = get_parent(a); b = get_parent(b); if (a != b) { if (asdasfsaf[a] < asdasfsaf[b]) swap(a, b); else if (asdasfsaf[a] == asdasfsaf[b]) ++asdasfsaf[a]; parent[a] = b; return true; } return false; } int cnt[MAXN]; bool used[MAXN]; void solve() { int n; cin >> n; long long ans = 0; for (int i = 1; i <= n; ++i) { int x; cin >> x; cnt[x]++; ans -= x; } cnt[0]++; for (int i = 0; i < (1 << 18); ++i) { parent[i] = i; } for (int mask = (1 << 18) - 1; mask >= 0; --mask) { for (int sub = mask;; sub = (sub - 1) & mask) { int u = sub; int v = mask ^ sub; if (cnt[u] && cnt[v]) { if (union_set(u, v)) { int val; if (used[u] && used[v]) { val = 1; } else if (used[u]) { val = cnt[v]; } else if (used[v]) { val = cnt[u]; } else { val = cnt[v] + cnt[u] - 1; } ans += 1ll * val * mask; } used[u] = used[v] = true; } if (sub == 0) break; } } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.precision(15); int t = 1; while (t--) solve(); 42; }
27
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const int mod = 1e9 + 7; class Solver { int n; vector<int> a; vector<int> g; public: 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 / gcd(a, b)) * b; } void solve() { cin >> n; a.assign(n, 0); g.assign(n, 0); for (auto &i : a) cin >> i; for (int i = n - 1; i >= 0; i--) { g[i] = a[i]; if (i + 1 < n) { g[i] = gcd(g[i], g[i + 1]); } } long long res = -1; for (int i = 0; i < n - 1; i++) { if (res == -1) { res = lcm(a[i], g[i + 1]); } else { res = gcd(res, lcm(a[i], g[i + 1])); } } cout << res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); Solver s; s.solve(); }
8
#include <bits/stdc++.h> using namespace std; const int MAX = 66666; const int maxn = 105; int f[maxn][5]; int a[maxn]; int main() { int n; scanf("%d", &n); memset(f, 0, sizeof f); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { f[i][0] = min(f[i - 1][0] + 1, f[i - 1][1] + 1); f[i][0] = min(f[i][0], f[i - 1][2] + 1); if (a[i] == 1 || a[i] == 3) f[i][1] = min(f[i - 1][0], f[i - 1][2]); else f[i][1] = MAX; if (a[i] == 2 || a[i] == 3) f[i][2] = min(f[i - 1][0], f[i - 1][1]); else f[i][2] = MAX; } int ans = min(f[n][0], f[n][1]); ans = min(ans, f[n][2]); printf("%d", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long base = 31LL; const long long mod = 1000000000009LL; long long h(char *x) { long long hash = 0; int len = strlen(x); for (int e = 0; e < len; e++) hash = (hash * base + (tolower(x[len - e - 1]) - 'a' + 1)) % mod; return hash; } char text[10100], aux[10100]; string alp[10100 * 10]; map<long long, int> hashs; vector<int> ans; int dp[10100], n; int solve(int ind) { if (ind == n) return 1; long long hash = 0; for (int e = ind; e < n; e++) { hash = (hash * base + (text[e] - 'a' + 1)) % mod; if (hashs.count(hash)) { if (solve(e + 1) != 0) { ans.push_back(hashs[hash]); return 1; } } } return 0; } int main() { scanf("%d %s", &n, text); int m; scanf("%d", &m); for (int e = 0; e < m; e++) { scanf("%s", aux); for (int f = 0; aux[f]; f++) alp[e].push_back(aux[f]); hashs[h(aux)] = e; } solve(0); reverse(ans.begin(), ans.end()); for (auto it : ans) printf("%s ", alp[it].c_str()); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int vert, edge, u, v; scanf("%d %d", &vert, &edge); vector<int> Adj[vert + 1]; int Graph[vert + 1][vert + 1]; memset(Graph, 0, sizeof(Graph)); for (int i = 0; i < edge; i++) { scanf("%d %d", &u, &v); Graph[u][v] = 1; Adj[u].push_back(v); } int result = 0; for (int u = 1; u <= vert; u++) { for (int v = 1; v <= vert; v++) { if (u == v) continue; int rohmb = 0; int sz = Adj[v].size(); for (int itr = 0; itr < sz; itr++) { int nbr = Adj[v][itr]; if (nbr == u || nbr == v) continue; if (Graph[nbr][u] == 1) { rohmb++; } } result += (rohmb * (rohmb - 1) / 2); } } printf("%d\n", result); return 0; }
9
#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; } inline int fp(int i) { if (i == 2) return 2; if (i == 1) return 1; return !(i & 1); } int fi(int i) { if (i == 1) return 1; if (i == 2) return 0; if (i == 3) return 1; if (i == 4) return 2; if (i & 1) return 0; int s[3] = {1, 0, 0}; s[fi(i >> 1)] = 1; if (s[1]) return 2; else return 1; } int main() { int i, n, k, x; scanf("%d %d", &n, &k); k = k & 1; int tot = 0; for (i = 0; i < n; i++) { scanf("%d", &x); if (k) tot ^= fi(x); else tot ^= fp(x); } if (tot) puts("Kevin"); else puts("Nicky"); }
14
#include <bits/stdc++.h> using namespace std; struct DSU { int N, components; vector<int> par, s, val; DSU(int n) { N = n; components = n; par.resize(N + 1); s.resize(N + 1); s.assign(N + 1, 1); val.resize(N + 1); iota((par).begin(), (par).end(), 0); } int find_par(int node) { if (node == par[node]) return node; else return par[node] = find_par(par[node]); } void merge(int u, int v) { u = find_par(u); v = find_par(v); if (u == v) return; if (s[u] < s[v]) swap(u, v); par[v] = u; s[u] += s[v]; s[v] = 0; val[u] = max({val[u], val[v], val[u] / 2 + val[v] / 2 + 2}); components--; } }; int n, m, q, vis[300005], h[300005]; vector<int> a[300005]; vector<int> v; int bfs(int node) { v.clear(); queue<int> Q; Q.push(node); int ans = -1; while (!Q.empty()) { auto d = Q.front(); Q.pop(); vis[d] = 1; ans = d; v.push_back(d); for (auto i : a[d]) { if (!vis[i]) Q.push(i), h[i] = h[d] + 1; } } return ans; } void solve() { cin >> n >> m >> q; DSU obj(n); for (int i = 0; i < m; i++) { int first, second; cin >> first >> second; a[first].push_back(second); a[second].push_back(first); } for (int i = 1; i < n + 1; i++) { if (!vis[i]) { int d = bfs(i); for (auto j : v) vis[j] = 0, h[j] = 0; d = bfs(d); for (auto j : v) { obj.merge(j, i); } obj.val[obj.find_par(i)] = h[d] + 1; } } while (q--) { int type; cin >> type; if (type == 1) { int first; cin >> first; first = obj.find_par(first); cout << obj.val[first] - 1 << '\n'; } else { int first, second; cin >> first >> second; obj.merge(first, second); } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
13
#include <bits/stdc++.h> using namespace std; int n, k; long long sum; const long long inf = 1000000000000000000LL; int pos; int a[100010], d[100010]; inline long long solve0() { long long ans = -inf, s = 0; for (int i = n; i; --i) { s += a[i]; ans = max(ans, s - d[i]); } return max(0LL, ans); } long long f[100010]; inline long long solve1() { long long Mind = inf, Mina = inf, ans = 0; for (int i = 1; i < n; ++i) { Mind = min(Mind, (long long)d[i]); Mina = min(Mina, (long long)a[i]); ans += a[i]; f[i] = max(f[i - 1], ans - Mind); } ans -= Mind; if (a[n] > d[n]) ans += a[n] - d[n]; ans = max(ans, (long long)a[n] - d[n]); long long s = 0; for (int i = n; i > 1; --i) { s += a[i]; ans = max(ans, s - d[i] + f[i - 1]); } return max(0LL, max(ans, sum - Mina - d[1])); } inline long long solve2() { long long Mind = inf; for (int i = 1; i < n; ++i) Mind = min(Mind, (long long)d[i]); return max(0LL, max(sum - Mind, (long long)a[n] - d[n])); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sum += a[i]; pos = 1; for (int i = 1; i <= n; ++i) { scanf("%d", &d[i]); if (d[i] < d[pos]) pos = i; } if (!k) printf("%I64d", solve0()); else if (k == 1) printf("%I64d", solve1()); else printf("%I64d", solve2()); }
14
#include <bits/stdc++.h> using namespace std; #define ll long long int #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define lf(i, a, b) for(ll i=a;i<b;i++) #define lb(i, a, b) for(ll i=a;i>b;i--) #define pb push_back void solve(){ int n, o = 0, z = 0, flag = 0, ans = 1; cin>>n; string a, b; stack <int> s; cin>>a>>b; lf (i, 0, n){ if (a[i] == '1') o++; else z++; if (o == z) s.push(i); } lb (i, n, -1){ if (!flag){ if (a[i] == b[i]){ if (!s.empty() && s.top() == i) s.pop(); continue; } if (!s.empty() && s.top() == i){ flag = 1; s.pop(); continue; } ans = 0; break; } else{ if (a[i] != b[i]){ if (!s.empty() && s.top() == i) s.pop(); continue; } if (!s.empty() && s.top() == i){ flag = 0; s.pop(); continue; } ans = 0; break; } } if (ans) cout<<"YES"<<endl; else cout<<"NO"<<endl; } int main(){ fast; bool test_cases = true; if (test_cases){ int t; cin>>t; lf(i, 0, t) solve(); } else solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1e6 + 3; inline long long pow(long long a, long long b) { long long ans = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) ans = ans * a % mod; return ans; } long long farc[1000005]; inline void prepare(long long a) { farc[0] = 1; for (long long i = 1; i <= a; ++i) farc[i] = farc[i - 1] * i % mod; } inline long long Csmall(long long m, long long n) { if (n < m) return 0; return farc[n] * pow(farc[m], mod - 2) % mod * pow(farc[n - m], mod - 2) % mod; } inline long long C(long long m, long long n) { long long ans = 1; while (n && m && ans) { ans = (ans * Csmall(m % mod, n % mod)) % mod; n /= mod, m /= mod; } return ans; } int main() { long long m, n; cin >> m >> n; prepare(m + n); printf("%lld\n", C(m, m + n) - 1); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, SQ = 1500; int a[N], t[N], x[N], p[N], Cnt[2 * N][N / SQ + 5], T[N][N / SQ + 5], n; vector<int> num; void Build() { for (int i = 0; i < n; i++) { a[i] = lower_bound(num.begin(), num.end(), a[i]) - num.begin(); Cnt[a[i]][i / SQ + 1]++; } for (int i = 0; i < 2 * N; i++) for (int b = 1; b * SQ < n + SQ; b++) Cnt[i][b] += Cnt[i][b - 1]; int b = 1; while (b * SQ < n + SQ) { for (int i = 0; i < 2 * N; i++) T[Cnt[i][b]][b]++; for (int i = N - 2; i >= 0; i--) T[i][b] += T[i + 1][b]; b++; } } void Change(int x, int y, int B) { T[Cnt[x][B]][B]--, Cnt[x][B]--, Cnt[y][B]++, T[Cnt[y][B]][B]++; return; } int Count(int idx) { int x = a[idx - 1]; int res = Cnt[x][idx / SQ]; while (idx % SQ != 0) res += (a[--idx] == x); return res; } int Get(int idx, int c) { int _idx = idx; int res = T[c][idx / SQ]; while (idx % SQ != 0) { idx--; Cnt[a[idx]][idx / SQ]++; if (Cnt[a[idx]][idx / SQ] == c) res++; } idx = _idx; while (idx % SQ != 0) { idx--; Cnt[a[idx]][idx / SQ]--; } return res; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i], num.push_back(a[i]); int m; cin >> m; for (int i = 0; i < m; i++) { cin >> t[i] >> x[i] >> p[i], p[i]--; if (t[i] == 1) num.push_back(x[i]); } sort(num.begin(), num.end()), num.resize(unique(num.begin(), num.end()) - num.begin()); Build(); for (int i = 0; i < m; i++) { if (t[i] == 1) { int pos = p[i]; x[i] = lower_bound(num.begin(), num.end(), x[i]) - num.begin(); while (pos < n) Change(a[p[i]], x[i], pos / SQ + 1), pos += SQ; a[p[i]] = x[i]; continue; } if (x[i] == 1) { cout << num[a[p[i]]] << "\n"; continue; } int C = Count(p[i] + 1); if (x[i] % 2 == 0) { cout << C << "\n"; continue; } cout << Get(p[i] + 1, C) << "\n"; } return 0; }
23
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n + 1); vector<int> yes(n + 1), no(n + 1); int cntn = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > 0) { yes[a[i]]++; } else { cntn++; no[abs(a[i])]++; } } set<int> pod; for (int i = 1; i <= n; i++) { int tru = yes[i] + cntn - no[i]; if (tru == m) pod.insert(i); } int cnt = pod.size(); for (int i = 1; i <= n; i++) { if (a[i] > 0) { if (pod.find(a[i]) != pod.end()) { if (cnt == 1) cout << "Truth" << endl; else cout << "Not defined" << endl; } else cout << "Lie" << endl; } else { if (pod.find(abs(a[i])) != pod.end()) { if (cnt == 1) cout << "Lie" << endl; else cout << "Not defined" << endl; } else cout << "Truth" << endl; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 2; long long n, a[maxn], dem = 0, sum = 0; map<long long, int> m; void Enter() { cin >> n; int i = 1, j = 1; while (i <= 2 * n) { if (j <= n) { a[j] = i; a[j + n] = i + 1; j = j + n + 1; } else { a[j] = i; a[j - n] = i + 1; j = j - n + 1; } i += 2; } } void Solve() { for (int i = 2 * n + 1; i <= 3 * n; i++) a[i] = a[i - 2 * n]; for (int i = 1; i < n; i++) sum += a[i]; for (int i = n; i <= 3 * n; i++) { sum += a[i]; sum -= a[i - n]; if (m[sum] == 0) { dem++; m[sum] = 1; } } if (dem > 2) cout << "NO"; else { cout << "YES" << '\n'; for (int i = 1; i <= 2 * n; i++) cout << a[i] << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); Enter(); Solve(); }
4
#include <bits/stdc++.h> using namespace std; char local = '0'; void solve() { long long int n; cin >> n; long long int e[n]; for (long long int i = (long long int)0; i < (long long int)n; i++) cin >> e[i]; sort(e, e + n, greater<long long int>()); int end = n - 1; int size = 1; int count = 0; while (1) { if (e[end] <= size) { count++; size -= e[end]; size++; end--; } else { size++; end--; } if (end == -1) { break; } } cout << count << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; struct idt { int v, idx; }; idt a[300015]; bool cmp(idt a, idt b) { return a.v < b.v; } int main() { int n, x1, x2; cin >> n >> x1 >> x2; for (int i = 0; i < n; i++) { cin >> a[i].v; a[i].idx = i + 1; } sort(a, a + n, cmp); int data = n; bool ans = 0; vector<int> v1, v2; for (int i = 0; i < n; i++) { int awal = ceil((double)x1 / a[i].v); if (data > awal) { int data2 = data - awal; int akhir = ceil((double)x2 / a[i + awal].v); if (data2 >= akhir) { ans = 1; for (int j = i; j < i + awal; j++) v1.push_back(a[j].idx); for (int j = i + awal; j < i + awal + akhir; j++) v2.push_back(a[j].idx); break; } } awal = ceil((double)x2 / a[i].v); if (data > awal) { int data2 = data - awal; int akhir = ceil((double)x1 / a[i + awal].v); if (data2 >= akhir) { ans = 1; for (int j = i; j < i + awal; j++) v2.push_back(a[j].idx); for (int j = i + awal; j < i + awal + akhir; j++) v1.push_back(a[j].idx); break; } } data--; } if (ans) { cout << "Yes\n"; int l1 = v1.size(), l2 = v2.size(); cout << l1 << ' ' << l2 << "\n"; for (int i = 0; i < l1 - 1; i++) cout << v1[i] << ' '; cout << v1.back() << "\n"; for (int i = 0; i < l2 - 1; i++) cout << v2[i] << ' '; cout << v2.back() << "\n"; } else cout << "No\n"; }
9
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; const int INF = 0x3F3F3F3F; const int64_t LINF = 4611686018427387903; const int MOD = 1e9 + 7; void solve() { string a, b; int n; cin >> n >> a >> b; set<pair<char, char>> has; for (int i = 0; i < n; i++) { if (a[i] > b[i]) { cout << "-1\n"; return; } if (a[i] != b[i]) has.insert({a[i], b[i]}); } int ans = 0; while (has.size()) { auto x = *has.begin(); while (has.size() && has.begin()->first == x.first) { char c = has.begin()->second; if (c != x.second) has.insert({x.second, c}); has.erase(has.begin()); } ans++; } cout << ans << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; template <typename T> T In() { T x; cin >> x; return x; } string shorter(string S, string T) { return S.size() <= T.size() ? S : T; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int k2 = In<int>(), k3 = In<int>(), k5 = In<int>(), k6 = In<int>(), b = min(k2, min(k5, k6)); cout << b * 256 + min(k2 - b, k3) * 32; return cout << endl, 0; }
0
#include <bits/stdc++.h> using std ::max; using std ::min; template <class T> bool chkmin(T &x, const T &y) { return y < x ? x = y, true : false; } template <class T> bool chkmax(T &x, const T &y) { return y > x ? x = y, true : false; } template <class T> inline void read(T &in) { char ch; int f = 1; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (in = 0; isdigit(ch); ch = getchar()) in = in * 10 + ch - '0'; in *= f; } const int maxx = 300000 + 25; int x, y, z, k, num; int ans, tot; long long n, m, cur = 1; int a[maxx], r[maxx], f[maxx]; char s[maxx]; std ::vector<int> G[maxx]; int main() { read(n); read(m); for (int i = (1), i_END = (n); i <= (i_END); ++i) { cur *= 2; if (cur > m) { k = 1; break; } } if (k) { printf("%I64d\n", m); } else { printf("%I64d\n", m % cur); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = (int)333; ; int num[maxn][maxn], geo[maxn][maxn], sp[2 * maxn][2 * maxn], n, m; long long ans; int pr[maxn * maxn], sz[maxn * maxn], col[maxn * maxn]; vector<pair<int, int> > gem[maxn * maxn]; map<int, int> fp; queue<pair<int, int> > q; int find(int a) { return (a == pr[a] ? a : pr[a] = find(pr[a])); } int spi, spj; bool cmp(pair<int, int> a, pair<int, int> b) { int i1 = a.first / m, j1 = a.first % m, i2 = b.first / m, j2 = b.first % m; return sp[maxn + i1 - spi][maxn + j1 - spj] < sp[maxn + i2 - spi][maxn + j2 - spj]; } void adsf(int a, int b, int w) { a = find(a); sort(gem[a].begin(), gem[a].end(), cmp); for (int i = 0; i < gem[a].size(); i++) { q.push(gem[a][i]); } gem[a].clear(); if (b == -1) { fp[col[a]] = -1; col[a] = w; fp[w] = a; ans += sz[a]; return; } b = find(b); pr[a] = b; fp[col[a]] = -1; ans += sz[a]; sz[b] += sz[a]; } const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; bool ok(int i, int j) { if (i < 0 || i >= 2 * maxn) return false; if (j < 0 || j >= 2 * maxn) return false; return true; } void makesp() { int i = maxn, j = maxn, sz = 1, now = 1, dir = 0; while (ok(i, j)) { for (int t = 0; t < 2; t++) { for (int _ = 0; _ < sz; _++) { i += di[dir]; j += dj[dir]; if (!ok(i, j)) return; sp[i][j] = now++; } dir++; dir %= 4; } sz++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> num[i][j]; if (fp.find(num[i][j]) != fp.end()) { pr[i * m + j] = fp[num[i][j]]; sz[pr[i * m + j]]++; } else { pr[i * m + j] = i * m + j; fp[num[i][j]] = i * m + j; col[i * m + j] = num[i][j]; sz[i * m + j]++; } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> geo[i][j]; if (geo[i][j] != -1) { gem[find(i * m + j)].push_back(make_pair(i * m + j, geo[i][j])); } } makesp(); int x, y; cin >> x >> y; x--; y--; q.push(make_pair(x * m + y, geo[x][y])); while (!q.empty()) { pair<int, int> g = q.front(); q.pop(); if (fp.find(g.second) == fp.end()) fp[g.second] = -1; int c = col[find(g.first)]; if (c == g.second || c == 0) continue; spi = g.first / m; spj = g.first % m; adsf(g.first, fp[g.second], g.second); } cout << ans; }
19
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int ans = 0; int p = 1; int diff; if (a > b) diff = a - b; else diff = b - a; for (int i = 0; i < diff / 2; i++) { ans += 2 * p; p++; } ans += diff % 2 * p; cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const long long MOD = (int)1e9 + 7; const int HWMAX = (int)1e5 + 5; const int NMAX = 2000 + 5; int h, w, n; long long dp[NMAX]; long long jc[2 * HWMAX], jcinv[2 * HWMAX]; long long qpow(long long a, int b) { long long c = 1; while (b) { if (b & 1) c = c * a % MOD; a = a * a % MOD; b >>= 1; } return c; } void CalFactorials() { jc[0] = 1; jcinv[0] = 1; for (int i = 1; i < 2 * HWMAX; i++) { jc[i] = jc[i - 1] * i % MOD; jcinv[i] = qpow(jc[i], MOD - 2); } } int Combn(int n, int m) { return jc[n] * jcinv[m] % MOD * jcinv[n - m] % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); CalFactorials(); cin >> h >> w >> n; vector<pair<int, int>> a(n + 1); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } a[n].first = h; a[n].second = w; sort(a.begin(), a.end()); for (int i = 0; i <= n; i++) { dp[i] = Combn(a[i].first + a[i].second - 2, a[i].first - 1); for (int j = 0; j < i; j++) { if (a[j].first > a[i].first || a[j].second > a[i].second) continue; dp[i] -= (long long)dp[j] * Combn(a[i].first + a[i].second - a[j].first - a[j].second, a[i].first - a[j].first) % MOD; dp[i] = (dp[i] + MOD) % MOD; } } cout << dp[n] << endl; return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; map<char, string> a; set<char> b; vector<char> answ; for (int i = 0; i < s.size(); ++i) { if (t[i] != a[s[i]][0]) a[s[i]] += t[i]; b.insert(t[i]); b.insert(s[i]); } int ans = 0; for (char c = 'a'; c <= 'y'; ++c) { for (char b = c + 1; b <= 'z'; ++b) { if (a[c][0] == a[b][0] && a[c].size() && a[b].size()) { cout << -1; return 0; } } } for (char c = 'a'; c <= 'z'; ++c) { if (a[c].size() && a[a[c][0]].size() && c != a[a[c][0]][0]) { cout << -1; return 0; } } for (auto it = a.begin(); it != a.end(); ++it) { if ((int)(*it).second.size() > 1) { cout << -1; return 0; } else { if ((int)(*it).second.size() == 1 && b.count((*it).first) && b.count((*it).second[0]) && (*it).first != (*it).second[0]) { ++ans; b.erase((*it).first); b.erase((*it).second[0]); answ.push_back((*it).first); answ.push_back((*it).second[0]); } } } cout << ans << endl; for (int i = 0; i < answ.size(); i += 2) { cout << answ[i] << " " << answ[i + 1] << endl; } }
7
#include <stdio.h> #include <stdlib.h> int main() { int n,t,m,a[222222],i,flag; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for( i=0;i<n;i++) scanf("%d",&a[i]); long long sum=0,temp=1; flag=1; while(flag) { for(i=0;i<n;i++) { if(a[i]%temp==0) sum+=a[i]; else { flag=0; break; } } temp*=m; } printf("%lld\n",sum); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, ans[1111]; bool b[1111]; vector<int> a; void query() { if (a.size() == 0) return; memset(b, true, sizeof(b)); printf("%d\n", a.size()); printf("%d", a[0]); b[a[0]] = false; for (int i = 1; i < a.size(); i++) { printf(" %d", a[i]); b[a[i]] = false; } printf("\n"); fflush(stdout); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (b[i]) { ans[i] = min(ans[i], x); } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { ans[i] = 2000000000; } int step = 1024; while (step > 1) { a.clear(); for (int i = 0; i < n; i++) if (i % step < step / 2) { a.push_back(i + 1); } query(); a.clear(); for (int i = 0; i < n; i++) if (i % step >= step / 2) { a.push_back(i + 1); } query(); step /= 2; } printf("-1\n"); for (int i = 1; i < n; i++) { printf("%d ", ans[i]); } printf("%d\n", ans[n]); return 0; }
11
#include <bits/stdc++.h> using namespace std; void solve() { int n, mn = INT_MAX; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int ans = 0; for (int i = n - 1; i >= 0; i--) { if (arr[i] > mn) ans++; mn = min(arr[i], mn); } cout << ans << "\n"; ; } int main() { int t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; ; struct _dbg { template <class T> _dbg& operator,(const T& v) { cerr << v << " "; return *this; }; }; _dbg _d; long long a[100100], N, s; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }; int main() { cin >> N; for (int i = 0, _i = (N); i < _i; ++i) cin >> a[i]; sort(a, a + N); for (int i = (1), _i = (N); i < _i; ++i) s += (a[i] - a[i - 1]) * i * (N - i) * 2; for (int i = 0, _i = (N); i < _i; ++i) s += a[i]; auto g = gcd(s, N); cout << s / g << ' ' << N / g << endl; };
8
#include <bits/stdc++.h> using namespace std; int main() { long long int m, k, l, n; scanf("%lld%lld%lld%lld", &n, &m, &k, &l); if (m > n) { cout << -1; return 0; } if ((l + k) % m == 0) { if ((long long)(l + k) > n) cout << -1; else cout << (long long)(l + k) / m; } else { if (((long long)(l + k) / m + 1) * m > n) cout << -1; else cout << (long long)(l + k) / m + 1; } }
6
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> T sqr(T a) { return a * a; } template <typename T> int sign(T a) { if (a == 0) return 0; return (a > 0 ? 1 : -1); } template <typename T> bool uax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool uin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; return out; } mt19937_64 rnd(0); const int LOG = 22; const int N = (int)5e5 + 7; const int MAXN = (int)2e5 + 7; const int MOD = (int)1e9 + 7; const int INF = (int)2e9 + 7; const int CONST = 450; const long long LINF = (int64_t)1e18; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1; struct Bitset { unsigned long long arr[MAXN] = {}; int len = 0; Bitset(){}; void resize(int n) { len = n / 64 + 2; } void set(int pos) { arr[pos >> 6] |= 1ULL << (pos & ((1 << 6) - 1)); } bool get(int pos) { return (arr[pos >> 6] & (1ULL << (pos & ((1 << 6) - 1)))) > 0; } void operator|=(const Bitset& b) { for (int i = 0; i < b.len; i++) arr[i] |= b.arr[i]; } void operator<<=(int shift) { if (shift % 64 == 0) { int sh = shift / 64; for (int i = len - sh - 1; i >= 0; i--) { arr[i + sh] = arr[i]; arr[i] = 0; } return; } int sh = shift / 64; int pos = len - sh - 1; int right = shift % 64, left = 64 - right; fill(arr + pos + 1, arr + len, 0); unsigned long long val = arr[pos] << right; arr[pos] = 0, arr[pos + sh] = val; for (int i = pos - 1; i >= 0; i--) { unsigned long long v1 = arr[i] << right, v2 = arr[i] >> left; arr[i] = 0; arr[i + sh] |= v1; arr[i + sh + 1] |= v2; } } void print() { vector<int> ones; for (int i = 0; i < len; i++) { for (int j = 0; j < 64; j++) { if (get((i << 6) + j)) ones.push_back((i << 6) + j); } } for (int x : ones) cout << x << ' '; cout << "\n"; } }; int n, x, y, ans[N]; Bitset cur, can; void solve() { cin >> n >> x >> y; can.resize(x); int a = 0, b = 0; for (int i = 0; i <= n; i++) { cin >> b; if (i == 0) continue; cur.resize(b); cur <<= b - a; cur.set(b - a); can |= cur; a = b; } fill(ans, ans + N, -1); for (int i = 0; i <= x; i++) { if (can.get(i)) ans[i + y] = i + y; } for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) uax(ans[j], ans[i]); } int q; cin >> q; for (int i = 0; i < q; i++) { int lap; cin >> lap; lap /= 2; cout << max(2 * ans[lap], -1) << ' '; } } int main() { double start = clock(); cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); rnd.seed(time(0)); int t = 1; for (int i = 0; i < t; i++) { solve(); } }
18
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e7 + 10; int a[101]; bool used[101]; int main() { int n; scanf("%d", &n); if (n % 2 == 0) { for (int i = 1; i <= n; i++) { if (i % 2 == 0) printf("%d ", i - 1); else printf("%d ", i + 1); } } else printf("%d", -1); return 0; }
0
#include <bits/stdc++.h> using namespace std; bool debug; const int inf = 1e9 + 5; const int nax = 1e6 + 5; struct wezel { int lewy; int prawy; wezel(int l = -1, int p = -1) : lewy(l), prawy(p) {} }; wezel d[nax]; int n, c, czas, a, b; int najwi[nax], najmi[nax]; char t[1000]; vector<int> vl[nax], vp[nax]; bool mozna = true; void swag(int x, int p) { int najw = -1; int najm = p + 1; bool zle = false; if (x > n) return; if (x == p) { if (!vl[x].empty() || !vp[x].empty()) mozna = false; return; } for (auto i : vl[x]) { najw = max(najw, najwi[i]); if (najmi[i] < czas || najwi[i] > p) zle = true; } for (int i = x + 1; i <= najw; ++i) { najw = max(najw, najwi[i]); } for (auto i : vp[x]) { najm = min(najm, najmi[i]); if (najmi[i] < czas || najwi[i] > p) zle = true; } if (najw >= najm || zle) { mozna = false; return; } if (najw != -1) { ++czas; d[czas] = wezel(-1, -1); d[x].lewy = czas; swag(czas, najw); } if (czas < p) { ++czas; d[czas] = wezel(-1, -1); d[x].prawy = czas; swag(czas, p); } } void inorder(int x) { if (d[x].lewy != -1) inorder(d[x].lewy); printf("%d ", x); if (d[x].prawy != -1) inorder(d[x].prawy); } int main(int argc, char* argv[]) { debug = argc > 1; scanf("%d%d", &n, &c); for (int i = 1; i <= n; ++i) najwi[i] = najmi[i] = i; for (int i = 0; i <= (c)-1; ++i) { scanf("%d %d %s", &a, &b, t); if (b <= a) { puts("IMPOSSIBLE"); return 0; } if (b > a) { if (t[0] == 'L') vl[a].push_back(b); else vp[a].push_back(b); } } for (int i = n; i >= 1; --i) { for (auto j : vl[i]) najwi[i] = max(najwi[i], najwi[j]); for (auto j : vp[i]) najwi[i] = max(najwi[i], najwi[j]); } czas = 1; d[1] = wezel(-1, -1); swag(1, n); if (mozna) { inorder(1); puts(""); } else puts("IMPOSSIBLE"); return 0; }
16
#include <bits/stdc++.h> using namespace std; const int INF = (int)2E9; const long long LINF = (long long)9E18; const long double PI = acos(-1.0); const long double EPS = 1E-11; int t[6][6] = { {2, 3, 4, 1, 5, 6}, {4, 1, 2, 3, 5, 6}, {1, 5, 3, 6, 4, 2}, {1, 6, 3, 5, 2, 4}, {5, 2, 6, 4, 3, 1}, {6, 2, 5, 4, 1, 3}, }; string s, p; vector<string> q, tmp; bool Free[6]; int res; string trans(string st, int i) { string res = st; for (int j = (0); j <= (5); ++j) res[j] = st[t[i][j] - 1]; return res; } void update() { string tg; if (!q.empty()) if (find(q.begin(), q.end(), p) != q.end()) return; res++; q.push_back(p); int i = q.size() - 1; while (i < q.size()) { for (int num = (0); num <= (5); ++num) { tg = trans(q[i], num); if (find(q.begin(), q.end(), tg) == q.end()) q.push_back(tg); } i++; } } void try_(int i) { if (i == 6) { update(); return; } for (int j = (0); j <= (5); ++j) if (Free[j]) { Free[j] = false; p[i] = s[j]; try_(i + 1); Free[j] = true; } } int main() { cin >> s; res = 0; memset((Free), (true), sizeof(Free)); p = s; try_(0); cout << res; }
9
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int n, m; int a[N], b[N]; long long w[N], sum1[N], sum2[N], dp[N]; int dfn[N], low[N], st[N], cp[N]; int dn = 0, sn = 0, cn = 1; vector<int> adj[N], adj2[N]; void init() { for (int i = 1; i <= 15000; i++) { sum1[i] = i; sum1[i] += sum1[i - 1]; sum2[i] = sum1[i]; sum2[i] += sum2[i - 1]; } } long long f(long long num) { int idx = lower_bound(sum1, sum1 + 15000, num) - sum1; return idx * num - sum2[idx - 1]; } void dfs(int u) { dfn[u] = low[u] = ++dn; st[sn++] = u; for (int e : adj[u]) { if (!dfn[b[e]]) { dfs(b[e]); low[u] = min(low[u], low[b[e]]); } else { low[u] = min(low[u], dfn[b[e]]); } } if (dfn[u] == low[u]) { int a = -1; while (a != u) { a = st[--sn]; low[a] = dfn[a] = 0x3f3f3f3f; cp[a] = cn; for (int e : adj[a]) adj2[cn].push_back(e); } long long in = 0; for (int e : adj2[cn]) { if (cp[b[e]] == cn) in += f(w[e]); else dp[cn] = max(dp[cn], dp[cp[b[e]]] + w[e]); } dp[cn] += in; cn++; } } int main() { init(); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%lld", a + i, b + i, w + i); adj[a[i]].push_back(i); } int s; scanf("%d", &s); for (int i = 1; i <= n; i++) { if (!dfn[i]) dfs(i); } printf("%lld\n", dp[cp[s]]); }
13
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long temp = 1, cnt = 1; while (temp < n) { cnt++; temp += cnt; } if (temp >= n) temp -= cnt; n = n - temp; cout << n << endl; }
2
#include <bits/stdc++.h> using namespace std; double x[2], y[2], r[2]; inline double dist() { return (x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1]); } inline bool cover(int a, int b) { return sqrt(dist()) + r[b] <= r[a]; } int main() { for (int i = 0; i < 2; i++) scanf("%lf %lf %lf", &x[i], &y[i], &r[i]); if (cover(0, 1)) { printf("%.10lf\n", (r[0] - sqrt(dist()) - r[1]) / 2.); return 0; } if (cover(1, 0)) { printf("%.10lf\n", (r[1] - sqrt(dist()) - r[0]) / 2.); return 0; } if (dist() <= (r[0] + r[1]) * (r[0] + r[1])) return 0 * printf("0\n"); double res = sqrt(dist()) - (1. * r[0] + r[1]); printf("%.15lf\n", res / 2.); return 0; }
10
#include <bits/stdc++.h> using namespace std; bool is_leap(int x) { if (x % 400 == 0) return 1; else if (x % 100 == 0) return 0; else if (x % 4 == 0) return 1; else return 0; } int y, cnt; int main() { scanf("%d", &y); for (int z = (y + 1); z <= (999999); z++) { if (is_leap(z - 1)) cnt += 2; else cnt++; if (cnt % 7 == 0 && is_leap(y) == is_leap(z)) { printf("%d\n", z); return 0; } } }
8
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int inf = 1e9; const long long INF = 1e18; const int mod = 1e9 + 7; const int N = 1e5 + 5; void solve() { int a, b; cin >> a >> b; cout << (a * b + 1) / 2 << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; for (int tc = 1; tc <= t; tc++) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int dp[1005][1005]; void chk(int &x, int y) { x = (x + y) % mod; } int main() { int n, m, K; cin >> n >> m >> K; int mx = max(n - 2, m - 2); for (int i = 1; i <= mx; i++) dp[1][i] = 1; for (int i = 2; i <= K; i++) for (int j = 0; j <= mx; j++) if (dp[i - 1][j]) for (int k = j + 2; k <= mx; k++) chk(dp[i][k], 1ll * dp[i - 1][j] * (k - j - 1) % mod); int ans1 = 0, ans2 = 0; for (int i = 1; i < n - 1; i++) chk(ans1, 1ll * dp[K][i] * (n - i - 1) % mod); for (int i = 1; i < m - 1; i++) chk(ans2, 1ll * dp[K][i] * (m - i - 1) % mod); cout << 1ll * ans1 * ans2 % mod << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int x, t, a, b, da, db; scanf("%d%d%d%d%d%d", &x, &t, &a, &b, &da, &db); if (x == 0) { printf("YES\n"); return 0; } for (int i = 0; i < t; ++i) if (b - i * db == x) { printf("YES\n"); return 0; } for (int i = 0; i < t; ++i) if (a - i * da == x) { printf("YES\n"); return 0; } for (int i = 0; i < t; ++i) for (int j = 0; j < t; ++j) if (a - i * da + b - j * db == x) { printf("YES\n"); return 0; } printf("NO\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long t = 1, ans = 0; int n; int main() { cin >> n; for (int i = 2; i <= n; ++i) { t = t * 3 % 1000000007; if (i % 2 == n % 2) ans += t; else ans -= t; ans %= 1000000007; } cout << (ans + 1000000007) % 1000000007 << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long #define pb push_back #define vi vector<int> #define size(x) (int)((x).size()) #define memf(a) memset(a,false,sizeof(a)) #define F first #define S second #define all(v) v.begin(),v.end() #define rep(i,a,b) for(int i=a;i<b;i++) #define fr(i, n) for(int i=0;i<n;i++) #define tr(v) for(auto it=v.begin();it!=v.end();it = next(it)) const int inf = 1e16; const int N = 1e5 + 1; const int mod = 1e9 + 7; void solve() { int n; cin >> n; int fact[n + 1]; fact[0] = fact[1] = 1; for (int i = 2; i <= n; i++) { fact[i] = (fact[i - 1] * i) % mod; } int a[n]; fr(i, n) cin >> a[i]; int final_and = a[0]; fr(i, n) { final_and = final_and & a[i]; } int cnt = 0; fr(i, n) { if (a[i] == final_and) cnt++; } fr(i, n) { if ((final_and & a[i]) != final_and) { cout << 0 << "\n"; return; } } if (cnt < 2) { cout << "0\n"; return; } int ans = (cnt * (cnt - 1)) % mod; ans = (fact[n - 2] * ans) % mod; cout << ans << "\n"; } signed main() { fastio; int t = 1; cin >> t; while (t--) solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int tux, foo = 0, bar = 0, baz = 0, quz = 1; cin >> tux; for (int i = 0; i < tux; i++) { int pur; cin >> pur; foo += pur; bar++; int a = foo * quz; int b = bar * baz; if (a >= b) { baz = foo; quz = bar; } else break; } cout << baz / (double)quz; }
9
#include <bits/stdc++.h> const int INF = (int)1e9 + 7; const int N = (int)1e5 + 7; const int MAXN = (int)5e5 + 228; using namespace std; int t[MAXN * 4][2], a[MAXN], n, b[MAXN]; void build(int v = 1, int l = 1, int r = n + n + n, bool isb = 0) { if (l == r) { if (!isb) t[v][0] = t[v][1] = a[l]; else t[v][0] = t[v][1] = b[l]; t[v][1] *= -1; return; } int mid = l + r >> 1; build(v + v, l, mid, isb); build(v + v + 1, mid + 1, r, isb); t[v][0] = max(t[v + v][0], t[v + v + 1][0]); t[v][1] = max(t[v + v][1], t[v + v + 1][1]); } int getMax(int l, int r, int type, int v = 1, int tl = 1, int tr = n + n + n) { if (l > tr || r < tl) return INT_MIN; if (l <= tl && r >= tr) return t[v][type]; int mid = tl + tr >> 1; return max(getMax(l, r, type, v + v, tl, mid), getMax(l, r, type, v + v + 1, mid + 1, tr)); } inline bool check(int l, int r) { if (getMax(l, r, 0) < l) return 1; else return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = n + 1; i <= n + n + n; ++i) a[i] = a[i - n]; build(); for (int i = 1; i <= n + n + n; ++i) { int l = 0, r = i; while (r - l > 1) { int mid = l + r >> 1; if ((getMax(mid, i - 1, 0) + 1 >> 1) > a[i]) l = mid; else r = mid; } b[i] = l; } build(1, 1, n + n + n, 1); for (int i = 1; i <= n; ++i) { int l = i, r = i + 2 * n + 1; while (r - l > 1) { int mid = l + r >> 1; if (check(i, mid)) l = mid; else r = mid; } if (l >= i + 2 * n) l = i - 2; cout << l - i + 1 << ' '; } return 0; }
12
#include <bits/stdc++.h> using namespace std; const long long modd = (1000LL * 1000LL * 1000LL + 7LL); int main(int argc, char *argv[]) { mt19937 rang(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); int test_cases = 1; for (int t = (1); t < (1) + (test_cases); ++t) { int n; cin >> n; set<long long> a; long long sum = 0; for (int i = (0); i < (0) + (n); ++i) { long long x; cin >> x; a.insert(x); sum += x; } int m; cin >> m; for (int i = (0); i < (0) + (m); ++i) { long long x; cin >> x; long long y; cin >> y; long long bord_low = x, bord_high = sum - y; long long ans = 3LL * modd * modd; if (bord_high < bord_low) { auto it = a.lower_bound(bord_low); if (it != a.begin()) { --it; ans = min(ans, max(bord_low - bord_high, bord_low - *it)); } it = a.lower_bound(bord_low); if (it != a.end()) { ans = min(ans, *it - bord_high); } } else { auto it = a.lower_bound(bord_low); if ((it != a.end()) && (*it <= bord_high)) { ans = 0; } else { if (it != a.begin()) { --it; ans = min(ans, bord_low - *it); } it = a.lower_bound(bord_high); if (it != a.end()) { ans = min(ans, *it - bord_high); } } } cout << ans << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } using ll = long long; using ld = long double; const string FILENAME = "input"; const int MAXN = 2005; int n, m; bitset<2005> a[MAXN]; int w[MAXN][MAXN]; int parent[MAXN]; vector<int> who[MAXN]; vector<int> g[MAXN]; int findset(int v) { if (v == parent[v]) { return v; } return parent[v] = findset(parent[v]); } int col[MAXN]; int uk = 0; int dfs(int u, int pr = -1) { int cnt = 1; for (auto h : g[u]) { if (h == pr || col[h] != uk) { continue; } cnt += dfs(h, u); } return cnt; } bool check(vector<int> x) { uk++; for (auto y : x) { col[y] = uk; } return dfs(x[0]) == (int)(x).size(); } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) { a[i].reset(); parent[i] = i; g[i].clear(); } for (int i = 0; i < m; i++) { string s; cin >> s; who[i].clear(); for (int j = 0; j < n; j++) { if (s[j] == '1') { who[i].push_back(j); a[j][i] = 1; } } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { w[i][j] = (a[i] & a[j]).count(); } } vector<pair<int, pair<int, int> > > st; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { st.push_back({w[i][j], make_pair(i, j)}); } } sort((st).begin(), (st).end()); reverse((st).begin(), (st).end()); vector<pair<int, int> > sts; for (auto x : st) { int ta = x.second.first; int tb = x.second.second; if (findset(ta) != findset(tb)) { parent[findset(ta)] = findset(tb); sts.push_back(make_pair(ta, tb)); g[ta].push_back(tb); g[tb].push_back(ta); } } bool bad = false; for (int i = 0; i < m; i++) { if (!check(who[i])) { bad = true; break; } } if (bad) { cout << "NO\n"; return; } cout << "YES\n"; for (auto x : sts) { cout << x.first + 1 << ' ' << x.second + 1 << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } }
25
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int x = sqrt(n); int extra = n - (x * x); if (extra == 0) printf("%d", 4 * x); else if (extra <= x) printf("%d", (4 * x) + 2); else printf("%d", (4 * x) + 4); return 0; }
2
/**Bismillahir Rahmanir Rahim.**/ #include <bits/stdc++.h> using namespace std; //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; //typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef string str; #define io() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define PB push_back #define MP make_pair #define F first #define S second #define sz(a) ((ll)(a).size()) #define mset(a , b) memset(a , b , sizeof(a)) #define aout(a , n) for(int iii = 0 ; iii < n ; iii++) cout << a[iii] << " "; cout << '\n'; #define aout2(a , n , m) for(ll iii = 0 ; iii < n ; iii++){for(ll jjj = 0 ; jjj < m ; jjj++){cout << a[iii][jjj] << " ";} cout << '\n';} #define deb(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } void print(ll t){cerr<<t;} void print(int t){cerr<<t;} void print(str t){cerr<<t;} void print(char t){cerr<<t;} void print(ld t){cerr<<t;} void print(double t){cerr<<t;} void print(ull t){cerr<<t;} template <class T, class V> void print(pair <T, V> p); template <class T> void print(vector <T> v); template <class T> void print(set <T> v); template <class T, class V> void print(map <T, V> v); template <class T> void print(multiset <T> v); template <class T, class V> void print(pair <T, V> p){cerr<< "{";print(p.F);cerr<<",";print(p.S);cerr<<"}";} template <class T> void print(vector <T> v){cerr<<"[ ";for(T i: v){print(i);cerr<<" ";}cerr << "]";cerr << '\n';} template <class T> void print(set <T> v){cerr<<"[ ";for(T i: v){print(i);cerr<<" ";}cerr << "]";cerr << '\n';} template <class T> void print(multiset <T> v){cerr<<"[ ";for(T i: v){print(i);cerr<<" ";}cerr << "]";cerr << '\n';} template <class T, class V> void print(map <T, V> v){cerr<<"[ ";for(auto i: v){print(i);cerr<<" ";}cerr << "]";cerr << '\n';} //void print(indexed_set &v){cerr<<"[ ";for(int i: v){print(i);cerr<<" ";}cerr << "]";cerr << '\n';} template <class T> T egcd(T a , T b , T &x , T &y){T gcd , xt , yt;if(a == 0){gcd = b;x = 0 , y = 1;}else {gcd = egcd(b % a , a , xt , yt);x = yt - (b/a)*xt; y = xt;}return gcd;} template <class T> T expo(T base , T exp , T mod){T res = 1;base = base % mod;while (exp > 0){if (exp & 1)res = (res*base) % mod;exp = exp>>1;base = (base*base) % mod;}return res;} template <class T> T modinv(T a , T mod){T x , y; egcd<T>(a , mod , x , y);while(x < 0) x += mod; while(x >= mod) x -= mod; return x;} template <class T> T modinvfermat(T a , T mod){return expo<T>(a , mod - 2 , mod);} template <class T> bool rev(T a , T b){return a > b;} template <class T> ll maxpower(T a , T b){ll ans = 0;while(a > 0 && a % b == 0){ans++;a /= b;}return ans;} template <class T> T mceil(T a, T b){if(a % b == 0) return a/b; else return a/b + 1;} template <class T> T lcm(T a, T b){return (a)/__gcd<T>(a, b)*(b);} //const ll MOD = 1e9+7; const ll MOD = (119 << 23) + 1; const ll INF = (ll) 1e18; const ld PI = acos((ld)-1); const ld EPS = 1e-9; const int MAX = 2e9 + 5; const int N = 3000 + 5; ///****************** SOLUTION STARTS HERE ***********************/// ///==============================================================/// double eucled(double a, double b, double c, double d) { return sqrt((double)pow(a - c, 2) + (double)pow(b - d, 2)); } int main() { io() //freopen("input.txt", "r", stind); //freopen("output.txt", "w", stdout); int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> a(n*2); for (int i = 0; i < n*2; i++) { cin >> a[i].F >> a[i].S; if (a[i].F < 0) a[i].F = abs(a[i].F); if (a[i].S < 0) a[i].S = abs(a[i].S); } //auto sorting = [&](const pair<int, int> &p, const pair<int, int> &q) { //return p.F < q.F; //}; sort(a.begin(), a.end()); //print(a); double ans = 0; int j = n; for (int i = 0; i < n; i++, j++) { ans += (eucled(a[i].F, a[j].F, a[i].S, a[j].S)); } printf("%.9f\n", (double) ans); } return 0; } /* {0, 1}, {0, -1}, {1, 0}, {-2, 0} */
4
#include <bits/stdc++.h> using namespace std; string basic = "What are you doing at the end of the world? Are you busy? Will you save " "us?"; string st = "What are you doing while sending \""; string se = "\"? Are you busy? Will you send \""; string ed = "\"?"; int pt1 = st.length(); int pt2 = se.length(); int pt3 = ed.length(); long long f[100001] = {0}; void func(long long f[], int n, long long k) { if (f[n] < k) cout << "."; else if (n == 0) { cout << basic[k - 1]; } else { if (k <= pt1) { k--; cout << st[k]; return; } else if (k <= f[n - 1] + pt1) { func(f, n - 1, k - pt1); } else if (k <= f[n - 1] + pt1 + pt2) { k--; cout << se[k - pt1 - f[n - 1]]; } else if (k <= 2 * f[n - 1] + pt1 + pt2) { func(f, n - 1, k - f[n - 1] - pt1 - pt2); } else { k--; cout << ed[k - 2 * f[n - 1] - pt1 - pt2]; } } return; } int main() { int q; cin >> q; int n; long long k; f[0] = basic.length(); for (int i = 1; i < 100001; i++) { f[i] = 2 * f[i - 1] + 68; if (f[i] > 1e18) f[i] = 1e18; } while (q--) { cin >> n >> k; func(f, n, k); } }
9
#include <bits/stdc++.h> int ans[] = {0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51, 1, 2, 1, 14, 1, 2, 2, 14, 1, 6, 1, 4, 2, 2, 1, 52, 2, 5, 1, 5, 1, 15, 2, 13, 2, 2, 1, 13, 1, 2, 4, 267, 1, 4, 1, 5, 1, 4, 1, 50, 1, 2, 3, 4, 1, 6, 1, 52, 15, 2, 1, 15, 1, 2, 1, 12, 1, 10, 1, 4, 2}; int main() { int n; scanf("%d", &n); printf("%d", ans[n]); return 0; }
6
#include <bits/stdc++.h> using namespace std; struct edge { int r, nxt; } e[100100 << 1]; struct data { int lc, rc, cnt; vector<int> v; } s[100100 << 2]; int dy[100100], anses[100100], dep[100100], ptr; int Q[100100], Qsz, bel[100100], idsz, A[100100], Asz; int ai[100100], rt[100100], bi[100100], ci[100100], di[100100]; int fa[100100], dfn[100100], son[100100]; int n, m, head[100100], top[100100], rig[100100], tim, esz, vis[100100]; void addedge(int u, int v) { e[++esz].r = v; e[esz].nxt = head[u]; head[u] = esz; e[++esz].r = u; e[esz].nxt = head[v]; head[v] = esz; } int dfs1(int u) { int sz = 1, s = 0, mxsize = 0; for (int t = head[u]; t; t = e[t].nxt) if (e[t].r != fa[u]) { fa[e[t].r] = u, s = dfs1(e[t].r), sz += s; if (mxsize < s) mxsize = s, son[u] = e[t].r; } return sz; } void dfs2(int u, int tp) { top[u] = tp, dfn[u] = ++tim, dep[u] = dep[fa[u]] + 1; rig[tp] = max(rig[tp], tim); if (son[u]) dfs2(son[u], tp); for (int t = head[u]; t; t = e[t].nxt) if (e[t].r != fa[u] && e[t].r != son[u]) dfs2(e[t].r, e[t].r); } void build(int& o, int l, int r) { o = ++ptr; if (l == r) return; int mid = l + r >> 1; build(s[o].lc, l, mid); build(s[o].rc, mid + 1, r); } void ins(int o, int l, int r, int ql, int qr, int id, int flg) { if (ql > qr) return; s[o].cnt += flg; if (ql <= l && r <= qr) { if (flg == 1) s[o].v.push_back(id); return; } int mid = l + r >> 1; if (ql <= mid) ins(s[o].lc, l, mid, ql, qr, id, flg); if (qr > mid) ins(s[o].rc, mid + 1, r, ql, qr, id, flg); } void instree(int u, int v, int id, int flg) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); ins(rt[top[u]], dfn[top[u]], rig[top[u]], dfn[top[u]], dfn[u], id, flg); u = fa[top[u]]; } if (dfn[u] > dfn[v]) swap(u, v); ins(rt[top[u]], dfn[top[u]], rig[top[u]], dfn[u] + 1, dfn[v], id, flg); } void init(int flg) { for (int i = 1; i <= ptr; ++i) s[i].v.clear(), s[i].cnt = 0; for (int i = 1; i <= m; ++i) { if (flg == 0) { instree(ai[i], bi[i], ((i)*2 + (1)), 1); instree(ci[i], di[i], ((i)*2 + (0)), 1); } else { instree(ai[i], bi[i], ((i)*2 + (0)), 1); instree(ci[i], di[i], ((i)*2 + (1)), 1); } } } int qry(int o, int l, int r, int ql, int qr) { if (!s[o].cnt) return -1; if (ql > qr) return -1; while (s[o].v.size()) { int x = s[o].v.back(); if (!vis[x]) return x; else s[o].v.pop_back(); } int mid = l + r >> 1, ret = -1; if (ql <= mid) ret = qry(s[o].lc, l, mid, ql, qr); if (qr > mid && ret == -1) ret = qry(s[o].rc, mid + 1, r, ql, qr); return ret; } int qtree(int fr, int flg) { int u, v; if (fr % 2 == flg) u = ai[fr >> 1], v = bi[fr >> 1]; else u = ci[fr >> 1], v = di[fr >> 1]; while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); int x = qry(rt[top[u]], dfn[top[u]], rig[top[u]], dfn[top[u]], dfn[u]); if (x != -1) return x; u = fa[top[u]]; } if (dfn[u] > dfn[v]) swap(u, v); int x = qry(rt[top[u]], dfn[top[u]], rig[top[u]], dfn[u] + 1, dfn[v]); return x; } void era(int fr, int flg) { vis[fr] = 1; int u, v; if (fr % 2 == !flg) u = ai[fr >> 1], v = bi[fr >> 1]; else u = ci[fr >> 1], v = di[fr >> 1]; instree(u, v, fr, -1); } void _ins(int fr, int flg) { vis[fr] = 0; int u, v; if (fr % 2 == !flg) u = ai[fr >> 1], v = bi[fr >> 1]; else u = ci[fr >> 1], v = di[fr >> 1]; instree(u, v, fr, 1); } void dfs(int u, int flg = 0) { if (flg == 1) bel[u] = idsz, A[++Asz] = u; vis[u] = 1, era(u, flg); while (true) { if (vis[u ^ 1]) { int nxt = qtree(u, flg); if (nxt != -1) dfs(nxt, flg); else break; } else { era(u ^ 1, flg); int nxt = qtree(u, flg); _ins(u ^ 1, flg); if (nxt != -1) dfs(nxt, flg); else break; } } if (flg == 0) Q[++Qsz] = u; } int main() { cin >> n; for (int i = 2, u, v; i <= n; ++i) { cin >> u >> v; addedge(u, v); } cin >> m; for (int i = 1; i <= m; ++i) cin >> ai[i] >> bi[i] >> ci[i] >> di[i]; dfs1(1), dfs2(1, 1); for (int i = 1; i <= n; ++i) if (top[i] == i) build(rt[i], dfn[i], rig[i]); init(0); for (int i = ((1) * 2 + (0)); i <= ((m)*2 + (1)); ++i) if (!vis[i]) dfs(i); init(1); memset(vis, 0, sizeof(vis)); for (int i = Qsz; i >= 1; --i) if (!vis[Q[i]]) ++idsz, dfs(Q[i], 1); for (int i = 1; i <= m; ++i) if (bel[((i)*2 + (0))] == bel[((i)*2 + (1))]) return cout << "NO", 0; cout << "YES" << endl; for (int i = Asz; i >= 1; --i) if (!dy[A[i]]) { dy[A[i]] = dy[A[i] ^ 1] = 1; anses[A[i] >> 1] = (A[i] % 2 == 0 ? 1 : 2); } for (int i = 1; i <= m; ++i) cout << anses[i] << endl; }
24
#include <bits/stdc++.h> using namespace std; class RTree { public: vector<int> data; vector<int> lft; vector<int> rgt; int sz; int tsz; void Init(int size) { sz = size; tsz = 2; while (tsz < sz) tsz *= 2; data.resize(1 + tsz - 1 + tsz); lft.resize(1 + tsz - 1 + tsz); rgt.resize(1 + tsz - 1 + tsz); int i; for (i = 1; i <= 2 * tsz - 1; i++) data[i] = 0; for (i = tsz; i < 2 * tsz; i++) { lft[i] = i - tsz; rgt[i] = i - tsz; } for (i = tsz - 1; i >= 1; i--) { lft[i] = lft[2 * i]; rgt[i] = rgt[2 * i + 1]; } } int Sum(int l, int r) { l += tsz; r += tsz; int res = 0; while (l <= r) { if (l & 1) { res += data[l]; l++; } if (!(r & 1)) { res += data[r]; r--; } l >>= 1; r >>= 1; } return res; } int FF0(int pos) { int ll = pos; int rr = sz - 1; int opt = sz; while (ll <= rr) { int mid = (ll + rr) / 2; int s = Sum(pos, mid); if (s == mid - pos + 1) { ll = mid + 1; } else { if (mid < opt) opt = mid; rr = mid - 1; } } if (opt == sz) return FF0(0); return opt; } void Add(int p, int d) { p += tsz; while (p >= 1) { data[p] += d; p >>= 1; } } }; int h, m, n; vector<RTree> trees; int wtree[110000]; int wpos[110000]; void Load() { scanf("%d%d%d", &h, &m, &n); memset(wtree, 0xFF, sizeof(wtree)); int i; for (i = 0; i < h; i++) { if (wtree[i] != -1) continue; int j; int cnt = 0; j = i; while (wtree[j] == -1) { wtree[j] = trees.size(); wpos[j] = cnt; cnt++; j = (j + m) % h; } RTree a; trees.push_back(a); trees.back().Init(cnt); } } map<int, int> whereidt; map<int, int> whereidpos; void Solve() { int i; long long res = 0; for (i = 0; i < n; i++) { char c = getchar(); while (!((c == '+') || (c == '-'))) c = getchar(); int id, hash; if (c == '+') { scanf("%d%d", &id, &hash); int t = wtree[hash]; int pos = wpos[hash]; int rp = trees[t].FF0(pos); if (rp >= pos) res += (rp - pos); else { res += rp + (trees[t].sz - pos); } trees[t].Add(rp, 1); whereidt[id] = t; whereidpos[id] = rp; } else { scanf("%d", &id); trees[whereidt[id]].Add(whereidpos[id], -1); } } cout << res; } int main() { Load(); Solve(); return 0; }
11
#include <bits/stdc++.h> using namespace std; void solve(long long int caseno) { long long int i, j, n, o = 0; cin >> n; vector<long long int> arr(n); for (i = 0; i < n; i++) { cin >> arr[i]; } if (arr[0] == 1) { cout << n + 1 << ' '; for (i = 1; i <= n; i++) cout << i << ' '; cout << '\n'; } else if (arr[n - 1] == 0) { for (i = 1; i <= n; i++) cout << i << ' '; cout << n + 1 << '\n'; } else { bool unvi = true; for (i = 0; i < n - 1; i++) { cout << i + 1 << ' '; if (unvi && arr[i] == 0 && arr[i + 1] == 1) { cout << n + 1 << ' '; unvi = false; } } cout << n << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; for (long long int i = 1; i <= t; i++) { solve(i); } return 0; }
4
#include <bits/stdc++.h> using namespace std; class item { public: int v; int dir; long long dist; item(int _v, int _dir, long long _dist) { v = _v; dir = _dir; dist = _dist; } }; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<int> adj[n]; for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 0; i < n; ++i) { if (adj[i].size() > 4) { cout << "NO"; return 0; } } queue<item> q; bool visited[n]; pair<long long, long long> coordinate[n]; for (int i = 0; i < n; ++i) { visited[i] = false; } visited[0] = true; q.push(item(0, 3, 1L << (31))); coordinate[0] = make_pair(0, 0); int dirX[] = {0, 1, 0, -1}; int dirY[] = {1, 0, -1, 0}; while (!q.empty()) { item curr = q.front(); q.pop(); int currDir = (curr.dir + 1) % 4; for (int w : adj[curr.v]) { if (!visited[w]) { visited[w] = true; coordinate[w] = make_pair(coordinate[curr.v].first + curr.dist * dirX[currDir], coordinate[curr.v].second + curr.dist * dirY[currDir]); q.push(item(w, (currDir + 2) % 4, curr.dist >> 1)); currDir = (currDir + 1) % 4; } } } cout << "YES\n"; for (int i = 0; i < n; ++i) { cout << coordinate[i].first << " " << coordinate[i].second << endl; } return 0; }
12
#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 extgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long d = extgcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } long long revrsno(long long n) { long long k = 0; vector<long long> z; while (n > 0) { z.push_back(n % 10); n /= 10; } for (long long i = 0; i < z.size(); i++) k = k * 10 + z[i]; return k; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } bool isPowerOfTwo(long long n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } long long powm(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return res; } long long divide(long long a, long long b) { return (a % 1000000007) * powm(b, 1000000007 - 2) % 1000000007; } long long mul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long add(long long a, long long b) { return (a % 1000000007 + b % 1000000007) % 1000000007; } long long Totfun(long long n) { long long z = n; if (n % 2 == 0) { while (n % 2 == 0) { n /= 2; } z /= 2; } for (long long i = 3; i <= sqrt(n); i += 2) { if (isPrime(i) && n % i == 0) { while (n % i == 0) { n /= i; } z -= z / i; } } if (n > 1) { z -= z / n; } return z; } long long maxPrimeFactor(long long n) { long long maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n >>= 1; } for (long long i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } if (n > 2) maxPrime = n; return maxPrime; } long long fact[100001]; long long nCr(long long n, long long r) { if (r > n || r < 0) return 0; long long z = fact[n]; z = mul(z, powm(fact[n - r], 1000000007 - 2)); z = mul(z, powm(fact[r], 1000000007 - 2)); return z; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; long long b[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long one = 0, zero = 0; for (long long i = 0; i < n; i++) { cin >> b[i]; if (b[i] == 1) one = 1; else zero = 1; } if (zero == 0 || one == 0) { bool flag = true; for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { cout << "NO" << endl; flag = false; break; } } if (flag) cout << "YES" << endl; } else { cout << "YES" << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int a1, b1, a2, b2; cin >> a1 >> b1 >> a2 >> b2; int sum = a1 * b1 + a2 * b2; if (a1 == b1 || a2 == b2) { cout << "No" << endl; continue; } int flag = 0; for (int i = 1; i <= sqrt(sum); i++) { if (i * i == sum) { int fn = i; if ((fn - a1 == a2 && b1 == b2) || (fn - a1 == b2 && a2 == b1) || (fn - b1 == a2 && b2 == a1) || (fn - b1 == b2 && a1 == a2)) { cout << "Yes" << endl; flag = 1; break; } } } if (!flag) cout << "No" << endl; } }
1
#include <bits/stdc++.h> using namespace std; int n, a[555555], t[555555], pos[555555], m; vector<int> ans; inline int mp(const int &first) { return lower_bound(t, t + m, first) - t; } void init() { for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]), t[i - 1] = a[i]; sort(t, t + n); m = unique(t, t + n) - t; } int lleft[555555], f[555555]; int work(int st) { stack<pair<int, int> > sta; int s; for (int i = (st); i <= (n); ++i) { s = pos[i]; if (f[s] != -1) { ans.push_back(t[f[s]]), ans.push_back(a[i]), ans.push_back(t[f[s]]), ans.push_back(a[i]); for (int j = (st); j <= (i); ++j) f[pos[j]] = -1; return i + 1; } if (lleft[s] >= st) { while ((int)sta.size() > 0 && sta.top().second != lleft[s]) f[sta.top().first] = s, sta.pop(); } sta.push(make_pair(s, i)); if (lleft[s] < st) lleft[s] = i; } return n + 1; } void solve() { ans.clear(); for (int i = 1; i <= n; ++i) pos[i] = mp(a[i]); int i = 1; memset(lleft, 0, sizeof(lleft)), memset(f, -1, sizeof(f)); while (i <= n) i = work(i); printf("%d\n", (int)ans.size()); for (int i = 0; i < (int)ans.size(); ++i) (int)ans.size() != i + 1 ? printf("%d ", ans[i]) : printf("%d\n", ans[i]); } int main() { while (scanf("%d", &n) != EOF) { init(); solve(); } return 0; }
15
#include <bits/stdc++.h> int main() { char s1[10], s2[10], s3[10]; gets(s1); int m, t1, t2, tm, hr, minn, xh, xm; scanf("%d", &m); s2[0] = s1[0]; s2[1] = s1[1]; s2[2] = '\0'; s3[0] = s1[3]; s3[1] = s1[4]; s3[2] = '\0'; int n1, n2; n1 = atoi(s2); n2 = atoi(s3); if (m > 60) { t1 = m - (60 * (m / 60)); t2 = m / 60; minn = t1 + n2; if (minn >= 60) { minn = minn - 60; t2 = t2 + 1; } else { minn = minn; } hr = t2 + n1; if (hr >= 24) { hr = hr - (24 * (hr / 24)); } else { hr = hr; } } else if (m <= 60) { t1 = m; t2 = 0; minn = t1 + n2; if (minn >= 60) { minn = minn - 60; t2 = t2 + 1; } else { minn = minn; } hr = t2 + n1; if (hr >= 24) { hr = hr - (24 * (hr / 24)); } else { hr = hr; } } if (minn < 10 && hr < 10) { printf("0%d:0%d\n", hr, minn); } if (minn >= 10 && hr < 10) { printf("0%d:%d\n", hr, minn); } if (minn < 10 && hr >= 10) { printf("%d:0%d\n", hr, minn); } if (minn >= 10 && hr >= 10) { printf("%d:%d\n", hr, minn); } }
1
#include <bits/stdc++.h> const int N = 3e5 + 5, mod = 998244353; int n, nxt[N << 1], to[N << 1], head[N], f[N][3], cnt; int read() { int x = 0, f = 1; char s; while ((s = getchar()) > '9' || s < '0') if (s == '-') f = -1; while (s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); return x * f; } void addEdge(const int u, const int v) { nxt[++cnt] = head[u], to[cnt] = v, head[u] = cnt; } int inv(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; x = 1ll * x * x % mod; y >>= 1; } return r; } void DP(const int u, const int fa) { f[u][0] = 0; f[u][1] = f[u][2] = 1; int tmp = 1; for (int i = head[u]; i; i = nxt[i]) if (to[i] ^ fa) { DP(to[i], u); f[u][2] = 1ll * f[u][2] * (f[to[i]][0] + f[to[i]][2]) % mod; f[u][1] = 1ll * f[u][1] * (f[to[i]][0] * 2 % mod + f[to[i]][2]) % mod; tmp = 1ll * tmp * (f[to[i]][0] * 2 % mod + f[to[i]][2]) % mod; } f[u][1] = (f[u][1] - f[u][2] + mod) % mod; for (int i = head[u]; i; i = nxt[i]) if (to[i] ^ fa) f[u][0] = (f[u][0] + 1ll * tmp * inv((f[to[i]][0] * 2 % mod + f[to[i]][2]) % mod, mod - 2) % mod * (f[to[i]][1] + f[to[i]][2]) % mod) % mod; } int main() { int u, v; n = read(); for (int i = 1; i < n; ++i) { u = read(), v = read(); addEdge(u, v), addEdge(v, u); } DP(1, 0); printf("%d\n", (f[1][0] + f[1][2]) % mod); return 0; }
16
#include <bits/stdc++.h> using namespace std; int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; } int main() { int n; vector<int> a; scanf("%d", &n); int min = 0; int max = 0; for (int i = 0; i < n; ++i) { int cur; scanf("%d", &cur); a.push_back(cur); if (cur < a[min]) min = i; if (cur > a[max]) max = i; } if (min > max) { int t = min; min = max; max = t; } printf("%d", maxx(n - 1 - min, max)); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int ans; int arr[300009]; vector<int> vr; void solve() { int a, b, c; cin >> a >> b >> c; a = max(a, b); a = max(a, c); cout << a << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int testcases; cin >> testcases; for (int t = 0; t < testcases; ++t) { solve(); } return 0; }
0
#include <bits/stdc++.h> int f_min(int x, int y) { if (x < y) return x; else return y; } int f_max(int x, int y) { if (x < y) return y; else return x; } void swap(int& x, int& y) { x = x ^ y; y = x ^ y; x = x ^ y; } int f_abs(int x) { return (x) > (0) ? (x) : (-x); } int lowbit(int x) { return (x) & (-x); } using namespace std; const int MM = 511111; const double Pi = acos(-1.0); const double lep = 1e-6; const double inf = 1000000000.00; int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, 1, -1, 0}; int N, M, Q; int num[MM], sum, all, cnt; bool vis[MM]; int ans[MM]; struct Pos { int x, y; } p[MM]; queue<Pos> que; vector<int> edge[MM]; void get_data() { int i, j, k, tt; for (i = 0; i <= N; i++) edge[i].clear(); for (i = 1; i <= N; i++) { if (i == M) continue; else { scanf("%d", &tt); edge[i].push_back(tt); edge[tt].push_back(i); } } } void dfs(int u) { int i, j, k, v; vis[u] = true; for (i = 0; i < edge[u].size(); i++) { v = edge[u][i]; if (!vis[v]) ans[v] = u, dfs(v); } } void solve() { int i, j, k; memset(vis, false, sizeof(vis)); dfs(Q); for (i = 1; i <= N; i++) { if (i == Q) continue; else printf("%d ", ans[i]); } printf("\n"); } int main() { while (scanf("%d%d%d", &N, &M, &Q) != EOF) get_data(), solve(); return 0; }
8
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; err(++it, args...); } const int MOD = 1e9 + 7, N = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; { long long n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ; map<long long, long long> m; m[0] = 1; long long sum = 0, ans = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (m[sum] != 0) { ans++; m.clear(); sum = a[i]; m[0] = 1; m[sum]++; } else m[sum]++; } cout << ans << "\n"; } }
7
#include <bits/stdc++.h> using namespace std; int main() { int x = 1; int output = 0; cin >> x; if (x == 3) output = 5; else { for (int n = 1; n <= 100;) { int temp = (n * n + 1) / 2; if (x <= temp) { output = n; break; } n = n + 2; } } cout << output; }
9
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; string s; cin >> s; bool res = false; if (n <= 1) { cout << "-1" << " " << "-1" << endl; res = true; } for (int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { cout << i + 1 << " " << i + 2 << endl; res = true; break; } } if (res == false) cout << "-1" << " " << "-1" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } long long powmod(long long a, long long b) { long long res = 1; a %= 1000000007; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % 1000000007; a = a * a % 1000000007; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t--) { long long d, m; cin >> d >> m; if (d == 1) cout << (1 % m), cout << endl; else if (d == 2) cout << (3 % m), cout << endl; else if (d == 3) cout << (5 % m), cout << endl; else { long long pre_ans = 5; long long poww = 4; long long pre_ans1 = 5; while (poww - 1 < d) { pre_ans1 = pre_ans; pre_ans = ((pre_ans * (poww + 1)) % m + poww % m) % m; poww *= 2; } poww /= 2; poww %= m; cout << (((d - poww + 2) * pre_ans1) % m + (d - poww + 1) % m) % m, cout << endl; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long int ans = 0; long long int takethree(long long int t) { return t * (t - 1) * (t - 2) / 6; } long long int taketwo(long long int t) { return t * (t - 1) / 2; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } map<int, int> mapper; void ini() { int Total = 0; for (int i = 2000; i > 0; i--) { int ans = 0; for (int j = i - 1; j > 0; j--) { ans += j; } mapper[ans] = i; } } pair<int, int> Slope(pair<int, int> p0, pair<int, int> p1) { int dx = p0.first - p1.first; int dy = p0.second - p1.second; int g = gcd(dx, abs(dy)); dx /= g; dy /= g; if (dx == 0) dy /= abs(dy); if (dy == 0) dx == 1; return pair<int, int>(dx, dy); } pair<int, int> data; int main() { ini(); int n; cin >> n; vector<pair<int, int> > data; long long int ans = takethree(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; data.push_back(pair<int, int>(x, y)); } sort(data.begin(), data.end(), greater<pair<int, int> >()); for (int i = 0; i < n; i++) { map<pair<int, int>, int> slops; for (int j = i + 1; j < n; j++) { ans -= slops[Slope(data[i], data[j])]; slops[Slope(data[i], data[j])]++; } } cout << ans << endl; int x; cin >> x; return 0; }
11
#include <bits/stdc++.h> using namespace std; using ll = long long; int sum() { int a, b, c, sum; sum = a + b + c; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<ll, ll> > a(n); for (auto &[f, s] : a) cin >> f; for (auto &[f, s] : a) cin >> s; sort(a.begin(), a.end()); ll ans = a.back().first; ll res = 0; for (int i = n - 1; i >= -1; i--) { ll cur = 0; if (i != -1) cur = a[i].first; ans = min(ans, max(cur, res)); if (i == -1) break; res += a[i].second; } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; bool a[1100]; vector<int> ans[100]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= k; i++) { int x; cin >> x; a[x] = true; ans[i].push_back(x); } int x = 1; for (int i = 1; i <= k; i++) { while (ans[i].size() != n) { if (a[x] == false) { ans[i].push_back(x); a[x] = true; } x++; } } for (int i = 1; i <= k; i++) { for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " "; cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct Eve { int color; int num; Eve(int num, int color) : num(num), color(color){}; }; bool operator<(Eve& a, Eve& b) { return a.num < b.num; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<vector<Eve> > r(n + 2, vector<Eve>()); vector<vector<Eve> > c(m + 2, vector<Eve>()); for (int i = 1; i <= k; i++) { int z, x, color; scanf("%d%d%d", &z, &x, &color); if (z == 1) { r[x].push_back(Eve(i, color)); } else { c[x].push_back(Eve(i, color)); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (r[i].size() || c[j].size()) { if (r[i].size() && c[j].size() == 0) { printf("%d ", r[i].back().color); } else if (r[i].size() == 0 && c[j].size()) { printf("%d ", c[j].back().color); } else if (r[i].back().num < c[j].back().num) { printf("%d ", c[j].back().color); } else { printf("%d ", r[i].back().color); } } else { printf("0 "); } } printf("\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, i, j, k, sa[N], rnk[N], h[N], tree[N * 4]; int ch[N * 2][26], fa[N * 2], len[N * 2], tot, last, pos[N * 2], vis[N * 2]; int head[N * 2], Next[N * 2], adj[N * 2]; long long ans, siz[N * 2]; char c[N]; struct asd { int x, y; bool operator<(const asd &a) const { return a.y > y; } }; set<asd> d[N * 2]; struct str { int x, y; }; void Build(int i, int l, int r) { if (l == r) { tree[i] = h[l]; return; } int mid = l + r >> 1; Build(i << 1, l, mid); Build(i << 1 | 1, mid + 1, r); tree[i] = min(tree[i << 1], tree[i << 1 | 1]); } int Query(int i, int l, int r, int ll, int rr) { if (l >= ll && r <= rr) return tree[i]; int mid = l + r >> 1, s = 1 << 30; if (mid >= ll) s = min(s, Query(i << 1, l, mid, ll, rr)); if (mid < rr) s = min(s, Query(i << 1 | 1, mid + 1, r, ll, rr)); return s; } void Insert(int c) { int p = last, nt = ++tot; len[tot] = len[last] + 1; last = nt; for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = nt; if (!p) fa[nt] = 1; else { int q = ch[p][c]; if (len[q] == len[p] + 1) fa[nt] = q; else { int nq = ++tot; memcpy(ch[nq], ch[q], sizeof(ch[nq])); fa[nq] = fa[q]; fa[q] = fa[nt] = nq; len[nq] = len[p] + 1; for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq; } } } void Push(int u, int v) { Next[++k] = head[u]; head[u] = k; adj[k] = v; } void dfs(int i) { int j; if (pos[i] && pos[i] < n - 1) { vis[i] = 1; siz[i] += n - pos[i] - 1; d[i].insert((asd){pos[i] + 2, rnk[pos[i] + 2]}); } else if (pos[i] == n - 1) vis[i] = 1; if (i == 1) { siz[i] += n - 1; d[i].insert((asd){2, rnk[2]}); } for (j = head[i]; j; j = Next[j]) { dfs(adj[j]); vis[i] |= vis[adj[j]]; if (d[i].size() < d[adj[j]].size()) { swap(d[i], d[adj[j]]); siz[i] = siz[adj[j]]; } for (auto it : d[adj[j]]) { auto tmp = d[i].lower_bound(it); auto t2 = tmp; if (tmp != d[i].end() && tmp != d[i].begin()) { --t2; siz[i] += Query(1, 1, n - 1, t2->y, tmp->y - 1); } if (tmp != d[i].end()) siz[i] -= Query(1, 1, n - 1, it.y, tmp->y - 1); if (tmp != d[i].begin()) { --tmp; siz[i] -= Query(1, 1, n - 1, tmp->y, it.y - 1); } d[i].insert(it); siz[i] += n - it.x + 1; } } ans += len[i] - len[fa[i]]; if (vis[i]) ans += (len[i] - len[fa[i]]) * (siz[i] + 1); } struct node { int a, b, x; } p[1000005]; bool cmp(node a, node b) { if (a.a == b.a) return a.b < b.b; return a.a < b.a; } int main() { scanf("%s", c + 1); n = strlen(c + 1); if (n == 1) { cout << 3; return 0; } for (i = 1; i <= n; i++) { p[i].a = c[i] - 'a' + 1; p[i].x = i; } sort(p + 1, p + 1 + n, cmp); for (i = 1; i <= n; i++) if (p[i].a == p[i - 1].a) sa[p[i].x] = sa[p[i - 1].x]; else sa[p[i].x] = i; for (k = 1; k < n; k *= 2) { for (i = 1; i <= n - k; i++) p[i] = (node){sa[i], sa[i + k], i}; for (i = n - k + 1; i <= n; i++) p[i] = (node){sa[i], 0, i}; sort(p + 1, p + 1 + n, cmp); for (i = 1; i <= n; i++) if (p[i].a == p[i - 1].a && p[i].b == p[i - 1].b) sa[p[i].x] = sa[p[i - 1].x]; else sa[p[i].x] = i; } for (i = 1; i <= n; ++i) rnk[sa[i]] = i; for (i = 1; i <= n; ++i) swap(sa[i], rnk[i]); h[1] = 0; last = 0; for (i = 2; i <= n; ++i) { for (j = 0; c[sa[i - 1] + j] == c[sa[i] + j]; ++j) ; h[i] = j; last = j; } for (i = 1; i < n; ++i) h[i] = h[i + 1]; Build(1, 1, n - 1); last = tot = 1; for (i = 1; i <= n; ++i) { pos[tot + 1] = i; Insert(c[i] - 'a'); } k = 0; for (i = 2; i <= tot; ++i) Push(fa[i], i); dfs(1); cout << ans + siz[1] + 2; }
26
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; int n, m, fa[maxn], f[maxn], d[maxn]; vector<int> G[maxn]; int id(int x, int y) { return (x - 1) * m + y; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { fa[find(x)] = find(y); } int main() { scanf("%d %d", &n, &m); iota(fa + 1, fa + n * m + 1, 1); auto a = vector<vector<int>>(n + 1, vector<int>(m + 1)); vector<pair<int, int>> E; for (int i = 1; i <= n; i++) { vector<int> V; for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]), V.push_back(j); } sort(V.begin(), V.end(), [&](int x, int y) { return a[i][x] < a[i][y]; }); for (int j = 1; j < V.size(); j++) { if (a[i][V[j - 1]] == a[i][V[j]]) unite(id(i, V[j - 1]), id(i, V[j])); else E.emplace_back(id(i, V[j - 1]), id(i, V[j])); } } for (int i = 1; i <= m; i++) { vector<int> V; for (int j = 1; j <= n; j++) { V.push_back(j); } sort(V.begin(), V.end(), [&](int x, int y) { return a[x][i] < a[y][i]; }); for (int j = 1; j < V.size(); j++) { if (a[V[j - 1]][i] == a[V[j]][i]) unite(id(V[j - 1], i), id(V[j], i)); else E.emplace_back(id(V[j - 1], i), id(V[j], i)); } } for (auto e : E) { int u = find(e.first), v = find(e.second); G[u].push_back(v), d[v]++; } queue<int> Q; fill(f + 1, f + n * m + 1, 1); for (int i = 1; i <= n * m; i++) if (i == find(i)) { if (!d[i]) Q.push(i); } while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int u : G[v]) { f[u] = max(f[u], f[v] + 1); if (!--d[u]) Q.push(u); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { printf("%d%c", f[find(id(i, j))], " \n"[j == m]); } } return 0; }
14
#include <bits/stdc++.h> using namespace std; int a[300], b[300], c[300]; void swap(int i) { int temp = b[i]; b[i] = b[i + 1]; b[i + 1] = temp; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); c[i] = b[i]; } int counter = 0; for (int i = 0; i < n; i++) { int k; for (int j = i; j < n; j++) { if (b[j] == a[i]) { k = j; break; } } if (i == k) continue; for (int j = k - 1; j >= i; j--) { swap(j); counter++; } } cout << counter << endl; for (int i = 0; i < n; i++) { b[i] = c[i]; } for (int i = 0; i < n; i++) { int k; for (int j = i; j < n; j++) { if (b[j] == a[i]) { k = j; break; } } if (i == k) continue; for (int j = k - 1; j >= i; j--) { printf("%d %d\n", j + 1, j + 2); swap(j); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { double a[10005], k, max1, min1, sum, x; int n, i; cin >> n >> k; for (i = 0; i < n; i++) cin >> a[i]; min1 = 0; max1 = 1000; while (max1 - min1 > 0.00000001) { sum = 0; x = (max1 + min1) / 2; for (i = 0; i < n; i++) if (a[i] > x) sum += (a[i] - x) * (100 - k) / 100; else sum += (a[i] - x); if (sum > 0) min1 = x; else max1 = x; } printf("%0.9f", x); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, x, y; cin >> n >> x >> y; int a[n + 7]; for (long long i = 0; i < n; i++) { cin >> a[i]; } int count = 0; int index = 0; for (long long i = 0; i < n; i++) { count = 0; for (long long j = i - x; j <= i + y; j++) { if (j != i) { if (j < 0 || j >= n) a[j] = INT_MAX; if (a[i] > a[j]) { count++; } } } if (count == 0) { index = i; break; } } cout << index + 1; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { int n; int m; int x; int k; } p[200005]; bool cmp(node a, node b) { return a.x > b.x; } int main(void) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { scanf("%d", &p[i].n); p[i].k = i; } for (int i = 0; i < n; i++) { scanf("%d", &p[i].m); p[i].x = p[i].m - p[i].n; } sort(p, p + n, cmp); int ans = 0, cnt = 0; for (int i = 0; i < n; i++) { if (p[i].x > 0) { ans += p[i].n; cnt++; } else if (p[i].x <= 0 && cnt < k) { ans += p[i].n; cnt++; } else { ans += p[i].m; } } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; double l[MAXN], r[MAXN]; double add[MAXN]; int main() { int n; long double sy, a, b; cin >> sy >> a >> b; cin >> n; add[0] = 0; for (int i = 1; i <= n; i++) { scanf("%lf%lf", &l[i], &r[i]); add[i] = add[i - 1] + (r[i] - l[i]); } int q; cin >> q; for (int i = 1; i <= q; i++) { long double ans = 0; double x, y; scanf("%lf%lf", &x, &y); double L = (1.0 * a * y - x * sy) / (y - sy); double R = (1.0 * b * y - x * sy) / (y - sy); int L1 = lower_bound(l + 1, l + n + 1, L) - l; L1--; int R1 = lower_bound(r + 1, r + n + 1, R) - r; if (L1 == R1) { ans = R - L; } else { ans = (add[R1 - 1] - add[L1]); if (l[L1] <= L && L <= r[L1]) { ans += (r[L1] - L); } if (l[R1] <= R && R <= r[R1]) { ans += (R - l[R1]); } } ans *= (1.0 * y - sy) / y; printf("%.15lf\n", (double)ans); } return 0; }
16
#include <bits/stdc++.h> using namespace std; template <typename T> void printRange(T &x) { for (auto y : x) cout << y << ' '; cout << endl; } const int maxn = 2e5 + 10; int n, m, u, v; vector<int> adj[maxn]; int color[maxn]; bool odd[maxn]; const int white = 0; const int gray = 1; const int black = 2; vector<tuple<int, int, int>> ans; set<pair<int, int>> fw; void dfs(int u, int par = -1) { color[u] = gray; vector<int> tmp; for (auto v : adj[u]) { if (v != par) { if (color[v] == white) { dfs(v, u); if (!odd[v]) tmp.push_back(v); } else if (color[v] == gray) tmp.push_back(v); } } while (tmp.size() > 1) { int a = tmp.back(); tmp.pop_back(); int b = tmp.back(); tmp.pop_back(); ans.emplace_back(a, u, b); } if (tmp.size() > 0) { odd[u] = true; if (par != -1) ans.emplace_back(par, u, tmp.back()); } color[u] = black; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int(i) = 0; (i) < (m); (i)++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } int part = 0; for (int(i) = (1); (i) <= (n); (i)++) { if (color[i] == white) dfs(i), part++; } cout << ans.size() << endl; for (auto t : ans) { int x, y, z; tie(x, y, z) = t; cout << x << ' ' << y << ' ' << z << '\n'; } }
15
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, d, ans; struct ky { int x, y, u, v; } a[N]; inline void gcd(const int a, const int b) { return !b ? d = a, void() : gcd(b, a % b); } inline int A(const int x) { return x < 0 ? -x : x; } map<pair<double, double>, int> mp; template <class I> inline I Max(I p, I q) { return p > q ? p : q; } template <class I> inline I Min(I p, I q) { return p < q ? p : q; } int main() { scanf("%d", &n); register int i, j; for (i = 1; i <= n; ++i) scanf("%d%d%d%d", &a[i].x, &a[i].y, &a[i].u, &a[i].v); for (i = 1; i <= n; ++i) gcd(A(a[i].v - a[i].y), A(a[i].u - a[i].x)), ans += d + 1; for (i = 1; i <= n; ++i) for (j = 2; j <= n; ++j) { const long long x1 = a[i].x, y1 = a[i].y, x2 = a[i].u, y2 = a[i].v, x3 = a[j].x, y3 = a[j].y, x4 = a[j].u, y4 = a[j].v; const double x = ((double)((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4))) / ((double)((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))), y = ((double)((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4))) / ((double)((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))); if (x != round(x) || y != round(y) || x < Min(x1, x2) || x > Max(x1, x2) || x < Min(x3, x4) || x > Max(x3, x4) || y < Min(y1, y2) || y > Max(y1, y2) || y < Min(y3, y4) || y > Max(y3, y4)) continue; if (!mp.count(make_pair(x, y))) mp[make_pair(x, y)] = i, --ans; else ans -= (mp[make_pair(x, y)] == i); } printf("%d", ans); return 0; }
16
#include <bits/stdc++.h> using namespace std; long long n, i, s, t1 = 1, t2; int main() { cin >> n; for (i = 1; i <= n; i *= 10) s += n - i + 1; cout << s; return 0; }
4
#include <bits/stdc++.h> int main() { char s[101], t[101]; gets(s); gets(t); int flag = 0, i; if (strcmp(s, t) != 0) { for (i = 0; i < strlen(s); i++) { if (s[i] < t[i]) { if (t[i] - s[i] > 1) { s[i] = s[i] + 1; flag = 1; break; } if (t[i] - s[i] == 1) { if (i < strlen(s) - 1) { if (s[i + 1] == 'z') { i++; while (i < strlen(s)) { if (t[i] != 'a') { t[i] = t[i] - 1; flag = 1; strcpy(s, t); break; } i++; } } else { s[i + 1]++; flag = 1; } } } if (flag == 1) break; } } } if (flag == 1) printf("%s", s); else printf("No such string"); }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, res = 0; cin >> n; for (int i = 2; i < n; i++) res += i * (i + 1); cout << res; return 0; }
4
#include <bits/stdc++.h> using namespace std; string g[] = {"Anka", "Chapay", "Cleo", "Troll", "Dracul", "Snowy", "Hexadecimal"}; int main() { set<pair<string, string> > M; int n; scanf("%d", &n); string t1, t2; for (int i = 0; i < n; i++) { cin >> t1 >> t2 >> t2; M.insert(make_pair(t1, t2)); } int x, y, z, p, q, r, mmin, mmax, diff = -1, like_cnt = 0, cnt, gmin = -1; scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); do { for (int i = 0; i < 5; i++) for (int j = i + 1; j < 6; j++) { int k = 6; cnt = 0; p = x / (i + 1); q = y / (j - i); r = z / (k - j); mmin = p < q ? p : q; mmin = mmin < r ? mmin : r; mmax = p > q ? p : q; mmax = mmax > r ? mmax : r; diff = mmax - mmin; if (diff < gmin || gmin == -1) { gmin = diff; if (gmin == 15) { printf("%d ", i); printf("%d ", j); printf("%d\n", k); printf("%d ", mmax); printf("%d\n", mmin); } for (int l = 0; l <= i; l++) for (int m = 0; m <= i; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; for (int l = i + 1; l <= j; l++) for (int m = i + 1; m <= j; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; for (int l = j + 1; l <= k; l++) for (int m = j + 1; m <= k; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; like_cnt = cnt; } else if (diff == gmin) { for (int l = 0; l <= i; l++) for (int m = 0; m <= i; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; for (int l = i + 1; l <= j; l++) for (int m = i + 1; m <= j; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; for (int l = j + 1; l <= k; l++) for (int m = j + 1; m <= k; m++) if (M.find(make_pair(g[l], g[m])) != M.end()) cnt++; like_cnt = cnt > like_cnt ? cnt : like_cnt; } } } while (next_permutation(g, g + 7)); printf("%d ", gmin); printf("%d\n", like_cnt); }
6
#include <bits/stdc++.h> using namespace std; int n, m; int a[1 << 22]; int flag[1 << 22], vis[1 << 22]; void dfs(int x) { vis[x] = 1; int tmp = (1 << n) - 1 - x; if (flag[x] && !vis[tmp]) dfs(tmp); for (int i = 0; i < n; i++) { int cnt = x ^ (1 << i); if (x & (1 << i) && !vis[cnt]) dfs(cnt); } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d", &a[i]); flag[a[i]] = 1; } sort(a, a + m); int ans = 0; for (int i = m - 1; i >= 0; i--) { if (!vis[a[i]]) { ans++; dfs((1 << n) - 1 - a[i]); } } printf("%d\n", ans); return 0; }
17
#include <bits/stdc++.h> using namespace std; int n, m; long long s[1000005], l[1000005], r[1000005]; long long ans = (1LL << 60); int main() { scanf("%d %d", &n, &m); for (int i = (1); i <= (n); i++) scanf("%lld", &s[i]); m = min(m, n); for (int i = (1); i <= (n); i++) { l[i] = l[i - 1]; if ((i - 1) % m == 0) l[i] += s[i]; } for (int i = (n); i >= (1); i--) { r[i] = r[i + 1]; if ((n - i) % m == 0) r[i] += s[i]; } for (int i = (1); i <= (n); i++) { int cl = (i - 1) / m + 1; int cr = (n - i) / m + 1; long long cost = s[i] * cl - l[i] + r[i] - s[i] * cr; ans = min(ans, cost); } printf("%lld\n", 2 * ans); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, res; int p[N], c[N]; vector<int> color; bool vis[N]; void get_res(int n, int d) { for (int i = 0; i < d; i++) { bool success = true; for (int j = i + d; j < n; j += d) if (color[i] != color[j]) { success = false; break; } if (success) { res = min(res, d); break; } } return; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; vis[i] = false; } for (int i = 1; i <= n; i++) cin >> c[i]; res = n; for (int i = 1; i <= n; i++) if (!vis[i]) { color.clear(); for (int j = i; !vis[j]; j = p[j]) { vis[j] = true; color.push_back(c[j]); } int sz = color.size(); for (int i = 1; i <= sz / i; i++) if (sz % i == 0) { get_res(sz, i); if (i != sz / i) get_res(sz, sz / i); } } cout << res << endl; } return 0; }
14
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353, maxn = 3e5 + 1, inf = 1e9, alpha = 26, maxk = 20; long long t[maxn]; long long bp(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long inv(long long x) { return bp(x, mod - 2); } long long c(long long n, long long k) { return t[n] * inv(t[k]) % mod * inv(t[n - k]) % mod; } long long a[300000]; void solve() { t[0] = 1; for (long long i = 1; i <= maxn; i++) t[i] = (t[i - 1] * i) % mod; long long n; cin >> n; for (long long i = 0; i < n * 2; i++) { cin >> a[i]; } long long k = c(2 * n - 1, n - 1) * 2; sort(a, a + 2 * n); long long sum = 0; for (long long i = 0; i < n * 2; i++) { if (i >= n) { sum += a[i] * k; sum %= mod; } else { sum -= a[i] * k; sum %= mod; sum = (sum + mod) % mod; } } cout << sum; } signed main() { long long t = 1; while (t--) { solve(); } }
11
#include <bits/stdc++.h> using namespace std; using namespace std; struct ins { int ans; ins() { ans = 1; } void read() {} void read1(char &s) { char c = getchar(); for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar()) ; s = c; if (c == EOF) ans = 0; } void read1(string &s) { s = ""; char c = getchar(); for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar()) ; for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar()) s += c; if (c == EOF) ans = 0; } template <typename T> void read1(T &n) { T x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -1; if (c == EOF) { ans = 0; return; } } for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; n = x * f; if (c == EOF) ans = 0; if (c != '.') return; T l = 0.1; while ((c = getchar()) <= '9' && c >= '0') x = x + (c & 15) * l, l *= 0.1; n = x * f; if (c == EOF) ans = 0; } void write() {} void write1(string s) { int n = s.size(); for (int i = 0; i < n; i++) putchar(s[i]); } void write1(const char *s) { int n = strlen(s); for (int i = 0; i < n; i++) putchar(s[i]); } void write1(char s) { putchar(s); } void write1(float s, int x = 6) { char y[10001]; sprintf(y, "%%.%df", x); printf(y, s); } void write1(double s, int x = 6) { char y[10001]; sprintf(y, "%%.%dlf", x); printf(y, s); } void write1(long double s, int x = 6) { char y[10001]; sprintf(y, "%%.%dLf", x); printf(y, s); } template <typename T> void write1(T n) { if (n < 0) n = -n, putchar('-'); if (n > 9) write1(n / 10); putchar('0' + n % 10); } template <typename T> friend ins operator>>(ins x, T &n); template <typename T> friend ins operator<<(ins x, T n); operator bool() { return ans; } }; template <typename T> ins operator>>(ins x, T &n) { if (!x.ans) return x; x.read1(n); return x; } template <typename T> ins operator<<(ins x, T n) { x.write1(n); return x; } ins yin; ins yout; int n; long long w1, w2, z1, z2; char s[1000001]; int main() { yin >> n; scanf("%s", s + 1); for (int i = 1; i <= n / 2; i++) { if (s[i] == '?') ++w1; else z1 += s[i] - '0'; } for (int i = n / 2 + 1; i <= n; i++) { if (s[i] == '?') ++w2; else z2 += s[i] - '0'; } if (w1 > w2) { if (((w1 - w2) / 2 * 9) == z2 - z1) puts("Bicarp"); else puts("Monocarp"); } else { if ((w2 - w1) / 2 * 9 == z1 - z2) puts("Bicarp"); else puts("Monocarp"); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2; cin >> s1 >> s2; long long cnt = 0; long long l = s1.size(); for (long long i = 0; i < l; i++) { if (s1[i] != s2[i]) { cnt += 1; } } if (cnt % 2) cout << "impossible"; else { long long j = 1; for (long long i = 0; i < l; i++) { if (s1[i] != s2[i] && j % 2 != 0) { cout << s1[i]; j += 1; } else if (s1[i] != s2[i] && j % 2 == 0) { cout << s2[i]; j += 1; } else { cout << s1[i]; } } } return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:367772116") using namespace std; long long getsum(long long cur) { long long mn = 1; while (cur) { mn *= cur % 10; cur /= 10; } return mn; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long bestans = getsum(n); long long cur = 10; for (size_t i = 0; i < 10; i++) { bestans = max(bestans, getsum(n - (n % cur + 1))); cur *= 10; } cout << bestans; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") using namespace std; const double eps = 1E-9; const double Exp = 2.718281828459045; const double Pi = 3.1415926535897932; const int NMAX = 100000 + 5; const int MMAX = 300 + 5; const int INF = 1000000000; const int BASE = 1000000007; template <typename T> inline T abs(const T a) { if (a < 0) return -a; return a; } template <typename T> inline T sqr(const T& a) { return a * a; } static int b[NMAX]; static int a[NMAX]; vector<int> t[4 * NMAX]; void make(int v, int tl, int tr) { if (tl == tr) { t[v].push_back(a[tl]); return; } int tm = (tl + tr) / 2; make(2 * v, tl, tm); make(2 * v + 1, tm + 1, tr); t[v].resize(int((t[2 * v]).size()) + int((t[2 * v + 1]).size())); merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin()); } int query(int v, int tl, int tr, int l, int r, int x) { if (tl == tr) { } } int main() { ios::sync_with_stdio(false); int n, m, s, e; int i, j, k; static int dp[MMAX][NMAX]; static vector<int> next[NMAX]; cin >> n >> m >> s >> e; for (i = 1; i <= n; i++) { cin >> a[i]; next[a[i]].push_back(i); } for (i = 0; i < NMAX; i++) { next[i].push_back(n + 1); } for (i = 1; i <= m; i++) cin >> b[i]; for (i = 0; i <= s / e + 1; i++) for (j = 0; j <= m; j++) dp[i][j] = INF; for (j = 0; j <= m; j++) { dp[0][j] = 0; } make(1, 1, n); int mn = INF; for (i = 1; i <= s / e; i++) { mn = INF; for (j = 1; j <= m; j++) { mn = min(mn, dp[i - 1][j - 1]); if (mn == INF) dp[i][j] = INF; else dp[i][j] = *lower_bound(next[b[j]].begin(), next[b[j]].end(), mn + 1); if (dp[i][j] == n + 1) dp[i][j] = INF; } } int ok = 0; for (i = 1; i <= s / e; i++) { for (j = 1; j <= m; j++) { if ((j + dp[i][j] + i * e) <= s) { ok = i; break; } } } cout << ok << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 2001 * 2001; const int dx[5] = {0, 0, 1, -1}; const int dy[5] = {1, -1, 0, 0}; int main() { int n, m; cin >> n >> m; vector<string> A(n); vector<vector<int>> G(n * m, vector<int>(4, -1)); int clear = 0; for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (A[i][j] == '*') continue; clear++; for (int k = 0; k < 4; ++k) { int newX = i + dx[k]; int newY = j + dy[k]; if (newX < 0 || newX >= n || newY < 0 || newY >= m) continue; if (A[newX][newY] == '*') continue; G[i * m + j][k] = (newX * m + newY); } } } vector<int> dg(n * m, 0); vector<int> deleted(n * m, 0); queue<int> Q; for (int i = 0; i < n * m; ++i) { for (int j = 0; j < 4; ++j) if (G[i][j] != -1) dg[i]++; if (dg[i] == 1) Q.push(i); } int dominos = 0; while (!Q.empty()) { int now = Q.front(); Q.pop(); for (int i = 0; i < 4; ++i) if (G[now][i] != -1 && !deleted[G[now][i]]) { dominos++; int x = now / m, y = now % m; int x2 = G[now][i] / m, y2 = G[now][i] % m; if (x == x2) { A[x][min(y, y2)] = '<'; A[x][max(y, y2)] = '>'; } else { A[min(x, x2)][y] = '^'; A[max(x, x2)][y] = 'v'; } deleted[now] = 1; int other = G[now][i]; for (int j = 0; j < 4; ++j) if (G[other][j] != -1 && !deleted[G[other][j]]) { dg[G[other][j]]--; if (dg[G[other][j]] == 1) Q.push(G[other][j]); } deleted[other] = 1; } } if (clear % 2 || dominos != clear / 2) { cout << "Not unique\n"; } else { for (auto line : A) cout << line << "\n"; } }
12
#include <bits/stdc++.h> const int N = 257, p = 31, mod = 1e9 + 7; char tab[N][N]; long long hash[N][N]; long long lit[30]; int n, m; int wyst[30]; bool kk[N][N][N]; int man[N], manparz[N]; void init() { long long akt = 1; for (int i = 0; i < 30; i++) { lit[i] = akt; akt *= p; akt %= mod; } } void hashuj(char *t, long long *h) { for (int i = 1; i <= m; i++) { h[i] = (h[i - 1] + lit[t[i] - 'a']) % mod; } } void check(int n, int m) { for (int y = 1; y <= n; y++) { for (int lx = 1; lx <= m; lx++) { int ileNieparzystych = 0; for (auto &it : wyst) it = 0; for (int rx = lx; rx <= m; rx++) { wyst[tab[y][rx] - 'a']++; wyst[tab[y][rx] - 'a'] % 2 == 0 ? ileNieparzystych-- : ileNieparzystych++; kk[y][lx][rx] = ileNieparzystych <= 1; } } } } long long manacher(int lx, int rx) { long long ret = 0; int l = 0, r = 0; for (int sr = 1; sr <= n; sr++) { if (!kk[sr][lx][rx]) { man[sr] = 0; continue; } int dl = r < sr ? 0 : std::min(man[l + r - sr], r - sr + 1); while (sr - dl > 0 && sr + dl <= n && kk[sr - dl][lx][rx] && kk[sr + dl][lx][rx] && (((hash[sr - dl][rx] - hash[sr - dl][lx - 1]) + mod) % mod) == (((hash[sr + dl][rx] - hash[sr + dl][lx - 1]) + mod) % mod)) dl++; if (sr + dl - 1 > r) { l = sr - dl + 1; r = sr + dl - 1; } man[sr] = dl; ret += dl; } return ret; } long long manacherParz(int lx, int rx) { long long ret = 0; int l = 0, r = 0; for (int sr = 1; sr <= n; sr++) { int dl = r < sr ? 0 : std::min(manparz[l + r - sr + 1], r - sr + 1); while (sr - dl - 1 > 0 && sr + dl <= n && kk[sr - dl - 1][lx][rx] && kk[sr + dl][lx][rx] && (((hash[sr - dl - 1][rx] - hash[sr - dl - 1][lx - 1]) + mod) % mod) == (((hash[sr + dl][rx] - hash[sr + dl][lx - 1]) + mod) % mod)) dl++; if (sr + dl - 1 > r) { l = sr - dl; r = sr + dl - 1; } manparz[sr] = dl; ret += dl; } return ret; } int main() { init(); scanf("%d%d", &n, &m); for (int y = 1; y <= n; y++) { scanf("%s", tab[y] + 1); hashuj(tab[y], hash[y]); } check(n, m); long long wynik = 0; for (int lx = 1; lx <= m; lx++) { for (int rx = lx; rx <= m; rx++) { wynik += manacher(lx, rx); wynik += manacherParz(lx, rx); } } printf("%lld", wynik); return 0; }
16
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 2005, md = 998244353; inline int add(int x, int y) { x += y; if (x >= md) x -= md; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += md; return x; } inline int mul(int x, int y) { return 1ll * x * y % md; } inline int fpow(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = mul(ans, x); y >>= 1; x = mul(x, x); } return ans; } char c[N]; int fac[N], inv[N], s1[N], s2[N], s3[N]; int n, ans; inline int C(int n, int m) { if (n < 0 || m < 0 || n < m) return 0; return mul(fac[n], mul(inv[m], inv[n - m])); } int main() { scanf("%s", c + 1); n = strlen(c + 1); fac[0] = 1; for (register int i = 1; i <= n; i++) { s1[i] = s1[i - 1] + (c[i] == '('); s2[i] = s2[i - 1] + (c[i] == ')'); s3[i] = s3[i - 1] + (c[i] == '?'); fac[i] = mul(fac[i - 1], i); } inv[n] = fpow(fac[n], md - 2); for (register int i = n; i >= 1; i--) inv[i - 1] = mul(inv[i], i); for (register int cnt = s2[n]; cnt <= n - s1[n]; cnt++) { for (register int res = s2[n] - s2[cnt]; res <= (n - cnt) - (s1[n] - s1[cnt]); res++) { ans = add(ans, 1ll * res * C(s3[n] - s3[cnt], (res - (s2[n] - s2[cnt]))) % md * C(s3[cnt], cnt - res - s2[cnt]) % md); } } print(ans, '\n'); return 0; }
18