solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int mo = 1e9 + 7; const int N = 2e5 + 10; int n, k, fac[N] = {1}, fav[N]; int qpow(int a, int b) { int ans = 1; for (; b >= 1; b >>= 1, a = 1ll * a * a % mo) if (b & 1) ans = 1ll * ans * a % mo; return ans; } int C(int n, int m) { return fac[n] * 1ll * fav[m] % mo * fav[n - m] % mo; } map<int, int> Map; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mo; fav[n] = qpow(fac[n], mo - 2); for (int i = n; i >= 1; i--) fav[i - 1] = 1ll * fav[i] * i % mo; for (int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &r); Map[l]++; Map[r + 1]--; } int ans = 0, cnt = 0; for (map<int, int>::iterator it = Map.begin(); it != Map.end(); it++) { map<int, int>::iterator nxt = it; nxt++; cnt += it->second; if (nxt != Map.end()) { if (cnt >= k) { ans += 1ll * (nxt->first - it->first) * C(cnt, k) % mo; if (ans >= mo) ans -= mo; } } } printf("%d\n", ans); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int N = 1000005; const double PI = 4 * atan(1); long long m, x, n, p, k, q, l, r; long long a, b; long long x0, y, x2, y2; map<pair<long long, long long>, bool> isbehy; map<pair<long long, long long>, bool> vis; map<pair<long long, long long>, long long> dist; int dx[] = {1, -1, 1, -1, 0, 0, 1, -1}; int dy[] = {1, -1, -1, 1, 1, -1, 0, 0}; bool ingrid(long long x, long long y) { return isbehy[make_pair(x, y)] == 1 && vis[make_pair(x, y)] == 0 && x >= 1 && x <= 1e9 && y >= 1 && y <= 1e9 && dist[make_pair(x, y)] == 0; } long long bfs() { queue<pair<long long, long long> > q; q.push(make_pair(x0, y)); pair<long long, long long> ras; while (!q.empty()) { ras = q.front(); q.pop(); if (vis[ras]) continue; vis[ras] = 1; for (int i = 0; i < 8; i++) { if (ingrid(ras.first + dx[i], ras.second + dy[i])) { dist[make_pair(ras.first + dx[i], ras.second + dy[i])] = dist[ras] + 1; q.push(make_pair(ras.first + dx[i], ras.second + dy[i])); } } } if (dist[make_pair(x2, y2)] == 0) return -1; return dist[make_pair(x2, y2)]; } int main() { ios::sync_with_stdio(0); cin >> x0 >> y >> x2 >> y2; cin >> n; for (int i = 0; i < n; i++) { cin >> r >> a >> b; for (int j = a; j <= b; j++) { isbehy[make_pair(r, j)] = 1; } } long long ans = bfs(); cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 0; for (; c < '0' || c > '9'; f |= c == '-', c = getchar()) ; for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar()) ; if (f) x = -x; } const int mo = 1e9 + 7; inline int add(int x) { return x >= mo ? x - mo : x; } inline int mul(int x, int y) { return (long long)x * y % mo; } inline void U(int &x, int y) { x = add(x + y); } const int N = 102; int n, m, K, dp[2][N][N][N]; long double c[N][N]; inline void myAssert(int x) { assert(0 <= x && x < N); } int main(void) { read(n); read(m); read(K); ++n; for (register int i = 0; i < (int)n + 1; i++) c[i][0] = 1; for (register int i = 1; i <= (n); i++) for (register int j = 1; j <= (i); j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; int cur = 1; for (int em = 1; em + 1 <= n; em++) { dp[cur][em + 1][em][1] = 1; } int ans = 0; for (int val = 2; val <= m; val++) { int pre = cur; cur ^= 1; memset(dp[cur], 0, sizeof dp[cur]); for (int em = 1; em <= n; em++) for (int pl = 1; pl + em <= n; pl++) for (int pg = 1; pg <= K; pg++) if (dp[pre][pl][em][pg]) { for (int nc = em; nc + pl <= n; nc++) { long double good = pg * c[nc - 1][em - 1]; if (good <= K) { U(dp[cur][pl + nc][nc - em][(int)good], dp[pre][pl][em][pg]); } } } for (int len = 1; len <= n; len++) for (int good = 1; good <= K; good++) U(ans, mul(m - val + 1, dp[cur][len][0][good])); } cout << ans << "\n"; }
20
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10, mod = 1e9 + 7; inline int ad(int x, int y) { int ans = x + y; if (ans > mod) ans -= mod; return ans; } inline int mu(int x, int y) { return 1LL * x * y % mod; } inline int wop(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = mu(ret, x); y >>= 1; x = mu(x, x); } return ret; } int fact[maxn], inv[maxn], lst[maxn], pr[maxn], cnt[maxn]; int num[maxn][20]; int pw[maxn], pww[maxn]; inline void prep() { int n = maxn - 5; fact[0] = 1; for (int i = 1; i < n; i++) fact[i] = mu(fact[i - 1], i); for (int i = 0; i < n; i++) inv[i] = wop(fact[i], mod - 2); pr[0] = pr[1] = 1; for (int i = 2; i < 300005; i++) { if (pr[i] == 0) { lst[i] = i; for (int j = i * 2; j < 300005; j += i) lst[j] = i, pr[j] = 1; } } for (int i = 1; i < n; i++) pw[i] = ad(mu(pw[i - 1], 2), 1); pww[0] = 1; for (int i = 1; i < n; i++) pww[i] = ad(pww[i - 1], pww[i - 1]); } inline int C(int r, int n) { if (r < 0 || r > n) return 0; return mu(mu(fact[n], inv[n - r]), inv[r]); } inline int cal(int x, int y) { if (x < 0 || y < 1) return 0; if (x == 0) return pw[y]; int ans = 0; for (int i = 1; i <= y; i++) ans = ad(ans, mu(pw[i], C(x - 1, x - 1 + y - i))); return ans; } inline int f(int x, int y) { if (y <= x) return cal(x, y); int oth = cal(y, x); int eq = 0; for (int i = 0; i <= min(x, y); i++) eq = ad(eq, mu(C(i, x), C(i, y))); oth = ad(oth, eq); return ad(pww[x + y], mod - oth); } inline int ff(int x, int y) { return f(x + 1, y - 1); } int ps[20], answer[20]; int solve(vector<int> a, bool rev) { int n = a.size(); for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i - 1]; int ans = 0; for (int j = 1; j < n; j++) { int tmp; if (rev) tmp = f(ps[j] - 1, ps[n] - ps[j]); if (!rev) tmp = ff(ps[j] - 1, ps[n] - ps[j]); answer[j] = tmp; } for (int i = 0; i < n; i++) { int sum = 0; for (int j = i + 1; j < n; j++) sum = ad(sum, answer[j]); ans = ad(ans, mu(sum, a[i])); } return ans; } int main() { prep(); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); cnt[x]++; } for (int i = 1; i <= 1e6; i++) { if (cnt[i] == 0) continue; int cur = i; while (cur != 1) { int p = lst[cur], tav = 0; while (cur % p == 0) cur /= p, tav++; num[p][tav] += cnt[i]; } } int ans = 0; for (int i = 2; i <= 300000; i++) { if (pr[i]) continue; vector<int> a; int sum = 0; for (int j = 1; j < 20; j++) sum += num[i][j]; if (sum == 0) continue; a.push_back(n - sum); for (int j = 1; j < 20; j++) a.push_back(num[i][j]); ans = ad(ans, solve(a, 0)); reverse(a.begin(), a.end()); ans = ad(ans, solve(a, 1)); } cout << ans << endl; }
23
#include <bits/stdc++.h> using namespace std; const int M = 5101; const int N = 330000; const long long oo = 4111111111L; int n, k; long long a[N]; long long f[M][M]; int main() { while (~scanf("%d%d", &n, &k)) { for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); sort(a + 1, a + n + 1); int p = n / k, q = n % k; if (n <= k) { printf("0\n"); continue; } { for (int i = 0; i <= k - q; i++) { for (int j = 0; j <= q; j++) { f[i][j] = oo; } } f[0][0] = 0; for (int i = 0; i <= k - q; i++) { for (int j = 0; j <= q; j++) { if (i) f[i][j] = min(f[i][j], f[i - 1][j] + a[i * p + j * (p + 1)] - a[(i - 1) * p + j * (p + 1) + 1]); if (j) f[i][j] = min(f[i][j], f[i][j - 1] + a[i * p + j * (p + 1)] - a[(i)*p + (j - 1) * (p + 1) + 1]); } } } printf("%I64d\n", f[k - q][q]); } return 0; }
12
#include <bits/stdc++.h> int main() { int t; char s[1000]; char f[] = {"po"}; char j1[] = {"desu"}; char j2[] = {"masu"}; scanf("%d", &t); while (t--) { int n; scanf("%s", s); n = strlen(s); if (strncmp(s + n - 2, f, 2) == 0) printf("FILIPINO\n"); else if (strncmp(s + n - 4, j1, 4) == 0 || strncmp(s + n - 4, j2, 4) == 0) printf("JAPANESE\n"); else printf("KOREAN\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; long long int a[n], s = 0; double sum; map<long long int, long long int> m; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; s += 2 * a[i]; } sum = (s * 1.0) / n; if (floor(sum) - sum != 0) { cout << "0\n"; return; } long long int count = 0; for (int i = 0; i < n; i++) { long long int data = sum - a[i]; m[a[i]]--; count += m[data]; } cout << count << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; bool check[MAXN + 10]; int dp[MAXN + 10]; void init() { int n; int ans = 0; cin >> n; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (check[x - 1]) dp[x] = dp[x - 1] + 1; else dp[x] = 1; check[x] = true; if (dp[x] > ans) ans = dp[x]; } cout << n - ans; return; } int main() { init(); return 0; }
8
#include <bits/stdc++.h> using namespace std; inline int sum(int n) { return (n * (n + 1)) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, t, a, b; cin >> a >> b; int x = abs(a - b); if (x % 2 == 0) { x /= 2; cout << 2 * sum(x) << endl; } else { x /= 2; x++; cout << sum(x) + sum(x - 1) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n, k, d, res = 0, flag[1000] = {0}, a[1000]; cin >> n >> k >> d; int an = n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= d; i++) { flag[a[i]]++; if (flag[a[i]] == 1) res++; } an = min(an, res); for (int i = 2; i <= n - d + 1; i++) { flag[a[i - 1]]--; if (flag[a[i - 1]] == 0) res--; flag[a[i + d - 1]]++; if (flag[a[i + d - 1]] == 1) res++; an = min(an, res); } cout << an << endl; } }
2
#include <bits/stdc++.h> using namespace std; constexpr long long INF = 0x1fffffffffffffff; constexpr int inf = 0x3fffffff; constexpr double inf_l = 1e18; constexpr long long MOD = 1000000007LL; constexpr int mod = 1000000007; vector<long long> f(100001), w(100001); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, k; cin >> n >> k; for (int i = 0; i < n; ++i) cin >> f[i]; for (int i = 0; i < n; ++i) cin >> w[i]; int limit = 0; while (1LL << limit <= k) ++limit; vector<vector<long long>> dps(limit + 1, vector<long long>(n, 0)), dpm(limit + 1, vector<long long>(n, INF)), dpto(limit + 1, vector<long long>(n, 0)); for (int i = 0; i < n; ++i) { dps[0][i] = w[i]; dpm[0][i] = w[i]; dpto[0][i] = f[i]; } for (int i = 1; i <= limit; ++i) { for (int j = 0; j < n; ++j) { dps[i][j] = dps[i - 1][j] + dps[i - 1][dpto[i - 1][j]]; dpm[i][j] = min(dpm[i - 1][j], dpm[i - 1][dpto[i - 1][j]]); dpto[i][j] = dpto[i - 1][dpto[i - 1][j]]; } } vector<int> e; for (int i = 0; 1LL << i <= k; ++i) { if ((1LL << i) & k) { e.emplace_back(i); } } for (int i = 0; i < n; ++i) { long long sum = 0, mn = INF; int p = i; for (int j = 0; j < e.size(); ++j) { sum += dps[e[j]][p]; mn = min(mn, dpm[e[j]][p]); p = dpto[e[j]][p]; } cout << sum << ' ' << mn << endl; } }
13
#include <bits/stdc++.h> using namespace std; int main() { int maxno; cin >> maxno; int ans = 0; for (int a = 2; a <= maxno; a++) { int n = a; int i = 0; vector<int> res; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); while (n % i == 0) { n = n / i; } } } if (n != 1) res.push_back(n); if (res.size() == 2) ans++; } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; double eps = 1e-10; int n; double a[2000]; double mn, mx; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; mn = 0, mx = 999999999.0; for (int i = 0; i < n; i++) mn = max(mn, a[i] / (double)(i + 1)), mx = min(mx, (a[i] + 1) / (double)(i + 1)); if ((int)((double)(n + 1) * mn) == (int)((double)(n + 1) * mx - eps)) { cout << "unique\n"; cout << (int)((double)(n + 1) * mn) << endl; } else cout << "not unique\n"; { int _; cin >> _; } }
10
#include <bits/stdc++.h> using namespace std; long long ok(long long a, long long b, long long c, long long d, long long e, long long f) { return ((c - a) * (f - b)) == ((e - a) * (d - b)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, i, j, k, l, t, r, e, p = 0, f; cin >> n; long long arr[n]; for (i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long sum, rum, kum, cum, val; for (i = 0; i <= 100; i++) { p = -1; sum = arr[0] + i; cum = arr[0] - i; kum = arr[n - 1] + i; rum = arr[n - 1] - i; if (sum == arr[n - 1]) val = sum; else if (arr[n - 1] == cum) val = cum; else if (arr[0] == kum) val = kum; else if (arr[0] == rum) val = rum; else if (sum == kum) { val = sum; } else if (sum == rum) val = sum; else if (cum == kum) val = cum; else if (cum == rum) val = cum; else continue; for (j = 1, k = n - 2; j <= k; j++, k--) { if (arr[j] + i == val || arr[j] == val) { if (arr[k] - i == val || arr[k] + i == val || arr[k] == val) continue; else { p = 1; break; } } else if (arr[j] - i == val || arr[j] == val) { if (arr[k] - i == val || arr[k] + i == val || arr[k] == val) continue; else { p = 1; break; } } else { p = 1; break; } } if (p == -1 && j > k) { cout << i << endl; return 0; } } cout << "-1" << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, d, a[110], x[110], y[110], vis[110], cost[110]; int D(int i, int j) { return d * (abs(x[i] - x[j]) + abs(y[i] - y[j])); } int Solve() { int i, v; for (i = 0; i <= n; i++) { vis[i] = 0; cost[i] = 10E8; } cost[n] = 0; v = 0; while (v != -1) { vis[v] = 1; for (i = 1; i <= n; i++) if (cost[i] > cost[v] + max(0, D(v, i) - a[i])) cost[i] = cost[v] + max(0, D(v, i) - a[i]); v = -1; for (i = 1; i <= n; i++) if (!vis[i] && (v == -1 || cost[i] < cost[v])) v = i; } return cost[1]; } int main(void) { int i; while (scanf("%d %d", &n, &d) != EOF) { for (i = 2; i < n; i++) scanf("%d", &a[i]); a[1] = 0; a[n] = 0; for (i = 1; i <= n; i++) scanf("%d %d", &x[i], &y[i]); printf("%d\n", Solve()); } return 0; }
13
#include <bits/stdc++.h> using namespace std; long long i, j, n, l, t, k, w, z, h, m, s; long long a[1235], b[12345], d[12346], x[123455], c[12345], f[123434]; double y; string q, qq, p, r; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; s = s + a[i]; k = k + b[i]; t = t + c[i]; } if (s == 0 && k == 0 && t == 0) { cout << "YES"; } else { cout << "NO"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const double EPS = -1e8; const double Pi = acos(-1); bool inline equ(double a, double b) { return fabs(a - b) < EPS; } const int MAXN = 100010; struct DSJ { int mom[MAXN]; void init(int x) { for (int i = 1; i <= x; i++) mom[i] = i; } int find(int x) { return mom[x] = mom[x] == x ? x : find(mom[x]); } void merge(int a, int b) { mom[find(a)] = find(b); } }; int n, m; vector<int> g[MAXN], rg[MAXN], vs; bool vis[MAXN]; DSJ dsj; int id[MAXN]; vector<int> scc[MAXN]; bool ok[MAXN]; int sz[MAXN]; void dfs(int u) { vis[u] = 1; for (auto v : g[u]) if (!vis[v]) dfs(v); vs.push_back(u); } void rdfs(int u, int mk) { vis[u] = 1; id[u] = mk; scc[mk].push_back(u); for (auto v : rg[u]) if (!vis[v]) rdfs(v, mk); } int main() { scanf("%d%d", &n, &m); dsj.init(n); for (int i = (1); i <= (m); i++) { int u, v; scanf("%d%d", &u, &v); dsj.merge(u, v); g[u].push_back(v); rg[v].push_back(u); } for (int i = (1); i <= (n); i++) sz[dsj.find(i)]++; int cnt = 0; for (int i = (1); i <= (n); i++) if (!vis[i]) dfs(i); reverse((vs).begin(), (vs).end()); fill(vis, vis + MAXN, 0); for (int u : vs) if (!vis[u]) { rdfs(u, ++cnt); if ((int)(scc[cnt]).size() > 1) ok[dsj.find(u)] = 1; } int ans = 0; for (int i = (1); i <= (n); i++) if (sz[i] > 0) { if (ok[i]) ans += sz[i]; else ans += sz[i] - 1; } printf("%d\n", ans); }
14
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << '\n'; err(++it, args...); } long long MOD = 1000000009; int n, m; int N = 100001; vector<pair<pair<long long int, long long int>, pair<long long int, long long int>>> adj(N); vector<vector<int>> g(N); vector<long long int> vis(N); vector<long long int> deg(N); vector<long long int> t; vector<long long int> ans2; bool check(long long int k) { g.clear(); g.resize(n + 1); vis.resize(n + 1); vis.assign(n + 1, 0); deg.resize(n + 1); deg.assign(n + 1, 0); for (int i = 0; i < m; i++) { if (adj[i].second.first > k) { g[adj[i].first.first].push_back(adj[i].first.second); deg[adj[i].first.second]++; } } queue<int> q; for (int i = 1; i <= n; i++) { if (deg[i] == 0) q.push(i); } t.clear(); while (!q.empty()) { int vert = q.front(); q.pop(); t.emplace_back(vert); vis[vert] = 1; for (auto i : g[vert]) { if (vis[i] == 0) { deg[i]--; if (deg[i] == 0) q.push(i); } } } if (t.size() == n) return true; else return false; } void topo(int p) { g.clear(); g.resize(n + 1); vis.resize(n + 1); vis.assign(n + 1, 0); deg.resize(n + 1); deg.assign(n + 1, 0); queue<int> q; for (int i = 0; i < m; i++) { if (adj[i].second.first > p) { g[adj[i].first.first].push_back(adj[i].first.second); deg[adj[i].first.second]++; } } for (int i = 1; i <= n; i++) { if (deg[i] == 0) q.push(i); } t.clear(); while (!q.empty()) { int vert = q.front(); q.pop(); t.emplace_back(vert); vis[vert] = 1; for (auto i : g[vert]) { if (vis[i] == 0) { deg[i]--; if (deg[i] == 0) q.push(i); } } } int ind[n + 1]; for (int i = 0; i < n; i++) ind[t[i]] = i; for (int i = 0; i < m; i++) { if (adj[i].second.first <= p) { if (ind[adj[i].first.first] > ind[adj[i].first.second]) ans2.emplace_back(i); } } } void solve() { cin >> n >> m; for (int i = 0; i < m; i++) { long long int u, v, c; cin >> u >> v >> c; adj[i] = make_pair(make_pair(u, v), make_pair(c, i)); } long long int lb = 0, ub = 1e9, ans = ub; while (ub >= lb) { long long int mm = lb + (ub - lb) / 2; if (check(mm)) { ans = mm; ub = mm - 1; } else lb = mm + 1; } topo(ans); cout << ans << " "; cout << ans2.size() << '\n'; for (auto i : ans2) cout << i + 1 << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
14
#include <bits/stdc++.h> using namespace std; const unsigned long long INF = ~0ull; const int N = 1e6 + 15; bool used[N]; int a[N], apos; void init() { memset(used, true, sizeof(used)); apos = 1; used[0] = used[1] = false; for (int i = 2; i < N; i++) { if (used[i]) { for (int j = 2; i * j < N; j++) { used[i * j] = false; } } } } int reverse(int x) { int ans = 0; while (x) { ans = ans * 10 + x % 10; x /= 10; } return ans; } int main() { init(); int n = 0; for (int i = 2; n < 11184; i++) { int x = i; int y = reverse(x); if (x != y && used[x] && used[y]) { a[apos++] = x; n++; } } int in; while (~scanf("%d", &in)) { printf("%d\n", a[in]); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { string s; cin >> s; int len = s.size(), c1 = 0; for (int i = 0; i < len; i++) { if (s[i] == 'a' && s[i + 1] == 'b') c1++; else if (s[i] == 'b' && s[i + 1] == 'a') c1--; } if (c1 == 0) cout << s; else if (c1 == 1) { s.erase(len - 1, 1); cout << s << 'a'; } else { s.erase(len - 1, 1); cout << s << 'b'; } cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, u = 0; string s; cin >> n >> x >> y >> s; deque<long long> A; if (s[0] == '0') A.push_back(0), u++; else A.push_back(1); for (int i = 1; i < n; i++) if (s[i - 1] != s[i]) { A.push_front(s[i] - 48); u += A[0] == 0; } cout << min((u - (u != 0)) * x + y, u * y); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const double eps = 1e-9; const int N = 2e5 + 10; vector<int> v[N]; int a[N]; int b[N]; int n; int w[N], d[N]; int f[N]; int res; void dfs(int x) { f[x] = w[x]; vector<int> vv; for (int i = 0; (i) < (((int)(v[x]).size())); ++i) { int y = v[x][i]; if (w[y] && b[y] && !f[y]) { dfs(y); f[x] = max(f[x], f[y] + w[x]); vv.push_back(f[y]); } } sort((vv).begin(), vv.end()); reverse((vv).begin(), vv.end()); if (((int)(vv).size()) >= 2) res = max(res, w[x] + vv[0] + vv[1]); res = max(res, f[x]); } int calc(int T) { for (int i = 0; (i) < (n); ++i) b[i] = a[i] >= T; for (int i = 0; (i) < (n); ++i) d[i] = ((int)(v[i]).size()); for (int i = 0; (i) < (n); ++i) w[i] = 1; set<pair<int, int> > second; for (int i = 0; (i) < (n); ++i) second.insert(make_pair(d[i], i)); for (int _ = 0; (_) < (n); ++_) { int x = second.begin()->second; second.erase(second.begin()); if (d[x] != 1) { break; } int to = -1; for (int i = 0; (i) < (((int)(v[x]).size())); ++i) { int y = v[x][i]; if (w[y]) { to = y; break; } } if (b[x] && b[to]) { second.erase(make_pair(d[to], to)); d[to] -= 1; w[to] += w[x]; w[x] = 0; second.insert(make_pair(d[to], to)); } } for (int i = 0; (i) < (n); ++i) f[i] = 0; res = 0; for (int i = 0; (i) < (n); ++i) if (b[i] && w[i] && !f[i]) { dfs(i); } return res; } int main() { int k; scanf("%d%d", &n, &k); for (int i = 0; (i) < (n); ++i) scanf("%d", a + i); for (int i = 0; (i) < (n - 1); ++i) { int x, y; scanf("%d%d", &x, &y); --x, --y; v[x].push_back(y); v[y].push_back(x); } int l = 0, r = 1e6; while (l < r) { int m = (l + r + 1) >> 1; if (calc(m) >= k) l = m; else r = m - 1; } printf("%d\n", l); return 0; }
18
#include <bits/stdc++.h> using namespace std; inline int gi() { register int x = 0, w = 0; char ch = 0; while (ch < '0' || ch > '9') { if (ch == '-') w = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return w ? -x : x; } const int N = 1e5 + 10; int n, m, p, H, T, d[N], h[N], t[N]; long long q[N], A[N], sumD[N], sumA[N], f[110][N]; double Slope(int id, int x, int y) { return 1.0 * (f[id][x] + sumA[x] - f[id][y] - sumA[y]) / (x - y); } int main() { register int i, j; n = gi(), m = gi(), p = gi(); for (i = 2; i <= n; ++i) d[i] = gi(), sumD[i] = sumD[i - 1] + d[i]; for (i = 1; i <= m; ++i) h[i] = gi(), t[i] = gi(), A[i] = t[i] - sumD[h[i]]; for (i = 1; i <= m; ++i) sumA[i] = sumA[i - 1] + A[i]; sort(A + 1, A + m + 1); memset(f, 0x3f, sizeof(f)); for (i = 1, f[0][0] = 0; i <= p; ++i) { H = T = 1, q[1] = 0; for (j = 1; j <= m; ++j) { while (H < T && (double)A[j] >= Slope(i - 1, q[H + 1], q[H])) ++H; f[i][j] = f[i - 1][q[H]] + sumA[q[H]] + A[j] * (j - q[H]) - sumA[j]; while (H < T && Slope(i - 1, j, q[T]) <= Slope(i - 1, q[T], q[T - 1])) --T; q[++T] = j; } } printf("%lld\n", f[p][m]); return 0; }
16
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar(); return x * f; } vector<pair<int, int> > g[N]; int k[N], b[N]; int ans[N]; int vis[N]; vector<int> p; int main() { int n = read(), m = read(); for (int i = 1; i <= m; i++) { int u = read(), v = read(), x = read(); g[u].push_back(make_pair(v, 2 * x)); g[v].push_back(make_pair(u, 2 * x)); } vector<int> o; for (int i = 1; i <= n; i++) { if (!vis[i]) { p.clear(); queue<int> q; q.push(i); p.push_back(i); vis[i] = 1, k[i] = 1, b[i] = 0; bool cricle = 0; int xx = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (pair<int, int> now : g[x]) { int to = now.first; int w = now.second; if (!vis[to]) { k[to] = -k[x]; vis[to] = 1; b[to] = w - b[x]; p.push_back(to); q.push(to); } else if (!cricle) { if (k[to] == -k[x] && b[x] + b[to] != w) { puts("NO"); return 0; } else if (k[to] == k[x]) { cricle = 1; xx = (w - b[x] - b[to]) / (k[to] + k[x]); } } else { if ((w - b[x] - b[to]) != xx * (k[to] + k[x])) { puts("NO"); return 0; } } } } if (!cricle) { o.clear(); for (int x : p) { o.push_back(-k[x] * b[x]); } sort(o.begin(), o.end()); xx = o[o.size() / 2]; } for (int x : p) { ans[x] = k[x] * xx + b[x]; } } } cout << "YES\n"; for (int i = 1; i <= n; ++i) cout << 0.5 * ans[i] << " "; }
13
#include <bits/stdc++.h> using namespace std; int main() { int n, d, e; cin >> n >> d >> e; int ans = n; for (int i = 0; i * 5 * e <= n; ++i) { ans = min(ans, (n - i * 5 * e) % d); } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; map<string, int> types; struct man { bool two; int f, s, num, ans; void read(int x) { num = x; string str, cur; cin >> str; two = false; for (int i(0); i < str.size(); ++i) if (str[i] == ',') { two = true; s = types[cur]; cur.clear(); } else cur += str[i]; f = types[cur]; } }; bool Compare(const man &a, const man &b) { if (a.two == b.two) return a.f < b.f; return !a.two; } bool C2(const man &a, const man &b) { return a.num < b.num; } int k[6]; string t[] = {"S", "M", "L", "XL", "XXL", "XXXL"}; int main() { ios_base::sync_with_stdio(false); types["S"] = 0; types["M"] = 1; types["L"] = 2; types["XL"] = 3; types["XXL"] = 4; types["XXXL"] = 5; for (int i(0); i < 6; ++i) cin >> k[i]; int n; cin >> n; man a[n]; for (int i(0); i < n; ++i) a[i].read(i); sort(a, a + n, Compare); for (int i(0); i < n; ++i) if (a[i].two) { if (k[a[i].s]) { --k[a[i].s]; a[i].ans = a[i].s; } else if (k[a[i].f]) { --k[a[i].f]; a[i].ans = a[i].f; } else { cout << "NO"; return 0; }; } else if (k[a[i].f]) { a[i].ans = a[i].f; --k[a[i].f]; } else { cout << "NO"; return 0; }; cout << "YES\n"; sort(a, a + n, C2); for (int i(0); i < n; ++i) cout << t[a[i].ans] << '\n'; }
10
#include <bits/stdc++.h> using namespace std; const int N = 400, mod = int(1e9) + 7; int n, k; long double ans, cur; int x[N]; pair<int, int> line[N][N]; int sz[N]; vector<pair<int, int> > rb; int an; long long A, C, A1, C1; bool ok = 0; long double getx(pair<int, int> x, pair<int, int> y, int i) { A = x.first - x.second; C = x.second * (i - 1) - x.first * i; A1 = y.first - y.second; C1 = y.second * (i - 1) - y.first * i; return (C - C1 + 0.0) / (A1 - A); } void add_line(int i, pair<int, int> p) { int k = sz[i]; while (k > 1) { long double x1 = getx(line[i][k], line[i][k - 1], i); long double x2 = getx(line[i][k], p, i); if (x2 > x1) break; k--; } line[i][++k] = p; sz[i] = k; } void add(int i) { rb.clear(); if (sz[i] == 0) { rb.push_back(make_pair(x[i - 1], x[i])); } else { bool first = 0; for (int j = 1; j <= sz[i]; j++) { if (!first && x[i - 1] > line[i][j].first || (x[i - 1] == line[i][j].first && x[i] > line[i][j].second)) { rb.push_back(make_pair(x[i - 1], x[i])); first = 1; } rb.push_back(line[i][j]); } if (!first) rb.push_back(make_pair(x[i - 1], x[i])); } int k = 1; line[i][1] = rb[0]; int mx = rb[0].second; sz[i] = 1; for (int j = 1; j < rb.size(); j++) { if (mx < rb[j].second) { add_line(i, rb[j]); mx = rb[j].second; } } } long double get(int i) { int k = sz[i]; long double res = 0; long double x, y, px, py; px = i - 1; py = line[i][1].first; for (int j = 2; j <= k; j++) { x = getx(line[i][j], line[i][j - 1], i); y = (C1 * A - C * A1 + 0.0) / (A1 - A); res += (x - px) * (y + py) / 2.0; py = y; px = x; } x = i; y = line[i][k].second; res += (x - px) * (y + py) / 2.0; return res; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i <= k; i++) { scanf("%d", &x[i]); if (i > 0) { ans += (x[i] + x[i - 1]) / 2.0; add(i); } } printf("%.12lf\n", (double)ans); for (int it = 2; it <= n; it++) { cur = ans; ans = 0; ok = 0; for (int i = 0; i <= k; i++) { scanf("%d", &x[i]); if (i > 0) add(i); } for (int i = 1; i <= k; i++) { ans += get(i); } printf("%.12lf\n", double(ans - cur)); } return 0; }
17
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const long long INF = 1e18; const int irand(int lo, int hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } const long long lrand(long long lo, long long hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } template <typename T> T getnum() { int sign = 1; T ret = 0; char c; do { c = getchar(); } while (c == ' ' || c == '\n'); if (c == '-') sign = -1; else ret = c - '0'; while (1) { c = getchar(); if (c < '0' || c > '9') break; ret = 10 * ret + c - '0'; } return sign * ret; } inline void ini(int& x) { x = getnum<int>(); } inline void scani(int& x) { scanf("%d", &x); } const int N = 1005; int t; int n; int arr[N]; multiset<int> st; int mex() { int ret = -1; for (auto x : st) { if (x == ret) continue; if (x == ret + 1) { ret++; continue; } break; } return ret + 1; } vector<int> ans; void sett(int now, int v) { st.erase(st.find(arr[now])); ans.push_back(now); arr[now] = v; st.insert(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(10); cin >> t; while (t--) { cin >> n; st.clear(); ans.clear(); for (int i = 1; i <= n; i += 1) { cin >> arr[i]; st.insert(arr[i]); } int now = 1; while (now <= n) { if (arr[now] == now - 1) { now++; } else { int m = mex(); if (m == now - 1) { sett(now, m); now++; } else { if (m < n && arr[m + 1] != m) { sett(m + 1, m); } else { int rem = now + 1; while (arr[rem] != now - 1) rem++; sett(rem, m); } } } } cout << ans.size() << '\n'; for (auto x : ans) { cout << x << " "; } cout << '\n'; } return 0; }
11
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 5; long long mod; vector<long long> numbers[maxn]; vector<long long> children[maxn]; char forbid[maxn]; long long dp[maxn]; long long parent[maxn]; long long gcd(long long a, long long b) { while (a && b) { if (a >= b) a %= b; else b %= a; } return a + b; } long long power(long long x, long long p) { if (p == 0) return 1 % mod; if (p & 1) return x * power(x, p - 1) % mod; return power(x * x % mod, p >> 1); } long long gcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long step(long long prev, long long cur) { long long x, y; long long g = gcd(prev, mod, x, y); return (x * (cur / g) % mod + mod) % mod; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long num; cin >> num >> mod; for (long long i = 0; i < num; ++i) { long long x; cin >> x; if (x == 0) x += mod; forbid[x] = true; } for (long long i = 1; i <= mod; ++i) { if (!forbid[i]) { numbers[gcd(i, mod)].push_back(i % mod); } } for (long long i = 1; i <= mod; ++i) { if (mod % i != 0) continue; for (long long j = i + i; j <= mod; j += i) { if (mod % j != 0) continue; children[i].push_back(j); } } for (long long i = mod; i >= 1; --i) { if (gcd(i, mod) != i) continue; long long maxval = 0; parent[i] = -1; for (auto d : children[i]) { if (dp[d] > maxval) { maxval = dp[d]; parent[i] = d; } } dp[i] = maxval + (long long)numbers[i].size(); } long long current = 1; vector<long long> muls; while (current != -1) { for (auto i : numbers[current]) { muls.push_back(i); } current = parent[current]; } for (long long i = 1; i <= mod; ++i) { if (forbid[i]) { numbers[gcd(i, mod)].push_back(i % mod); } } cout << muls.size() << '\n'; for (long long i = 0; i < muls.size(); ++i) { if (i == 0) { cout << muls[i] << ' '; } else { long long tmp = step(muls[i - 1], muls[i]); cout << tmp << ' '; } } }
15
#include <bits/stdc++.h> using namespace std; int n, cnt = 1; string s; vector<int> v; int main() { cin >> n; getline(cin, s); getline(cin, s); for (int i = 0; i < s.length(); ++i) { if (s[i] == '.' || s[i] == '?' || s[i] == '!') { if (cnt > n) { puts("Impossible"); return 0; } v.push_back(cnt); i++; cnt = 0; } cnt++; } int count = 0, ans = 1; int leng = 0; for (int i = 0; i < v.size(); ++i) { leng += v[i] + 1; if (leng > n + 1) { ans++; leng = 0; i--; } } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, d, tmp; bool flag[N]; vector<int> V[N]; int f[2][N]; void dfs(int u, int p, int d, int f[]) { f[u] = d; for (int i = 0; i < V[u].size(); i++) if (V[u][i] != p) dfs(V[u][i], u, d + 1, f); } int main() { while (~scanf("%d %d %d", &n, &m, &d)) { memset(flag, 0, sizeof flag); for (int i = 0; i < m; i++) { scanf("%d", &tmp); tmp--; flag[tmp] = 1; } for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--, v--; V[u].push_back(v); V[v].push_back(u); } dfs(0, -1, 0, f[0]); int u; for (int i = 0; i < n; i++) if (flag[i]) u = i; for (int i = 0; i < n; i++) if (flag[i] && f[0][i] > f[0][u]) u = i; dfs(u, -1, 0, f[0]); for (int i = 0; i < n; i++) if (flag[i] && f[0][i] > f[0][u]) u = i; dfs(u, -1, 0, f[1]); int ans = 0; for (int i = 0; i < n; i++) if (f[0][i] <= d && f[1][i] <= d) ans++; printf("%d\n", ans); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int co[] = { 3, 3, 3, 3, 15, 27, 15, 29, 27, 27, 23, 83, 27, 43, 23, 45, 15, 39, 39, 83, 39, 57, 43, 27, 15, 71, 39, 83, 23, 83, 15, 197, 83, 281, 387, 387, 83, 99, 147, 57, 15, 153, 89, 101, 27, 449, 51, 657, 113, 29, 75, 75, 71, 329, 71, 149, 45, 99, 149, 53, 39, 105, 51, 27, 27, 833, 39, 163, 101, 43, 43, 1545, 29}; int k, a[120]; int main() { scanf("%d", &k); int z = co[k - 1]; for (int i = 0; i < (int)(k); i++) { a[i] = z & 1; z >>= 1; } for (int i = k - 1; i >= 0; i--) { if (i < k - 1) printf(" "); printf("%d", a[i]); } printf("\n"); for (int i = 0; i < (int)(k); i++) { if (i) printf(" "); printf("%d", i == 0); } return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { int NO, n; double x[200] = {0}, Xsc, Ysc, Vb, Vs, MIN1 = 1e12, MIN2 = 1e12, Line1, Line2; cin >> n >> Vb >> Vs; for (int i = 0; i < n; i++) cin >> x[i]; cin >> Xsc >> Ysc; for (int i = 1; i < n; i++) { Line1 = x[i] / Vb + sqrt((Xsc - x[i]) * (Xsc - x[i]) + Ysc * Ysc) / Vs; Line2 = sqrt((Xsc - x[i]) * (Xsc - x[i]) + Ysc * Ysc); if (Line1 < MIN1) { MIN1 = Line1; NO = i; } else if (Line1 == MIN1) { if (MIN2 > Line2) { MIN2 = Line2; NO = i; } } } cout << NO + 1; return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const double pi = 3.14159265359; const double eps = 1e-9; const int mod = 1e9 + 7; const int mod1 = 1e9 + 9; const int INF = 1e9; const long long INFLL = 2e18; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<long long, long long> > a(n); long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; sum += a[i].first; } sort(a.begin(), a.end()); vector<int> ans; for (int i = 0; i < n; i++) { if (i < n - 1) { sum -= a[i].first; sum -= a.back().first; if (sum == a.back().first) ans.push_back(a[i].second + 1); sum += a[i].first + a.back().first; } else { sum -= a[i].first + a[i - 1].first; if (sum == a[i - 1].first) ans.push_back(a[i].second + 1); sum += a[i].first + a[i - 1].first; } } cout << ans.size() << endl; for (auto i : ans) cout << i << ' '; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long get(long long n) { for (long long i = 2; i * i <= n; ++i) if (n % i == 0) return i; return n; } int main() { long long n; cin >> n; long long cnt = 0; if (n % 2 != 0) { n -= get(n); ++cnt; } cout << cnt + n / 2; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, i, j, n, k, sum; while (cin >> n >> k) { sum = 0, d = 0; if (k >= 240) { cout << "0" << endl; } else { for (i = 1; i <= n; i++) { sum = sum + (i * 5); c = sum + k; if (c > 240) break; d++; } cout << d << endl; } } }
0
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, ai; vector<long long> data; cin >> n; for (long long i = 0; i < n; i++) { cin >> ai; data.push_back(ai); } vector<vector<long long> > v; for (long long i = 0; i < n; i++) { long long z = n - data[i]; long long diff = data[(i + 1) % n] - z; vector<long long> help; help.push_back(diff); help.push_back(i); v.push_back(help); } sort(v.begin(), v.end()); vector<long long> answer(n); for (long long i = 0; i < n; i++) { long long b = v[i][1]; answer[b] = i; } for (long long i = 0; i < n; i++) { cout << answer[i] << " "; } return 0; }
13
#include <bits/stdc++.h> using namespace std; double a[1002], b[1002]; int main() { int n; double m, cm; cin >> n >> m; cm = m; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] <= 1.0 + 1e-9) { cout << "-1"; return 0; } } for (int i = 1; i <= n; i++) { cin >> b[i]; if (b[i] <= 1.0 + 1e-9) { cout << "-1"; return 0; } } m = m / (b[1] - 1.0) + m; for (int i = n; i > 1; i--) { m = m / (b[i] - 1.0) + m; m = m / (a[i] - 1.0) + m; } m = m / (a[1] - 1.0) + m; printf("%.10lf", m - cm); return 0; }
7
#include <bits/stdc++.h> #include<map> #include<unordered_map> #include<queue> #include<stack> #include<vector> #include <cmath> #include<math.h> #define ll long long int #define pb push_back #define mod 1000000007 #define ip(ar,m,n) for(ll i=0;i<m;i++){for(ll j=0;j<n;j++){cin>>ar[i][j];}} #define mk(ar,m,n) for(ll i=0;i<m;i++){ar[i]=new ll[n];} using namespace std; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// double dis(ll x,ll y){ return sqrt(x*x+y*y); } void solve(ll r,ll z,ll k){ double d=dis(z,z+k); if(d<=r){ cout<<"Ashish"<<endl; } else{ cout<<"Utkarsh"<<endl; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t; cin>>t; while(t--){ double rt_2=sqrt(2); double r,k,z; cin>>r>>k; z=r/rt_2; int tmp=(int)z,tmp2=(int)k; z=tmp-tmp%tmp2; solve(r,z,k); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int Inf = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; auto ask = [&](int x, int y) { cout << "? " << x + 1 << ' ' << y + 1 << endl; int ret; cin >> ret; return ret; }; int bc = -1, val = Inf; for (int j = 0; j < m; ++j) { int cur = ask(0, j); if (cur < val) { val = cur; bc = j; } } int ans = Inf; for (int i = 0; i < n; ++i) { ans = min(ans, ask(i, bc)); } cout << "! " << ans << endl; }
20
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } int N; string T; int Q; pair<string, vector<int>> solt(string T) { int N = T.length(); vector<int> v{-1}; string ret; int suc = 0; for (int i = (0), i_end_ = (N); i < i_end_; i++) { if (T[i] == '0') { if (suc % 2) ret += '1'; suc = 0; ret += '0'; v.push_back(i); } else suc++; } if (suc % 2) ret += '1'; v.push_back(N); return make_pair(ret, v); } struct rand_int_ { using lint = long long; mt19937 mt; rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } lint operator()(lint l, lint r) { uniform_int_distribution<lint> d(l, r - 1); return d(mt); } } rnd; template <typename V> struct rolling_hash { V B; vector<V> hash; vector<V> power; rolling_hash() {} rolling_hash(const string &s, V b) : B(b) { int N = s.length(); hash.resize(N + 1), power.resize(N + 1, 1); for (int i = 0; i < N; i++) { power[i + 1] = power[i] * B; hash[i + 1] = hash[i] * B + s[i]; } } V get_hash(int l, int r) { return hash[r] - hash[l] * power[r - l]; } }; using lint = long long; using plint = pair<lint, lint>; struct DoubleHash : public plint { static plint MODs; DoubleHash(plint x) : plint(x) {} DoubleHash(lint x, lint y) : plint(x, y) {} DoubleHash(lint x) : DoubleHash(x, x) {} DoubleHash() : DoubleHash(0) {} static inline DoubleHash mod_subtract(plint x) { if (x.first >= MODs.first) x.first -= MODs.first; if (x.second >= MODs.second) x.second -= MODs.second; return x; } DoubleHash operator+(const DoubleHash &x) const { return mod_subtract(plint(this->first + x.first, this->second + x.second)); } DoubleHash operator+(lint x) const { return mod_subtract(plint(this->first + x, this->second + x)); } DoubleHash operator-(const DoubleHash &x) const { return mod_subtract(plint(this->first - x.first + MODs.first, this->second - x.second + MODs.second)); } DoubleHash operator*(const DoubleHash &x) const { return make_pair(this->first * x.first % MODs.first, this->second * x.second % MODs.second); } DoubleHash operator*(lint x) const { return make_pair(this->first * x % MODs.first, this->second * x % MODs.second); } }; plint DoubleHash::MODs = plint(1000000007, 998244353); rolling_hash<DoubleHash> rh; vector<int> nxtz, prvz; vector<int> zaatsu; pair<DoubleHash, pint> solve(int l, int r) { int lz = nxtz[l]; int rz = prvz[r - 1]; if (lz > rz) { return make_pair(DoubleHash(-1, -1), pint(r - l - 10000000, -1)); } pint rp = make_pair((lz - l) % 2, (r - rz) % 2); return make_pair(rh.get_hash(zaatsu[lz], zaatsu[rz]), rp); } int main() { cin >> N >> T; N = T.length(); nxtz.resize(N); prvz.resize(N); string Ttrunc; vector<int> zeropos; tie(Ttrunc, zeropos) = solt(T); { int t = 0; for (int i = (0), i_end_ = (N); i < i_end_; i++) { while (zeropos[t] < i) t++; nxtz[i] = zeropos[t]; } t = zeropos.size() - 1; for (int i = (N)-1, i_begin_ = (0); i >= i_begin_; i--) { while (zeropos[t] > i) t--; prvz[i] = zeropos[t]; } } zaatsu.resize(N); { int j = 0; for (int i = (0), i_end_ = (N); i < i_end_; i++) if (T[i] == '0') { while (Ttrunc[j] != '0') j++; zaatsu[i] = j; j++; } } DoubleHash b(rnd(1, 100000), rnd(1, 1000000)); rh = rolling_hash<DoubleHash>(Ttrunc, b); vector<int> ret; cin >> Q; while (Q--) { int l1, l2, len; cin >> l1 >> l2 >> len; l1--, l2--; auto h1 = solve(l1, l1 + len); auto h2 = solve(l2, l2 + len); ret.push_back(h1 == h2); } for (auto v : ret) { if (v) puts("Yes"); else puts("No"); } }
17
#include <bits/stdc++.h> struct Vector { long long int x, y; Vector() : x(0), y(0) {} Vector(const Vector& a, const Vector& b) : x(b.x - a.x), y(b.y - a.y) {} double len() { return hypot(x, y); } }; long long int cross(Vector& a, Vector& b); long long int cross(Vector& a, Vector& b) { return a.x * b.y - a.y * b.x; } int main() { Vector a, b, c; std::cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y; Vector ba(b, a), bc(b, c); if ((ba.x * ba.x + ba.y * ba.y != bc.x * bc.x + bc.y * bc.y) || (cross(ba, bc) == 0)) std::cout << "No"; else std::cout << "Yes"; return 0; }
6
#include <bits/stdc++.h> using namespace std; set<int> candid; vector<int> candid_arr; vector<int> mapper; vector<bool> flag; int k, n; void dump_set() { for (set<int>::iterator ptr = candid.begin(); ptr != candid.end(); ptr++) { cout << *ptr << ' '; } cout << endl; } void dump_mapper() { for (int i = 0; i < mapper.size(); i++) { cout << mapper[i] << ' '; } cout << endl; } int get_ans() { int cnt = 0; for (set<int>::iterator ptr = candid.begin(); ptr != candid.end(); ptr++) { int bias = *ptr - mapper[0]; bool failed = false; for (int i = 1; i < n; i++) { if (candid.find(mapper[i] + bias) == candid.end()) { failed = true; break; } } if (failed == false) { cnt++; } } return cnt; } int main() { int val = 0; cin >> k >> n; candid_arr.reserve(k); mapper.reserve(n); flag.resize(k, false); for (int i = 0; i < k; i++) { int cur; cin >> cur; val += cur; candid.insert(val); } for (int i = 0; i < n; i++) { int tmp; cin >> tmp; mapper.push_back(tmp); } cout << get_ans() << endl; }
9
#include <bits/stdc++.h> using namespace std; const int NMAX = 1005; int N, M, K; bool dragnea[NMAX]; vector<int> graph[NMAX]; bitset<NMAX> seen; vector<int> nodes; void dfs(int node) { nodes.push_back(node); seen[node] = 1; for (auto& x : graph[node]) if (seen[x] == 0) dfs(x); } vector<int> bad, good; int main() { cin >> N >> M >> K; for (int i = 1; i <= K; ++i) { int u; cin >> u; dragnea[u] = true; } for (int i = 1; i <= M; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } long long answer = 0; for (int i = 1; i <= N; ++i) if (seen[i] == 0) { nodes.clear(); dfs(i); int cnt = 0; bool isdragnea = false; for (auto& j : nodes) { cnt += graph[j].size(); if (dragnea[j]) isdragnea = true; } cnt /= 2; int all = 1LL * nodes.size() * (nodes.size() - 1) / 2; answer += all - cnt; if (isdragnea) bad.push_back(nodes.size()); else good.push_back(nodes.size()); } if (!good.empty() && !bad.empty()) { int mx = 0; for (auto& i : bad) mx = max(mx, i); int cnt = good[0]; for (int i = 1; i < good.size(); ++i) { answer += 1LL * cnt * good[i]; cnt += good[i]; } answer += 1LL * cnt * mx; } cout << answer << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } double f(double x1, double y1, double x2, double y2, double pointX, double pointY) { double diffX = x2 - x1; float diffY = y2 - y1; if ((diffX == 0) && (diffY == 0)) { diffX = pointX - x1; diffY = pointY - y1; return sqrt(diffX * diffX + diffY * diffY); } float t = ((pointX - x1) * diffX + (pointY - y1) * diffY) / (diffX * diffX + diffY * diffY); if (t < 0) { diffX = pointX - x1; diffY = pointY - y1; } else if (t > 1) { diffX = pointX - x2; diffY = pointY - y2; } else { diffX = pointX - (x1 + t * diffX); diffY = pointY - (y1 + t * diffY); } return sqrt(diffX * diffX + diffY * diffY); } long long int a, b, c; int main() { long long int x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; if ((x1 == x2 and x2 == x3) or (y1 == y2 and y2 == y3)) cout << 1 << endl; else { if (x1 == x2 or x3 == x1 or x2 == x3) { if (x1 == x2) { a = y1; b = y2; c = y3; } if (x3 == x2) { a = y3; b = y2; c = y1; } if (x1 == x3) { a = y1; b = y3; c = y2; } if (c == a or c == b) cout << 2 << endl; else if ((c > a and c < b) or (c < a and c > b)) cout << 3 << endl; else if ((c > a and c > b) or (c < a and c < b)) cout << 2 << endl; } else if (y1 == y2 or y3 == y1 or y2 == y3) { swap(x1, y1); swap(x2, y2); swap(x3, y3); if (x1 == x2) { a = y1; b = y2; c = y3; } if (x3 == x2) { a = y3; b = y2; c = y1; } if (x1 == x3) { a = y1; b = y3; c = y2; } if (c == a or c == b) cout << 2 << endl; else if ((c > a and c < b) or (c < a and c > b)) cout << 3 << endl; else if ((c > a and c > b) or (c < a and c < b)) cout << 2 << endl; swap(x1, y1); swap(x2, y2); swap(x3, y3); } else { cout << 3 << endl; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n; int k = 0; int h = 0; if (n > 77774444) { cout << 4444477777; return 0; } for (int i = n; i <= 1000000000; i++) { int j = i; h = 0; k = 0; while (j > 0) { if (j % 10 != 4 && j % 10 != 7) { h = -1; k = 0; break; } if (j % 10 == 4) { k++; } else { h++; } j = j / 10; } if (h == k) { cout << i << endl; break; } } return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = int(v); } static int inv_mod(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } explicit operator int64_t() const { return val; } _m_int &operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int &operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return unsigned(x % m); unsigned x_high = unsigned(x >> 32), x_low = unsigned(x); unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } _m_int &operator*=(const _m_int &other) { val = fast_mod(uint64_t(val) * other.val); return *this; } _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_int &other) const { return val == other.val; } bool operator!=(const _m_int &other) const { return val != other.val; } _m_int inv() const { return inv_mod(val); } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; p >>= 1; if (p > 0) a *= a; } return result; } friend ostream &operator<<(ostream &os, const _m_int &m) { return os << m.val; } }; extern const int MOD = 998244353; using mod_int = _m_int<MOD>; struct mod_matrix_fixed_size { static const int SIZE = 2; mod_int values[SIZE][SIZE]; mod_matrix_fixed_size() { for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) values[i][j] = 0; } template <typename T> mod_matrix_fixed_size(const vector<vector<T>> &v) { init(v); } template <typename T> void init(const vector<vector<T>> &v) { assert(v.size() == SIZE); for (int i = 0; i < SIZE; i++) { assert(v[i].size() == SIZE); for (int j = 0; j < SIZE; j++) values[i][j] = v[i][j]; } } mod_int *operator[](int index) { return values[index]; } const mod_int *operator[](int index) const { return values[index]; } void make_identity() { for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) values[i][j] = i == j ? 1 : 0; } mod_matrix_fixed_size operator*(const mod_matrix_fixed_size &other) const { mod_matrix_fixed_size product; for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) if (values[i][j] != 0) for (int k = 0; k < SIZE; k++) product[i][k] += values[i][j] * other[j][k]; return product; } mod_matrix_fixed_size &operator*=(const mod_matrix_fixed_size &other) { return *this = *this * other; } mod_matrix_fixed_size power(int64_t p) const { assert(p >= 0); mod_matrix_fixed_size m = *this; mod_matrix_fixed_size result; result.make_identity(); while (p > 0) { if (p & 1) result *= m; p >>= 1; if (p > 0) m *= m; } return result; } void print() const { for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) cout << values[i][j] << (j < SIZE - 1 ? ' ' : '\n'); } }; struct segment { mod_matrix_fixed_size transition; segment(int digit = -1) { if (digit < 0) transition.make_identity(); else transition.init( vector<vector<int>>{{0, 9 - digit}, {digit == 1, digit + 1}}); } void join(const segment &other) { transition *= other.transition; } void join(const segment &a, const segment &b) { *this = a; join(b); } }; int right_half[32]; struct basic_seg_tree { static const bool POWER_OF_TWO_MODE = true; int tree_n = 0; vector<segment> tree; basic_seg_tree(int n = -1) { if (n >= 0) init(n); } void init(int n) { if (POWER_OF_TWO_MODE) { tree_n = 1; while (tree_n < n) tree_n *= 2; } else { tree_n = n; } tree.assign(2 * tree_n, segment()); } void build(const vector<segment> &initial) { int n = int(initial.size()); init(n); assert(n <= tree_n); for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i]; for (int position = tree_n - 1; position > 0; position--) tree[position].join(tree[2 * position], tree[2 * position + 1]); } segment query(int a, int b) const { assert(0 <= a && a <= b && b <= tree_n); segment answer; int r_size = 0; for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2) { if (a & 1) answer.join(tree[a++]); if (b & 1) right_half[r_size++] = --b; } for (int i = r_size - 1; i >= 0; i--) answer.join(tree[right_half[i]]); return answer; } segment query_single(int index) const { assert(0 <= index && index < tree_n); return tree[tree_n + index]; } void join_up(int position) { while (position > 1) { position /= 2; tree[position].join(tree[2 * position], tree[2 * position + 1]); } } void update(int index, const segment &seg) { assert(0 <= index && index < tree_n); int position = tree_n + index; tree[position] = seg; join_up(position); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; string C; cin >> N >> M >> C; basic_seg_tree tree(N); vector<segment> initial(N); for (int i = 0; i < N; i++) initial[i] = segment(C[i] - '0'); tree.build(initial); mod_matrix_fixed_size dp_start; dp_start[0][1] = 1; for (int q = 0; q < M; q++) { int x, d; cin >> x >> d; x--; tree.update(x, segment(d)); cout << (dp_start * tree.tree[1].transition)[0][1] << '\n'; } }
18
#include <bits/stdc++.h> using namespace std; int n, m, groups[100 + 10]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &groups[i]); } int bus = m, index, count = 1; while (index < n) { if (bus - groups[index] >= 0) { bus -= groups[index]; index++; } else { bus = m; count++; } } cout << count; return 0; }
2
#include <bits/stdc++.h> using namespace std; using LL = long long; void solve() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } bool sorted = true; for (int i = 1; i < n; ++i) { if (arr[i] <= arr[i - 1]) { sorted = false; break; } } if (sorted) { cout << 0 << endl; return; } cout << n + 1 << endl; cout << 1 << ' ' << n << ' ' << 103000 << endl; int cnt = 0; for (int i = 0; i < n; ++i) { cout << 2 << ' ' << i + 1 << ' ' << arr[i] + 103000 - i - 1 << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(); double startTime = clock(); solve(); cerr << "\nThe time is: " << (clock() - startTime) / CLOCKS_PER_SEC << endl; }
6
#include <bits/stdc++.h> using namespace std; vector<int>::iterator it; vector<long long int>::iterator lit; int main() { ios::sync_with_stdio(false); long long int tcase; cin >> tcase; while (tcase--) { long long int mina = LLONG_MAX; long long int n; cin >> n; vector<long long int> a(n, 0), b(n, 0), s(n, 0); long long int sum = 0; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 0; i < n; i++) { cin >> b[i]; sum += b[i]; } multimap<long long int, long long int> mp; for (long long int i = 0; i < n; i++) mp.insert(pair<long long int, long long int>(a[i], b[i])); long long int x = 0; long long int j = (n - 1); for (auto i = mp.rbegin(); i != mp.rend(); ++i) { x += i->second; s[j] = x; j--; } long long int count = 0; long long int ans; for (auto i = mp.begin(); i != mp.end(); ++i) { if (count + 1 < n) { if ((i->first) >= s[count + 1]) ans = i->first; else ans = s[count + 1]; if (ans < mina) mina = ans; } else if (i->first < mina) { mina = i->first; } if (sum < mina) mina = sum; count++; } cout << mina << "\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-7; const int MAXN = 20; int n, k; double p[MAXN]; double prob[2][1 << MAXN]; double pr[1 << MAXN]; double sum[1 << MAXN]; int bcnt[1 << MAXN]; int id[1 << MAXN]; double res[MAXN]; const int STEPS = 2000; int get_lowest(int mask) { int l = mask & (mask - 1); return id[l ^ mask]; } int invert(int mask) { return ((1 << n) - 1) ^ mask; } int bitcnt(int n) { int res = 0; while (n) { res += n & 1; n >>= 1; } return res; } int testbit(int n, int k) { return (n >> k) & 1; } void brute() { int t = 0; vector<int> nice; for (int i = 0; i < (1 << n); i++) if (bcnt[i] <= k) nice.push_back(i); double err = 0; prob[0][0] = 1; int iter = 0; while (true) { iter++; for (int i : nice) prob[1 - t][i] = 0; for (int i : nice) { if (bcnt[i] == k) prob[1 - t][i] += prob[t][i]; else { prob[1 - t][i] += prob[t][i] * sum[i]; int j = invert(i); while (j) { int l = get_lowest(j); j ^= (1 << l); prob[1 - t][i ^ (1 << l)] += prob[t][i] * p[l]; } } } err = 0; for (int i : nice) if (bcnt[i] != k) err += prob[1 - t][i]; fprintf(stderr, "%.10lf\n", err); t = 1 - t; if (fabs(err) < 1e-7) break; } for (int i = 0; i < (n); i++) res[i] = 0; for (int i = 0; i < (1 << n); i++) if (bcnt[i] == k) { for (int j = 0; j < (n); j++) if (testbit(i, j)) res[j] += prob[t][i]; } else err += prob[t][i]; for (int i = 0; i < (n); i++) printf("%.10lf ", res[i]); puts(""); fprintf(stderr, "%d\n", iter); fprintf(stderr, "%.10lf\n", err); } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < (n); i++) id[1 << i] = i; for (int i = 0; i < (n); i++) scanf("%lf", &p[i]); sum[0] = 0; for (int i = 1; i < (1 << n); i++) { int l = get_lowest(i); sum[i] = sum[i ^ (1 << l)] + p[l]; bcnt[i] = bcnt[i ^ (1 << l)] + 1; } pr[0] = 1; for (int i = 0; i < (1 << n); i++) { if (bcnt[i] == k || fabs(sum[invert(i)]) < 1e-5) { for (int j = 0; j < (n); j++) if (testbit(i, j)) res[j] += pr[i]; } else { int j = invert(i); double den = sum[j]; while (j) { int l = get_lowest(j); j ^= (1 << l); pr[i ^ (1 << l)] += pr[i] * p[l] / den; } } } for (int i = 0; i < (n); i++) printf("%.10lf ", res[i]); puts(""); return 0; }
16
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e12; const int N = (int)1e6 + 10; int n, m, k; long long a[N]; long long b[N]; long long c[N]; long long ANS = INF * N; vector<pair<long long, int> > d[N]; int main() { scanf("%d%d%d", &n, &m, &k); while (m--) { int t, v, u; long long w; scanf("%d%d%d%lld", &t, &v, &u, &w); if (v == 0) v = -u; d[t].push_back(make_pair(w, v)); } for (int i = 1; i <= n; i++) a[i] = INF; for (int i = 0; i < N; i++) b[i] = c[i] = INF; long long val = n * INF; for (int i = 0; i < N; i++) { for (pair<long long, int> t : d[i]) { if (t.second < 0) continue; int id = t.second; long long w = t.first; if (w >= a[id]) continue; val -= a[id]; a[id] = w; val += a[id]; } b[i] = val; } for (int i = 1; i <= n; i++) a[i] = INF; val = n * INF; for (int i = N - 1; i >= 0; i--) { for (pair<long long, int> t : d[i]) { if (t.second > 0) continue; int id = -t.second; long long w = t.first; if (w >= a[id]) continue; val -= a[id]; a[id] = w; val += a[id]; } c[i] = val; } for (int i = 0; i + k + 1 < N; i++) ANS = min(ANS, b[i] + c[i + k + 1]); if (ANS >= INF) printf("-1\n"); else printf("%lld\n", ANS); return 0; }
10
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a < b) gcd(b, a); int r; while ((r = a % b)) { a = b; b = r; } return b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } const int maxn = 1 << 24; int n2, dat[32][600005][2]; int n, k; int kazu; void init(int n_) { for (int z = 0; z < kazu; z++) { n2 = 1; while (n2 < n_) n2 *= 2; for (int i = 0; i < 2 * n2 - 1; i++) { dat[z][i][0] = INT_MIN; dat[z][i][1] = INT_MAX; } } } void update(int kkkkk, int* a) { for (int z = 0; z < kazu; z++) { int kk = kkkkk + n2 - 1; int num = 0; for (int tmp = 0; tmp < k; tmp++) { if ((z >> tmp) & 1) num += a[tmp]; else num -= a[tmp]; } dat[z][kk][0] = num; dat[z][kk][1] = num; while (kk > 0) { kk = (kk - 1) / 2; dat[z][kk][0] = max(dat[z][kk * 2 + 1][0], dat[z][kk * 2 + 2][0]); dat[z][kk][1] = min(dat[z][kk * 2 + 1][1], dat[z][kk * 2 + 2][1]); } } } int query2(int z, int a, int b, int kkkkk, int l, int r) { if (r <= a || b <= l) return INT_MIN; if (a <= l && r <= b) return dat[z][kkkkk][0]; return max(query2(z, a, b, kkkkk * 2 + 1, l, (l + r) / 2), query2(z, a, b, kkkkk * 2 + 2, (l + r) / 2, r)); } int query3(int z, int a, int b, int kkkkk, int l, int r) { if (r <= a || b <= l) return INT_MAX; if (a <= l && r <= b) return dat[z][kkkkk][1]; return min(query3(z, a, b, kkkkk * 2 + 1, l, (l + r) / 2), query3(z, a, b, kkkkk * 2 + 2, (l + r) / 2, r)); } int query(int a, int b) { int ans = INT_MIN; for (int z = 0; z < kazu; z++) { ans = max(ans, query2(z, a, b, 0, 0, n2) - query3(z, a, b, 0, 0, n2)); } return ans; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> k; kazu = 1 << k; kazu /= 2; init(n); int a[5]; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { cin >> a[j]; } update(i, a); } int q; cin >> q; int b[5]; int inst; int l, r; for (int i = 0; i < q; i++) { cin >> inst; if (inst == 1) { cin >> l; for (int j = 0; j < k; j++) { cin >> b[j]; } update(l - 1, b); } else { cin >> l >> r; cout << query(l - 1, r) << "\n"; } } }
15
#include <bits/stdc++.h> using namespace std; void run() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vector<int> s; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int x = (v[i] ^ v[j]); s.push_back(x); } } sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); sort(v.begin(), v.end()); for (int it = 0; it < (int)s.size(); it++) { int k = s[it]; if (k == 0) continue; vector<int> t; for (int i = 0; i < n; i++) { int x = (v[i] ^ k); t.push_back(x); } sort(t.begin(), t.end()); if (t == v) { cout << k << endl; return; } } cout << "-1" << endl; } int main() { int t; cin >> t; while (t--) { run(); } }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; priority_queue<int> q; for (int i = 0; i < (int)(n); i++) q.push(i + 1); vector<int> A, B; while (q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); A.push_back(a); B.push_back(b); q.push((a + b + 1) / 2); } cout << q.top() << endl; for (int i = 0; i < (int)(n - 1); i++) { cout << A[i] << " " << B[i] << endl; } } return 0; }
2
#include <bits/stdc++.h> const int mod = 1000000007; const int gmod = 3; const int inf = 1039074182; const double eps = 1e-9; const double pi = 3.141592653589793238462643383279; const long long llinf = 2LL * inf * inf; template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) { if (b < x) x = b; } template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) { if (b > x) x = b; } inline void chadd(int &x, int b) { x += b - mod; x += (x >> 31 & mod); } template <typename T1, typename T2> inline void chadd(T1 &x, T2 b) { x += b; if (x >= mod) x -= mod; } template <typename T1, typename T2> inline void chmul(T1 &x, T2 b) { x = 1LL * x * b % mod; } template <typename T1, typename T2> inline void chmod(T1 &x, T2 b) { x %= b, x += b; if (x >= b) x -= b; } template <typename T> inline T mabs(T x) { return (x < 0 ? -x : x); } using namespace std; using namespace std; template <typename T> ostream &operator<<(ostream &cout, const vector<T> &vec) { cout << "{"; for (int i = 0; i < (int)vec.size(); i++) { cout << vec[i]; if (i != (int)vec.size() - 1) cout << ','; } cout << "}"; return cout; } template <typename T1, typename T2> ostream &operator<<(ostream &cout, pair<T1, T2> p) { cout << "(" << p.first << ',' << p.second << ")"; return cout; } template <typename T, typename T2> ostream &operator<<(ostream &cout, set<T, T2> s) { vector<T> t; for (auto x : s) t.push_back(x); cout << t; return cout; } template <typename T, typename T2> ostream &operator<<(ostream &cout, multiset<T, T2> s) { vector<T> t; for (auto x : s) t.push_back(x); cout << t; return cout; } template <typename T> ostream &operator<<(ostream &cout, queue<T> q) { vector<T> t; while (q.size()) { t.push_back(q.front()); q.pop(); } cout << t; return cout; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &cout, map<T1, T2, T3> m) { for (auto &x : m) { cout << "Key: " << x.first << ' ' << "Value: " << x.second << endl; } return cout; } template <typename T1, typename T2> void operator+=(pair<T1, T2> &x, const pair<T1, T2> y) { x.first += y.first; x.second += y.second; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &x, const pair<T1, T2> &y) { return make_pair(x.first + y.first, x.second + y.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &x, const pair<T1, T2> &y) { return make_pair(x.first - y.first, x.second - y.second); } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> x) { return make_pair(-x.first, -x.second); } template <typename T> vector<vector<T>> operator~(vector<vector<T>> vec) { vector<vector<T>> v; int n = vec.size(), m = vec[0].size(); v.resize(m); for (int i = 0; i < m; i++) { v[i].resize(n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { v[i][j] = vec[j][i]; } } return v; } void print0x(int x) { std::vector<int> vec; while (x) { vec.push_back(x & 1); x >>= 1; } std::reverse(vec.begin(), vec.end()); for (int i = 0; i < (int)vec.size(); i++) { std::cout << vec[i]; } std::cout << ' '; } template <typename T> void print0x(T x, int len) { std::vector<int> vec; while (x) { std::cout << (x & 1); x >>= 1; len--; } while (len--) cout << 0; } vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } namespace HASHTABLE { struct HashInt { int operator()(int x) { x ^= (x >> 13); return x; } }; } // namespace HASHTABLE template <typename Key, typename Value, typename Hash, int N, int mod> struct HashTable { int head[mod + 5]; int nxt[N + 5]; Value val[N + 5]; Key key[N + 5]; Hash hash; int cnt; HashTable() : cnt(1) {} inline bool exist(const Key &k) { int t = hash(k) % mod; for (int i = head[t]; i; i = nxt[i]) { if (key[i] == k) return true; } return false; } inline Value &at(const Key &k) { int t = hash(k) % mod; for (int i = head[t]; i; i = nxt[i]) { if (key[i] == k) return val[i]; } key[cnt] = k; nxt[cnt] = head[t]; head[t] = cnt; assert(cnt <= N); return val[cnt++]; } inline Value &operator[](const Key &k) { return at(k); } inline void clear() { cnt = 1; memset(head, 0, sizeof(head)); } }; namespace combinatorics { int *fac; int *ifac; int __Tmod; inline int add(int a, int b) { return (a + b) % __Tmod; } inline int sub(int a, int b) { return (a - b + __Tmod) % __Tmod; } inline int mult(int a, int b) { return (1LL * a * b) % __Tmod; } inline int fastpow(int basic, int x) { chmod(x, __Tmod - 1); if (x == 0) return 1; int res = 1; while (x) { if (x & 1) res = mult(res, basic); basic = mult(basic, basic); x >>= 1; } return res; } inline int inv(int x) { return fastpow(x, __Tmod - 2); } inline int div(int a, int b) { return mult(a, inv(b)); } void init(int n, int tmod) { __Tmod = tmod; fac = new int[n + 5]; ifac = new int[n + 5]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = mult(fac[i - 1], i); } ifac[n] = inv(fac[n]); for (int i = n - 1; i >= 0; i--) { ifac[i] = mult(ifac[i + 1], i + 1); } } inline int C(int n, int m) { if (n < m || n < 0 || m < 0) return 0; return mult(mult(fac[n], ifac[m]), ifac[n - m]); } inline int Cat(int x) { return mult(C(x * 2, x), inv(x + 1)); } } // namespace combinatorics using combinatorics::fac; using combinatorics::ifac; const int prime = 1000000; using namespace std; int n, k; int go[12][12]; struct State { int a[12]; }; int tmp[12]; void lsh(State &x) { memset(tmp, -1, sizeof(tmp)); int cnt = 1; for (int i = 0; i < n; i++) { if (x.a[i] == 0) continue; if (tmp[x.a[i]] == -1) tmp[x.a[i]] = cnt++; x.a[i] = tmp[x.a[i]]; } } ostream &operator<<(ostream &cout, const State &s) { vector<int> v; for (int i = 0; i < n; i++) v.push_back(s.a[i]); cout << v; return cout; } int to[12]; struct HashState { inline int operator()(const State &s) const { int res = 0; memset(to, -1, sizeof(to)); int cnt = 0; for (int i = 0; i < n; i++) { if (to[s.a[i]] == -1) to[s.a[i]] = ++cnt; res = (1LL * res * prime + to[s.a[i]]) % mod; } return res; } }; struct HashPiiState { inline int operator()(const pair<pair<int, int>, State> &s) const { int x = s.first.first, y = s.first.second; if (x > y) swap(x, y); int res = x * n + y; memset(to, -1, sizeof(to)); int cnt = 0; for (int i = 0; i < n; i++) { if (to[s.second.a[i]] == -1) to[s.second.a[i]] = ++cnt; res = (1LL * res * prime + to[s.second.a[i]]) % mod; } return res; } }; HashState hs; bool operator==(State a, State b) { lsh(a); lsh(b); for (int i = 0; i < n; i++) { if (a.a[i] != b.a[i]) return false; } return true; } HashTable<State, bool, HashState, 1000000, 2000003> ht; HashTable<pair<pair<int, int>, State>, bool, HashPiiState, 2000000, 2000003> ht2; HashTable<State, bool, HashState, 1000000, 2000003> hfuck; void dfs(State now) { if (ht.exist(now)) return; ht[now] = 1; int cols[12]; for (int i = 0; i < n; i++) { cols[i] = now.a[i]; } sort(cols, cols + n); int k = unique(cols, cols + n) - cols; for (int i = 0; i < k; i++) { for (int j = i + 1; j < k; j++) { State nxt = now; for (int t = 0; t < n; t++) { if (nxt.a[t] == cols[j]) nxt.a[t] = cols[i]; } dfs(nxt); } } } vector<State> vs; void bfs(State now, int x, int y, int p) { if (p == n / 2 + 1) { if (hfuck.exist(now)) return; hfuck[now] = 1; vs.emplace_back(now); return; } if (ht2.exist(make_pair(make_pair(x, y), now))) return; ht2[make_pair(make_pair(x, y), now)] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (now.a[i] || now.a[j] || !go[x][i] || !go[y][j]) continue; now.a[i] = now.a[j] = p; bfs(now, i, j, p + 1); now.a[i] = now.a[j] = 0; } } } void read() { int m; cin >> n >> m >> k; while (m--) { int x, y; scanf("%d%d", &x, &y); x--; y--; go[x][y] = go[y][x] = 1; } } int main() { combinatorics::init(12, mod); read(); State st; memset(st.a, 0, sizeof(st.a)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (!go[i][j]) continue; st.a[i] = st.a[j] = 1; bfs(st, i, j, 2); st.a[i] = st.a[j] = 0; } } for (auto x : vs) { dfs(x); } long long res = 0; for (int i = 1; i < ht.cnt; i++) { st = ht.key[i]; map<int, int> m; for (int j = 0; j < n; j++) { m[st.a[j]]++; } if ((int)m.size() > k) continue; res += 1LL * fac[k] * ifac[k - m.size()] % mod; } cout << res << endl; return 0; }
22
#include <bits/stdc++.h> using namespace std; long long ncrp(long long n, long long r, long long p) { long long C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (long long i = 1; i <= n; i++) for (long long j = min(i, r); j >= 1; j--) C[j] = (C[j] + C[j - 1]) % p; return C[r]; } long long mod_exp(long long x, long long y, long long mm) { x %= mm; if (y == 0) return (1); else if (y % 2 == 0) return (mod_exp((x * x) % mm, y / 2, mm)); else return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, r, s; cin >> n; r = (sqrt(1 + 8 * n) - 1) / 2; s = n - (r * (r + 1) / 2); cout << r << endl; for (long long i = r; i >= 1; i--) { if (s != 0) { cout << i + 1 << " "; s--; } else cout << i << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, o = 0; for (int i = 0; i < 2; ++i) { scanf("%d%d%d", &a, &b, &c); o += (a + b) * c + a * b; } printf("%d\n", o); }
8
#include <bits/stdc++.h> double f(int d, int R); double f(int d, int R) { if (d == 0) { return 2.0f * R; } if (d == 1) { return 2.0f * R + sqrt(2.0f) * R; } return 2.0f * sqrt(2.0f) * R + 2.0f * R + 2.0f * R * (d - 2); } int q(int d, int m); int q(int d, int m) { return d == 0 ? m : 2 * (m - d); } int main() { int m = 0, R = 0; scanf("%d %d", &m, &R); double res = 0.0f; for (int i = 0; i < m; ++i) { res += f(i, R) * q(i, m); } res /= m; res /= m; printf("%.10f", res); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 15; vector<pair<vector<int>, int>> v; vector<int> adj[N], adj2[N]; set<pair<int, int>> s; int final_pos = 1, n, m, x, y, pos = 1, root, ind[N], ans[N], is; void dfs(int x, int p) { ans[x] = final_pos++; for (int i = 0; i < adj2[x].size(); ++i) if (adj2[x][i] != p) dfs(adj2[x][i], x); } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; ++i) { adj[i].push_back(i); sort(adj[i].begin(), adj[i].end()); v.push_back(make_pair(adj[i], i)); } sort(v.begin(), v.end()); ind[v[0].second] = pos++; for (int i = 1; i < n; ++i) { if (v[i - 1].first == v[i].first) ind[v[i].second] = pos - 1; else ind[v[i].second] = pos++; } --pos; for (int i = 0; i < n; ++i) { if (i != 0 && ind[v[i - 1].second] == ind[v[i].second]) continue; else { y = ind[v[i].second]; for (int j = 0; j < v[i].first.size(); ++j) { x = ind[v[i].first[j]]; if (x != y) { if (!s.count(make_pair(y, x))) { s.insert(make_pair(y, x)); adj2[y].push_back(x); } } } if (adj2[y].size() > 2) is = 1; if (adj2[y].size() == 1) root = y; } } if (is) return cout << "NO", 0; cout << "YES" << endl; if (pos == 1) { for (int i = 1; i <= n; ++i) cout << 1 << " "; return 0; } dfs(root, 0); for (int i = 1; i <= n; ++i) cout << ans[ind[i]] << " "; }
16
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; void solve() { int n; cin >> n; long long ans = n; while (!(n & 1)) { n >>= 1; ans += n; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { ans += max(i, n / i); n = max(i, n / i); i = 2; } } if (n != 1) ans++; cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int q = 1; while (q--) { solve(); } }
2
#include <bits/stdc++.h> struct nodo { int val; struct nodo* sigte; }; struct nodo* creaNodo(int dato); void imprimeLista(struct nodo** Last); int MCD(int N, int M); void agregar(struct nodo** Last, int dato); int main() { int n, i; scanf("%d", &n); int a[n]; i = 0; while (i < n && scanf("%d", &a[i++]) == 1) ; int k = 0; for (i = 0; i < n - 1; i++) { if (MCD(a[i], a[i + 1]) != 1) { k++; } } printf("%d\n", k); if (k > 0) { struct nodo* last = NULL; int count = 0; i = 0; while (i < n) { if (MCD(a[i], a[i + 1]) != 1 && i < n - 1) { agregar(&last, a[i]); agregar(&last, 1); count += 2; } else { agregar(&last, a[i]); count++; } i++; } imprimeLista(&last); } else { for (i = 0; i < n; i++) { printf("%d ", a[i]); } } return 0; } int MCD(int N, int M) { if (M == 0) { return N; } if (N < M) { return MCD(M, N); } else { return MCD(M, N % M); } } void agregar(struct nodo** Last, int dato) { struct nodo* nuevo = NULL; nuevo = creaNodo(dato); if (*Last != NULL) { nuevo->sigte = *Last; } *Last = nuevo; } struct nodo* creaNodo(int dato) { struct nodo* aux; aux = (struct nodo*)malloc(sizeof(struct nodo)); aux->val = dato; aux->sigte = NULL; return aux; } void imprimeLista(struct nodo** Last) { int n; if ((*Last) != NULL) { imprimeLista(&((*Last)->sigte)); n = (*Last)->val; printf("%d ", n); } }
4
#include <bits/stdc++.h> using namespace std; int a[100010], dp[100010], b[100010]; int main() { int n, k, num; int i, j; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) scanf("%d", &a[i]); num = 0; b[num++] = a[1]; for (i = 2; i <= n; i++) { if (a[i] != a[i - 1]) b[num++] = a[i]; } dp[b[0]]++; for (i = 1; i < num - 1; i++) { if (b[i - 1] == b[i + 1]) dp[b[i]] += 2; else dp[b[i]]++; } dp[b[num - 1]]++; int ans = 0; j = 1; for (i = 1; i <= k; i++) { if (ans < dp[i]) { ans = dp[i]; j = i; } } printf("%d\n", j); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c; cin >> n >> a >> b >> c; int sum = 0; for (int i = 0; i <= a; i = i + 2) { for (int j = 0; j <= b; j++) { int res = (i / 2) + (j); int diff = n - res; if (diff < 0) { break; } if (diff % 2 == 0 && diff / 2 <= c) { sum++; } } } cout << sum; }
7
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 111; long long c[111][111]; void init() { int i, j; c[0][0] = 1; for (i = 1; i < 100; i++) { c[i][0] = c[i - 1][0]; for (j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } } int main() { int i, j; long long n, m; init(); cin >> n >> m; if (m & (m - 1)) { puts("0"); return 0; } long long d = 0, tmp = m; while (tmp) { d++; tmp >>= 1; } n++; n++; long long ans = d == 1 ? -1 : 0; for (i = 60; i >= 0; i--) { long long tp = 1ll << i; if (n < tp) continue; if (n & tp) { if (d < 0) break; ans += c[i][d]; d--; n -= (1ll << i); } } cout << ans << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { string wd; cin >> wd; int len = wd.length(); string f = ""; int count = 1, s; if (len % 2 == 0) s = len / 2 - 1; else s = len / 2; f = f + wd[s]; while (count != len) { if (count % 2 != 0) s = s + count; else s = s - count; f = f + wd[s]; count++; } cout << f; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int T, n; vector<int> v1, v2, res; string str1, str2; void solve() { v1.clear(); v2.clear(); res.clear(); for (int i = 0; i < int(n); i++) { v1.push_back(str1[i] - '0'); v2.push_back(str2[i] - '0'); } int round = 0, idx = n - 1, left = 0, right = n - 1; for (int i = int(n - 1); i >= 0; i--) { if (v1[i] != v2[i]) { break; } idx--; right--; } while (idx >= 0) { int cur = round & 1 ? right-- : left++; if (round++ & 1) { v1[cur] ^= 1; } if (v1[cur] == v2[idx]) { res.push_back(1); } res.push_back(idx + 1); idx--; } cout << res.size(); for (int i = 0; i < int(res.size()); i++) { cout << " " << res[i]; } cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> T; while (T--) { cin >> n >> str1 >> str2; solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 100, INF = 10000; int main() { int n; cin >> n; vector<long long int> arr(n + 1); for (int i = 3; i <= n; ++i) { arr[i] = arr[i - 1] + i * (i - 1); } cout << arr[n] << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long n, m, sum = 0; unsigned long long nums[2][10]; cin >> n >> m; for (int i = 0; i < 10; ++i) { nums[0][i] = n / 10LL + ((n % 10LL >= (unsigned long long)i && i > 0LL) ? 1LL : 0LL); nums[1][i] = m / 10LL + ((m % 10LL >= (unsigned long long)i && i > 0LL) ? 1LL : 0LL); } sum = nums[0][0] * nums[1][0]; sum += nums[0][0] * nums[1][5] + nums[0][5] * nums[1][0]; sum += nums[0][1] * nums[1][4] + nums[0][4] * nums[1][1]; sum += nums[0][1] * nums[1][9] + nums[0][9] * nums[1][1]; sum += nums[0][2] * nums[1][3] + nums[0][3] * nums[1][2]; sum += nums[0][2] * nums[1][8] + nums[0][8] * nums[1][2]; sum += nums[0][3] * nums[1][7] + nums[0][7] * nums[1][3]; sum += nums[0][4] * nums[1][6] + nums[0][6] * nums[1][4]; sum += nums[0][5] * nums[1][5]; sum += nums[0][6] * nums[1][9] + nums[0][9] * nums[1][6]; sum += nums[0][7] * nums[1][8] + nums[0][8] * nums[1][7]; cout << sum; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, len; long long t[4 * 200007], dp[200007], mod[4 * 200007], total, modulo; void push(int v, int tl, int tr) { int tm = (tl + tr) >> 1; t[2 * v + 1] += (mod[v] * (tm - tl + 1)) % modulo; t[2 * v + 2] += (mod[v] * (tr - tm)) % modulo; t[2 * v + 1] %= modulo; t[2 * v + 2] %= modulo; mod[2 * v + 1] += mod[v]; mod[2 * v + 2] += mod[v]; mod[2 * v + 1] %= modulo; mod[2 * v + 2] %= modulo; mod[v] = 0; return; } void update(int v, int tl, int tr, int L, int R, int val) { if (tr < L || R < tl) return; if (L <= tl && tr <= R) { mod[v] += val; mod[v] %= modulo; t[v] += (tr - tl + 1) * val; t[v] %= modulo; return; } push(v, tl, tr); int tm = (tl + tr) >> 1; update(2 * v + 1, tl, tm, L, R, val); update(2 * v + 2, tm + 1, tr, L, R, val); t[v] = (t[2 * v + 1] + t[2 * v + 2]) % modulo; return; } long long get_dp(int v, int tl, int tr, int x) { if (tr < x || x < tl) return 0; if (tl == tr) return t[v]; push(v, tl, tr); int tm = (tl + tr) >> 1; long long dp1 = get_dp(2 * v + 1, tl, tm, x); long long dp2 = get_dp(2 * v + 2, tm + 1, tr, x); return dp1 + dp2; } int main() { cin >> n >> modulo; dp[1] = 1; total = 1; len = 2; for (int i = 1 + 1; i <= n; i += 1) { update(0, 1, n, i, min(n, i + len - 1), dp[1]); len++; } for (int i = 2; i <= n; i++) { dp[i] = (total + get_dp(0, 1, n, i)) % modulo; total += dp[i]; total %= modulo; len = 2; for (int j = 2 * i; j <= n; j += i) { update(0, 1, n, j, min(n, j + len - 1), dp[i]); len++; } } cout << dp[n]; return 0; }
9
#include <bits/stdc++.h> using namespace std; using lint = long long; using VI = vector<int>; using VVI = vector<VI>; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; VI y1(n), y2(m); for (int i = 0; i < n; i++) { cin >> y1[i]; y1[i] += 10000; y1[i] *= 2; } for (int i = 0; i < m; i++) { cin >> y2[i]; y2[i] += 10000; y2[i] *= 2; } set<int> inter; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { inter.insert((y1[i] + y2[j]) / 2); } } VI iof(40001); auto itr = inter.begin(); int sn = inter.size(); for (int i = 0; i < sn; i++) { iof[*itr] = i; itr++; } VVI tab(sn, VI(sn)); for (int i = 0; i < n; i++) { set<int> a; for (int j = 0; j < m; j++) { int t = (y1[i] + y2[j]) / 2; a.insert(t); } for (int k = 0; k < sn; k++) { for (auto&& j : a) { tab[iof[j]][k]++; tab[k][iof[j]]++; } } for (auto&& j : a) { for (auto&& k : a) { tab[iof[j]][iof[k]]--; } } } for (int i = 0; i < m; i++) { set<int> a; for (int j = 0; j < n; j++) { int t = (y2[i] + y1[j]) / 2; a.insert(t); } for (int k = 0; k < sn; k++) { for (auto&& j : a) { tab[iof[j]][k]++; tab[k][iof[j]]++; } } for (auto&& j : a) { for (auto&& k : a) { tab[iof[j]][iof[k]]--; } } } int res = 0; for (int i = 0; i < sn; i++) { for (int j = 0; j < sn; j++) { res = max(res, tab[i][j]); } } cout << res << '\n'; return 0; }
13
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define all(v) begin(v), end(v) #define decision (yes ? "YES" : "NO") #define countlz __builtin_clz #define popcount __builtin_popcount #define mem(arr, val) memset(arr, val, sizeof(arr)) typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, int> pli; typedef pair<int, long long> pil; typedef pair<long long, long long> pll; template <typename T> inline bool hasbit(T mask, int i) { return (mask & (1LL << i)) != 0; } template <typename T> inline T setbit(T mask, int i) { return (mask | (1LL << i)); } template <typename T> inline T invbit(T mask, int i) { return (mask ^ (1LL << i)); } template <typename T> inline T clrbit(T mask, int i) { return (mask & ~(1LL << i)); } // const double PI = acos(-1.0); // const double E = exp(1.0); const double EPS = 1e-9; const int INF = (int)1e9; const int MOD = (int)1e9 + 7; const int N = (int)1e5; inline bool ask(int y) { cout << y << endl; cout.flush(); int r; cin >> r; if (r == -1) { exit(1); } return r; } void work() { int n, k; cin >> n >> k; bool done = ask(0); for (int i = 1; i < n && !done; i++) { int y = (i ^ (i - 1)); done = ask(y); } } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); int T = 0; cin >> T; for (int casen = 1; casen <= T; casen++) { work(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int col[300005]; vector<int> v[300005], g[300005]; void dfs(int s, int pp) { set<int> st; vector<int> vv; int i, p = 0; for (i = 0; i < v[s].size(); ++i) { int x = v[s][i]; if (col[x]) st.insert(col[x]); else vv.push_back(x); } for (i = 0; p < vv.size() && i < v[s].size(); ++i) { if (st.find(i + 1) == st.end()) col[vv[p++]] = i + 1; } for (i = 0; i < g[s].size(); ++i) if (g[s][i] != pp) dfs(g[s][i], s); } int main() { int ans = 1, n, m, i, j; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) { int sz; scanf("%d", &sz); ans = max(ans, sz); for (j = 0; j < sz; ++j) { int x; scanf("%d", &x); v[i].push_back(x); } } for (i = 1; i < n; ++i) { int u, v; scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); printf("%d\n", ans); for (i = 1; i <= m; ++i) printf("%d ", col[i] == 0 ? 1 : col[i]); return 0; }
14
#include <bits/stdc++.h> int main() { int r, c; scanf("%d%d", &r, &c); if (r == 1 && c == 1) { printf("0\n"); return 0; } for (int i = 1; i <= r; ++i, puts("")) for (int j = 1; j <= c; ++j) printf("%d ", r < c ? i * (r + j) : j * (c + i)); }
6
#include <bits/stdc++.h> using namespace std; bool siv[100000]; vector<long long> prime; void sieve(); void pri(); long long int fact(long long int n); int main() { int p; cin >> p; int need = p - 10; int n = 13; int have[13] = {0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 15, 4}; int ans = 0; for (int i = 1; i < n; i++) { if (need == i) { ans = have[i]; } } cout << ans << endl; } void sieve() { memset(siv, true, sizeof(siv)); int root = sqrt(100000); for (int i = 2; i <= root; i++) { for (int j = i * 2; j <= 100000; j += i) { siv[j] = false; } } } void pri() { sieve(); siv[0] = false; siv[1] = false; for (int i = 0; i < 100000; i++) if (siv[i] == true) prime.push_back(i); } long long int fact(long long int n) { long long int ans = 1; for (long long int i = 1; i <= n; i++) { ans *= i; } return ans; }
0
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using qi = queue<int>; using si = set<int>; using misi = map<int, si>; using graph = vector<misi>; const int INF = 1000000; int main() { cin.tie(0), ios::sync_with_stdio(0); int n, m, u, v, t; cin >> n >> m; graph g(n); for (int i = 0; i < m; i++) { cin >> u >> v >> t; u--, v--; g[v][u].insert(t); } vi c(n); vi d(n, INF); vi s(n, 0); d[n - 1] = 0; qi q; q.push(n - 1); while (!q.empty()) { int u = q.front(); q.pop(); if (u == 0) break; vi todelete; for (auto [v, colors] : g[u]) { if (d[v] != INF) continue; if (s[v]) { for (int x : colors) if (x == c[v]) { d[v] = d[u] + 1; q.push(v); } todelete.push_back(v); continue; } if (colors.size() != 2) { todelete.push_back(v); c[v] = !*colors.begin(); s[v] = 1; } else { d[v] = d[u] + 1; q.push(v); } } for (int v : todelete) g[u].erase(v); } if (d[0] == INF) cout << "-1\n"; else cout << d[0] << '\n'; for (int x : c) cout << x; cout << '\n'; }
17
#include <bits/stdc++.h> using namespace std; long long l, r, mod, k; struct mat { long long a, b, c; mat() { a = 0; b = 1; c = 1; } mat operator*(mat& dr) const { mat ret; ret.a = a * dr.a + b * dr.b; ret.a %= mod; ret.b = a * dr.b + b * dr.c; ret.b %= mod; ret.c = b * dr.b + c * dr.c; ret.c %= mod; return ret; } }; long long getFib(long long x) { mat ans = mat(), mult = mat(); while (x) { if (x % 2) { ans = ans * mult; } mult = mult * mult; x /= 2; } return ans.a; } long long g(long long x) { return r / x - l / x; } vector<long long> divs, nums; int main() { cin >> mod >> l >> r >> k; l--; for (long long i = 1; i * i <= r; i++) { long long x1 = r / i; long long x2 = r / (r / i); divs.push_back(x1); divs.push_back(x2); } sort(divs.begin(), divs.end()); for (long long i = 0; i < divs.size(); i++) { if (g(divs[i]) < k) { cout << getFib(divs[i - 1]) << "\n"; return 0; } } return 0; }
16
#include <bits/stdc++.h> using namespace std; long long t1 = 114514334, t2 = 810893364; long long m1 = 1000000007, m2 = 1000000009; pair<long long, long long> gethash(vector<int> v) { sort(v.begin(), v.end()); pair<long long, long long> z = make_pair(123123, 23454); for (int i = 0; i < v.size(); i++) { z.first *= t1; z.second *= t2; z.first += v[i]; z.second += v[i]; z.first %= m1; z.second %= m2; } return z; } vector<int> pat[300000]; int dat[300000]; set<int> se[300000]; int ans[300000]; int c1[300000], c2[300000]; int ko[300000]; int main() { int num, way; scanf("%d%d", &num, &way); for (int i = 0; i < way; i++) { int za, zb; scanf("%d%d", &za, &zb); za--, zb--; pat[za].push_back(zb); pat[zb].push_back(za); } for (int i = 0; i < num; i++) pat[i].push_back(i); vector<pair<pair<long long, long long>, long long> > v; for (int i = 0; i < num; i++) v.push_back(make_pair(gethash(pat[i]), i)); sort(v.begin(), v.end()); pair<long long, long long> now = make_pair(-1, -1); int pt = 0; for (int i = 0; i < num; i++) { if (now != v[i].first) { now = v[i].first; pt++; } dat[v[i].second] = pt - 1; } for (int i = 0; i < num; i++) { for (int j = 0; j < pat[i].size(); j++) { int v = pat[i][j]; if (dat[i] != dat[v]) se[dat[i]].insert(dat[v]); } } int rr = -1; for (int i = 0; i < pt; i++) { if (se[i].size() >= 3) { printf("NO\n"); return 0; } if (se[i].size() <= 1) rr = i; } if (rr == -1) { printf("NO\n"); return 0; } int tt = 0; for (;;) { ans[rr] = tt++; if (se[rr].size() == 0) break; set<int>::iterator it = se[rr].begin(); se[*it].erase(rr); rr = *it; } for (int i = 0; i < num; i++) { ko[ans[dat[i]]]++; for (int j = 0; j < pat[i].size(); j++) { if (ans[dat[i]] == ans[dat[pat[i][j]]]) c1[ans[dat[i]]]++; else c2[min(ans[dat[i]], ans[dat[pat[i][j]]])]++; } } for (int i = 0; i < pt; i++) { if ((long long)ko[i] * (long long)ko[i] != c1[i]) { printf("NO\n"); return 0; } } for (int i = 0; i < pt - 1; i++) { if ((long long)ko[i] * (long long)ko[i + 1] * 2 != c2[i]) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 0; i < num; i++) printf("%d ", ans[dat[i]] + 1); printf("\n"); }
16
#include <bits/stdc++.h> using namespace std; int main() { int l, x, res; scanf("%d", &l); ; string s; cin >> s; for (x = 1; x < l; x++) res += (s[x] != s[x - 1]); printf("%d", min(res + 2, l)); }
8
#include <bits/stdc++.h> using namespace std; int a[2005]; int sum[3][2005]; int f[2005][2005][3]; int main() { int n, ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) sum[1][i] = sum[1][i - 1] + (a[i] == 1); for (int i = n; i >= 1; i--) sum[2][i] = sum[2][i + 1] + (a[i] == 2); for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { f[i][j][2] = f[i][j - 1][2] + (a[j] == 2); f[i][j][1] = max(f[i][j - 1][1], f[i][j - 1][2]) + (a[j] == 1); ans = max(ans, sum[1][i - 1] + sum[2][j + 1] + max(f[i][j][1], f[i][j][2])); } } printf("%d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, m, bl, c[200005 * 5], a[200005]; long long ans, Ans[200005]; struct node { int l, r, id; } p[200005]; inline bool cmp(node x, node y) { if (x.l / bl != y.l / bl) return x.l < y.l; if ((x.l / bl) & 1) return x.r > y.r; return x.r < y.r; } inline void add(int x) { x = a[x], ans -= 1ll * c[x] * c[x] * x, c[x]++, ans += 1ll * c[x] * c[x] * x; } inline void del(int x) { x = a[x], ans -= 1ll * c[x] * c[x] * x, c[x]--, ans += 1ll * c[x] * c[x] * x; } struct FastIO { static const int S = 1048576; char buf[S], *L, *R; int stk[20], Top; ~FastIO() { clear(); } inline char nc() { return L == R && (R = (L = buf) + fread(buf, 1, S, stdin), L == R) ? EOF : *L++; } inline void clear() { fwrite(buf, 1, Top, stdout); Top = 0; } inline void pc(char ch) { Top == S && (clear(), 0); buf[Top++] = ch; } inline void endl() { pc('\n'); } FastIO& operator>>(char& ch) { while (ch = nc(), ch == ' ' || ch == '\n') ; return *this; } template <typename T> FastIO& operator>>(T& ret) { ret = 0; int f = 1; char ch = nc(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = nc(); } while (ch >= '0' && ch <= '9') { ret = ret * 10 + ch - '0'; ch = nc(); } ret *= f; return *this; } FastIO& operator>>(char* s) { int Len = 0; char ch = nc(); while (ch != '\n') { *(s + Len) = ch; Len++; ch = nc(); } } template <typename T> FastIO& operator<<(T x) { if (x < 0) { pc('-'); x = -x; } do { stk[++stk[0]] = x % 10; x /= 10; } while (x); while (stk[0]) pc('0' + stk[stk[0]--]); return *this; } FastIO& operator<<(char ch) { pc(ch); return *this; } FastIO& operator<<(string str) { int Len = str.size() - 1; for (stk[0] = 0; Len >= 0; Len--) stk[++stk[0]] = str[Len]; while (stk[0]) pc(stk[stk[0]--]); return *this; } } fin, fout; int main() { fin >> n >> m; for (register int i = 1; i <= n; i++) fin >> a[i]; for (register int i = 1; i <= m; i++) fin >> p[i].l >> p[i].r, p[i].id = i; bl = (int)ceil(0.5 * n / sqrt(m)), sort(p + 1, p + m + 1, cmp); int L = 1, R = 0; for (register int i = 1; i <= m; i++) { int l = p[i].l, r = p[i].r; while (L > l) add(--L); while (R < r) add(++R); while (L < l) del(L++); while (R > r) del(R--); Ans[p[i].id] = ans; } for (register int i = 1; i <= m; i++) fout << Ans[i] << '\n'; return 0; }
14
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > mul(vector<vector<long long> >& A, vector<vector<long long> >& B, int N) { vector<vector<long long> > C( N, vector<long long>(N, 1070000000LL * 1070000000LL)); for (int i = 0; i < (N); i++) for (int k = 0; k < (N); k++) for (int j = 0; j < (N); j++) { C[i][j] = min(C[i][j], A[i][k] + B[k][j]); } return C; } vector<vector<long long> > pow(vector<vector<long long> > A, int N, long long n) { vector<vector<long long> > B( N, vector<long long>(N, 1070000000LL * 1070000000LL)); for (int i = 0; i < (N); i++) B[i][i] = 0; while (n) { if (n & 1) B = mul(B, A, N); A = mul(A, A, N); n >>= 1; } return B; } const int L = 100; int n, m, a[22], b[22]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < (n); i++) cin >> a[i]; for (int i = 0; i < (n); i++) cin >> b[i]; vector<vector<long long> > M( L + 1, vector<long long>(L + 1, 1070000000LL * 1070000000LL)); for (int mask = 0; mask < (1 << n); mask++) { long long cost = 0; int open = 0; int need = 0; for (int i = 0; i < (n); i++) { if (mask >> i & 1) { cost += a[i]; open++; } else { cost += b[i]; open--; need = max(need, -open); } } for (int i = need; i < (min(L, L - open) + 1); i++) { M[i][i + open] = min(M[i][i + open], cost); } } M = pow(M, L + 1, m); cout << M[0][0]; }
17
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long inf = 2e18; signed main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; int domino = (n * m) / 2; int extra_hori = 0, extra_ver = 0; if (n % 2 == 1) { extra_hori = m / 2; } if (m % 2 == 1) { extra_ver = n / 2; } if (extra_hori > k) { cout << "NO" << endl; continue; } int remain = domino - extra_hori - extra_ver; k -= extra_hori; bool f = false; if (k % 2 == 0 and (k <= remain)) { f = true; } cout << (f ? "YES" : "NO") << endl; ; } }
9
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long oo = 1e16 + 5; long long m[N][20], M[N][20]; long long n, s, l; long long dis(long long L, long long R) { long long k = log2(R - L + 1); R = R - (1 << k) + 1; return max(M[L][k], M[R][k]) - min(m[L][k], m[R][k]); } long long it[4 * N]; void update(long long i, long long l, long long r, long long pos, long long val) { if (l > pos || r < pos || l > r) return; if (l == r) { it[i] = val; return; } long long m = (l + r) / 2; update(i * 2, l, m, pos, val); update(i * 2 + 1, m + 1, r, pos, val); it[i] = min(it[i * 2], it[i * 2 + 1]); } long long get(long long i, long long l, long long r, long long a, long long b) { if (l > b || r < a || l > r) return 1e9 + 7; if (a <= l && r <= b) return it[i]; long long m = (l + r) / 2; return min(get(i * 2, l, m, a, b), get(i * 2 + 1, m + 1, r, a, b)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s >> l; for (long long i = 1; i <= n; ++i) { long long x; cin >> x; m[i][0] = M[i][0] = x; } for (long long i = 0; i < 16; ++i) for (long long j = 1; j + (1 << (i + 1)) <= n + 1; ++j) { m[j][i + 1] = min(m[j][i], m[j + (1 << i)][i]); M[j][i + 1] = max(M[j][i], M[j + (1 << i)][i]); } for (long long i = 1; i <= n; ++i) { if (i < l || dis(i - l + 1, i) > s) { update(1, 0, n, i, n + 1); continue; } long long be = 1, en = i - l + 1; while (be != en) { long long mi = (be + en) / 2; if (dis(mi, i) <= s) en = mi; else be = mi + 1; } long long val = get(1, 0, n, be - 1, i - l) + 1; update(1, 0, n, i, val); } long long ans = get(1, 0, n, n, n); if (ans > n) ans = -1; cout << ans << endl; }
12
#include <bits/stdc++.h> using namespace std; int n; int binpow(int x, int y) { int res = 1; while (y) { if (y & 1) res *= x; x *= x; y >>= 1; } return res; } void solve() { cin >> n; string s(n, 'a'); if (n % 2 == 0) { s[n / 2 - 1] = 'b'; } else { s[n / 2] = 'b'; s[0] = 'c'; } cout << s << "\n"; return; for (int mask = 0; mask < binpow(3, n); mask++) { string s; map<string, int> mp; for (int i = 0; i < n; i++) { int val = mask / binpow(3, i) % 3; s += (char)(val + '0'); } for (int i = 0; i < n; i++) { string cur; for (int j = i; j < n; j++) { cur += s[j]; ++mp[cur]; } } bool cool = true; for (auto it = mp.begin(); it != mp.end(); it++) if (it->second % 2 == 0) { cool = false; break; } if (cool) cout << s << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) { solve(); } }
10
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int tree[maxn]; vector<int> v[maxn]; int pre; int t; bool cmp2(int a, int b) { int x = (a - pre + t) % t; int y = (b - pre + t) % t; return x < y; } int lowbit(int x) { return x & (-x); } void update(int x, int n) { while (x < maxn) { tree[x] += n; x += lowbit(x); } } int getsum(int x) { int ans = 0; while (x > 0) { ans += tree[x]; x -= lowbit(x); } return ans; } int main() { int a; scanf("%d", &t); for (int i = 1; i <= t; i++) { scanf("%d", &a); v[a].push_back(i); update(i, 1); } pre = 1; for (int i = 1; i <= 100000; i++) { if (v[i].size()) { sort(v[i].begin(), v[i].end(), cmp2); pre = v[i][v[i].size() - 1]; } } int pre = 1; long long ans = 0; for (int i = 1; i <= 100000; i++) { if (!v[i].size()) continue; for (int j = 0; j < v[i].size(); j++) { int now = v[i][j]; if (now >= pre) ans += getsum(now) - getsum(pre - 1); else ans += getsum(t) - (getsum(pre - 1) - getsum(now)); pre = now; update(now, -1); } } printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimization("unroll-loops, no-stack-protector") #pragma GCC target("avx,avx2,fma") using namespace std; mt19937_64 rnd; const long long maxn = 20; const long long mod = 1e9 + 7; const long long base = 3e18; const long double eps = 1e-7; map<long long, long long> dp; long long a[maxn]; long long n, m, k; bool dd[maxn][maxn]; pair<long long, long long> adj[10]; bool dp1[(1ll << 6)][6]; bool chk(pair<long long, long long> a, pair<long long, long long> b) { if (dd[a.first][b.first] && dd[a.second][b.second]) return true; if (dd[a.first][b.second] && dd[a.second][b.first]) return true; return false; } void dosth(long long id) { if (id == n / 2 + 1) { long long msk = 0; memset(dp1, 0, sizeof(dp1)); for (int i = 1; i <= n; i++) { msk = msk * 7 + a[i]; if (!adj[a[i] - 1].first) adj[a[i] - 1].first = i; else adj[a[i] - 1].second = i; } long long len = n / 2; for (int t = 0; t < len; t++) dp1[(1ll << t)][t] = 1; for (int i = 1; i < (1ll << len); i++) { for (int j = 0; j < len; j++) { if (!dp1[i][j]) continue; for (int t = 0; t < len; t++) { if (i & (1ll << t)) continue; dp1[i + (1ll << t)][t] |= chk(adj[t], adj[j]); } } } bool kt = false; for (int t = 0; t < len; t++) { if (dp1[(1ll << len) - 1][t] && dd[adj[t].first][adj[t].second]) { kt = true; break; } } if (kt) { dp[msk] = 2; } else dp[msk] = 1; for (int i = 0; i < len; i++) adj[i] = make_pair(0ll, 0ll); return; } for (int i = 1; i <= n; i++) { if (!a[i]) { a[i] = id; for (int j = i + 1; j <= n; j++) { if (!a[j]) { a[j] = id; dosth(id + 1); a[j] = 0; } } a[i] = 0; return; } } } long long f(long long msk) { if (dp.count(msk)) return dp[msk]; vector<long long> vt; long long nw = msk; vector<long long> cnt(11, 0ll); for (int i = 0; i < n; i++) { vt.push_back(msk % 7); cnt[msk % 7]++; msk /= 7; } reverse(vt.begin(), vt.end()); long long ans = 1; for (int i = 0; i < vt.size(); i++) { if (cnt[vt[i]] >= 4) { for (int j = i + 1; j < vt.size(); j++) { if (vt[j] == vt[i]) { long long pre = vt[i]; vt[i] = n / 2 + 1; vt[j] = n / 2 + 1; vector<long long> kt(8, 0ll); long long nxt = 0; long long cnt = 1; for (int t = 0; t < vt.size(); t++) { if (!kt[vt[t]]) { kt[vt[t]] = cnt; cnt++; } nxt = nxt * 7 + kt[vt[t]]; } if (f(nxt) == 2) { ans = 2; break; } vt[i] = pre; vt[j] = pre; } } break; } } return dp[msk] = ans; } long long ans = 0; void dosth1(long long id, long long cnt) { if (id == n + 1) { vector<long long> dem(13, 0ll); long long msk = 0; for (int i = 1; i <= n; i++) { dem[a[i]]++; msk = msk * 7 + a[i]; } for (int i = 1; i <= n; i++) { if (dem[a[i]] % 2 == 1) return; } if (f(msk) == 2) { long long res = 1; for (long long t = k; t >= k - cnt + 1; t--) res = res * t; ans = (ans + res); } return; } for (int t = 1; t <= cnt; t++) { a[id] = t; dosth1(id + 1, cnt); a[id] = 0; } a[id] = cnt + 1; dosth1(id + 1, cnt + 1); a[id] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("t.inp", "r")) { freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); } cin >> n >> m >> k; for (int i = 1; i <= m; i++) { long long x, y; cin >> x >> y; dd[x][y] = 1; dd[y][x] = 1; } dosth(1); dosth1(1, 0); cout << ans << "\n"; }
22
#include <bits/stdc++.h> using namespace std; long long dp1[100010]; long long dp2[100010]; long long a[100100]; long long b[100100]; int main() { int n, ask, type, l, r; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b, b + n); dp1[0] = a[0]; dp2[0] = b[0]; for (long long i = 1; i < n; i++) { dp1[i] = dp1[i - 1] + a[i]; dp2[i] = dp2[i - 1] + b[i]; } scanf("%d", &ask); while (ask--) { scanf("%d%d%d", &type, &l, &r); if (type == 1) printf("%I64d\n", dp1[r - 1] - dp1[l - 2]); if (type == 2) printf("%I64d\n", dp2[r - 1] - dp2[l - 2]); } return 0; }
4
#include <bits/stdc++.h> const int N = 31; const int MOD = (int)1e9 + 7; int n; int dp0[N][N], dp1[N][N]; inline void Ad(int &a, int b) { if ((a += b) >= MOD) a -= MOD; } int main() { scanf("%d", &n); dp1[30][0] = 1; for (int i = 30; i >= 1; --i) { for (int j = 0; j <= 30; ++j) { int x = (j) ? (1 << (j - 1)) : (1); int y = (j) ? (1 << (j - 1)) : (0); { Ad(dp0[i - 1][j + 1], dp0[i][j]); Ad(dp0[i - 1][j], (1LL << j) * dp0[i][j] % MOD); } { if ((n >> (i - 1)) & 1) { Ad(dp0[i - 1][j], (long long)x * dp1[i][j] % MOD); Ad(dp1[i - 1][j + 1], dp1[i][j]); Ad(dp1[i - 1][j], (long long)y * dp1[i][j] % MOD); } else { Ad(dp1[i - 1][j], (long long)x * dp1[i][j] % MOD); } } } } int ans = 0; for (int i = 0; i <= 30; ++i) { Ad(ans, dp0[0][i]); Ad(ans, dp1[0][i]); } printf("%d\n", ans); return 0; }
19
#include <bits/stdc++.h> template <class _, class __> bool checkmax(_ &a, __ b) { return a < b ? a = b, true : false; } template <class _, class __> bool checkmin(_ &a, __ b) { return a > b ? a = b, true : false; } using namespace std; const int P = 1e9 + 7; char str[105]; int A[105]; int dp[105][4050]; int n; void Add(int &a, int b) { a += b; if (a >= P) a -= P; } void solve() { memset(dp, 0, sizeof(dp)); for (int i = (0), i_end_ = (26); i < i_end_; ++i) dp[1][i] = 1; for (int i = (2), i_end_ = (105); i < i_end_; ++i) { for (int j = (0), j_end_ = (4050); j < j_end_; ++j) { for (int k = (0), k_end_ = (26); k < k_end_; ++k) if (j >= k) Add(dp[i][j], dp[i - 1][j - k]); } } } int main() { int test; solve(); cin >> test; while (test--) { int sum = 0; scanf("%s", str + 1); n = strlen(str + 1); for (int i = (1), i_end_ = (n); i <= i_end_; ++i) sum += str[i] - 'a'; printf("%d\n", dp[n][sum] - 1); } return EXIT_SUCCESS; }
12
#include <bits/stdc++.h> using namespace std; double v[302000], b[302000]; struct node { int L, R; double Max; int Mid() { return (L + R) / 2; } } a[302000 * 4]; void Build(int r, int L, int R) { a[r].L = L, a[r].R = R; a[r].Max = 0; if (L == R) return; Build(r << 1, L, a[r].Mid()); Build(r << 1 | 1, a[r].Mid() + 1, R); } double Query(int r, int L, int R) { if (L > R) return 0; if (a[r].L == L && a[r].R == R) return a[r].Max; if (R <= a[r].Mid()) return Query(r << 1, L, R); else if (L > a[r].Mid()) return Query(r << 1 | 1, L, R); else { double ans1 = Query(r << 1, L, a[r].Mid()); double ans2 = Query(r << 1 | 1, a[r].Mid() + 1, R); return max(ans1, ans2); } } void Update(int r, int pos, double num) { if (a[r].L == a[r].R && a[r].L == pos) { a[r].Max = num; return; } if (pos <= a[r].Mid()) Update(r << 1, pos, num); else Update(r << 1 | 1, pos, num); a[r].Max = max(a[r << 1].Max, a[r << 1 | 1].Max); } int main() { int n; long long r, h; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) { scanf("%I64d %I64d", &r, &h); v[i] = b[i] = 4 * atan(1.0) * h * r * r; } sort(b, b + n); int len = unique(b, b + n) - b; Build(1, 1, len); double ans = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(b, b + len, v[i]) - b; double res = Query(1, 1, pos - 1) + v[i]; Update(1, pos, res); ans = max(ans, res); } printf("%.12f\n", ans); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int sx, sy, ex, ey; char str[3]; vector<string> ans; int main() { scanf("%s", str); sx = (int)(str[0] - 'a'), sy = (int)(str[1] - '1'); scanf("%s", str); ex = (int)(str[0] - 'a'), ey = (int)(str[1] - '1'); while (sx != ex || sy != ey) { if (sx == ex) { if (sy < ey) ans.push_back("U"), sy++; else ans.push_back("D"), sy--; } else if (sy == ey) { if (sx < ex) ans.push_back("R"), sx++; else ans.push_back("L"), sx--; } else { if (sx < ex) { if (sy < ey) ans.push_back("RU"), sy++; else ans.push_back("RD"), sy--; sx++; } else { if (sy < ey) ans.push_back("LU"), sy++; else ans.push_back("LD"), sy--; sx--; } } } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) cout << ans[i] << endl; return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using pii = pair<int, int>; using pll = pair<long long, long long>; using vb = vector<bool>; using vi = vector<int>; using vl = vector<long long>; using vvb = vector<vector<bool>>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; using vpii = vector<pair<int, int>>; using mii = map<int, int>; using umii = unordered_map<int, int>; using seti = set<int>; using useti = unordered_set<int>; template <typename... ArgTypes> void print(ArgTypes... args); template <typename... ArgTypes> void input(ArgTypes &...args); template <> void print() {} template <> void input() {} template <typename T, typename... ArgTypes> void print(T t, ArgTypes... args) { cout << t; print(args...); } template <typename T, typename... ArgTypes> void input(T &t, ArgTypes &...args) { cin >> t; input(args...); } int n; vvi adj; vi downMemo; vi upMemo; vi parent; vector<pii> downs; void check(int &x, int &y, int c) { if (c >= x) { y = x; x = c; } else if (c > y) y = c; } int downDp(int node, int par) { parent[node] = par; if (downMemo[node]) return downMemo[node]; for (auto a : adj[node]) if (a != parent[node]) check(downs[node].first, downs[node].second, downDp(a, node)); if (adj[node].size() == 1) return downMemo[node] = 1; int sz = adj[node].size() - (par != -1); return downMemo[node] = sz + downs[node].first; } int upDp(int node) { if (upMemo[node]) return upMemo[node]; int cc = 0, x = 0; int now = parent[node]; int self = downMemo[node]; bool taken = 0; if (now) { cc++; x = max(x, upDp(now)); } int sz = adj[now].size() - (parent[now] != -1); if (downs[now].first == self) { if (sz >= 2) x = max(x, downs[now].second); else return upMemo[node] = 1 + x; } else x = max(x, downs[now].first); return upMemo[node] = cc + sz + x - 1; } inline void _() { cin >> n; adj = vvi(n, vi()); downMemo = vi(n); upMemo = vi(n); parent = vi(n); downs = vector<pii>(n, {0, 0}); 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); } if (n == 1) { cout << 1 << endl; return; } int res = 0; downDp(0, -1); for (int node = 0; node < n; node++) { int k = 0, x = 0, y = 0; if (node) { k++; check(x, y, upDp(node)); } for (auto a : adj[node]) if (a != parent[node]) { k++; check(x, y, downMemo[a]); } if (k >= 2) res = max(res, x + y + k - 1); else res = max(res, x + 1); } cout << res << endl; } int main() { ios_base::sync_with_stdio(0); int __; cin >> __; while (__--) _(); }
14
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int main() { int n, k; string t; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; cin >> t; string zz = t; int l1 = t.length() * (k - 1); for (int i = 0; i < l1; i++) zz.push_back('*'); l1 += (t.length()); int idx = 0; while (k > 0) { string cmp = zz.substr(idx, t.length()); int sm = 0, l2 = t.length(); for (int i = 0; i < l2; i++) if (t[i] == cmp[i] || cmp[i] == '*') sm++; if (sm == l2) { k--; int kk = 0; for (int i = idx; i < idx + l2; i++) zz[i] = t[kk], kk++; } idx++; } for (int i = 0; i < l1; i++) { if (zz[i] == '*') break; cout << zz[i]; } cout << "\n"; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, sum = 0; cin >> n; for (int i = 1; i <= 999; ++i) { sum += i; if (sum == n) { cout << "YES"; return 0; } if (sum > n) { cout << "NO"; return 0; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long t, n, s[2000001], f[2000001], m = 1000000007; void dp() { s[1] = 1; s[2] = 1; f[1] = 1; f[2] = 1; for (int i = 3; i < 2000001; i++) s[i] = (s[i - 2] * 2 + s[i - 1]) % m; for (int i = 3; i < 2000001; i++) f[i] = (f[i - 3] + s[i]) % m; } int main() { scanf("%d", &t); dp(); while (t--) { scanf("%d", &n); if (n <= 2) printf("0\n"); else printf("%lld\n", (4 * f[n - 2]) % m); } return 0; }
11
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > beacons; int a[100005], b[100005], bst[100005]; int main() { int n, x, y; cin >> n; for (int i = 0; i < n; ++i) { cin >> x >> y; beacons.push_back(make_pair(x, y)); } sort(beacons.begin(), beacons.end()); for (int i = 0; i < n; ++i) { a[i] = beacons[i].first; b[i] = beacons[i].second; } bst[0] = 0; int opt = n - 1; for (int i = 1; i < n; ++i) { long long it = upper_bound(a, a + i, a[i] - b[i] - 1) - a; bst[i] = bst[it - 1] + i - it; opt = min(opt, bst[i] + n - i - 1); } cout << opt; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = n; i >= 1; i--) cout << i << " "; cout << endl; } return 0; }
0
#include <bits/stdc++.h> int a[10] = {1, 6, 28, 120, 496, 2016, 8128, 32640, 130816, 523776}; int main() { int n, i, r; scanf("%d", &n); for (i = 9; i >= 0; i--) if (n % a[i] == 0) { printf("%d\n", a[i]); return 0; } return 0; }
2
#include <bits/stdc++.h> int n, m, q; char str[1005]; bool a[1005][1005]; std::vector<std::pair<int, int> > del; int num[1005][1005], tot, ld, ru; std::set<std::pair<int, int> > s; namespace FU { int f[1005 * 1005]; std::vector<int> inv[1005 * 1005]; std::set<int> su; void init(int i) { if (su.count(i)) return; su.insert(i); f[i] = i; inv[i].push_back(i); } void mrg(int u, int v) { init(u); init(v); u = f[u]; v = f[v]; if (u == v) return; for (int i = 0; i < inv[u].size(); i++) { f[inv[u][i]] = v; inv[v].push_back(inv[u][i]); } inv[u].clear(); } bool cnn(int u, int v) { init(u); init(v); return f[u] == f[v]; } void cl() { for (std::set<int>::iterator i = su.begin(); i != su.end(); i++) { f[*i] = 0; inv[*i].clear(); } su.clear(); } } // namespace FU using namespace FU; inline void dfs(int x, int y, int id) { num[x][y] = id; for (int i = x - 1; i <= x + 1; i++) for (int j = y - 1; j <= y + 1; j++) if (a[i][j] && !num[i][j]) dfs(i, j, id); } inline void R1(int x, int y) { for (int i = x - 1; i <= x + 1; i++) for (int j = y - 1; j <= y + 1; j++) if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j]) mrg(num[x][y], num[i][j]); } inline void R2(int x, int y) { for (int i = x - 2; i <= x + 2; i++) for (int j = y - 2; j <= y + 2; j++) if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j] && num[x][y] != num[i][j]) s.insert(std::make_pair(num[x][y], num[i][j])); } inline bool chk(int x, int y) { for (int i = x - 2; i <= x + 2; i++) for (int j = y - 2; j <= y + 2; j++) if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j]) if ((cnn(num[x][y], ld) && cnn(num[i][j], ru)) || (cnn(num[x][y], ru) && cnn(num[i][j], ld))) return 1; return 0; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 3; i <= n + 1; i++) a[i][0] = 1; for (int i = 1; i <= n - 2; i++) a[i][m + 1] = 1; for (int i = 3; i <= m + 1; i++) a[0][i] = 1; for (int i = 1; i <= m - 2; i++) a[n + 1][i] = 1; for (int i = 1; i <= n; i++) { scanf("%s", str + 1); for (int j = 1; j <= m; j++) a[i][j] = str[j] == '#'; } for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) if (a[i][j] && !num[i][j]) dfs(i, j, ++tot); for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) if (num[i][j]) R2(i, j); ld = num[3][0]; ru = num[0][3]; while (q--) { cl(); init(ld); init(ru); del.clear(); int k; scanf("%d", &k); int lst = tot; bool flg = 1; for (int i = 1; i <= k; i++) { int x, y; scanf("%d%d", &x, &y); num[x][y] = ++tot; R1(x, y); del.push_back(std::make_pair(x, y)); } for (std::set<int>::iterator i = su.begin(); i != su.end(); i++) for (std::set<int>::iterator j = su.begin(); j != su.end(); j++) if (cnn(*i, ld) && cnn(*j, ru) && (s.count(std::make_pair(*i, *j)) || s.count(std::make_pair(*j, *i)))) flg = 0; for (std::vector<std::pair<int, int> >::iterator it = del.begin(); it != del.end(); it++) if (chk(it->first, it->second)) flg = 0; puts(flg ? "YES" : "NO"); fflush(stdout); for (std::vector<std::pair<int, int> >::iterator it = del.begin(); it != del.end(); it++) num[it->first][it->second] = 0; tot -= k; } }
27