solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160000") template <typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template <typename T> using max_heap = std::priority_queue<T, std::vector<T>, std::less<T>>; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int bestPrice = 1000000000LL; int ans = 0; for (int i = (0); i < (n); i++) { int a, p; cin >> a >> p; bestPrice = min(p, bestPrice); ans += a * bestPrice; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int isPrime[1000005]; void pre() { for (long long int i = 2; i < 1000005; i++) isPrime[i] = 1; for (long long int i = 2; i < 1000005; i++) if (isPrime[i]) { for (long long int j = i * i; j < 1000005; j += i) isPrime[j] = 0; } } vector<int> prefix; int firstOccurence(int val) { if (prefix[prefix.size() - 1] < val) return -1; int start = 0, end = prefix.size() - 1, mid; while (start <= end) { mid = (start + end) / 2; if (prefix[mid] < val) start = mid + 1; if (prefix[mid] == val) { if (mid == 0) return mid; if (prefix[mid - 1] != val) return mid; else end = mid - 1; } if (prefix[mid] > val) end = mid - 1; } } int main(void) { int i, j, a, b, l, ans = 0, k; pre(); cin >> a >> b >> k; for (i = 1; i < a; i++) isPrime[i] = 0; prefix.push_back(0); for (i = 1; i <= b; i++) prefix.push_back(prefix[i - 1] + isPrime[i]); for (i = a; i <= b; i++) { int idx; if (isPrime[i] == 0) idx = firstOccurence(prefix[i] + k); else idx = firstOccurence(prefix[i] + k - 1); if (idx > 0) ans = max(ans, idx - i + 1); } if (ans == 0) cout << "-1"; else { int remainingElements = b - a + 1; for (i = b; i >= a; i--) { if (i - ans < a) break; if (i - ans >= a && prefix[i] - prefix[i - ans] < k) { ans++; i++; } } cout << ans; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int max_n = 505; int n, a[max_n]; int dp[max_n]; vector<int> stk; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) { dp[i] = i; stk.clear(); for (int j = i; j >= 1; j--) { int x = a[j]; while (!stk.empty() && stk.back() == x) x++, stk.pop_back(); stk.push_back(x); if (stk.size() == 1) dp[i] = min(dp[j - 1] + 1, dp[i]); } } printf("%d\n", dp[n]); return 0; }
13
#include <bits/stdc++.h> using namespace std; const int N = int(1e7) + 5; int pr[N]; vector<int> primes; unsigned long long P[N]; bool PODHODIT(unsigned long long n) { for (int i = 0; i < primes.size(); i++) { unsigned long long sn = n; unsigned long long counter = 0; while (sn) { sn /= primes[i]; counter += sn; } if (counter < P[primes[i]]) return false; } return true; } int main() { ios::sync_with_stdio(false); for (int i = 2; i <= N; i++) { if (!pr[i]) { primes.push_back(i); pr[i] = i; } for (int j = 0; j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++) pr[i * primes[j]] = primes[j]; } int n; cin >> n; unsigned long long rg = 0, lg = 0; for (int i = 0; i < n; i++) { int inp; cin >> inp; rg += inp; P[inp]++; } for (int i = N - 2; i >= 2; i--) P[i] += P[i + 1]; for (int i = N - 1; i >= 2; i--) { if (pr[i] == i) continue; P[pr[i]] += P[i]; P[i / pr[i]] += P[i]; P[i] = 0; } while (lg < rg) { unsigned long long mid = (lg + rg) / 2; if (PODHODIT(mid)) rg = mid; else lg = mid + 1; } cout << (rg ? rg : 1) << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; int n, m, cnt[200009], spec[2000009], node1 = 1e9, ans, mx, node2 = 1e9; vector<int> v[2000009]; void dfs(int node, int p, int h, int &bstnode) { if ((h > mx || (h == mx && node < bstnode)) && spec[node]) mx = h, bstnode = node; for (auto i : v[node]) if (i != p) dfs(i, node, h + 1, bstnode); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; ans = 2 * n - 2; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; cnt[x]++, cnt[y]++; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= m; i++) { int x; cin >> x; node1 = min(x, node1); spec[x] = 1; } if (m == 1) return cout << node1 << "\n" << 0, 0; vector<int> leaf; for (int i = 1; i <= n; i++) if (cnt[i] == 1 && !spec[i]) leaf.push_back(i); while (!leaf.empty()) { vector<int> temp; ans -= 2 * leaf.size(); for (auto i : leaf) for (auto j : v[i]) { cnt[j]--; if (cnt[j] == 1 && !spec[j]) temp.push_back(j); } leaf = temp; } dfs(node1, 0, 0, node1); mx = 0; dfs(node1, 0, 0, node2); cout << min(node1, node2) << "\n" << ans - mx; }
14
#include <bits/stdc++.h> using std::make_pair; using std::pair; const int N = 1000 + 10; std::vector<std::tuple<int, int, int>> edges; std::vector<int> neighbors[N]; int weights[N], size[N], father[N], n; namespace union_set { int ancestor[N], size[N]; void init(int n) { for (int i = 1; i <= n; ++i) { ancestor[i] = i; size[i] = 1; } } int get_ancestor(int u) { return ancestor[u] == u ? u : ancestor[u] = get_ancestor(ancestor[u]); } void merge(int u, int v) { u = get_ancestor(u); v = get_ancestor(v); if (size[u] < size[v]) { std::swap(u, v); } size[u] += size[v]; ancestor[v] = u; } } // namespace union_set void dfs(int u, int f) { size[u] = 1; father[u] = f; for (auto v : neighbors[u]) { if (v == f) continue; dfs(v, u); size[u] += size[v]; } } void paint(int u, int f, int set_root, int coeff) { int s = 0; for (auto v : neighbors[u]) { if (v == f) continue; if (set_root == -1 || union_set::get_ancestor(v) == set_root) { edges.push_back(std::make_tuple(u, v, (s + 1) * coeff)); paint(v, u, -1, coeff); s += size[v]; } } } void run(int case_id = 0) { std::cin >> n; for (int i = 1; i < n; ++i) { int u, v; std::cin >> u >> v; neighbors[u].push_back(v); neighbors[v].push_back(u); } if (n == 1) return; if (n == 2) { std::cout << "1 2 1" << std::endl; return; } dfs(1, -1); int root = 0, max_size = 0; for (int u = 1; u <= n; ++u) { int temp = n - size[u]; for (auto v : neighbors[u]) { if (v == father[u]) continue; if ((temp) < (size[v])) (temp) = (size[v]); } if (root == 0 || temp < max_size) { root = u; max_size = temp; } } dfs(root, -1); union_set::init(n); std::priority_queue<pair<int, int>, std::vector<pair<int, int>>, std::greater<pair<int, int>>> heap; for (auto v : neighbors[root]) { heap.push(make_pair(size[v], v)); } while (heap.size() > 2) { auto p = heap.top(); heap.pop(); auto q = heap.top(); heap.pop(); union_set::merge(p.second, q.second); heap.push(make_pair(p.first + q.first, p.second)); } int root_u = union_set::get_ancestor(heap.top().second); heap.pop(); int root_v = union_set::get_ancestor(heap.top().second); heap.pop(); int size_u = 0; for (auto v : neighbors[root]) { if (union_set::get_ancestor(v) == root_u) { size_u += size[v]; } } paint(root, -1, root_u, 1); paint(root, -1, root_v, 1 + size_u); for (auto e : edges) { int u = std::get<0>(e); int v = std::get<1>(e); int w = std::get<2>(e); std::cout << u << " " << v << " " << w << "\n"; } } int main() { std::ios::sync_with_stdio(false); run(); return 0; }
19
#include <bits/stdc++.h> using namespace std; namespace IO { char buf[1 << 21], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; inline int read() { int x = 0, sign = 0; char s = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); while (!isdigit(s)) sign |= s == '-', s = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); while (isdigit(s)) x = (x << 1) + (x << 3) + (s - '0'), s = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); return sign ? -x : x; } void print(int x) { if (x > 9) print(x / 10); (*O++ = x % 10 + '0'); } } // namespace IO namespace math { const int mod = 99824353; const int maxn = 1e6 + 5; long long ksm(long long a, long long b) { long long s = 1, m = a; while (b) { if (b & 1) s = s * m % mod; m = m * m % mod, b >>= 1; } return s; } long long inv(long long x) { return ksm(x, mod - 2); } long long fc[maxn], ifc[maxn]; void init_m(int n) { fc[0] = 1; for (int i = 1; i <= n; i++) fc[i] = fc[i - 1] * i % mod; ifc[n] = inv(fc[n]); for (int i = n - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % mod; } long long C(long long n, long long m) { return fc[n] * ifc[m] % mod * ifc[n - m] % mod; } } // namespace math using namespace IO; const int N = 1e3 + 5; const int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0}; int n, m, t, cnum, col[N][N], cov[N * N], ori[N * N]; char c[N][N]; vector<int> e[N * N]; long long dis[N * N]; int dfs(int x, int y, char z) { col[x][y] = cnum; int sz = 1; for (int i = 0; i < 4; i++) { int xx = x + dx[i], yy = y + dy[i]; if (xx && yy && xx <= n && yy <= m && !col[xx][yy] && c[xx][yy] == z) sz += dfs(xx, yy, z); } return sz; } void solve() { cin >> n >> m >> t; queue<int> q; memset(dis, 0x7f, sizeof(dis)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> c[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (!col[i][j]) { ori[++cnum] = c[i][j] - '0'; int tmp = dfs(i, j, c[i][j]); if (tmp > 1) q.push(cnum), cov[cnum] = cnum, dis[cnum] = 0; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int k = 0; k < 4; k++) { int ii = i + dx[k], jj = j + dy[k]; if (ii && jj && ii <= n && jj <= m && col[i][j] != col[ii][jj]) { e[col[i][j]].emplace_back(col[ii][jj]); e[col[ii][jj]].emplace_back(col[i][j]); } } while (!q.empty()) { int t = q.front(); q.pop(); for (int it : e[t]) if (!cov[it]) cov[it] = cov[t], dis[it] = dis[t] + 1, q.push(it); } while (t--) { long long x, y, m; cin >> x >> y >> m; int c = col[x][y]; if (dis[c] >= m) cout << ori[c] << endl; else cout << ((m & 1) ^ ori[cov[c]]) << endl; } } int main() { int t = 1; while (t--) solve(); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; const int MOD = 1e9 + 7; int l[MAXN], r[MAXN]; int inv(int x) { int ans = 1; for (int b = MOD - 2; b; b >>= 1) { if (b & 1) ans = 1LL * ans * x % MOD; x = 1LL * x * x % MOD; } return ans; } int d(int i) { int a = max(0, min(r[i], r[i + 1]) - max(l[i], l[i + 1]) + 1); int b = 1LL * (r[i] - l[i] + 1) * (r[i + 1] - l[i + 1] + 1) % MOD; return 1LL * a * inv(b) % MOD; } int p(int i) { int a = 1 - d(i); if (a < 0) a += MOD; return a; } int p1(int i) { int a = 1 - d(i) - d(i + 1); while (a < 0) a += MOD; int b = max(0, min(min(r[i], r[i + 1]), r[i + 2]) - max(max(l[i], l[i + 1]), l[i + 2]) + 1); int c = 1LL * (r[i] - l[i] + 1) * (r[i + 1] - l[i + 1] + 1) % MOD * (r[i + 2] - l[i + 2] + 1) % MOD; a = (1LL * a * c + b) % MOD; return 1LL * a * inv(c) % MOD; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &l[i]); for (int i = 1; i <= n; ++i) scanf("%d", &r[i]); int sum = 1, ans = 0; for (int i = 1; i < n; ++i) { int x = p(i), y = p(i + 1); ans = (ans - 1LL * x * x % MOD + x + MOD) % MOD; if (i < n - 1) ans = (ans - 2LL * x * y % MOD + 2 * p1(i) + 2 * MOD) % MOD; sum += x; if (sum >= MOD) sum -= MOD; } ans = (ans + 1LL * sum * sum) % MOD; printf("%d\n", ans); return 0; }
17
#include <bits/stdc++.h> int main(void) { long long int n; scanf(" %lld", &n); long long int pos = 0; for (int i = 1;; i++) { if (pos + i >= n) { break; } pos += i; } long long int r = 0; while (pos < n) { pos++; r++; } printf("%lld\n", r); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long k, a, b; cin >> k >> a >> b; long long an = a / k; long long ans = b / k; long long aa = a % k; long long bb = b % k; bool fl = true; if (aa > (ans * k) || bb > (an * k)) fl = false; if (fl == false) cout << "-1" << endl; else cout << (ans + an) << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[20005], maxflow = 0; int n, m, g, sex[20005], v[20005], w[2005], k[2005], b[2005]; int S, T, vis[20005], sign[20005], head[20005], len = 1, cnt = 0; struct way { int num, next, cap; } G[20005 * 6 + 5]; void link(int x, int y, int z) { G[++len] = (way){y, head[x], z}, head[x] = len; } bool bfs() { int i, x; queue<int> Q; vis[S] = ++cnt, sign[S] = 0; for (Q.push(S); !Q.empty();) { x = Q.front(), Q.pop(); for (i = head[x]; i; i = G[i].next) if (G[i].cap && vis[G[i].num] != cnt) { vis[G[i].num] = cnt; sign[G[i].num] = sign[x] + 1; Q.push(G[i].num); } } return vis[T] == cnt; } int dfs(int t, int maxi) { int i, ans = 0, res; if (t == T || !maxi) return maxi; for (i = head[t]; i; i = G[i].next) if (G[i].cap && sign[G[i].num] == sign[t] + 1) { res = dfs(G[i].num, min(maxi, G[i].cap)); ans += res, maxi -= res, G[i].cap -= res, G[i ^ 1].cap += res; if (!maxi) return ans; } sign[t] = (1 << 30); return ans; } int main() { int i, j, s; scanf("%d %d %d", &n, &m, &g); S = n + m + 1, T = n + m + 2; for (i = 1; i <= n; i++) scanf("%d", &sex[i]); for (i = 1; i <= n; i++) scanf("%d", &v[i]); for (i = 1; i <= n; i++) if (sex[i]) link(i, T, v[i]), link(T, 0, i); else link(S, i, v[i]), link(i, S, 0); for (i = 1; i <= m; i++) { scanf("%d %d %d", &b[i], &w[i], &k[i]); for (j = 1; j <= k[i]; j++) scanf("%d", &a[j]); scanf("%d", &s), maxflow += w[i]; if (s) w[i] += g; for (j = 1; j <= k[i]; j++) if (b[i]) link(a[j], i + n, (1 << 30)), link(i + n, a[j], 0); else link(i + n, a[j], (1 << 30)), link(a[j], i + n, 0); if (b[i]) link(i + n, T, w[i]), link(T, i + n, 0); else link(S, i + n, w[i]), link(i + n, S, 0); } while (bfs()) maxflow -= dfs(S, (1 << 30)); printf("%d", maxflow); return 0; }
15
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const int maxn = 2009; int a[maxn], b[maxn]; int q[maxn * maxn]; int mm[maxn]; int pos[maxn]; int ql, qr; int cur; int n; struct segtree { int maxx[maxn * 4], pos[maxn * 4]; void build(int o, int L, int R) { if (L == R) { maxx[o] = a[L]; pos[o] = L; return; } int mid = L + R >> 1; maxx[o] = 0; if (mid >= L) { build(o * 2, L, mid); if (maxx[o * 2] > maxx[o]) { maxx[o] = maxx[o * 2]; pos[o] = pos[o * 2]; } } if (mid < R) { build(o * 2 + 1, mid + 1, R); if (maxx[o * 2 + 1] > maxx[o]) { maxx[o] = maxx[o * 2 + 1]; pos[o] = pos[o * 2 + 1]; } } } pair<int, int> query(int o, int L, int R) { pair<int, int> ret = make_pair(0, 0); if (ql <= L && qr >= R) { return make_pair(maxx[o], pos[o]); } int mid = L + R >> 1; if (ql <= mid) { ret = max(ret, query(o * 2, L, mid)); } if (qr > mid) { ret = max(ret, query(o * 2 + 1, mid + 1, R)); } return ret; } void modify(int o, int L, int R) { if (L == R) { maxx[o] = cur; pos[o] = L; return; } int mid = L + R >> 1; if (ql <= mid) { modify(o * 2, L, mid); } if (ql > mid) { modify(o * 2 + 1, mid + 1, R); } if (maxx[o * 2] > maxx[o * 2 + 1]) { maxx[o] = maxx[o * 2]; pos[o] = pos[o * 2]; } else { maxx[o] = maxx[o * 2 + 1]; pos[o] = pos[o * 2 + 1]; } } } t; pair<int, int> ans[maxn * maxn]; int rear = 0; int cnt = 0; void exchange(int i, int j) { cnt += abs(i - j); pos[a[j]] = i; pos[a[i]] = j; swap(a[i], a[j]); ql = i; cur = a[i]; t.modify(1, 1, n); ql = j; cur = a[j]; t.modify(1, 1, n); ans[++rear] = make_pair(i, j); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &b[i]); mm[b[i]] = i; } for (int i = 1; i <= n; ++i) { a[i] = mm[a[i]]; pos[a[i]] = i; } t.build(1, 1, n); for (int i = 1; i <= n; ++i) { int j = pos[i]; while (a[j] != j) { ql = a[j]; qr = j; pair<int, int> tmp = t.query(1, 1, n); exchange(j, tmp.second); j = tmp.second; } } printf("%d\n", cnt); printf("%d\n", rear); for (int i = 1; i <= rear; ++i) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
15
#include <bits/stdc++.h> using namespace std; double EPS = 1e-9; int INF = 0x7FFFFFFF; long long INFF = 0x7FFFFFFFFFFFFFFF; double PI = acos(-1.0); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; inline void scanf(int &d1) { scanf("%d", &d1); } inline void scanf(int &d1, int &d2) { scanf("%d%d", &d1, &d2); } inline void scanf(int &d1, int &d2, int &d3) { scanf("%d%d%d", &d1, &d2, &d3); } inline void scanf(long long &d1) { scanf("%I64d", &d1); } inline void scanf(long long &d1, long long &d2) { scanf("%I64d%I64d", &d1, &d2); } inline void scanf(long long &d1, long long &d2, long long &d3) { scanf("%I64d%I64d%I64d", &d1, &d2, &d3); } inline void scanf(char *temp) { scanf("%s", temp); } inline void printf(int d1) { printf("%d\n", d1); } inline void printf(int d1, int d2) { printf("%d %d\n", d1, d2); } inline void printf(double f1) { printf("%.17g\n", f1); } inline void printf(long long l1) { printf("%I64d\n", l1); } inline void printf(long long l1, long long l2) { printf("%I64d %I64d\n", l1, l2); } inline string tostring(int a) { char x[100]; sprintf(x, "%d", a); string s = x; return s; } inline void toupperstring(string &s) { for (int(i) = (0); (i) < ((int)s.size()); ++(i)) { if (s[i] >= 'a' && s[i] <= 'z') { s[i] = s[i] - 'a' + 'A'; } } } inline void tolowerstring(string &s) { for (int(i) = (0); (i) < ((int)s.size()); ++(i)) { if (s[i] >= 'A' && s[i] <= 'Z') { s[i] = s[i] - 'A' + 'a'; } } } inline int toint(string a) { char x[100]; int res; strcpy(x, a.c_str()); sscanf(x, "%d", &res); return res; } inline void gopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } inline void gcase(int id) { printf("Case #%d:", id); } template <typename T1, typename T2, typename T> inline typename T::iterator findorinsert(T &m, const T1 &t1, const T2 &t2) { typename T::iterator iter = m.find(t1); if (iter == m.end()) { m.insert(make_pair(t1, t2)); return m.find(t1); } else { return iter; } } template <typename T> inline T gcd(T u, T v) { while (v != 0) { T r = u % v; u = v; v = r; } return u; } template <typename T, int n> struct Vector { T data[n]; void read() { for (int(i) = (0); (i) < (n); ++(i)) { cin >> data[i]; } } inline T operator*(const Vector<T, n> &v) const { T res = 0; for (int(i) = (0); (i) < (n); ++(i)) { res += data[i] * v[i]; } return res; } inline bool operator<(const Vector<T, n> &v) const { for (int(i) = (0); (i) < (n); ++(i)) { if (data[i] != v[i]) { return data[i] < v[i]; } } return false; } inline bool operator==(const Vector<T, n> &v) const { for (int(i) = (0); (i) < (n); ++(i)) { if (data[i] != v[i]) { return false; } } return true; } inline T &operator[](int id) { return data[id]; } inline T operator[](int id) const { return data[id]; } inline int size() const { return n; } }; template <typename T> inline Vector<T, 2> make_vector(const T &t1, const T &t2) { Vector<T, 2> res = {t1, t2}; return res; } template <typename T, int n> struct Matrix { Vector<T, n> data[n]; inline Vector<T, n> operator*(const Vector<T, n> &v) const { Vector<T, n> res; for (int(i) = (0); (i) < (n); ++(i)) { res[i] = data[i] * v; } return res; } inline Matrix<T, n> operator*(const Matrix<T, n> &m) const { Matrix<T, n> res; for (int(i) = (0); (i) < (n); ++(i)) { Vector<T, n> temp; for (int(j) = (0); (j) < (n); ++(j)) { temp[j] = m[j][i]; } for (int(j) = (0); (j) < (n); ++(j)) { res[j][i] = data[j] * temp; } } return res; } static inline Matrix<T, n> identity() { Matrix<T, n> res; for (int(i) = (0); (i) < (n); ++(i)) { memset(res[i].data, 0, sizeof(T) * n); } for (int(i) = (0); (i) < (n); ++(i)) { res[i][i] = 1; } return res; } Vector<T, n> &operator[](int id) { return data[id]; } Vector<T, n> operator[](int id) const { return data[id]; } int size() const { return n; } }; template <typename T, int n> class BIT { public: BIT(int size = n) { memset(bit, 0, sizeof(bit)); this->size = size; } BIT(T *data, int size = n) { this->size = size; for (int i = 1; i <= size; i++) { bit[i] = data[i - 1]; for (int j = i - 1; j > i - lowbit(i); j--) { bit[i] += bit[j]; } } } void edit(int i, T delta) { for (int j = i + 1; j <= size; j += lowbit(j)) { bit[j] += delta; } } T sum(int k) { T ans = 0; for (int i = k + 1; i > 0; i -= lowbit(i)) { ans += bit[i]; } return ans; } private: T bit[n + 1]; int size; inline int lowbit(int x) const { return x & (-x); } }; class Prime { public: Prime(int num) { bits.resize(num + 1, true); sieve(); collect(); } bool is_prime(int num) const { return bits[num]; } int find1factor(int num) const { for (int(i) = (0); (i) < ((int)results.size()); ++(i)) { if (num % results[i] == 0) { return results[i]; } } return num; } private: vector<bool> bits; vector<int> results; void sieve() { bits[0] = false; bits[1] = false; for (int i = 2; i < (int)bits.size(); ++i) { if (bits[i] == true) { for (int j = 2;; ++j) { int v2 = j * i; if (v2 >= (int)bits.size()) { break; } bits[v2] = false; } } } } void collect() { for (int i = 0; i < (int)bits.size(); ++i) { if (bits[i]) { results.push_back(i); } } } }; template <int n, int m> struct Graph { int head[n], edge_id; struct { int to, next; } edges[m]; void init(int num = n) { edge_id = 0; memset(head, -1, sizeof(int) * num); } int add(int from, int to) { edges[edge_id].next = head[from]; edges[edge_id].to = to; head[from] = edge_id; edge_id++; return head[from]; } inline int next(int edge) const { return edges[edge].next; } inline int to(int edge) const { return edges[edge].to; } }; template <class T> inline void hash_combine(std::size_t &seed, const T &v) { std::hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } template <typename T, int n> struct VectorHash { inline std::size_t operator()(const Vector<T, n> &v) const { std::size_t seed = 0; for (int(i) = (0); (i) < (n); ++(i)) { hash_combine(seed, v[i]); } return seed; } }; const int max_n = 200000; int a[max_n]; double b[max_n]; int n; double maxSum(double A[]) { double ans = 0, tmp = 0; for (int i = 0; i < n; i++) { tmp += A[i]; if (tmp < 0) tmp = 0; ans = max(ans, tmp); } return ans; } double calc(double x) { for (int i = 0; i < n; i++) b[i] = a[i] - x; double ans1 = maxSum(b); for (int i = 0; i < n; i++) b[i] = -b[i]; double ans2 = maxSum(b); return max(ans1, ans2); } int main() { double vmax = -INF, vmin = INF; scanf(n); for (int(i) = (0); (i) < (n); ++(i)) { scanf(a[i]); vmax = max(vmax, (double)a[i]); vmin = min(vmin, (double)a[i]); } double v1, v2, c1, c2; while (true) { v1 = (2 * vmin + vmax) / 3; v2 = (vmin + 2 * vmax) / 3; c1 = calc(v1); c2 = calc(v2); if (c1 < c2) { vmax = v2; } else { vmin = v1; } if (abs(vmax - vmin) < 1e-11) { break; } } printf(c1); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, mod = 1e9 + 7; int n, x; map<int, int> mp; int f[N], inv[N]; int inverse(int b) { int p = mod - 2, ret = 1; while (p) { if (p & 1) ret = 1ll * ret * b % mod; b = 1ll * b * b % mod; p >>= 1; } return ret; } int choose(int n, int r) { return 1ll * f[n] * inv[r] % mod * inv[n - r] % mod; } int main() { scanf("%d", &n); f[0] = inv[0] = 1; for (int i = 1; i <= n; ++i) { f[i] = 1ll * i * f[i - 1] % mod; inv[i] = inverse(f[i]); } int mx = 0; for (int i = 0; i < n; ++i) { scanf("%d", &x); mx = max(mx, x); ++mp[x]; } vector<pair<int, int>> mpv; map<int, int> prv_cnt; for (auto go : mp) mpv.push_back(go); int cnt = 0; for (auto go : mpv) { prv_cnt[go.first] = cnt; cnt += go.second; } reverse(mpv.begin(), mpv.end()); cnt = 0; int ans = 0; for (auto go : mpv) { if (go.first != mx) { int cur = 1ll * go.second * f[cnt + go.second - 1] % mod; cur = 1ll * cur * f[prv_cnt[go.first]] % mod * choose(n, prv_cnt[go.first]) % mod; cur = 1ll * cur * go.first % mod; ans = (ans + cur) % mod; } cnt += go.second; } printf("%d", ans); return 0; }
15
#include <bits/stdc++.h> using namespace std; long long get_ans(long long n, long long k) { while (true) { if (k % 2 == 0) { return k / 2 + 1; } long long cnt = (k / 2 + 1); k += n - cnt; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; int q; cin >> q; for (int i = 0; i < q; ++i) { long long pos = 0; cin >> pos; --pos; cout << get_ans(n, pos) << endl; } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long inline gcd(long long x, long long y) { return !y ? (long long)abs(x) : gcd(y, x % y); } long long inline lcm(long long a, long long b) { return ((long long)abs((a / gcd(a, b)) * b)); } double inline cartesian_dist(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } long long bigmod(long long b, long long p, long long m) { if (!p) return 1 % m; else if (p & 1) return (((b % m) + m) % m * bigmod(b, p - 1, m)) % m; else { long long ret = bigmod(b, p / 2, m); return (ret * ret) % m; } } long long inverse(long long a, long long m) { return bigmod(a, m - 2, m); } long long extended(long long a, long long m) { long long oldr = a, r = m, oldans = 1, ans = 0, quotient; while (r) { quotient = oldr / r; oldr = oldr - r * quotient; swap(oldr, r); oldans = oldans - ans * quotient; swap(oldans, ans); } return ((oldans % m) + m) % m; } const double PI = acos(-1.0); const double eps = 10E-12; long long MOD = 101LL; int n, q, m, arr[2 * (int)1e5 + 5], qt, l, r, b; vector<pair<int, pair<int, int> > > vec; vector<int> ans; int main() { scanf("%d %d %d", &n, &q, &m); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); while (q--) { scanf("%d %d %d", &qt, &l, &r); vec.push_back(make_pair(qt, make_pair(l, r))); } while (m--) { scanf("%d", &b); for (int j = vec.size() - 1; j >= 0; j--) { if (vec[j].first == 1 && b >= vec[j].second.first && b <= vec[j].second.second) { b -= 1; if (b < vec[j].second.first) b = vec[j].second.second; } else if (vec[j].first == 2 && b >= vec[j].second.first && b <= vec[j].second.second) b = vec[j].second.first + (vec[j].second.second - b); } ans.push_back(arr[b]); } for (int i = 0; i < ans.size(); i++) { if (i) printf(" "); printf("%d", ans[i]); } printf("\n"); return 0; }
10
#include <bits/stdc++.h> using namespace std; vector<int> divisors[100010]; int a[100010]; long long exp(long long a, int k) { long long ans = 1LL; if (a >= 1000000007LL) a %= 1000000007LL; while (k) { if (k & 1) ans *= a, ans %= 1000000007LL; a *= a; a %= 1000000007LL; k >>= 1; } return ans; } int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 2; i < 100010; i++) { for (int j = i; j < 100010; j += i) { divisors[j].push_back(i); } } sort(a, a + n); long long tot = 0LL; for (int l = 1; l <= a[n - 1]; l++) { long long ans = 1LL; int ult = 0; long long k = 1; for (auto &u : divisors[l]) { int p = (int)(lower_bound(a, a + n, u) - a); ans *= exp(k, (p - ult)); ans %= 1000000007LL; ult = p; k++; } ans *= ((exp(k, (n - ult)) - exp(k - 1, (n - ult))) % 1000000007LL) + 1000000007LL; tot = (tot + ans) % 1000000007LL; } cout << tot << "\n"; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int q; long long int a, b, c; cin >> q; for (int i = 0; i < q; i++) { cin >> a >> b >> c; long long int z; z = (a + b + c) / 2; long long int k; k = (a + b + c) % 2; if (k == 0) cout << z << endl; else { long long int m; m = ((a + b + c) - k) / 2; cout << m << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, x, y; void solve() { cin >> n >> x >> y; int z = (y - x); set<int> d; for (int i = 1; i * i <= z; i++) { if (z % i == 0) { d.insert(i); d.insert(z / i); } } int a = 0; int dif = 0; long long int last = 1000000007; for (auto j : d) { for (int i = 1; i <= x; i++) { if ((x - i) % j == 0 and (y - i) % j == 0) { long long int l = (i + (n - 1) * j); if (l < last and l >= y) { a = i; dif = j; last = l; } } } } for (int i = 0; i < n; i++) { cout << a + i * dif << " "; } } int main() { { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }; int t = 1; cin >> t; while (t--) { solve(); cout << "\n"; } return 0; }
4
#include <bits/stdc++.h> int n, k; double point[310][310], area[310]; int main(void) { int i, j, l; double max, y; scanf("%d %d", &n, &k); for (i = 1; i <= n; i++) { for (j = 0; j <= k; j++) scanf("%lf", &point[i][j]); } int T = 2000; for (i = 0; i < k; i++) { for (j = 0; j <= T; j++) { max = 0; for (l = 1; l <= n; l++) { y = point[l][i] * (double)(T - j) + (point[l][i + 1]) * (double)j; y /= T; if (y > max) { if (j == 0 || j == T) area[l] += (y - max) / (2 * T); else area[l] += (y - max) / T; max = y; } } } } for (i = 1; i <= n; i++) printf("%lf\n", area[i]); return false; }
17
#include <bits/stdc++.h> using namespace std; int P[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, }; bool isPrime(int n) { for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; } bool call(int n) { int cnt = 0; for (int i = 0; i < 15; ++i) { if ((n % P[i]) == 0) { cnt++; } } return cnt < 2; } bool test() { for (int i = 2; i < 101; ++i) { if (isPrime(i) != call(i)) { printf("Error %d\n", i); } } } int main() { int hits = 0; string s; for (int i = 0; i < 15; ++i) { cout << P[i] << endl; cout.flush(); cin >> s; if (s == "yes") { hits++; if (hits == 1) { if (P[i] * P[i] <= 100) { cout << P[i] * P[i] << endl; cout.flush(); cin >> s; if (s == "yes") hits++; } } } if (hits >= 2) { cout << "composite" << endl; cout.flush(); return 0; } } cout << "prime" << endl; cout.flush(); }
6
#include <bits/stdc++.h> using namespace std; const int MAXB = 1e7; char buf[MAXB], *cp = buf; inline void rd(int &x) { x = 0; int f = 1; while (*cp < '0' || '9' < *cp) { if (*cp == '-') f = -1; cp++; } while ('0' <= *cp && *cp <= '9') x = (x << 3) + (x << 1) + *cp - 48, cp++; x *= f; } const int MAXN = 200010; int n, m, ans; int f[MAXN], h[MAXN], full[MAXN]; struct info { int a, b, w; inline void get() { rd(a); rd(b); rd(w); } } t[MAXN]; bool operator<(info x, info y) { return x.w > y.w; } void init() { for (int i = 1; i <= n; i++) f[i] = i, h[i] = 1, full[i] = 0; } int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } void uni(int x, int y) { x = find(x); y = find(y); if (x == y) { full[x] = 1; return; } if (h[x] < h[y]) f[y] = x; else if (h[x] > h[y]) f[x] = y; else f[x] = y, h[y]++; } int main() { fread(buf, 1, MAXB, stdin); rd(n); rd(m); for (int i = 0; i < m; i++) t[i].get(); sort(t, t + m); init(); ans = 0; for (int i = 0; i < m; i++) { t[i].a = find(t[i].a); t[i].b = find(t[i].b); if (full[t[i].a] && full[t[i].b]) continue; if (full[t[i].a]) full[t[i].b] = 1; else if (full[t[i].b]) full[t[i].a] = 1; else uni(t[i].a, t[i].b); ans += t[i].w; } printf("%d\n", ans); return 0; }
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; int prime[5 * MAXN + 100]; int p[5 * MAXN + 100][100]; int vis[5 * MAXN + 100]; int vis_times[5 * MAXN + 100]; int val[MAXN * 5 + 100]; void start() { for (int i = 2; i <= 5 * MAXN; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] * i <= 5 * MAXN; j++) { prime[prime[j] * i] = 1; if (i % prime[j] == 0) break; } } for (int i = 1; i <= prime[0]; i++) { int k = prime[i]; for (int j = k; j <= 5 * MAXN; j += k) p[j][++p[j][0]] = k; } } int solve(int n) { int len = p[n][0], ret = 0; for (int i = 1; i <= (1 << len) - 1; i++) { int tmp = 1; int cnt = 0; for (int j = 0; j <= len; j++) if (i & (1 << j)) { tmp *= p[n][j + 1]; cnt++; } if (cnt & 1) ret += vis_times[tmp]; else ret -= vis_times[tmp]; } if (ret) ret--; return ret; } void cnt(int n, int val) { for (int i = 1; i * i <= n; i++) if (n % i == 0) { vis_times[i] += val; if (n / i != i) vis_times[n / i] += val; } } int main() { start(); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", val + i); long long ret = 0, ans = 0; for (int i = 1; i <= m; i++) { int tmp; scanf("%d", &tmp); if (vis[tmp]) { ans -= ret - 1 - solve(val[tmp]); vis[tmp] = 0; cnt(val[tmp], -1); ret--; } else { vis[tmp] = 1; cnt(val[tmp], 1); ret++; ans += ret - 1 - solve(val[tmp]); } printf("%I64d\n", ans); } return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n % 2 == 0) cout << (n / 2) << endl; else cout << -(n / 2 + 1) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int ans = 2 * n; int arr[2 * n]; int c1 = 0; int c2 = 0; for (int i = 0; i < 2 * n; i++) { cin >> arr[i]; if (arr[i] == 1) c1++; else c2++; } if (c1 == c2) { cout << 0 << endl; continue; } if (c1 > c2) { int c = 0; map<int, int> m; m[0] = n; for (int i = n - 1; i >= 0; i--) { if (arr[i] == 1) c++; else c--; if (m.find(c) == m.end()) m[c] = i; if (c == c1 - c2) ans = min(ans, n - i); } c = 0; for (int i = n; i < 2 * n; i++) { if (arr[i] == 1) c++; else c--; if (c > 0 && c <= c1 - c2 && m.find((c1 - c2) - c) != m.end()) ans = min(ans, n - m[(c1 - c2) - c] + i - n + 1); } } else { int c = 0; map<int, int> m; m[0] = n; for (int i = n - 1; i >= 0; i--) { if (arr[i] == 2) c++; else c--; if (m.find(c) == m.end()) m[c] = i; if (c == c2 - c1) ans = min(ans, n - i); } c = 0; for (int i = n; i < 2 * n; i++) { if (arr[i] == 2) c++; else c--; if (c > 0 && c <= c2 - c1 && m.find((c2 - c1) - c) != m.end()) ans = min(ans, n - m[(c2 - c1) - c] + i - n + 1); } } cout << ans << endl; } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int a, s = 0; cin >> a; string c; for (int i = 0; i < a; i++) { cin >> c; if (c[c.size() - 2] == 'p' && c[c.size() - 1] == 'o') { cout << "FILIPINO" << endl; } else if (c[c.size() - 4] == 'd' && c[c.size() - 3] == 'e' && c[c.size() - 2] == 's' && c[c.size() - 1] == 'u' || c[c.size() - 4] == 'm' && c[c.size() - 3] == 'a' && c[c.size() - 2] == 's' && c[c.size() - 1] == 'u') { cout << "JAPANESE" << endl; } else if (c[c.size() - 5] == 'm' && c[c.size() - 4] == 'n' && c[c.size() - 3] == 'i' && c[c.size() - 2] == 'd' && c[c.size() - 1] == 'a') { cout << "KOREAN" << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double sn = 1e-6; int n; map<int, long long> mpp; map<long long, int> dp; map<long long, bool> vis; int mex(set<int> &s) { int c = 0; while (s.find(c) != s.end()) c++; return c; } int grundy(long long v) { if (v == 0) return 0; if (vis[v]) return dp[v]; set<int> s; for (int i = 1; (1 << (i - 1)) <= v; i++) { long long v2 = 0; if (i != 1) v2 = ((1 << (i - 1)) - 1) & v; s.insert(grundy((v >> i) | v2)); } vis[v] = true; return dp[v] = mex(s); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int v; scanf("%d", &v); for (long long j = 2; j * j <= v; j++) { int c = 0; while (v % j == 0) { v /= j; c++; } if (c != 0) { mpp[j] |= (1 << (c - 1)); } } if (v != 1) mpp[v] |= 1; } int xs = 0; for (auto v : mpp) { xs ^= grundy(v.second); } if (xs == 0) printf("Arpa\n"); else printf("Mojtaba\n"); return 0; }
14
#include <bits/stdc++.h> using namespace std; string a[210], b[210]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= n; ++i) { if (b[i] == "rat") cout << a[i] << endl; } for (int i = 1; i <= n; ++i) { if (b[i] == "woman" || b[i] == "child") cout << a[i] << endl; } for (int i = 1; i <= n; ++i) { if (b[i] == "man") cout << a[i] << endl; } for (int i = 1; i <= n; ++i) { if (b[i] == "captain") cout << a[i] << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int a[n], b[n]; long long sumo = 0, sume = 0; for (i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) { sumo = sumo + a[i]; b[i] = sumo; } else { sume = sume + a[i]; b[i] = sume; } } int p = 0, ans = 0, q, r, s; i = 0; if (i == 0) { p = sumo - a[i]; if (p == sume) ans++; } for (i = 1; i < n; i++) { if (i % 2 == 0) { p = b[i] - a[i]; q = b[i - 1]; r = sumo - p + q - a[i]; s = sume + p - q; if (s == r) ans++; } else { p = b[i] - a[i]; q = b[i - 1]; r = sume - p + q - a[i]; s = sumo + p - q; if (s == r) ans++; } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; const int N = 105; char str[N][N]; int a[N], vis[N], len; char ans[N]; bool match(int i) { int l = strlen(str[i]); if (l != len) return 0; for (int j = 0; j < len; j++) if (ans[j] != '?' && str[i][j] != ans[j]) return 0; return 1; } int main() { int m, n; while (~scanf("%d%d", &n, &m)) { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) scanf("%s", str[i]); for (int i = 0; i < m; i++) { scanf("%d", &a[i]); vis[a[i]] = 1; } bool flag = true; len = strlen(str[a[0]]); for (int i = 1; i < m; i++) if (len != strlen(str[a[i]])) flag = false; for (int i = 0; i < len; i++) ans[i] = str[a[0]][i]; ans[len] = 0; for (int i = 1; i < m; i++) for (int j = 0; j < len; j++) if (str[a[i]][j] != ans[j]) ans[j] = '?'; for (int i = 1; i <= n; i++) if (!vis[i]) { if (match(i)) flag = false; } if (flag) { cout << "Yes" << endl; cout << ans << endl; } else cout << "No" << endl; } }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; int const N = 5 * 1000 * 1000 + 16; int const M = 1000 * 1000 * 1000 + 7; int t, l, r; int p[N]; ll dp[N]; int main() { cin.tie(0); cin.sync_with_stdio(0); cin >> t >> l >> r; ll inf = 9e18; fill_n(dp + 2, r, inf); for (int i = 2; i <= r; ++i) { dp[i] = min(dp[i], i * (i - 1ll) >> 1); for (int j = i + i, z = 2; j <= r && z <= i; j += i, ++z) { ll v = (z * (z - 1ll) >> 1) * i + dp[i]; dp[j] = min(dp[j], v); } } ll p = 1; ll ans = 0; for (int i = l; i <= r; ++i) { ans += (dp[i] % M) * p % M; ans %= M; p *= t; p %= M; } cout << ans; }
10
#include <bits/stdc++.h> using namespace std; char a[10001]; int main() { cin >> a; int ans = 0; for (int i = 0; i < strlen(a); i++) { if (a[i] == 'A') ans += 1; else if (a[i] == '1') ans += 10; else ans += a[i] - '0'; } cout << ans; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { string second; cin >> second; bool u = 0, l = 0, dig = 0; if (second.size() < 5) return cout << "Too weak", 0; for (int i = 0; i < second.size(); i++) { if (islower(second[i])) l = 1; if (isupper(second[i])) u = 1; if (isdigit(second[i])) dig = 1; } cout << (u and l and dig ? "Correct" : "Too weak"); }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T; cin >> T; while (T--) { long long n; cin >> n; long long pos[n + 1]; long long a[n]; long long i, j, k, l; for (i = 0; i < n; i++) { cin >> a[i]; pos[a[i]] = i; } long long mx = 0; for (i = 1; i <= n; i++) { long long t = mx; mx = max(pos[i], mx); if (pos[i] >= t && pos[i] > i - 1) { for (j = pos[i]; j > t; j--) { if (a[j - 1] > a[j]) { swap(pos[a[j]], pos[a[j - 1]]); swap(a[j], a[j - 1]); } } } } for (auto &x : a) { cout << x << " "; } cout << "\n"; } }
6
#include <bits/stdc++.h> using namespace std; int main() { string s; char mas[2][2], mas1[2][2], Prob[2][2]; cin >> s; mas[0][0] = s[0]; mas[0][1] = s[1]; cin >> s; mas[1][0] = s[0]; mas[1][1] = s[1]; cin >> s; mas1[0][0] = s[0]; mas1[0][1] = s[1]; cin >> s; mas1[1][0] = s[0]; mas1[1][1] = s[1]; Prob[0][0] = mas[0][0]; Prob[0][1] = mas[0][1]; Prob[1][0] = mas[1][0]; Prob[1][1] = mas[1][1]; for (int i = 0; i < 100; i++) { if (Prob[0][0] == 'X') { swap(Prob[0][0], Prob[0][1]); } else if (Prob[0][1] == 'X') { swap(Prob[0][1], Prob[1][1]); } else if (Prob[1][0] == 'X') { swap(Prob[1][0], Prob[0][0]); } else if (Prob[1][1] == 'X') { swap(Prob[1][0], Prob[1][1]); } if (Prob[0][0] == mas1[0][0] && Prob[0][1] == mas1[0][1] && Prob[1][1] == mas1[1][1] && Prob[1][0] == mas1[1][0]) { cout << "YES"; return 0; } } Prob[0][0] = mas[0][0]; Prob[0][1] = mas[0][1]; Prob[1][0] = mas[1][0]; Prob[1][1] = mas[1][1]; for (int i = 0; i < 100; i++) { if (Prob[0][0] == 'X') { swap(Prob[0][0], Prob[1][0]); } else if (Prob[0][1] == 'X') { swap(Prob[0][1], Prob[0][0]); } else if (Prob[1][0] == 'X') { swap(Prob[1][0], Prob[1][1]); } else if (Prob[1][1] == 'X') { swap(Prob[0][1], Prob[1][1]); } if (Prob[0][0] == mas1[0][0] && Prob[0][1] == mas1[0][1] && Prob[1][1] == mas1[1][1] && Prob[1][0] == mas1[1][0]) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<string> strs(n); for (int i = 0; i < n; i++) { cin >> strs[i]; } map<int, int> counters; set<int> cont; vector<int> contV; int specNuls = 0; for (int i = 0; i < n; i++) { string curr = strs[i]; int q = 0; bool neg = false; for (int j = 0; j < curr.size(); j++) { if (curr[j] == '(') { q++; } else { q--; } if (q < 0) { neg = true; } } if (q >= 0 && neg) { continue; } if (q < 0 && curr[curr.size() - 1] == '(') { continue; } if (q > 0 && curr[0] == ')') { continue; } bool llll = false; if (q < 0) { int m = 0; for (int l = curr.size() - 1; l >= 0; l--) { if (curr[l] == '(') { m++; } else { m--; } if (m > 0) { llll = true; break; } } } if (llll) { continue; } if (cont.count(-q) == 0 && cont.count(q) == 0) { cont.insert(q); contV.push_back(q); } counters[q]++; } int answ = 0; for (int i = 0; i < contV.size(); i++) { int num = contV[i]; if (num != 0) { if (counters.count(num) != 0 && counters.count(-num) != 0) { answ += min(counters[num], counters[-num]); } } else { answ += counters[0] / 2; } } cout << answ << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; map<int, int> count_; int n, a[100007], sum; int main() { sum = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } long long res = 0; int temp = 0; for (int i = 1; i < n; i++) { temp += a[i]; if (temp % 2 == 0 && temp / 2 == sum - temp) res += count_[temp / 2]; count_[temp]++; } cout << res << endl; }
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (auto &x : a) cin >> x; vector<int> even, odd; for (int i = 0; i < n; i++) { if (i % 2 == 0) { even.push_back(a[i]); } else { odd.push_back(a[i]); } } sort(even.begin(), even.end()); sort(odd.begin(), odd.end()); vector<int> b(n); for (int i = 0; i < n; i++) { if (i % 2 == 0) { b[i] = even[i / 2]; } else { b[i] = odd[i / 2]; } } cout << (is_sorted(b.begin(), b.end()) ? "YES" : "NO") << '\n'; } return 0; }
7
#include <bits/stdc++.h> int main() { long long i, n; scanf("%I64d", &n); if (n >= 0) { printf("%I64d", n); } else { if ((n - (n % 10)) / 10 > ((n - (n % 100)) / 10) + (n % 10)) { printf("%I64d", ((n - n % 10) / 10)); } else { printf("%I64d", ((n - n % 100) / 10) + (n % 10)); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; void edit(vector<int> &skill, int n) { for (int i = 0; i < n; i++) { int x; cin >> x; skill.push_back(x); } } void achilles() { int n, l, r, x; cin >> n >> l >> r >> x; vector<int> diff; int ans = 0; edit(diff, n); sort(diff.begin(), diff.end()); for (int i = 1; i < 1 << n; i++) { vector<int> rating; int sm = 0; for (int k = 0; k < n; k++) { if (i & (1 << k)) { rating.push_back(diff[k]); sm += diff[k]; } } auto mx = *max_element(rating.begin(), rating.end()); auto mn = *min_element(rating.begin(), rating.end()); if (mx - mn >= x) { if (sm >= l && sm <= r) { ans++; } } } cout << ans << "\n"; } int main() { ios ::sync_with_stdio(0); cin.tie(0); achilles(); return 0; }
6
#include<bits/stdc++.h> using namespace std; #define ll long long ll pwr(ll a, ll b){ ll res=1; while(b>0){ if(b&1) res=res*a; a*=a; b>>=1; } return res; } int main() { ll t; cin>>t; while(t--) { ll n,m; cin>>n; ll r[n]; for(int i=0;i<n;i++) cin>>r[i]; cin>>m; ll b[m]; for(int i=0;i<m;i++) cin>>b[i]; ll x=0,y=0; ll ans[n+m]; for(int i=0;i<n+m;i++) { if(x<n&&y<m) { if(r[x]>b[y]) { ans[i]=r[x]; x++; } else { ans[i]=b[y]; y++; } } else if(x==n) { ans[i]=b[y]; y++; } else { ans[i]=r[x]; x++; } } //for(int i=0;i<n+m;i++) //cout<<ans[i]<<" "; ll maxi=0,sum=0; for(int i=0;i<n+m;i++) { sum+=ans[i]; maxi=max(maxi,sum); } //cout<<maxi<<endl; ll a=0,bm=0; sum=0; for(int i=0;i<n;i++) { sum+=r[i]; a=max(a,sum); } sum=0; for(int i=0;i<m;i++) { sum+=b[i]; bm=max(bm,sum); } if(a>bm) a=max(a,bm+a); else { a=max(a+bm,bm);} a=max(maxi,a); cout<<a<<endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int primes[17] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59}; int N, A[110], dp[110][1 << 17], s[60]; const int Field = 0x1FFFF; int main() { scanf("%d", &N); int i; for (i = 0; i <= N - 1; i++) scanf("%d", &A[i]); for (i = 1; i <= 59; i++) s[i] = 0; for (i = 0; i <= 16; i++) for (int j = primes[i]; j < 60; j += primes[i]) s[j] |= 1 << i; int j, k; for (i = 0; i <= N; i++) for (j = 0; j <= Field; j++) dp[i][j] = 0x23333333; dp[0][0] = 0; for (i = 0; i <= N - 1; i++) for (k = 1; k <= 59; k++) for (int f = j = (Field ^ s[k]);; j = (j - 1) & f) { dp[i + 1][j ^ s[k]] = min(dp[i + 1][j ^ s[k]], dp[i][j] + abs(A[i] - k)); if (j == 0) break; } vector<int> RET; RET.clear(); int ans = 0x23333333; int lv, u = 0; for (i = 0; i <= Field; i++) if (ans > dp[N][i]) { ans = dp[N][i]; u = i; } for (lv = N - 1; lv >= 0; lv--) for (k = 1; k <= 59; k++) if (((u & s[k]) == s[k]) && (dp[lv][u ^ s[k]] + abs(k - A[lv]) == dp[lv + 1][u])) { RET.push_back(k); u ^= s[k]; break; } for (i = 1; i <= N; i++) printf("%d%s", RET[N - i], (i == N) ? "\n" : " "); }
12
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; double f[MAXN], p[MAXN], ans, lst = 1, g; inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x; } inline double calc(int x) { return (f[x] + (1 - f[x]) * p[x]) / f[x]; } inline double calc2(int x) { return f[x] + (1 - f[x]) * p[x]; } int main() { int n = read(); for (int i = 1; i <= n; i++) p[i] = f[i] = read() / 100.0; for (int i = 1; i <= n; i++) lst *= p[i]; ans = lst * n; for (int i = n + 1; i <= 300000; i++) { int tmp = 1; for (int j = 1; j <= n; j++) if (calc(j) > calc(tmp)) tmp = j; g = lst * calc(tmp), f[tmp] = calc2(tmp), ans += (g - lst) * i, lst = g; } printf("%.8lf\n", ans); return 0; }
19
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:3336777216") using namespace std; int IT_MAX = 1 << 19; const long long MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double ERR = 1e-10; int bit[26][100050]; void update(int x, int p, int v) { for (; p <= 100000; p += p & (-p)) bit[x][p] ^= v; } int getsum(int x, int p) { int rv = 0; for (; p; p -= p & (-p)) rv ^= bit[x][p]; return rv; } char in[100050]; int nxt[100050][26]; int prv[100050][26]; int dpl[100050][26]; int dpr[100050][26]; vector<pair<int, int> > Vl; bool mycmp(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) return a.first > b.first; return a.second < b.second; } vector<int> Vp[26]; bool grchk[30]; int getgr(int S, int E) { memset(grchk, 0, sizeof(grchk)); int i; for (i = 0; i < 26; i++) { if (nxt[S][i] > E) continue; int p1 = nxt[S][i]; int p2 = prv[E][i]; int gr = dpr[S][i] ^ dpl[E][i]; if (p1 < p2) { gr ^= getsum(i, p2 - 1); gr ^= getsum(i, p1 - 1); } grchk[gr] = true; } for (i = 0;; i++) if (!grchk[i]) return i; } int DEBUG = 0; int main() { if (!DEBUG) scanf("%s", in + 1); else { srand(132132); for (int i = 1; i <= 100000; i++) in[i] = rand() % 26 + 'a'; } int N = strlen(in + 1), i, j; for (i = 1; i <= N; i++) { for (j = 0; j < 26; j++) prv[i][j] = prv[i - 1][j]; prv[i][in[i] - 'a'] = i; for (j = 0; j < 26; j++) { if (in[i] - 'a' == j) continue; if (prv[i][j] == 0) continue; Vl.emplace_back(prv[i][j] + 1, i); } } for (i = 0; i < 26; i++) nxt[N + 1][i] = N + 1; for (i = N; i >= 1; i--) { for (j = 0; j < 26; j++) nxt[i][j] = nxt[i + 1][j]; nxt[i][in[i] - 'a'] = i; for (j = 0; j < 26; j++) { if (in[i] - 'a' == j) continue; if (nxt[i][j] == N + 1) continue; Vl.emplace_back(i, nxt[i][j] - 1); } } for (i = 1; i <= N; i++) Vp[in[i] - 'a'].push_back(i); for (i = 0; i < 26; i++) { for (j = 0; j + 1 < Vp[i].size(); j++) { int t1 = Vp[i][j], t2 = Vp[i][j + 1]; if (t1 + 1 != t2) Vl.emplace_back(t1 + 1, t2 - 1); } } sort((Vl).begin(), (Vl).end(), mycmp); Vl.erase(unique((Vl).begin(), (Vl).end()), Vl.end()); int cnt = 0; for (auto it : Vl) { cnt++; memset(grchk, 0, sizeof(grchk)); int S = it.first, E = it.second; i = getgr(S, E); if (S != 1 && E != N && in[S - 1] == in[E + 1]) update(in[S - 1] - 'a', S - 1, i); if (E != N && nxt[S][in[E + 1] - 'a'] == E + 1) dpr[S][in[E + 1] - 'a'] = i; if (S != 1 && prv[E][in[S - 1] - 'a'] == S - 1) dpl[E][in[S - 1] - 'a'] = i; } int Q; if (!DEBUG) scanf("%d", &Q); else Q = 100000; while (Q--) { int S, E; if (!DEBUG) scanf("%d %d", &S, &E); else { S = rand() % 100000 + 1; E = rand() % 100000 + 1; if (S > E) swap(S, E); } if (!getgr(S, E) && !DEBUG) printf("Bob\n"); else if (!DEBUG) printf("Alice\n"); } return 0; }
24
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, pos = -1, min = 1000000001, max = -1; bool flag = false; cin >> n; for (int i = 1; i <= n; i++) { cin >> a >> b; if (a <= min && b >= max) { pos = i; min = a; max = b; flag = true; } else if (a < min) { flag = false; min = a; } else if (b > max) { flag = false; max = b; } } if (flag) cout << pos << endl; else cout << "-1" << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, s2, s3; cin >> s >> s2 >> s3; if (s[1] == '<') swap(s[0], s[2]); if (s2[1] == '<') swap(s2[0], s2[2]); if (s3[1] == '<') swap(s3[0], s3[2]); if (s[0] == s2[0]) cout << s3[2] << s3[0] << s[0]; else if (s[0] == s3[0]) cout << s2[2] << s2[0] << s[0]; else if (s2[0] == s3[0]) cout << s[2] << s[0] << s2[0]; else cout << "Impossible"; }
4
#include <bits/stdc++.h> using namespace std; int find_mx1(string s) { int cnt = 0; vector<int> temp; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } return *max_element(temp.begin(), temp.end()); } int find_mx2(string s) { int cnt = 0; vector<int> temp; for (int x = 1; x <= 2; x++) for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } return *max_element(temp.begin(), temp.end()); } int find_mn1(string s) { int cnt = 0; vector<int> temp; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } return *min_element(temp.begin(), temp.end()); } int find_mn2(string s) { int cnt = 0; vector<int> temp; for (int x = 1; x <= 2; x++) for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } return *min_element(temp.begin(), temp.end()); } int main() { int t; cin >> t; while (t--) { int n, x; cin >> n >> x; string s; cin >> s; int mx1 = find_mx1(s); int mn1 = find_mn1(s); int mx2 = find_mx2(s); int mn2 = find_mn2(s); if (mx2 > mx1) { if (x < mn1) { if (x == 0) cout << "1" << endl; else cout << "0" << endl; } else { int gap = mx2 - mx1; vector<int> temp; int cnt = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } int ans = 0; for (int i = 0; i < temp.size(); i++) { if ((x - temp[i]) % gap == 0 && (x - temp[i]) / gap >= 0) ans++; } if (x == 0) ans++; cout << ans << endl; } } else { if (mn1 == mn2) { if (x >= mn1 && x <= mx1) cout << "-1" << endl; else { if (x == 0) cout << "1" << endl; else cout << "0" << endl; } } else if (mn2 < mn1) { if (x > mx1) { if (x == 0) cout << "1" << endl; else cout << "0" << endl; } else { int gap = mn2 - mn1; int cnt = 0; vector<int> temp; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cnt++; else cnt--; temp.push_back(cnt); } int ans = 0; for (int i = 0; i < temp.size(); i++) { if ((x - temp[i]) % gap == 0 && (x - temp[i]) / gap >= 0) ans++; } if (x == 0) ans++; cout << ans << endl; } } } } }
9
#include <bits/stdc++.h> using namespace std; int n, m, flag; int dx[5] = {1, -1, 0, 0}, dy[5] = {0, 0, 1, -1}; char s[1505][1505]; int vis[1505][1505][3], a[1505][1505]; void dfs(int x, int y, int px, int py) { if (flag) return; if (vis[x][y][0] && (vis[x][y][1] != px || vis[x][y][2] != py)) { flag = 1; return; } vis[x][y][1] = px, vis[x][y][2] = py, vis[x][y][0] = 1; for (int i = 0; i <= 3; i++) { int x0 = (x + dx[i] + n) % n, y0 = (y + dy[i] + m) % m; int px0 = px + dx[i], py0 = py + dy[i]; if (!a[x0][y0]) { if (vis[x0][y0][1] != px0 || vis[x0][y0][2] != py0 || !vis[x0][y0][0]) dfs(x0, y0, px0, py0); } } } int main() { int x, y; while (scanf("%d%d", &n, &m) == 2) { flag = 0; memset(a, 0, sizeof(a)); memset(vis, 0, sizeof(vis)); for (int i = 0; i <= n - 1; i++) scanf("%s", s[i]); for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= m - 1; j++) { if (s[i][j] == '#') a[i][j] = 1; else if (s[i][j] == 'S') { x = i; y = j; } } } dfs(x, y, x, y); if (flag) puts("Yes"); else puts("No"); } }
12
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); for (int q = 1; q <= t; q++) { long long int n, k; cin >> n >> k; bool fl = true; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { n += i; fl = false; break; } } if (fl) n += n; n += (k - 1) * 2; cout << n << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class t> inline t read(t &x) { char c = getchar(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } int n, m, ans, k; signed main() { read(n, m, k); for (int i = k, x = n - 1, y = m - 1; i; i--, x -= 4, y -= 4) ans += x + y << 1; write(ans); puts(""); }
0
#include <bits/stdc++.h> using namespace std; const int maxn(100005); int n, ans, mx, len[maxn]; string s[maxn]; int main() { int i, j, c, cnt, lmx, rmx, nmx; ios ::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; ++i) cin >> s[i], len[i] = s[i].size(); for (c = 0; c < 26; ++c) { mx = cnt = 0; for (i = 0; i < len[1]; ++i) if (s[1][i] - 'a' == c) ++cnt; else mx = max(mx, cnt), cnt = 0; mx = max(mx, cnt); for (i = 2; i <= n; ++i) { lmx = rmx = 0; for (j = 0; j < len[i]; ++j) { if (s[i][j] - 'a' != c) break; ++lmx; } for (j = len[i] - 1; ~j; --j) { if (s[i][j] - 'a' != c) break; ++rmx; } nmx = cnt = 0; for (j = 0; j < len[i]; ++j) if (s[i][j] - 'a' == c) ++cnt; else nmx = max(nmx, cnt), cnt = 0; if (lmx == len[i]) mx = len[i] + mx * len[i] + mx; else mx = mx ? lmx + rmx + 1 : max(lmx, rmx); mx = max(mx, nmx); } ans = max(ans, mx); } cout << ans << endl; return 0; }
15
#include <bits/stdc++.h> template <typename T> inline void rd(T& x) { int si = 1; char c = getchar(); x = 0; while (!isdigit(c)) si = c == '-' ? -1 : si, c = getchar(); while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); x *= si; } template <typename T, typename... Args> inline void rd(T& x, Args&... args) { rd(x); rd(args...); } const int kN = 3e3 + 5; const long long kMod = 998244353; long long QPow(long long a, long long b) { long long ans = 1, bas = a; for (; b; b >>= 1, bas = bas * bas % kMod) if (b & 1) ans = ans * bas % kMod; return ans; } char s[kN], t[kN]; int n, m; long long f[kN][kN]; int main() { int T = 1; while (T--) { scanf("%s%s", s + 1, t + 1); n = strlen(s + 1); m = strlen(t + 1); for (int i = 1; i <= n; ++i) f[i][i] = 2 * (i <= m ? s[1] == t[i] : 1); for (int len = 1; len < n; ++len) for (int i = 1; i + len - 1 <= n; ++i) { int j = i + len - 1, x = len + 1; if (j < n && (j + 1 > m || s[x] == t[j + 1])) f[i][j + 1] = (f[i][j + 1] + f[i][j]) % kMod; if (i > 1 && (i - 1 > m || s[x] == t[i - 1])) f[i - 1][j] = (f[i - 1][j] + f[i][j]) % kMod; } long long ans = 0; for (int i = m; i <= n; ++i) ans = (ans + f[1][i]) % kMod; printf("%lld", ans); } return 0; }
14
#include <bits/stdc++.h> int main() { char s[100]; int l = 0, i, j, max = 0, li = 0; scanf("%s\n", s); while (s[l] != '\0') { l++; } for (i = 0; i < l; i++) { if (s[i] != 'A' && s[i] != 'E' && s[i] != 'I' && s[i] != 'O' && s[i] != 'U' && s[i] != 'Y') { s[i] = '1'; } } for (i = 0; i < l; i++) { if (s[i] == '1') { int c = 0; int ind = i; while (s[ind] == '1' && ind < l) { ind++; c++; } if (c >= max) max = c; } } for (j = 0; j < l; j++) { if (s[j] == 'A' || s[j] == 'E' || s[j] == 'I' || s[j] == 'O' || s[j] == 'U' || s[j] == 'Y') { if (j > li) { li = j; } } } max++; int temp2 = (l - li); if (temp2 >= max) max = temp2; printf("%d", max); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1000010; const int mod = (int)1e9 + 7; int a[N]; int stk[N]; int prv[N], nxt[N]; int add(int a, int b) { return (a + b) % mod; } int sub(int a, int b) { return (a - b + mod) % mod; } int mul(int a, int b) { return (long long)a * b % mod; } int fit(int len, int k) { long long times = (long long)len / (k - 1); long long ret = times * len - (k - 1) * times * (times + 1) / 2; ret %= mod; return ret; } int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", a + i); } { int ptr = 0; for (int i = 0; i < n; i++) { while (ptr - 1 >= 0 && a[stk[ptr - 1]] <= a[i]) { ptr--; } if (ptr == 0) { prv[i] = 0; } else { prv[i] = stk[ptr - 1] + 1; } stk[ptr++] = i; } } { int ptr = 0; for (int i = n - 1; i >= 0; i--) { while (ptr - 1 >= 0 && a[stk[ptr - 1]] < a[i]) { ptr--; } if (ptr == 0) { nxt[i] = n - 1; } else { nxt[i] = stk[ptr - 1] - 1; } stk[ptr++] = i; } } int ans = 0; for (int i = 0; i < n; i++) { int sum = sub(fit(nxt[i] - prv[i] + 1, k), add(fit(i - prv[i], k), fit(nxt[i] - i, k))); ans = add(ans, mul(a[i], sum)); } printf("%d\n", ans); return 0; }
17
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define N 5005 int n, ans[N]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); int lst = 0, res = 0; for (int i = 2; i <= n; i++) { printf("? 1 %d\n", i - 1); printf("%d\n", i); for (int j = 1; j < i; j++) printf("%d%c", j, j == i - 1 ? 10 : 32); fflush(stdout); scanf("%d", &res); if (res) { lst = i; break; } } int l = 1, r = lst - 1; while (l < r) { int mid = (l + r) >> 1; printf("? 1 %d\n", mid - l + 1); printf("%d\n", lst); for (int j = l; j <= mid; j++) printf("%d%c", j, j == mid ? 10 : 32); fflush(stdout); scanf("%d", &res); if (res) r = mid; else l = mid + 1; } int sum = 0; for (int i = 1; i < lst; i++) if (i != l) ans[++sum] = i; for (int i = lst + 1; i <= n; i++) { printf("? 1 1\n"); printf("%d\n%d\n", lst, i); fflush(stdout); scanf("%d", &res); if (!res) ans[++sum] = i; } printf("! %d", sum); for (int i = 1; i <= sum; i++) printf(" %d", ans[i]); putchar(10); fflush(stdout); } }
19
#include <bits/stdc++.h> FILE *_stderr = freopen("nul", "w", stderr); using std::pair; using std::vector; template <class T> inline vector<T> &operator<<(vector<T> &x, const T &y) { x.push_back(y); return x; } using std::map; using std::set; template <class T> inline set<T> &operator<<(set<T> &x, const T &y) { x.insert(y); return x; } using std::list; template <class T> inline list<T> &operator<<(list<T> &x, const T &y) { x.push_back(y); return x; } template <class T> inline list<T> &operator>>(list<T> &x, const T &y) { x.push_front(y); return x; } inline void read(int &x) { char c = getchar(); while (c < '0') c = getchar(); x = 0; while (c >= '0') x = (x << 3) + (x << 1) + c - '0', c = getchar(); } inline void read(long long &x) { char c = getchar(); while (c < '0') c = getchar(); x = 0; while (c >= '0') x = (x << 3) + (x << 1) + c - '0', c = getchar(); } inline void read(double &x) { scanf("%lf", &x); } inline void read(long double &x) { double tmp; scanf("%lf", &tmp); x = tmp; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 1000000000) goto x9; else if (x >= 100000000) goto x8; else if (x >= 10000000) goto x7; else if (x >= 1000000) goto x6; else if (x >= 100000) goto x5; else if (x >= 10000) goto x4; else if (x >= 1000) goto x3; else if (x >= 100) goto x2; else if (x >= 10) goto x1; else goto x0; x9: putchar(x / 1000000000 + '0'); x %= 1000000000; x8: putchar(x / 100000000 + '0'); x %= 100000000; x7: putchar(x / 10000000 + '0'); x %= 10000000; x6: putchar(x / 1000000 + '0'); x %= 1000000; x5: putchar(x / 100000 + '0'); x %= 100000; x4: putchar(x / 10000 + '0'); x %= 10000; x3: putchar(x / 1000 + '0'); x %= 1000; x2: putchar(x / 100 + '0'); x %= 100; x1: putchar(x / 10 + '0'); x %= 10; x0: putchar(x + '0'); } inline void write(const long long x) { printf("%I64d", x); } template <class T> inline T min(const T x, const T y) { if (x < y) return x; else return y; } template <class T> inline T &min2(const T &x, const T &y) { if (x < y) return x; else return y; } template <class T> inline T max(const T x, const T y) { if (x > y) return x; else return y; } template <class T> inline T &max2(const T &x, const T &y) { if (x > y) return x; else return y; } template <class T> inline T sqr(const T x) { return x * x; } template <class T> inline T sqr2(const T &x) { return x * x; } int n, w; int first[300086], second[300086], lv[300086]; struct s { int c, x, y; }; inline bool operator<(const s &first, const s &second) { if (first.c != second.c) return first.c < second.c; if (first.x != second.x) return first.x < second.x; return first.y < second.y; } std::set<s> S; int main() { read(n); read(w); for (int i = (1); i <= (n); ++i) { read(first[i]), read(second[i]); second[i] -= first[i]; if (first[i] >= second[i]) { S << (s){first[i] + second[i], i, 2}; } else { S << (s){first[i] + first[i], i, 1}; S << (s){second[i] + second[i], i, 1}; } } memset(lv, 0, sizeof(lv)); int ma2 = -1, ma2i = -1; int cw = 0; long long tans = 0; while (cw < w) { int cc = S.begin()->c; std::set<s>::iterator til = S.lower_bound((s){cc, 400000, 400000}); int ccw = 0; for (std::set<s>::iterator i = S.begin(); i != til; ++i) ccw += i->y; if (cw + ccw <= w) { cw += ccw; for (std::set<s>::iterator i = S.begin(); i != til; ++i) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; if (i->y == 2) { int ca2 = second[i->x]; if (ca2 > ma2) ma2 = ca2, ma2i = i->x; } else { int ca2 = i->c / 2LL; if (ca2 > ma2) ma2 = ca2, ma2i = i->x; } } S.erase(S.begin(), til); } else if ((w - cw) % 2) { bool succ = false; for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->y == 1) { succ = true; break; } if (succ) { if (cw + 1 < w) for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->y == 2) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw + 1 == w) break; } if (cw < w) for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->y == 1) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw == w) break; } } else { int mm = 1000000009, mi; for (int i = (1); i <= (n); ++i) { if (lv[i] == 0 && first[i] < mm) mm = first[i], mi = i; if (lv[i] == 1 && second[i] < mm) mm = second[i], mi = i; } if (mm < cc - ma2) { ++lv[mi]; tans += (long long)mm; ++cw; if (cw < w) for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->x != mi) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw == w) break; } } else { --lv[ma2i]; tans -= (long long)ma2; --cw; if (cw < w) for (std::set<s>::iterator i = S.begin(); i != til; ++i) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw == w) break; } } } } else { for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->y == 2) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw == w) break; } if (cw < w) { for (std::set<s>::iterator i = S.begin(); i != til; ++i) if (i->y == 1) { lv[i->x] += i->y; tans += (long long)cc * (long long)i->y / 2LL; cw += i->y; if (cw == w) break; } } } } printf("%I64d\n", tans); for (int i = (1); i <= (n); ++i) printf("%d", lv[i]); return 0; }
18
#include <bits/stdc++.h> template <typename T> inline T max(T a, T b) { return a > b ? a : b; } template <typename T> inline T min(T a, T b) { return a < b ? a : b; } template <typename T> inline T abs(T a) { return a > 0 ? a : -a; } template <typename T> inline bool repr(T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool repl(T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T gcd(T a, T b) { T t; if (a < b) { while (a) { t = a; a = b % a; b = t; } return b; } else { while (b) { t = b; b = a % b; a = t; } return a; } } template <typename T> inline T sqr(T x) { return x * x; } struct Cg { inline char operator()() { return getchar(); } }; struct Cp { inline void operator()(char x) { putchar(x); } }; inline bool IS(char x) { return x == 10 || x == 13 || x == ' '; } template <typename T> struct Fr { T P; inline Fr &operator,(int &x) { x = 0; char t = P(); while ((t < '0' || t > '9') && t != '-') t = P(); bool f = 0; if (t == '-') t = P(), f = 1; x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; if (f) x = -x; return *this; } inline operator int() { int x; *this, x; return x; } inline Fr &operator,(long long &x) { x = 0; char t = P(); while ((t < '0' || t > '9') && t != '-') t = P(); bool f = 0; if (t == '-') t = P(), f = 1; x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; if (f) x = -x; return *this; } inline operator long long() { long long x; *this, x; return x; } inline Fr &operator,(char &x) { for (x = P(); IS(x); x = P()) ; return *this; } inline operator char() { char x; *this, x; return x; } inline Fr &operator,(char *x) { char t = P(); for (; IS(t); t = P()) ; if (~t) { for (; !IS(t) && ~t; t = P()) *x++ = t; } *x++ = 0; return *this; } inline Fr &operator,(double &x) { x = 0; char t = P(); while ((t < '0' || t > '9') && t != '-') t = P(); bool f = 0; if (t == '-') t = P(), f = 1; x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; if (t == '.') { double u = 0.1; for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0'); } if (f) x = -x; return *this; } inline operator double() { double x; *this, x; return x; } inline Fr &operator,(long double &x) { x = 0; char t = P(); while ((t < '0' || t > '9') && t != '-') t = P(); bool f = 0; if (t == '-') t = P(), f = 1; x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; if (t == '.') { double u = 0.1; for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0'); } if (f) x = -x; return *this; } inline operator long double() { long double x; *this, x; return x; } inline Fr &operator,(unsigned int &x) { x = 0; char t = P(); while (t < '0' || t > '9') t = P(); x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; return *this; } inline operator unsigned int() { unsigned int x; *this, x; return x; } inline Fr &operator,(unsigned long long &x) { x = 0; char t = P(); while (t < '0' || t > '9') t = P(); x = t - '0'; for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0'; return *this; } inline operator unsigned long long() { unsigned long long x; *this, x; return x; } }; Fr<Cg> in; template <typename T> struct Fw { T P; inline Fw &operator,(int x) { if (x) { if (x < 0) P('-'), x = -x; char s[10], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator()(int x) { if (x) { if (x < 0) P('-'), x = -x; char s[10], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator,(unsigned int x) { if (x) { char s[10], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator()(unsigned int x) { if (x) { char s[10], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator,(long long x) { if (x) { if (x < 0) P('-'), x = -x; char s[19], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator()(long long x) { if (x) { if (x < 0) P('-'), x = -x; char s[19], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator,(unsigned long long x) { if (x) { char s[20], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator()(unsigned long long x) { if (x) { char s[20], c = 0; while (x) s[c++] = x % 10 + '0', x /= 10; while (c--) P(s[c]); } else P('0'); return *this; } inline Fw &operator,(char x) { P(x); return *this; } inline Fw &operator()(char x) { P(x); return *this; } inline Fw &operator,(const char *x) { while (*x) P(*x++); return *this; } inline Fw &operator()(const char *x) { while (*x) P(*x++); return *this; } inline Fw &operator()(double x, int y) { if (y) { double t = 0.5; for (int i = y; i--;) t *= 0.1; if (x >= 0) x += t; else x -= t, P('-'); *this, (long long)(abs(x)); P('.'); if (x < 0) x = -x; while (y--) { x *= 10; x -= floor(x * 0.1) * 10; P(((int)x) % 10 + '0'); } } else if (x >= 0) *this, (long long)(x + 0.5); else *this, (long long)(x - 0.5); ; return *this; } inline Fw &operator()(long double x, int y) { if (y) { double t = 0.5; for (int i = y; i--;) t *= 0.1; if (x >= 0) x += t; else x -= t, P('-'); *this, (long long)(abs(x)); P('.'); if (x < 0) x = -x; while (y--) { x *= 10; x -= floor(x * 0.1) * 10; P(((int)x) % 10 + '0'); } } else if (x >= 0) *this, (long long)(x + 0.5); else *this, (long long)(x - 0.5); ; return *this; } }; Fw<Cp> out; inline void p(int x, int y) { out, x, ' ', y, '\n'; } int main() { int n = in; if (n % 4 > 1) { out, "NO\n"; return 0; } out, "YES\n"; for (; n > 3; n -= 4) { for (int i = 1, iend = n - 4; i <= iend; i++) p(i, n - 3); for (int i = n - 3; i; i--) p(i, n - 2); for (int i = 1, iend = n - 4; i <= iend; i++) p(i, n - 1); p(n - 1, n); for (int i = n - 4; i; i--) p(i, n); p(n - 3, n - 1); p(n - 2, n); p(n - 3, n); p(n - 2, n - 1); } }
23
#include <bits/stdc++.h> using namespace std; const long long N = 3e3; long long q; int main() { cin >> q; for (long long j = 0; j < q; ++j) { long long n, r; cin >> n >> r; --r; long long sum = 0; for (long long i = 0; i < n - 1; ++i) { long long x; cin >> x; --x; sum += x; } long long ans = (n - sum % n + r) % n; cout << ans + 1 << "\n"; } return 0; }
16
#include <bits/stdc++.h> using namespace std; typedef pair <int, int> pii; typedef pair <pii, int> ppi; const int N = 100050; const int mod = 998244353; int f[N], invf[N]; int c(int n, int k) { return f[n] * 1ll * invf[k] % mod * invf[n - k] % mod; } void solve() { int n; scanf("%d", &n); string s; cin >> s; int z = count(s.begin(), s.end(), '0'); int on = 0; for (int i = 0; i + 1 < s.size(); ++i) { if (s[i] == '1' && s[i + 1] == '1') { on++; i++; } } printf("%d\n", c(on + z, on)); } int main() { #ifdef FLOWMASTER freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif f[0] = 1; for (int i = 1; i <= 1e5; ++i) f[i] = f[i - 1] * 1ll * i % mod; invf[0] = invf[1] = 1; for (int i = 2; i <= 1e5; ++i) invf[i] = (mod - invf[mod % i] * 1ll * (mod / i) % mod) % mod; for (int i = 2; i <= 1e5; ++i) invf[i] = invf[i - 1] * 1ll * invf[i] % mod; int t; scanf("%d", &t); for (int i = 0; i < t; ++i) solve(); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int N, i, j; cin >> N; vector<long long> A(N + 1), F(N + 1), G(N + 1), Pre(N + 1, 0); for (i = 1; i <= N; ++i) { cin >> A[i]; Pre[i] = Pre[i - 1] + A[i]; } long long L = Pre[N]; F[N] = Pre[N]; for (i = N - 1; i >= 2; --i) { F[i] = Pre[i] - L; L = max(L, F[i]); } L = -1e18; for (i = N; i >= 2; --i) L = max(F[i], L); cout << L; return 0; }
14
#include <bits/stdc++.h> using namespace std; long long n, m, t[200007], h[200007], a[800007], b[800007], c[800007], d[800007]; void BT(long long x, long long l, long long r) { if (l == r) { a[x] = 2 * h[l] + t[l]; b[x] = 2 * h[l]; c[x] = t[l]; d[x] = 0; return; } long long m1 = (l + r) / 2; BT(x * 2 + 1, l, m1); BT(x * 2 + 2, m1 + 1, r); a[x] = max(a[x * 2 + 2], a[x * 2 + 1] + c[x * 2 + 2]); b[x] = max(b[x * 2 + 1], b[x * 2 + 2] + c[x * 2 + 1]); c[x] = c[x * 2 + 1] + c[x * 2 + 2]; d[x] = max(a[x * 2 + 1] + b[x * 2 + 2], max(d[x * 2 + 1], d[x * 2 + 2])); return; } pair<pair<long long, long long>, pair<long long, long long> > S( long long x, long long l, long long r, long long tl, long long tr) { if (r < tl || tr < l) { return {{0, 0}, {0, 0}}; } if (tl <= l && r <= tr) { return {{a[x], b[x]}, {c[x], d[x]}}; } long long m1 = (l + r) / 2; pair<pair<long long, long long>, pair<long long, long long> > r1 = S(x * 2 + 1, l, m1, tl, tr); pair<pair<long long, long long>, pair<long long, long long> > l1 = S(x * 2 + 2, m1 + 1, r, tl, tr); if (r1.first.first + r1.first.second + r1.second.first + r1.second.second == 0) { return l1; } else if (l1.first.first + l1.first.second + l1.second.first + l1.second.second == 0) return r1; long long a1 = max(l1.first.first, r1.first.first + l1.second.first); long long b1 = max(r1.first.second, l1.first.second + r1.second.first); long long c1 = r1.second.first + l1.second.first; long long d1 = max(r1.first.first + l1.first.second, max(r1.second.second, l1.second.second)); return {{a1, b1}, {c1, d1}}; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> t[i]; t[i + n] = t[i]; } for (int i = 1; i <= n; i++) { cin >> h[i]; h[i + n] = h[i]; } BT(0, 1, n * 2); for (int i = 1; i <= m; i++) { long long l, r; cin >> l >> r; if (l <= r) { cout << S(0, 1, n * 2, r + 1, l + n - 1).second.second << endl; } else { cout << S(0, 1, n * 2, r + 1, l - 1).second.second << endl; } } return 0; }
15
#include <bits/stdc++.h> using namespace std; long long nb, ns, nc, pb, ps, pc; long long cnt[3] = {0}; long long r; bool can(long long m) { long long cb = pb * (max(m * cnt[0] - nb, 1LL * 0)); long long cs = ps * (max(m * cnt[1] - ns, 1LL * 0)); long long cc = pc * (max(m * cnt[2] - nc, 1LL * 0)); if (r >= cb + cs + cc) return true; else return false; } int main() { string s; cin >> s; cin >> nb >> ns >> nc >> pb >> ps >> pc >> r; for (int i = 0; i < s.size(); ++i) if (s[i] == 'B') ++cnt[0]; else if (s[i] == 'S') ++cnt[1]; else ++cnt[2]; long long ans = 0; long long high = 1e12 + 100, low = 0, mid; while (high >= low) { mid = (high + low) / 2; if (can(mid)) { ans = max(ans, mid); low = mid + 1; } else high = mid - 1; } cout << ans; }
8
#include <bits/stdc++.h> using namespace std; const int __ = 5e5 + 5; struct DisjointSetUnion { int pre[__]; DisjointSetUnion() { clear(); } void un(int x, int y) { x = fd(x), y = fd(y); if (x == y) return; if (pre[x] > pre[y]) swap(x, y); pre[x] += pre[y], pre[y] = x; } int fd(int x) { if (pre[x] < 0) return x; return pre[x] = fd(pre[x]); } void clear() { memset(pre, -1, sizeof(pre)); } } dsu; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = (1); i <= (m); ++i) { int k, x; scanf("%d", &k); if (k) scanf("%d", &x); for (int i = (2); i <= (k); ++i) { int y; scanf("%d", &y); dsu.un(x, y); } } for (int i = (1); i <= (n); ++i) printf("%d ", -dsu.pre[dsu.fd(i)]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int i, j; double y1, y2, yw, x, y, r; scanf("%lf%lf%lf%lf%lf%lf", &y1, &y2, &yw, &x, &y, &r); if (y2 - y1 <= 2 * r) { puts("-1"); return 0; } yw -= r; y1 += r; double x0 = (yw - y1) * x / (2 * yw - y1 - y); double d2 = x0 * x0 * (y2 - y1) * (y2 - y1) / (x0 * x0 + (yw - y1) * (yw - y1)); if (d2 < r * r) puts("-1"); else printf("%.10f\n", x0); return 0; }
12
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pi; typedef pair<double, double> pd; typedef vector<ll> vi; typedef vector<vector<ll>> vvi; typedef vector<vector<pi>> vvpi; typedef vector<pi> vpi; ll INF = 2e18; double eps = 1e-12; #define forn(i, e) for (ll i = 0; i < e; i++) #define forsn(i, s, e) for (ll i = s; i < e; i++) #define rforn(i, s) for (ll i = s; i >= 0; i--) #define rforsn(i, s, e) for (ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) void fast_cin() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void read(vi &x) { for (ll i = 0; i < sz(x); i++) { cin >> x[i]; } return; } void read(vvi &x) { for (ll i = 0; i < sz(x); i++) { for (ll j = 0; j < sz(x[i]); j++) { cin >> x[i][j]; } } return; } void solve() { ll n, q, k; cin >> n >> q >> k; vi a(n); read(a); vi pre(n), post(n), mid(n); forn(i, n) { if (i == 0) { pre[i] = k - 1; post[i] = a[i + 1] - 2; continue; } else if (i == n - 1) { pre[i] = k - a[i - 1] - 1; post[i] = k - 1; continue; } pre[i] = k - a[i - 1] - 1; post[i] = a[i + 1] - 2; mid[i] = a[i + 1] - a[i - 1] - 2; } for (ll i = 2; i < n - 1; i++) { mid[i] += mid[i - 1]; } // forn(i,n) // { // dbg(pre[i]); // dbg(post[i]); // dbg(mid[i]); // } ll l, r; while (q--) { cin >> l >> r; if (l == r) { cout << k - 1 << ln; continue; } ll tmp = (l + 1 < r) ? (mid[r - 2] - mid[l - 1]) : 0; ll ans = post[l - 1] + pre[r - 1] + tmp; cout << ans << ln; } return; } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif fast_cin(); ll t = 1; // cin >> t; for (int it = 1; it <= t; it++) { // cout << "Case #" << it + 1 << ": "; solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long X, K; long long fastmod(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long tmp = (fastmod(a, b / 2)) % MOD; tmp = (tmp * tmp) % MOD; if ((b % 2) != 0) return (tmp * a) % MOD; return tmp; } int main() { cin >> X >> K; if (X == 0) { cout << "0\n"; return 0; } long long two_power_k = fastmod(2, K); X = X % MOD; long long aaa = ((((two_power_k * two_power_k) % MOD) * X) % MOD) - ((((two_power_k * (two_power_k - 1 + MOD)) % MOD) * fastmod(2, MOD - 2)) % MOD); if (aaa < 0) aaa += MOD; aaa *= 2; aaa = aaa % MOD; aaa = (aaa * fastmod(two_power_k, MOD - 2)) % MOD; cout << aaa << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; long long n, x, MAX = 0; bool used[2050]; vector<vector<long long> > s; long long dfs(long long i, long long len) { used[i] = 1; MAX = max(MAX, len); long long ans = len; for (int j = 0; j < s[i].size(); j++) ans = dfs(s[i][j], len + 1); return ans; } int main() { ios::sync_with_stdio(0); cin >> n; s.resize(n + 5); for (int i = 1; i <= n; i++) { cin >> x; if (x != -1) s[x].push_back(i); } for (int i = 1; i <= n; i++) { long long len = 1; if (used[i] != 1) dfs(i, len); } cout << MAX; }
1
#include <bits/stdc++.h> using namespace std; int main() { set<int> s; vector<int> v; int a, b, m, r0, K, j = 0; scanf("%d %d %d %d", &a, &b, &m, &r0); while (true) { K = (a * r0 + b) % m; if (s.find(K) != s.end()) { for (int i = 0;; i++) { if (v[i] == K) { printf("%d\n", j - i); return 0; } } } v.push_back(K); s.insert(K); r0 = K; j++; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, a[155][6], dem[155], cost[155], tong[155]; void nhap() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= 5; j++) { cin >> a[i][j]; if (a[i][j] != -1) dem[j]++; } } void init() {} void solve() { for (int them = 0; them <= 31 * n + 2; them++) { for (int j = 1; j < 6; j++) { int total = n + them, cur = dem[j]; if (a[1][j] != -1 && a[2][j] != -1 && a[1][j] > a[2][j]) cur += them; cost[j] = 500; while (cost[j] < 3000 && 2 * cur <= total) cur *= 2, cost[j] += 500; } for (int i = 1; i < 3; i++) { tong[i] = 0; for (int j = 1; j < 6; j++) if (a[i][j] != -1) tong[i] += cost[j] / 250 * (250 - a[i][j]); } if (tong[1] > tong[2]) { printf("%d\n", them); return; } } { cout << "-1\n"; return; } } int main() { nhap(); solve(); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; struct Sofa { int x[2], y[2]; int l, r, t, b; } a[N]; int cntX[N], cntY[N]; int d, n, m; int cntL, cntR, cntT, cntB; int main() { ios::sync_with_stdio(false); cin >> d; cin >> m >> n; for (int i = 1; i <= d; ++i) { cin >> a[i].y[0] >> a[i].x[0] >> a[i].y[1] >> a[i].x[1]; if (a[i].x[0] > a[i].x[1]) swap(a[i].x[0], a[i].x[1]); if (a[i].y[0] > a[i].y[1]) swap(a[i].y[0], a[i].y[1]); cntX[a[i].x[0]]++; cntY[a[i].y[0]]++; } cin >> cntL >> cntR >> cntT >> cntB; for (int i = 1; i <= n; ++i) cntX[i] += cntX[i - 1]; for (int i = 1; i <= m; ++i) cntY[i] += cntY[i - 1]; for (int i = 1; i <= d; ++i) { a[i].t = cntX[a[i].x[1] - 1]; if (a[i].x[0] == a[i].x[1] - 1) a[i].t--; a[i].l = cntY[a[i].y[1] - 1]; if (a[i].y[0] == a[i].y[1] - 1) a[i].l--; } memset(cntX, 0, sizeof(cntX)); memset(cntY, 0, sizeof(cntY)); for (int i = 1; i <= d; ++i) { cntX[a[i].x[1]]++; cntY[a[i].y[1]]++; } for (int i = n; i >= 1; --i) cntX[i] += cntX[i + 1]; for (int i = m; i >= 1; --i) cntY[i] += cntY[i + 1]; for (int i = 1; i <= d; ++i) { a[i].b = cntX[a[i].x[0] + 1]; if (a[i].x[1] == a[i].x[0] + 1) a[i].b--; a[i].r = cntY[a[i].y[0] + 1]; if (a[i].y[1] == a[i].y[0] + 1) a[i].r--; } int ans = -1; for (int i = 1; i <= d; ++i) { if (a[i].l == cntL && a[i].r == cntR && a[i].t == cntT && a[i].b == cntB) { ans = i; break; } } cout << ans << '\n'; return 0; }
12
#include <bits/stdc++.h> using namespace std; using vi = vector<long long>; template <typename T> std::istream& operator>>(std::istream& input, std::pair<T, T>& data) { input >> data.first >> data.second; return input; } template <typename T> std::istream& operator>>(std::istream& input, std::vector<T>& data) { for (T& first : data) input >> first; return input; } template <typename T> std::ostream& operator<<(std::ostream& output, const pair<T, T>& data) { output << "(" << data.first << "," << data.second << ")"; return output; } template <typename T> std::ostream& operator<<(std::ostream& output, const std::vector<T>& data) { for (const T& first : data) output << first << " "; return output; } std::ostream& operator<<(std::ostream& output, const __int128& first) { __int128 n = first; if (n == 0) { output << "0"; return output; } if (n < 0) { n = -n; output << "-"; } string s; while (n) { s += '0' + (n % 10); n /= 10; } reverse(s.begin(), s.end()); cout << s; return output; } long long div_up(long long a, long long b) { return a / b + ((a ^ b) > 0 && a % b); } long long div_down(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); } long long math_mod(long long a, long long b) { return a - b * div_down(a, b); } template <class T> using V = vector<T>; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } long long gcd(long long a, long long b) { while (b) { tie(a, b) = make_pair(b, a % b); } return a; } long long Bit(long long mask, long long bit) { return (mask >> bit) & 1; } const long long N = 23, INF = 1e18; string greed[N]; struct Chel { long long first, second, t; }; V<pair<long long, long long> > v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; struct Dinic { struct edge { long long to, flow, cap; }; const static long long N = 23 * 23 * 4; vector<edge> e; vector<long long> g[N + 7]; long long dp[N + 7]; long long ptr[N + 7]; void clear() { for (long long i = 0; i < N + 7; i++) g[i].clear(); e.clear(); } void addEdge(long long a, long long b, long long cap) { g[a].push_back(e.size()); e.push_back({b, 0, cap}); g[b].push_back(e.size()); e.push_back({a, 0, 0}); } long long minFlow, start, finish; bool bfs() { for (long long i = 0; i < N; i++) dp[i] = -1; dp[start] = 0; vector<long long> st; long long uk = 0; st.push_back(start); while (uk < st.size()) { long long v = st[uk++]; for (long long to : g[v]) { auto ed = e[to]; if (ed.cap - ed.flow >= minFlow && dp[ed.to] == -1) { dp[ed.to] = dp[v] + 1; st.push_back(ed.to); } } } return dp[finish] != -1; } long long dfs(long long v, long long flow) { if (v == finish) return flow; for (; ptr[v] < g[v].size(); ptr[v]++) { long long to = g[v][ptr[v]]; edge ed = e[to]; if (ed.cap - ed.flow >= minFlow && dp[ed.to] == dp[v] + 1) { long long add = dfs(ed.to, min(flow, ed.cap - ed.flow)); if (add) { e[to].flow += add; e[to ^ 1].flow -= add; return add; } } } return 0; } long long dinic(long long start, long long finish) { Dinic::start = start; Dinic::finish = finish; long long flow = 0; for (minFlow = (1 << 30); minFlow; minFlow >>= 1) { while (bfs()) { for (long long i = 0; i < N; i++) ptr[i] = 0; while (long long now = dfs(start, (long long)2e9 + 7)) flow += now; } } return flow; } } dinic; long long n, m; void bfs(long long first, long long second, long long dist[N][N]) { for (long long i = (0); i < (N); ++i) { for (long long j = (0); j < (N); ++j) { dist[i][j] = INF; } } if (greed[first][second] == '#') { return; } queue<pair<long long, long long> > q; q.push(make_pair(first, second)); dist[first][second] = 0; while (q.size()) { auto p = q.front(); q.pop(); long long i = p.first, j = p.second; for (auto& e : v) { long long first = i + e.first, second = j + e.second; if (0 <= first && first < n && 0 <= second && second < m && greed[first][second] == '.') { if (ckmin(dist[first][second], dist[i][j] + 1)) { q.push(make_pair(first, second)); } } } } } long long dist[N][N][N][N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long males, females; cin >> n >> m >> males >> females; for (long long i = (0); i < (n); ++i) { cin >> greed[i]; } long long first, second, t; cin >> first >> second >> t; first--; second--; if (abs(males - females) != 1) { cout << -1 << '\n'; exit(0); } V<Chel> a(males), b(females); for (long long i = (0); i < (males); ++i) { cin >> a[i].first >> a[i].second >> a[i].t; a[i].first--; a[i].second--; } for (long long i = (0); i < (females); ++i) { cin >> b[i].first >> b[i].second >> b[i].t; b[i].first--; b[i].second--; } if (males > females) { b.push_back({first, second, t}); } else { a.push_back({first, second, t}); } for (long long i = (0); i < (n); ++i) { for (long long j = (0); j < (m); ++j) { bfs(i, j, dist[i][j]); } } assert(a.size() == b.size()); long long k = a.size(); auto num = [&](long long first, long long second) { return first * m + second; }; auto check = [&](long long Tim) { dinic.clear(); const long long S = dinic.N - 2; const long long T = dinic.N - 1; for (long long i = (0); i < (k); ++i) { dinic.addEdge(S, i, 1); for (long long first = (0); first < (n); ++first) { for (long long second = (0); second < (m); ++second) { long long d = dist[a[i].first][a[i].second][first][second]; if (d != INF) { if (d * a[i].t <= Tim) { dinic.addEdge(i, 2 * k + num(first, second), 1); } } } } } for (long long first = (0); first < (n); ++first) { for (long long second = (0); second < (m); ++second) { if (greed[first][second] == '.') { dinic.addEdge(2 * k + num(first, second), 2 * k + n * m + num(first, second), 1); } } } for (long long i = (0); i < (k); ++i) { dinic.addEdge(i + k, T, 1); for (long long first = (0); first < (n); ++first) { for (long long second = (0); second < (m); ++second) { long long d = dist[b[i].first][b[i].second][first][second]; if (d != INF) { if (d * b[i].t <= Tim) { dinic.addEdge(2 * k + n * m + num(first, second), i + k, 1); } } } } } return dinic.dinic(S, T) == k; }; long long l = -1, r = INF; if (!check(r)) { cout << -1 << '\n'; exit(0); } while (l < r - 1) { long long m = (l + r) / 2; if (check(m)) { r = m; } else { l = m; } } cout << r << '\n'; }
19
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, f, e, k, p, ans = 0, rs = 0; scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f); k = min(min(b, c), d); ans += f * k; p = d - k; p = min(a, p); ans += e * p; k = min(a, d); rs += k * e; p = d - k; p = min(min(b, c), p); rs += f * p; printf("%d", max(ans, rs)); return 0; }
0
#include <bits/stdc++.h> using namespace std; void print(int n, int p) { int x = 2 * n + p; for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { printf("%d %d\n", i, j); x--; if (x == 0) break; } if (x == 0) break; } } int t; int main() { scanf("%d", &t); for (int i = 0; i < t; ++i) { int n, p; scanf("%d%d", &n, &p); print(n, p); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int ri() { int x; scanf("%d", &x); return x; } const int N = 100000, NN = 262144; const int64_t NINF = -1e15; int d[N * 2], h[N * 2]; int64_t L[N * 2], R[N * 2]; struct P { int64_t L, R, S; P operator+(const P &o) const { return {max(L, o.L), max(R, o.R), max(max(S, o.S), L + o.R)}; } } tr[NN * 2]; P getMax(int l, int r) { P x{NINF, NINF, NINF}, y{NINF, NINF, NINF}; for (l += NN - 1, r += NN - 1; l < r; l /= 2, r /= 2) { if (l % 2 == 0) x = x + tr[l++]; if (r % 2 == 0) y = tr[--r] + y; } return x + y; } int main() { int n = ri(), m = ri(); for (int i = (0); i < (n); i++) d[i] = d[n + i] = ri(); for (int i = (0); i < (n); i++) h[i] = h[n + i] = ri(); int64_t s = 0; for (int i = (0); i < (2 * n); i++) { L[i] = 2 * h[i] - s; R[i] = 2 * h[i] + s; s += d[i]; } for (int i = (0); i < (NN); i++) tr[NN - 1 + i] = i < 2 * n ? (P){L[i], R[i], NINF} : (P){NINF, NINF, NINF}; for (int i = (NN - 1); --i >= (0);) tr[i] = tr[2 * i + 1] + tr[2 * i + 2]; for (int i = (0); i < (m); i++) { int x = ri() - 1, y = ri() - 1; if (x <= y) x += n; printf("%" PRId64 "\n", getMax(y + 1, x).S); } }
15
#include <bits/stdc++.h> using namespace std; int n, d, a[500400], t[500400]; bool u[500400]; int q[500400], st = 0, fi = 0; void add(int x) { for (int i = 500400 - 1; i >= x; --i) u[i] |= u[i - x]; } void read(void) { cin >> n >> d; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); } void kill(void) { u[0] = 1; int m = 0, s = 0; while (m < n && s + d >= a[m]) { s += a[m]; add(a[m]); m++; } t[s] = 0; q[fi++] = s; for (int i = s - 1; i >= 0; --i) if (u[i]) { while (q[st] > i + d) st++; t[i] = t[q[st]] + 1; q[fi++] = i; } cout << s << " " << t[0] << "\n"; } int main() { ios_base::sync_with_stdio(0); read(); kill(); return 0; }
14
#include <bits/stdc++.h> using namespace std; long long bin_pow(int a, int b) { long long d = a; long long res = 1; while (b > 0) { if (b & 1) { res = res * d; } d = d * d; b = b / 2; } return res; } int main() { int n; cin >> n; int t = 2 * n - 2; int p = (t - n + 1); int extra = p - 2; long long res; int k = t - n; if (k == 1) { res = 6; } else { long long resmid = bin_pow(4, k - 2); long long resend = bin_pow(4, k - 1); resmid = resmid * extra * 9; resend = resend * 2 * 3; res = resmid + resend; } cout << res * 4 << endl; }
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; long long best = 8e18; for (int m = 1; m <= 18; ++m) { for (int x = 0; x <= 9; ++x) { int sum = 0; for (int i = 0; i <= k; ++i) sum += (x + i) % 10; if (x + k <= 9) { int rest = n - sum; if (rest >= 0 && (rest % (k + 1) == 0) && (rest / (k + 1) <= 9 * (m - 1))) { if (m == 1 || rest >= k + 1) { vector<int> ans; ans.push_back(x); int digleft = m - 1; rest /= (k + 1); while (digleft > 1 && rest >= 10) ans.push_back(9), digleft--, rest -= 9; if (digleft > 1) ans.push_back(rest - 1), digleft--; while (digleft > 1) ans.push_back(0), digleft--; if (digleft) ans.push_back(rest); long long cur = 0ll; for (int i = 0; i < ans.size(); ++i) cur *= 10, cur += ans[ans.size() - 1 - i]; best = min(best, cur); } } } else for (int s = 1; s <= m - 1; ++s) { for (int y = 0; y <= 8; ++y) { int rest = n - ((10 - x) * (9 * (s - 1) + y) + (k + 1 - 10 + x) * (y + 1) + sum); if (rest >= 0 && (rest % (k + 1) == 0) && (rest / (k + 1) <= 9 * (m - s - 1))) { if (s == m - 1 || rest >= k + 1) { vector<int> ans; ans.push_back(x); for (int i = 0; i < s - 1; ++i) ans.push_back(9); ans.push_back(y); int digleft = m - s - 1; rest /= (k + 1); while (digleft > 1 && rest >= 10) ans.push_back(9), digleft--, rest -= 9; if (digleft > 1) ans.push_back(rest - 1), digleft--; while (digleft > 1) ans.push_back(0), digleft--; if (digleft) ans.push_back(rest); long long cur = 0ll; for (int i = 0; i < ans.size(); ++i) cur *= 10, cur += ans[ans.size() - 1 - i]; best = min(best, cur); } } } } } } if (best == 8e18) cout << -1 << endl; else cout << best << endl; } }
14
#include <bits/stdc++.h> using namespace std; long long a, b, s, sum; int main() { cin >> a >> b >> s; sum = abs(a) + abs(b); s -= sum; if (s >= 0 && s % 2 == 0) cout << "Yes"; else cout << "No"; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<long long> a(n); multiset<long long> k; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<long long> y(31); y[0] = 1; for (int i = 1; i < 30; i++) y[i] = (y[i - 1] << 1) + 1; for (int i = 0; i < n; i++) { int x; cin >> x; k.insert(x); } for (int i = 0; i < n; i++) { long long cur = 0; for (int l = 29; l > 0; l--) { cur |= (a[i] & ((long long)1 << l)); long long lf = cur, r = cur; if (l != 0) r |= y[l - 1]; lf--; auto it1 = k.upper_bound(lf); auto it2 = k.upper_bound(r); if (it1 == it2) cur ^= (1 << l); } auto it = k.upper_bound(cur - 1); auto it2 = k.upper_bound(cur); cur = *it; long long b = a[i] ^ cur; if ((it2 != k.end()) && (b > (a[i] ^ (*it2)))) { a[i] = a[i] ^ (*it2); k.erase(it2); } else { a[i] = b; k.erase(it); } } for (int i = 0; i < n; i++) cout << a[i] << " "; return 0; }
10
#include <bits/stdc++.h> using namespace std; int sq(int z) { return z * z; } int INF = 1000 * 1000 * 1000 + 91; int ox, oy, n; int xs[27], ys[27]; int d[27][27]; int len[1 << 24], from[1 << 24]; int main() { cin >> ox >> oy >> n; for (int i = 0; i < n; i++) cin >> xs[i] >> ys[i]; xs[n] = ox, ys[n] = oy; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) d[i][j] = sq(xs[i] - xs[j]) + sq(ys[i] - ys[j]); for (int i = 0; i < (1 << 24); i++) len[i] = INF; len[0] = 0; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if (!(i & (1 << j))) { int newm = i | (1 << j); int newd = len[i] + d[n][j] + d[j][n]; if (newd < len[newm]) { len[newm] = newd; from[newm] = i; } for (int h = j + 1; h < n; h++) { if (!(newm & (1 << h))) { int modernm = newm | (1 << h); int modernd = len[i] + d[n][j] + d[j][h] + d[h][n]; if (modernd < len[modernm]) { len[modernm] = modernd; from[modernm] = i; } } } break; } } } int cm = (1 << n) - 1; cout << len[cm] << endl; for (; cm > 0; cm = from[cm]) { cout << 0 << ' '; for (int i = 0; i < n; i++) if ((from[cm] ^ cm) & (1 << i)) cout << i + 1 << ' '; } cout << 0 << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; int n, r; int cnt, total; int simul(int a, int b) { if (b == 0) return a; else { if (a > b) { if (a % b == 0) { cnt += a / b - 2, total += a / b; return simul(b, 0); } else { cnt += a / b - 1, total += a / b; return simul(a % b, b); } } else { if (b % a == 0) { cnt += b / a - 1, total += b / a; return simul(a, 0); } else { cnt += b / a - 1, total += b / a; return simul(a, b % a); } } } } stack<char> S; void print(int a, int b) { if (b == 0) return; else { if (a > b) { if (a % b == 0) { int cnt = a / b - 1; while (cnt--) S.push('B'); S.push('T'); return print(b, 0); } else { int cnt = a / b; while (cnt--) S.push('B'); return print(a % b, b); } } else { if (b % a == 0) { int cnt = b / a; while (cnt--) S.push('T'); print(a, 0); } else { int cnt = b / a; while (cnt--) S.push('T'); print(a, b % a); } } } } int main() { scanf("%d%d", &n, &r); if (r == 1) { if (n > 1) printf("IMPOSSIBLE\n"); else printf("%d\n", 0), printf("T\n"); } else { int ans = INT_MAX, opt = -1, z; for (int other = (1); other <= (r - 1); ++other) { cnt = 0, total = 0; int d = simul(other, r); if (d == 1 && total == n) { if (cnt < ans) { ans = cnt, opt = other, z = 1; } } cnt = 0, total = 0; d = simul(r, other); if (d == 1 && total == n) { if (cnt < ans) { ans = cnt, opt = other, z = 0; } } } if (ans == INT_MAX) printf("IMPOSSIBLE\n"); else { printf("%d\n", ans); if (z == 1) print(opt, r); else print(r, opt); while (!S.empty()) { printf("%c", S.top()); S.pop(); } printf("\n"); } } return 0; }
13
#include <bits/stdc++.h> using namespace std; typedef unsigned int UI; typedef long int LI; typedef unsigned long int ULI; typedef long long int LL; typedef unsigned long long int ULL; LL mod = 1e9 + 7; inline int scanInt() { int n = 0; char ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { n = (n << 1) + (n << 3) + (int)(ch - '0'); ch = getchar(); } return n * sign; } inline LL scanLong() { LL n = 0; char ch = getchar(); LL sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { n = (n << 1) + (n << 3) + (LL)(ch - '0'); ch = getchar(); } return n * sign; } vector<pair<LL, LL> > getRotations(pair<LL, LL> pos, pair<LL, LL> home) { vector<pair<LL, LL> > ans; ans.push_back(pos); LL dx = pos.first - home.first, dy = pos.second - home.second; ans.push_back(make_pair(home.first - dy, home.second + dx)); ans.push_back(make_pair(home.first - dx, home.second - dy)); ans.push_back(make_pair(home.first + dy, home.second - dx)); return ans; } LL dist(pair<LL, LL> a, pair<LL, LL> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } bool isSquare(vector<pair<LL, LL> > sq) { vector<LL> distances; for (__typeof(sq.size()) i = (0) - ((0) > (sq.size())); i != (sq.size()) - ((0) > (sq.size())); i += 1 - 2 * ((0) > (sq.size()))) { for (__typeof(sq.size()) j = (i + 1) - ((i + 1) > (sq.size())); j != (sq.size()) - ((i + 1) > (sq.size())); j += 1 - 2 * ((i + 1) > (sq.size()))) { distances.push_back(dist(sq[i], sq[j])); } } sort(distances.begin(), distances.end()); if (set<LL>(distances.begin(), distances.end()).size() != 2) return false; LL min = distances.front(), max = distances.back(); if (2 * min != max) return false; return true; } int main() { LL n = scanLong(); ; for (__typeof(n) x = (0) - ((0) > (n)); x != (n) - ((0) > (n)); x += 1 - 2 * ((0) > (n))) { vector<pair<LL, LL> > rotations[4]; for (__typeof(4) i = (0) - ((0) > (4)); i != (4) - ((0) > (4)); i += 1 - 2 * ((0) > (4))) { pair<LL, LL> pos, home; pos.first = scanLong(); pos.second = scanLong(); home.first = scanLong(); home.second = scanLong(); rotations[i] = getRotations(pos, home); } LL ans = LONG_MAX; for (__typeof(4) i = (0) - ((0) > (4)); i != (4) - ((0) > (4)); i += 1 - 2 * ((0) > (4))) { for (__typeof(4) j = (0) - ((0) > (4)); j != (4) - ((0) > (4)); j += 1 - 2 * ((0) > (4))) { for (__typeof(4) k = (0) - ((0) > (4)); k != (4) - ((0) > (4)); k += 1 - 2 * ((0) > (4))) { for (__typeof(4) l = (0) - ((0) > (4)); l != (4) - ((0) > (4)); l += 1 - 2 * ((0) > (4))) { vector<pair<LL, LL> > sq; sq.push_back(rotations[0][i]); sq.push_back(rotations[1][j]); sq.push_back(rotations[2][k]); sq.push_back(rotations[3][l]); if (isSquare(sq)) ans = min(ans, (LL)(i + j + k + l)); } } } } if (ans == LONG_MAX) ans = -1; printf("%lld ", ans); printf("\n"); } }
12
#include <bits/stdc++.h> using namespace std; const int N = 52; char a[N][N]; bool block[N][N]; bool vis[N][N]; int n, m; int tot; void build_wall() { int i, j; for (j = 0; j <= m + 1; j++) { a[0][j] = a[n + 1][j] = '.'; } for (i = 0; i <= n + 1; i++) { a[i][0] = a[i][m + 1] = '.'; } } const int Dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void unblock() { int i, j; for (i = 0; i <= n + 1; i++) for (j = 0; j <= m + 1; j++) if (a[i][j] != '#') block[i][j] = true; else block[i][j] = false; } bool is_connect() { deque<int> qi; deque<int> qj; int i0, j0, i2, j2, i1, j1, d; bool found = false; for (i0 = 1; i0 <= n; i0++) { for (j0 = 1; j0 <= m; j0++) { if (not block[i0][j0]) { found = true; break; } } if (found) { break; } } if (not found) { return true; } qi.push_back(i0); qj.push_back(j0); vis[i0][j0] = true; int cnt = 1; while (not qi.empty()) { i1 = qi.front(); j1 = qj.front(); for (d = 0; d < 4; d++) { i2 = i1 + Dir[d][0]; j2 = j1 + Dir[d][1]; if (not block[i2][j2] and not vis[i2][j2]) { vis[i2][j2] = true; cnt += 1; qi.push_back(i2); qj.push_back(j2); } } qi.pop_front(); qj.pop_front(); } return cnt == tot; } bool try_block(int i, int j, int k, int c) { if (k == c) { memset(vis, 0, sizeof vis); tot -= c; bool ans = not is_connect(); tot += c; return ans; } int i1, j1; for (i1 = i; i1 <= n; i1++) { for (j1 = (i1 == i ? j + 1 : 1); j1 <= m; j1++) { if (a[i1][j1] == '#') { block[i1][j1] = true; if (try_block(i1, j1, k + 1, c)) { return true; } block[i1][j1] = false; } } } return false; } int main() { cin >> n >> m; build_wall(); int i, j; int c, minc; tot = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { cin >> a[i][j]; if (a[i][j] == '#') tot++; } if (tot <= 1) { cout << -1 << endl; return 0; } minc = 2; for (c = 0; c < minc; c++) { unblock(); if (try_block(0, 0, 0, c)) { minc = c; break; } } if (minc >= tot) minc = -1; cout << minc << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, m, answerN = 1, ans[10000]; cin >> n >> m; string s[100]; int k = 0; for (int i = 0; i < n; i++) cin >> s[i]; for (int j = 0; j < m; j++) { int max = 0; for (int i = 0; i < n; i++) { if (s[i][j] - '0' > max) { max = s[i][j] - '0'; } } for (int i = 0; i < n; i++) if (max == s[i][j] - '0') { ans[k] = i; k++; } } for (int i = 0; i < k; i++) { for (int j = 0; j <= i; j++) { if (ans[i] > ans[j]) { int temp = ans[i]; ans[i] = ans[j]; ans[j] = temp; } } } for (int i = 0; i < k - 1; i++) if (ans[i] != ans[i + 1]) answerN++; cout << answerN; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; struct TPoint { double x, y; }; typedef struct TLine { double a, b, c; } TLine; double max(double a, double b) { return a > b ? a : b; } double min(double a, double b) { return a < b ? a : b; } double dis(TPoint p1, TPoint p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } double multi(TPoint p1, TPoint p2, TPoint p0) { return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); } TLine lineFromSegment(TPoint p1, TPoint p2) { TLine tmp; tmp.a = p2.y - p1.y; tmp.b = p1.x - p2.x; tmp.c = p2.x * p1.y - p1.x * p2.y; return tmp; } bool isIntersected(TPoint s1, TPoint e1, TPoint s2, TPoint e2) { if ((max(s1.x, e1.x) >= min(s2.x, e2.x)) && (max(s2.x, e2.x) >= min(s1.x, e1.x)) && (max(s1.y, e1.y) >= min(s2.y, e2.y)) && (max(s2.y, e2.y) >= min(s1.y, e1.y)) && (multi(s2, e1, s1) * multi(e1, e2, s1) >= 0) && (multi(s1, e2, s2) * multi(e2, e1, s2) >= 0)) return true; return false; } TPoint LineInter(TLine l1, TLine l2) { TPoint tmp; double a1 = l1.a; double b1 = l1.b; double c1 = l1.c; double a2 = l2.a; double b2 = l2.b; double c2 = l2.c; if (fabs(b1) < eps) { tmp.x = -c1 / a1; tmp.y = (-c2 - a2 * tmp.x) / b2; } else { tmp.x = (c1 * b2 - b1 * c2) / (b1 * a2 - b2 * a1); tmp.y = (-c1 - a1 * tmp.x) / b1; } return tmp; } bool in(TLine t1, TPoint p1) { if (fabs(t1.a * p1.x + t1.b * p1.y + t1.c) < eps) return 1; else return 0; } TPoint ps, pe; bool mark[100]; int n; TPoint Po[100]; TLine Tl[100]; TPoint Pi[100]; TLine TT; int pp[100]; int ppp[100]; bool flag; int main() { int n, i, j, k, all; double s, ss; while (cin >> ps.x >> ps.y >> pe.x >> pe.y) { flag = 0; memset(mark, 0, sizeof(mark)); cin >> n; for (i = 0; i < n; i++) { cin >> Po[i].x >> Po[i].y; } Po[n].x = Po[0].x; Po[n].y = Po[0].y; all = 0; TT = lineFromSegment(pe, ps); s = 0; ss = 0; for (i = 0; i < n; i++) { ss += dis(Po[i], Po[i + 1]); if (!flag && isIntersected(Po[i], Po[i + 1], ps, pe)) { Tl[i] = lineFromSegment(Po[i], Po[i + 1]); if (in(TT, Po[i]) && in(TT, Po[i + 1])) { flag = 1; } else { Pi[i] = LineInter(Tl[i], TT); if (i == 0) { mark[i] = 1; pp[all++] = i; } if (i > 0 && !(fabs(Pi[i].x - Pi[i - 1].x) < eps && fabs(Pi[i].y - Pi[i - 1].y) < eps)) { mark[i] = 1; pp[all++] = i; } } } } for (i = 0; i < all; i++) { ppp[i] = pp[i]; } if (flag || all == 0 || all == 1) { printf("%.9lf\n", dis(pe, ps)); } else { for (i = pp[0] + 1; i < pp[1]; i++) { s += dis(Po[i], Po[i + 1]); } if (pp[0] < pp[1]) { s += dis(Pi[pp[0]], Po[pp[0] + 1]); s += dis(Pi[pp[1]], Po[pp[1]]); } ss -= s; if (ss < s) { s = ss; } ss = dis(Pi[pp[0]], Pi[pp[1]]); if (ss * 2 < s) s = ss * 2; if (fabs(pe.x - Pi[pp[0]].x) < fabs(pe.x - Pi[pp[1]].x)) { s += dis(pe, Pi[pp[0]]) + dis(ps, Pi[pp[1]]); } else { s += dis(pe, Pi[pp[1]]) + dis(ps, Pi[pp[0]]); } printf("%.9lf\n", s); } } return 0; }
16
#include <bits/stdc++.h> using namespace std; long long int t, n, m, a, b, x, k, y, q, M = 1e9 + 7; float ans(long long int sum, long long int n) { return (float)sum / (float)n; } long long int cntless(long long int x) { long long int ans = 0; for (long long int i = 1; i <= n; i++) { ans += (min((x - 1) / i, m)); } return ans; } int main() { cin >> n >> m >> k; long long int f = 1, b = n * m, p, l, i; while (f < b) { long long int l = 0; p = (f + b) / 2; for (i = 1; i <= n; i++) l += min(m, p / i); if (l >= k) b = p; else f = p + 1; } cout << f << endl; }
10
#include <bits/stdc++.h> int in() { int x = 0; char c = getchar(); bool f = 0; while (c < '0' && c > '9') f |= c == '-', c = getchar(); while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? -x : x; } long long lin() { long long x = 0; char c = getchar(); bool f = 0; while (c < '0' && c > '9') f |= c == '-', c = getchar(); while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? -x : x; } const int N = 4e6 + 5; int f[N], pre[N], n, mod; int main() { n = in(), mod = in(); f[1] = pre[1] = 1; for (int i = 2; i <= n; ++i) { for (int l = 2, r; l <= i; l = r + 1) { r = i / (i / l); (f[i] += 1ll * (r - l + 1) * f[i / l] % mod) %= mod; } (f[i] += pre[i - 1]) %= mod; pre[i] = (pre[i - 1] + f[i]) % mod; } printf("%d\n", f[n]); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] >= 'A' && s[0] <= 'Z') { cout << s; return 0; } s[0] -= 32; cout << s; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<long long> factors(long long x) { vector<long long> v; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { v.push_back(i); if (i != x / i) { v.push_back(x / i); } } } return v; } bool chk(string s, string t) { int i = 0; int j = 0; while (i < s.size()) { if (s[i] == t[j]) { j++; } if (j == t.size()) { return true; } i++; } return false; } int main() { string s, s2; cin >> s; cin >> s2; int n = s.size(); int ans = 0; for (int l = 1; l < n; l++) { int lf = 0; int rt = l - 1; while (rt < n) { string t1; for (int i = 0; i < lf; i++) { t1 += s[i]; } for (long long i = rt + 1; i < n; i++) { t1 += s[i]; } if (chk(t1, s2)) { ans = max(ans, l); } rt++; lf++; } } cout << ans << endl; }
8
#include <bits/stdc++.h> using namespace std; int n, m; string s; int main() { cin >> n >> m >> s; if (m == 0) { cout << s << "\n"; return 0; } int vt; for (vt = 0; vt < n; ++vt) if (s[vt] == '.') break; if (vt >= n) { cout << s << "\n"; return 0; } int i; for (i = vt + 1; i < n; ++i) { if (s[i] >= '5') break; } if (i >= n) { cout << s << "\n"; return 0; } int j; for (j = i - 1; j >= 0; --j) { if (m == 0) { j++; break; } if (s[j] == '.') { j--; break; } m--; if (s[j] < '4') break; } if (s[j] != '.') { int k = j; while (k >= 0 && s[k] == '9') { s[k] = '0'; k--; } if (k >= 0) s[k]++; else { s = "1" + s; j++; n++; } } if (j + 1 < n) s.erase(j + 1, n - 1 - j); if (s[s.length() - 1] == '.') s.erase(s.length() - 1, 1); cout << s << "\n"; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, d = 0; cin >> n; char c; string s; for (int i = 0; i < n; ++i) { cin >> c; if (c == '0') { s += '0' + d; d = 0; } else { d++; } } if (c == '0') { s += '0'; } else { s += '0' + d; } cout << s << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { string j, l = ""; getline(cin, j); int i = 0; while (i < j.length()) { while (i < j.length() && j[i] == ' ') i++; if (j[i] == ',') { cout << ","; if (i != j.length() - 1) cout << " "; l = "c"; i++; } else if (j[i] == '.') { if (i == 0 || l == "c") cout << "..."; else cout << " ..."; i += 3; l = "d"; } else { if (l == "n") cout << " "; while (i < j.length() && j[i] <= '9' && j[i] >= '0') { cout << j[i]; i++; } l = "n"; } } }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; if (n == 1 && m == 1) { cout << "YES\n1\n"; return 0; } if (max(n, m) <= 2 || (min(n, m) <= 2 && max(n, m) == 3)) { cout << "NO" << endl; return 0; } if (n == 3 && m == 3) { cout << "YES\n1 3 4 \n5 7 6 \n9 2 8 \n"; return 0; } bool swp = 0; if (n > m) swap(n, m), swp = 1; int t[n][m]; for (int i = 0; i < n; ++i) { int xj = 1; if (m & 1) xj = i % 2; for (int j = 0; j < m; ++j) { t[i][j] = i * m + xj; xj += 2; if (xj >= m) { xj -= m; if (~m & 1) xj = 0; } } if ((~m & 1) && i & 1) { reverse(t[i], t[i] + m); } } cout << "YES" << endl; if (!swp) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cout << t[i][j] + 1 << " "; } cout << endl; } } else { for (int j = 0; j < m; ++j) { for (int i = 0; i < n; ++i) { int v = t[i][j]; int x = v / m, y = v % m; cout << (y * n + x) + 1 << " "; } cout << endl; } } }
14
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; cout << n + (k - n % k) << endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20; struct Node { int l, r, id; } a[N]; long long flag[N], pre[N]; long long ans[N], Ans = 0; int cn[N]; int n, m, k; int L = 1, R = 0; bool cmp(Node a, Node b) { if (cn[a.l] == cn[b.l]) return a.r < b.r; return a.l < b.l; } void add(int x) { Ans += flag[pre[x] ^ k]; flag[pre[x]]++; } void del(int x) { Ans -= flag[pre[x] ^ k]; if ((pre[x] ^ k) == pre[x]) Ans++; flag[pre[x]]--; } int main() { cin >> n >> m >> k; int sz = sqrt(n); flag[0] = 1; for (int i = 1; i <= n; i++) { long long x; cin >> x; pre[i] = pre[i - 1] ^ x; cn[i] = i / sz + 1; } for (int i = 1; i <= m; i++) { cin >> a[i].l >> a[i].r; a[i].id = i; } sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= m; i++) { while (L < a[i].l) { del(L - 1); L++; } while (L > a[i].l) { add(L - 2); L--; } while (R < a[i].r) { add(++R); } while (R > a[i].r) { del(R--); } ans[a[i].id] = Ans; } for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } return 0; }
14
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, deg[N], mn = 1e9, mx, dep[N]; vector<int> adj[N]; void dfs(int x, int p = -1) { if (adj[x].size() == 1) mn = min(mn, dep[x]); mx = max(mx, dep[x]); for (auto& u : adj[x]) { if (u == p) continue; dep[u] = dep[x] + 1; dfs(u, x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; if (n < 4) return cout << "No", 0; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); deg[u]++; deg[v]++; } deque<int> leaf; int root; for (int i = 1; i <= n; i++) if (deg[i] == 1) leaf.push_back(i); while (leaf.size()) { int x = leaf.front(); root = x; leaf.pop_front(); for (auto& u : adj[x]) { deg[u]--; if (deg[u] == 1) leaf.push_back(u); } } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) continue; if (i == root && adj[i].size() < 3) return cout << "No", 0; else if (i != root && adj[i].size() < 4) return cout << "No", 0; } dfs(root); if (mx == k && mn == k) cout << "Yes"; else cout << "No"; }
10
#include <bits/stdc++.h> using namespace std; struct edge { int from, to; long long cost; }; signed main() { int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<int> ans(n); vector<bool> use(n + 1, false); vector<int> zero; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { ans[i] = a[i]; use[a[i]] = true; } else { ans[i] = 0; zero.push_back(i); } } if ((int)zero.size() == 1) { for (int i = 1; i <= n; i++) { if (!use[i]) { ans[zero[0]] = i; break; } } } else { int j = 0; for (int i = 1; i <= n; i++) { if (!use[i]) { ans[zero[j++]] = i; use[i] = true; if (j > 1) break; } } bool flag = true; for (int i = 0; i < 2; i++) { int zi = zero[i]; if (a[zi] == ans[zi] && b[zi] != ans[zi]) { continue; } else if (a[zi] != ans[zi] && b[zi] == ans[zi]) { continue; } else { flag = false; break; } } if (!flag) { swap(ans[zero[0]], ans[zero[1]]); } } for (int i = 0; i < n - 1; i++) { cout << ans[i] << " "; } cout << ans[n - 1]; }
5
#include <bits/stdc++.h> using namespace std; int mem[505][505], tot[505], vis[505]; char s[505]; int adj(int a, int b) { if (s[a] == s[b]) return 1; if (s[a] - s[b] == 1 || s[a] - s[b] == -1) return 1; return 0; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { memset(mem, 0, sizeof(mem)); memset(tot, 0, sizeof(tot)); int x, y; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); mem[x][y] = mem[y][x] = 1; tot[x]++; tot[y]++; } memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (tot[i] == n - 1) continue; else { s[i] = 'a'; for (int j = 1; j <= n; j++) { if (i == j || tot[j] == n - 1) continue; if (mem[i][j] == 1) s[j] = 'a'; else s[j] = 'c'; } break; } } for (int i = 1; i <= n; i++) { if (tot[i] == n - 1) s[i] = 'b'; } int is = 1; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (adj(i, j) != mem[i][j]) is = 0; } if (is == 0) break; } if (is) { printf("Yes\n"); for (int i = 1; i <= n; i++) printf("%c", s[i]); printf("\n"); } else printf("No\n"); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int maxDiv[1000005]; int tree[1000005 * 6]; void init(int node, int b, int e) { if (b == e) { tree[node] = INT_MAX; return; } int Left = node * 2; int Right = node * 2 + 1; int mid = (b + e) / 2; init(Left, b, mid); init(Right, mid + 1, e); tree[node] = INT_MAX; } int query(int node, int b, int e, int i, int j) { if (i > e || j < b) return INT_MAX; if (b >= i && e <= j) return tree[node]; int Left = node * 2; int Right = node * 2 + 1; int mid = (b + e) / 2; int p1 = query(Left, b, mid, i, j); int p2 = query(Right, mid + 1, e, i, j); return min(p1, p2); } void update(int node, int b, int e, int i, int newvalue) { if (i > e || i < b) return; if (b >= i && e <= i) { tree[node] = newvalue; return; } int Left = node * 2; int Right = node * 2 + 1; int mid = (b + e) / 2; update(Left, b, mid, i, newvalue); update(Right, mid + 1, e, i, newvalue); tree[node] = min(tree[Left], tree[Right]); } int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); int a, b; scanf("%d %d", &a, &b); for (int i = 0; i < v.size(); i++) { int st = (b + v[i] - 1) / v[i] * v[i]; for (int j = st; j < a; j += v[i]) { maxDiv[j - b] = v[i]; } } init(1, 0, a - b); update(1, 0, a - b, a - b, 0); for (int i = a - 1; i >= b; i--) { if (!maxDiv[i - b]) { int q = query(1, 0, a - b, i - b + 1, i - b + 1); update(1, 0, a - b, i - b, q + 1); continue; } int mx = maxDiv[i - b] - 1; int l = i - b; int r = min(a - b, i - b + mx); int q = query(1, 0, a - b, l, r); update(1, 0, a - b, i - b, q + 1); } for (int i = 0; i <= a - b; i++) { } printf("%d", query(1, 0, a - b, 0, 0)); }
14
#include <bits/stdc++.h> using namespace std; int n, qwq, a[70], in[70], s[70], t[70], tot, tt, ts, cnt[1 << 15], f[70], num[70], ss; long long ans = 1, sum, dp[65][1 << 15], fac[70], inv[70], inc[70]; queue<int> q; const int mod = 1e9 + 7; void init() { fac[0] = inv[0] = inv[1] = inc[0] = inc[1] = 1; for (int i = 1; i <= 60; i++) fac[i] = fac[i - 1] * i % mod; for (int i = 2; i <= 60; i++) inv[i] = inv[mod % i] * (mod - mod / i) % mod; for (int i = 2; i <= 60; i++) inc[i] = inc[i - 1] * inv[i] % mod; return; } inline long long C(int a, int b) { return fac[a] * inc[b] % mod * inc[a - b] % mod; } int main() { init(); scanf("%d", &n); qwq = n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int S = 1; S <= n; S++) { if (f[S]) continue; memset(in, 0, sizeof(in)); memset(dp, 0, sizeof(dp)); memset(cnt, 0, sizeof(cnt)); ts = tt = sum = 0; q.push(S); f[S] = S; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 1; i <= n; i++) if (!f[i] && (a[i] % a[u] == 0 || a[u] % a[i] == 0)) q.push(i), f[i] = S; } for (int i = 1; i <= n; i++) { if (f[i] != S) continue; for (int j = 1; j <= n; j++) { if (i == j || f[j] != S) continue; if (a[i] % a[j] == 0) in[i]++; } } for (int i = 1; i <= n; i++) { if (f[i] != S) continue; if (in[i]) t[++tt] = a[i]; else s[++ts] = a[i]; } if (tt <= 1) continue; memset(in, 0, sizeof(in)); for (int i = 1; i <= tt; i++) { for (int j = 1; j <= ts; j++) in[i] |= (t[i] % s[j] == 0) << (j - 1); for (int j = in[i]; j < (1 << ts); j++) cnt[j] += ((in[i] & j) == in[i]); } for (int i = 1; i <= tt; i++) dp[1][in[i]]++; for (int i = 1; i <= tt; i++) { for (int j = 0; j < (1 << ts); j++) { for (int k = 1; k <= tt; k++) if ((j & in[k]) && (j | in[k]) != j) (dp[i + 1][j | in[k]] += dp[i][j]) %= mod; (dp[i + 1][j] += dp[i][j] * max(0, cnt[j] - i) % mod) %= mod; } } for (int j = 0; j < (1 << ts); j++) sum = (sum + dp[tt][j]) % mod; ans = ans * sum % mod; num[++tot] = tt - 1; ss += tt - 1; } for (int i = 1; i <= tot; i++) ans *= C(ss, num[i]), ss -= num[i]; printf("%lld", ans); return 0; }
27