solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; map<string, int> M; int main() { int N; M["Tetrahedron"] = 4; M["Cube"] = 6; M["Octahedron"] = 8; M["Dodecahedron"] = 12; M["Icosahedron"] = 20; while (cin >> N) { int Sum = 0; for (int i = 0; i < N; i++) { string Temp; cin >> Temp; Sum += M[Temp]; } cout << Sum << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool chk[16]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { int s = 0, x; for (int j = 0; j < k; j++) { scanf("%d", &x); s = s * 2 + x; } if (s == 0) return 0 & puts("YES"); chk[s] = true; } for (int i = 0; i < (1 << k); i++) { for (int j = 0; j < (1 << k); j++) { if (i & j) continue; if (chk[i] && chk[j]) return 0 & puts("YES"); } } puts("NO"); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const long long Mod = 1e9 + 7; long long powmod(long long a, long long b) { long long res = 1; a %= Mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % Mod; a = a * a % Mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int x, res; char resc; int main() { scanf("%d", &x); resc = 'A'; switch (x % 4) { case 0: res = 1; break; case 1: res = 0; break; case 2: res = 1; resc = 'B'; break; case 3: res = 2; break; } printf("%d %c\n", res, resc); return 0; }
0
#include <bits/stdc++.h> long long pow(long long n, int power) { long long ans = 1; for (int i = 0; i < power; i++) { ans *= n; } return ans; } int main() { long long n; int size = 0; std::cin >> n; long long temp = n, sum = 0; while (temp) { size++; temp /= 10; } for (int i = 1; i < size; i++) { sum += i * (pow(10, i) - pow(10, i - 1)); } sum += size * (n - (pow(10, size - 1))); sum += size; std::cout << sum; }
2
#include <bits/stdc++.h> using namespace std; int n, k; void solve() { cin >> n >> k; int ans = n; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (i <= k) ans = min(n / i, ans); if (n / i <= k) ans = min(i, ans); } } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); } const long long MAX = 1111; long long C[MAX][MAX]; void pascal_triangle(long long mod) { for (long long i = 0; i < MAX; ++i) C[i][0] = C[i][i] = 1; for (long long i = 2; i < MAX; ++i) for (long long j = 1; j < i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } pair<long long, pair<long long, long long> > extendedEuclid(long long a, long long b) { if (a == 0) return make_pair(b, make_pair(0, 1)); pair<long long, pair<long long, long long> > p; p = extendedEuclid(b % a, a); return make_pair( p.first, make_pair(p.second.second - p.second.first * (b / a), p.second.first)); } long long modInverse(long long a, long long m) { return (extendedEuclid(a, m).second.first + m) % m; } long long inverseMod(long long x, long long y, long long z) { return ((x % z) * modInverse(y, z)) % z; } int a[50], n; string s; int dp[10000000]; int main() { string s1, s2; cin >> s1 >> s2; if (s1 == s2) cout << s1; else cout << 1; }
0
#include <bits/stdc++.h> using namespace std; long long readi() { long long input = 0; char c = ' '; while (c < '-') { c = getchar(); } bool negative = false; if (c == '-') { negative = true; c = getchar(); } while (c >= '0') { input = 10 * input + (c - '0'); c = getchar(); } if (negative) { input = -input; } return input; } void printi(long long output) { if (output == 0) { putchar('0'); return; } if (output < 0) { putchar('-'); output = -output; } int aout[20]; int ilen = 0; while (output) { aout[ilen] = ((output % 10)); output /= 10; ilen++; } for (int i = ilen - 1; i >= 0; i--) { putchar(aout[i] + '0'); } return; } string reads() { string input = ""; char c = ' '; while (c <= ' ') { c = getchar(); } while (c > ' ') { input += c; c = getchar(); } return input; } void prints(string output) { for (int i = 0; i < output.length(); i++) { putchar(output[i]); } return; } int N; pair<int, int> coor[200010]; map<int, int> compress; vector<int> pts[200010]; int64_t pw2[200010]; int parent[200010]; int edge[200010], vert[200010]; int64_t ans = 1ll; int fpar(int u) { if (u == parent[u]) { return u; } parent[u] = fpar(parent[u]); return parent[u]; } void merge(int u, int v) { u = fpar(u); v = fpar(v); if (u == v) { return; } parent[u] = v; return; } int32_t main() { ios_base::sync_with_stdio(false); srand(time(NULL)); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin >> N; for (int i = 0; i < N; i++) { cin >> coor[i].first >> coor[i].second; } pw2[0] = 1; for (int i = 1; i <= 200000; i++) { pw2[i] = pw2[i - 1] * 2; pw2[i] %= 1000000007; } for (int i = 0; i < N; i++) { compress[coor[i].first] = 1; } int spam = 0; for (auto it = compress.begin(); it != compress.end(); it++) { it->second = spam; spam++; } for (int i = 0; i < N; i++) { coor[i].first = compress[coor[i].first]; } compress.clear(); for (int i = 0; i < N; i++) { compress[coor[i].second] = 1; } spam = 0; for (auto it = compress.begin(); it != compress.end(); it++) { it->second = spam; spam++; } for (int i = 0; i < N; i++) { coor[i].second = compress[coor[i].second]; } for (int i = 0; i < N; i++) { pts[coor[i].first].push_back(coor[i].second + N); } for (int i = 0; i < 2 * N; i++) { parent[i] = i; } for (int i = 0; i < 2 * N; i++) { for (int j = 0; j < pts[i].size(); j++) { merge(i, pts[i][j]); } } for (int i = 0; i < 2 * N; i++) { for (int j = 0; j < pts[i].size(); j++) { edge[fpar(i)]++; } vert[fpar(i)]++; } for (int i = 0; i < 2 * N; i++) { if (edge[i] == vert[i] - 1) { ans *= (pw2[vert[i]] - 1); } else { ans *= pw2[vert[i]]; } ans %= 1000000007; } cout << ans << '\n'; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; int ans = 0; int c; map<int, int> m, cnt; for (int i = 1; i <= n; i++) { cin >> c; m[c]++; if (m[c] == 1) { cnt[1]++; } else { cnt[m[c] - 1]--; if (cnt[m[c] - 1] == 0) cnt.erase(m[c] - 1); cnt[m[c]]++; } if (cnt.size() == 1) { auto it = cnt.begin(); if (it->first == 1 or it->second == 1) ans = i; } else if (cnt.size() == 2) { auto it = cnt.begin(); int x = it->first; it++; int y = it->first; if (abs(x - y) == 1 and it->second == 1) ans = i; else if (x == 1 and cnt.begin()->second == 1) ans = i; } } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 2; const int inf = 1e3 + 3; int n; int a[N], d[N]; int tmp[N]; pair<int, int> arr[N]; pair<int, int> fff[N]; int solve() { for (int i = 1; i <= n; ++i) { fff[i] = make_pair(i - a[i] * inf, i); } sort(fff + 1, fff + 1 + n); for (int i = 1; i <= n; ++i) { arr[i] = {fff[i].second, fff[i].first}; } int res = 0; for (int i = 1; i <= n; ++i) { int idx = tmp[i]; int where; for (int j = 1; j <= n; ++j) { if (arr[j].first == idx) { where = j; break; } } arr[where].second = idx - (a[idx] + d[idx]) * inf; while (where > 1 && arr[where - 1].second > arr[where].second) { swap(arr[where], arr[where - 1]); --where; ++res; } while (where < n && arr[where + 1].second < arr[where].second) { swap(arr[where], arr[where + 1]); ++where; ++res; } } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d %d", a + i, d + i); tmp[i] = i; } sort(tmp + 1, tmp + 1 + n, [](int x, int y) { if (d[x] > 0 && d[y] > 0) { return make_pair(a[x], -x) < make_pair(a[y], -y); } if (d[x] < 0 && d[y] < 0) { return make_pair(a[x], -x) > make_pair(a[y], -y); } return d[x] < d[y]; }); printf("%d\n", solve()); }
7
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; vector<string> a(n); unordered_set<string> s; for (int i = 0; i < n; ++i) { cin >> a[i]; s.insert(a[i]); } long long ans = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { if (i == j) continue; string t; for (int l = 0; l < k; ++l) { if (a[i][l] == a[j][l]) t += a[i][l]; else { if (a[i][l] == 'S' && a[j][l] == 'E' || a[i][l] == 'E' && a[j][l] == 'S') { t += 'T'; continue; } if (a[i][l] == 'S' && a[j][l] == 'T' || a[i][l] == 'T' && a[j][l] == 'S') { t += 'E'; continue; } if (a[i][l] == 'E' && a[j][l] == 'T' || a[i][l] == 'T' && a[j][l] == 'E') { t += 'S'; continue; } } } ans += s.count(t); if (t == a[i]) ans--; if (t == a[j]) ans--; } cout << ans / 6; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); { solve(), cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1000100; const int mod = 1e9 + 7; const long long llinf = (long long)2e18; long long dp[N][3]; long long a[N]; long long n; long long fi, se; long long ans = llinf; vector<long long> pr; inline void get(long long x) { long long p = 2; while (p * p <= x) { if (x % p == 0) pr.push_back(p); while (x % p == 0) x /= p; p++; } if (x > 1) pr.push_back(x); } inline void calc() { long long res = llinf; for (int j = 0; j < pr.size(); ++j) { long long x = pr[j]; for (int k = 0; k < 3; ++k) dp[0][k] = 0; for (int i = 1; i <= n; ++i) for (int k = 0; k < 3; ++k) dp[i][k] = llinf; for (int i = 1; i <= n; ++i) { int canstay = (a[i] % x == 0); int canchange = (((a[i] + 1) % x == 0) || ((a[i] - 1) % x == 0)); dp[i][1] = dp[i - 1][1] + fi; dp[i][1] = min(dp[i][1], dp[i - 1][0] + fi); if (canstay) dp[i][0] = dp[i - 1][0]; if (canchange) dp[i][0] = min(dp[i][0], dp[i - 1][0] + se); if (canstay) { dp[i][2] = min(dp[i][2], dp[i - 1][2]); dp[i][2] = min(dp[i][2], dp[i - 1][1]); } if (canchange) { dp[i][2] = min(dp[i][2], dp[i - 1][2] + se); dp[i][2] = min(dp[i][2], dp[i - 1][1] + se); } } res = min(res, min(dp[n][1], min(dp[n][2], dp[n][0]))); } ans = min(ans, res); } int main() { scanf("%lld%lld%lld", &n, &fi, &se); for (int i = 1; i <= (int)n; ++i) scanf("%lld", a + i); get(a[1] + 1); get(a[1]); get(a[1] - 1); get(a[n] + 1); get(a[n] - 1); get(a[n]); calc(); cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long a[maxn], sum[maxn], lazy[maxn]; vector<int> st[maxn], d[maxn]; vector<int> big; bool Less[maxn]; int cnt[maxn][510]; int main() { int n, m, q, k; long long x, ans; char op[2]; scanf("%d%d%d", &n, &m, &q); int bsz = sqrt(n); big.clear(); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= m; i++) { scanf("%d", &k); Less[i] = (k <= bsz); if (!Less[i]) big.push_back(i); for (int j = 0; j < k; j++) { scanf("%lld", &x); st[i].push_back(x); if (!Less[i]) d[x].push_back(big.size() - 1), sum[i] += a[x]; } } for (int i = 1; i <= m; i++) { for (int j = 0; j < st[i].size(); j++) { int v = st[i][j]; for (int k = 0; k < d[v].size(); k++) { cnt[i][d[v][k]]++; } } } while (q--) { scanf("%s", op); ans = 0; if (op[0] == '?') { scanf("%d", &k); if (Less[k]) { for (int i = 0; i < st[k].size(); i++) ans += a[st[k][i]]; for (int i = 0; i < big.size(); i++) ans += lazy[big[i]] * (long long)cnt[k][i]; } else { ans = sum[k]; for (int i = 0; i < big.size(); i++) ans += lazy[big[i]] * (long long)cnt[k][i]; } printf("%lld\n", ans); } else { scanf("%d%lld", &k, &x); if (Less[k]) { for (int i = 0; i < st[k].size(); i++) a[st[k][i]] += x; for (int i = 0; i < big.size(); i++) sum[big[i]] += x * (long long)cnt[k][i]; } else { lazy[k] += x; } } } }
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, count = 0; cin >> n; vector<pair<long double, long double>> vct(n); long long i, j; for (i = 0; i < n; i++) cin >> vct[i].first >> vct[i].second; sort(vct.begin(), vct.end()); for (i = 0; i < vct.size() - 1; i++) { for (j = i + 1; j < vct.size(); j++) { pair<long double, long double> vpr; vpr.first = (vct[i].first + vct[j].first) / 2; vpr.second = (vct[i].second + vct[j].second) / 2; if (binary_search(vct.begin(), vct.end(), vpr)) { count += 1; } } } cout << count << endl; }
2
#include <bits/stdc++.h> using namespace std; const int MAX_H = 100; const int MAX_W = 100; int fs[MAX_H][MAX_W], tps[MAX_H][MAX_W]; int main() { int h, w; scanf("%d%d", &h, &w); memset(fs, -1, sizeof(fs)); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) if (fs[y][x] < 0) for (int c = 0; c < 4; c++) if ((y == 0 || fs[y - 1][x] != c) && (x + 1 >= w || fs[y][x + 1] < 0 || fs[y][x + 1] != c)) { if (x == 0 || fs[y][x - 1] != c) { fs[y][x] = c; tps[y][x] = 1; break; } if (tps[y][x - 1] > 0 && y + tps[y][x - 1] < h) { fs[y][x] = c; tps[y][x] = tps[y][x - 1] + 1; int y1 = y + tps[y][x - 1], x1 = x - tps[y][x - 1]; for (int y0 = y + 1; y0 < y1; y0++) fs[y0][x] = c; for (int x0 = x1; x0 <= x; x0++) fs[y1][x0] = c; break; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) putchar('A' + fs[y][x]); putchar('\n'); } return 0; }
7
#include <bits/stdc++.h> double dp[55][5005]; int f[64], s[64], p[64]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d%d%d", &f[i], &s[i], &p[i]); } double l = 0, r = 1000000000; while (r * 0.9999999999 > l) { double mid = (l + r) / 2; for (int i = 0; i <= m; i++) { dp[n][i] = 0; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= m; j++) { if (j + f[i] > m) { dp[i][j] = mid; continue; } if (j + s[i] > m) { dp[i][j] = (mid + s[i]) * (100 - p[i]) / 100 + (dp[i + 1][j + f[i]] + f[i]) * p[i] / 100; } else { dp[i][j] = (dp[i + 1][j + s[i]] + s[i]) * (100 - p[i]) / 100 + (dp[i + 1][j + f[i]] + f[i]) * p[i] / 100; } if (dp[i][j] > mid) { dp[i][j] = mid; } } } if (dp[0][0] < mid) { r = mid; } else { l = mid; } } printf("%.10f\n", l); return 0; }
8
#include <bits/stdc++.h> using namespace std; typedef struct { int v_, l_, r_; } tnode_t; tnode_t lstree[1 << 22]; char s[1000002]; int n, m; void build(int nidx, int left, int right) { if (left == right) { lstree[nidx].v_ = 0; lstree[nidx].l_ = s[left] == ')' ? 1 : 0; lstree[nidx].r_ = s[left] == '(' ? 1 : 0; } else { int mid = ((left + right) >> 1), ln = (nidx << 1), rn = (nidx << 1) + 1; build(ln, left, mid); build(rn, mid + 1, right); lstree[nidx].l_ = lstree[ln].l_ + max(0, lstree[rn].l_ - lstree[ln].r_); lstree[nidx].r_ = lstree[rn].r_ + max(0, lstree[ln].r_ - lstree[rn].l_); lstree[nidx].v_ = lstree[ln].v_ + lstree[rn].v_ + min(lstree[ln].r_, lstree[rn].l_); } } void adjust(int nidx, int l, int r, int p) { if (l == r) { s[p] = s[p] == ')' ? '(' : ')'; lstree[nidx].v_ = 0; lstree[nidx].l_ = s[p] == ')' ? 1 : 0; lstree[nidx].r_ = s[p] == '(' ? 1 : 0; } else { int mid = (l + r) >> 1, ln = (nidx << 1), rn = (nidx << 1) + 1; if (p > mid) adjust(rn, mid + 1, r, p); else adjust(ln, l, mid, p); lstree[nidx].l_ = lstree[ln].l_ + max(0, lstree[rn].l_ - lstree[ln].r_); lstree[nidx].r_ = lstree[rn].r_ + max(0, lstree[ln].r_ - lstree[rn].l_); lstree[nidx].v_ = lstree[ln].v_ + lstree[rn].v_ + min(lstree[ln].r_, lstree[rn].l_); } } tnode_t query(int nidx, int l, int r, int ql, int qr) { if (l == ql && r == qr) return lstree[nidx]; int mid = (l + r) >> 1, ln = (nidx << 1), rn = (nidx << 1) + 1; if (ql > mid) return query(rn, mid + 1, r, ql, qr); if (qr <= mid) return query(ln, l, mid, ql, qr); tnode_t lr = query(ln, l, mid, ql, mid), rr = query(rn, mid + 1, r, mid + 1, qr), res; res.l_ = lr.l_ + max(0, rr.l_ - lr.r_); res.r_ = rr.r_ + max(0, lr.r_ - rr.l_); res.v_ = lr.v_ + rr.v_ + min(lr.r_, rr.l_); return res; } int main(int argc, char **argv) { std::ios::sync_with_stdio(false); cin >> (s + 1) >> m; n = strlen(s + 1); build(1, 1, n); for (int i = 1, l, r; i <= m; i++) { cin >> l >> r; if (l == 0) adjust(1, 1, n, r); else cout << (query(1, 1, n, l, r).v_ << 1) << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 5; int num[N], op[N], n, x; int main() { scanf("%d%d", &n, &x); for (int i = 1; i <= n; i++) { scanf("%d", op + i); ++num[op[i]]; } long long ans = 0; for (int i = 1; i <= n; i++) { int to = x ^ op[i]; to = to == op[i] ? num[to] - 1 : num[to]; ans += to; } printf("%lld", ans >> 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 1; int n, m, a[maxn], ans[maxn]; const int B = 20; struct LB { int vec[B], pos[B]; LB() { memset(vec, 0, sizeof(vec)); memset(pos, 0, sizeof(pos)); } inline void push(int value, int pos) { for (int i = B - 1; i >= 0; --i) if ((value >> i) & 1) { if (!vec[i]) { vec[i] = value; this->pos[i] = pos; return; } if (pos > this->pos[i]) { swap(pos, this->pos[i]); swap(value, vec[i]); } value ^= vec[i]; } } inline int ans(int l) { int ret = 0; for (int i = B - 1; i >= 0; --i) if (vec[i] && pos[i] >= l && ((ret >> i) & 1) == 0) ret ^= vec[i]; return ret; } } lb[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) { lb[i] = lb[i - 1]; lb[i].push(a[i], i); } scanf("%d", &m); for (int i = 1; i <= m; ++i) { int l, r; scanf("%d %d", &l, &r); printf("%d\n", lb[r].ans(l)); } }
8
#include <bits/stdc++.h> using namespace std; namespace mine { long long qread() { long long ans = 0, f = 1; char c = getchar(); while (c < '0' or c > '9') { if (c == '-') f = -1; c = getchar(); } while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar(); return ans * f; } void write(long long num) { if (num < 0) putchar('-'), num = -num; if (num >= 10) write(num / 10); putchar('0' + num % 10); } void write1(long long num) { write(num); putchar(' '); } void write2(long long num) { write(num); putchar('\n'); } template <typename T> inline bool chmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool chmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } bool IN(long long x, long long l, long long r) { return l <= x and x <= r; } const int INF = 0x3f3f3f3f; const int MOD = 998244353; int mm(const int x) { return x >= MOD ? x - MOD : x; } template <typename T> void add(T &x, const int &y) { x = (x + y >= MOD ? x + y - MOD : x + y); } long long qpower(long long x, long long e, int mod = MOD) { long long ans = 1; while (e) { if (e & 1) ans = ans * x % mod; x = x * x % mod; e >>= 1; } return ans; } long long invm(long long x) { return qpower(x, MOD - 2); } const int N = 4e5 + 10; int dd[N]; int dfn[N], dep[N], bak[N], dfnid; int lg[N], stb[21][N]; int mn(int x, int y) { return dep[x] < dep[y] ? x : y; } int ff[N][21]; vector<int> son2[N]; void dfs(int x, int fa = 0) { ff[x][0] = fa; for (int i = (1), I = (20); i <= I; i++) ff[x][i] = ff[ff[x][i - 1]][i - 1]; dd[x] = dd[fa] + 1; stb[0][dfn[x] = ++dfnid] = x; bak[dfnid] = x; for (auto y : son2[x]) dfs(y), stb[0][++dfnid] = x; } int getlca(int x, int y) { if (dfn[x] > dfn[y]) swap(x, y); x = dfn[x], y = dfn[y]; int t = lg[y - x + 1]; return mn(stb[t][x], stb[t][y - (1ll << (t)) + 1]); } int getdis(int x, int y) { return dep[x] + dep[y] - 2 * dep[getlca(x, y)]; } int n; long long ans; int go[N]; set<int> real[N]; long long realsiz[N]; vector<int> son[N]; int val[N]; void insert(int fm, int to) { for (auto t : real[fm]) { if (real[to].count(t)) continue; set<int>::iterator it = real[to].insert(t).first, al = it, ar = it; ++ar; if (ar == real[to].end()) ar = real[to].begin(); if (al == real[to].begin()) al = --real[to].end(); else al--; int a = bak[*al], b = bak[*ar]; realsiz[to] += getdis(a, bak[t]) + getdis(bak[t], b) - getdis(a, b); } real[fm].clear(); realsiz[fm] = 0; } void dsu(int x) { for (auto y : son[x]) { dsu(y); if (((int)(real[go[x]]).size()) < ((int)(real[go[y]]).size())) insert(go[x], go[y]), swap(go[y], go[x]); else insert(go[y], go[x]); } ans += (realsiz[go[x]] / 2) * val[x]; } char str[N]; int rev[N]; struct SAM { struct Nod { int son[26], fail, len; } p[N]; int id, lst; SAM() { id = lst = 1; } int insert(int c) { int now = ++id; p[now].len = p[lst].len + 1; int a = lst; while (a and !p[a].son[c]) p[a].son[c] = now, a = p[a].fail; int b = p[a].son[c]; if (!b) p[now].fail = 1; else if (p[b].len == p[a].len + 1) p[now].fail = b; else { int tmp = ++id; p[tmp] = p[b]; p[tmp].len = p[a].len + 1; p[b].fail = p[now].fail = tmp; while (a and p[a].son[c] == b) p[a].son[c] = tmp, a = p[a].fail; } return lst = now; } void build1() { for (int i = (1), I = (id); i <= I; i++) son[p[i].fail].push_back(i), val[i] = p[i].len - p[p[i].fail].len, go[i] = i; for (int i = 1, now = 1; i <= n - 2; i++) { now = p[now].son[str[i] - 'a']; real[now].insert(dfn[rev[i + 2]]), real[now].insert(dfn[1]); realsiz[now] = dep[rev[i + 2]] * 2; } dsu(1); } void build2() { for (int i = (1), I = (id); i <= I; i++) dep[i] = p[i].len, son2[p[i].fail].push_back(i); dfs(1); for (int i = (2), I = (N - 1); i <= I; i++) lg[i] = lg[i >> 1] + 1; for (int t = (1), I = (20); t <= I; t++) for (int i = (1), I = (dfnid - (1ll << (t)) + 1); i <= I; i++) stb[t][i] = mn(stb[t - 1][i], stb[t - 1][i + (1ll << (t - 1))]); } long long getcnt() { long long ret = 0; for (int i = (1), I = (id); i <= I; i++) ret += p[i].len - p[p[i].fail].len; return ret; } } A, B; void main() { scanf("%s", str + 1); n = strlen(str + 1); for (int i = (1), I = (n); i <= I; i++) { if (i == n) ans += A.getcnt(); A.insert(str[i] - 'a'); } for (int i = (n), I = (1); i >= I; i--) { if (i == 1) ans += B.getcnt(); rev[i] = B.insert(str[i] - 'a'); } B.build2(); A.build1(); write(ans + A.getcnt() + 1 + 1); } }; // namespace mine signed main() { srand(time(0)); mine::main(); }
13
#include <bits/stdc++.h> using namespace std; struct ss { int v, t; }; ss s[100006]; int n, a, d; double tmm[100006]; double t1, t2, t3, s1; int main() { scanf("%d%d%d", &n, &a, &d); for (int i = 0; i < n; i++) { scanf("%d%d", &s[i].t, &s[i].v); if (i == 0) { t1 = (s[i].v * 1.0) / (a * 1.0); s1 = .5 * a * t1 * t1; if (s1 >= d * 1.0 || fabs(s1 - d * 1.0) < 1e-9) { t2 = sqrt(2.0 * d / (a * 1.0)); tmm[i] = s[i].t + t2; } else { t2 = (d * 1.0 - s1) / (s[i].v * 1.0); tmm[i] = t1 + s[i].t + t2; } } else { t1 = (s[i].v * 1.0) / (a * 1.0); s1 = .5 * a * t1 * t1; if (s1 >= d * 1.0 || fabs(s1 - d * 1.0) < 1e-9) { t2 = sqrt(2.0 * d / (a * 1.0)); tmm[i] = s[i].t + t2; } else { t2 = (d * 1.0 - s1) / (s[i].v * 1.0); tmm[i] = t1 + s[i].t + t2; } if (tmm[i] < tmm[i - 1]) tmm[i] = tmm[i - 1]; } printf("%.10lf\n", tmm[i]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<int> a[1005]; vector<int> b[1005]; int n, sz[1005], x, st, ed, id, LL, RR, lo, hi; int sol(int L, int R, int id) { lo = -1, hi = b[id].size(); while (lo + 1 < hi) { int mid = (lo + hi) / 2; if (b[id][mid] > L) hi = mid; else lo = mid; } LL = hi; lo = -1, hi = b[id].size(); while (lo + 1 < hi) { int mid = (lo + hi) / 2; if (b[id][mid] > R) hi = mid; else lo = mid; } RR = lo; return max(0, RR - LL + 1); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &sz[i]); for (int j = 0; j < sz[i]; j++) { scanf("%d", &x); a[i].push_back(x); } sort(a[i].begin(), a[i].end()); id = (i + 1) % n; for (int j = 0; j < sz[i]; j++) { b[i].push_back(a[i][j]); b[id].push_back(a[i][j]); } } for (int i = 0; i < n; i++) sort(b[i].begin(), b[i].end()); int ret = 0; for (int i = 0; i < n; i++) { id = (i + 1) % n; for (int j = 0; j < sz[i] - 1; j++) { st = a[i][j]; ed = a[i][j + 1]; if (sol(st, ed, i) != sol(st, ed, id)) ret++; } } printf("%d\n", ret); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 500001; const int oo = 1e9; const int LOGN = 20; const int MOD = 1000000007; const double PI = acos(-1); int n, a[N]; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } int mn = oo, idx = -1; for (int t = 1; t <= 101; ++t) { int cur = 0; for (int j = 0; j < n; ++j) { int cst = max(0, abs(a[j] - t) - 1); cur += cst; } if (cur < mn) { mn = cur; idx = t; } } cout << idx << " " << mn << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int nax = 4e5 + 10; long long mod = 1e9 + 7; long long sparse[nax][2]; map<int, int> map_l[2]; long long bin_pow(long long a, long long p) { long long r = 1; while (p) { if (p & 1) r = r * a % mod; a = a * a % mod; p >>= 1; } return r; } int main() { ios::sync_with_stdio(0); cin.tie(0); int k, n[2]; cin >> k >> n[0] >> n[1]; vector<int> pos; for (int t = 0; t < 2; t++) { for (int i = 0; i < n[t]; i++) { int a, b; cin >> a >> b; pos.push_back(a - 1); pos.push_back(b - 1); map_l[t][b] = max(map_l[t][b], a); } } pos.push_back(0); pos.push_back(k - 1); pos.push_back(k); sort(pos.begin(), pos.end()); pos.resize(unique(pos.begin(), pos.end()) - pos.begin()); { sparse[0][0] = sparse[0][1] = 1; int reach[2] = {-1, -1}; for (int pi = 1; pi < pos.size(); pi++) { long long mi[2]; for (int t = 0; t < 2; t++) { int starti = pos[pi - 1] + 1; if (!map_l[t].count(starti)) continue; int v = map_l[t][pos[pi - 1] + 1] - 1; int j = lower_bound(pos.begin(), pos.end(), v) - pos.begin(); assert(pos[j] == v); reach[t] = max(reach[t], j); } for (int t = 0; t < 2; t++) { mi[t] = reach[t] > -1 ? sparse[reach[t]][!t] : 0; sparse[pi][t] = sparse[pi - 1][t]; } int k = pos[pi] - pos[pi - 1]; long long sum = sparse[pi][0] + sparse[pi][1], m = -(mi[0] + mi[1]) % mod; sum = (bin_pow(2, k - 1) * (sum + m) - m) % mod; for (int t = 0; t < 2; t++) { sparse[pi][t] = (sum - mi[t]) % mod; } } } int l = pos.size() - 2; cout << (sparse[l + 1][0] + sparse[l + 1][1] - sparse[l][0] - sparse[l][1] + mod * 4) % mod << endl; }
10
#include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef WA_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif using ll = long long; using ull = unsigned long long; #define pb push_back #define fi first #define se second #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++) #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--) const int mod = 1e9+7; const int inf = 0x3f3f3f3f; const int maxn = 1e5+10; int main() { #ifndef WA_DEBUG ios::sync_with_stdio(false);cin.tie(nullptr); #endif int n,m,x; cin>>n>>m>>x; vector<ll> a(n+1); ll sum=0; rep(i,1,n) { cin>>a[i]; sum+=a[i]; } if(sum<1ll*(n-1)*x) { cout<<"NO"<<'\n'; return 0; } vector<pair<int,int>> edge(m+1); vector<vector<int>> g(n+1); rep(i,1,m) { cin>>edge[i].fi>>edge[i].se; g[edge[i].fi].pb(i); g[edge[i].se].pb(i); } vector<int> ans(n-1); vector<bool> vis(n+1); int l=0,r=n-2; function<void(int,int)> dfs=[&](int now,int fa) { vis[now]=true; for(int id:g[now]) { int to=edge[id].fi^edge[id].se^now; if(to==fa||vis[to]) continue; dfs(to,now); if(a[to]+a[now]>=x) { a[now]+=a[to]-x; ans[l++]=id; } else ans[r--]=id; } }; dfs(1,0); cout<<"YES"<<'\n'; for(int x:ans) cout<<x<<'\n'; return 0; }
9
#include <bits/stdc++.h> using namespace std; string A[102], T = "12345RGBYW"; int color[102], num[102], n; int main() { cin >> n; string inp; for (int i = 0; i < n; i++) cin >> A[i]; int ans = INT_MAX; vector<string> rem; for (int mask = 0; mask < (1 << 10); mask++) { memset(color, 0, sizeof color); memset(num, 0, sizeof num); rem.clear(); for (int bit = 0; bit < 5; bit++) { if (mask & (1 << bit)) { for (int i = 0; i < n; i++) { if (A[i][1] == T[bit]) { num[i] = 1; } } } } for (int bit = 5; bit < 10; bit++) { if (mask & (1 << bit)) { for (int i = 0; i < n; i++) { if (A[i][0] == T[bit]) { color[i] = 1; } } } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (A[i] == A[j]) continue; if ((color[i] || color[j]) && A[i][0] != A[j][0]) continue; if ((num[i] || num[j]) && A[i][1] != A[j][1]) continue; rem.push_back(A[i]); rem.push_back(A[j]); } } bool flag = true; for (int i = 1; i < rem.size(); i++) { if (rem[i] != rem[i - 1]) { flag = false; break; } } if (flag) { ans = min(ans, __builtin_popcount(mask)); } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, m; char s[N]; int main() { scanf("%d%d%s", &n, &m, s + 1); char mn = 'z', mx = 'a'; for (int i = 1; i <= n; ++i) mn = min(mn, s[i]), mx = max(mx, s[i]); if (m > n) { for (int i = 1; i <= n; ++i) putchar(s[i]); for (int i = n + 1; i <= m; ++i) putchar(mn); return 0; } int pos = m; while (s[pos] == mx) --pos; for (int i = 1; i < pos; ++i) putchar(s[i]); char bb = 'z'; for (int i = 1; i <= n; ++i) if (s[i] > s[pos]) bb = min(bb, s[i]); putchar(bb); for (int i = pos + 1; i <= m; ++i) putchar(mn); return 0; }
3
#include <bits/stdc++.h> using namespace std; int sc[150]; int p[75][75]; double q[150][75]; int l[150], r[150]; double dp[150][75]; double sum[150]; int main() { int i, j, k, n, N, pnt; cin >> N; n = 1 << N; for (i = 1; i <= n; i++) { q[0][i] = 1; for (j = 1; j <= n; j++) { scanf("%d", &p[i][j]); } } for (pnt = 1; pnt < n; pnt <<= 1) ; for (i = 1; i <= n; i++) { l[pnt + i - 1] = i; r[pnt + i - 1] = i; sc[pnt + i - 1] = 100000; q[pnt + i - 1][i] = 1; } for (i = pnt; --i;) { sc[i] = sc[2 * i]; int l1 = l[2 * i], l2 = l[2 * i + 1]; int r1 = r[2 * i], r2 = r[2 * i + 1]; for (j = l1; j <= r1; j++) { for (q[i][j] = 0, k = l2; k <= r2; k++) q[i][j] += 0.01 * p[j][k] * q[2 * i + 1][k]; q[i][j] *= q[2 * i][j]; dp[i][j] = max(dp[i][j], dp[2 * i][j] + dp[2 * i + 1][0] + sc[i] * q[i][j]); } for (k = l2; k <= r2; k++) { for (q[i][k] = 0, j = l1; j <= r1; j++) q[i][k] += 0.01 * p[k][j] * q[2 * i][j]; q[i][k] *= q[2 * i + 1][k]; dp[i][k] = max(dp[i][k], dp[2 * i + 1][k] + dp[2 * i][0] + sc[i] * q[i][k]); } l[i] = min(l1, l2), r[i] = max(r1, r2), sc[i] = 2 * sc[2 * i]; for (dp[i][0] = 0, j = l[i]; j <= r[i]; j++) dp[i][0] = max(dp[i][0], dp[i][j]); } printf("%.10lf\n", dp[1][0] / 100000); }
6
#include <bits/stdc++.h> using namespace std; void solve(); int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } bool comp(pair<int, int>& a, pair<int, int>& b) { if (a.first < b.first) return true; if (a.first == b.first) return a.second > b.second; return false; } void solve() { int v; cin >> v; int n = 9; pair<int, int> a[n]; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i + 1; } sort(a, a + n, comp); if (v < a[0].first) { cout << -1 << endl; return; } int d = v / a[0].first; string ans = string(d, char(a[0].second + '0')); v %= a[0].first; for (int dig = 0; dig < d; dig++) { int prev = a[0].first; for (int i = 8; i >= 0; i--) { if (v + prev >= a[i].first && a[i].second > ans[dig] - '0') { ans[dig] = char(a[i].second + '0'); v = v + prev - a[i].first; prev = a[i].first; } } } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char c; int f = 1; while (!isdigit(c = getchar())) if (c == '-') f = -1; x = (c & 15); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); x *= f; } const int mod = 998244353, inv2 = (mod + 1) / 2; int n, m, i, j, inv[35], c[35][35], a[35]; int qp(int x, int y) { int z = 1; while (y) { if (y & 1) { z = 1ll * x * z % mod; } x = 1ll * x * x % mod; y /= 2; } return z; } struct func { int l, r; vector<int> xs; }; bool operator<(func x, func y) { return x.l < y.l; } vector<func> s, nxt; vector<int> intg(int l, int r) { vector<int> ans, tmp; int i, j, k; for (i = 0; i < s.size(); i++) { tmp.clear(); tmp.resize(s[i].xs.size() + 1); if (s[i].l <= l - 1000000) { if (s[i].r <= l - 1000000) { continue; } else if (l - 1000000 < s[i].r && s[i].r <= r - 1000000 || r - 1000000 < s[i].r && s[i].r <= l) { for (j = 1; j <= s[i].xs.size(); j++) { for (k = 0; k <= j; k++) { { tmp[k] -= (1ll * inv[j] * s[i].xs[j - 1] % mod * c[j][k] % mod * qp(mod - 1000000, j - k) % mod); if (tmp[k] < 0) tmp[k] += mod; } } { tmp[0] += (1ll * inv[j] * s[i].xs[j - 1] % mod * qp(s[i].r, j) % mod); if (tmp[0] >= mod) tmp[0] -= mod; } } } else exit(-1); } else if (l - 1000000 < s[i].l && s[i].l <= r - 1000000 || r - 1000000 < s[i].l && s[i].l <= l) { if (l - 1000000 < s[i].r && s[i].r <= r - 1000000) { exit(-1); } else if (r - 1000000 < s[i].r && s[i].r <= l) { for (j = 1; j <= s[i].xs.size(); j++) { { tmp[0] -= (1ll * inv[j] * s[i].xs[j - 1] % mod * qp(s[i].l, j) % mod); if (tmp[0] < 0) tmp[0] += mod; } { tmp[0] += (1ll * inv[j] * s[i].xs[j - 1] % mod * qp(s[i].r, j) % mod); if (tmp[0] >= mod) tmp[0] -= mod; } } } else if (l < s[i].r) { for (j = 1; j <= s[i].xs.size(); j++) { { tmp[0] -= (1ll * inv[j] * s[i].xs[j - 1] % mod * qp(s[i].l, j) % mod); if (tmp[0] < 0) tmp[0] += mod; } { tmp[j] += (1ll * inv[j] * s[i].xs[j - 1] % mod); if (tmp[j] >= mod) tmp[j] -= mod; } } } } else continue; while (ans.size() < tmp.size()) ans.push_back(0); for ((j) = 0; (j) < (tmp.size()); (j)++) { ans[j] += (tmp[j]); if (ans[j] >= mod) ans[j] -= mod; } } return ans; } int main() { inv[1] = 1; for ((i) = (2); (i) <= (32); (i)++) { inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; } c[0][0] = 1; for ((i) = 1; (i) <= (32); (i)++) { c[i][0] = 1; for ((j) = 1; (j) <= (i); (j)++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; } } cin >> n; for ((i) = 1; (i) <= (n); (i)++) { double x; cin >> x; a[i] = x * 1000000 + 0.5; if (a[i] == 0) { puts("0"); return 0; } a[i] = min(a[i], a[i - 1] + 1000000); } vector<int> tmp; tmp.push_back(1); s.push_back((func){0, a[1], tmp}); for ((i) = (2); (i) <= (n); (i)++) { vector<int> all; nxt.clear(); for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) { all.push_back(it->l); all.push_back(it->r); all.push_back(it->l + 1000000); all.push_back(it->r + 1000000); } sort(all.begin(), all.end()); all.resize(unique(all.begin(), all.end()) - all.begin()); while (all.back() > a[i]) all.pop_back(); if (all.back() != a[i]) all.push_back(a[i]); for (j = 1; j < all.size(); j++) { nxt.push_back((func){all[j - 1], all[j], intg(all[j - 1], all[j])}); } s = nxt; } int ans = 0; for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) { for ((i) = 1; (i) <= (it->xs.size()); (i)++) { { ans += (1ll * inv[i] * it->xs[i - 1] % mod * qp(it->r, i) % mod); if (ans >= mod) ans -= mod; } { ans -= (1ll * inv[i] * it->xs[i - 1] % mod * qp(it->l, i) % mod); if (ans < 0) ans += mod; } } } printf("%d\n", 1ll * ans * qp(qp(1000000, mod - 2), n) % mod); return 0; }
13
#include <bits/stdc++.h> const long long INF = 1000000000000000000ll; void GetDivisors(int a, std::set<int> &divs) { for (int i = 2; i * i <= a; i++) { if (a % i == 0) { divs.insert(i); while (a % i == 0) a /= i; } } if (a != 1) divs.insert(a); } int Abs(int value) { if (value < 0) return -value; return value; } int GetShift(int value, int candidate) { for (int shift = -1; shift <= 1; shift++) { if ((value + shift) % candidate == 0) { return Abs(shift); } } return -1; } void Relax(long long &value, long long nvalue) { value = std::min(value, nvalue); } int main() { int n, A, B; scanf("%d %d %d", &n, &A, &B); std::vector<int> elements(n); for (int i = 0; i < n; i++) scanf("%d", &elements[i]); std::set<int> candidates; int PossibleSt[] = {elements[0], elements[0] - 1, elements[0] + 1, elements[n - 1], elements[n - 1] - 1, elements[n - 1] + 1}; for (int i = 0; i < 6; i++) GetDivisors(PossibleSt[i], candidates); long long bestAns = (n - 1) * 1ll * A; for (std::set<int>::iterator it = candidates.begin(); it != candidates.end(); it++) { int candidate = *it; std::vector<long long> d(3, 0); for (int i = 0; i < n; i++) { std::vector<long long> nd(3, INF); int shift = GetShift(elements[i], candidate) * B; if (shift >= 0) { Relax(nd[0], d[0] + shift); Relax(nd[2], d[1] + shift); Relax(nd[2], d[2] + shift); } Relax(nd[1], d[0] + A); Relax(nd[1], d[1] + A); d = nd; } for (int id = 0; id < 3; id++) Relax(bestAns, d[id]); } printf("%lld\n", bestAns); }
7
#include <bits/stdc++.h> using namespace std; long long int ll[4], rr[4]; long long int ans_x, ans_y, ans_z; bool check(long long int R) { for (int i = 0; i < 4; i++) if (ll[i] - R > rr[i] + R) return false; for (int i = 0; i < 2; i++) { long long int l[4], r[4]; for (int j = 0; j < 4; j++) { l[j] = (ll[j] - R); r[j] = (rr[j] + R); } for (int k = 0; k < 4; k++) { if (abs(l[k]) % 2 != i) l[k]++; if (abs(r[k]) % 2 != i) r[k]--; } l[0] = max(l[0], l[1] + l[2] + l[3]); r[0] = min(r[0], r[1] + r[2] + r[3]); if (l[0] > r[0]) continue; if (l[1] > r[1]) continue; if (l[2] > r[2]) continue; if (l[3] > r[3]) continue; long long int s1 = l[1], s2 = l[2], s3 = l[3]; s1 = min(r[1], l[0] - s2 - s3); s2 = min(r[2], l[0] - s1 - s3); s3 = min(r[3], l[0] - s1 - s2); ans_x = (s2 + s3) / 2; ans_y = (s1 + s3) / 2; ans_z = (s1 + s2) / 2; return true; } return false; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 0; i < 4; i++) { ll[i] = -1e100; rr[i] = 1e100; } for (int i = 0; i < n; i++) { long long int x, y, z; scanf("%lld%lld%lld", &x, &y, &z); ll[0] = max(ll[0], x + y + z); rr[0] = min(rr[0], x + y + z); ll[1] = max(ll[1], -x + y + z); rr[1] = min(rr[1], -x + y + z); ll[2] = max(ll[2], x - y + z); rr[2] = min(rr[2], x - y + z); ll[3] = max(ll[3], x + y - z); rr[3] = min(rr[3], x + y - z); } long long int l = -1, r = 4e18, m; while (l + 1 != r) { m = (l + r) / 2; if (check(m)) r = m; else l = m; } printf("%lld %lld %lld\n", ans_x, ans_y, ans_z); } }
10
#include <bits/stdc++.h> using namespace std; int n, m; int a[1010][1010]; int in[1010]; int check() { for (int t = 1; t < n; ++t) { for (int i = 0; i < n; ++i) { int j = (i + t) % n; if (a[j][i] || a[i][j] || in[i] == m) continue; a[i][j] = 1; ++in[i]; } } for (int i = 0; i < n; ++i) if (in[i] != m) return 0; int ans = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (a[i][j]) ++ans; return ans; } int main() { scanf("%d%d", &n, &m); int ans = check(); if (ans) { printf("%d\n", ans); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (a[i][j]) { printf("%d %d\n", i + 1, j + 1); } } else puts("-1"); }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int count = 0, a, b, temp = 1; cin >> a >> b; while (temp != 0) { count += a / b; temp = a % b; a = b; b = temp; } cout << count; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); string tostore = "[:|:]"; int curr = 0; unsigned long long toremove = 0; int i; for (i = 0; i < n; i++) { if (s[i] == tostore[curr]) { if (curr < 2) curr++; if (curr == 2) break; } else { toremove++; } } int j; curr = 4; for (j = n - 1; j > i; j--) { if (s[j] == tostore[curr]) { if (curr > 2) curr--; if (curr == 2) break; } else { toremove++; } } for (int x = i + 1; x < j; x++) { if (s[x] != '|') toremove++; } if (n - toremove >= 4) cout << n - toremove; else cout << -1; }
2
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long ans = 1; x %= p; while (y > 0) { if (y & 1) ans = ((ans % p) * (x % p)) % p; y = y >> 1; x = (x * x) % p; } return ans % p; } map<long long, long long> M; long long n, m; long long cost[100005]; set<pair<long long, long long> > s; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i, j, k; long long T; T = 1; while (T--) { cin >> n >> m; for (i = 1; i <= n; ++i) { cin >> j; M[i] = j; } for (i = 1; i <= n; ++i) { cin >> cost[i]; s.insert(make_pair(cost[i], i)); } long long type, q; long long ans; while (m--) { ans = 0; cin >> type >> q; long long take = min(M[type], q); M[type] -= take; if (M[type] == 0) { M.erase(type); auto it = s.find(make_pair(cost[type], type)); if (it != s.end()) s.erase(it); } q -= take; ans += take * cost[type]; while (s.size() && q) { auto it = s.begin(); type = (*it).second; take = min(M[type], q); M[type] -= take; if (M[type] == 0) { M.erase(type); auto it = s.find(make_pair(cost[type], type)); if (it != s.end()) s.erase(it); } q -= take; ans += take * cost[type]; } if (q == 0) cout << ans << "\n"; else cout << 0 << "\n"; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const unsigned long long magic = 1e9 + 7; int main() { char s[2005]; char s1[2005]; char s2[2005]; scanf("%s", &s); scanf("%s", &s1); scanf("%s", &s2); int sz = strlen(s); int sz1 = strlen(s1); int sz2 = strlen(s2); unsigned long long t1 = 0, t2 = 0; unsigned long long t = 0; vector<int> v1, v2; unsigned long long tmp1 = 1, tmp2 = 1; for (int i = 0; i < sz1; i++) { t1 = t1 * magic + s1[i]; tmp1 *= magic; } for (int i = 0; i < sz2; i++) { t2 = t2 * magic + s2[i]; tmp2 *= magic; } for (int i = 0; i < sz; i++) { t = t * magic + s[i]; if (i >= sz1) t -= s[i - sz1] * tmp1; if (t == t1) v1.push_back(i - sz1 + 1); } t = 0; for (int i = 0; i < sz; i++) { t = t * magic + s[i]; if (i >= sz2) t -= s[i - sz2] * tmp2; if (t == t2) v2.push_back(i - sz2 + 1); } if (s1 == s2) { cout << v1.size() << endl; return 0; } unordered_map<unsigned long long, int> mp; int cnt = 0; for (int i = 0; i < v1.size(); i++) { int l = v1[i]; unsigned long long tmp = 0; for (int j = 0; j < v2.size(); j++) { int r = v2[j]; if (l + sz1 > r + sz2 || l > r) continue; while (l != r) { tmp = tmp * magic + s[l]; l++; } if (!mp[tmp + t2]) { mp[tmp + t2]++; cnt++; } } } cout << cnt << endl; }
6
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; long long power(long long x, unsigned long long y) { long long res = 1; while (y > 0) { if (y & 1) { res = res * x; } y = y >> 1; x = x * x; } return res; } long long powermod(long long x, unsigned long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } long long min(long long a, long long b) { return a < b ? a : b; } long long max(long long a, long long b) { return a > b ? a : b; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { long long f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } void snow() { long long n; cin >> n; long long arr[n]; long long on = 0; long long ans = 0; for (long long i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == 1) { on++; continue; } if (arr[i] == 2) { if (on) { ans++; on--; continue; } else { on += 2; } continue; } long long p = arr[i] / 2; long long qq = min(p, on); ans += min(p, on); on -= qq; arr[i] -= (qq * 2); ans += arr[i] / 3; on += arr[i] % 3; } cout << ans << '\n'; } int32_t main() { auto start = high_resolution_clock::now(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long _; _ = 1; while (_--) snow(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); return 0; }
5
#include <bits/stdc++.h> using namespace std; unsigned long long int combi(unsigned long long int num) { unsigned long long int ans; ans = (num * (num - 1)) / 2; return ans; } int main() { unsigned long long int i, j, rem, ans, a, b, mx, mi, sum, res, div; cin >> a >> b; res = a - (b - 1); mx = (res * (res - 1)) / 2; if (b == 1) cout << mx << " " << mx << endl; else { if ((a / 2) < b) { ans = 0; div = a / 2; if (a % 2) ans = 1; res = b - div; mi = div - res + ans; } else if ((a / 2) == b) mi = b; else { res = a / b; if (res * b == a) mi = combi(res) * b; else { sum = 0; rem = a - (res * b); ans = b - rem; sum = sum + combi(res) * ans; sum += combi(res + 1) * rem; mi = sum; } } cout << mi << " " << mx << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, temp; cin >> n; int arr[101]; for (int i = 0; i < 101; i++) arr[i] = 0; for (int i = 0; i < n; i++) { cin >> temp; arr[temp]++; } int var = 0; int c1 = 0, c2 = 0; int p1, p2; for (int i = 0; i < 101; i++) { if (arr[i] != 0) { if (c1 == 0) { c1 = arr[i]; p1 = i; } else { c2 = arr[i]; p2 = i; } var++; } } if (var != 2) { cout << "NO" << endl; } else { if (c1 == c2) { cout << "YES" << endl; cout << p1 << " " << p2 << endl; } else { cout << "NO" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int w, h, k, sum = 0; cin >> w >> h >> k; for (int i = 0; i < k; i++) { if ((w - 2) >= 0 && (h - 2) >= 0) { sum = sum + w * h - ((w - 2) * (h - 2)); w = w - 4; h = h - 4; } } printf("%d", sum); }
0
#include <bits/stdc++.h> using namespace std; int sm[400005]; struct th { int tp, pl, id; bool operator<(const th &r) const { return pl < r.pl; } } p[400005]; const int pls = 200002; int nr[400005], fr[400005], nx[400005]; int fl[400005]; int ns[400005], res[400005]; int m, n; int cal(int a, int b) { if (a > b) swap(a, b); int dis = b - a; return min(dis, m - dis); } void mch(int u, int v) { if (u > v) swap(u, v); res[u] = v - n; } int main() { scanf("%d%d", &m, &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i].pl), p[i].tp = 1, p[i].id = i; for (int j = 1; j <= n; j++) scanf("%d", &p[j + n].pl), p[j + n].tp = -1, p[j + n].id = j + n; sort(p + 1, p + 2 * n + 1); int l = 2 * n; for (int i = 1; i <= l; i++) { sm[i] = sm[i - 1] + p[i].tp; if (!fr[sm[i] + pls]) fr[sm[i] + pls] = i; } for (int i = l; i >= 1; i--) { nr[sm[i] + pls] = i; if (nr[sm[i - 1] + pls]) nx[i] = nr[sm[i - 1] + pls]; else { int fsum = -(sm[l] - sm[i - 1]); nx[i] = fr[fsum + pls]; } } long long ans = 0; for (int i = 1; i <= l; i++) { if (fl[i]) continue; int ncnt = 0; ns[ncnt++] = i; int pl = nx[i]; while (pl != i) { ns[ncnt++] = pl, pl = nx[pl]; } for (int j = 0; j < ncnt; j++) fl[ns[j]] = 1; long long nans = 1ll * m * 400005; for (int t = 0; t < 2; t++) { long long n1 = 0; for (int i = t; i < ncnt; i += 2) { int u = p[ns[i]].pl, v = p[ns[i + 1]].pl; if (i == ncnt - 1) v = p[ns[0]].pl; n1 += cal(u, v); } if (n1 < nans) { nans = min(nans, n1); for (int i = t; i < ncnt; i += 2) { int u = p[ns[i]].id, v = p[ns[i + 1]].id; if (i == ncnt - 1) v = p[ns[0]].id; mch(u, v); } } } ans += nans; } cout << ans << endl; for (int i = 1; i <= n; i++) printf("%d ", res[i]); return 0; }
9
#include <bits/stdc++.h> using namespace std; long long dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; long long diry[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long dx[] = {-1, 1, 0, 0}; long long dy[] = {0, 0, -1, 1}; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) a[i]--; vector<bool> vis(n, 0); int res[n]; memset(res, 0, sizeof(res)); for (int i = 0; i < n; i++) { if (vis[i]) continue; int k = i, j = a[i]; vis[i] = true; int len = 1; vector<int> temp; temp.push_back(i); while (j != k) { temp.push_back(j); vis[j] = true; j = a[j]; len++; } for (int i = 0; i < temp.size(); i++) { res[temp[i]] = len; } temp.clear(); } for (int i = 0; i < n; i++) { cout << res[i] << " "; } cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; long long int r; cin >> r; vector<long long int> l(n + 1), t(n + 1); long long int res = 0; for (int i = 0; i < n; i++) { cin >> l[i]; } for (int i = 0; i < n; i++) { cin >> t[i]; if (l[i] > t[i]) { cout << -1 << endl; return 0; } } for (int i = 0; i < n; i++) { l[i] *= 2; } vector<long long int> ans; long long int now = 0; for (int i = 0; i < n; i++) { if (t[i] >= l[i]) { now += l[i]; continue; } long long int k = max(0LL, (l[i] - t[i] + r - 1) / r); long long int pp = l[i] - t[i]; now += (t[i] - pp); long long int qq = t[i] - pp; t[i] -= qq; l[i] -= qq; if (res + k > 100000) { res += k; } else { for (int j = 0; j < k; j++) { ans.push_back(now); now += r; res++; } } long long int p = k * r * 2; p -= l[i]; i++; while (i < n && p >= l[i]) { p -= l[i]; i++; } l[i] -= p; t[i] -= p / 2; i--; } if (res > 100000) { cout << res << endl; return 0; } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; }
7
#include <bits/stdc++.h> using namespace std; char x[102400], y[102400]; int XN, YN; void reduce(char *cp) { for (cp += 2; *cp; cp++) { if (cp[0] == '1' && cp[-1] == '1') { cp[0] = '0'; cp[-1] = '0'; cp[-2] = '1'; cp -= 3; } } } int main() { char ans = '='; gets(x + 1); gets(y + 1); x[0] = '0'; y[0] = '0'; XN = strlen(x); YN = strlen(y); reduce(x); reduce(y); int xi = 0, yi = 0; if (XN > YN) { for (xi = 0; xi < XN - YN; xi++) if (x[xi] == '1') { ans = '>'; } } if (XN < YN) { for (yi = 0; yi < YN - XN; yi++) if (y[yi] == '1') { ans = '<'; } } if (ans == '=') { while (y[yi]) { if (x[xi] > y[yi]) { ans = '>'; break; } if (x[xi] < y[yi]) { ans = '<'; break; } xi++, yi++; } } std::cout << ans; }
4
#include <bits/stdc++.h> const int N = 1e6 + 7; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-7; using namespace std; int a[N], s[N]; map<int, int> asum, ssum; map<int, vector<int> > pos; map<int, int> size; int main() { int n, m, k, S; cin >> m >> k >> n >> S; for (int i = 1; i <= m; i++) { scanf("%d", a + i); asum[a[i]]++; } for (int i = 1; i <= S; i++) { scanf("%d", s + i); ssum[s[i]]++; } for (int i = 1; i <= m; i++) { if (ssum[a[i]] > 0) { pos[a[i]].push_back(i); } } for (auto x : ssum) { if (asum[x.first] < x.second) { puts("-1"); return 0; } } for (auto x : pos) { size[x.first] = x.second.size(); } int p, q, l, r, minl, maxr; bool f; bool flag = 1; for (int num = 1; num <= n; num++) { p = (num - 1) * k; f = 0; minl = INF; maxr = -1; for (auto x : pos) { q = upper_bound(x.second.begin(), x.second.end(), p) - x.second.begin(); if (q + ssum[x.first] > size[x.first]) { f = 1; break; } minl = min(minl, x.second[q]); maxr = max(maxr, x.second[q + ssum[x.first] - 1]); } if (f) break; if (m - maxr < (n - num) * k) continue; else { printf("%d\n", max(0, maxr - p - k)); int cnt = maxr - p; bool ff = 1; for (int i = p + 1; i <= maxr; i++) { if (cnt <= k) break; if (ssum[a[i]] == 0) { printf("%s%d", ff ? "" : " ", i); cnt--; ff = 0; } else { ssum[a[i]]--; } } cout << endl; flag = 0; break; } } if (flag) puts("-1"); return 0; }
5
#include <bits/stdc++.h> int main() { int n, ans = 0, a; int p[10001]; memset(p, 0, sizeof(p)); bool flag = true; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); p[a] = i + 1; } for (int j = 10000; j >= 1; j--) { if (p[j] > 0 && flag) { printf("%d ", p[j]); flag = false; } else if (p[j] > 0) { printf("%d\n", j); break; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long dp[505][505]; long long pw[505][505]; long long c[505][505]; long long fast(long long x, long long y, long long k) { long long s = 1; while (y) { if (y % 2) s = s * x % k; x = x * x % k; y = y >> 1; } return s; } long long inv(long long x) { return fast(x, 998244353 - 2, 998244353); } int32_t main() { c[0][0] = 1; for (long long i = 1; i <= 500; i++) { pw[i][0] = 1; for (long long j = 1; j <= 500; j++) { pw[i][j] = pw[i][j - 1] * i % 998244353; } c[i][0] = 1; for (long long j = 1; j <= 500; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 998244353; } } long long n, x; scanf("%lld%lld", &n, &x); for (long long i = 0; i <= x; i++) { dp[0][i] = 1; } for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= x; j++) { for (long long k = 0; k <= i; k++) { dp[i][j] += dp[i - k][max(0ll, j - (i - 1))] * pw[min(j, i - 1)][k] % 998244353 * c[i][k] % 998244353; dp[i][j] %= 998244353; } } } printf("%lld\n", dp[n][x]); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; const int inf = (1 << 30) - 1; const long long inf64 = ((long long)1 << 62) - 1; const long double pi = 3.1415926535897932384626433832795; template <class T> T sqr(T x) { return x * x; } template <class T> T abs(T x) { return x < 0 ? -x : x; } struct side { vector<pair<int, pair<int, int> > > have; vector<pair<int, int> > res; vector<pair<int, vector<pair<int, int> > > > e; int n, len; bool rv; void add(int x, int l, int r) { if (l > r) swap(l, r); have.push_back(make_pair(x, make_pair(l, r))); } void check(vector<pair<int, int> > &e, int x, int ll, int rs) { if ((ll ^ rs) > ll) return; int w = (ll ^ rs); int st = 0; int fin = 0; int now = 0; for (int i = 0; i < (int)(e.size()); i++) { if (e[i].second - e[i].first + now < w) { now += e[i].second - e[i].first; continue; } fin = e[i].first + w - now; puts("FIRST"); if (rv) { printf("%d %d %d %d", x, fin, x, len); } else { printf("%d %d %d %d", fin, x, len, x); } exit(0); } exit(1); } void find(int rs) { int cnt = n - res.size(); if (cnt > 0) { int w = 1, pos = 0; while (pos < res.size() && res[pos].first == w) { pos++; w++; } vector<pair<int, int> > q; q.push_back(make_pair(0, len)); check(q, w, len, rs); } for (int i = 0; i < (int)(e.size()); i++) { check(e[i].second, res[i].first, res[i].second, rs); } } void recalc() { sort(have.begin(), have.end()); for (int i = 0; i < (int)(have.size()); i++) { int now = have[i].second.first, pos = have[i].second.second; int j = i + 1; vector<pair<int, int> > e1; e1.push_back(make_pair(0, have[i].second.first)); while (j < have.size() && have[j].first == have[i].first) { int w = have[j].second.first; if (w <= pos) { pos = max(pos, have[j].second.second); } else { now += w - pos; e1.push_back(make_pair(pos, w)); pos = have[j].second.second; } j++; } now += len - pos; e1.push_back(make_pair(pos, len)); e.push_back(make_pair(have[i].first, e1)); res.push_back(make_pair(have[i].first, now)); i = j - 1; } } int getNim() { int nim = 0; int cnt = n - res.size(); if (cnt & 1) nim ^= len; for (int i = 0; i < (int)(res.size()); i++) nim ^= res[i].second; return nim; } }; side a, b; int n, m, k; int main() { cin >> n >> m >> k; a.n = n - 1; a.len = m; a.rv = 1; b.n = m - 1; b.len = n; b.rv = 0; for (int i = 0; i < (int)(k); i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); if (x1 == x2) a.add(x1, y1, y2); else b.add(y1, x1, x2); } a.recalc(); b.recalc(); int res = a.getNim() ^ b.getNim(); cerr << res << endl; if (res == 0) { puts("SECOND"); exit(0); } a.find(res); b.find(res); puts("SECOND"); return 0; }
8
#include <bits/stdc++.h> const int N = 20, M = 6, K = 5; int n, a[N]; int f[N][K], g[N][K]; struct Tuple { int a, b; Tuple() {} Tuple(int _a, int _b) : a(_a), b(_b) {} inline int sum() { return 7 * a + 4 * b; } }; std::vector<Tuple> tuple; void init() { for (int a = 0; a <= M; ++a) for (int b = 0; a + b <= M; ++b) tuple.push_back(Tuple(a, b)); } char s[M][N]; void print(int n, int k) { if (!n) return; const Tuple &t = tuple[g[n][k]]; for (int i = 0; i < t.a; ++i) s[i][n - 1] = '7'; for (int i = 0; i < t.b; ++i) s[t.a + i][n - 1] = '4'; for (int i = t.a + t.b; i < M; ++i) s[i][n - 1] = '0'; print(n - 1, f[n][k]); } void write(char *num) { int t = strlen(num); while (t && num[t - 1] == '0') --t; for (int i = t - 1; i >= 0; --i) putchar(num[i]); if (!t) putchar('0'); putchar(' '); } void solve() { memset(f, -1, sizeof f); f[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < K; ++j) { int cur = f[i][j]; if (cur == -1) continue; for (int k = 0; k < tuple.size(); ++k) { int t = tuple[k].sum() + j; if (t % 10 != a[i]) continue; f[i + 1][t / 10] = j; g[i + 1][t / 10] = k; } } } if (f[n][0] != -1) { memset(s, 0, sizeof s); print(n, 0); for (int j = 0; j < M; ++j) write(s[j]); putchar('\n'); } else { puts("-1"); } } int main() { init(); int tcase; for (scanf("%d", &tcase); tcase--;) { long long x; scanf("%lld", &x); n = 0; for (; x; x /= 10) a[n++] = x % 10; solve(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; reverse(s1.begin(), s1.end()); cout << (s2 == s1 ? "YES" : "NO"); return 0; }
0
#include <bits/stdc++.h> template <typename A, typename B> inline void umin(A &x, B y) { if (x > y) x = y; } template <typename A, typename B> inline void umax(A &x, B y) { if (x < y) x = y; } const int N = 200005; using namespace std; int a, b; long long cnt[N]; long long ans; int main() { cin >> a >> b; for (int i = 1; i <= b; i++) cnt[i % 5]++; for (int i = 1; i <= a; i++) for (int h = 0; h < 5; h++) if (((i % 5) + h) % 5 == 0) ans += cnt[h]; cout << ans; }
1
#include <bits/stdc++.h> using namespace std; const int MOD = (int)(1e9 + 7); const int N = 1111; long long f[N][N][4], result[N], C[N][N]; int n, kk; inline void add(long long &a, const long long &b) { a += b; if (a >= MOD) a -= MOD; } int main() { scanf("%d %d", &n, &kk); for (int i = 0; i <= n; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; } } f[0][0][1] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= n; j++) { for (int k = 0; k < 4; k++) for (int l = 0; l < 4; l++) { if ((l >> 1) != (k & 1)) continue; if ((k >> 1)) { if (j) add(f[i][j][k], f[i - 1][j - 1][l]); } else { add(f[i][j][k], f[i - 1][j][l]); if ((l & 1) == 0 && j) add(f[i][j][k], f[i - 1][j - 1][l]); } } } for (int i = n; i >= kk; i--) { for (int j = 0; j < 2; j++) add(result[i], f[n][i][j]); for (int j = 1; j <= n - i; j++) result[i] = (result[i] * j) % MOD; for (int j = n; j > i; j--) { add(result[i], (MOD - result[j] * C[j][i] % MOD)); } } printf("%I64d\n", result[kk]); return 0; }
9
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using ull = unsigned long long; using pII = pair<int, int>; using pLL = pair<ll, ll>; constexpr int mod = 1e9 + 7; template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; } template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; } template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; } inline int nextInt() { int x; cin >> x; return x; } void rd() {} template <class T, class... Ts> void rd(T &arg, Ts &...args) { cin >> arg; rd(args...); } void err() { cout << "\033[39;0m" << "\n"; } template <class T, class... Ts> void err(const T &arg, const Ts &...args) { cout << arg << ' '; err(args...); } template <template <typename...> class T, typename t, typename... A> void err(const T<t> &arg, const A &...args) { for (auto &v : arg) cout << v << ' '; err(args...); } void ptt() { cout << "\n"; } template <class T, class... Ts> void ptt(const T &arg, const Ts &...args) { cout << ' ' << arg; ptt(args...); } template <class T, class... Ts> void pt(const T &arg, const Ts &...args) { cout << arg; ptt(args...); } void pt() {} template <template <typename...> class T, typename t, typename... A> void pt(const T<t> &arg, const A &...args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); } inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; } constexpr int N = 1e6 + 10; int n, m, f[N], g[N]; char a[N], b[N], s[N]; struct ExKMP { int Next[N]; int extend[N]; void get_Next(char *s) { int lens = strlen(s + 1), p = 1, pos; Next[1] = lens; while (p + 1 <= lens && s[p] == s[p + 1]) ++p; Next[pos = 2] = p - 1; for (int i = 3; i <= lens; ++i) { int len = Next[i - pos + 1]; if (len + i < p + 1) Next[i] = len; else { int j = max(p - i + 1, 0); while (i + j <= lens && s[j + 1] == s[i + j]) ++j; p = i + (Next[pos = i] = j) - 1; } } } void work(char *s, char *t) { get_Next(t); int lens = strlen(s + 1), lent = strlen(t + 1), p = 1, pos; while (p <= lent && s[p] == t[p]) ++p; p = extend[pos = 1] = p - 1; for (int i = 2; i <= lens; ++i) { int len = Next[i - pos + 1]; if (len + i < p + 1) extend[i] = len; else { int j = max(p - i + 1, 0); while (i + j <= lens && j <= lent && t[j + 1] == s[i + j]) ++j; p = i + (extend[pos = i] = j) - 1; } } } } exkmp; struct BIT { ll a[N]; void init() { memset(a, 0, sizeof a); } int lowbit(int x) { return x & -x; } void add(int x, ll v) { ++x; for (int i = x; i < N; i += lowbit(i)) a[i] += v; } ll query(int x) { ++x; ll ret = 0; for (int i = x; i > 0; i -= lowbit(i)) ret += a[i]; return ret; } ll query(int l, int r) { if (l > r) return 0; return query(r) - query(l - 1); } } bit[2]; void run() { cin >> (a + 1) >> (b + 1) >> (s + 1); exkmp.get_Next(a); exkmp.work(a, s); memset(f, 0, sizeof f); for (int i = 1; i <= n; ++i) { f[i] = min(exkmp.extend[i], m - 1); } reverse(b + 1, b + 1 + n); reverse(s + 1, s + 1 + m); exkmp.get_Next(b); exkmp.work(b, s); memset(g, 0, sizeof g); for (int i = 1; i <= n; ++i) { g[n - i + 1] = min(exkmp.extend[i], m - 1); } bit[0].init(); bit[1].init(); ll ans = 0; int l = 1, r = 0; for (int i = 1; i <= n; ++i) { int _n = min(n, i + m - 2); while (r < _n) { bit[0].add(g[r + 1], 1); bit[1].add(g[r + 1], g[r + 1]); ++r; } while (l < i) { bit[0].add(g[l], -1); bit[1].add(g[l], -g[l]); ++l; } int x = f[i] - m + 1; ans += 1ll * x * bit[0].query(-x, m); ans += bit[1].query(-x, m); } pt(ans); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(20); while (cin >> n >> m) run(); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (true) { if (n == 1) { cout << "YES"; return 0; } if (n % 2 == 1) { cout << "NO"; return 0; } n = n / 2; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1 << 17; const int inf = 2e8 + 10; const int mod = 1000000007; long long n; int k; string s, a, b; int calc(string s, string p, int x = -1) { string a = p + "#" + s; if (x != -1) x += p.length() + 1; int n = a.length(); vector<int> z(n); int ans = 0; int l = 0, r = -1; for (int i = 1; i < n; i++) { if (i <= r) z[i] = min(z[i - l], r - i + 1); while (i + z[i] < n && a[z[i]] == a[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; if ((x == -1 || i < x && i + z[i] - 1 >= x) && z[i] == p.size()) ans++; } return ans; } int main() { cin >> n >> k; for (int i = 0; i < k; i++) { cin >> s; a = "a"; b = "b"; int j = 1; while (a.length() < s.length()) { string c = b + a; a = b; b = c; j++; } if (n < j) { cout << 0 << endl; continue; } vector<int> ans(n + 1); ans[j] = calc(a, s); if (j + 1 <= n) ans[j + 1] = calc(b, s); int cross[2], cur = 0; cross[0] = calc(b + a, s, b.length()); cross[1] = calc(b + a + b, s, b.length() + a.length()); j += 2; while (j <= n) { ans[j] = (ans[j - 1] + ans[j - 2] + cross[cur]) % mod; cur ^= 1; j++; } cout << ans[n] << endl; } return 0; }
8
#include <bits/stdc++.h> using namespace std; template <class T> int sgn(T x) { return (x > 0) - (x < 0); } template<class T> struct Point { typedef Point P; T x, y; explicit Point(T x_=0, T y_=0) : x(x_), y(y_) {} bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); } bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); } P operator+(P p) const { return P(x+p.x, y+p.y); } P operator-(P p) const { return P(x-p.x, y-p.y); } P operator*(T d) const { return P(x*d, y*d); } P operator/(T d) const { return P(x/d, y/d); } T dot(P p) const { return x*p.x + y*p.y; } T cross(P p) const { return x*p.y - y*p.x; } T cross(P a, P b) const { return (a-*this).cross(b-*this); } T dist2() const { return x*x + y*y; } double dist() const { return sqrt((double)dist2()); } double angle() const { return atan2(y, x); } P unit() const { return *this/dist(); } P perp() const { return P(-y, x); } P normal() const { return perp().unit(); } P rotate(double a) const { return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); } friend ostream& operator<<(ostream& os, P p) {return os << "(" << p.x << "," << p.y << ")"; }}; template<class P>pair<int, P> lineInter(P s1, P e1, P s2, P e2) {auto d = (e1 - s1).cross(e2 - s2);if (d == 0) return {-(s1.cross(e1, s2) == 0), P(0, 0)};auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);return {1, (s1 * p + e1 * q) / d};} template<class P>vector<P> circleLine(P c, double r, P a, P b) {P ab = b - a, p = a + ab * (c-a).dot(ab) / ab.dist2();double s = a.cross(b, c), h2 = r*r - s*s / ab.dist2(); if (h2 < 0) return {};if (h2 == 0) return {p};P h = ab.unit() * sqrt(h2);return {p - h, p + h};} using P = Point<double>; int main() { using namespace std;ios_base::sync_with_stdio(false), cin.tie(nullptr);int N; double L; cin >> N >> L;vector<P> pts(3*N); for (int i = 0; i < N; i++) cin >> pts[i].x >> pts[i].y;{ P center = pts[0]; double ang = (pts[0] - pts[N-1]).angle(); for (int i = 0; i < N; i++) {pts[i] = pts[i] - center; pts[i] = pts[i].rotate(-ang);}} for (int i = 0; i < N; i++) pts[2*N+i] = pts[N+i] = pts[i];for (int i = 0; i < N; i++) L -= (pts[i+1] - pts[i]).dist();assert(L > 0);int lo = 0;int hi = 1; P cur_st; while (true) { L += (pts[hi] - pts[hi-1]).dist(); double mi = 0; double ma = L; for (int z = 0; z < 50; z++) { double md = (mi + ma) / 2; if (md + (pts[hi] - P(md, 0)).dist() <= L) { mi = md; } else ma = md; }cur_st = P(mi, 0); if (cur_st.cross(pts[hi], pts[hi+1]) >= 0) break;else { hi++; continue; } } auto seg_val = [&](P a, P b) -> double {return (b.x - a.x) * (b.y + a.y) / 2;};double ans = 0;while (lo != N) { P center = (pts[lo] + pts[hi]) / 2; P dir = pts[lo] - pts[hi]; double ang = dir.angle(); double major = L / 2; double minor = sqrt(major * major - dir.dist2() / 4); auto transform_pt = [&](P p) -> P { p = p - center; p = p.rotate(-ang); p.x /= major; p.y /= minor; return p; }; auto untransform_pt = [&](P p) -> P { p.y *= minor; p.x *= major; p = p.rotate(ang); p = p + center; return p; }; auto untransform_area = [&](double d) -> double { return d * minor * major; }; auto get_circ = [&](P a, P b) -> P { vector<P> res = circleLine(P(0,0), 1, a, b); assert(!res.empty()); if (res.size() >= 2 && res[1].y > res[0].y) { swap(res[0], res[1]); } return res[0]; }; P plo = transform_pt(pts[lo]); P plo1 = transform_pt(pts[lo+1]); P phi = transform_pt(pts[hi]); P phi1 = transform_pt(pts[hi+1]); P st = transform_pt(cur_st); P cndlo = get_circ(plo, plo1); P cndhi = get_circ(phi, phi1); double alo = (cndlo*-1).perp().angle(); double ahi = (cndhi*-1).perp().angle(); double ast = (st*-1).perp().angle(); bool do_lo = hi+1 == lo+N || (lo+1 < hi && alo < ahi); P nxt_st = untransform_pt(do_lo ? cndlo : cndhi); double cur_val = untransform_area(((do_lo ? alo : ahi) - ast) / 2); cur_val += seg_val(nxt_st, center) + seg_val(center, cur_st); if (do_lo) { L -= (pts[lo+1] - pts[lo]).dist(); lo++; } else { L += (pts[hi+1] - pts[hi]).dist(); hi++; } cur_st = nxt_st; ans += cur_val; } cout << fixed << setprecision(20) << ans << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, h, d; cin >> n >> d >> h; if (d > 2 * h || (d == 1 && n != 2)) { cout << -1; return 0; } for (int i = 1; i <= h; ++i) cout << i << ' ' << i + 1 << '\n'; int x = 1; for (int i = 1; i <= d - h; ++i) { int y = h + 1 + i; cout << x << ' ' << y << '\n'; x = y; } for (int i = d + 2; i <= n; ++i) { cout << ((d == h) ? 2 : 1) << ' ' << i << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m; char temp[11][36]; string arr[11]; int arrc[11]; int goback(string &str, int cnt, int indx = 0) { if (cnt < 0 || ((indx == n) && cnt)) return 0; if (indx == n) { for (int i = 1; i < m; i++) { int c = 0; for (int j = 0; j < n; j++) if (str[j] == arr[i][j]) c++; if (c != arrc[i]) return 0; } return 1; } int ret = 0; str[indx] = (!(str[indx] - '0')) + '0'; ret += goback(str, cnt, indx + 1); str[indx] = (!(str[indx] - '0')) + '0'; ret += goback(str, cnt - 1, indx + 1); return ret; } int main() { int t = 1; while (t--) { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) scanf("%s%d", &temp[i], &arrc[i]); for (int i = 0; i < m; i++) arr[i] = temp[i]; printf("%d\n", goback(arr[0], arrc[0])); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long ans = 0, same = 0; for (long long i = 0; i < n - 1; i++) if (a[i + 1] < a[i]) { ans++; break; } for (long long i = 0; i < n - 1; i++) if (a[i + 1] == a[i]) { same++; break; } if (n % 2 == 0) cout << "YES" << endl; else if (ans || same) cout << "YES" << endl; else cout << "NO" << endl; } }
1
#include <bits/stdc++.h> void out(int n, int k) { printf("%d", n); while (k--) printf("0"); puts(""); } int a[200]; char s[110000]; int main() { scanf("%s", s); int numw = 0, i, j, cnt = 0; for (i = 0; s[i]; i++) { if (s[i] == '?') numw++; else if (s[i] >= 'A' && s[i] <= 'Z') a[s[i]]++; } for (i = 'A'; i <= 'Z'; i++) if (a[i]) cnt++; int ans = 1; if (cnt > 10) puts("0"); else { if (s[0] >= 'A') { ans = 9; cnt--; i = 9; while (cnt--) ans *= i, i--; } else { i = 10; while (cnt--) ans *= i, i--; } if (s[0] == '?') out(ans * 9, numw - 1); else out(ans, numw); } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; unordered_map<long long, long long> vis; long long ans = LLONG_MAX; for (long long i = 0; i < n; i++) { cin >> a[i]; if (vis.find(a[i]) != vis.end()) { ans = min(ans, i - vis[a[i]] + 1); } vis[a[i]] = i; } if (ans == LLONG_MAX) { ans = -1; } cout << ans << endl; } }
2
#include <bits/stdc++.h> //includes maximum header files using namespace std; using vi = vector<int>; //works like typedef using ll = long long; using vll = vector<long long>; using vd = vector<double>; using twodv = vector<vector<ll>>; #define f(m,n) for(int i = m; i < n; i++) #define vect(v) v.begin(),v.end() #define scan(a,n) vll a(n); f(0,n) cin >> a[i] #define print(a) f(0,a.size()) cout << a[i] << ' '; cout << endl; #define mod 1000000007; ll gcd(ll a,ll b){ if(b == 0) return a; return gcd(b,a % b); } ll lcm(ll a,ll b){ return a *(b/gcd(a,b)); } void solve(){ ll n; cin >> n; ll temp = 2*(n - (n/2)); ll ans = temp; ll l = lcm(1,2); ll i = 3; while(l <= n){ temp = i*((n/l)-(n/lcm(l,i))); l = lcm(l,i); ans += temp % mod; i++; } ans %= mod; cout << ans << endl; } int main(){ int t = 1; cin >> t; while(t--){ solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; const int MAXSEG = 1 << 18; int N, Q; int A[MAXN]; double seg[MAXSEG]; double mult[MAXSEG]; double add[MAXSEG]; void build_tree(int node, int left, int right) { if (left == right) { seg[node] = A[left]; return; } int mid = (left + right) / 2; build_tree(2 * node, left, mid); build_tree(2 * node + 1, mid + 1, right); seg[node] = seg[2 * node] + seg[2 * node + 1]; } void down(int node, int left, int right) { int mid = (left + right) / 2; seg[2 * node] = mult[node] * seg[2 * node] + add[node] * (mid - left + 1); seg[2 * node + 1] = mult[node] * seg[2 * node + 1] + add[node] * (right - mid); mult[2 * node] *= mult[node]; mult[2 * node + 1] *= mult[node]; add[2 * node] = mult[node] * add[2 * node] + add[node]; add[2 * node + 1] = mult[node] * add[2 * node + 1] + add[node]; mult[node] = 1; add[node] = 0; } void update_mult(int node, int left, int right, int ql, int qr, double val) { if (left > right || ql > qr || right < ql || qr < left) return; if (ql <= left && right <= qr) { seg[node] *= val; mult[node] *= val; add[node] *= val; return; } down(node, left, right); int mid = (left + right) / 2; update_mult(2 * node, left, mid, ql, qr, val); update_mult(2 * node + 1, mid + 1, right, ql, qr, val); seg[node] = seg[2 * node] + seg[2 * node + 1]; } void update_add(int node, int left, int right, int ql, int qr, double val) { if (left > right || ql > qr || right < ql || qr < left) return; if (ql <= left && right <= qr) { seg[node] += val * (right - left + 1); add[node] += val; return; } down(node, left, right); int mid = (left + right) / 2; update_add(2 * node, left, mid, ql, qr, val); update_add(2 * node + 1, mid + 1, right, ql, qr, val); seg[node] = seg[2 * node] + seg[2 * node + 1]; } double query(int node, int left, int right, int ql, int qr) { if (left > right || ql > qr || right < ql || qr < left) return 0; if (ql <= left && right <= qr) return seg[node]; down(node, left, right); int mid = (left + right) / 2; return query(2 * node, left, mid, ql, qr) + query(2 * node + 1, mid + 1, right, ql, qr); } int main() { scanf("%d %d", &N, &Q); for (int i = 1; i <= N; i++) scanf("%d", &A[i]); build_tree(1, 1, N); for (int i = 0; i < MAXSEG; i++) mult[i] = 1; for (int i = 0, type; i < Q; i++) { scanf("%d", &type); if (type == 1) { int l1, r1, l2, r2; scanf("%d %d %d %d", &l1, &r1, &l2, &r2); int len1 = r1 - l1 + 1; int len2 = r2 - l2 + 1; double sum1 = query(1, 1, N, l1, r1); double sum2 = query(1, 1, N, l2, r2); update_mult(1, 1, N, l1, r1, 1.0 * (len1 - 1) / len1); update_mult(1, 1, N, l2, r2, 1.0 * (len2 - 1) / len2); update_add(1, 1, N, l1, r1, sum2 / len1 / len2); update_add(1, 1, N, l2, r2, sum1 / len1 / len2); } else { int ql, qr; scanf("%d %d", &ql, &qr); printf("%.10f\n", query(1, 1, N, ql, qr)); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<int> edges[100005]; int num[100005], stp = -1; vector<pair<int, int> > output; int ans = 0; void dfs(int u, int p) { num[u] = ++stp; output.push_back({u, stp}); if (stp == ans) { stp = num[u] - 1 - (int)edges[u].size() + 1; output.push_back({u, stp}); } int cnt = 0; for (int v : edges[u]) { if (v == p) continue; dfs(v, u); ++cnt; output.push_back({u, stp}); if (u != 1 && stp == ans) { stp = num[u] - 1 - (int)edges[u].size() + cnt + 1; output.push_back({u, stp}); } } if (u != 1 && stp != num[u] - 1) { output.push_back({u, stp = num[u] - 1}); } ++stp; } int main() { int n; scanf("%d", &n); if (n == 1) { printf("1\n1 0\n"); return 0; } for (int i = (1); i <= (n - 1); i++) { int u, v; scanf("%d %d", &u, &v); edges[u].push_back(v); edges[v].push_back(u); } for (int i = (1); i <= (n); i++) ans = max(ans, (int)edges[i].size()); dfs(1, -1); printf("%d\n", (int)output.size()); for (auto u : output) { printf("%d %d\n", u.first, u.second); } return 0; }
9
#include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) typedef long long ll; const int N=55; int n,k,kb[N]; ll d,lim[N][2],c[N],f[N][N][N][4][4],ans; bool ck(int i,int l,int r) { fo(j,l+1,r-1) if(i+1>=kb[j]) return 0; return 1; } void Mn(ll&x,ll y) {if(x>y)x=y;} int main() { scanf("%d%d",&n,&k); ll d=1ll<<k; fo(i,1,n) scanf("%lld%lld",lim[i],lim[i]+1), kb[i]=log2((lim[i][0]+=d-1)^(lim[i][1]+=d+1)); lim[0][0]=lim[1][0], lim[0][1]=lim[1][1], kb[0]=kb[1]; lim[n+1][0]=lim[n][0], lim[n+1][1]=lim[n][0], kb[n+1]=kb[n]; fo(i,0,k-1) scanf("%lld",c+i); memset(f,63,sizeof f); k+=2; fo(i,0,n) fo(sl,0,1) fo(sr,0,1) f[k][i][i+1][sl][sr]=0; fd(i,k-1,0) fd(l,n,0) fo(r,l+1,n+1) fo(sl,0,3) fo(sr,0,3) { if(sl&2&&(i>=kb[l]||(lim[l][sl&1]>>i&1)^(sl&1))) continue; if(sr&2&&(i>=kb[r]||(lim[r][sr&1]>>i&1)^(sr&1))) continue; if(ck(i,l,r)) Mn(f[i][l][r][sl][sr],f[i+1][l][r][sl&1][sr&1]+(l&&r<=n&&(lim[l][sl&1]>>i&1^sl/2)^(lim[r][sr&1]>>i&1^sr/2))*c[i]); fo(m,l+1,r-1) fo(s,2,3) Mn(f[i][l][r][sl][sr],f[i][l][m][sl][s]+f[i][m][r][s][sr]); } ans=1e18; fo(sl,0,3) fo(sr,0,3) Mn(ans,f[0][0][n+1][sl][sr]); printf("%lld",ans); }
13
#include <bits/stdc++.h> using namespace std; bool f(int x, int y) { return x > y; } bool comp(int a, int b) { return (a < b); } int len(long long num) { int count = 0; while (num != 0) { count++; num /= 10; } return count; } long long int fact(long long int a, long long int b) { long long int j = 1, a1 = 2, b1 = 2; for (long long int i = 1; i <= a + b; i++) { j *= i; if (j % a1 == 0) { if (a1 <= a) { j /= a1; a1++; } } if (j % b1 == 0) { if (b1 <= b) { j /= b1; b1++; } } } return j; } void solve() { long long int n; cin >> n; long long int seven = n / 7, four = 0, k = 0; while (1) { four = (n - (7 * seven)) / 4; if (seven == 0) { if (n % 4 == 0) { for (long long int i = 0; i < n / 4; i++) cout << 4; } else cout << -1; return; } if (7 * seven + four * 4 != n) { seven--; } else { for (long long int i = 0; i < four; i++) cout << 4; for (long long int i = 0; i < seven; i++) cout << 7; return; } } if (n % 4 == 0) { for (long long int i = 0; i < n / 4; i++) cout << 4; } else cout << -1; return; } int main() { long long int n; for (long long int i = 0; i < 1; i++) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:255000000") bool firstout = 1; template <class T> T &minn(T &a, T b) { if (b < a) a = b; return a; } template <class T> T &maxx(T &a, T b) { if (a < b) a = b; return a; } int &madd(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } int &msub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; return a; } int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; } int mdiv(long long a, long long b, long long m) { a = (a % m + m) % m; b = (b % m + m) % m; if (a % b == 0) return a / b; return (a + m * mdiv(-a, m, b)) / b; } int n, m, q; int A[3012][3012], H[3012][3012], HH[3012][3012], V[3012][3012], VV[3012][3012]; int X[101234], Y[101234], XX[101234], YY[101234]; bool DD[3012][3012], DU[3012][3012], UD[3012][3012], UU[3012][3012]; int main() { int i, j, k; char c; int a, d; int ts; for (ts = 1; scanf("%" "d", &(n)) > 0; ++ts) { for (i = (0); i < (3012); ++i) for (j = (0); j < (3012); ++j) A[i][j] = H[i][j] = HH[i][j] = V[i][j] = VV[i][j] = DD[i][j] = DU[i][j] = UD[i][j] = UU[i][j] = 0; for (i = (0); i < (n); ++i) scanf( "%" "d", &(X[i])), scanf( "%" "d", &(Y[i])), scanf( "%" "d", &(XX[i])), scanf( "%" "d", &(YY[i])); for (k = (0); k < (n); ++k) { int x, y, xx, yy; x = X[k]; y = Y[k]; xx = XX[k]; yy = YY[k]; for (i = (x); i < (xx); ++i) for (j = (y); j < (yy); ++j) A[i + 1][j + 1] = 1; DD[x][y] = 1; UD[xx][y] = 1; DU[x][yy] = 1; UU[xx][yy] = 1; for (i = (x); i < (xx); ++i) H[i + 1][y] = 1, HH[i + 1][yy] = 1; for (j = (y); j < (yy); ++j) V[x][j + 1] = 1, VV[xx][j + 1] = 1; } for (i = (1); i < (3012); ++i) for (j = (1); j < (3012); ++j) A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; for (j = (0); j < (3012); ++j) for (i = (1); i < (3012); ++i) H[i][j] += H[i - 1][j], HH[i][j] += HH[i - 1][j]; for (i = (0); i < (3012); ++i) for (j = (1); j < (3012); ++j) V[i][j] += V[i][j - 1], VV[i][j] += VV[i][j - 1]; int ii, jj, l; for (i = (0); i < (3012); ++i) { for (j = (0); j < (3012); ++j) if (DD[i][j]) { for (jj = (j + 1); jj < (3012); ++jj) if (DU[i][jj]) { l = jj - j; ii = i + l; if (UD[ii][j] && UU[ii][jj] && V[i][jj] - V[i][j] == l && VV[ii][jj] - VV[ii][j] == l && H[ii][j] - H[i][j] == l && HH[ii][jj] - HH[i][jj] == l && A[ii][jj] - A[i][jj] - A[ii][j] + A[i][j] == ((l) * (l))) break; } if (jj < 3012) break; } if (j < 3012) break; } if (i == 3012) printf( "%" "s", ("NO")), printf("\n"), firstout = 1; else { printf((firstout) ? "%" "s" : " %" "s", ("YES")), firstout = 0; int res = 0; for (k = (0); k < (n); ++k) if (i <= X[k] && j <= Y[k] && XX[k] <= ii && YY[k] <= jj) ++res; printf((firstout) ? "%" "d" : " %" "d", (res)), firstout = 0; printf("\n"), firstout = 1; for (k = (0); k < (n); ++k) if (i <= X[k] && j <= Y[k] && XX[k] <= ii && YY[k] <= jj) printf((firstout) ? "%" "d" : " %" "d", (k + 1)), firstout = 0; printf("\n"), firstout = 1; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long p = 1000003LL; long long n, c; long long power(long long x, long long n) { long long ret = 1LL; while (n > 0LL) { if (n & 1LL) ret = ret * x % p; x = x * x % p; n >>= 1LL; } return ret; } long long ans; int main() { cin >> n >> c; ans = 1LL; for (long long i = n + 1LL; i <= n + c; i++) { ans = (ans * i) % p; } for (long long i = 2LL; i <= c; i++) { ans = (ans * power(i, p - 2)) % p; } cout << ans - 1LL << endl; }
5
#include <bits/stdc++.h> const int MAXN = 100005; using namespace std; void goodbye() { cout << "NO\n"; exit(EXIT_SUCCESS); } void check_occ(const string &s) { int occ[26] = {0}; for (char c : s) { occ[c - 'a']++; if (occ[c - 'a'] > 1) { goodbye(); } } } string merge(string s1, string s2) { int occ[26]; fill(occ, occ + 26, -1); for (int i = 0; i < s1.size(); i++) { occ[s1[i] - 'a'] = i; } int pos1 = -1, pos2 = -1; for (int i = 0; i < s2.size(); i++) { if (occ[s2[i] - 'a'] != -1) { pos1 = occ[s2[i] - 'a']; pos2 = i; break; } } if (pos1 == -1) { return string(); } if (pos1 < pos2) { swap(pos1, pos2); swap(s1, s2); } for (int i = 0; pos1 - i >= 0 && pos2 - i >= 0; i++) { if (s1[pos1 - i] != s2[pos2 - i]) { goodbye(); } } for (int i = 0; pos1 + i < s1.size() && pos2 + i < s2.size(); i++) { if (s1[pos1 + i] != s2[pos2 + i]) { goodbye(); } } string res = s1; if (s1.size() - pos1 >= s2.size() - pos2) { return s1; } for (int i = pos2 + (s1.size() - pos1); i < s2.size(); i++) { res.push_back(s2[i]); } check_occ(res); return res; } int N; vector<string> vec; void add(string s) { for (int i = 0; i < vec.size(); i++) { string m = merge(s, vec[i]); if (!m.empty()) { swap(vec[i], vec.back()); vec.pop_back(); add(m); return; } } vec.push_back(s); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; i++) { string s; cin >> s; check_occ(s); add(s); } sort(vec.begin(), vec.end()); for (string s : vec) { cout << s; } cout << '\n'; }
6
#include <bits/stdc++.h> using namespace std; char s[1000005]; int main() { int i, n, tots = 0, mx = 0, ans = 0, totone; gets(s); n = strlen(s); for ((i) = 0; (i) < (n); (i)++) { totone = 0; tots++; while (i < n && s[i] == '1') { i++; totone++; } mx = max(totone, mx); if (i == n || (i == n - 1 && s[i] == '0') || (s[i] == '0' && s[i + 1] == '0')) { if (mx == 1) ans += tots; else ans += tots + 1; tots = 0; mx = 0; while (s[i] == '0') i++; i--; } } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int seg[2000001][2], lzy[2000001], fib[200001], M; void update(int idx, int s, int e) { if (s == e) { lzy[idx] = 0; return; } int v1 = (seg[idx * 2 + 1][0] * fib[lzy[idx] - 1] + seg[idx * 2 + 1][1] * fib[lzy[idx]]) % M; int v2 = (seg[idx * 2 + 1][0] * fib[lzy[idx]] + seg[idx * 2 + 1][1] * fib[lzy[idx] + 1]) % M; seg[idx * 2 + 1][0] = v1; seg[idx * 2 + 1][1] = v2; lzy[idx * 2 + 1] += lzy[idx]; v1 = (seg[idx * 2 + 2][0] * fib[lzy[idx] - 1] + seg[idx * 2 + 2][1] * fib[lzy[idx]]) % M; v2 = (seg[idx * 2 + 2][0] * fib[lzy[idx]] + seg[idx * 2 + 2][1] * fib[lzy[idx] + 1]) % M; seg[idx * 2 + 2][0] = v1; seg[idx * 2 + 2][1] = v2; lzy[idx * 2 + 2] += lzy[idx]; lzy[idx] = 0; } void insert1(int idx, int s, int e, int p, int v1, int v2) { if (lzy[idx]) { update(idx, s, e); } if (s == e) { seg[idx][0] = v1; seg[idx][1] = v2; return; } int mid = (s + e) / 2; if (p <= mid) insert1(idx * 2 + 1, s, mid, p, v1, v2); else insert1(idx * 2 + 2, mid + 1, e, p, v1, v2); for (int i = (0); i < (2); ++i) seg[idx][i] = (seg[idx * 2 + 1][i] + seg[idx * 2 + 2][i]) % M; } void insert2(int idx, int s, int e, int st, int ed) { if (st > ed) return; if (lzy[idx]) { update(idx, s, e); } if (s == st && e == ed) { int v = (seg[idx][0] + seg[idx][1]) % M; seg[idx][0] = seg[idx][1]; seg[idx][1] = v; lzy[idx]++; return; } int mid = (s + e) / 2; if (ed <= mid) insert2(idx * 2 + 1, s, mid, st, ed); else if (st > mid) insert2(idx * 2 + 2, mid + 1, e, st, ed); else { insert2(idx * 2 + 1, s, mid, st, mid); insert2(idx * 2 + 2, mid + 1, e, mid + 1, ed); } for (int i = (0); i < (2); ++i) seg[idx][i] = (seg[idx * 2 + 1][i] + seg[idx * 2 + 2][i]) % M; } pair<int, int> query1(int idx, int s, int e, int st, int ed) { if (lzy[idx]) { update(idx, s, e); } if (st > ed) return make_pair(0, 0); if (s == st && e == ed) return make_pair(seg[idx][0], seg[idx][1]); int mid = (s + e) / 2; if (ed <= mid) return query1(idx * 2 + 1, s, mid, st, ed); else if (st > mid) return query1(idx * 2 + 2, mid + 1, e, st, ed); else { pair<int, int> pl = query1(idx * 2 + 1, s, mid, st, mid); pair<int, int> pr = query1(idx * 2 + 2, mid + 1, e, mid + 1, ed); return make_pair((pl.first + pr.first) % M, (pl.second + pr.second) % M); } } int seg2[2000001]; void insert(int idx, int s, int e, int p) { if (s == e) { seg2[idx]++; return; } int mid = (s + e) / 2; if (p <= mid) insert(idx * 2 + 1, s, mid, p); else insert(idx * 2 + 2, mid + 1, e, p); seg2[idx]++; } int query(int idx, int s, int e, int st, int ed) { if (st > ed) return 0; if (s == st && e == ed) return seg2[idx]; int mid = (s + e) / 2; if (ed <= mid) return query(idx * 2 + 1, s, mid, st, ed); else if (st > mid) return query(idx * 2 + 2, mid + 1, e, st, ed); else { return query(idx * 2 + 1, s, mid, st, mid) + query(idx * 2 + 2, mid + 1, e, mid + 1, ed); } } pair<int, int> pp[300001]; vector<pair<int, int> > vc[300001]; int res[300001]; map<int, int> mm; int ar[300001], val[300001]; int on[300001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n >> M; for (int i = (0); i < (n); ++i) { cin >> ar[i]; mm[ar[i]] = 1; } int cnt = 0; for (auto &v : mm) { v.second = ++cnt; val[v.second] = v.first % M; } for (int i = (0); i < (n); ++i) { ar[i] = mm[ar[i]]; } fib[0] = 0; fib[1] = 1 % M; for (int i = 2; i <= 200000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % M; int SZ = 250; int q; cin >> q; for (int i = (0); i < (q); ++i) { int l, r; cin >> l >> r; l--; r--; pp[i] = make_pair(l, r); if ((l / SZ) == (r / SZ)) { set<int> S; for (int j = l; j <= r; j++) S.insert(ar[j]); int cc = 0; for (auto &v : S) { res[i] = (res[i] + val[v] * fib[++cc]) % M; } continue; } vc[r].push_back(make_pair(l, i)); } for (int i = 0; i < n; i += SZ) { memset(on, 0, sizeof on); memset(seg, 0, sizeof seg); memset(seg2, 0, sizeof seg2); for (int j = i + SZ; j < n; j++) { if (on[ar[j]] == 0) { on[ar[j]] = 1; int pre = query(0, 1, cnt, 1, ar[j] - 1) + 1; insert(0, 1, cnt, ar[j]); insert1(0, 1, cnt, ar[j], (val[ar[j]] * fib[pre - 1]) % M, (val[ar[j]] * fib[pre]) % M); insert2(0, 1, cnt, ar[j] + 1, cnt); } for (int k = (0); k < (vc[j].size()); ++k) { if (vc[j][k].first < i || vc[j][k].first >= i + SZ) continue; set<int> S; for (int l = i + SZ - 1; l >= vc[j][k].first; l--) { if (on[ar[l]] || S.count(ar[l])) continue; S.insert(ar[l]); } int cc = 0, pre = 0, tot = 0; for (auto &v : S) { pair<int, int> p = query1(0, 1, cnt, pre + 1, v); int pcnt = query(0, 1, cnt, pre + 1, v); int vv = (p.first * fib[cc] + p.second * fib[cc + 1]) % M; pre = v; tot += pcnt + 1; cc++; vv = (vv + val[v] * fib[tot]) % M; res[vc[j][k].second] = (res[vc[j][k].second] + vv) % M; } pair<int, int> p = query1(0, 1, cnt, pre + 1, cnt); int vv = (p.first * fib[cc] + p.second * fib[cc + 1]) % M; res[vc[j][k].second] = (res[vc[j][k].second] + vv) % M; } } } for (int i = (0); i < (q); ++i) cout << res[i] << "\n"; }
11
#include <bits/stdc++.h> using namespace std; void yes() { cout << "YES" << "\n"; } void no() { cout << "NO" << "\n"; } int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; const long long INF = 8e18 + 8; const double EPS = 1e-9; const long long MOD = 1e9 + 7; void solve() { int n, d; cin >> n >> d; long long m; cin >> m; vector<long long> less, more; for (int i = 0; i < n; i++) { long long x; cin >> x; if (x > m) more.push_back(x); else less.push_back(x); } sort((less).begin(), (less).end()); sort((more).begin(), (more).end()); reverse((more).begin(), (more).end()); long long sum = 0; for (int i = 0; i < (int)((less).size()); i++) sum += less[i]; if (more.empty()) { cout << sum << "\n"; return; } int j = 0; long long mx = sum; for (int i = 0; i < (int)((more).size()); i++) { sum += more[i]; int mezhdu = i * d; int fromless = i + 1 + mezhdu - (int)((more).size()); while (j < (int)((less).size()) and j < fromless) { sum -= less[j]; j++; } if (fromless > j) break; mx = max(mx, sum); } cout << mx << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); bool tests = 0; int t = 1; if (tests) cin >> t; for (int i = 1; i <= t; i++) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int oo = (int)1e9; const double PI = 2 * acos(0.0); const double eps = 1e-9; double large = 1000000000000000000.; double primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; int n; double power(double x, int p) { double ans = 1ll; for (int i = 0; i < p; ++i) { ans *= x; if (ans > large) return large + 1.; } return ans; } double ans; vector<int> powers; void f(int d) { if (d == 1) { vector<int> v = powers; double tmpans = 1.; for (int i = 0; i < (int)powers.size(); ++i) { tmpans *= power(primes[i], powers[i]); if (tmpans > large) { return; } } ans = min(ans, tmpans); return; } for (int i = 2; i <= d; ++i) { if (d % i == 0) { powers.push_back(i - 1); f(d / i); powers.pop_back(); } } } int main() { cin >> n; ans = large + 2; f(n); cout << (long long)ans << endl; return 0; }
6
#include<bits/stdc++.h> #define ll long long #define eb emplace_back #define fi first #define se second #define pii pair<int,int> using namespace std; const int M=3e5+9; int n,m,id=0; ll ans=0; int s1[M],s2[M],fat[M],lid[M],dep[M],f[M<<1][29]; vector<int>g[M],d[M]; vector<pii>p[M]; map<int,int>mp[M]; void dfs(int u,int fa){ dep[f[lid[u]=++id][0]=u]=dep[fat[u]=fa]+1; d[u].eb(id); for(auto v:g[u]) if(v!=fa)dfs(v,u),f[++id][0]=u,d[u].eb(id); } int Min(int u,int v){return dep[u]<dep[v]?u:v;} int lca(int u,int v){ int l=lid[u],r=lid[v],k=log2(r-l+1); return Min(f[l][k],f[r-(1<<k)+1][k]); } int find(int u,int v){ if(u==v)return -1; int l=lid[u],r=lid[v]; int x=lower_bound(d[u].begin(),d[u].end(),lid[v])-d[u].begin()-1; x=d[u][x]; return f[x+1][0]; } void DFS(int u){ for(auto v:g[u]) if(v!=fat[u]){ DFS(v); s1[u]+=s1[v]; s2[u]+=s2[v]; } int sum=s2[u]; for(auto o:p[u]){ int x=o.fi,y=o.se; ans+=--sum; if(y!=-1)ans-=(--s1[y]); if(x!=-1)ans-=(--s1[x]),ans+=mp[x][y]++; } } int main(){ scanf("%d",&n); for(int i=1,u,v;i<n;++i){ scanf("%d%d",&u,&v); g[u].eb(v); g[v].eb(u); } dfs(1,0); for(int j=1;j<=23;++j){ for(int i=1;i+(1<<j)-1<=id;++i){ f[i][j]=Min(f[i][j-1],f[i+(1<<j-1)][j-1]); } } scanf("%d",&m); for(int i=1,u,v,w;i<=m;++i){ scanf("%d%d",&u,&v); if(lid[u]>lid[v])swap(u,v); w=lca(u,v); s1[u]++;s1[v]++;s1[w]-=2; s2[u]++;s2[v]++;s2[w]--;s2[fat[w]]--; u=find(w,u); v=find(w,v); p[w].eb(pii{u,v}); } DFS(1); printf("%lld\n",ans); return 0; } /* */
9
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int x[n], y[n], t[n]; double p[n]; for (int i = 0; i < n; ++i) scanf("%d%d%d%lf", x + i, y + i, t + i, p + i); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (t[j] < t[i]) std::swap(x[i], x[j]), std::swap(y[i], y[j]), std::swap(t[i], t[j]), std::swap(p[i], p[j]); double best[n]; for (int i = n - 1; i >= 0; --i) { best[i] = p[i]; for (int j = i + 1; j < n; ++j) if (t[j] - t[i] >= hypot(x[i] - x[j], y[i] - y[j])) best[i] = std::max(best[i], p[i] + best[j]); } for (int i = 1; i < n; ++i) if (best[i] > best[0]) best[0] = best[i]; printf("%.9lf\n", best[0]); }
5
#include <bits/stdc++.h> using namespace std; long long t, n, m, k, a[300010], q, l, r, x, o; long long suf[200010]; long long fac[30]; void build() { for (long long i = n; i >= 1; i--) { a[i] = i; suf[i] = suf[i + 1] + a[i]; } fac[0] = 1; for (long long i = 1; i <= 15; i++) { fac[i] = fac[i - 1] * i; } } void update() { long long x = t + 1; vector<long long> v; for (long long i = max(1ll, n - 15); i <= n; i++) { v.push_back(i); } for (long long i = max(1ll, n - 15); i <= n; i++) { a[i] = v[(x - 1) / (fac[(n - i)])]; v.erase(v.begin() + (x - 1) / fac[n - i]); x -= ((x - 1) / fac[(n - i)]) * fac[n - i]; } for (long long i = n; i >= (max(n - 15, 1ll)); i--) { suf[i] = suf[i + 1] + a[i]; } } void check() { cin >> n >> q; build(); while (q--) { long long x; cin >> x; t = x; update(); for (long long i = 1; i <= n; i++) { cout << a[i] << " "; } cout << "\n"; } } int32_t main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin >> n >> q; build(); suf[n + 1] = 0; while (q--) { cin >> o; if (o == 1) { cin >> l >> r; cout << (suf[l] - suf[r + 1]) << "\n"; } else { cin >> x; t += x; update(); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long p[n], c[n]; for (long long i = 0; i < n; i++) cin >> p[i] >> c[i]; if (p[0] >= c[0]) { long long flag = 0; for (long long i = 1; i < n; i++) { if ((p[i] >= c[i]) && (p[i] >= p[i - 1]) && (c[i] >= c[i - 1]) && ((p[i] - p[i - 1]) >= (c[i] - c[i - 1]))) continue; else { flag = 1; break; } } if (flag) cout << "NO" << endl; else cout << "YES" << endl; } else cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x[n]; int y[n]; bool r, l, u, d; r = false; l = false; d = false; u = false; int count = 0; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { int X = x[i]; int Y = y[i]; for (int j = 0; j < n; j++) { if (j != i) { int x1 = x[j]; int y1 = y[j]; if (X == x1) { if (Y < y1) { u = true; } else if (Y > y1) { d = true; } } else if (Y == y1) { if (X < x1) { r = true; } else if (X > x1) { l = true; } } } } if (u && d && l && r) count++; r = false; l = false; d = false; u = false; } cout << count << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long A, long long B) { return A > B ? gcd(B, A) : A ? gcd(B % A, A) : B; } long long lcm(long long A, long long B) { return A / gcd(A, B) * B; } long long pow(long long A, long long B, long long P) { if (!B) return 1 % P; long long h = pow(A, B / 2, P); h *= h; h %= P; if (B % 2) h *= A; return h % P; } pair<int, int> P[262145]; vector<int> g[262145], c[262145]; int lp[1048577]; vector<int> pr; void get_pr(int N) { for (int i = 2; i <= N; i++) { if (lp[i] == 0) { pr.push_back(i); lp[i] = i; } for (int j = 0; j < pr.size() && pr[j] <= lp[i] && pr[j] * i <= N; j++) lp[pr[j] * i] = pr[j]; } } int tst; void get_ready() { tst = 1; } string D[6]; int k, i; int main() { get_ready(); while (tst--) { D[0] = "+------------------------+"; D[1] = "|#.#.#.#.#.#.#.#.#.#.#.|D|)"; D[2] = "|#.#.#.#.#.#.#.#.#.#.#.|.|"; D[3] = "|#.......................|"; D[4] = "|#.#.#.#.#.#.#.#.#.#.#.|.|)"; D[5] = "+------------------------+"; cin >> k; for (i = 1; i <= k; i++) { if (i <= 4) { D[i][1] = 'O'; } else { int z = i - 4; int k = z % 3; z = (z + 2) / 3; if (k == 1) D[1][z * 2 + 1] = 'O'; else if (k == 2) D[2][z * 2 + 1] = 'O'; else D[4][z * 2 + 1] = 'O'; } } cout << D[0] << endl << D[1] << endl << D[2] << endl << D[3] << endl << D[4] << endl << D[5] << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int p = (n - k); cout << a[p]; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) { int n, t, r = 0; cin >> n >> t; bool f = true; for (int i = 0; i < n; i++) { int a; cin >> a; if (!i) { r += a; continue; } if (f) { int q = t / i; if (q >= a) r += a, t -= i * a; else r += q, t -= i * q; if (!t || t < i) f = false; } } cout << r << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using papers = bitset<20000>; const int N = 200; int n, m; papers p[N + 1]; unordered_map<papers, vector<int>> g; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> n; m = n * (n - 1) / 2; if (n == 2) { for (int i = 0; i < m; i++) { int k, j; cin >> k >> j; k--; cout << "1 " << j << endl; cout << k; while (k-- > 0) cin >> j, cout << ' ' << j; cout << endl; } return 0; } for (int i = 0; i < m; i++) { int k, j; cin >> k; while (k-- > 0) cin >> j, p[j][i] = true; } for (int i = 1; i <= 200; i++) { if (p[i].any()) g[p[i]].push_back(i); } for (auto& kv : g) { auto& ans = kv.second; cout << ans.size(); for (int x : ans) cout << ' ' << x; cout << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100100, mod = 1e9 + 7; char s[N]; int cnt[200]; int main() { int n, mx = 0, c = 0; cin >> n; scanf("%s", s); for (int i = 0; s[i]; i++) { cnt[s[i]]++; mx = max(mx, cnt[s[i]]); } if (cnt['A'] == mx) c++; if (cnt['G'] == mx) c++; if (cnt['T'] == mx) c++; if (cnt['C'] == mx) c++; long long res = 1; for (int i = 1; i <= n; i++) (res *= c) %= mod; cout << res << endl; }
3
#include <bits/stdc++.h> using namespace std; long long a[1000005], d[1000006]; int main() { ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; memset(d, 0, sizeof(d)); for (int i = 1; i <= n; i++) { cin >> a[i]; d[a[i]]++; } int res = 0, vt = 0; for (int i = 0; i <= 1e6; i++) { vt += d[i]; if (i >= m) { vt -= d[i - m]; } if (vt >= k) { res += (vt - (k - 1)); d[i] -= (vt - (k - 1)); vt = k - 1; } } cout << res; }
4
#include <bits/stdc++.h> using namespace std; int R, N; double result; int main() { cin >> N >> R; for (int i = 1; i <= N; ++i) { result += 2; if (i - 1 >= 1) result += 2 + sqrt(2); if (i - 2 >= 1) { result += 1.0 * (i - 2) * (2 + 2 * sqrt(2)); result += 1.0 * (i - 2) * (i - 3); } if (N - i >= 1) result += 2 + sqrt(2); if (N - i - 1 >= 1) { result += 1.0 * (N - i - 1) * (2 + 2 * sqrt(2)); result += 1.0 * (N - i - 1) * (N - i - 2); } } result /= 1.0 * N * N; cout << fixed << setprecision(8) << result * R << '\n'; }
5
#include <bits/stdc++.h> using namespace std; struct Tpoint { double x, y; Tpoint() {} Tpoint(double _x, double _y) { x = _x; y = _y; } inline void read() { scanf("%lf%lf", &x, &y); } inline void show() { printf("%lf %lf\n", x, y); } inline double norm() { return sqrt(((x) * (x)) + ((y) * (y))); } }; inline Tpoint operator+(const Tpoint &a, const Tpoint &b) { return Tpoint(a.x + b.x, a.y + b.y); } inline Tpoint operator-(const Tpoint &a, const Tpoint &b) { return Tpoint(a.x - b.x, a.y - b.y); } inline Tpoint operator*(const Tpoint &a, const double &b) { return Tpoint(a.x * b, a.y * b); } inline Tpoint operator/(const Tpoint &a, const double &b) { return Tpoint(a.x / b, a.y / b); } inline double det(const Tpoint &a, const Tpoint &b) { return a.x * b.y - a.y * b.x; } inline double dot(const Tpoint &a, const Tpoint &b) { return a.x * b.x + a.y * b.y; } struct node { long long a, b; int c, id; inline void read() { scanf("%I64d%I64d%d", &a, &b, &c); } } a[55]; inline bool cmp(const node &a, const node &b) { return a.c < b.c; } long long f[55][55][105]; struct Tpre { short i, k; } pre[55][55][105]; int n, m, k; inline void print(int i, int j, int k) { if (j != 1) print(pre[i][j][k].i, j - 1, pre[i][j][k].k); printf("%d %I64d\n", a[i].id, a[i].a + k); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; ++i) { a[i].read(); a[i].id = i + 1; } sort(a, a + m, cmp); memset(f, -1, sizeof(f)); for (int i = 0; i < m; ++i) { for (int j = 0; j <= a[i].b - a[i].a; ++j) f[i][1][j] = a[i].a + j; for (int j = 0; j < i; ++j) if (a[i].c > a[j].c) { for (int sel = 0; sel < n; ++sel) for (int u = 0; u <= a[j].b - a[j].a; ++u) if (f[j][sel][u] != -1) { long long cur = f[j][sel][u]; long long v; if (a[j].a + u <= a[i].b - k) { v = (a[j].a + u) + k; if (v >= a[i].a && v <= a[i].b) { int now = v - a[i].a; long long &res = f[i][sel + 1][now]; if (res < cur + v) { res = cur + v; pre[i][sel + 1][now].i = j; pre[i][sel + 1][now].k = u; } } } if (a[j].a + u <= a[i].b / k) { v = (a[j].a + u) * k; if (v <= a[i].b && v >= a[i].a) { int now = v - a[i].a; long long &res = f[i][sel + 1][now]; if (res < cur + v) { res = cur + v; pre[i][sel + 1][now].i = j; pre[i][sel + 1][now].k = u; } } } } } } long long ans = -1; for (int i = 0; i < m; ++i) for (int j = 0; j <= a[i].b - a[i].a; ++j) if (f[i][n][j] != -1) { if (f[i][n][j] > ans) ans = f[i][n][j]; } if (ans == -1) { puts("NO"); return 0; } puts("YES"); for (int i = 0; i < m; ++i) for (int j = 0; j <= a[i].b - a[i].a; ++j) if (f[i][n][j] == ans) { print(i, n, j); return 0; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; int allin = 0; int in = 0; int folded = 0; unsigned char c; cin >> n; for (int i = 0; i < n; i++) { cin >> c; if (c == 'A') allin++; else if (c == 'F') folded++; else in++; } if (allin == 0 && folded == 0) { cout << 0 << endl; return 0; } if (in == 1) { cout << 1 << endl; return 0; } if (in > 1) { cout << 0 << endl; return 0; } cout << n - folded << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 5; char s[N]; int main() { int n; scanf("%d", &n); scanf("\n%s", s + 1); int p = 1, cnt = 0; if (n & 1) { printf("%c", s[1]); p++; } while (p <= n) { cnt++; if (cnt & 1 && cnt != 1) printf("-"); printf("%c", s[p++]); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, k, x; bitset<512> B[512]; int main() { for (cin >> n >> k, B[0][0] = 1; cin >> x;) for (int i = k; i >= x; i--) if (B[i - x].any()) B[i] = (B[i - x] << x) | B[i] | B[i - x]; cout << B[k].count() << endl; for (int i = 0; i <= k; i++) if (B[k][i]) cout << i << " "; }
5
#include <bits/stdc++.h> using namespace std; int main() { int st, nd, cnt = 0, n, female[5500] = {0}, male[5500] = {0}; char gen; cin >> n; for (int i = 0; i < n; i++) { cin >> gen; cin >> st >> nd; for (int j = st; j <= nd; j++) { if (gen == 'M') male[j]++; else female[j]++; } } for (int i = 0; i < 370; i++) { if (cnt < min(male[i], female[i])) cnt = min(male[i], female[i]); else continue; } cout << cnt * 2; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a = 0; cin >> n >> m; while (m >= 2 && n >= 3) if (n > m + 1) { n -= 3; m -= 1; a += 6; } else { n -= 2; m -= 2; a += 6; } if (n >= 3 || m == 0) a += 2 * n; else if (m >= 2 || n <= 1) a += 3 * m; else a += 4; cout << a << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll OO = 1e18; int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; string ys = "YES", no = "NO"; const long double dgr = acos(-1) / 180, dg = 180 / acos(-1); const int mod = 1e8 + 7, N = 1e7, M = 2001; vector<ll> v; void solve(ll m, int i = 0) { if (!m) exit(puts("YES")); if (i == v.size()) return; solve(m, i + 1); solve(m + v[i], i + 1); solve(m - v[i], i + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; ll w, m; cin >> w >> m; if (w == 2 || w == 3) return cout << ys << '\n', 0; v.push_back(1); while (v.back() * w <= 9e9 + 99) v.push_back(v.back() * w); solve(m, 0); cout << no << '\n'; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 5111; const long long base = 10000000; const int mod = 1000000007; template <typename T> T ABS(T n) { return n > 0 ? n : -n; } long long convert(long long x, long long y, long long dx, long long dy) { return (x << 22) | (y << 2) | (dx << 1) | (dy); } set<long long> h1, h2; long long n, m; long long code(long long x, long long y, long long dx, long long dy) { long long tx, ty; long long a, b; if (dx) { tx = n - x; if (dy) { ty = m - y; if (tx > ty) a = x + ty, b = m, dy ^= 1; else if (tx < ty) a = n, b = y + tx, dx ^= 1; else a = n, b = m, dx ^= 1, dy ^= 1; } else { ty = y - 1; if (tx > ty) a = x + ty, b = 1, dy ^= 1; else if (tx < ty) a = n, b = y - tx, dx ^= 1; else a = n, b = 1, dx ^= 1, dy ^= 1; } } else { tx = x - 1; if (dy) { ty = m - y; if (tx > ty) a = x - ty, b = m, dy ^= 1; else if (tx < ty) a = 1, b = y + tx, dx ^= 1; else a = 1, b = m, dx ^= 1, dy ^= 1; } else { ty = y - 1; if (tx > ty) a = x - ty, b = 1, dy ^= 1; else if (tx < ty) a = 1, b = y - tx, dx ^= 1; else a = 1, b = 1, dx ^= 1, dy ^ 1; } } long long res = 0; res = (a << 22) | (b << 2) | (dx << 1) | dy; return res; } long long mask = (1 << 20) - 1; long long code(long long x, long long y) { return (x << 20) | y; } void decode(long long n, long long &x, long long &y, long long &dx, long long &dy) { x = (n >> 22) & mask; y = (n >> 2) & mask; dx = (n >> 1) & 1; dy = n & 1; } int main() { ios::sync_with_stdio(false); long long x, y, dx, dy; string s; cin >> n >> m; decode(code(3, 1, 0, 1), x, y, dx, dy); cin >> x >> y; cin >> s; dx = s[0] == 'D'; dy = s[1] == 'R'; if (x == 1 || x == n || y == 1 || y == m) h1.insert(code(x, y)), h2.insert(convert(x, y, dx, dy)); long long t; int cnt = 0; long long ans = 0; long long tx, ty; while (true) { tx = x, ty = y; t = code(x, y, dx, dy); if (h2.count(t)) { break; } decode(t, x, y, dx, dy); h2.insert(t); h1.insert(code(x, y)); ans += ABS(x - tx); if (h1.size() == m + n - 2) break; } if (h1.size() == n + m - 2) cout << ans + 1 << endl; else cout << -1 << endl; return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; bool sortbysec(const pair<long long, long long>& a, const pair<long long, long long>& b) { if (a.second == b.second) return a.first < b.first; return (a.second < b.second); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long isPrime(long long n) { if (n < 2) return 0; if (n < 4) return 1; if (n % 2 == 0 or n % 3 == 0) return 0; for (long long i = 5; i * i <= n; i += 6) if (n % i == 0 or n % (i + 2) == 0) return 0; return 1; } long long modexpo(long long x, long long p) { long long res = 1; x = x % 1000000007; while (p) { if (p % 2) res = res * x; p >>= 1; x = x * x % 1000000007; res %= 1000000007; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { long long n; cin >> n; string s1, s2; cin >> s1 >> s2; long long ot = 0, os = 0; for (long long i = 0; i < n; ++i) { if (s1[i] == '1') os++; if (s2[i] == '1') ot++; } if (ot != os) { cout << -1; return 0; } vector<long long> v; for (long long i = 0; i < n; ++i) { if (s1[i] == s2[i]) v.push_back(0); else if (s1[i] == '1') { v.push_back(1); } else v.push_back(-1); } long long ans = 0, m1 = 0; for (long long i = 0; i < n; ++i) { m1 += v[i]; if (m1 < 0) m1 = 0; ans = (ans > m1 ? ans : m1); } long long m2 = 0; for (long long i = 0; i < n; ++i) { m2 += v[i]; if (m2 > 0) m2 = 0; ans = (ans > -m2 ? ans : -m2); } cout << ans; cout << "\n"; } }
6
#include <bits/stdc++.h> using namespace std; const int N = 200005; int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1}; int dr4[] = {0, 1, -1, 0}; int dc4[] = {1, 0, 0, -1}; int n, m, h, w; pair<int, int> p[N]; long long int fact[N]; long long int dp[N]; long long int pw(long long int x, long long int y) { long long int r = 1; while (y) { if (y & 1) r = (r * x) % 1000000007; x = (x * x) % 1000000007; y /= 2; } return r; } long long int f(long long int x, long long int y) { return (fact[x + y - 2] * pw((fact[x - 1] * fact[y - 1]) % 1000000007, 1000000007 - 2)) % 1000000007; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(fact, 0, sizeof fact); for (int i = 0; i < N; i++) fact[i] = i ? (fact[i - 1] * i) % 1000000007 : 1; cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second; p[n] = {h, w}; n++; sort(p, p + n); for (int i = 0; i < n; i++) { int x = p[i].first, y = p[i].second; dp[i] = f(x, y); for (int j = 0; j < n; j++) if (i != j && x >= p[j].first && y >= p[j].second) dp[i] = (dp[i] - ((dp[j] * f(x - p[j].first + 1, y - p[j].second + 1)) % 1000000007) + 1000000007) % 1000000007; } cout << dp[n - 1]; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int x, y, a, b; cin >> x >> y >> a >> b; if ((y - x) % (a + b) == 0) cout << (y - x) / (a + b) << endl; else cout << "-1" << endl; } }
0
#include <bits/stdc++.h> using namespace std; int n, a[500021], ans[1021][1021]; int main() { ios::sync_with_stdio(false); cin >> n; int pos, x, y; for (int i = 1; i <= n; i++) { cin >> pos >> x >> y; if (pos == 1) { a[x] += y; for (int i = 1; i <= 710; i++) ans[i][x % i] += y; } else { if (x <= 710) cout << ans[x][y] << endl; else { int now = y, sum = 0; while (now <= 500000) { sum += a[now]; now += x; } cout << sum << endl; } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n + 10]; for (int i = 1; i <= n; ++i) cin >> a[i]; long long s, f; cin >> s >> f; long long b[n + 10]; for (int i = 1; i <= n + 10; ++i) b[i] = 0; long long j = 1; long long z = 0; for (long long i = 1; i <= n + f - s; ++i) { if (i == n) z += a[n]; else z += a[i % n]; if (i >= f - s) { b[j] = z; z -= a[j]; j++; } } long long max = 1; for (int i = 1; i <= n; ++i) { if (b[i] > b[max]) max = i; else if (b[i] == b[max]) if (i > s && max > s) { if (s - i + n < s - max + n) max = i; } else if (max <= s && i > s) { if (s - i + n < s - max) max = i; } else if (max > s && i <= s) { if (s - i < s - max + n) max = i; } else if (s - i < s - max) max = i; } long long w[n + 10]; w[max] = s; while (max != 1) { max--; if (w[max + 1] == 1) w[max] = n; else w[max] = w[max + 1] - 1; } cout << w[1]; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int cmax = 223466; int p[cmax]; set<int> s; int n, q; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } void merge(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) return; if (rand() % 2) swap(xx, yy); p[xx] = yy; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) p[i] = i; for (int i = 0; i < n; i++) s.insert(i); s.insert(1 << 30); for (int i = 0; i < q; i++) { int t = 0, x = 0, y = 0; cin >> t >> x >> y; if (x > y) swap(x, y); if (t == 1) { merge(x, y); } else if (t == 2) { vector<int> v; auto it = s.lower_bound(x); while (it != s.end()) { if (*it >= y) break; v.push_back(*it); it++; } for (int i = 0; i < v.size(); i++) s.erase(v[i]); for (int i = 0; i < v.size(); i++) merge(v[i], v[i] + 1); } else { int xx = find(x); int yy = find(y); if (xx == yy) cout << "YES\n"; else cout << "NO\n"; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; using i64 = long long; using Pii = pair<int, int>; constexpr int MAX = 1000100; using namespace std; struct SegTree { i64 sum[MAX]; int M, LIM; void PushUp(int x) { sum[x] = sum[x << 1] + sum[(x << 1) | 1]; } void Init(int n, int m) { for (M = 1; M < n + 2; M <<= 1) ; memset(sum, 0, sizeof(sum)); LIM = m; } void Update(int x, i64 add) { sum[x |= M] += add; if (sum[x] > LIM) sum[x] = LIM; for (x >>= 1; x; x >>= 1) PushUp(x); } i64 Query(int l, int r) { i64 ret = 0; for (l = (l | M) - 1, r = (r | M) + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ret += sum[l ^ 1]; if (r & 1) ret += sum[r ^ 1]; } return ret; } } sta, stb; int main() { int n, k, a, b, q; while (~scanf("%d%d%d%d%d", &n, &k, &a, &b, &q)) { sta.Init(n, b); stb.Init(n, a); int c, u, v; for (int i = 0; i < q; ++i) { scanf("%d", &c); if (c == 1) { scanf("%d%d", &u, &v); sta.Update(u, v); stb.Update(u, v); } else { scanf("%d", &u); i64 ans = 0; if (u > 1) ans += sta.Query(1, u - 1); if (u + k <= n) ans += stb.Query(u + k, n); cout << ans << endl; } } } return 0; }
4