func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; long long w = 0, o = 0; for (long long i = 0; i < s.length() - 1; i++) { if (s[i] == v && s[i + 1] == v ) w++; else { if (s[i] == o ) o++; } } if (s[s.length() - 1] == o ) o++; long long x = 0, ans = 0; for (long long i = 0; i < s.length() - 1; i++) { if (s[i] == v && s[i + 1] == v ) x++; else { if (s[i] == o ) { ans += x * (w - x); } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int lins, cols; cin >> lins >> cols; for (int i(0); i < lins; i++) { if (i % 2 == 0) { for (int j(0); j < cols; j++) { cout << # ; } cout << endl; } else if ((i + 1) % 4 == 0) { cout << # ; for (int j(0); j < cols - 1; j++) { cout << . ; } cout << endl; } else { for (int j(0); j < cols - 1; j++) { cout << . ; } cout << # ; cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int ans = 0; while (1) { int maxi = 0; for (int i = 0; i < n; i++) { if (arr[i] >= arr[maxi]) maxi = i; } if (maxi == 0) break; ans++; arr[0]++; arr[maxi]--; } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; t = 1; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, l = 0, p = 0, i, flag = 0, k, j, r = 0, q = 0, t, curr = 0, d, x; cin >> n >> x >> k; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (long long i = 0; i < n; i++) { long long r1 = a[i] + (x * k); if (a[i] % x == 0) { r1--; } r1 -= r1 % x; long long r2 = r1 + x - 1; r1 = max(a[i], r1); auto it = upper_bound(a, a + n, r2); auto it1 = lower_bound(a, a + n, r1); p += it - it1; } cout << p; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename T1> T amax(T &a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T &a, T1 b) { if (b < a) a = b; return a; } bool isPrime(long long x) { if (x == 1) return false; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } bool isPowerOfTwo(long long n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } long long gcdByBit(long long a, long long b) { if (!a || !b) return a | b; unsigned shift = __builtin_ctz(a | b); a >>= __builtin_ctzl(a); do { b >>= __builtin_ctzl(b); if (a > b) { swap(a, b); } b -= a; } while (b); return a << shift; } long long powerMod(long long n, long long p) { long long res = 1; while (p) { if (p % 2 == 0) { n = n * 1ll * n % 1000000007; p /= 2; } else { res = res * 1ll * n % 1000000007; p--; } } return res; } long long powerMod2(long long b, long long p, long long m) { if (p == 0) return 1; return (b * powerMod2(b, p - 1, m) % m) % m; } long long fact(long long n) { long long res = 1; for (long long i = 1; i <= n; i++) { res = res * 1ll * i % 1000000007; } return res; } long long nCr(long long n, long long r) { return fact(n) * 1ll * powerMod(fact(r), 1000000007 - 2) % 1000000007 * 1ll * powerMod(fact(n - r), 1000000007 - 2) % 1000000007; } long long arr[300005]; long long ans[300005]; long long parent(long long x) { if (arr[x] != x) arr[x] = parent(arr[x]); return arr[x]; } void Chaliye_Shuru_Karte_Hai() { for (long long i = 0; i < 300005; i++) arr[i] = i; long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b, c; cin >> a >> b >> c; for (long long j = parent(a); j <= b; j = parent(j + 1)) { if (j != c) ans[j] = c, arr[j] = j + 1; } } for (long long i = 1; i < n + 1; i++) cout << ans[i] << ; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tc = 1; while (tc--) { Chaliye_Shuru_Karte_Hai(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2005; const int M = 101; const double EPS = 1e-9; const int INF = 1e9; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } double getDist(complex<double> a, complex<double> b) { return sqrt((a.real() - b.real()) * (a.real() - b.real()) + (a.imag() - b.imag()) * (a.imag() - b.imag())); } int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } long long mod = 1000000007, fib[N], dp[N][N]; long long power(long long base, int p) { if (!p) return 1; long long ret = power(base, p / 2); ret *= ret; ret %= mod; if (p & 1) ret *= base, ret %= mod; return ret; } long long modInverse(int a) { return power(a, mod - 2); } long long ncr(int n, int r) { return (((fib[n] * modInverse(fib[r])) % mod) * modInverse(fib[n - r])) % mod; } bool emty[N], taken[N]; long long solve(int n, int k) { if (k == 0) return fib[n]; if (k == 1) return (fib[n] * n) % mod; if (dp[n][k] != -1) return dp[n][k]; long long ret = (solve(n, k - 1) * n) % mod + (solve(n + 1, k - 2) * (k - 1)) % mod; return dp[n][k] = ret % mod; } int main() { fib[0] = fib[1] = 1; for (int i = 2; i < N; i++) fib[i] = (fib[i - 1] * i) % mod; int n, free = 0, k = 0; scanf( %d , &n); for (int i = 1; i <= n; i++) { int a; scanf( %d , &a); if (a == -1) emty[i] = 1; else taken[a] = 1; } for (int i = 1; i <= n; i++) { if (emty[i] == 1) { if (taken[i] == 1) free++; else k++; } } memset(dp, -1, sizeof(dp)); cout << solve(free, k); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, tmp, sum = 1; int map[200000] = {0}; cin >> n; for (int i = 0; i < n; i++) { cin >> tmp; if (map[tmp] == 0) { map[tmp]++; } else sum++; } cout << sum; }
#include <bits/stdc++.h> using namespace std; long long N; long long i, j; vector<long long> v; map<long long, long long> mp; long long arr[200]; int main() { scanf( %lld , &N); for (i = 0; i < N; i++) scanf( %lld , &arr[i]); sort(arr, arr + N); long long ans = 0; for (i = 0; i < N; i++) { long long get = arr[i]; if (mp[arr[i]] == 1) { continue; } else { ans++; for (j = 1; j < 200; j++) { mp[get * j] = 1; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxd = 1e6 + 10; int a[maxd]; long long sum, ans = 1e18; vector<long long> b; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= n; i++) sum += a[i]; if (sum == 1) { printf( -1 n ); return 0; } for (long long i = 2; i * i <= sum; i++) { if (sum % i == 0) { b.push_back(i); sum /= i; i--; } } b.push_back(sum); for (auto x : b) { long long res = 0; sum = 0; for (int i = 1; i <= n; i++) { sum += a[i]; sum %= x; res += min(sum, x - sum); } ans = min(res, ans); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, a; vector<int> q; int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { bool w = true; for (int j = 0; j < n; j++) { scanf( %d , &a); if (a == 1 || a == 3) w = false; } if (w) q.push_back(i + 1); } printf( %d n , q.size()); for (int i = 0; i < q.size(); i++) printf( %d , q[i]); return 0; }
#include <bits/stdc++.h> using namespace std; bool can(bool print, int t, long long S, const vector<pair<int, int> >& bugs, const vector<pair<int, pair<int, int> > >& st) { priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; int ind = bugs.size() - 1; int stind = st.size() - 1; vector<int> ans(bugs.size()); long long cost = 0; while (ind >= 0) { while (stind >= 0 and st[stind].first >= bugs[ind].first) { pq.push(pair<int, int>(st[stind].second.first, st[stind].second.second)); --stind; } if (pq.empty()) return false; long long c = pq.top().first; int indans = pq.top().second; pq.pop(); cost += c; if (cost > S) return false; for (int i = 0; ind >= 0 and i < t; ++i) { ans[bugs[ind].second] = indans; --ind; } } if (print) { for (int i = 0; i < int(bugs.size()); ++i) cout << (i ? : ) << ans[i]; cout << endl; } return true; } int main() { int n, m, s; cin >> n >> m >> s; vector<pair<int, int> > bugs(m); for (int i = 0; i < m; ++i) { cin >> bugs[i].first; bugs[i].second = i; } vector<pair<int, pair<int, int> > > st(n); for (int i = 0; i < n; ++i) cin >> st[i].first; for (int i = 0; i < n; ++i) { cin >> st[i].second.first; st[i].second.second = i + 1; } sort(bugs.begin(), bugs.end()); sort(st.begin(), st.end()); int low = 1, hi = m + 1; while (low < hi) { int mid = (low + hi) / 2; if (can(false, mid, s, bugs, st)) hi = mid; else low = mid + 1; } if (low == m + 1) cout << NO << endl; else { cout << YES << endl; can(true, low, s, bugs, st); } }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 10; int n, m, q, pos[MAX], bit[MAX], res[MAX], XOR[MAX]; pair<int, int> cache[MAX], a[MAX], b[MAX]; struct Node { int lef, rig, tt; bool operator<(const Node& other) const { return this->lef < other.lef; } }; Node query[MAX]; void up(int i) { int val = a[i].first; while (i <= n) { bit[i] ^= val; i += i & (-i); } } int get(int i) { int res = 0; while (i) { res ^= bit[i]; i -= i & (-i); } return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , &cache[i].first); a[i].first = cache[i].first; cache[i].second = i; } sort(cache + 1, cache + 1 + n); for (int i = 1; i <= n; ++i) { if (i == 1 || cache[i].first > cache[i - 1].first) ++m; a[cache[i].second].second = m; } for (int i = 1; i <= n; ++i) { b[i] = pair<int, int>(pos[a[i].second], i); pos[a[i].second] = i; XOR[i] = XOR[i - 1] ^ a[i].first; } sort(b + 1, b + 1 + n); scanf( %d , &q); for (int i = 1; i <= q; ++i) { scanf( %d %d , &query[i], &query[i].rig); query[i].tt = i; } sort(query + 1, query + 1 + q); int j = 1; for (int i = 1; i <= q; ++i) { while (j <= n && b[j].first < query[i].lef) up(b[j++].second); res[query[i].tt] = XOR[query[i].rig] ^ XOR[query[i].lef - 1]; res[query[i].tt] ^= get(query[i].rig) ^ get(query[i].lef - 1); } for (int i = 1; i <= q; ++i) printf( %d n , res[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100], b[100], i, c = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i] >> b[i]; if ((b[i] - a[i]) >= 2) c++; } cout << c; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a = 0, l[101], r[101], k; cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; } cin >> k; for (int i = 0; i < n; i++) { if (k <= r[i]) { a++; } } cout << a; }
#include <bits/stdc++.h> using namespace std; int main(void) { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> ans1, ans2; string a, b; cin >> a >> b; a = a + 0 ; b = b + 0 ; for (int i = 0; i < n; i++) { if (a[i] != a[i + 1]) ans1.push_back(i); } for (int i = 0; i < n; i++) { if (b[i] != b[i + 1]) ans2.push_back(i); } cout << ans1.size() + ans2.size() << ; for (int i = 0; i < ans1.size(); i++) cout << ans1[i] + 1 << ; for (int i = ans2.size() - 1; i >= 0; i--) cout << ans2[i] + 1 << ; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long p, x, a, b, ni, ans; void e_gcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return; } e_gcd(b, a % b, x, y); int tmp = x; x = y; y = tmp - (a / b) * y; return; } int main() { cin >> a >> b >> p >> x; long long i, j, s; e_gcd(a, p, ni, s); a = 1; for (j = 1; j <= p - 1; j++) { a = a * ni % p; i = (j - b * a % p + p) % p; long long n = i * (p - 1) + j; if (n <= x) ans += (x - n) / (p * (p - 1)) + 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); int *a = new int[n]; for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } int m; scanf( %d , &m); int bred[10000]; for (int i = 0; i < 10000; i++) bred[i] = 0; for (int i = 0; i < m; i++) { int b; scanf( %d , &b); for (int j = 0; j < n; j++) { if (b % a[j] != 0) continue; bred[b / a[j]]++; } } for (int i = 9999; i >= 0; i--) { if (bred[i] != 0) { printf( %d , bred[i]); break; } } return 0; }
#include <bits/stdc++.h> inline int rd(int f = 1, int x = 0, char ch = ) { while (!isdigit(ch = getchar())) if (ch == - ) f = -1; while (isdigit(ch)) x = x * 10 + ch - 0 , ch = getchar(); return f * x; } const int N = 1e5 + 5; int n, a[N], ans; int main() { for (int T = rd(); T--;) { n = rd(), ans = 0; for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 2; i <= n; ++i) ans |= a[i - 1] <= a[i]; puts(ans ? YES : NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k, q, a[200005], f[200005]; int main() { cin >> n >> k >> q; for (int i = 1; i <= n; i++) { long long l, r; cin >> l >> r; a[l]++; a[r + 1]--; } for (int i = 1; i <= 200000; i++) a[i] += a[i - 1]; for (int i = 1; i <= 200000; i++) { f[i] += f[i - 1]; if (a[i] >= k) { f[i]++; } } for (int i = 1; i <= q; i++) { long long l, r; cin >> l >> r; cout << f[r] - f[l - 1] << endl; } }
#include <bits/stdc++.h> using namespace std; constexpr int INF = 2e9; int main() { int t; t = 1; while (t--) { int n; string s; cin >> n; cin >> s; int res = 0; for (int d = 25; d >= 0; d--) { string tmp = ; for (int i = 0; i < s.size(); i++) { int now = s[i] - a ; if (now != d) { tmp += s[i]; } else { if (tmp.back() - a == d - 1) { res++; } else { tmp += s[i]; } } } swap(s, tmp); tmp = ; for (int i = s.size() - 1; i >= 0; i--) { int now = s[i] - a ; if (now != d) { tmp += s[i]; } else { if (tmp.back() - a == d - 1) { res++; } else { tmp += s[i]; } } } reverse(tmp.begin(), tmp.end()); swap(s, tmp); } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 2e5 + 5; int n, k, d, ansL = 1, ansR = 1, top1, top2; int a[MAXN], st1[MAXN], st2[MAXN]; map<int, int> lst; namespace SegmentTree { struct Tree { int l, r; int w, tag; } tree[MAXN << 2]; void update(int k) { tree[k].w = min(tree[k << 1].w, tree[k << 1 | 1].w); } void apply(int k, int val) { tree[k].w += val; tree[k].tag += val; } void down(int k) { if (tree[k].tag != 0) { apply(k << 1, tree[k].tag); apply(k << 1 | 1, tree[k].tag); tree[k].tag = 0; } } void Build(int k, int l, int r) { tree[k].l = l; tree[k].r = r; if (l == r) { tree[k].w = l; return; } int mid = (l + r) >> 1; Build(k << 1, l, mid); Build(k << 1 | 1, mid + 1, r); update(k); } void Modify(int k, int l, int r, int val) { if (tree[k].l >= l && tree[k].r <= r) { apply(k, val); return; } down(k); int mid = (tree[k].l + tree[k].r) >> 1; if (l <= mid) Modify(k << 1, l, r, val); if (r > mid) Modify(k << 1 | 1, l, r, val); update(k); } void Delete(int k, int x) { if (tree[k].l == tree[k].r) { tree[k].w = INF; return; } down(k); int mid = (tree[k].l + tree[k].r) >> 1; if (x <= mid) Delete(k << 1, x); else Delete(k << 1 | 1, x); update(k); } int Query(int k, int l, int r, int x) { if (tree[k].w > x) return INF; if (tree[k].l == tree[k].r) return tree[k].l; if (tree[k].l >= l && tree[k].r <= r) { down(k); if (tree[k << 1].w <= x) return Query(k << 1, l, r, x); else return Query(k << 1 | 1, l, r, x); } else { down(k); int mid = (tree[k].l + tree[k].r) >> 1; int res; if (l <= mid) { res = Query(k << 1, l, r, x); if (res != INF) return res; } if (r > mid) { res = Query(k << 1 | 1, l, r, x); if (res != INF) return res; } return -1; } } } // namespace SegmentTree int main() { scanf( %d%d%d , &n, &k, &d); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); a[i] += INF; } if (d == 0) { for (int i = 1; i <= n; i++) { int l = i; while (a[i] == a[i + 1] && i < n) i += 1; if (i - l + 1 > ansR - ansL + 1) { ansL = l; ansR = i; } } printf( %d %d n , ansL, ansR); return 0; } SegmentTree::Build(1, 1, n); int L = 1; for (int R = 1; R <= n; R++) { int tmp = L; if (a[R] % d == a[R - 1] % d) L = max(lst[a[R]] + 1, L); else L = R; lst[a[R]] = R; while (tmp < L) { SegmentTree::Delete(1, tmp); tmp += 1; } while (top1 && st1[top1] >= L && a[st1[top1]] >= a[R]) { SegmentTree::Modify(1, max(L, st1[top1 - 1] + 1), st1[top1], a[st1[top1]] / d); top1 -= 1; } SegmentTree::Modify(1, max(L, st1[top1] + 1), R, -a[R] / d); st1[++top1] = R; while (top2 && st2[top2] >= L && a[st2[top2]] <= a[R]) { SegmentTree::Modify(1, max(L, st2[top2 - 1] + 1), st2[top2], -a[st2[top2]] / d); top2 -= 1; } SegmentTree::Modify(1, max(L, st2[top2] + 1), R, a[R] / d); st2[++top2] = R; int pos = SegmentTree::Query(1, L, R, k + R); if (R - pos + 1 > ansR - ansL + 1) { ansL = pos; ansR = R; } } printf( %d %d n , ansL, ansR); return 0; }
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b, long long mod) { return (a % mod + b % mod) % mod; } long long sub(long long a, long long b, long long mod) { return (a % mod - b % mod + mod) % mod; } long long mul(long long a, long long b, long long mod) { return (a % mod * b % mod) % mod; } long long sumodd(long long num, long long mod) { return mul(num, num, mod); } long long sumeven(long long num, long long mod) { return mul(num, num + 1, mod); } long long sumrange(long long st, long long en, long long num) { return (num * (st + en) / 2); } long long gcd(long long a, long long b) { while (b != 0) { long long a2 = a; a = b; b = a2 % b; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } string makeitbinary(long long n) { string s; while (n) { s = s + (char)((n % 2) + 0 ); n /= 2; } reverse(s.begin(), s.end()); return s; } bool bit(int num, int i) { return ((num >> i) & 1); } long long fastpowermod(long long b, long long p, long long mod) { long long ans = 1; while (p) { if (p % 2) { ans = mul(ans, b, mod); } b = mul(b, b, mod); p /= 2; } return ans; } long long fastpower(long long b, long long p) { long long ans = 1; while (p) { if (p % 2) { ans = ans * b; } b = b * b; p /= 2; } return ans; } double fastpower_double(double b, long long p) { double ans = 1; while (p) { if (p % 2) { ans = ans * b; } b = b * b; p /= 2; } return ans; } long long summation_formula(long long n) { return (n * (n + 1) / 2); } bool lower_vowel(char c) { return (c == i || c == o || c == u || c == a || c == e ); } string bigint_mini(string s1, string s2) { if (s1.size() > s2.size()) { return s2; } else if (s2.size() > s1.size()) { return s1; } for (int i = 0; i < s1.size(); i++) { if ((s1[i] - 0 ) > (s2[i] - 0 )) { return s2; } else if ((s2[i] - 0 ) > (s1[i] - 0 )) { return s1; } } return s1; } double polygon_area(int n, vector<double> X, vector<double> Y) { double area = 0.0; int j = n - 1; for (int i = 0; i < n; i++) { area += (X[j] + X[i]) * (Y[j] - Y[i]); j = i; } return abs(area / 2.0); } long long sum_of_digits(string s) { long long sum = 0; for (int i = 0; i < s.size(); i++) { sum += s[i] - 0 ; } return sum; } string makeitbase(long long num, long long base) { string s; while (num) { long long mod = num % base; s += (mod + 0 ); num /= base; } reverse(s.begin(), s.end()); return s; } bool intersect(long long l1, long long r1, long long l2, long long r2) { return (max(l1, l2) <= min(r1, r2)); } pair<long long, long long> find_intersection(long long l1, long long r1, long long l2, long long r2) { return {max(l1, l2), min(r1, r2)}; } long long sum_ranges(long long l, long long r) { return summation_formula(r) - summation_formula(l); } double power_2(double num) { return num * num; } struct point { int x, y; }; long long dist(point p1, point p2) { return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); } bool square(point p1, point p2, point p3, point p4) { long long d2 = dist(p1, p2); long long d3 = dist(p1, p3); long long d4 = dist(p1, p4); if (d2 == d3 && 2 * d2 == d4 && 2 * dist(p2, p4) == dist(p2, p3)) { return true; } if (d3 == d4 && 2 * d3 == d2 && 2 * dist(p3, p2) == dist(p3, p4)) { return true; } if (d2 == d4 && 2 * d2 == d3 && 2 * dist(p2, p3) == dist(p2, p4)) { return true; } return false; } const int sz = 1e6 + 1; long long M = 1e9 + 7; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, k, m, t; cin >> n >> k >> m >> t; while (t--) { int x, y; cin >> x >> y; if (x == 1) { if (y <= k) { k++; } n++; } else { if (k >= 1 && k <= y) { n -= (n - y); } else { n -= y; k -= y; } } cout << n << << k << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int f = n * 1.0 / 3 + 0.5; cout << f / 12 << << f % 12 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX1 = 3e5 + 10; long long a, b, c, d, e, cnt, x, y, z, l, r, flag, num[MAX1], mod = 1e10 + 7; string s; char p[MAX1]; long long sta(long long n) { for (int i = 1; i < 1000000; ++i) { if (i * (i - 1) / 2 == n) return i; } return 0; } int main() { cin >> a >> b >> c >> d; x = sta(a); y = sta(d); if (a == 0) { if (b == 0 && c == 0 && x && y) { for (int i = 0; i < y; ++i) cout << 1; return 0; } else x = 1; } if (d == 0) { if (b == 0 && c == 0 && x && y) { for (int i = 0; i < x; ++i) cout << 0; return 0; } else y = 1; } if (x == 0 || y == 0) { cout << Impossible ; return 0; } if (x * y != b + c) { cout << Impossible ; return 0; } cnt = b / y; b -= cnt * y; x -= cnt; for (long long i = 0; i < cnt; ++i) cout << 0; for (long long i = 0; i < y - b; ++i) cout << 1; y -= y - b; if (x && b) cout << 0, --x; for (long long i = 0; i < y; ++i) cout << 1; for (long long i = 0; i < x; ++i) cout << 0; return 0; }
#include <bits/stdc++.h> using namespace std; long long bigpow(long long x, long long n) { if (n == 0) return 1; long long temp = bigpow(x, n / 2); return n % 2 == 0 ? (temp * temp) % 1000000009 : (x * ((temp * temp) % 1000000009)) % 1000000009; } int main() { long long n, m, k, x, t, p, q, gaps, groups; cin >> n >> m >> k; gaps = n - m; groups = m % (k - 1) == 0 ? m / (k - 1) : m / (k - 1) + 1; if (gaps >= groups - 1) cout << m << endl; else { x = n - gaps * k; t = 2 * (bigpow(2, x / k) - 1) * k + x % k; t += gaps * (k - 1); cout << t % 1000000009 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char s = getchar(); while (s < 0 || s > 9 ) { if (s == - ) f *= -1; s = getchar(); } while (s >= 0 && s <= 9 ) { x = (x << 1) + (x << 3) + s - 0 ; s = getchar(); } return x * f; } int n = read(), w[5], dp[1005][630], s[15], c[15] = {0, 125, 25, 5, 1, 300, 60, 12, 465, 93, 624}; bool p[6][1005]; int main() { for (int i = 1; i < 5; i++) w[i] = read(); char ss = getchar(); for (int i = 1; i < 5; i++) for (int j = 1; j <= n; j++) { while (ss != . && ss != * ) ss = getchar(); p[i][j] = (ss == * ); ss = getchar(); } memset(dp, 0x3f3f3f3f, sizeof(dp)); dp[1][0] = 0; int b[5] = {0, 125, 25, 5, 1}; for (int i = 1; i < 11; i++) { if (i < 5) s[i] = w[1]; else if (i > 4 && i < 8) s[i] = w[2]; else if (i > 7 && i < 10) s[i] = w[3]; else s[i] = w[4]; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 625; j++) { int a[5] = {0, j / 125, j / 25 % 5, j / 5 % 5, j % 5}; if (max(max(a[1], a[2]), max(a[3], a[4])) > n - i + 1) continue; for (int o = 1; o < 5; o++) if (a[o] > 0 && !p[o][i + a[o] - 1]) dp[i][j] = min(dp[i][j], dp[i][j - b[o]]); if (min(min(a[1], a[2]), min(a[3], a[4])) > 0) dp[i + 1][j - 156] = min(dp[i + 1][j - 156], dp[i][j]); for (int k = 1; k < 11; k++) { int b[5] = {0, c[k] / 125, c[k] / 25 % 5, c[k] / 5 % 5, c[k] % 5}, l = 0; for (int o = 1; o < 5; o++) l = l * 5 + max(a[o], b[o]); dp[i][l] = min(dp[i][l], dp[i][j] + s[k]); } } } printf( %d n , dp[n + 1][0]); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int MOD = 1e9 + 7; const int N = 0; int a[5], m; long long sum(int len, int x) { return 1LL * len * x - 1LL * (x + 1) * x / 2 + x; } long long cal(int x, int y, int bar, int len) { if (len < 0) return 0; int R = min(bar - y, len), L = min(len - (bar - x), R); long long ans = 0; if (L >= 0) ans += 1LL * (L + 1) * (bar - x + 1); else L = -1; ans += sum(len, R) - sum(len, L); return ans; } long long solve(int x, int y, int z, int len) { int tmp = max(max(y, z) + 1, x); len -= tmp - x; x = tmp; long long ans = 0; for (auto i = 0; i <= len; i++) { int cur = x + i, rem = min(2 * (cur - 1) - y - z, len - i); if (rem > cur - y - z) ans += cal(y, z, cur - 1, rem) - cal(y, z, cur - 1, (cur - y - z)); } return ans; } long long solve2(int x, int y, int z, int len) { int tmp = max(x, y); len -= 2 * tmp - x - y; x = tmp; if (x <= z) len -= 2 * (z + 1 - x), x = z + 1; long long ans = 0; if (len >= 0) for (auto i = 0; i <= len / 2; i++) { int rem = len - 2 * i; ans += min(x + i - 1 - z, rem) + 1; } return ans; } int main() { for (auto i = 0; i <= 2; i++) cin >> a[i]; cin >> m; long long ans = 0; for (auto i = 0; i <= 2; i++) ans += solve(a[i], a[(i + 1) % 3], a[(i + 2) % 3], m); for (auto i = 0; i <= 2; i++) ans += solve2(a[i], a[(i + 1) % 3], a[(i + 2) % 3], m); int tmp = *max_element(a, a + 3), v = 0; for (auto i = 0; i <= 2; i++) v += tmp - a[i]; if (v <= m) ans += (m - v) / 3 + 1; cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[k]; for (int i = 0; i < k; i++) cin >> a[i]; list<int> leader; for (int i = 1; i <= n; i++) leader.insert(leader.end(), i); int l = 1; int i = 0; int begin = 0; while (k--) { l = (leader.size() + a[i] + begin) % leader.size(); list<int>::iterator pnlist = leader.begin(); advance(pnlist, l); if (k == 0) cout << *pnlist << endl; else cout << *pnlist << ; leader.erase(pnlist); begin = l; i++; } }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } int main() { int n; cin >> n; vector<int> q(n); for (int i = 0; i < n; ++i) cin >> q[i]; vector<int> cost(n, -1); int m; cin >> m; for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; if (cost[b - 1] == -1 || cost[b - 1] > c) { cost[b - 1] = c; } } int ans = 0, head = 0; for (int i = 0; i < n; ++i) { if (cost[i] != -1) ans += cost[i]; else head++; } if (head > 1) cout << -1 << endl; else { if (!head) { int maxx = cost[0]; for (int i = 1; i < n; ++i) maxx = max(maxx, cost[i]); ans -= maxx; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> g[200002]; vector<pair<int, pair<int, int> > > first[200002]; int lvl[200002]; int p[20][200002]; int h[20][200002]; int d[200002][2]; inline int lca(int v1, int v2) { if (lvl[v1] < lvl[v2]) swap(v1, v2); for (int i = 20 - 1; i >= 0; --i) { if (lvl[p[i][v1]] >= lvl[v2]) { v1 = p[i][v1]; } } if (v1 == v2) return v1; for (int i = 20 - 1; i >= 0; --i) { if (p[i][v1] != p[i][v2]) { v1 = p[i][v1]; v2 = p[i][v2]; } } return p[0][v1]; } void dfs(int v = 0, int level = 0, int pr = 0) { lvl[v] = level; p[0][v] = pr; for (int i = 1; i < 20; ++i) p[i][v] = p[i - 1][p[i - 1][v]]; for (const int& v2 : g[v]) { dfs(v2, level + 1, v); } } inline int getit(int v, int k) { if (h[k][v] != -1) return h[k][v]; return h[k][v] = getit(p[k - 1][v], k - 1) + getit(v, k - 1); } inline int get(int v, int k) { if (k < 0) return 0; int res = 0; for (int i = 20 - 1; i >= 0; --i) { if ((k & (1 << i)) > 0) { res += getit(v, i); k ^= (1 << i); v = p[i][v]; } } return res; } void go(int v = 0) { for (const int& v2 : g[v]) { go(v2); d[v][0] += max(d[v2][0], d[v2][1]); } for (const int& v2 : g[v]) { h[0][v2] = d[v][0] - max(d[v2][0], d[v2][1]); } for (const auto& t : first[v]) { int cost = t.first; int v1 = t.second.first; int v2 = t.second.second; int w = -d[v][0] + get(v1, lvl[v1] - lvl[v]) + get(v2, lvl[v2] - lvl[v]) + d[v1][0] + d[v2][0] + cost; d[v][1] = max(d[v][1], w); } } int main() { memset(h, -1, sizeof(h)); scanf( %d%d , &n, &m); for (int i = 0; i < n - 1; ++i) { int t; scanf( %d , &t); --t; g[t].push_back(i + 1); } dfs(); for (int i = 0; i < m; ++i) { int v1, v2, cost; scanf( %d%d%d , &v1, &v2, &cost); --v1, --v2; int lcav = lca(v1, v2); first[lcav].push_back(make_pair(cost, make_pair(v1, v2))); } go(); printf( %d n , max(d[0][0], d[0][1])); return 0; }
#include <bits/stdc++.h> using namespace std; int t[2000002]; int longestPrefix(string &s) { t[0] = 0; for (int pos = 1; pos < s.length(); ++pos) { int prev = t[pos - 1]; while (prev > 0 && s[pos] != s[prev]) { prev = t[prev - 1]; } if (s[pos] == s[prev]) { t[pos] = prev + 1; } else { t[pos] = prev; } } return t[s.length() - 1]; } int main() { int t; cin >> t; while (t-- > 0) { string s; cin >> s; int lindex = 0, rindex = s.length(); while (lindex < rindex && s[lindex] == s[rindex - 1]) { ++lindex; --rindex; } if (lindex >= rindex) { cout << s << endl; continue; } string mid = s.substr(lindex, rindex - lindex); string rmid = mid; reverse(rmid.begin(), rmid.end()); string pre = mid + * + rmid; string suf = rmid + * + mid; int pref = longestPrefix(pre); int suff = longestPrefix(suf); int len = max(pref, suff); for (int i = 0; i < lindex; ++i) { cout << s[i]; } if (pref >= suff) { for (int i = lindex; i < lindex + pref; ++i) { cout << s[i]; } } else { for (int i = rindex - suff; i < rindex; ++i) { cout << s[i]; } } for (int i = rindex; i < s.length(); ++i) { cout << s[i]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int V[200010]; char vowel[] = aeiouAEIOU ; int v(int a, int k) { if (k == 0) return 0; int x = V[a + k - 1]; if (a > 0) x -= V[a - 1]; return x; } int f(int a, int k) { return 2 * k - 3 * v(a, k); } int g(int k) { return f(0, k); } bool Good(int a, int k) { return f(a, k) >= 0; } bool is_vowel(char c) { for (int i = 0; i < 10; i++) { if (vowel[i] == c) return true; } return false; } bool sf(const int& a, const int& b) { if (g(a) < g(b)) return true; if (g(a) > g(b)) return false; return a < b; } int Q[200010]; vector<int> P; int R[200010]; int my_bin_search(int s, string S) { int a = 0; int b = P.size() - 1; while (a <= b) { int k = (a + b) / 2; if (P[k] == s) return k; else if (sf(P[k], s)) a = k + 1; else b = k - 1; } throw element not found ; } void create_data(string S) { int N = S.length(); for (int a = 0; a <= N; a++) { P.push_back(a); } sort(P.begin(), P.end(), sf); for (int x = 0; x < P.size(); x++) { R[P[x]] = x; } Q[0] = P[0]; for (unsigned int x = 1; x < P.size(); x++) { Q[x] = min(Q[x - 1], P[x]); } } int main() { string S; cin >> S; int N = S.length(); V[0] = 0; for (unsigned int i = 0; i < S.length(); i++) { if (i > 0) V[i] = V[i - 1]; if (is_vowel(S[i])) V[i]++; } create_data(S); int bestk = 0; for (int a = 0; a <= N; a++) { int x = R[a]; int b = Q[x]; bestk = max(bestk, a - b); } if (bestk == 0) { cout << No solution << endl; return 0; } int found = 0; for (int a = 0; (a + bestk) <= N; a++) { if (Good(a, bestk)) { found++; } } cout << bestk << << found << endl; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); void read(int& x) { x = 0; int f = 1; char ch = getchar(); while ((ch < 0 || ch > 9 ) && ch != - ) ch = getchar(); if (ch == - ) f = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); x *= f; } int n, f[200005], g[200005], els[200005]; vector<int> E[200005]; int power(int a, int b, int p) { int s = 1, w = a; while (b) { if (b & 1) s = (long long)s * w % p; w = (long long)w * w % p; b >>= 1; } return s; } vector<int> F[200005], B[200005]; void dfs(int now) { f[now] = 1; for (int i = (0); i < (E[now].size()); i++) { dfs(E[now][i]), f[now] = (long long)f[now] * (f[E[now][i]] + 1) % 1000000007; F[now].push_back(f[E[now][i]] + 1), B[now].push_back(f[E[now][i]] + 1); } for (int i = (1); i < (E[now].size()); i++) F[now][i] = (long long)F[now][i - 1] * F[now][i] % 1000000007; for (int i = (E[now].size() - 2); i >= (0); i--) B[now][i] = (long long)B[now][i + 1] * B[now][i] % 1000000007; if (E[now].size() == 1) { els[E[now][0]] = 1; return; } for (int i = (0); i < (E[now].size()); i++) { if (i == 0) els[E[now][i]] = B[now][i + 1]; else if (i + 1 == E[now].size()) els[E[now][i]] = F[now][i - 1]; else els[E[now][i]] = (long long)B[now][i + 1] * F[now][i - 1] % 1000000007; } } void work(int now) { for (int i = (0); i < (E[now].size()); i++) { g[E[now][i]] = ((long long)g[now] * els[E[now][i]] + 1) % 1000000007; work(E[now][i]); } } int main() { read(n); for (int i = (2); i <= (n); i++) { int x; read(x); E[x].push_back(i); } dfs(1); g[1] = 1; work(1); for (int i = (1); i <= (n); i++) printf( %lld , (long long)f[i] * g[i] % 1000000007); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; string n; int i, j, a[9][7], x, y; int main() { cin >> n; for (i = n.size() - 1; i >= 0; i--) { x = n[i] - 48; y = max(y, x); for (j = 0; j < x; j++) a[j][7 + i - n.size()] = 1; } cout << y << endl; for (i = 0; i < y; cout << , i++) for (j = 0, x = 0; j < 7; j++) { if (a[i][j] != 0) x = 1; if (x != 0) cout << a[i][j]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long s = 0, w = 1; register char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) w = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } long long n; string s; signed main() { n = read(); cin >> s; for (register long long i = 2; i <= n; i++) { if (n % i) continue; for (register long long j = 0; j < i / 2; j++) swap(s[j], s[i - j - 1]); } cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int D = 505; const int N = 5005; pair<int, int> pre[D][N]; int ans[D][N]; void solve() { memset(pre, -1, sizeof(pre)); memset(ans, -1, sizeof(ans)); int d, s; cin >> d >> s; queue<pair<int, int> > que; que.push(pair<int, int>(0, 0)); vector<int> vec; while (!que.empty()) { pair<int, int> u = que.front(); que.pop(); if (u.first == 0 && u.second == s) { pair<int, int> cur = u; while (!(cur.first == 0 && cur.second == 0)) { vec.push_back(ans[cur.first][cur.second]); cur = pre[cur.first][cur.second]; } break; } for (int i = 0; i <= 9; i++) { pair<int, int> v = pair<int, int>((u.first * 10 + i) % d, u.second + i); if (v.second <= s && ans[v.first][v.second] == -1) { ans[v.first][v.second] = i; pre[v.first][v.second] = u; que.push(v); } } } if (!vec.size()) printf( -1 n ); else { reverse(vec.begin(), vec.end()); for (auto i : vec) putchar(i + 0 ); printf( n ); } } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5, MOD = 998244353; int m, n, k; long long res; int free_Row, free_Col, line_vert, line_hori; int Num_Row[N], Num_Row2[N][2], Num_Col[N], Num_Col2[N][2]; int c[2][2]; map<pair<int, int>, int> M; long long power(long long x, int k) { long long ans = 1; while (k) { if (k & 1) ans = ans * x % MOD; x = x * x % MOD; k >>= 1; } return ans; } void Calculate() { res = 0; if (line_vert && line_hori) return; if (!line_vert) (res += power(2, free_Row)) %= MOD; if (!line_hori) (res += power(2, free_Col)) %= MOD; if (!line_vert && !line_vert) { if (c[0][0] == 0 && c[1][1] == 0) (res += MOD - 1) %= MOD; if (c[0][1] == 0 && c[1][0] == 0) (res += MOD - 1) %= MOD; } } void Delete(int x, int y) { if (!M.count({x, y})) return; int z = M[{x, y}]; M.erase({x, y}); --c[z][(x + y) & 1]; if (--Num_Row[x] == 0) ++free_Row; if (--Num_Row2[x][z ^ (y & 1)] == 0 && Num_Row[x]) --line_vert; if (--Num_Col[y] == 0) ++free_Col; if (--Num_Col2[y][z ^ (x & 1)] == 0 && Num_Col[y]) --line_hori; } void Add(int x, int y, int z) { M[{x, y}] = z; ++c[z][(x + y) & 1]; if (Num_Row[x]++ == 0) --free_Row; if (Num_Row2[x][z ^ (y & 1)]++ == 0 && Num_Row[x] > 1) ++line_vert; if (Num_Col[y]++ == 0) --free_Col; if (Num_Col2[y][z ^ (x & 1)]++ == 0 && Num_Col[y] > 1) ++line_hori; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> m >> n >> k; free_Row = m; free_Col = n; line_hori = 0; line_vert = 0; int x, y, z; while (k--) { cin >> x >> y >> z; Delete(x, y); if (z >= 0) Add(x, y, z); Calculate(); cout << res << n ; } }
#include <bits/stdc++.h> using namespace std; const int N = 10000; vector<int> vec(N); int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> vec[i]; } sort(vec.begin(), vec.begin() + n); int sum = 0; for (int i = 0; i < m; i++) { sum += vec[i]; } cout << sum << endl; }
#include <bits/stdc++.h> namespace algo { using std::swap; const long double PI = 3.141592653589793238462643383279502884L; const long double E = 2.718281828459045235360287471352662498L; const long double EPS = 1e-12L; template <typename T1, typename T2> T1 bin_pow(T1 a, T2 n) { T1 r{1}; while (n > 0) { if (n % 2 == 1) r *= a; a *= a; n /= 2; } return r; } template <typename T1, typename T2> T1 bin_pow(T1 a, T2 n, const T1& m) { if (m == 1) return 0; T1 r{1}; while (n > 0) { if (n % 2 == 1) { r *= a; r %= m; } a *= a; a %= m; n /= 2; } return r; } template <typename T> T gcd(const T& a, const T& b) { if (b == 0) return a < 0 ? -a : a; return gcd(b, a % b); } template <typename T> T gcd(const T& a, const T& b, T& x, T& y) { if (b == 0) { x = a < 0 ? -1 : 1; y = 0; return x * a; } T d{gcd(b, a % b, y, x)}; y -= (a / b) * x; return d; } template <typename T> T lcm(const T& a, const T& b) { T r{a / gcd(a, b) * b}; return r < 0 ? -r : r; } template <typename T> T mod_add(const T& a, const T& b, const T& m) { if (a < m - b) return a + b; return a - (m - b); } template <typename T> T mod_sub(const T& a, const T& b, const T& m) { if (a < b) return a + (m - b); return a - b; } template <typename T> T mod_mul(T a, T b, const T& m) { if (b > a) swap(a, b); T r{0}; while (b > 0) { if (b % 2 == 1) r = mod_add(r, a, m); a = mod_add(a, a, m); b /= 2; } return r; } template <typename T1, typename T2> T1 mod_pow(T1 a, T2 n, const T1& m) { if (m == 1) return 0; T1 r{1}; while (n > 0) { if (n % 2 == 1) r = mod_mul(r, a, m); a = mod_mul(a, a, m); n /= 2; } return r; } template <typename T> T mod_inv(const T& a, const T& m) { T x, y; T d{gcd(a, m, x, y)}; if (d != 1) return 0; if (x < 0) x += m; return x; } template <typename T> T phi(T n) { T t = std::sqrt(n) + EPS; T r = n; for (T i = 2; i <= t; ++i) { if (n % i == 0) { r -= r / i; do { n /= i; } while (n % i == 0); t = std::sqrt(n) + EPS; } } if (n > 1) r -= r / n; return r; } class LinearPhi { public: LinearPhi(uint32_t n = 1 << 20) : size_{n} { assert(size_ > 0 && Size should be nonzero ); assert(size_ <= INT32_MAX && Size should fit into an 32-bit signed int ); uint32_t sz = (size_ + 1) / 2; phi_ = new int32_t[sz]; mark_.assign(sz, false); phi_[0] = 1; if (size_ > 1) primes_.push_back(2); for (uint32_t i = 1; i < sz; ++i) { uint32_t val = i << 1 | 1; if (!mark_[i]) { phi_[i] = val - 1; primes_.push_back(val); } if (val < sz) { for (uint32_t j = 1; j < primes_.size(); ++j) { uint32_t t = val * primes_[j]; if (t > size_) break; mark_[t >> 1] = true; if (val % primes_[j] == 0) { phi_[t >> 1] = phi_[i] * primes_[j]; break; } phi_[t >> 1] = phi_[i] * (primes_[j] - 1); } } } } LinearPhi(const LinearPhi&) = delete; LinearPhi& operator=(const LinearPhi&) = delete; ~LinearPhi() { delete[] phi_; phi_ = nullptr; } const uint32_t& size() const { return size_; } int32_t phi(int32_t i) const { int32_t r = 1; int k = __builtin_ctz(i); if (k > 0) { i >>= k; r <<= k - 1; } return r * phi_[i >> 1]; } int32_t operator[](int32_t i) const { return phi(i); } const std::vector<int32_t>& primes() const { return primes_; } private: int32_t* phi_ = nullptr; std::vector<bool> mark_; uint32_t size_; std::vector<int32_t> primes_; }; class Sieve { public: Sieve(uint32_t n = 1 << 20) : size_{n} { assert(size_ > 0 && Size should be nonzero ); assert(size_ <= INT32_MAX && Size should fit into an 32-bit signed int ); uint32_t sz = (size_ + 1) / 2; prime_.assign(sz, true); prime_[0] = false; for (uint32_t i = 1; i < sz; ++i) { uint32_t val = i << 1 | 1; if (prime_[i] && val * 1ULL * val <= size_) for (uint32_t j = val * val; j <= size_; j += 2 * val) prime_[j >> 1] = false; } } Sieve(const Sieve&) = delete; Sieve& operator=(const Sieve&) = delete; const uint32_t& size() const { return size_; } bool isPrime(const int32_t& i) const { if (i % 2 == 0) return i == 2; return prime_[i >> 1]; } bool operator[](const int32_t& i) const { return isPrime(i); } private: uint32_t size_; std::vector<bool> prime_; }; class LinearSieve { public: LinearSieve(uint32_t n = 1 << 20) : size_{n} { assert(size_ > 0 && Size should be nonzero ); assert(size_ <= INT32_MAX && Size should fit into an 32-bit signed int ); uint32_t sz = (size_ + 1) / 2; lpf_ = new uint16_t[sz]; std::fill(lpf_, lpf_ + sz, 0); lpf_[0] = 1; if (size_ > 1) primes_.push_back(2); for (uint32_t i = 1; i < sz; ++i) { uint32_t val = i << 1 | 1; if (!lpf_[i]) primes_.push_back(val); if (val < sz) { for (uint32_t j = 1; j < primes_.size(); ++j) { uint32_t t = val * primes_[j]; if (t > size_) break; lpf_[t >> 1] = static_cast<uint16_t>(primes_[j]); if (lpf_[t >> 1] == (lpf_[i] ? lpf_[i] : val)) break; } } } } LinearSieve(const LinearSieve&) = delete; LinearSieve& operator=(const LinearSieve&) = delete; ~LinearSieve() { delete[] lpf_; lpf_ = nullptr; } const uint32_t& size() const { return size_; } bool isPrime(const int32_t& i) const { if (i % 2 == 0) return i == 2; return !lpf_[i >> 1]; } bool operator[](const int32_t& i) const { return isPrime(i); } int32_t lpf(const int32_t& i) const { if (i % 2 == 0) return 2; if (!lpf_[i >> 1]) return i; return lpf_[i >> 1]; } int32_t phi(int32_t i) const { int32_t r = 1; int k = __builtin_ctz(i); if (k > 0) { i >>= k; r <<= k - 1; } int32_t n, t = lpf_[i >> 1] ? lpf_[i >> 1] : i; while (t != 1) { r *= (t - 1); i /= t; n = lpf_[i >> 1] ? lpf_[i >> 1] : i; while (n == t) { r *= n; i /= t; n = lpf_[i >> 1] ? lpf_[i >> 1] : i; } t = n; } return r; } const std::vector<int32_t>& primes() const { return primes_; } private: uint16_t* lpf_ = nullptr; uint32_t size_; std::vector<int32_t> primes_; }; } // namespace algo using namespace std; using namespace algo; namespace task { int x1, y1, x2, y2, x3, y3; int main() { cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; cout << 3 << n ; cout << x3 - x1 + x2 << << y3 - y1 + y2 << n ; cout << x1 - x2 + x3 << << y1 - y2 + y3 << n ; cout << x2 - x3 + x1 << << y2 - y3 + y1 << n ; return 0; } } // namespace task int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.precision(11); cout.setf(ios::fixed); return task::main(); }
#include <bits/stdc++.h> using namespace std; int main() { int t, i; string s; cin >> t; while (t--) { cin >> s; int cap = 0, sm = 0, dig = 0; for (i = 0; i < s.size(); i++) { if (s[i] >= 65 && s[i] <= 90) { cap++; } else if (s[i] >= 97 && s[i] <= 122) { sm++; } else if (s[i] >= 48 && s[i] <= 57) { dig++; } } if (sm == 0) { if (cap > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 65 && s[i] <= 90) { s[i] = a ; break; } } } else if (dig > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 48 && s[i] <= 57) { s[i] = a ; break; } } } else { s = s + a ; } } if (cap == 0) { if (sm > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 97 && s[i] <= 122) { s[i] = A ; break; } } } else if (dig > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 48 && s[i] <= 57) { s[i] = A ; break; } } } else { s = s + A ; } } if (dig == 0) { if (cap > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 65 && s[i] <= 90) { s[i] = 1 ; break; } } } else if (sm > 1) { for (i = 0; i < s.size(); i++) { if (s[i] >= 97 && s[i] <= 122) { s[i] = 1 ; break; } } } else { s = s + 1 ; } } cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool BeauYear(int y) { int a = y % 10; int b = (y / 10) % 10; int c = (y / 100) % 10; int d = (y / 1000) % 10; if (a != b && a != c && a != d && b != c && b != d && c != d) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int year; cin >> year; for (int i = year + 1; i <= 9012; i++) { if (BeauYear(i) == true) { cout << i; i = 9013; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, a, b, q, p[N], d[N]; vector<pair<int, int> > g[N]; int dp[N][17], v, ud[N], ans[N]; bool vis[N]; void dfs(int u) { vis[u] = 1; for (int i = 0; i < (int)g[u].size(); ++i) { v = g[u][i].first; if (!vis[v]) { p[v] = u; d[v] = d[u] + 1; dfs(v); } } } int lca(int a, int b) { if (d[a] < d[b]) swap(a, b); int log = 0; for (; (1 << log) <= d[a]; ++log) ; --log; for (int i = log; i >= 0; --i) { if (d[a] - (1 << i) >= d[b]) a = dp[a][i]; } if (a == b) return a; for (int i = log; i >= 0; --i) { if (dp[a][i] != dp[b][i]) { a = dp[a][i]; b = dp[b][i]; } } return p[a]; } int DFS(int u) { vis[u] = 1; for (int i = 0; i < (int)g[u].size(); ++i) { int v = g[u][i].first; if (!vis[v]) { ud[u] += DFS(v); ans[g[u][i].second] = ud[v]; } } return ud[u]; } int main(int argc, char **argv) { scanf( %d , &n); for (int i = 1; i < n; ++i) { scanf( %d%d , &a, &b); g[a].push_back(make_pair(b, i)); g[b].push_back(make_pair(a, i)); } memset(p, -1, sizeof p); memset(dp, -1, sizeof dp); dfs(1); for (int i = 0; i <= n; ++i) dp[i][0] = p[i]; for (int j = 1; (1 << j) <= n; ++j) { for (int i = 0; i <= n; ++i) { if (dp[i][j - 1] != -1) dp[i][j] = dp[dp[i][j - 1]][j - 1]; } } scanf( %d , &q); int LCA; while (q--) { scanf( %d%d , &a, &b); LCA = lca(a, b); ++ud[a], ++ud[b], ud[LCA] -= 2; } memset(vis, 0, sizeof vis); DFS(1); for (int i = 1; i < n; ++i) { if (i > 1) printf( ); printf( %d , ans[i]); } puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; string tobinary(long long x) { string ret; char ch; while (x) ch = 0 + (x % 2), ret = ch + ret, x /= 2; return ret; } long long todecimal(string s) { int sz = s.size(); long long ret = 0; for (int i = 0; i < sz; i++) ret = 2 * ret + (s[i] - 0 ); return ret; } bool _greater(string s, string t) { int n = s.size(), m = t.size(); if (n > m) return true; if (n < m) return false; for (int i = 0; i < n; i++) { if (s[i] > t[i]) return true; else if (s[i] < t[i]) return false; } return false; } long long eval(long long l, long long r) { string s = tobinary(l), t = tobinary(r), ret = s; while (1) { if (_greater(s, t)) break; ret = s; int sz = s.size(); bool ok = false; for (int i = sz - 1; i >= 0; i--) if (s[i] == 0 ) { s[i] = 1 ; ok = true; break; } if (!ok) { s = 1 + s; int n = s.size(), m = t.size(); if (n > m) break; } } return todecimal(ret); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; while (n--) { long long l, r; cin >> l >> r; cout << eval(l, r) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > points; pair<int, int> dif(pair<int, int> pa, pair<int, int> pb) { pair<int, int> res; res.first = pb.first - pa.first; res.second = pb.second - pa.second; return res; } int dot(pair<int, int> a, pair<int, int> b) { return a.first * b.first + a.second * b.second; } double dist(pair<int, int> a, pair<int, int> b) { return ((double)((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second))); } double len(pair<int, int> a) { return dist(a, make_pair(0, 0)); } int main() { for (int i = 0; i < 8; i++) { int x, y; cin >> x >> y; points.push_back(make_pair(x, y)); } vector<int> perm; for (int i = 0; i < 8; i++) perm.push_back(i); int cnt = 0; bool found = false; do { cnt++; bool fs = false, ss = false; bool fis = true, sif = true; pair<int, int> a = points[perm[0]]; pair<int, int> b = points[perm[1]]; pair<int, int> c = points[perm[2]]; pair<int, int> d = points[perm[3]]; pair<int, int> e = points[perm[4]]; pair<int, int> f = points[perm[5]]; pair<int, int> g = points[perm[6]]; pair<int, int> h = points[perm[7]]; pair<int, int> ab = dif(a, b); pair<int, int> bc = dif(b, c); pair<int, int> cd = dif(c, d); pair<int, int> da = dif(d, a); fs = (dot(ab, bc) == 0 && dot(bc, cd) == 0 && dot(cd, da) == 0 && dot(da, ab) == 0 && fabs(len(ab) - len(bc)) < 0.01 && fabs(len(bc) - len(cd)) < 0.01 && fabs(len(cd) - len(da)) < 0.01); pair<int, int> ef = dif(e, f); pair<int, int> fg = dif(f, g); pair<int, int> gh = dif(g, h); pair<int, int> he = dif(h, e); ss = (dot(ef, fg) == 0 && dot(fg, gh) == 0 && dot(gh, he) == 0 && dot(he, ef) == 0 && fabs(len(ef) - len(gh)) < 0.01 && fabs(len(fg) - len(he)) < 0.01); if (ss && fs) { found = true; break; } } while (next_permutation(perm.begin(), perm.end())); if (found) { cout << YES << endl; for (int i = 0; i < 4; i++) { if (i != 0) cout << ; cout << perm[i] + 1; } cout << endl; for (int i = 4; i < 8; i++) { if (i != 4) cout << ; cout << perm[i] + 1; } cout << endl; } else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n; char s[5001]; int dp[5001][5001]; int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( n%c , &s[i]); } dp[1][0] = 1; for (int j = 1; j <= n; ++j) { dp[1][j] = 0; } for (int i = 2; i <= n; ++i) { if (s[i - 1] == f ) { for (int j = 1; j <= n; ++j) { dp[i][j] = dp[i - 1][j - 1]; } } else { int x = 0; for (int j = n; j >= 0; --j) { x += dp[i - 1][j]; if (x >= mod) { x -= mod; } dp[i][j] = x; } } } int res = 0; for (int i = 0; i <= n; ++i) { res += dp[n][i]; if (res >= mod) { res -= mod; } } cout << res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; using lli = long long; using Double = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s[9][3]; for (int i = 0; i < 9; ++i) { for (int j = 0; j < 3; ++j) { cin >> s[i][j]; } } int x, y, cnt = 0; cin >> x >> y, --x, --y; for (auto c : s[x % 3 * 3][y % 3]) cnt += c == . ; for (auto c : s[x % 3 * 3 + 1][y % 3]) cnt += c == . ; for (auto c : s[x % 3 * 3 + 2][y % 3]) cnt += c == . ; if (cnt == 0) { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 3; ++j) { for (auto c : s[i][j]) { cout << (c == . ? ! : c); } cout << n [j == 2]; } if (i % 3 == 2) cout << n ; } } else { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 3; ++j) { for (auto c : s[i][j]) { cout << (c == . and i / 3 == x % 3 and j == y % 3 ? ! : c); } cout << n [j == 2]; } if (i % 3 == 2) cout << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; char str[606]; long long tmp[80]; long long mat[2000][32]; int see[2000]; int main() { int M; scanf( %d , &M); for (int i = 0; i < M; i++) { scanf( %s , str); int len = 0; while (str[len]) len++; for (int j = 0; j < 80; j++) tmp[j] = 0; for (int j = 0; j < len; j++) { long long &t = tmp[(len - j - 1) / 8]; t = t * 10 + str[j] - 0 ; } for (int j = 0; j < 250; j++) { long long t = tmp[0] & 255; for (int k = 0; k < 8; k++) if (t & (1 << k)) mat[j * 8 + k][i / 64] |= 1ll << (i % 64); for (int k = 79; k; k--) { tmp[k - 1] += (tmp[k] & 255) * 100000000ll; tmp[k] >>= 8; } tmp[0] >>= 8; } } for (int i = 0, j = 0; i < 2000 && j < M; j++) { bool seen = false; for (int k = i; k < 2000; k++) { if (mat[k][j / 64] & (1ll << (j % 64))) { for (int l = j / 64; l < 32; l++) { swap(mat[i][l], mat[k][l]); } seen = true; break; } } if (!seen) { int cnt = 0; for (int k = 0; k < i; k++) if (mat[k][j / 64] & (1ll << (j % 64))) cnt++; printf( %d , cnt); for (int k = 0; k < i; k++) if (mat[k][j / 64] & (1ll << (j % 64))) printf( %d , see[k]); puts( ); continue; } see[i] = j; printf( 0 n ); for (int k = 0; k < 2000; k++) if (k != i && (mat[k][j / 64] & (1ll << (j % 64)))) { for (int l = j / 64; l < 32; l++) mat[k][l] ^= mat[i][l]; } i++; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxm = 2e5 + 10; set<int> g; queue<set<int> > q; int m, n, x, y; int dfs(int top) { if (abs(top) <= 1) return 0; int now = 0; if ((-top) % 2 == 0) now = top / 2; else now = (top + 1) / 2; if (g.find(now) == g.end()) { g.erase(top); g.insert(now); return 1; } else return dfs(now); } int main() { cin >> m; for (int i = 1; i <= m; i++) { cin >> x; g.insert(-x); } for (;;) { int top = *g.begin(); if (!dfs(top)) break; } for (auto e : g) cout << -e << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v; int qtd[100001]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { int a; scanf( %d , &a); v.push_back(a); } sort(v.begin(), v.end()); int cont = 0; for (int i = 1, tmp = 0; i < n; i++) { if (v[i] > v[i - 1]) { cont += i - tmp; tmp = i; } qtd[i] = cont; } int ans = 0; int used = 0; for (int i = 0; i < n; i++) { if (qtd[i] > used) { used++; ans++; } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; struct W { int t, l, r; } w[100010]; int n, m, s, f; int main() { while (cin >> n >> m >> s >> f) { for (int i = (0); i < (m); i++) { cin >> w[i].t >> w[i].l >> w[i].r; } int t = 1, p = 0; int dir = f > s ? 1 : -1; while (s != f) { if (p < m && w[p].t == t && ((w[p].l <= s && s <= w[p].r) || (w[p].l <= s + dir && s + dir <= w[p].r))) { cout << X ; } else { s += dir; cout << (dir == 1 ? R : L ); } if (++t > w[p].t) p++; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; int n, d, p[30005], dp1[60005][700]; int occ[30005]; int main() { scanf( %d%d , &n, &d); int xb, x2, x3; for (xb = 0; xb < n; xb++) { scanf( %d , &p[xb]); p[xb]--; occ[p[xb]]++; } d--; int res = 0; for (xb = 0; xb < 60005; xb++) { for (x3 = 0; x3 < 700; x3++) dp1[xb][x3] = -inf; } dp1[d][350] = 0; for (xb = d; xb < 30000; xb++) { for (x3 = 0; x3 < 700; x3++) { if (dp1[xb][x3] == -inf) continue; int tv = d + x3 - 349, adg = occ[xb]; if (tv > 0) res = (((res) > (adg + dp1[xb][x3])) ? (res) : (adg + dp1[xb][x3])); if (tv > 1) dp1[xb + tv - 1][x3 - 1] = (((dp1[xb + tv - 1][x3 - 1]) > (dp1[xb][x3] + adg)) ? (dp1[xb + tv - 1][x3 - 1]) : (dp1[xb][x3] + adg)); if (tv > 0) dp1[xb + tv][x3] = (((dp1[xb + tv][x3]) > (dp1[xb][x3] + adg)) ? (dp1[xb + tv][x3]) : (dp1[xb][x3] + adg)); if (tv > -1) dp1[xb + tv + 1][x3 + 1] = (((dp1[xb + tv + 1][x3 + 1]) > (dp1[xb][x3] + adg)) ? (dp1[xb + tv + 1][x3 + 1]) : (dp1[xb][x3] + adg)); } } printf( %d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; int t, cnt[N]; int mx; int check(int row, int col, int n, int m) { if (row < 0 || row >= n || col < 0 || col >= m) return 0; int i = 0, j = 0, ok = 1; for (; i < n; i++) { for (j = 0; j < m; j++) { if (!cnt[abs(row - i) + abs(col - j)]--) { ok = 0; break; } } if (!ok) break; } if (ok) { cout << n << << m << endl; cout << row + 1 << << col + 1 << endl; return 1; } for (int a = 0; a <= i; a++) for (int b = 0; b < m && (a != i || b <= j); b++) ++cnt[abs(row - a) + abs(col - b)]; return 0; } bool solve(int row, int n, int m, int mxi, int mxj) { if (check(row, mxj - (mx - abs(row - mxi)), n, m)) return 1; return check(row, mxj + (mx - abs(row - mxi)), n, m); } int main() { scanf( %d , &t); for (int i = 0; i < t; ++i) { int v; scanf( %d , &v); ++cnt[v]; mx = max(mx, v); } if (cnt[0] != 1) return puts( -1 ); int a; if (cnt[1] != 4) a = 0; else { a = 1; while (cnt[a + 1] == (a + 1) * 4) a++; } for (int n = 1; n * n <= t; n++) { if (t % n) continue; int m = t / n; if (solve(a, n, m, 0, 0)) return 0; if (solve(a, n, m, 0, m - 1)) return 0; if (solve(a, n, m, n - 1, 0)) return 0; if (solve(a, n, m, n - 1, m - 1)) return 0; if (solve(a, m, n, 0, 0)) return 0; if (solve(a, m, n, 0, n - 1)) return 0; if (solve(a, m, n, m - 1, 0)) return 0; if (solve(a, m, n, m - 1, n - 1)) return 0; } return puts( -1 ); }
#include <bits/stdc++.h> int main() { int i, j; int str[6]; int num[6]; char ch; for (i = 0; i < 6; i++) { str[i] = 0; num[i] = 0; } for (i = 0; i < 6; i++) { scanf( %c , &ch); switch (ch) { case R : str[0]++; break; case O : str[1]++; break; case Y : str[2]++; break; case G : str[3]++; break; case B : str[4]++; break; case V : str[5]++; break; } } for (j = 0; j < 6; j++) { num[str[j]]++; } switch (num[0]) { case 0: printf( 30 n ); break; case 1: printf( 15 n ); break; case 2: switch (num[1]) { case 2: printf( 8 n ); break; case 3: printf( 5 n ); break; } break; case 3: switch (num[1]) { case 2: printf( 2 n ); break; case 1: printf( 3 n ); break; case 0: printf( 6 n ); break; } break; case 4: switch (num[1]) { case 1: printf( 1 n ); break; case 0: printf( 2 n ); break; } break; case 5: printf( 1 n ); break; } }
#include <bits/stdc++.h> using namespace std; int Head[1000010], Next[1000010], Go[1000010], Val[1000010], iscut[1000010], Len[1000010], Dep[1000010], Fa[400010][21], Cnt = 1, n, m, q; int Head2[1000010], Next2[1000010], Go2[1000010], Val2[1000010], Cnt2 = 1; int DFN[1000010], MIN[1000010], ins[1000010], Bel[1000010], vis[1000010], tim = 0, tt = 0; vector<int> S; void addedge(int x, int y) { Go[++Cnt] = y; Next[Cnt] = Head[x]; Head[x] = Cnt; } void addedge2(int x, int y, int v) { Go2[++Cnt2] = y; Next2[Cnt2] = Head2[x]; Head2[x] = Cnt2; Val2[Cnt2] = v; } void Tarjan(int x, int f = 0) { ins[x] = 1; S.push_back(x); DFN[x] = ++tim; MIN[x] = DFN[x]; int las = x; for (int T = Head[x]; T; T = Next[T]) if (Go[T] != f) { if (!DFN[Go[T]]) { Tarjan(Go[T], x); MIN[x] = min(MIN[x], MIN[Go[T]]); if (DFN[x] <= MIN[Go[T]]) { tt++; for (;;) { if (S.back() == las) break; addedge2(tt + n + 1, S.back(), 1); addedge2(S.back(), tt + n + 1, 1); S.pop_back(); } addedge2(x, tt + n + 1, 1); addedge2(tt + n + 1, x, 1); } else las = S.back(); } else if (ins[Go[T]]) MIN[x] = min(MIN[x], DFN[Go[T]]); } ins[x] = 0; } void DFS2(int x, int dep = 1, int len = 0) { vis[x] = 1; Len[x] = len; Dep[x] = dep; for (int T = Head[x]; T; T = Next[T]) if (!vis[Go[T]]) { Fa[Go[T]][0] = x; DFS2(Go[T], dep + 1, len + Val[T]); } } int lca(int x, int y) { if (Dep[x] < Dep[y]) swap(x, y); int det = Dep[x] - Dep[y]; for (int i = 18; i >= 0; i--) if ((det >> i) & 1) x = Fa[x][i]; if (x == y) return x; for (int i = 18; i >= 0; i--) if (Fa[x][i] != Fa[y][i]) x = Fa[x][i], y = Fa[y][i]; return Fa[x][0]; } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= m; i++) { int a, b; scanf( %d%d , &a, &b); addedge(a, b); addedge(b, a); } for (int i = 1; i <= n; i++) if (!DFN[i]) Tarjan(i); memcpy(Head, Head2, sizeof Head); memcpy(Next, Next2, sizeof Next); memcpy(Go, Go2, sizeof Go); memcpy(Val, Val2, sizeof Val); for (int i = 1; i <= n + tt + 1; i++) if (!vis[i]) DFS2(i); for (int i = 1; i <= 20; i++) for (int j = 1; j <= n + tt + 1; j++) Fa[j][i] = Fa[Fa[j][i - 1]][i - 1]; for (int i = 1; i <= q; i++) { int a, b; scanf( %d%d , &a, &b); printf( %d n , (Len[a] + Len[b] - 2 * Len[lca(a, b)]) / 2); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = (x << 1) + (x << 3) - 0 + ch; ch = getchar(); } return x * f; } int main() { priority_queue<int> q; int n = read(), T = read(); int ans = 0; for (int i = 1; i <= n; i++) { int x = read(); x = max(x, i); while (!q.empty() && q.top() >= T - i) { q.pop(); } if (x < T) q.push(x - i); ans = max(ans, (int)q.size()); } printf( %d n , ans); }
#include <bits/stdc++.h> using namespace std; int a[1005]; int b[1005]; int h; bool cmp(int x, int y) { return x > y; } bool check(int x) { for (int i = 1; i <= x; i++) { b[i] = a[i]; } sort(b + 1, b + x + 1, cmp); long long sum = 0; for (int i = 1; i <= x; i += 2) { sum += (long long)(b[i]); } if (sum <= h) { return 1; } else { return 0; } } int main() { int n; cin >> n >> h; for (int i = 1; i <= n; i++) { cin >> a[i]; } int l = 1, r = n, mid; while (l < r) { mid = (l + r + 1) / 2; if (check(mid)) { l = mid; } else { r = mid - 1; } } cout << l; return 0; }
#include <bits/stdc++.h> using namespace std; inline int in() { int x; scanf( %d , &x); return x; } template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j]; return i; } template <typename T> string join(const vector<T>& v) { stringstream s; for (int i = 0; i < ((int)(v).size()); ++i) s << << v[i]; return s.str().substr(1); } template <typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { if ((int)(v).size()) o << join(v); return o; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } template <typename T1, typename T2> inline ostream& operator<<(ostream& o, const pair<T1, T2>& v) { return o << v.first << , << v.second; } template <typename T> inline long long int suma(const vector<T>& a) { long long int res(0); for (auto&& x : a) res += x; return res; } const double eps = 1e-10; const long long int LINF = 1001002003004005006ll; const int INF = 1001001001; const int MX = 200005; int n, m; int dp[55][55][55]; struct Q { int l, r, x, c; int cost(int i, int h) { if (l <= i && i < r && h > x) return c; return 0; } }; vector<Q> q; int dfs(int l, int r, int h) { if (l == r) return 0; int& now = dp[l][r][h]; if (now != -1) return now; now = 0; if (h) now = dfs(l, r, h - 1); for (int i = l; i < r; ++i) { int c = h * h; for (int k = 0; k < (m); ++k) { if (l <= q[k].l && q[k].r <= r) c -= q[k].cost(i, h); } c += dfs(l, i, h) + dfs(i + 1, r, h); (now = max(now, c)); } return now; } int main() { int h; cin >> n >> h >> m; q = vector<Q>(m); for (int i = 0; i < (m); ++i) { int l, r, x, c; scanf( %d%d%d%d , &l, &r, &x, &c); --l; q[i] = (Q){l, r, x, c}; } for (int i = 0; i < (n); ++i) for (int j = 0; j < (n + 1); ++j) for (int k = 0; k < (h + 1); ++k) dp[i][j][k] = -1; int ans = dfs(0, n, h); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } p[8]; int cmp(node a, node b) { if (a.x == b.x) return (a.y < b.y); return a.x < b.x; } int main() { while (scanf( %d %d , &p[0].x, &p[0].y) != EOF) { int cnt1 = 0; int cnt2 = 0; int cnt3 = 0; int cnt4 = 0; for (int i = 1; i < 8; i++) { scanf( %d %d , &p[i].x, &p[i].y); } sort(p, p + 8, cmp); node zuoshang = p[0]; node youxia = p[7]; for (int i = 1; i < 7; i++) { if (p[i].x == zuoshang.x) cnt1++; if (p[i].y == zuoshang.y) cnt2++; if (p[i].x == youxia.x) cnt3++; if (p[i].y == youxia.y) cnt4++; } if (cnt1 == 2 && cnt2 == 2 && cnt3 == 2 && cnt4 == 2 && p[1].y == p[6].y && p[4].x == p[3].x) puts( respectable ); else puts( ugly ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { double x, y, z, a, b, c, sq; cin >> x; a = 1.0, b = x, c = x; z = (b * b) - (4 * a * c); if (z < 0) { cout << N << endl; continue; } y = ((-b + (sqrt((b * b) - (4 * a * c)))) / 2 * a); if (y != 0) y = -1 * y; cout << fixed << setprecision(9) << Y << (x - y) << << y << endl; } }
#include <bits/stdc++.h> using namespace std; int r1, r2; int c1, c2; int b, e, k; int main() { ios_base::sync_with_stdio(0); b = e = k = 0; cin >> r1 >> c1; cin >> r2 >> c2; if (r1 > r2) { swap(r1, r2); swap(c1, c2); } b = (r1 == r2 || c1 == c2) ? 1 : 2; e = ((((r1 & 1) == (c1 & 1)) && ((r2 & 1) == (c2 & 1))) || (((r1 & 1) != (c1 & 1)) && ((r2 & 1) != (c2 & 1)))) ? 2 : 0; e = (abs(r1 - r2) == abs(c1 - c2) && e != 0) ? 1 : e; k = min(abs(r1 - r2), abs(c1 - c2)) + max(abs(r1 - r2), abs(c1 - c2)) - min(abs(r1 - r2), abs(c1 - c2)); cout << b << << e << << k << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 789; const int M = 2e5 + 5; const int NN = 1e5 + 55; const long long mod = 1e9 + 7; const long long inf = 0x3f3f3f3f; const long long INF = 1LL << 62; bitset<2000> P[N], S[N], T; char ms[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; P[0].reset(); for (int i = 0; i < n; i++) { cin >> ms[i]; if (i) P[i] = P[i - 1]; for (int j = 0; j < m; j++) { if (ms[i][j] == 1 ) P[i][j] = 1; } } for (int i = n - 1; i >= 0; i--) { if (i != n - 1) S[i] = S[i + 1]; for (int j = 0; j < m; j++) { if (ms[i][j] == 1 ) S[i][j] = 1; } } bool f = 0; for (int i = 0; i < n; i++) { T.reset(); if (i - 1 >= 0) T |= P[i - 1]; if (i + 1 < n) T |= S[i + 1]; if (T.count() == m) f = 1; } if (f) cout << YES << endl; else cout << NO << endl; }
#include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int NMax = 1e6 + 50; string convert(const string &s) { string ret = @ ; for (int i = 0; i < (int)s.size(); ++i) { ret += # + s.substr(i, 1); } ret += #$ ; return ret; } int P[NMax * 2]; void Solve() { string s; cin >> s; int n = (int)s.size(); int a = 0; int b = n - 1; while (a <= b) { if (a == b) { cout << s << n ; return; } if (s[a] == s[b]) { ++a; --b; if (a > b) { cout << s << n ; return; } } else { string nw = convert(s); int cen = 0, r = 0; for (int i = 0; i < (int)nw.size(); ++i) P[i] = 0; for (int i = 1; i < (int)nw.size() - 1; ++i) { int mir = cen - (i - cen); if (r > i) { P[i] = min(r - i, P[mir]); } while (nw[i + 1 + P[i]] == nw[i - 1 - P[i]]) { P[i]++; } if (i + P[i] > r) { cen = i; r = i + P[i]; } } int best = 0; int pos = 0; for (int i = 1; i < (int)nw.size() - 1; i++) { int start = (i - 1 - P[i]) / 2; int fin = start + P[i] - 1; if (start < a) { int mv = a - start; start += mv; fin -= mv; } if (fin > b) { int mv = fin - b; start += mv; fin -= mv; } if (start != a && fin != b) continue; if (start <= fin && (fin - start + 1) > best) { best = fin - start + 1; pos = start; } } for (int i = 0; i < a; ++i) cout << s[i]; for (int i = pos; i < pos + best; ++i) cout << s[i]; for (int i = a - 1; i >= 0; --i) cout << s[i]; cout << n ; return; } } } int main() { Boost(); int t; cin >> t; while (t--) { Solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; bool is_prefix(string s, string comp) { bool res = false; int sz = s.size(); int comp_sz = comp.size(); if (sz > comp_sz) return res; else { res = true; for (int i = 0; i < sz; i++) { if (s[i] != comp[i]) { res = false; break; } } } return res; } int main(int argc, char* argv[]) { int n; string s; vector<string> res; cin >> s; cin >> n; for (int i = 0; i < n; i++) { string t; cin >> t; if (is_prefix(s, t)) res.push_back(t); } if (res.size() == 0) { cout << s; } else { sort(res.begin(), res.end()); cout << res[0]; } return 0; }
#include <bits/stdc++.h> int du[100010], fa[100010], s1[100010], s2[100010]; int find(int x) { if (x != fa[x]) fa[x] = find(fa[x]); return fa[x]; } int main() { int n, m, x, y, i, j, k, sum; scanf( %d%d , &n, &m); for (i = 1; i <= n; i++) fa[i] = i; memset(du, 0, sizeof(du)); memset(s1, 0, sizeof(s1)); memset(s2, 0, sizeof(s2)); for (i = 1; i <= m; i++) { scanf( %d%d , &x, &y); j = find(x); k = find(y); du[x]++; du[y]++; if (j != k) { if (j < k) fa[k] = j; else fa[j] = k; } } for (i = 1; i <= n; i++) { j = find(i); s1[j] = s1[j] + du[i]; s2[j]++; } sum = 0; for (i = 1; i <= n; i++) if (s2[i] != 0) { j = s1[i] / 2; if (j < s2[i]) sum = sum + s2[i] - j; } printf( %d n , sum); }
#include <bits/stdc++.h> const int maxn = 100001; int n, val[maxn], cL[maxn], cR[maxn], ord[maxn], tot, in[maxn], out[maxn], bit[maxn], ans; bool vis[maxn]; void dfs(int u) { if (u == -1) return; in[u] = ++tot; dfs(cL[u]); dfs(cR[u]); out[u] = tot; } void bit_add(int x, int v) { for (; x <= n; x += x & -x) bit[x] += v; } int bit_sum(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += bit[x]; return ret; } bool cmp(int const &x, int const &y) { return val[x] < val[y]; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d%d%d , val + i, cL + i, cR + i); if (cL[i] != -1) vis[cL[i]] = 1; if (cR[i] != -1) vis[cR[i]] = 1; ord[i] = i; } std::sort(ord + 1, ord + n + 1, cmp); for (int i = 1; i <= n; ++i) if (!vis[i]) dfs(i); for (int i = 1; i <= n; ++i) if (cR[i] != -1) { bit_add(in[cR[i]], 1); bit_add(out[cR[i]] + 1, -1); } for (int i = 1, j = 1; i <= n;) { for (; j <= n && val[ord[j]] <= val[ord[i]]; ++j) { int u = ord[j]; if (cL[u] != -1) { bit_add(in[cL[u]], 1); bit_add(out[cL[u]] + 1, -1); } if (cR[u] != -1) { bit_add(in[cR[u]], -1); bit_add(out[cR[u]] + 1, 1); } } int cnt = 0, flg = 0; for (int v = val[ord[i]]; i <= n && val[ord[i]] == v; ++i, ++cnt) flg |= !bit_sum(in[ord[i]]); if (!flg) ans += cnt; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 210; int n, m, x; long long dp[maxn][maxn], v[maxn], ans = -1; void read_and_parse() { scanf( %d%d%d , &n, &m, &x); for (int i = 1; i <= n; i++) scanf( %d , &v[i]); } void solve() { memset(dp, 0xcf, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= x; j++) for (int k = max(0, i - m); k < i; k++) dp[i][j] = max(dp[i][j], dp[k][j - 1] + v[i]); for (int i = n - m + 1; i <= n; i++) ans = max(ans, dp[i][x]); printf( %lld n , ans); } int main() { read_and_parse(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; void checkDefine(); string x; string y; string z; int main() { cin >> x >> y; long long int n = x.size(); for (int i = (0), _b = (n - 1); i <= _b; i++) { if (x[i] < y[i]) { cout << -1; return 0; } } cout << y; return 0; } void checkDefine() { long long int n, a[200005]; map<long long int, long long int> m; cin >> n; for (int i = (0), _b = (n - 1); i <= _b; i++) { cin >> a[i]; m[a[i]]++; } string s; cin >> s; { cout << s << = ; cout << (s) << endl; }; { cout << a << = ; for (int _ = 0, _a = (n); _ < _a; _++) cout << a[_] << ; cout << endl; }; { cout << ------------ << = ; cout << ( ------------ ) << endl; }; for (__typeof(m.begin()) it = m.begin(); it != m.end(); ++it) { cout << it->first << << it->second << endl; } }
#include <bits/stdc++.h> using namespace std; template <class L, class R> ostream &operator<<(ostream &os, map<L, R> P) { for (auto const &vv : P) os << ( << vv.first << , << vv.second << ) ; return os; } template <class T> ostream &operator<<(ostream &os, set<T> V) { os << [ ; for (auto const &vv : V) os << vv << , ; os << ] ; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << [ ; for (auto const &vv : V) os << vv << , ; os << ] ; return os; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { os << ( << P.first << , << P.second << ) ; return os; } inline int fstoi(const string &str) { auto it = str.begin(); bool neg = 0; int num = 0; if (*it == - ) neg = 1; else num = *it - 0 ; ++it; while (it < str.end()) num = num * 10 + (*it++ - 0 ); if (neg) num *= -1; return num; } inline void getch(char &x) { while (x = getchar_unlocked(), x < 33) { ; } } inline void getstr(string &str) { str.clear(); char cur; while (cur = getchar_unlocked(), cur < 33) { ; } while (cur > 32) { str += cur; cur = getchar_unlocked(); } } template <typename T> inline bool sc(T &num) { bool neg = 0; int c; num = 0; while (c = getchar_unlocked(), c < 33) { if (c == EOF) return false; } if (c == - ) { neg = 1; c = getchar_unlocked(); } for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; return true; } template <typename T, typename... Args> inline void sc(T &num, Args &...args) { bool neg = 0; int c; num = 0; while (c = getchar_unlocked(), c < 33) { ; } if (c == - ) { neg = 1; c = getchar_unlocked(); } for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; sc(args...); } int getsum(int a, int b) { int oor, oand; cout << and << a << << b << endl; cin >> oand; cout << or << a << << b << endl; cin >> oor; return oor + oand; } int main() { int n, k; cin >> n >> k; int cr; int x, y, z; x = getsum(1, 2); y = getsum(1, 3); z = getsum(2, 3); int a[n + 1]; a[3] = (y + z - x) / 2; a[2] = (-y + z + x) / 2; a[1] = (y - z + x) / 2; for (auto i = (4); i <= (n); ++i) { int wh = getsum(1, i); a[i] = wh - a[1]; } sort(a + 1, a + n + 1); cout << finish << a[k] << endl; cout << a[k] << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const long double pi = acos(-1); const int MOD = 1e9 + 7; const int N = 2e5 + 100; int tr_min[4 * N][1 << 5]; int tr_max[4 * N][1 << 5]; vector<int> cara[N]; int n, k; void build(int node, int b, int e) { if (b == e) { for (int cord = 0; cord < (1 << k); cord++) { long long x = 0; for (int i = 0; i < k; i++) { if (cord & (1 << i)) x += cara[b][i]; else x -= cara[b][i]; } tr_max[node][cord] = tr_min[node][cord] = x; } } else { int mid = (b + e) / 2; build(2 * node, b, mid); build(2 * node + 1, mid + 1, e); for (int i = 0; i < (1 << k); i++) { tr_max[node][i] = max(tr_max[2 * node][i], tr_max[2 * node + 1][i]); tr_min[node][i] = min(tr_min[2 * node][i], tr_min[2 * node + 1][i]); } } } void update(int node, int b, int e, int i, vector<int> &cara) { if (e < i or b > i) return; if (b == e and b == i) { for (int cord = 0; cord < (1 << k); cord++) { long long x = 0; for (int i = 0; i < k; i++) { if (cord & (1 << i)) x += cara[i]; else x -= cara[i]; } tr_max[node][cord] = tr_min[node][cord] = x; } return; } int mid = (b + e) / 2; update(2 * node, b, mid, i, cara); update(2 * node + 1, mid + 1, e, i, cara); for (int i = 0; i < (1 << k); i++) { tr_max[node][i] = max(tr_max[2 * node][i], tr_max[2 * node + 1][i]); tr_min[node][i] = min(tr_min[2 * node][i], tr_min[2 * node + 1][i]); } } int query(int node, int b, int e, int i, int j, int cord) { if (e < i or b > j) return -INF; if (i <= b and j >= e) { return tr_max[node][cord]; } int mid = (b + e) / 2; return max(query(2 * node, b, mid, i, j, cord), query(2 * node + 1, mid + 1, e, i, j, cord)); } int query2(int node, int b, int e, int i, int j, int cord) { if (e < i or b > j) return INF; if (i <= b and j >= e) { return tr_min[node][cord]; } int mid = (b + e) / 2; return min(query2(2 * node, b, mid, i, j, cord), query2(2 * node + 1, mid + 1, e, i, j, cord)); } int proc(int cord, int l, int r) { int maxi = query(1, 1, n, l, r, cord); int mini = query2(1, 1, n, l, r, cord); return abs(mini - maxi); } int main() { scanf( %d %d , &n, &k); for (int i = 1; i <= n; i++) { for (int j = 0; j < k; j++) { int x; scanf( %d , &x); cara[i].push_back(x); } } int q; build(1, 1, n); scanf( %d , &q); while (q--) { int op; scanf( %d , &op); if (op == 1) { int idx; scanf( %d , &idx); vector<int> val; for (int j = 0; j < k; j++) { int x; scanf( %d , &x); val.push_back(x); } update(1, 1, n, idx, val); } else { int l, r; scanf( %d %d , &l, &r); int resp = 0; for (int i = 0; i < (1 << k); i++) resp = max(proc(i, l, r), resp); printf( %d n , resp); } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; bool operator<(pair<int, int> lhs, pair<int, int> rhs) { return (lhs.first != rhs.first) ? (lhs.first < rhs.first) : (lhs.second < rhs.second); } set<pair<int, int> > inqUp, inqDown, pendUp, pendDown; int N, M, prsis, rci; long long T, Ans[MAXN]; struct User { int id, ar, f, t; }; bool cmp_Useraraz(User lhs, User rhs) { return lhs.ar < rhs.ar; } User u[MAXN], ru[MAXN]; int Where; void Unload() { while ((inqUp.size()) && (inqUp.begin()->first == Where)) { Ans[inqUp.begin()->second] = T; inqUp.erase(inqUp.begin()); rci++; } while (((inqDown.size()) && (--inqDown.end())->first == Where)) { Ans[(--inqDown.end())->second] = T; inqDown.erase(--inqDown.end()); rci++; } } void Request() { while ((prsis != N) && (u[prsis + 1].ar == T)) { prsis++; if (u[prsis].f >= Where) pendUp.insert(make_pair(u[prsis].f, u[prsis].id)); else pendDown.insert(make_pair(u[prsis].f, u[prsis].id)); } } void Load(int id) { if (ru[id].t >= Where) inqUp.insert(make_pair(ru[id].t, id)); else inqDown.insert(make_pair(ru[id].t, id)); } void Load() { while ((pendUp.size()) && (pendUp.begin()->first == Where)) { Load(pendUp.begin()->second); pendUp.erase(pendUp.begin()); } while ((pendDown.size()) && ((--pendDown.end())->first == Where)) { Load((--pendDown.end())->second); pendDown.erase(--pendDown.end()); } } int main() { scanf( %d%d , &N, &M); int i; for (i = 1; i <= N; i++) { scanf( %d%d%d , &u[i].ar, &u[i].f, &u[i].t); u[i].id = i; ru[i] = u[i]; } sort(u + 1, u + N + 1, cmp_Useraraz); inqUp.clear(); inqDown.clear(); pendUp.clear(); pendDown.clear(); Where = 1; T = 0; prsis = rci = 0; while (rci < N) { Unload(); Request(); Load(); int pUp = inqUp.size() + pendUp.size(); int pDown = inqDown.size() + pendDown.size(); int Action = (pUp + pDown) ? ((pUp >= pDown) ? 1 : -1) : 0; int timePass = (prsis == N) ? M : (u[prsis + 1].ar - T); if (Action == 1) { if (pendUp.size()) timePass = min(timePass, pendUp.begin()->first - Where); if (inqUp.size()) timePass = min(timePass, inqUp.begin()->first - Where); } if (Action == -1) { if (pendDown.size()) timePass = min(timePass, Where - (--pendDown.end())->first); if (inqDown.size()) timePass = min(timePass, Where - (--inqDown.end())->first); } T += timePass; Where += timePass * Action; } for (i = 1; i <= N; i++) printf( %I64d n , Ans[i]); }
#include <bits/stdc++.h> using namespace std; map<char, char> mp; string check(string s) { for (int i = 0; i <= s.size() / 2; i++) if (s[i] != mp[s[s.size() - 1 - i]]) return NIE ; return TAK ; } int main() { mp[ A ] = A ; mp[ b ] = d ; mp[ d ] = b ; mp[ H ] = H ; mp[ I ] = I ; mp[ M ] = M ; mp[ O ] = O ; mp[ o ] = o ; mp[ p ] = q ; mp[ q ] = p ; mp[ T ] = T ; mp[ U ] = U ; mp[ V ] = V ; mp[ v ] = v ; mp[ W ] = W ; mp[ w ] = w ; mp[ X ] = X ; mp[ x ] = x ; mp[ Y ] = Y ; string s; cin >> s; cout << check(s) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a[3][3], b[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; a[i][j] %= 2; b[i][j] = 0; } } pair<int, int> f[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {0, 0}}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (auto m : f) { if (i + m.first < 3 && i + m.first > -1) { if (j + m.second < 3 && j + m.second > -1) { if (a[i + m.first][j + m.second] % 2) { b[i][j]++; b[i][j] %= 2; } } } } } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << (b[i][j] ^ 1); } cout << n ; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 200000; const long long mod = 1e9 + 7; long long fac[maxn + 5], inv[maxn + 5]; long long f[2005]; struct node { long long x, y; } data[2005]; long long qmod(long long a, long long b) { long long ans = 1; a = a % mod; while (b) { if (b & 1) { ans = (ans * a) % mod; } b = b / 2; a = (a * a) % mod; } return ans; } void init() { fac[0] = 1; for (long long i = 1; i <= maxn; i++) { fac[i] = (fac[i - 1] * i) % mod; } } long long com(long long n, long long m) { if (n < m) return 0; else if (n == m) return 1; else return (fac[n] * qmod(fac[m] * fac[n - m], mod - 2)) % mod; } long long Lucas(long long n, long long m) { if (m == 0) return 1; else return (com(n % mod, m % mod) * Lucas(n / mod, m / mod)) % mod; } bool cmp(node a, node b) { if (a.x < b.x) return true; else if (a.x == b.x) { if (a.y < b.y) return true; else return false; } else return false; } int main(int argc, char const *argv[]) { long long h, w, n; init(); scanf( %lld %lld %lld , &h, &w, &n); for (int i = 1; i <= n; i++) { scanf( %lld %lld , &data[i].x, &data[i].y); } sort(data + 1, data + 1 + n, cmp); memset(f, 0, sizeof(f)); n++; data[n].x = h; data[n].y = w; for (int i = 1; i <= n; i++) { long long dx, dy; dx = data[i].x - 1; dy = data[i].y - 1; f[i] = Lucas(dx + dy, dx); for (int j = 1; j < i; j++) { if (data[i].x >= data[j].x && data[i].y >= data[j].y) { dx = data[i].x - data[j].x; dy = data[i].y - data[j].y; f[i] = f[i] - (f[j] * Lucas(dx + dy, dx)) % mod; f[i] = (f[i] + mod) % mod; } } } cout << (f[n] + mod) % mod << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, x[100], s[100]; struct par { int v, id; bool operator<(const par &r) const { return v < r.v; } }; vector<par> v; int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &x[i]); v.push_back({x[i], i}); } sort(v.begin(), v.end()); vector<par> v1 = v; rotate(v1.begin(), v1.begin() + 1, v1.end()); for (int i = 0; i < (int)v.size(); i++) s[v[i].id] = v1[i].v; for (int i = 0; i < n; i++) { printf( %d , s[i]); i == n - 1 ? printf( n ) : printf( ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 3e5 + 7; unordered_map<int, vector<int>> values[2]; int t, n, q, a[oo], p[oo]; vector<int> ans; int psum(int l, int r) { return p[r] - (l ? p[l - 1] : 0); } int first(vector<int>& list, int val) { int l = 0, r = list.size(); while (l < r) { int s = (l + r) / 2; if (list[s] >= val) r = s; else l = s + 1; } if (l == list.size()) return n * 2; return list[l]; } void problem(int l, int r) { int sum = psum(l, r); if (sum == 0) return; if (sum % 2 == 0) { ans.push_back(r); problem(l, r - 1); return; } int pl = p[l] - a[l]; int vals[2] = {pl + (sum + 1) / 2 - 1, pl + (sum - 1) / 2 + 1}; for (int i = 0; i < 2; i++) { int pans = first(values[i][vals[i]], l); if (pans <= r) { ans.push_back(pans); return; } } throw 69; } void solve() { cin >> n >> q; string ss; cin >> ss; for (int i = 0; i < n; i++) a[i] = (ss[i] == + ? 1 : -1) * (i % 2 == 0 ? -1 : 1); p[0] = a[0]; for (int i = 1; i < n; i++) p[i] = p[i - 1] + a[i]; for (int i = 0; i < n; i++) values[(a[i] + 1) / 2][p[i]].push_back(i); while (q--) { int l, r; cin >> l >> r; l--, r--; problem(l, r); cout << ans.size() << endl; for (int e : ans) cout << e + 1 << ; cout << endl; ans.clear(); } values[0].clear(); values[1].clear(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; int a[1050], b[1050]; int main() { int n, m, s = 0, d = 0; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a[i]; b[i] = a[i]; } for (int i = 1; i <= n; i++) { sort(a, a + m); for (int j = 0; j < m; j++) { if (a[j] > 0) { s += a[j]; a[j]--; break; } } } for (int i = 1; i <= n; i++) { sort(b, b + m); for (int j = m - 1; j >= 0; j--) { if (b[j] > 0) { d += b[j]; b[j]--; break; } } } cout << d << << s; return 0; }
#include <bits/stdc++.h> using namespace std; long long mx(long long a, long long b) { return a >= b ? a : b; } long long mn(long long a, long long b) { return a <= b ? a : b; } string a[3] = {( twone ), ( two ), ( one )}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t, x; cin >> t; while (t--) { string str, str2; long long ans = 0; vector<long long> v; cin >> str; if (str.size() <= 2) { cout << 0 << n << n ; continue; } for (long long i = 0; i < str.size() - 2; i++) { if (str[i] == t and str[i + 1] == w and str[i + 2] == o and str[i + 3] == n and str[i + 4] == e ) str[i + 2] = l , ans++, v.push_back(i + 3); if (str[i] == t and str[i + 1] == w and str[i + 2] == o ) ans++, str[i + 1] = l , v.push_back(i + 2); if (str[i] == o and str[i + 1] == n and str[i + 2] == e ) ans++, str[i + 1] = l , v.push_back(i + 2); } cout << ans << n ; for (long long i = 0; i < v.size(); i++) cout << v[i] << ; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int max = 0; vector<int> b; int x; for (int i = 0; i < a; i++) { cin >> x; b.push_back(x); } sort(b.begin() + 1, b.end()); int count = 0; if (b[0] > b[b.size() - 1]) { cout << 0 ; } else { while (1) { b[b.size() - 1]--; count++; b[0] = b[0] + 1; sort(b.begin() + 1, b.end()); if (b[0] > b[b.size() - 1]) { cout << count; break; } } } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int vet[1010], dp[2][1010]; const int mod = 998244353; long long soma(long long x, long long y) { x += y; if (x >= mod) x -= mod; return x; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> vet[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[1][j] = dp[0][j]; } if (vet[i] < n and vet[i] > 0) { dp[1][vet[i]] = soma(soma(dp[0][0], 1), dp[1][vet[i]]); } for (int k = 0; k < n; k++) { dp[1][k] = soma(dp[1][k], dp[0][k + 1]); } swap(dp[0], dp[1]); } cout << dp[0][0] << endl; }
#include <bits/stdc++.h> using namespace std; template <class node> struct link_cut_tree { bool connected(node* u, node* v) { return lca(u, v) != NULL; } int depth(node* u) { access(u); return get_sz(u->ch[0]); } node* get_root(node* u) { access(u); while (u->ch[0]) u = u->ch[0], u->push(); return access(u), u; } node* ancestor(node* u, int k) { k = depth(u) - k; for (;; u->push()) { int sz = get_sz(u->ch[0]); if (sz == k) return access(u), u; if (sz < k) k -= sz + 1, u = u->ch[1]; else u = u->ch[0]; } } node* lca(node* u, node* v) { if (u == v) return u; access(u); access(v); if (!u->p) return NULL; u->splay(); return u->p ?: u; } void link(node* u, node* v) { make_root(v); access(u); set_link(v, u, 0); v->update(); } void cut(node* u) { access(u); u->ch[0]->p = NULL; u->ch[0] = NULL; u->update(); } void cut(node* u, node* v) { cut(depth(u) > depth(v) ? u : v); } void make_root(node* u) { access(u); u->rev ^= 1; access(u); } void access(node* u) { for (node *v = u, *pre = NULL; v; v = v->p) { v->splay(); if (pre) v->update_vsub(pre, false); if (v->ch[1]) v->update_vsub(v->ch[1], true); v->ch[1] = pre; v->update(); pre = v; } u->splay(); } node* operator[](int i) { return &data[i]; } int operator[](node* i) { return i - &data[0]; } vector<node> data; link_cut_tree(int n) : data(n) {} }; template <typename pnode> struct splay_tree { pnode ch[2], p; bool rev; int sz; splay_tree() { ch[0] = ch[1] = p = NULL; rev = 0; sz = 1; } friend int get_sz(pnode u) { return u ? u->sz : 0; } virtual void update() { if (ch[0]) ch[0]->push(); if (ch[1]) ch[1]->push(); sz = 1 + get_sz(ch[0]) + get_sz(ch[1]); } virtual void push() { if (rev) { if (ch[0]) ch[0]->rev ^= 1; if (ch[1]) ch[1]->rev ^= 1; swap(ch[0], ch[1]); rev = 0; } } int dir() { if (!p) return -2; if (p->ch[0] == this) return 0; if (p->ch[1] == this) return 1; return -1; } bool is_root() { return dir() < 0; } friend void set_link(pnode u, pnode v, int d) { if (v) v->p = u; if (d >= 0) u->ch[d] = v; } void rotate() { int x = dir(); pnode g = p; set_link(g->p, static_cast<pnode>(this), g->dir()); set_link(g, ch[x ^ 1], x); set_link(static_cast<pnode>(this), g, x ^ 1); g->update(); update(); } void splay() { while (!is_root() && !p->is_root()) { p->p->push(), p->push(), push(); dir() == p->dir() ? p->rotate() : rotate(); rotate(); } if (!is_root()) p->push(), push(), rotate(); push(); } }; struct node; node* root; bool capture; vector<pair<int, int>> vec; struct node : splay_tree<node*> { using splay_tree::ch; int val, lazy; node() : splay_tree() { val = -1e9; lazy = -1; } void update() override { splay_tree::update(); } void update_vsub(node* v, bool add) { if (!add && capture) vec.push_back({this - root, val}); } void push() override { splay_tree::push(); if (lazy != -1) { val = lazy; if (ch[0]) ch[0]->lazy = lazy; if (ch[1]) ch[1]->lazy = lazy; lazy = -1; } } }; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> adj(n); for (int i = 1, u, v, w; i < n; ++i) { cin >> u >> v >> w; --u, --v; adj[u].push_back({v, w}); } vector<long long> dist(n); vec.reserve(n); link_cut_tree<node> lct(n); function<void(int)> dfs_access = [&](int u) { for (auto v : adj[u]) { dist[v.first] = dist[u] + v.second; lct.link(lct[u], lct[v.first]); dfs_access(v.first); } }; dfs_access(0); root = lct[0]; vector<pair<long long, long long>> s; for (int i = 0, d, t; i < m; ++i) { cin >> d >> t; --d; auto u = lct[d], v = u; while (!v->is_root()) v = v->p, v->push(); while (v->ch[1]) v = v->ch[1], v->push(); vec.clear(); capture = true; lct.access(u); capture = false; for (auto x : vec) s.push_back({dist[x.first] + x.second, dist[x.first] + t}); int val = u->val; u->lazy = t; u->push(); u->val = val; lct.access(v); } long long explosion = -1, changes = s.size(); sort(s.begin(), s.end()); priority_queue<long long, vector<long long>, greater<long long>> pq; for (long long t = 1, i = 0; i < s.size() || !pq.empty();) { if (pq.empty() && t <= s[i].first) t = s[i].first + 1; while (i < s.size() && s[i].first < t) pq.push(s[i].second), ++i; if (t > pq.top()) { explosion = t - 1; break; } pq.pop(); ++t; } if (explosion != -1) changes -= count_if(s.begin(), s.end(), [&](pair<long long, long long> x) { return x.second >= explosion; }); cout << explosion << << changes << n ; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O2 ) using namespace std; const int N = 1e5 + 10; const long long mod = 1e9 + 7; const long long mod2 = 998244353; const int inf = 1e9; const int LOG = 22; long long pw(long long a, long long b, long long M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); } struct node { int mx1, mx2, cnt, lz; long long sum; node(int m1 = -inf, int m2 = -inf, int c = 0, int l = inf, long long s = 0) { mx1 = m1, mx2 = m2, cnt = c, lz = l, sum = s; } friend node operator|(node L, node R) { if (L.mx1 < R.mx1) swap(L, R); if (L.mx1 == R.mx1) L.cnt += R.cnt; else L.mx2 = max(L.mx2, R.mx1); L.mx2 = max(L.mx2, R.mx2); L.sum += R.sum; L.lz = inf; return L; } }; struct segment_beats { node seg[N << 2]; void shift(int v, int lz) { seg[v].lz = min(seg[v].lz, lz); seg[v].sum -= 1ll * seg[v].cnt * max(0, seg[v].mx1 - lz); seg[v].mx1 = min(seg[v].mx1, lz); } void second(int v, int tl, int tr) { if (tl == tr) return; shift(v << 1, seg[v].lz); shift(v << 1 | 1, seg[v].lz); } void change(int p, int x, int v = 1, int tl = 1, int tr = N - 1) { second(v, tl, tr); if (p > tr || p < tl) return; if (tl == tr) { seg[v] = node(x, -inf, 1, inf, x); return; } int mid = (tl + tr) >> 1; change(p, x, v << 1, tl, mid), change(p, x, v << 1 | 1, mid + 1, tr); seg[v] = seg[v << 1] | seg[v << 1 | 1]; } void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = N - 1) { second(v, tl, tr); if (l > tr || r < tl || l > r || seg[v].mx1 <= x) return; if (l <= tl && tr <= r && seg[v].mx2 < x) { shift(v, x); return; } int mid = (tl + tr) >> 1; upd(l, r, x, v << 1, tl, mid), upd(l, r, x, v << 1 | 1, mid + 1, tr); seg[v] = seg[v << 1] | seg[v << 1 | 1]; } long long get(int l, int r, int v = 1, int tl = 1, int tr = N - 1) { second(v, tl, tr); if (l > tr || r < tl || l > r) return 0ll; if (l <= tl && tr <= r) { return seg[v].sum; } int mid = (tl + tr) >> 1; return get(l, r, v << 1, tl, mid) + get(l, r, v << 1 | 1, mid + 1, tr); } } seg1, seg2; struct segment { int lz[N << 2]; segment() { for (int i = 0; i < N << 2; i++) lz[i] = inf; } void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = N - 1) { if (l > tr || r < tl || l > r) return; if (l <= tl && tr <= r) { lz[v] = min(lz[v], x); return; } int mid = (tl + tr) >> 1; upd(l, r, x, v << 1, tl, mid), upd(l, r, x, v << 1 | 1, mid + 1, tr); } int ask(int p, int v = 1, int tl = 1, int tr = N - 1) { if (tl == tr) { return lz[v]; } int mid = (tl + tr) >> 1; if (p <= mid) { return min(lz[v], ask(p, v << 1, tl, mid)); } return min(lz[v], ask(p, v << 1 | 1, mid + 1, tr)); } } Seg1, Seg2; set<int> st1, st2; int main() { for (int i = 1; i < N; i++) st1.insert(i), st2.insert(i); int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { int tp, l, r; scanf( %d%d%d , &tp, &l, &r); r--; if (tp == 1) { int x; scanf( %d , &x); if (x < 0) { x = abs(x); seg1.upd(l, r, x); Seg1.upd(l, r, x); while (1) { auto it = st1.lower_bound(l); if (it == st1.end() || *it > r) break; int cu = *it; st1.erase(cu); if (st2.count(cu) == 0) { seg1.change(cu, x); seg2.change(cu, Seg2.ask(cu)); } } } else { seg2.upd(l, r, x); Seg2.upd(l, r, x); while (1) { auto it = st2.lower_bound(l); if (it == st2.end() || *it > r) break; int cu = *it; st2.erase(cu); if (st1.count(cu) == 0) { seg2.change(cu, x); seg1.change(cu, Seg1.ask(cu)); } } } } else { printf( %lld n , seg1.get(l, r) + seg2.get(l, r)); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; string operator*(const string& s, int k) { if (k == 0) return ; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { long long to, cap, rev; Edge(long long _to, long long _cap, long long _rev) { to = _to; cap = _cap; rev = _rev; } }; void add_edge(vector<vector<Edge>>& G, long long from, long long to, long long cap, bool revFlag, long long revCap) { G[from].push_back(Edge(to, cap, (long long)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); } long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, long long f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { long long flow = 0; for (;;) { vector<bool> used(G.size()); for (int(i) = (int)(0); i < (int)(used.size()); i++) used[i] = false; long long f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, vector<long long>& negative) { d.resize(G.size()); negative.resize(G.size()); for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i] = INF; for (int(i) = (int)(0); i < (int)(d.size()); i++) negative[i] = false; d[s] = 0; for (int(k) = (int)(0); k < (int)(G.size() - 1); k++) { for (int(i) = (int)(0); i < (int)(G.size()); i++) { for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } for (int(k) = (int)(0); k < (int)(G.size() - 1); k++) { for (int(i) = (int)(0); i < (int)(G.size()); i++) { for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { d.resize(G.size()); for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i] = INF; d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; q.push(make_pair(0, s)); while (!q.empty()) { pair<long long, long long> a = q.top(); q.pop(); if (d[a.second] < a.first) continue; for (int(i) = (int)(0); i < (int)(G[a.second].size()); i++) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { d.resize(G.size()); for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i].resize(G.size()); for (int(i) = (int)(0); i < (int)(d.size()); i++) { for (int(j) = (int)(0); j < (int)(d[i].size()); j++) { d[i][j] = INF; } } for (int(i) = (int)(0); i < (int)(G.size()); i++) { for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { d[i][G[i][j].to] = G[i][j].cap; } } for (int(i) = (int)(0); i < (int)(G.size()); i++) { for (int(j) = (int)(0); j < (int)(G.size()); j++) { for (int(k) = (int)(0); k < (int)(G.size()); k++) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(vector<vector<Edge>>& graph, vector<int>& order) { int n = graph.size(), k = 0; vector<long long> in(n); for (auto& es : graph) for (auto& e : es) in[e.to]++; priority_queue<long long, vector<long long>, greater<long long>> que; for (int(i) = (int)(0); i < (int)(n); i++) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const vector<vector<Edge>>& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; long long num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } int numSet() { return num; } }; class SumSegTree { private: long long _sum(long long a, long long b, long long k, long long l, long long r) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return dat[k]; else { long long s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); long long s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); return s1 + s2; } } public: long long n, height; vector<long long> dat; SumSegTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, 0); } void add(long long i, long long x) { i += n - 1; dat[i] += x; while (i > 0) { i = (i - 1) / 2; dat[i] += x; } } long long sum(long long a, long long b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: long long _find(long long a, long long b, long long k, long long l, long long r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return dat[k]; else { long long s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); long long s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); return min(s1, s2); } } public: long long n, height; vector<long long> dat; RmqTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, INF); } void update(long long i, long long x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } long long find(long long a, long long b) { return _find(a, b, 0, 0, n); } }; void divisor(long long n, vector<long long>& ret) { for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } vector<long long> lis_fast(const vector<long long>& a) { const long long n = a.size(); vector<long long> A(n, INT_MAX); vector<long long> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } long long m = *max_element(id.begin(), id.end()); vector<long long> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string& str, vector<int>& z, long long s) { const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string& str, vector<int>& z) { const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; RollingHash(const string& S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; long long mod_pow(long long x, long long n, long long mod) { long long res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long mod_inv(long long x, long long mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: vector<long long> fact; vector<long long> inv; long long mod; long long mod_inv(long long x) { long long n = mod - 2LL; long long res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long nCr(long long n, long long r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } long long nPr(long long n, long long r) { return (fact[n] * inv[n - r]) % mod; } long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } Combination(long long n, long long _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; for (int(i) = (int)(0); i < (int)(n); i++) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); for (int(i) = (int)(0); i < (int)(n + 1); i++) { inv[i] = mod_inv(fact[i]); } } }; long long gcd(long long m, long long n) { if (n == 0) return m; return gcd(n, m % n); } long long lcm(long long m, long long n) { return m / gcd(m, n) * n; } vector<vector<long long>> mIdentity(long long n) { vector<vector<long long>> A(n, vector<long long>(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } vector<vector<long long>> mMul(const vector<vector<long long>>& A, const vector<vector<long long>>& B) { vector<vector<long long>> C(A.size(), vector<long long>(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % 1000000007) * (B[k][j] % 1000000007)) %= 1000000007; return C; } vector<vector<long long>> mPow(const vector<vector<long long>>& A, long long e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>>& s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; long long _garner(vector<long long>& xs, vector<long long>& mods) { int M = xs.size(); vector<long long> coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { long long mod_i = mods[i]; long long v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { long long mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T>& a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long>& a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); const long long g = primitive_root; long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; long long h = mod_pow(g, tmp, mod); if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow(h, n / m2, mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long>& input) { _ntt(input, 1); } void intt(vector<long long>& input) { _ntt(input, -1); const long long n_inv = mod_pow(input.size(), mod - 2, mod); for (auto& x : input) x = (x * n_inv) % mod; } vector<long long> convolution(const vector<long long>& a, const vector<long long>& b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long>& a, vector<long long>& b, long long mod = 1224736769LL) { for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; long long maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, k; cin >> n >> k; long long mi = (n + 1) * n / 2; long long ma = mi * 2 - (n / 2 + 1) * (n / 2); if (n % 2 == 1) ma -= n / 2 + 1; if (k < mi) { cout << -1 << endl; return 0; } cout << min(k, ma) << endl; for (int(i) = (int)(0); i < (int)(n); i++) cout << i + 1 << ; cout << endl; if (k >= ma) { for (int(i) = (int)(0); i < (int)(n); i++) cout << n - i << ; cout << endl; return 0; } long long rest = k - mi; vector<long long> ans(n); for (int(i) = (int)(0); i < (int)(n); i++) ans[i] = i + 1; long long now = 0; for (int i = n - 1; i >= 0; i--) { if (rest >= i - now) { swap(ans[i], ans[now]); rest -= i - now; } else { swap(ans[i], ans[i - rest]); break; } now++; } for (int(i) = (int)(0); i < (int)(n); i++) cout << ans[i] << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int vis[100005], mark[100005]; vector<int> g[100005]; int prob = 0; void dfs(int node, int k) { mark[node] = k; vis[node] = 1; for (auto i : g[node]) { if (!vis[i]) { dfs(i, 1 - k); } else { if (mark[i] == k) { prob = 1; } } } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf( %d , &u); scanf( %d , &v); g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i, 0); } if (prob) { cout << -1 ; return 0; } int k = 0; for (int i = 1; i <= n; i++) { if (mark[i] == 1) k++; } cout << k << n ; for (int i = 1; i <= n; i++) { if (mark[i] == 1) cout << i << ; } cout << n ; k = 0; for (int i = 1; i <= n; i++) { if (mark[i] == 0) k++; } cout << k << n ; for (int i = 1; i <= n; i++) { if (mark[i] == 0) cout << i << ; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, pd[4][10100], x, y, gox[4] = {1, 0, -1, 0}, goy[4] = {0, 1, 0, -1}, w[16], dp[10100][8], mo = 1000000007; char getin() { char ch = getchar(); while (ch != . && ch != X && ch != O ) ch = getchar(); return ch; } int check(int k1, int k2, int k3) { for (int i = 0; i < 3; i++) { if (pd[i + 1][k1]) { if (k2 & (1 << i)) return 0; } if (pd[i + 1][k1 - 1]) { if (k3 & (1 << i)) return 0; else k3 += (1 << i); } } int a[3] = {0}; for (int i = 0; i < 3; i++) if ((k3 & (1 << i)) == 0) a[i]--; for (int i = 0; i < 3; i++) if (k2 & (1 << i)) a[i]++; for (int i = 0; i < 3; i++) if (a[i] < 0) return 0; for (int i = 0; i < 2; i++) if (a[i] && a[i + 1] == 0) return 0; else if (a[i]) a[i + 1] = 0; if (a[2]) return 0; return 1; } int solve(int k) { memset(dp, 0x00, sizeof dp); dp[0][7] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j < 8; j++) for (int k = 0; k < 8; k++) if (check(i, j, k)) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mo; int k1 = 0; for (int i = 0; i < 3; i++) if (pd[i + 1][n]) k1 += (1 << i); return dp[n][7 ^ k1]; } int dfs(int k1, int k2) { if (k1 == 4) return w[k2] * solve(k2); int flag = 0; for (int i = 1; i <= 2; i++) { int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; if (k3 <= 0 || k3 > 3 || k4 <= 0 || k4 > n || pd[k3][k4]) { flag = 1; break; } } int ans = 0; if (flag == 0) { for (int i = 1; i <= 2; i++) { int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; pd[k3][k4] = 1; } ans += dfs(k1 + 1, k2 + (1 << k1)); for (int i = 1; i <= 2; i++) { int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; pd[k3][k4] = 0; } } ans = (ans + dfs(k1 + 1, k2)) % mo; return ans; } int main() { scanf( %d , &n); for (int i = 1; i <= 3; i++) for (int j = 1; j <= n; j++) { char ch = getin(); if (ch == X ) pd[i][j] = 1; else if (ch == O ) { pd[i][j] = 2; x = i; y = j; } } for (int i = 1; i < 16; i++) { w[i] = 1; for (int j = (i - 1) & i; j; j = (j - 1) & i) w[i] = w[i] - w[j]; } cout << (dfs(0, 0) + mo) % mo << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0 ? -x : x; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } set<vector<int> > s; vector<pair<char, pair<int, int> > > ans; bool rec(vector<int>& a) { if (a.size() == 1) { return a[0] == 24; } if (s.find(a) != s.end()) { return false; } s.insert(a); for (size_t i = 0; i < a.size(); ++i) { for (size_t j = i + 1; j < a.size(); ++j) { vector<int> b = a; int x = a[i], y = a[j]; b.erase(b.begin() + j); b.erase(b.begin() + i); b.push_back(x + y); ans.push_back(make_pair( + , make_pair(x, y))); if (rec(b)) { return true; } b.pop_back(); ans.pop_back(); b.push_back(y - x); ans.push_back(make_pair( - , make_pair(y, x))); if (rec(b)) { return true; } b.pop_back(); ans.pop_back(); b.push_back(x - y); ans.push_back(make_pair( - , make_pair(x, y))); if (rec(b)) { return true; } b.pop_back(); ans.pop_back(); b.push_back(x * y); ans.push_back(make_pair( * , make_pair(x, y))); if (rec(b)) { return true; } b.pop_back(); ans.pop_back(); } } return false; } int main(int, char**) { ios_base::sync_with_stdio(false); int n; cin >> n; if (n < 4) { cout << NO << endl; return 0; } cout << YES << endl; vector<int> a; for (int i = 1; i <= n % 2 + 4; ++i) { a.push_back(i); } rec(a); for (int i = n % 2 + 4; i < n; i += 2) { ans.push_back(make_pair( - , make_pair(i + 2, i + 1))); ans.push_back(make_pair( * , make_pair(24, 1))); } for (size_t i = 0; i < ans.size(); ++i) { int x = ans[i].second.first; int y = ans[i].second.second; int r = -1; switch (ans[i].first) { case - : r = x - y; break; case + : r = x + y; break; case * : r = x * y; break; } cout << x << << ans[i].first << << y << = << r << endl; } fprintf(stderr, Time execute: %.3lf sec n , clock() / (double)CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> using namespace std; void fast(); int main() { fast(); int k, d; cin >> k >> d; string ans = ; if ((d > (pow(10, k) - 1)) || (d == 0 && k != 1)) { cout << No solution ; return 0; } for (int i = 0; i < k; i++) { if (d > 9) { ans += 9 ; d -= 9; } else { stringstream t; t << d; ans += t.str(); d = 0; } } cout << ans; return 0; } void fast() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); }
#include <bits/stdc++.h> using namespace std; const int INF = 1029384756; const double PI = acos(-1); const double EPS = 0.0000000304; long long A, B, C; int main() { scanf( %I64d%I64d%I64d , &A, &B, &C); if ((A % 2) == (B % 2) && (B % 2) == (C % 2)) { printf( %I64d , A + B + C - max(A, max(B, C)) - min(A, min(B, C))); } else if (A % 2 == B % 2) { printf( %I64d , max(A, B)); } else if (A % 2 == C % 2) { printf( %I64d , max(A, C)); } else { printf( %I64d , max(B, C)); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long int> v; int main() { long long int n, m, a, b, sum = 0, i, j, k; cin >> n >> k; long long int s[n], x[n]; for (i = 0; i < n; i++) { cin >> a >> b; sum += min(a, b); v.push_back(min(2 * a, b) - min(a, b)); } sort(v.begin(), v.end()); for (i = 0; i < n && i < k; i++) sum += v[n - i - 1]; cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << p.first << , << p.second; } template <typename T> struct point { T x, y; point(T xx = 0, T yy = 0) : x(xx), y(yy) {} T len2() const { return x * x + y * y; } double len() const { return sqrt(double(len2())); } bool operator==(const point<T>& p) const { return x == p.x && y == p.y; } }; template <typename T> point<T> operator+(const point<T>& a, const point<T>& b) { return point<T>(a.x + b.x, a.y + b.y); } template <typename T> point<T> operator-(const point<T>& a, const point<T>& b) { return point<T>(a.x - b.x, a.y - b.y); } template <typename T> T scal(const point<T>& a, const point<T>& b) { return a.x * b.x + a.y * b.y; } template <typename T> T vect(const point<T>& a, const point<T>& b) { return a.x * b.y - a.y * b.x; } template <typename T> ostream& operator<<(ostream& os, const point<T>& p) { return os << ( << p.x << , << p.y << ) ; } const int maxn = 100100; pair<int, pair<int, int> > a[maxn], b[maxn]; int below[maxn], direct[maxn], belowsecond[maxn]; long long d[maxn][2]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { scanf( %d%d , &a[i].second.first, &a[i].first); a[i].second.second = i; } int m; cin >> m; for (int i = 0; i < m; i++) { scanf( %d%d , &b[i].second.first, &b[i].first); b[i].second.second = i; } sort(a, a + n); sort(b, b + m); memset(direct, -1, sizeof(direct)); memset(below, -1, sizeof(below)); memset(belowsecond, -1, sizeof(belowsecond)); for (int i = 0; i < n; i++) { pair<int, pair<int, int> >* d1 = lower_bound(b, b + m, pair<int, pair<int, int> >( a[i].first, pair<int, int>(a[i].second.first, 0))); pair<int, pair<int, int> >* d2 = lower_bound(b, b + m, pair<int, pair<int, int> >( a[i].first - 1, pair<int, int>(a[i].second.first, 0))); if (d1 != b + m && d1->first == a[i].first && d1->second.first >= a[i].second.first) { direct[i] = d1->second.second; } if (d2 != b + m && d2->first + 1 == a[i].first && d2->second.first >= a[i].second.first) { below[i] = d2->second.second; d2++; if (d2 != b + m && d2->first + 1 == a[i].first && d2->second.first >= a[i].second.first) { belowsecond[i] = d2->second.second; } } } long long mx = 0; for (int i = 0; i < n; i++) { if (direct[i] >= 0) { long long q = max(d[i][0], d[i][1]); d[i + 1][1] = max(d[i + 1][1], a[i].second.first + q); } if (below[i] >= 0) { d[i + 1][0] = max(d[i + 1][0], d[i][0] + a[i].second.first); if (i > 0 && direct[i - 1] != below[i]) { d[i + 1][0] = max(d[i + 1][0], d[i][1] + a[i].second.first); } if (i > 0 && direct[i - 1] == below[i] && belowsecond[i] >= 0) { d[i + 1][0] = max(d[i + 1][0], d[i][1] + a[i].second.first); } } d[i + 1][0] = max(d[i + 1][0], max(d[i][0], d[i][1])); mx = max(mx, max(d[i + 1][0], d[i + 1][1])); } cout << mx << endl; int cur = n; int t = 0; if (d[cur][t] != mx) t++; assert(d[cur][t] == mx); vector<pair<int, int> > res; set<int> guyused; for (; cur > 0; cur--) { if (res.size()) { guyused.insert(res.back().second); } if (d[cur - 1][0] == d[cur][t]) { t = 0; continue; } if (d[cur - 1][1] == d[cur][t]) { t = 1; continue; } int p = a[cur - 1].second.first; if (!guyused.count(direct[cur - 1])) { if (p + d[cur - 1][1] == mx && direct[cur - 1] >= 0) { res.push_back( pair<int, int>(a[cur - 1].second.second, direct[cur - 1])); mx -= p; t = 1; continue; } if (p + d[cur - 1][0] == mx && direct[cur - 1] >= 0) { res.push_back( pair<int, int>(a[cur - 1].second.second, direct[cur - 1])); mx -= p; t = 0; continue; } } assert(below[cur - 1] >= 0); if (!guyused.count(below[cur - 1])) { if (d[cur - 1][0] + p == mx) { res.push_back(pair<int, int>(a[cur - 1].second.second, below[cur - 1])); mx -= p; t = 0; continue; } if (cur - 2 < 0 || (direct[cur - 2] != below[cur - 1] && d[cur - 1][1] + p == mx)) { res.push_back(pair<int, int>(a[cur - 1].second.second, below[cur - 1])); mx -= p; t = 1; continue; } } assert(!guyused.count(belowsecond[cur - 1])); if (cur - 2 >= 0 && direct[cur - 2] == below[cur - 1] && belowsecond[cur - 1] >= 0 && d[cur - 1][1] + p == mx) { res.push_back( pair<int, int>(a[cur - 1].second.second, belowsecond[cur - 1])); mx -= p; t = 1; continue; } assert(0); } printf( %d n , res.size()); for (int i = 0; i < res.size(); i++) { printf( %d %d n , res[i].second + 1, res[i].first + 1); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == - ) neg = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - 0 , c = getchar(); return x * neg; } inline int qpow(int x, int e, int _MOD) { int ans = 1; while (e) { if (e & 1) ans = ans * x % _MOD; x = x * x % _MOD; e >>= 1; } return ans; } int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0; vector<int> g[200005], cyc[200005]; int k = 0; inline void dfs(int x) { while (!g[x].empty()) { int y = g[x].back(); g[x].pop_back(); dfs(a[y]); cyc[k].push_back(y); } } signed main() { for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i]; sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) if (c[i] != c[i - 1]) d[++num] = c[i]; for (int i = 1; i <= n; i++) a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { cnt++; g[b[i]].push_back(i); } } if (!cnt) return puts( 0 ), 0; if (cnt > s) return puts( -1 ), 0; for (int i = 1; i <= num; i++) { if (!g[i].empty()) { k++; dfs(i); } } for (int i = 1; i <= k; i++) reverse(cyc[i].begin(), cyc[i].end()); if (k == 1) { cout << 1 << endl << cyc[1].size() << endl; for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++) cout << *it << ; return 0; } if (cnt <= s - k) { cout << 2 << endl; cout << cnt << endl; for (int i = 1; i <= k; i++) { for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << ; } puts( ); cout << k << endl; for (int i = k; i >= 1; i--) cout << cyc[i][0] << ; puts( ); return 0; } else { int t = cnt - (s - k); if (t == k) cout << t << endl; else if (t == k - 1) cout << t + 1 << endl; else cout << t + 2 << endl; for (int i = 1; i <= t; i++) { cout << cyc[i].size() << endl; for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << ; puts( ); } int sum = 0; if (t != k) { for (int i = t + 1; i <= k; i++) sum += cyc[i].size(); cout << sum << endl; for (int i = t + 1; i <= k; i++) for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << ; if (t != k - 1) { puts( ); cout << k - t << endl; for (int i = k; i >= t + 1; i--) cout << cyc[i][0] << ; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int t, n, son[maxn], root, p[maxn], rt; vector<int> g[maxn]; void dfs(int u, int fa) { son[u] = 1; p[u] = fa; for (auto it : g[u]) { if (it == fa) continue; dfs(it, u); son[u] += son[it]; } } void getroot(int u, int fa) { root = u; for (auto it : g[u]) { if (it == fa) continue; if (son[it] > n - son[it]) getroot(it, u); } } int getleaf(int u, int fa) { if (g[u].size() == 1) return u; for (auto it : g[u]) { if (it == fa) continue; return getleaf(it, u); } } int main() { scanf( %d , &t); while (t--) { root = rt = 0; scanf( %d , &n); for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); getroot(1, 0); for (auto it : g[root]) { if (it != p[root] && son[it] == n - son[it]) rt = it; } if (!rt) { printf( %d %d n%d %d n , root, g[root][0], root, g[root][0]); } else { int l = getleaf(rt, p[rt]); printf( %d %d n%d %d n , p[l], l, root, l); } for (int i = 1; i <= n; i++) g[i].clear(), p[i] = 0; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool cmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int N = 444444; int fa[N], ch[N][26], l[N], f[N], a[N], c[N], pos[N], top[N]; char s[N]; int last = 1, cnt = 1, n, ans = 1; void ins(int c, int k) { int p = last, np = ++cnt; last = np, l[np] = l[p] + 1, pos[np] = k; for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np; if (!p) fa[np] = 1; else { int q = ch[p][c]; if (l[q] == l[p] + 1) fa[np] = q; else { int nq = ++cnt; l[nq] = l[p] + 1, pos[nq] = pos[q]; memcpy(ch[nq], ch[q], sizeof(ch[q])); fa[nq] = fa[q], fa[np] = fa[q] = nq; for (; ch[p][c] == q; p = fa[p]) ch[p][c] = nq; } } } void calc() { for (int i = 1; i <= cnt; ++i) ++c[l[i]]; for (int i = 1; i <= cnt; ++i) c[i] += c[i - 1]; for (int i = 1; i <= cnt; ++i) a[c[l[i]]--] = i; } int L[N * 25], R[N * 25], rt[N], tot; void modify(int &now, int l, int r, int x) { now = ++tot; if (l == r) return; int mid = l + r >> 1; if (x <= mid) modify(L[now], l, mid, x); else modify(R[now], mid + 1, r, x); } int merge(int x, int y) { if (!x || !y) return x | y; int z = ++tot; L[z] = merge(L[x], L[y]); R[z] = merge(R[x], R[y]); return z; } int query(int x, int l, int r, int ql, int qr) { if (!x) return 0; if (ql <= l && qr >= r) return 1; int mid = l + r >> 1; if (ql <= mid && query(L[x], l, mid, ql, qr)) return 1; if (qr > mid && query(R[x], mid + 1, r, ql, qr)) return 1; return 0; } int main() { scanf( %d , &n); scanf( %s , s + 1); for (int i = 1; i <= n; ++i) ins(s[i] - a , i), modify(rt[last], 1, n, i); calc(); for (int i = cnt; i > 1; --i) rt[fa[a[i]]] = merge(rt[fa[a[i]]], rt[a[i]]); for (int i = 2; i <= cnt; ++i) { int u = a[i], ff = fa[u]; if (ff == 1) { f[u] = 1, top[u] = u; continue; } int x = query(rt[top[ff]], 1, n, pos[u] - l[u] + l[top[ff]], pos[u] - 1); if (x) f[u] = f[ff] + 1, top[u] = u; else f[u] = f[ff], top[u] = top[ff]; cmax(ans, f[u]); } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const long long MOD = 998244353; const long long N = (long long)1e4 + 9; const long long inf = 1 << 30; template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <typename T> T exp(T b, T p) { T x = 1; while (p) { if (p & 1) x = (x * b); b = (b * b); p = p >> 1; } return x; } inline long long ones(long long n) { long long res = 0; while (n && ++res) n -= n & (-n); return res; } inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } long long mem[109][3]; long long n, arr[109]; long long go(long long idx, long long last) { if (idx == n) return 0; long long &ret = mem[idx][last]; if (ret != -1) return ret; ret = -inf; if (arr[idx] == 0) ret = go(idx + 1, 0); if (arr[idx] == 3) { if (last != 1) ret = max(ret, 1 + go(idx + 1, 1)); if (last != 2) ret = max(ret, 1 + go(idx + 1, 2)); } if (arr[idx] == 1) { if (last != 1) ret = 1 + go(idx + 1, 1); else ret = go(idx + 1, 0); } if (arr[idx] == 2) { if (last != 2) ret = 1 + go(idx + 1, 2); else ret = go(idx + 1, 0); } return ret; } int32_t main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(0); cin >> n; for (long long i = 0; i < n; ++i) { cin >> arr[i]; } memset(mem, -1, sizeof(mem)); long long ans = go(0, 0); cout << n - ans; }
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; int a[100010], d[100010]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf( %d , a + i); for (int i = 1; i <= n + 1; i++) d[i] = a[i] - a[i - 1]; stack<int> st; int r = 0; for (int i = 1; i <= n; i++) { if (d[i] > 0) r += d[i]; } cout << r << endl; for (int i = 1; i <= n + 1; i++) { if (d[i] > 0) { for (int j = 0; j < d[i]; j++) { st.push(i); } } if (d[i] < 0) { for (int j = 0; j > d[i]; j--) { printf( %d %d n , st.top(), i - 1); st.pop(); } } } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k, q, a, b, ans, t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> q; while (q--) { cin >> k >> n >> a >> b; ans = a * n; if (b * n >= k) cout << -1 << n ; else { if (ans < k) cout << n << n ; else { t = a - b; ans = (ans - k) / t + 1; cout << n - ans << n ; } } } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, k, x, y, z, a[5005], b[50005], ans, val; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; cin >> m; for (long long i = 0; i < m; i++) cin >> b[i]; sort(a, a + n); sort(b, b + m); cout << a[n - 1] << << b[m - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long INF = ~0ULL >> 1; template <class T> inline void read(T &n) { char c; int flag = 1; for (c = getchar(); !(c >= 0 && c <= 9 || c == - ); c = getchar()) ; if (c == - ) flag = -1, n = 0; else n = c - 0 ; for (c = getchar(); c >= 0 && c <= 9 ; c = getchar()) n = n * 10 + c - 0 ; n *= flag; } const int maxn = 2100000; const int mo = 1000000007; int N, K, M, ans, vis[1000]; char s[maxn]; int nxt[maxn][26]; int dp[maxn]; void add(int &x, int y) { x += y; if (x >= mo) x -= mo; } int main(int argc, char *argv[]) { scanf( %d%d , &N, &K); scanf( %s , s + 1); M = strlen(s + 1); string temp = ; for (int i = (M); i >= (1); --i) if (!vis[s[i]]) vis[s[i]] = true, temp = s[i] + temp; for (int i = ( a ); i <= ( a + K - 1); ++i) if (!vis[i]) temp = (char)i + temp; for (int i = (0); i < (N); ++i) s[++M] = temp[i % temp.size()]; for (int j = (0); j < (K); ++j) nxt[M + 1][j] = M + 1; for (int i = (M); i >= (1); --i) { memcpy(nxt[i], nxt[i + 1], sizeof(nxt[i])); nxt[i][s[i] - a ] = i; } int cur = 0; dp[0] = 1; for (int i = (0); i < (M); ++i) { int flag = false; for (int j = (0); j < (K); ++j) if (nxt[i + 1][j] <= M) add(dp[nxt[i + 1][j]], dp[i]); } for (int i = (0); i <= (M); ++i) add(ans, dp[i]); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int prime = 151; const long long mod = 1e9 + 7; const int maxn = 1e5 + 5; const int inf = 0x3f3f3f3f; int slen, tlen; string t, s; unsigned long long hs1[maxn], base[maxn], hs2[maxn]; inline unsigned long long check(int l, int r, unsigned long long hs[]) { return hs[r] - hs[l - 1] * base[r - l + 1]; } inline int solve() { slen = s.length(), tlen = t.length(); base[0] = 1; for (int i = 1; i <= max(slen, tlen); ++i) base[i] = base[i - 1] * prime; for (int i = 1; i <= tlen; ++i) hs2[i] = hs2[i - 1] * prime + t[i - 1]; for (int i = 1; i <= slen; ++i) hs1[i] = hs1[i - 1] * prime + s[i - 1]; int ans = 0; if (s.find(t) == string::npos && t.find(s) == string::npos) return 0; if (tlen < slen) { for (int i = 1; i <= tlen; ++i) { if (check(1 + i, tlen, hs2) == check(1, tlen - i, hs2) && tlen % i == 0 && check(1 + i, slen, hs1) == check(1, slen - i, hs1) && slen % i == 0) ++ans; } } else { for (int i = 1; i <= slen; ++i) { if (check(1 + i, slen, hs1) == check(1, slen - i, hs1) && slen % i == 0 && check(1 + i, tlen, hs2) == check(1, tlen - i, hs2) && tlen % i == 0) ++ans; } } return ans; } int main() { { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } cin >> t >> s; cout << solve(); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& x) { bool fu = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == - ) fu = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - 0 ; if (fu) x = -x; }; template <class T> inline void read(T& x, T& y) { read(x); read(y); } template <class T> inline void read(T& x, T& y, T& z) { read(x); read(y); read(z); } inline char getc() { char c; for (c = getchar(); c <= 32; c = getchar()) ; return c; } int n, m, st, i, j, k, l, K, P, x, y; int D0[610], D1[610], n0[610], n1[610]; int e[610][610]; long long a[610][610]; bool vis[610]; long long f[610]; long long dfs(int i) { if (i == st) return 1; if (vis[i]) return f[i]; vis[i] = 1; long long ans = 0; for (int j = 1; j <= n; j++) if (e[j][i]) ans = (ans + dfs(j) * e[j][i]) % P; return f[i] = ans; } int main() { read(n, m, P); for (i = 1; i <= m; i++) read(x, y), e[x][y]++, D0[x]++, D1[y]++; for (i = 1; i <= n; i++) if (D0[i] == 0) n0[i] = ++n0[0]; for (i = 1; i <= n; i++) if (D1[i] == 0) n1[i] = ++n1[0]; K = n0[0]; for (i = 1; i <= n; i++) if (n1[i]) { memset(vis, 0, sizeof(vis)); memset(f, 0, sizeof(f)); st = i; for (j = 1; j <= n; j++) if (n0[j]) a[n1[i]][n0[j]] = dfs(j); } long long ans = 1; for (i = 1; i <= K; i++) { for (j = i; j <= K; j++) if (a[j][i]) { if (j != i) ans = -ans; for (k = i; k <= K; k++) swap(a[j][k], a[i][k]); break; } if (a[i][i] == 0) continue; for (j = i + 1; j <= K; j++) while (a[j][i]) { long long res = a[j][i] / a[i][i]; for (k = i; k <= K; k++) a[j][k] = (a[j][k] - a[i][k] * res) % P; if (a[j][i]) { ans = -ans; for (k = i; k <= K; k++) swap(a[j][k], a[i][k]); } } } for (i = 1; i <= K; i++) ans = ans * a[i][i] % P; if (ans < 0) ans += P; cout << ans << endl; return 0; }
#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 lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } template <class T> inline T gethash(T h[], T power[], int l, int r) { if (r < l) return (T)0; return h[l] - h[r + 1] * power[r - l + 1]; } struct data { data() {} bool friend operator<(data a, data b) { return 0; } }; int n, m, k; long long N, M, K; int a[200005]; long long ans[200005]; vector<int> adj[200005]; int subtree[200005]; bool bad[200005]; int tot = 0; int cnt[200005]; unordered_set<int> st; unordered_set<int>::iterator it; unordered_map<int, int> mm[200005]; int _gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; int p = _gcd(b, a % b); return p; } void dfs(int u, int p) { subtree[u] = 1; tot++; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p || bad[v]) continue; dfs(v, u); subtree[u] += subtree[v]; } } int getcenter(int u, int p) { for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p || bad[v]) continue; if (subtree[v] > tot / 2) return getcenter(v, u); } return u; } void query(int u, int p, int g) { g = _gcd(g, a[u]); for (it = st.begin(); it != st.end(); it++) { int x = *it; int gc = _gcd(g, x); ans[gc] += cnt[x]; } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p || bad[v]) continue; query(v, u, g); } } void update(int u, int p, int g) { g = _gcd(a[u], g); st.insert(g); cnt[g]++; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p || bad[v]) continue; update(v, u, g); } } void decompose(int u, int p) { tot = 0; dfs(u, p); int c = getcenter(u, p); ans[a[c]]++; cnt[a[c]] = 1; st.insert(a[c]); bad[c] = 1; for (int i = 0; i < adj[c].size(); i++) { int v = adj[c][i]; if (v == p || bad[v]) continue; query(v, c, 0); update(v, c, a[c]); } for (it = st.begin(); it != st.end(); it++) { int x = *it; cnt[x] = 0; } st.clear(); for (int i = 0; i < adj[c].size(); i++) { int v = adj[c][i]; if (v == p || bad[v]) continue; decompose(v, c); } } int main() { int t, tc = 1; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } for (int i = 1; i < n; i++) { int u, v; scanf( %d %d , &u, &v); adj[u].push_back(v); adj[v].push_back(u); } decompose(1, -1); for (int i = 1; i <= 200000; i++) { if (ans[i]) { printf( %d %lld n , i, ans[i]); } } return 0; }