func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int maxn = 3e3 + 7; const int mod = 998244353; int col[maxn]; namespace DSU { int far[maxn]; void init() { for (int i = 0; i < maxn; i++) far[i] = i; } int Find(int x) { return x == far[x] ? x : far[x] = Find(far[x]); } void Union(int x, int y) { x = Find(x), y = Find(y); if (x < y) swap(x, y); far[y] = x; } }; // namespace DSU string s; int var, w, b; bool isp[maxn]; vector<int> G[maxn]; bool dfs(int u) { for (auto &v : G[u]) { if (col[v] == -1) { col[v] = col[u] ^ 1; if (!dfs(v)) return false; } else { if (col[v] == col[u]) return false; } } return true; } long long solve(int len) { for (int i = 0; i <= b; i++) G[i].clear(); memset(isp, 0, sizeof(isp)); DSU::init(); for (int i = 0; i < s.length() / 2; i++) DSU::Union(i, s.length() - i - 1); for (int i = 0; i < len / 2; i++) DSU::Union(var + i, var + len - 1 - i); isp[w] = isp[b] = 1; DSU::Union(var + len - 1, b); for (int i = 0; i < s.length(); i++) isp[i] = 1; for (int i = 0; i < len; i++) isp[i + var] = 1; for (int i = 0; i < len; i++) if (s[i] == 0 ) DSU::Union(i, var + i); for (int i = len; i < s.length(); i++) { if (s[i] == 1 ) DSU::Union(i, b); else if (s[i] == 0 ) DSU::Union(i, w); } DSU::Union(s.length() - 1, b); if (DSU::Find(b) == DSU::Find(w)) return 0; for (int i = 0; i < len; i++) { if (s[i] == 1 ) { G[DSU::Find(i)].push_back(DSU::Find(var + i)); G[DSU::Find(var + i)].push_back(DSU::Find(i)); } } G[DSU::Find(w)].push_back(DSU::Find(b)); G[DSU::Find(b)].push_back(DSU::Find(w)); long long ret = 1; memset(col, -1, sizeof(col)); for (int i = b; i >= 0; i--) { if (isp[i]) { int cur = DSU::Find(i); if (col[cur] == -1) { if (cur == b) col[cur] = 1; else col[cur] = 0; if (!dfs(cur)) return 0; else { if (cur != b && cur != w) ret = ret * 2 % mod; } } } } return ret; } int main() { cin >> s; var = s.length(); reverse(s.begin(), s.end()); w = var * 2 + 1, b = var * 2 + 2; long long ans = 0; for (int i = 1; i < s.length(); i++) ans = (ans + solve(i)) % mod; cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string a; string b; cin >> a >> b; int order = 0; while (a[order] == 0 ) { order++; } a = a.substr(order); order = 0; while (b[order] == 0 ) { order++; } b = b.substr(order); if (a.size() > b.size()) { cout << > ; } else if (a.size() < b.size()) { cout << < ; } else { for (int i = 0; i < a.size(); i++) { if (a[i] > b[i]) { cout << > ; return 0; } else if (a[i] < b[i]) { cout << < ; return 0; } } cout << = ; } } |
#include <bits/stdc++.h> using namespace std; using SI = set<int>; using ULL = unsigned long long; using LL = long long; using VI = vector<int>; using LD = long double; using PULL = pair<ULL, ULL>; using UI = unsigned int; using PLL = pair<LL, LL>; using PDD = pair<LD, LD>; using MII = map<int, int>; using TDD = pair<PDD, LD>; using TLL = pair<PLL, LL>; using PII = pair<int, int>; using TII = pair<PII, int>; using QDD = pair<PDD, PDD>; using QII = pair<PII, PII>; using QLL = pair<PLL, PLL>; using STII = set<TII>; using VTII = vector<TII>; using MTII = map<TII, TII>; using MITII = map<int, TII>; using UMITII = unordered_map<int, TII>; using SQII = set<QII>; using VQII = vector<QII>; using MQII = map<QII, QII>; using MIQII = map<int, QII>; using UMIQII = unordered_map<int, QII>; using SLL = set<LL>; using VLL = vector<LL>; using MLL = map<LL, LL>; using MILL = map<int, LL>; using UMILL = unordered_map<int, LL>; using SULL = set<ULL>; using VULL = vector<ULL>; using MULL = map<ULL, ULL>; using MIULL = map<int, ULL>; using UMIULL = unordered_map<int, ULL>; using SPULL = set<PULL>; using VPULL = vector<PULL>; using MPULL = map<PULL, PULL>; using MIPULL = map<int, PULL>; using UMIPULL = unordered_map<int, PULL>; using SQLL = set<QLL>; using VQLL = vector<QLL>; using MQLL = map<QLL, QLL>; using MIQLL = map<int, QLL>; using UMIQLL = unordered_map<int, QLL>; using SLD = set<LD>; using VLD = vector<LD>; using MLD = map<LD, LD>; using MILD = map<int, LD>; using UMILD = unordered_map<int, LD>; using SPDD = set<PDD>; using VPDD = vector<PDD>; using MPDD = map<PDD, PDD>; using MIPDD = map<int, PDD>; using UMIPDD = unordered_map<int, PDD>; using SVI = set<VI>; using VVI = vector<VI>; using MVI = map<VI, VI>; using MIVI = map<int, VI>; using UMIVI = unordered_map<int, VI>; using STLL = set<TLL>; using VTLL = vector<TLL>; using MTLL = map<TLL, TLL>; using MITLL = map<int, TLL>; using UMITLL = unordered_map<int, TLL>; using SUI = set<UI>; using VUI = vector<UI>; using MUI = map<UI, UI>; using MIUI = map<int, UI>; using UMIUI = unordered_map<int, UI>; using SPII = set<PII>; using VPII = vector<PII>; using MPII = map<PII, PII>; using MIPII = map<int, PII>; using UMIPII = unordered_map<int, PII>; using SPLL = set<PLL>; using VPLL = vector<PLL>; using MPLL = map<PLL, PLL>; using MIPLL = map<int, PLL>; using UMIPLL = unordered_map<int, PLL>; using SSI = set<SI>; using VSI = vector<SI>; using MSI = map<SI, SI>; using MISI = map<int, SI>; using UMISI = unordered_map<int, SI>; using SVVI = set<VVI>; using VVVI = vector<VVI>; using MVVI = map<VVI, VVI>; using MIVVI = map<int, VVI>; using UMIVVI = unordered_map<int, VVI>; using UMII = unordered_map<int, int>; ULL randULL() { return ((ULL)rand()) ^ ((ULL)rand() << 15) ^ ((ULL)rand() << 30) ^ ((ULL)rand() << 45) ^ ((ULL)rand() << 60); } using STDD = set<TDD>; using VTDD = vector<TDD>; using MTDD = map<TDD, TDD>; using MITDD = map<int, TDD>; using UMITDD = unordered_map<int, TDD>; using SQDD = set<QDD>; using VQDD = vector<QDD>; using MQDD = map<QDD, QDD>; using MIQDD = map<int, QDD>; using UMIQDD = unordered_map<int, QDD>; int popc(int x) { return __builtin_popcount(x); } int popc(UI x) { return __builtin_popcount(x); } int popc(LL x) { return __builtin_popcountll(x); } int popc(ULL x) { return __builtin_popcountll(x); } int ctz(int x) { return __builtin_ctz(x); } int ctz(UI x) { return __builtin_ctz(x); } int ctz(LL x) { return __builtin_ctzll(x); } int ctz(ULL x) { return __builtin_ctzll(x); } int clz(int x) { return __builtin_clz(x); } int clz(UI x) { return __builtin_clz(x); } int clz(LL x) { return __builtin_clzll(x); } int clz(ULL x) { return __builtin_clzll(x); } int bit_parity(int x) { return __builtin_parity(x); } int bit_parity(UI x) { return __builtin_parity(x); } int bit_parity(LL x) { return __builtin_parityll(x); } int bit_parity(ULL x) { return __builtin_parityll(x); } template <class T, class U> bool one_of(T x, U y) { return x == y; } template <class T, class U, class... V> bool one_of(T x, U y, V... z) { return x == y || one_of(x, z...); } template <class T> bool diff(T _) { return true; } template <class T, class... U> bool diff(T x, U... y) { return !one_of(x, y...) && diff(y...); } template <class T, class U> bool equal(T x, U y) { return x == y; } template <class T, class U, class... V> bool equal(T x, U y, V... z) { return x == y && equal(x, z...); } template <class T, class U> bool in_order(T x, U y) { return x <= y; } template <class T, class U, class... V> bool in_order(T x, U y, V... z) { return x <= y && in_order(y, z...); } template <class T, class U> bool in_strorder(T x, U y) { return x < y; } template <class T, class U, class... V> bool in_strorder(T x, U y, V... z) { return x < y && in_strorder(y, z...); } template <class T> void swap3(T &x, T &y, T &z) { swap(x, y); swap(x, z); } template <class T> pair<T, T> dup(T x) { return make_pair(x, x); } template <class T> pair<pair<T, T>, T> thrice(T x) { return make_pair(make_pair(x, x), x); } template <class T> pair<pair<T, T>, pair<T, T>> fourfold(T x) { return make_pair(make_pair(x, x), make_pair(x, x)); } template <class T, class U, class V> T safvec(const vector<T> &v, int i, U low, V high) { if (i < 0) return low; if (i >= ((int)(v).size())) return high; return v[i]; } template <class T, class U> T safar(T *a, int i, U low) { if (i < 0) return low; return a[i]; } template <class T, class U, class V> auto med(const T &a, const U &b, const V &c) -> decltype(1 ? a : (1 ? b : c)) { return my_min(my_max(a, b), my_max(my_min(a, b), c)); } namespace std { template <class T, class U> struct hash<pair<T, U>> { size_t operator()(const pair<T, U> &a) const { return (hash<T>()(a.first) * 1420343LL ^ hash<U>()(a.second) * 923275LL) % 1463984161; } }; } // namespace std template <class T, class U> auto my_min(const U &x, const T &y) -> decltype(1 ? x : y) { return x < y ? x : y; } template <class T, class U> bool mini(U &x, T y) { if (y < x) { x = y; return true; } return false; } template <class T, class U, class V> auto my_min(const T &a, const U &b, const V &c) -> decltype(1 ? a : (1 ? b : c)) { return my_min(a, my_min(b, c)); } template <class T, class U, class V, class W> auto my_min(const T &a, const U &b, const V &c, const W &d) -> decltype(1 ? a : (1 ? b : (1 ? c : d))) { return my_min(a, my_min(b, c, d)); } template <class T, class U> auto my_max(const U &x, const T &y) -> decltype(1 ? x : y) { return x > y ? x : y; } template <class T, class U> bool maxi(U &x, T y) { if (y > x) { x = y; return true; } return false; } template <class T, class U, class V> auto my_max(const T &a, const U &b, const V &c) -> decltype(1 ? a : (1 ? b : c)) { return my_max(a, my_max(b, c)); } template <class T, class U, class V, class W> auto my_max(const T &a, const U &b, const V &c, const W &d) -> decltype(1 ? a : (1 ? b : (1 ? c : d))) { return my_max(a, my_max(b, c, d)); } template <class T> T brange(int l, int r) { return ~(~((T)0) << (r - l + 1)) << l; } template <class T> T brange(int l, int r, int second) { return (brange<T>(l, l + (r - l) / second * second - 1) / brange<T>(0, second - 1)) | ((T)1 << (l + (r - l) / second * second)); } template <class T> auto O2kQ(T x, int i, int j) -> vector<typename remove_reference<decltype(*x.end())>::type> { vector<typename remove_reference<decltype(*x.end())>::type> ans; for (auto c : x) if ((--i < 0) & (j-- >= 0)) ans.push_back(c); return ans; } template <class T> vector<T> O2kQ(const T *x, int i, int j) { vector<T> ans; for (int _ = i; _ <= (int)(j); _++) ans.push_back(x[_]); return ans; } int CON_BOUND = 10; string E0ms() { return ; } template <class T> string E0ms(T x) { stringstream os; os << boolalpha << x; return os.str(); } template <class T, class... U> string E0ms(T x, U... y) { return E0ms(x) + , + E0ms(y...); } string SqC6(bool x) { return x ? true : false ; } template <class T> typename enable_if<is_integral<T>::value, string>::type SqC6(T r) { string a; for (int i = 8 * sizeof(T) - 1; i >= (int)(0); i--) { if (i % 5 == 4) a += . ; a += 0 + !!(((T)1 << i) & r); } return a; } template <class T, class C> string SqC6(pair<T, C>); template <class T> auto SqC6(T x) -> decltype(x.end(), string()) { string a = ; for (auto c : x) { if (((int)(a).size())) a += , ; a += SqC6(c); } return { + a + }_ + to_string(((int)(x).size())); } template <class T, class C> string SqC6(pair<T, C> x) { return < + SqC6(x.first) + , + SqC6(x.second) + > ; } template <class T, int N> T &arrp(T a[][N], PII x) { return a[x.first][x.second]; } const int mod = 1e9 + 7; LL mpow(LL b, int e) { LL ans = 1; while (e) { if (e & 1) ans = ans * b % mod; b = b * b % mod; e /= 2; } return ans; } struct zet_p { int v; int get() { return v; } zet_p(LL x = 0) { v = x % mod; } zet_p operator+(zet_p x) { return (v + x.v) % mod; } zet_p operator-(zet_p x) { return v - x.v; } zet_p operator*(zet_p x) { return v * 1ll * x.v; } zet_p operator/(zet_p a) { return v * mpow(a.v, mod - 2); } }; const bool deb = false; struct frac_mod { int n, d; frac_mod(int x = 0) { n = x % mod; d = 1; } frac_mod(int x, int y) { n = x; d = y; } frac_mod operator+(frac_mod x) { return {(int)((x.d * 1ll * n + d * 1ll * x.n) % mod), x.d * d % mod}; } frac_mod operator-(frac_mod x) { return {(int)((n * 1ll * x.d - x.n * 1ll * d) % mod), x.d * d % mod}; } frac_mod operator*(frac_mod x) { return {(int)((n * (LL)x.n) % mod), (int)((d * (LL)x.d) % mod)}; } frac_mod operator/(frac_mod x) { return {(int)(n * (LL)x.d % mod), (int)(d * (LL)x.n % mod)}; } int get() { int ans = n * mpow(d, mod - 2) % mod; if (ans < 0) ans += mod; return ans; } }; template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator+( frac_mod x, T y) { return x + frac_mod(y); } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator+( T x, frac_mod y) { return frac_mod(x) + y; } template <class T> frac_mod &operator+=(frac_mod &x, T y) { return x = x + y; } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator-( frac_mod x, T y) { return x - frac_mod(y); } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator-( T x, frac_mod y) { return frac_mod(x) - y; } template <class T> frac_mod &operator-=(frac_mod &x, T y) { return x = x - y; } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator*( frac_mod x, T y) { return x * frac_mod(y); } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator*( T x, frac_mod y) { return frac_mod(x) * y; } template <class T> frac_mod &operator*=(frac_mod &x, T y) { return x = x * y; } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator/( frac_mod x, T y) { return x / frac_mod(y); } template <class T> typename enable_if<is_arithmetic<T>::value, frac_mod>::type operator/( T x, frac_mod y) { return frac_mod(x) / y; } template <class T> frac_mod &operator/=(frac_mod &x, T y) { return x = x / y; } template <class T, class C> ostream &operator<<(ostream &ans, pair<T, C> x) { return ans << < << x.first << , << x.second << > ; } template <class T> auto operator<<(ostream &o, T x) -> typename enable_if<!is_same<T, string>::value, decltype(x.end(), o)>::type { o << { ; int i = 0; for (auto c : x) { if (i++ == ((int)(x).size()) - 1 || ((int)(x).size()) < CON_BOUND + 2 || i < CON_BOUND) { if (i - 1 && (i != ((int)(x).size()) || ((int)(x).size()) < CON_BOUND + 2)) o << , ; o << c; } if (i == CON_BOUND && ((int)(x).size()) > CON_BOUND + 1) o << ... ; } return o << }_ << x.size(); } string b7Pe(string x) { int dep = 0; for (char c : x) { for (char d : ([{ ) if (c == d) dep++; for (char d : }]) ) if (c == d) dep--; for (char d : +-*/%^<>=&|?, ) if (c == d && !dep) return ( + x + ) ; } return x; } template <class T, class U> pair<U, T> pinv(pair<T, U> x) { return make_pair(x.second, x.first); } template <class T> struct Wt71 { T c; int l, r, L, R; }; template <class T> ostream &operator<<(ostream &o, Wt71<T> M) { o << n ; for (int i = M.l; i <= (int)(M.r); i++) { o << << i << : | ; for (int j = M.L; j <= (int)(M.R); j++) { o << M.c[i][j] << ((j - M.L + 1) % 5 ? : ; ); } o << n ; } return o; } template <class T> Wt71<T> q9cX(T a, int i, int j, int k, int q) { return {a, i, j, k, q}; } int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}, big_primes[] = {1307533217, 1336158883, 1339172111, 1367797777, 1376837461, 1399436671, 1426555723, 1440115249, 1444635091, 1489833511, 1553111299, 1562150983, 1587763421, 1610362631, 1652547823, 1666107349, 1700759471, 1782116627, 1797182767, 1810742293, 1818275363, 1892099449, 1904152361, 1919218501, 1940311097, 1944830939, 1968936763, 1977976447, 1985509517, 1990029359}, gens[] = {1, 2, 3, 5, 2, 11, 3, 14, 19, 11, 17, 32, 30, 34, 29, 19, 34, 31, 63, 33, 5, 63, 53, 58, 40, 83, 21, 18, 99, 33, 106, 40, 85, 109, 40, 77, 91, 120, 105, 91, 79, 53, 182, 10, 184, 48, 48, 150, 168, 72, 65, 46, 31, 216, 101, 134, 163, 147, 93, 42, 69, 7, 151, 308, 112, 12, 227, 185, 214, 305, 163, 234, 154, 316, 3, 11, 233, 390, 271, 173, 82, 407, 13, 135, 414, 391, 177, 293, 426, 432, 337, 281, 385, 103, 95, 152, 12, 371, 390, 77, 532, 34, 287, 73, 308, 493, 258, 549, 528, 411, 12, 283, 614, 596, 622, 309, 91, 511, 287, 433, 585, 123, 2, 170, 183, 341, 459, 322, 333, 337, 507, 718, 82, 517, 396, 315, 565, 48, 362, 617, 776, 425, 584, 231, 578, 395, 127, 27, 506, 435, 727, 659, 697, 385, 290, 109, 430, 422, 344, 849, 513, 453, 852, 920, 152, 482, 653, 271}, big_gens[] = {661761153, 1315209189, 235649158, 80905954, 861543922, 677870461, 332266749, 1192707557, 820697698, 93737534, 1395132003, 176849700, 1113502216, 860516128, 1543755630, 328298286, 1219407972, 1407392293, 733053145, 400000570, 1081174233, 847228024, 1516266762, 83276926, 500618997, 44975430, 47827763, 1411328206, 151880843, 19696955}; LL vbig_primes[] = { 1223771071LL, 2557943639LL, 4625922079LL, 8465729693LL, 16196062793LL, 32628322457LL, 64355194333LL, 128143942897LL, 256728067087LL, 512368953949LL, 1024440714547LL, 2048302681847LL, 4096121499499LL, 8192933500519LL, 16384652744209LL, 32768255472209LL, 65536007832649LL, 131072225217863LL, 262144391360893LL, 524288060819213LL, 1048576249040491LL, 2097152266756471LL, 4194304028438527LL, 8388608209001387LL, 16777216572671047LL, 33554432072300339LL, 67108864903274897LL, 134217728402776373LL, 268435456069367221LL, 536870912016065747LL}; const int inf = 1e9, NONE = -1; const LL llinf = 3e18; const char *sc2 = %d%d , *sc3 = %d%d%d , *sc4 = %d%d%d%d , *pr2 = %d %d n , *pr3 = %d %d %d n , *pr4 = %d %d %d %d n ; VI factor(int x) { VI ans; for (int p = 2; p * p <= x; ++p) while (x % p == 0) { ans.push_back(p); x /= p; } if (x > 1) ans.push_back(x); return ans; } template <class T, class B, class C, class D> auto operator+(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first + y.first), decltype(x.second + y.second)> { return make_pair(x.first + y.first, x.second + y.second); } template <class T, class B, class C> auto operator+(pair<T, B> x, C y) -> pair<decltype(x.first + y), decltype(x.second + y)> { return make_pair(x.first + y, x.second + y); } template <class T, class C, class D> auto operator+(T x, pair<C, D> y) -> pair<decltype(x + y.first), decltype(x + y.second)> { return make_pair(x + y.first, x + y.second); } template <class T, class B, class C, class D> auto operator-(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first - y.first), decltype(x.second - y.second)> { return make_pair(x.first - y.first, x.second - y.second); } template <class T, class B, class C> auto operator-(pair<T, B> x, C y) -> pair<decltype(x.first - y), decltype(x.second - y)> { return make_pair(x.first - y, x.second - y); } template <class T, class C, class D> auto operator-(T x, pair<C, D> y) -> pair<decltype(x - y.first), decltype(x - y.second)> { return make_pair(x - y.first, x - y.second); } template <class T, class B, class C, class D> auto operator/(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first / y.first), decltype(x.second / y.second)> { return make_pair(x.first / y.first, x.second / y.second); } template <class T, class B, class C> auto operator/(pair<T, B> x, C y) -> pair<decltype(x.first / y), decltype(x.second / y)> { return make_pair(x.first / y, x.second / y); } template <class T, class C, class D> auto operator/(T x, pair<C, D> y) -> pair<decltype(x / y.first), decltype(x / y.second)> { return make_pair(x / y.first, x / y.second); } template <class T, class B, class C, class D> auto operator*(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first * y.first), decltype(x.second * y.second)> { return make_pair(x.first * y.first, x.second * y.second); } template <class T, class B, class C> auto operator*(pair<T, B> x, C y) -> pair<decltype(x.first * y), decltype(x.second * y)> { return make_pair(x.first * y, x.second * y); } template <class T, class C, class D> auto operator*(T x, pair<C, D> y) -> pair<decltype(x * y.first), decltype(x * y.second)> { return make_pair(x * y.first, x * y.second); } template <class T, class B, class C, class D> auto operator%(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first % y.first), decltype(x.second % y.second)> { return make_pair(x.first % y.first, x.second % y.second); } template <class T, class B, class C> auto operator%(pair<T, B> x, C y) -> pair<decltype(x.first % y), decltype(x.second % y)> { return make_pair(x.first % y, x.second % y); } template <class T, class C, class D> auto operator%(T x, pair<C, D> y) -> pair<decltype(x % y.first), decltype(x % y.second)> { return make_pair(x % y.first, x % y.second); } template <class T, class B, class C, class D> auto operator&(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first & y.first), decltype(x.second & y.second)> { return make_pair(x.first & y.first, x.second & y.second); } template <class T, class B, class C> auto operator&(pair<T, B> x, C y) -> pair<decltype(x.first & y), decltype(x.second & y)> { return make_pair(x.first & y, x.second & y); } template <class T, class C, class D> auto operator&(T x, pair<C, D> y) -> pair<decltype(x & y.first), decltype(x & y.second)> { return make_pair(x & y.first, x & y.second); } template <class T, class B, class C, class D> auto operator|(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first | y.first), decltype(x.second | y.second)> { return make_pair(x.first | y.first, x.second | y.second); } template <class T, class B, class C> auto operator|(pair<T, B> x, C y) -> pair<decltype(x.first | y), decltype(x.second | y)> { return make_pair(x.first | y, x.second | y); } template <class T, class C, class D> auto operator|(T x, pair<C, D> y) -> pair<decltype(x | y.first), decltype(x | y.second)> { return make_pair(x | y.first, x | y.second); } template <class T, class B, class C, class D> auto operator^(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first ^ y.first), decltype(x.second ^ y.second)> { return make_pair(x.first ^ y.first, x.second ^ y.second); } template <class T, class B, class C> auto operator^(pair<T, B> x, C y) -> pair<decltype(x.first ^ y), decltype(x.second ^ y)> { return make_pair(x.first ^ y, x.second ^ y); } template <class T, class C, class D> auto operator^(T x, pair<C, D> y) -> pair<decltype(x ^ y.first), decltype(x ^ y.second)> { return make_pair(x ^ y.first, x ^ y.second); } template <class T, class B, class C, class D> auto operator&&(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first && y.first), decltype(x.second && y.second)> { return make_pair(x.first && y.first, x.second && y.second); } template <class T, class B, class C> auto operator&&(pair<T, B> x, C y) -> pair<decltype(x.first && y), decltype(x.second && y)> { return make_pair(x.first && y, x.second && y); } template <class T, class C, class D> auto operator&&(T x, pair<C, D> y) -> pair<decltype(x && y.first), decltype(x && y.second)> { return make_pair(x && y.first, x && y.second); } template <class T, class B, class C, class D> auto operator||(pair<T, B> x, pair<C, D> y) -> pair<decltype(x.first || y.first), decltype(x.second || y.second)> { return make_pair(x.first || y.first, x.second || y.second); } template <class T, class B, class C> auto operator||(pair<T, B> x, C y) -> pair<decltype(x.first || y), decltype(x.second || y)> { return make_pair(x.first || y, x.second || y); } template <class T, class C, class D> auto operator||(T x, pair<C, D> y) -> pair<decltype(x || y.first), decltype(x || y.second)> { return make_pair(x || y.first, x || y.second); } template <class T, class B> pair<T, B> &operator++(pair<T, B> &x) { ++x.first, ++x.second; return x; } template <class T, class B> pair<T, B> operator++(pair<T, B> &x, int _) { return make_pair(x.first++, x.second++); } template <class T, class B> pair<T, B> &operator--(pair<T, B> &x) { --x.first, --x.second; return x; } template <class T, class B> pair<T, B> operator--(pair<T, B> &x, int _) { return make_pair(x.first--, x.second--); } template <class T, class B, class C, class D> pair<T, B> &operator+=(pair<T, B> &x, pair<C, D> y) { x.first += y.first; x.second += y.second; return x; } template <class T, class B, class C> pair<T, B> &operator+=(pair<T, B> &x, C y) { x.first += y; x.second += y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator-=(pair<T, B> &x, pair<C, D> y) { x.first -= y.first; x.second -= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator-=(pair<T, B> &x, C y) { x.first -= y; x.second -= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator*=(pair<T, B> &x, pair<C, D> y) { x.first *= y.first; x.second *= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator*=(pair<T, B> &x, C y) { x.first *= y; x.second *= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator/=(pair<T, B> &x, pair<C, D> y) { x.first /= y.first; x.second /= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator/=(pair<T, B> &x, C y) { x.first /= y; x.second /= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator%=(pair<T, B> &x, pair<C, D> y) { x.first %= y.first; x.second %= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator%=(pair<T, B> &x, C y) { x.first %= y; x.second %= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator^=(pair<T, B> &x, pair<C, D> y) { x.first ^= y.first; x.second ^= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator^=(pair<T, B> &x, C y) { x.first ^= y; x.second ^= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator&=(pair<T, B> &x, pair<C, D> y) { x.first &= y.first; x.second &= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator&=(pair<T, B> &x, C y) { x.first &= y; x.second &= y; return x; } template <class T, class B, class C, class D> pair<T, B> &operator|=(pair<T, B> &x, pair<C, D> y) { x.first |= y.first; x.second |= y.second; return x; } template <class T, class B, class C> pair<T, B> &operator|=(pair<T, B> &x, C y) { x.first |= y; x.second |= y; return x; } template <class T, class B> auto operator*(pair<T, B> x) -> pair<typename remove_reference<decltype(*x.first)>::type, typename remove_reference<decltype(*x.second)>::type> { return make_pair(*x.first, *x.second); } template <class T, class B> auto operator~(pair<T, B> x) -> pair<typename remove_reference<decltype(~x.first)>::type, typename remove_reference<decltype(~x.second)>::type> { return make_pair(~x.first, ~x.second); } template <class T, class B> auto operator!(pair<T, B> x) -> pair<typename remove_reference<decltype(!x.first)>::type, typename remove_reference<decltype(!x.second)>::type> { return make_pair(!x.first, !x.second); } template <class T, class B> auto operator-(pair<T, B> x) -> pair<typename remove_reference<decltype(-x.first)>::type, typename remove_reference<decltype(-x.second)>::type> { return make_pair(-x.first, -x.second); } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator+(zet_p x, T y) { return x + zet_p(y); } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator+(T x, zet_p y) { return zet_p(x) + y; } template <class T> zet_p &operator+=(zet_p &x, T y) { return x = x + y; } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator-(zet_p x, T y) { return x - zet_p(y); } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator-(T x, zet_p y) { return zet_p(x) - y; } template <class T> zet_p &operator-=(zet_p &x, T y) { return x = x - y; } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator*(zet_p x, T y) { return x * zet_p(y); } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator*(T x, zet_p y) { return zet_p(x) * y; } template <class T> zet_p &operator*=(zet_p &x, T y) { return x = x * y; } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator/(zet_p x, T y) { return x / zet_p(y); } template <class T> typename enable_if<is_arithmetic<T>::value, zet_p>::type operator/(T x, zet_p y) { return zet_p(x) / y; } template <class T> zet_p &operator/=(zet_p &x, T y) { return x = x / y; } void jGB4A(); const int MN = 1e5 + 44; SPII best[MN]; void add(SPII &se, PII val) { ; auto pre = se.upper_bound(make_pair(val.first, inf)); pre--; if (pre->second > val.second) { ; return; } while (pre != se.end() && pre->second <= val.second) { if (pre->first >= val.first) se.erase(pre++); else pre++; } se.insert(val); ; } int main() { SPII test; for (int i = 0; i < (int)(10); i++) test.insert(make_pair(i * 3, i * 3)); add(test, make_pair(6, 6)); add(test, make_pair(6, 7)); add(test, make_pair(6, 6)); add(test, make_pair(0, 7)); ; ; ; ; cerr << setprecision(10); int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= (int)(n); i++) { best[i].insert(make_pair(-inf, 0)); } for (int i = 0; i < (int)(m); i++) { int a, b, x; scanf( %d%d%d , &a, &b, &x); auto it = best[a].upper_bound(make_pair(x, -inf)); ; ; it--; ; add(best[b], make_pair(x, it->second + 1)); } int ans = 0; for (int i = 1; i <= (int)(n); i++) { auto l = best[i].end(); l--; maxi(ans, l->second); } printf( %d n , ans); jGB4A(); } void jGB4A() { #pragma GCC diagnostic ignored -Woverflow ; ; ; ; ; ; ; ; ; ; while (deb && getchar() != EOF) ; exit(0); } |
#include <bits/stdc++.h> using namespace std; const int MXN = 1e5 + 1; int n, dp[MXN], k[MXN], ans = 1, rg[MXN], res, maxi; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> k[i]; dp[k[i]] += dp[k[i] - 1] + 1; } for (int i = 1; i <= n; i++) { if (dp[k[i]] > maxi) { maxi = dp[k[i]]; } } cout << n - maxi; return 0; } |
#include <bits/stdc++.h> using namespace std; void base() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; if (a > b) swap(a, b); if (a * 2 < b) cout << NO n ; else { int dif = b - a; if (a - dif >= 0 && b - 2 * dif >= 0 && (a - dif) % 3 == 0) { cout << YES n ; } else { cout << NO n ; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; long long int power(long long int x, long long int y, long long int mod = 1000000007) { long long int ret = 1; x = x % mod; while (y) { if (y & 1) ret = (ret * x) % mod; y = y >> 1; x = (x * x) % mod; } return ret % mod; } long long int isprime(long long int x) { for (long long int i = 2; i * i <= x; i++) if (x % i == 0) return 0; return 1; } void solve() { long long int n, k; cin >> n >> k; if (n <= k) { cout << 1 << endl; return; } vector<long long int> fact; for (long long int i = 1; i * i <= n; i++) { if (n % i == 0) { if (i <= k) fact.push_back(i); if ((n / i) <= k) fact.push_back(n / i); } } sort(fact.begin(), fact.end()); cout << n / fact.back() << endl; } signed main() { long long int t; cin >> t; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; int main(void) { char sav[200]; scanf( %s , sav); int flag = 0; int len = strlen(sav); for (int i = 0; i < len; i++) { if (sav[i] == 1 ) continue; else if (sav[i] == 4 ) { if (i - 1 >= 0 && sav[i - 1] == 1 ) continue; else if (i - 1 >= 0 && sav[i - 1] == 4 ) { if (i - 2 >= 0 && sav[i - 2] == 1 ) continue; else flag = 1; } else flag = 1; } else flag = 1; } if (flag) cout << NO << endl; else cout << YES << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T1, class T2, class T3> bool _IN(T1 x, T2 y, T3 z) { return x <= y && x >= z || x <= z && x >= y; } unsigned long long gcd(unsigned long long a, unsigned long long b) { if (!b) return a; while (b ^= a ^= b ^= a %= b) ; return a; } extern const long long mod; long long ksm(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } char DATaJNTFnlmAoya[2]; double PDlaQLoCkCjCKyr; bool S(char* a) { return scanf( %s , a) == 1; } bool S(int& a) { return scanf( %d , &a) == 1; } bool S(bool& a) { return scanf( %d , &a) == 1; } bool S(unsigned& a) { return scanf( %u , &a) == 1; } bool S(float& a) { return scanf( %f , &a) == 1; } bool S(double& a) { return scanf( %lf , &a) == 1; } bool S(long long& a) { return scanf( %I64d , &a) == 1; } bool S(unsigned long long& a) { return scanf( %I64u , &a) == 1; } bool S(long double& a) { if (scanf( %lf , &PDlaQLoCkCjCKyr) == -1) return 0; a = PDlaQLoCkCjCKyr; return 1; } bool S(char& a) { if (scanf( %1s , DATaJNTFnlmAoya) == -1) return 0; a = *DATaJNTFnlmAoya; return 1; } bool SL(char* a) { a[0] = 0; while (gets(a) && !a[0]) ; return a[0]; } void _P(const int& x) { printf( %d , x); } void _P(const bool& x) { printf( %d , x); } void _P(const unsigned& x) { printf( %u , x); } void _P(const char& x) { printf( %c , x); } void _P(const char* x) { printf( %s , x); } void _P(const string& x) { printf( %s , x.c_str()); } void _P(const long long& x) { printf( %I64d , x); } void _P(const unsigned long long& x) { printf( %I64u , x); } void _P(const float& x) { printf( %.2f , x); } void _P(const double& x) { printf( %.2f , x); } void _P(const long double& x) { printf( %.2f , (double)x); } template <class T1, class T2> bool S(T1& a, T2& b) { return S(a) + S(b) == 2; } template <class T1, class T2, class T3> bool S(T1& a, T2& b, T3& c) { return S(a) + S(b) + S(c) == 3; } template <class T1, class T2, class T3, class T4> bool S(T1& a, T2& b, T3& c, T4& d) { return S(a) + S(b) + S(c) + S(d) == 4; } template <class T1, class T2, class T3, class T4, class T5> bool S(T1& a, T2& b, T3& c, T4& d, T5& e) { return S(a) + S(b) + S(c) + S(d) + S(e) == 5; } template <class T1> void P(const T1& a) { _P(a); putchar( ); } template <class T1, class T2> void P(const T1& a, const T2& b) { _P(a); putchar( ); _P(b); putchar( ); } template <class T1> void PN(const T1& a) { _P(a); puts( ); } template <class T1, class T2> void PN(const T1& a, const T2& b) { _P(a); putchar( ); _P(b); puts( ); } template <class T1, class T2, class T3> void PN(const T1& a, const T2& b, const T3& c) { _P(a); putchar( ); _P(b); putchar( ); _P(c); puts( ); } template <class T1, class T2, class T3, class T4> void PN(const T1& a, const T2& b, const T3& c, const T4& d) { _P(a); putchar( ); _P(b); putchar( ); _P(c); putchar( ); _P(d); puts( ); } template <class T1, class T2, class T3, class T4, class T5> void PN(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e) { _P(a); putchar( ); _P(b); putchar( ); _P(c); putchar( ); _P(d); putchar( ); _P(e); puts( ); } template <class T> void PA(T* a, int n) { for (long long i = 0, pJNwFPtlXiwFoIv = (n - 1); i < pJNwFPtlXiwFoIv; i++) _P(a[i]), putchar( ); PN(a[n - 1]); } template <class T> void PA(const T& x) { for (__typeof((x).begin()) it = (x).begin(), CluhxSchFuDeugk = (x).end(); it != CluhxSchFuDeugk; it++) { _P(*it); if (it == --x.end()) puts( ); else putchar( ); } } int kase; const double pi = 4 * atan(1); const double ep = 1e-9; const int INF = 0x3f3f3f3f; const long long INFL = 0x3f3f3f3f3f3f3f3fll; const long long mod = 1000000007; template <long long mod, long long lim, int MAX_D> struct Hash_set { struct node { string x; node* next; node(node* a = 0, const string& b = ) : next(a), x(b) {} void add(const string& a) { erase(a); next = new node(next, a); } bool find(const string& a) { node* cur = next; while (cur) { if (cur->x == a) return 1; cur = cur->next; } return 0; } void clear() { while (next) { node* tmp = next->next; delete next; next = tmp; } } void erase(const string& a) { node *cur = next, *p = this; while (cur) { if (cur->x == a) { node* tmp = cur; p->next = cur->next; delete cur; cur = p->next; } if (!cur) break; p = cur; cur = cur->next; } } }; long long* d; node* ha; Hash_set() { ha = new node[mod]; d = new long long[MAX_D]; d[0] = 1; for (int i = 1; i < MAX_D; i++) d[i] = d[i - 1] * lim % mod; } ~Hash_set() { for (int i = 0; i < mod; i++) ha[i].clear(); delete[] ha; delete[] d; } long long hash(const string& x) { long long res = 0; for (int i = 0; i < x.size(); i++) res = (res + x[i] * d[i]) % mod; return res; } bool find(const string& x, int h = -1) { if (h == -1) h = hash(x); return ha[h].find(x); } void insert(const string& x, int h = -1) { if (h == -1) h = hash(x); ha[h].add(x); } void erase(const string& x, int h = -1) { if (h == -1) h = hash(x); ha[h].erase(x); } void clear() { for (int i = 0; i < mod; i++) ha[i].clear(); } int convert(long long h, int where, char from, char to) { return ((h + ((long long)to - from) * d[where]) % mod + mod) % mod; } }; char bf[600005]; Hash_set<600011, 311, 600000> x; bool OK() { S(bf); string cur = bf; long long ha = x.hash(cur); for (long long i = 0, pJNwFPtlXiwFoIv = (cur.size()); i < pJNwFPtlXiwFoIv; i++) { char c = cur[i]; for (long long j = ( a ), alVDbhLBoMEGSwA = ( c ); j <= alVDbhLBoMEGSwA; j++) { if (j == c) continue; cur[i] = j; long long ch = x.convert(ha, i, c, j); if (x.find(cur, ch)) return 1; } cur[i] = c; } return 0; } int main() { ; int n, m; S(n, m); while (n--) { S(bf); x.insert(bf); } while (m--) { puts(OK() ? YES : NO ); } } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool Chkbit(int N, int pos) { return (bool)(N & (1 << pos)); } int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; const double eps = 1e-6; int n, m, k; int vis[2000005]; int achhe[2000005]; int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int x; scanf( %d , &x); vis[x] = 1; } int ans = 1; int fl = 0; achhe[1] = 1; for (int i = 1; i <= k; i++) { int x, y; scanf( %d %d , &x, &y); if (fl == 0) { if (achhe[x]) { achhe[x] = 0; achhe[y] = 1; if (vis[y]) { fl = 1; ans = y; } else ans = y; } else if (achhe[y]) { achhe[y] = 0; achhe[x] = 1; if (vis[x]) { fl = 1; ans = x; } else ans = x; } else { int nothing; } } } if (vis[1]) { cout << 1 << endl; return 0; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n; void solve() { cin >> n; long long x, y, a, b; cout << YES n ; for (long long i = 1; i <= (n); ++i) { cin >> x >> y >> a >> b; cout << 2 * (x & 1) + (y & 1) + 1; cout << n ; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); solve(); return 0; } |
#include<bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(0);cin.tie(NULL); int t; cin>>t; while(t--) { ll n,x; ll min=0,max=0,s=0; cin>>n>>x; ll a[n]; for(ll i=0;i<n;i++) { cin>>a[i]; s+=a[i]; if(a[i]%x==0) max+=(a[i]/x); else max+=((a[i]/x)+1); } if(s%x==0) min+=(s/x); else min+=((s/x)+1); cout<<min<< <<max<<endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace chrono; const long long int MOD = 1000000007; const long long int MAXN = 100005; long long int k, dp[MAXN]; long long int recurse(long long int n) { if (n == 0) return 1; if (dp[n] != -1) return dp[n]; long long int ans = 0; if (n >= k) { ans = (ans + recurse(n - k)) % MOD; } if (n > 0) { ans = (ans + recurse(n - 1)) % MOD; } return (dp[n] = ans); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t >> k; long long int a, b; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (long long int i = 1; i < 100001; ++i) { dp[i] = recurse(i); } for (long long int i = 1; i < 100001; ++i) { dp[i] = (dp[i] + dp[i - 1]) % MOD; } while (t--) { cin >> a >> b; cout << ((MOD + dp[b] - dp[a - 1])) % MOD << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long getint() { long long _x = 0, _tmp = 1; char _tc = getchar(); while ((_tc < 0 || _tc > 9 ) && _tc != - ) _tc = getchar(); if (_tc == - ) _tc = getchar(), _tmp = -1; while (_tc >= 0 && _tc <= 9 ) _x *= 10, _x += (_tc - 0 ), _tc = getchar(); return _x * _tmp; } long long mypow(long long _a, long long _x, long long _mod) { if (_x == 0) return 1ll; long long _tmp = mypow(_a, _x / 2, _mod); _tmp = (_tmp * _tmp) % _mod; if (_x & 1) _tmp = (_tmp * _a) % _mod; return _tmp; } inline long long add(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x + _y; if (_ >= _mod) _ -= _mod; return _; } inline long long sub(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x - _y; if (_ < 0) _ += _mod; return _; } inline long long mul(long long _x, long long _y, long long _mod = 1000000007ll) { long long _ = _x * _y; if (_ >= _mod) _ %= _mod; return _; } inline bool equal(double _x, double _y) { return _x > _y - 1e-9 && _x < _y + 1e-9; } int __ = 1, _cs; int n, k; vector<int> v; void build() {} void init() { n = getint(); k = getint(); for (int i = 1; i <= n; i++) v.push_back(getint()); sort((v).begin(), (v).end()); v.resize(unique((v).begin(), (v).end()) - v.begin()); } int dp[1001 * 1001]; void solve() { for (int i = 1; i <= (v.back() - v[0]) * k; i++) { dp[i] = 1001 * 1001; for (size_t j = 1; j < v.size(); j++) if (v[j] - v[0] > i) break; else dp[i] = min(dp[i], dp[i - v[j] + v[0]] + 1); } for (int i = 0; i <= (v.back() - v[0]) * k; i++) if (dp[i] <= k) printf( %d , i + v[0] * k); puts( ); } int main() { build(); while (__--) { init(); solve(); } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, k; string s; while (cin >> n >> s) { int sum = 0; int left = 0, right = 0, up = 0, down = 0; for (i = 0; i < n; i++) { if (s[i] == R ) { if (left == 1) { sum++; left = 0; up = 0; down = 0; } right = 1; } else if (s[i] == L ) { if (right == 1) { sum++; right = 0; up = 0; down = 0; } left = 1; } else if (s[i] == U ) { if (down == 1) { sum++; down = 0; left = 0; right = 0; } up = 1; } else if (s[i] == D ) { if (up == 1) { sum++; up = 0; left = 0; right = 0; } down = 1; } } cout << sum + 1 << endl; } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,unroll-loops ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2 ) using namespace std; mt19937 mrnd(time(0)); const int INF = 1000000007; const long long INF_LL = 1152921504606846977; void solve() { int n, m; cin >> n >> m; vector<set<int>> g(n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; if (u > v) swap(u, v); g[u].insert(v); } set<int> dead; for (int i = 0; i < n; ++i) { if (!g[i].empty()) { dead.insert(i); } } int q; cin >> q; auto upd = [&](int node) { if (g[node].empty()) { dead.erase(node); } else { dead.insert(node); } }; for (int i = 0; i < q; ++i) { int z, u, v; cin >> z; if (z == 1) { cin >> u >> v; --u, --v; if (u > v) swap(u, v); g[u].insert(v); upd(u), upd(v); } else if (z == 2) { cin >> u >> v; --u, --v; if (u > v) swap(u, v); g[u].erase(v); upd(u), upd(v); } else { cout << n - int(dead.size()) << n ; } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(20); int tests = 1; while (tests--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, mostleft = 1000000000, mostright = 0, ans = -1; int l[100000], r[100000]; bool bo_l[100000], bo_r[100000]; cin >> n; for (int i = 0; i < n; i++) { bo_l[i] = false; bo_r[i] = false; } for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; mostleft = min(mostleft, l[i]); mostright = max(mostright, r[i]); } for (int i = 0; i < n; i++) { if (mostleft == l[i]) bo_l[i] = true; if (mostright == r[i]) bo_r[i] = true; } for (int i = 0; i < n; i++) { if (bo_l[i] && bo_r[i]) ans = i + 1; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = 200005; const unsigned long long HA = 41793404541; int n, m; int a[maxN], b[maxN]; unsigned long long fac[maxN], pre[maxN], nxt[maxN]; int ansn, ans[maxN]; bool check(int l, int r) { return pre[r] - pre[l - 1] * fac[r - l + 1] == nxt[l] - nxt[r + 1] * fac[r - l + 1]; } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); for (int i = 1; i < n; ++i) b[i] = a[i + 1] - a[i]; fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * HA; for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] * HA + (unsigned long long)b[i]; for (int i = n - 1; i; --i) nxt[i] = nxt[i + 1] * HA + (unsigned long long)b[i]; for (int i = 1; i <= n; ++i) { bool pd = true; if (i != 1) pd &= check(1, i - 1); if (i != n) { pd &= (a[1] + a[i] == a[i + 1] + a[n] - m); if (i != n - 1) pd &= check(i + 1, n - 1); } if (pd) ans[++ansn] = (a[1] + a[i]) % m; } printf( %d n , ansn); sort(ans + 1, ans + ansn + 1); for (int i = 1; i <= ansn; ++i) printf( %d , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000000; int f[N], sz[N], ak, Ak, n, m, ans[N]; struct data { int a, b, c, d, e; data(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) : a(a), b(b), c(c), d(d), e(e) {} } S[N]; struct edge { int x, y, z, id; bool operator<(const edge& e) const { return z < e.z; } } e[N], ee[N]; int find(int x) { return x == f[x] ? x : find(f[x]); } void me(int x, int y) { int X = find(x), Y = find(y); if (X == Y) return; S[Ak++] = data(X, Y, sz[X], sz[Y], ak); if (sz[X] & 1 & sz[Y]) ak -= 2; if (sz[X] > sz[Y]) swap(X, Y); f[X] = Y; sz[Y] += sz[X]; } void pop(int tt) { while (Ak > tt) { data haha = S[--Ak]; ak = haha.e; f[haha.a] = haha.a; f[haha.b] = haha.b; sz[haha.a] = haha.c; sz[haha.b] = haha.d; } } void solve(int l, int r, int x, int y) { if (x > y) return; if (l == r) { for (int i = x; i <= y; i++) ans[i] = l; return; } int mid = (l + r) >> 1, t = y + 1, tt = Ak; for (int i = l; i <= mid; i++) if (ee[i].id < x) me(ee[i].x, ee[i].y); for (int i = x; i <= y; i++) { if (e[i].z <= mid) me(e[i].x, e[i].y); if (ak == 0) { t = i; break; } } pop(tt); for (int i = x; i < t; i++) if (e[i].z < l) me(e[i].x, e[i].y); solve(l, mid, t, y); pop(tt); for (int i = l; i <= mid; i++) if (ee[i].id < x) me(ee[i].x, ee[i].y); solve(mid + 1, r, x, t - 1); pop(tt); } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) f[i] = i, sz[i] = 1; ak = n; for (int i = 1; i <= m; i++) { scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z); e[i].id = i; ee[i] = e[i]; } sort(ee + 1, ee + 1 + m); for (int i = 1; i <= m; i++) e[ee[i].id].z = i; solve(1, m + 1, 1, m); ee[m + 1].z = -1; for (int i = 1; i <= m; i++) printf( %d n , ee[ans[i]].z); return 0; } |
#include <bits/stdc++.h> const long long mod = 1000000007; const int maxn = 100000; using namespace std; int main() { long long n, k; scanf( %I64d , &n); scanf( %I64d , &k); vector<long long> fib(n + 1); fib[1] = 1; for (int i = 2; i < n + 1; i++) fib[i] = fib[i - 1] + fib[i - 2]; for (int i = 0; i < n; i++) { if (fib[n - i] < k) { cout << i + 2 << << i + 1 << ; k -= fib[n - i]; i++; } else cout << i + 1 << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; int x = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { x++; } } cout << x; return 0; } |
#include <bits/stdc++.h> using namespace std; [[maybe_unused]] const int MAX = INT32_MAX, MIN = -INT32_MAX, maxInd = 1E6 + 1E5 + 1; [[maybe_unused]] bool directed = true, end_now = false; bool possible = false; int n, k; struct node_data_structure { int pre = 0, post = 0; bool visited = false, processed = false; } nodes[maxInd]; vector<int> graph[maxInd]; void insert_graph(int pre, int post) { graph[pre].push_back(post); nodes[pre].post++; nodes[post].pre++; if (!directed) { graph[post].push_back(pre); nodes[post].post++; nodes[pre].pre++; } } [[maybe_unused]] void undoVisit() { for (auto &i : nodes) { i.visited = false; i.processed = false; } end_now = false; } void dfs(int u, int ac = 1) { if (end_now) return; if (u > 1E6) { if (u - 1E6 < ac) return; if (u - 1E6 + k > n || u - 1E6 == n) { end_now = true; possible = true; return; } } else { if (u < ac) return; if (u + k > n || u == n) { end_now = true; possible = true; return; } } nodes[u].visited = true; for (int v : graph[u]) { if (!nodes[v].visited) { dfs(v, ac + 1); } } nodes[u].processed = true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s1, s2; cin >> n >> k >> s1 >> s2; for (int i = 1; i < n; i++) { if (i + k <= n) { if (s1[i - 1] == - && s2[i - 1 + k] == - ) { insert_graph(i, 1E6 + i + k); } if (s2[i - 1] == - && s1[i - 1 + k] == - ) { insert_graph(1E6 + i, i + k); } } if (s1[i] == - && s1[i - 1] == - ) { insert_graph(i, i + 1); insert_graph(i + 1, i); } if (s2[i] == - && s2[i - 1] == - ) { insert_graph(1E6 + i, 1E6 + i + 1); insert_graph(1E6 + i + 1, 1E6 + i); } } dfs(1); possible ? cout << YES << endl : cout << NO << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long mul(long long a, long long b) { return a * b % mod; } long long add(long long a, long long b) { long long res = a + b; if (res >= mod) { res -= mod; } return res; } const long long MAXN = 1e6 + 5; vector<long long> fact(MAXN); vector<long long> fact_obr(MAXN); vector<long long> pow_n(MAXN); vector<long long> pow_m(MAXN); long long n; long long m; long long fast_pow(long long a, long long b) { if (b == 0) { return 1; } else { if (b % 2 == 1) { return mul(fast_pow(a, b - 1), a); } else { long long res = fast_pow(a, b / 2); return mul(res, res); } } } void pre_calc() { fact[0] = 1; fact_obr[0] = fast_pow(fact[0], mod - 2); pow_n[0] = 1; pow_m[0] = 1; for (long long i = 1; i < MAXN; ++i) { fact[i] = mul(fact[i - 1], i); fact_obr[i] = fast_pow(fact[i], mod - 2); pow_n[i] = mul(pow_n[i - 1], n); pow_m[i] = mul(pow_m[i - 1], m); } } long long A(long long n, long long k) { return mul(fact[n], fact_obr[n - k]); } long long C(long long n, long long k) { if (k > n) { return 0; } if (n == 0 && k != 0) { return 0; } return mul(A(n, k), fact_obr[k]); } long long f(long long y) { if (y == n) { return 1; } return mul(y, pow_n[n - y - 1]); } int32_t main() { cin >> n >> m; pre_calc(); long long ans = 0; for (long long edges = 1; edges < n; ++edges) { long long _add = 1; _add = mul(_add, A(n - 2, edges - 1)); _add = mul(_add, f(edges + 1)); _add = mul(_add, C(m - 1, edges - 1)); _add = mul(_add, pow_m[n - edges - 1]); ans = add(ans, _add); } cout << ans; } |
#include <bits/stdc++.h> using namespace std; struct creature { long long hp, dmg; long long div; bool operator<(const creature& rhs) const { return div > rhs.div; } }; int n, a, b; creature in[200050]; long long ans = 0; int it, use; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b; for (int i = 0; i < n; ++i) { cin >> in[i].hp >> in[i].dmg; in[i].div = in[i].hp - in[i].dmg; } sort(in, in + n); use = b; for (int i = 0; i < n; ++i) { if (use && in[i].hp > in[i].dmg) { in[i].dmg = in[i].hp; --use; } ans += in[i].dmg; } it = b - use; int icur = it - 1; in[n] = {0, 0, 0}; long long dx = 0; if (b && a) for (int i = 0; i < n; ++i) { long long tmp = (in[i].hp << a) - in[i].dmg; if (use) dx = dx > tmp ? dx : tmp; else if (i < it) dx = tmp > dx ? tmp : dx; else dx = (tmp - in[icur].div > dx) ? tmp - in[icur].div : dx; } cout << ans + dx; return 0; } |
#include <bits/stdc++.h> using LL = long long; const int kN = 200000 + 5; const int kLog = 19; const LL inf = 3e14; std::vector<std::pair<int, int>> edges[kN]; int n, m, s; LL dist[kN]; int plca[kLog][kN], depth[kN]; int sz[kN]; int get_lca(int a, int b) { if (depth[a] < depth[b]) std::swap(a, b); for (int i = 0; i < kLog; ++i) if (depth[a] - depth[b] >> i & 1) a = plca[i][a]; if (a == b) return a; for (int i = kLog - 1; i >= 0; --i) if (plca[i][a] != plca[i][b]) a = plca[i][a], b = plca[i][b]; return plca[0][a]; } int main() { scanf( %d%d%d , &n, &m, &s); s--; for (int i = 0; i < m; ++i) { int u, v, w; scanf( %d%d%d , &u, &v, &w); u--; v--; edges[u].emplace_back(w, v); edges[v].emplace_back(w, u); } std::priority_queue<std::pair<LL, int>> pq; std::fill(dist, dist + n, inf); dist[s] = 0; pq.emplace(0, s); std::vector<int> order; while (!pq.empty()) { LL tmp; int u; std::tie(tmp, u) = pq.top(); pq.pop(); if (-tmp != dist[u]) continue; order.emplace_back(u); int lca = -1; for (auto e : edges[u]) { if (dist[e.second] > dist[u] + e.first) { dist[e.second] = dist[u] + e.first; pq.emplace(-dist[e.second], e.second); } else if (dist[e.second] + e.first == dist[u]) { if (lca == -1) lca = e.second; else lca = get_lca(lca, e.second); } } plca[0][u] = lca; depth[u] = lca == -1 ? 0 : depth[lca] + 1; for (int i = 1; i < kLog; ++i) plca[i][u] = plca[i - 1][u] == -1 ? -1 : plca[i - 1][plca[i - 1][u]]; } int result = 0; std::fill(sz, sz + n, 1); for (int i = (int)order.size() - 1; i > 0; --i) { int u = order[i]; sz[plca[0][u]] += sz[u]; result = std::max(result, sz[u]); } printf( %d n , result); } |
#include <bits/stdc++.h> using namespace std; int n, w; double tmp; vector<double> a; vector<double> boy; vector<double> girl; double volume; double base; int main() { cin >> n >> w; for (int i = 0; i < (2 * n); i++) { cin >> tmp; a.push_back(tmp); } sort(a.begin(), a.end()); girl.assign(a.begin(), a.begin() + n); boy.assign(a.begin() + n, a.end()); if (boy[0] / 2 > girl[0]) base = girl[0]; else base = boy[0] / 2; volume = (3 * base * n); if (volume > w) volume = w; cout << fixed << setprecision(10) << volume; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int kase, ks = 0; int i, j, k; int n; int arr[1000]; string s; cin >> n; for (i = 1; i < n + 1; i++) cin >> arr[i]; int q; cin >> q; int x, y; while (q--) { cin >> x >> y; if (x > 1) arr[x - 1] += (y - 1); if (x < n) arr[x + 1] += (arr[x] - y); arr[x] = 0; } for (i = 1; i < n + 1; i++) cout << arr[i] << endl; return 0; } |
#include <bits/stdc++.h> int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) printf( %d , i % 2); puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 3000 + 12; int p[maxn]; int Et[maxn]; int mark[maxn][maxn]; queue<pair<int, int> > act; void input(int n) { for (int i = 0; i < n; i++) cin >> p[i]; } void solve(int n, int k) { int sef = 0; bool yek = 0; for (int j = n - 1; j >= 0; j--) { if (p[j] == 0) sef++; if (p[j] == 100) yek = 1; if (sef == n - j) Et[j] = 1; if (yek) Et[j] = 2; } act.push(make_pair(0, 1)); mark[0][1] = 0; int ans = 0; int x = 0, y = 1; while (act.size() && mark[x][y] <= k) { pair<int, int> a = act.front(); x = a.first, y = a.second; if (mark[x][y] <= k) ans++; if (Et[y] != 1 && p[x] != 0 && y + 2 <= n + 1 && mark[y + 1][y + 2] > mark[x][y] + 1) act.push(make_pair(y + 1, y + 2)), mark[y + 1][y + 2] = mark[x][y] + 1; if (Et[y] != 2 && p[x] != 0 && y + 1 <= n && mark[x][y + 1] > mark[x][y] + 1) act.push(make_pair(x, y + 1)), mark[x][y + 1] = mark[x][y] + 1; if (Et[y] != 1 && p[x] != 100 && y + 1 <= n && mark[y][y + 1] > mark[x][y] + 1) act.push(make_pair(y, y + 1)), mark[y][y + 1] = mark[x][y] + 1; act.pop(); } cout << ans; } int main() { int n, k; cin >> n >> k; input(n); memset(mark, 31, sizeof mark); solve(n, k); return 0; } |
#include <bits/stdc++.h> using namespace std; using lli = long long; using pii = pair<int, int>; using pll = pair<lli, lli>; template <typename T> using Vector = vector<vector<T>>; template <typename T> using Prior = priority_queue<T>; template <typename T> using prior = priority_queue<T, vector<T>, greater<T>>; const lli mod = 1E9 + 7; void solve() { lli x, len; string s; cin >> x >> s; len = s.size(); for (int i = 1; i <= x; ++i) { if (s.size() < x) { if (s[i - 1] == 2 ) s += s.substr(i, s.size() - i); if (s[i - 1] == 3 ) s += s.substr(i, s.size() - i) + s.substr(i, s.size() - i); len = s.size(); } else { if (s[i - 1] == 2 ) len = (2 * len - i) % mod; if (s[i - 1] == 3 ) len = (3 * len - 2 * i) % mod; } } cout << (len < 0 ? len + mod : len) << n ; } int main() { int t = 1; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, min = 1000; scanf( %d %d , &n, &m); vector<int> v(m); for (int i = 0; i < m; i++) scanf( %d , &v[i]); sort(v.begin(), v.end()); for (int i = 0; n + i - 1 < m; i++) { if (min > v[n + i - 1] - v[i]) min = v[n + i - 1] - v[i]; } printf( %d , min); return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[1000007], b[1000007], i, n, x, s, f, t; int main() { cin >> n; for (i = 1; i <= 2 * n; i++) { cin >> x; if (a[x] == 0) a[x] = i; else if (a[x] != 0) b[x] = i; } s = a[1] - 1; for (x = 2; x <= n; x++) { t = a[x] - a[x - 1]; if (t < 0) t = -t; s = s + t; } s = s + b[1] - 1; for (x = 2; x <= n; x++) { t = b[x] - b[x - 1]; if (t < 0) t = -t; s = s + t; } cout << s; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100005; char s[N]; int n, a[N], pre[N][26], suf[N][26]; int f[N][26], g[N], mem[27]; inline int mex(int st) { return __builtin_ctz(~st); } int calc(int l, int r, int c, int fl) { if (l > r) return 0; if (~mem[c]) return mem[c]; int st = 0; for (int j = 0; j < 26; j++) { int ll = suf[l][j], rr = pre[r][j]; if (rr >= l) st |= 1 << (g[rr] ^ g[ll] ^ (!fl ? f[ll - 1][c] : calc(l, ll - 1, j, 1)) ^ (fl ? f[r][j] : calc(rr + 1, r, j, 0))); } return mem[c] = mex(st); } int main() { ios::sync_with_stdio(false); cin >> (s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) a[i] = s[i] - a ; for (int i = 1; i <= n; i++) { for (int j = 0; j < 26; j++) pre[i][j] = pre[i - 1][j]; pre[i][a[i]] = i; } for (int j = 0; j < 26; j++) suf[n + 1][j] = n + 1; for (int i = n; i > 0; --i) { for (int j = 0; j < 26; j++) suf[i][j] = suf[i + 1][j]; suf[i][a[i]] = i; } for (int i = 1; i <= n; i++) { g[i] = f[i - 1][a[i]] ^ g[pre[i - 1][a[i]]]; memset(mem, -1, sizeof(mem)); for (int j = 0; j < 26; j++) f[i][j] = calc(pre[i][j] + 1, i, j, 0); } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; memset(mem, -1, sizeof(mem)); if (calc(l, r, 26, 1)) puts( Alice ); else puts( Bob ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; template <class C> inline void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> inline void maxi(C& a4, C b4) { a4 = max(a4, b4); } int n, v, e; int a[307], b[307]; vector<int> GG[307], G[307]; bool del[307], vis[307]; int xx[1000007], yy[1000007], dd[1000007], moves; long long int go(int x) { long long int res = a[x] - b[x]; vis[x] = true; for (__typeof((GG[x]).begin()) i = (GG[x]).begin(); i != (GG[x]).end(); ++i) if (!vis[*i]) { G[x].push_back(*i); G[*i].push_back(x); res += go(*i); } return res; } void push(int x, int o, int lim, bool down) { for (__typeof((G[x]).begin()) i = (G[x]).begin(); i != (G[x]).end(); ++i) if (*i != o && !del[*i]) { push(*i, x, INF, down); int can = lim; int l = x, r = *i; if (!down) swap(l, r); mini(can, a[l]); mini(can, v - a[r]); lim -= can; if (can > 0) { a[l] -= can; a[r] += can; xx[moves] = l; yy[moves] = r; dd[moves] = can; moves++; } } } int main() { std::ios_base::sync_with_stdio(0); cout << setprecision(15) << fixed; cin >> n >> v >> e; for (int i = (1); i <= (n); i++) cin >> a[i]; for (int i = (1); i <= (n); i++) cin >> b[i]; while (e--) { int x, y; cin >> x >> y; GG[x].push_back(y); GG[y].push_back(x); } for (int i = (1); i <= (n); i++) if (!vis[i]) if (go(i) != 0) { cout << NO << endl; return 0; } bool ok; do { ok = false; for (int i = (1); i <= (n); i++) if (!del[i]) { int deg = 0; for (__typeof((G[i]).begin()) j = (G[i]).begin(); j != (G[i]).end(); ++j) deg += !del[*j]; if (deg != 1) continue; if (a[i] != b[i]) push(i, i, abs(a[i] - b[i]), a[i] > b[i]); del[i] = true; ok = true; break; } } while (ok); cout << moves << endl; for (int i = 0; i < (moves); i++) cout << xx[i] << << yy[i] << << dd[i] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } typedef void (*callback_function)(void); const long long ZERO = 0LL; const long long INF64 = 1e18; const long long INF32 = 1e9; const long long MOD = 1e9 + 7; const long double PI = acos(-1.0L); const long double EPS = static_cast<long double>(1e-9); const long long N = 1e6 + 7; long long n, a[N], del; void input() { cin >> n; for (long long i = (1); i <= (n); i++) { cin >> a[i]; } } void solve() { map<long long, long long> mp; for (long long i = (1); i <= (n); i++) { while (mp[a[i]]) { a[mp[a[i]]] = 0; mp[a[i]] = 0; a[i] *= 2; del++; } mp[a[i]] = i; } } void output() { cout << n - del << endl; for (long long i = (1); i <= (n); i++) { if (a[i]) cout << a[i] << ; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(); { input(); solve(); output(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, a[200002], v1, v2, ans[200002], mx, subtr[200002]; vector<long long> g[200002]; void dfs(int v, int d, int p) { ans[1] += a[v] * d; for (int to : g[v]) { if (to == p) continue; dfs(to, d + 1, v); } } void dfs2(int v, int p) { long long curr = a[v]; for (int to : g[v]) { if (to == p) continue; dfs2(to, v); curr += subtr[to]; } subtr[v] = curr; } void dfs3(int v, int p) { for (int to : g[v]) { if (to == p) continue; ans[to] = ans[v] + subtr[1] - 2 * subtr[to]; dfs3(to, v); } mx = max(mx, ans[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n - 1; i++) { cin >> v1 >> v2; g[v1].push_back(v2); g[v2].push_back(v1); } dfs(1, 0, -1); dfs2(1, -1); dfs3(1, -1); cout << mx; return 0; } |
#include <bits/stdc++.h> using namespace std; void read(int& 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; }; char getc() { char c; for (c = getchar(); c <= 32; c = getchar()) ; return c; } const int N = 100010; const int inf = 1000000000; int n, m, d, i, j, k, l, p, x, y, ans; bool have[N]; int edge, e[N * 2], b[N * 2], fir[N], last[N]; void clear() { edge = 0; memset(fir, -1, sizeof(fir)); memset(b, -1, sizeof(b)); } void adde(int x, int y) { e[edge] = y; if (fir[x] == -1) fir[x] = edge; else b[last[x]] = edge; last[x] = edge++; } int st[N], ed[N], ti, fa[N], dep[N], home[N]; void dfs(int i) { st[i] = ++ti; home[ti] = i; int k; for (k = fir[i]; k >= 0; k = b[k]) if (e[k] != fa[i]) { fa[e[k]] = i; dep[e[k]] = dep[i] + 1; dfs(e[k]); } ed[i] = ti; } int key[N * 4], add[N * 4]; void padd(int i, int s) { add[i] += s; key[i] += s; } void pdown(int i) { if (add[i]) padd(i * 2, add[i]), padd(i * 2 + 1, add[i]), add[i] = 0; } void upd(int i) { key[i] = max(key[i * 2], key[i * 2 + 1]); } void build(int i, int x, int y) { if (x != y) { build(i * 2, x, (x + y) / 2); build(i * 2 + 1, (x + y) / 2 + 1, y); upd(i); } else key[i] = (have[home[x]]) ? dep[home[x]] : -inf; } void find(int i, int x, int y, int l, int r, int s) { if (y < l || x > r) return; if (x >= l && y <= r) { padd(i, s); return; } pdown(i); find(i * 2, x, (x + y) / 2, l, r, s); find(i * 2 + 1, (x + y) / 2 + 1, y, l, r, s); upd(i); } void doit(int l, int r, int s) { find(1, 1, n, l, r, s); if (l > 1) find(1, 1, n, 1, l - 1, -s); if (r < n) find(1, 1, n, r + 1, n, -s); } void go(int i) { if (key[1] <= d) ans++; int k; for (k = fir[i]; k >= 0; k = b[k]) if (e[k] != fa[i]) { doit(st[e[k]], ed[e[k]], -1); go(e[k]); doit(st[e[k]], ed[e[k]], 1); } } int main() { read(n); read(m); read(d); for (i = 1; i <= m; i++) read(x), have[x] = 1; clear(); for (i = 1; i <= n - 1; i++) read(x), read(y), adde(x, y), adde(y, x); dfs(1); build(1, 1, n); go(1); printf( %d n , ans); scanf( n ); return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:667772160 ) using namespace std; struct __isoff { __isoff() { if (0) freopen( input.txt , r , stdin), freopen( output.txt , w , stdout); srand( C + T + A + C + Y + M + B + A ); } ~__isoff() {} } __osafwf; const unsigned long long p1 = 31; const unsigned long long p2 = 29; const double eps = 1e-8; const double pi = acos(-1.0); const int infi = 1e9 + 7; const long long inf = 1e18 + 7; const long long dd = 7e5 + 7; const long long mod = 1e9 + 7; const long long mod2 = 1e6 + 3; long long fac[dd], ifac[dd]; long long pows(long long x, int p) { if (p == 0) return 1; if (p % 2) return pows(x, p - 1) * x % mod2; x = pows(x, p / 2); return x * x % mod2; } long long cnk(long long a, long long b) { return fac[a] * ifac[b] % mod2 * ifac[a - b] % mod2; } const int fd = 1e3 + 7; int H[dd], IH[dd], dp[fd]; int main() { fac[0] = ifac[0] = 1; for (int i = 1; i < dd; i++) { fac[i] = fac[i - 1] * i % mod2; ifac[i] = ifac[i - 1] * pows(i, mod2 - 2) % mod2; } int c, h, w; cin >> c >> w >> h; { H[0] = 1; for (int i = 1; i < dd; i++) { H[i] = (H[i - 1] * h) % mod2; } IH[0] = 1; for (int i = 1; i < dd; i++) { IH[i] = (IH[i - 1] * pows(h, mod2 - 2)) % mod2; } } dp[0] = 1; deque<long long> G; G.push_back(1); G.push_back(1); long long t = h + 1; for (int i = 1; i <= c; i++) { if (0) { long long p = 1; for (int j = i - 1, l = 0; j >= -1 && l <= w; j--, l++) { long long t = 1; if (j >= 0) { t = dp[j]; } dp[i] = (dp[i] + t * p) % mod2; p = (p * h) % mod2; } } { G.push_back(t); t = (t * h + t) % mod2; if (G.size() > w + 1) { t = ((t - G.front() * H[w + 1]) % mod2 + mod2) % mod2; G.pop_front(); } } } cout << G.back(); } |
#include <bits/stdc++.h> using namespace std; int expo(int x, int y) { int res = 1; while (y) { if (y % 2) res = (res * x % 1000000007) % 1000000007; x = (x * x) % 1000000007; y /= 2; } return res; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long nextPrime(long long N) { if (N <= 1) return 2; long long prime = N; bool found = false; while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } long long cl(long long n, long long d) { return (n + d - 1) / d; } long long binToDec(string s) { return bitset<64>(s).to_ullong(); } string decToBin(long long a) { return bitset<64>(a).to_string(); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / (gcd(a, b)); } void print(pair<long long, long long> a) { cout << a.first << << a.second; cout << endl; ; } void print(long long a) { cout << a; cout << endl; } void print(std::vector<long long> a) { for (auto i : a) cout << i << ; cout << endl; ; } void print(string s) { cout << s; cout << endl; ; } void printr(std::vector<long long> a, long long start, long long end) { for (long long i = start; i < end; i++) cout << a[i] << ; cout << endl; ; } void print(long long a, long long b) { cout << a << << b; } void amax(long long &a, long long b) { a = max(a, b); } void amin(long long &a, long long b) { a = min(a, b); } long long madd(long long a, long long b) { return ((a % 1000000007) + (b % 1000000007)) % 1000000007; } long long mmul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long msub(long long a, long long b) { return ((a % 1000000007) - (b % 1000000007) + 1000000007) % 1000000007; } long long fpow(long long x, long long y, long long p = 1000000007) { x %= p; long long sum = 1; while (y) { if (y & 1) sum = sum * x; sum %= p; y = y >> 1; x = x * x; x %= p; } return sum; } bool isPerSquare(long double a) { if (a < 0) return false; long long sr = sqrt(a); return (sr * sr == a); } bool isPalindrome(string str) { long long low = 0, high = long long(str.size()) - 1; while (low < high) { if (str[low] != str[high]) return false; low++, high--; } return true; } void solve() { long long n; cin >> n; ; vector<set<long long>> vp(n + 1); for (__typeof(n + 1) i = (1) - ((1) > (n + 1)); i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) { long long x; cin >> x; ; set<long long> temp; for (__typeof(x) i = (0) - ((0) > (x)); i != (x) - ((0) > (x)); i += 1 - 2 * ((0) > (x))) { long long y; cin >> y; ; temp.insert(y); } vp[i] = temp; } vector<vector<long long>> dependent(n + 1); set<long long> books; for (long long i = 1; i <= n; i++) { for (auto j : vp[i]) { dependent[j].push_back(i); } } long long ans = 1; for (long long i = 1; i <= n; i++) { if (long long(vp[i].size()) == 0) { books.insert(i); } } long long rem = 0; while (long long(books.size())) { bool added = false, erased = false; std::vector<long long> add; auto i = *books.begin(); while (1) { if (!added && long long(dependent[i].size()) && dependent[i][0] < i) { added = true; ans++; } for (long long j = 0; j < long long(dependent[i].size()); j++) { vp[dependent[i][j]].erase(i); if (long long(vp[dependent[i][j]].size()) == 0) { books.insert(dependent[i][j]); } } dependent[i].clear(); rem++; books.erase(i); if (rem == n) { print(ans); return; } if (books.upper_bound(i) == books.end()) { break; } else { i = *books.upper_bound(i); } } } print(-1); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; void solve() { long long int n, i, j, ans = 0; cin >> n; long long int a[n]; for (auto &x : a) { cin >> x; }; map<long long int, long long int> cnt; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { cnt[a[i] + a[j]]++; ans = max(ans, cnt[a[i] + a[j]]); } } cout << ans << n ; } signed main() { ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); long long int t = 1; while (t--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, s[3010], c[3010], ans = 1e10; cin >> n; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < n; i++) { long long min1 = 1e10, min2 = 1e10; for (int j = 0; j < i; j++) { if (s[j] < s[i]) min1 = min(min1, c[j]); } for (int j = i; j < n; j++) { if (s[j] > s[i]) min2 = min(min2, c[j]); } ans = min(ans, min1 + c[i] + min2); } if (ans >= 1e10) puts( -1 ); else cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf( %d %d , &n, &m); unordered_map<string, string> d; for (int i = 0; i < m; i++) { string x, y; cin >> x >> y; if ((int)x.size() > (int)y.size()) swap(x, y); d[y] = d[x] = x; } for (int i = 0; i < n; i++) { string x; cin >> x; cout << d[x] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int h; long long n; long long dfs(int d, long long n, int lr) { if (d == h) return 0; long long tmp = 1ll << (h - d - 1); if (!lr && n <= tmp) return dfs(d + 1, n, !lr) + 1; if (!lr && n > tmp) return (tmp << 1) + dfs(d + 1, n - tmp, 0); if (lr && n <= tmp) return (tmp << 1) + dfs(d + 1, n, lr); if (lr && n > tmp) return dfs(d + 1, n - tmp, 0) + 1; } int main() { cin >> h >> n; cout << dfs(0, n, 0) << endl; } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } const int MAXN = 100010; int n, s[MAXN]; int main() { scanf( %d , &n); printf( %d , n / 5 + (n % 5 != 0)); } |
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, i = 0; cin >> n >> s; std::vector<int> v(n); std::vector<bool> cycle(n); for (int i = 0; i < n; i++) cin >> v[i]; for (i = 0; i < n;) { if (i <= -1 || i >= n) break; if (cycle[i] == true) break; else { cycle[i] = true; if (s[i] == < ) i -= v[i]; else i += v[i]; } } if (i <= -1 || i >= n) cout << FINITE n ; else cout << INFINITE n ; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; long long int inf = -1e15; long long int seg[maxn << 2], lazy[maxn << 2]; int n, m, p; vector<long long int> espadas, armaduras; vector<pair<int, int> > monstros[maxn]; void update(int x, long long int val) { seg[x] += val; lazy[x] += val; } void propaga(int x) { if (!lazy[x]) return; update(x * 2, lazy[x]); update(x * 2 + 1, lazy[x]); lazy[x] = 0; } void add(int i, int j, int x, int l, int r, int val) { if (i >= j || l >= r || i >= r || j <= l) return; if (i >= l && j <= r) { update(x, val); return; } propaga(x); int md = (i + j) >> 1; add(i, md, x * 2, l, r, val); add(md, j, x * 2 + 1, l, r, val); seg[x] = max(seg[x * 2], seg[x * 2 + 1]); } void build(int i, int j, int no) { int l = 2 * no; int r = l + 1; if (i + 1 == j) { if (armaduras[i]) seg[no] = -armaduras[i]; else seg[no] = inf; return; } int mid = (i + j) >> 1; build(i, mid, l); build(mid, j, r); seg[no] = max(seg[l], seg[r]); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); long long int ans = inf; espadas.resize(maxn); armaduras.resize(maxn); cin >> n >> m >> p; int atk, def; long long int gold; for (int i = 1; i <= n; i++) { cin >> atk >> gold; atk--; if (!espadas[atk]) espadas[atk] = gold; else espadas[atk] = min(espadas[atk], gold); } for (int i = 1; i <= m; i++) { cin >> def >> gold; if (!armaduras[def]) armaduras[def] = gold; else armaduras[def] = min(armaduras[def], gold); } build(1, maxn, 1); for (int i = 1; i <= p; i++) { cin >> def >> atk >> gold; monstros[def].push_back(pair<int, int>(atk, gold)); } for (int i = 0; i <= maxn - 1; i++) { for (auto a : monstros[i]) add(1, maxn, 1, a.first + 1, maxn, a.second); if (espadas[i]) ans = max(ans, seg[1] - espadas[i]); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int cnt2 = 0; int en = 0; struct Dinic { static const int MXN = 100000; struct Edge { int v, f, re; Edge(int a, int b, int c) : v(a), f(b), re(c) {} }; int n, s, t, level[MXN]; vector<Edge> E[MXN]; void init(int _n, int _s, int _t) { n = _n; s = _s; t = _t; for (int i = 0; i <= n; i++) E[i].clear(); } void add_edge(int u, int v, int f, int flag = 0) { en += 2; if (flag) { E[u].push_back(Edge(v, f, E[v].size())); E[v].push_back(Edge(u, f, E[u].size() - 1)); } else { E[u].push_back(Edge(v, f, E[v].size())); E[v].push_back(Edge(u, 0, E[u].size() - 1)); } } bool BFS() { memset(level, -1, sizeof(level)); queue<int> que; que.push(s); level[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (auto &it : E[u]) { cnt2++; if (it.f > 0 && level[it.v] == -1) { level[it.v] = level[u] + 1; que.push(it.v); } } } return level[t] != -1; } int DFS(int u, int nf) { if (u == t) return nf; int res = 0; for (auto &it : E[u]) { cnt2++; if (it.f > 0 && level[it.v] == level[u] + 1) { int tf = DFS(it.v, min(nf, it.f)); res += tf; nf -= tf; it.f -= tf; E[it.v][it.re].f += tf; if (nf == 0) return res; } } if (!res) level[u] = -1; return res; } int flow(int res = 0) { while (BFS()) { res += DFS(s, 2147483647); } return res; } } flow; int vis[100005]; bool dfs(int first) { if (vis[first]) return false; if (first == 0) return true; vis[first] = 1; for (auto &it : flow.E[first]) { cnt2++; if (it.f && dfs(it.v)) { it.f--; flow.E[it.v][it.re].f++; return true; } } return false; } bool dfs2(int first) { if (vis[first]) return false; if (first == flow.t) return true; vis[first] = 1; for (auto &it : flow.E[first]) { cnt2++; if (it.f && dfs2(it.v)) { it.f--; flow.E[it.v][it.re].f++; return true; } } return false; } void solve() { int n; scanf( %d , &n); int a[205][205]; flow.init(n * n * 2 + 2, 0, n * n * 2 + 1); int X[4] = {0, 0, -1, 1}; int Y[4] = {1, -1, 0, 0}; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf( %d , &a[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (a[i][j] != -1) { flow.add_edge(i * n + j + 1, i * n + j + n * n + 1, 1000000, 1); for (int k = 0; k < 4; k++) { int first = X[k] + i, second = Y[k] + j; if (first >= 0 && first < n && second >= 0 && second < n && a[first][second] != -1 && (a[i][j] == 0 || a[first][second] == 0)) { flow.add_edge(i * n + j + 1, first * n + second + n * n + 1, 1); } } } } } vector<pair<int, pair<int, int> > > v; for (int i = 1; i < n - 1; i++) { v.push_back(make_pair(a[0][i], make_pair(0, i))); flow.add_edge(n * n + i + 1, n * n * 2 + 1, 1); v.push_back(make_pair(a[n - 1][i], make_pair(n - 1, i))); flow.add_edge(n * n + i + (n - 1) * n + 1, n * n * 2 + 1, 1); v.push_back(make_pair(a[i][0], make_pair(i, 0))); flow.add_edge(n * n + i * n + 1, n * n * 2 + 1, 1); v.push_back(make_pair(a[i][n - 1], make_pair(i, n - 1))); flow.add_edge(n * n + i * n + n - 1 + 1, n * n * 2 + 1, 1); } long long ans = 0; for (int i = 0; i < n - 1; i++) { ans += abs(a[0][i + 1] - a[0][i]); ans += abs(a[i][0] - a[i + 1][0]); ans += abs(a[n - 1][i] - a[n - 1][i + 1]); ans += abs(a[i][n - 1] - a[i + 1][n - 1]); } sort(v.begin(), v.end()); long long now = 0; long long last = 0; int tot = 0; int cnt = 0; for (auto it : v) { fill(vis, vis + 100005, 0); while (dfs2(0)) { now++; fill(vis, vis + 100005, 0); } ans += now * (it.first - last); last = it.first; int first = it.second.first * n + it.second.second + 1; flow.add_edge(0, first, 1); for (auto &it : flow.E[first + n * n]) { if (it.v == n * n * 2 + 1) { if (it.f == 1) { it.f = 0; } else { flow.E[it.v][it.re].f = 0; now--; fill(vis, vis + 100005, 0); dfs(first + n * n); } } } } printf( %lld n , ans); } int main() { int t = 1; while (t--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int n, m, dp[30010][210][4], num[30010]; int main() { int i, j; cin >> n >> m; for (i = 1; i <= n; i++) scanf( %d , &num[i]); memset(dp[0], -0x3f, sizeof(dp[0])); for (i = 0; i < 4; i++) dp[0][0][i] = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { int tmp = 1 + (j != 1 && j != m); dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j - 1][3]) - tmp * num[i]; dp[i][j][1] = max(dp[i - 1][j][1], dp[i][j][0]); dp[i][j][2] = max(dp[i - 1][j][2], dp[i - 1][j - 1][1]) + tmp * num[i]; dp[i][j][3] = max(dp[i - 1][j][3], dp[i][j][2]); if (tmp == 2) { dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - 1][1]); dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j - 1][3]); } } } cout << max(max(dp[n][m][0], dp[n][m][1]), max(dp[n][m][2], dp[n][m][3])); } |
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e7; const long long maxm = 1e7; const long long INF = 0x3f3f3f3f; const long long mod = 1e9 + 7; long long uu[maxn]; int main() { long long n, a, b; cin >> n >> a >> b; for (long long i = 0; i < n; i++) scanf( %lld , &uu[i]); long long ans = -1; long long mxb = b; for (long long i = 0; i < n; i++) { if (a == 0 && b == 0) { ans = i; break; } if (uu[i] == 0) { if (b > 0) b--; else a--; } if (uu[i] == 1) { if (b == mxb) { b--; } else if (a > 0) { a--; b++; if (b > mxb) b = mxb; } else b--; } } if (ans == -1) ans = n; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e8; int n, m; char s[22][22]; int x[22][22], dp[1 << 20], state[30], sum[30], maxN[30]; vector<int> sta[22]; vector<int> cost[22]; int bit[1 << 20]; void initialIt() { for (int i = 0; i < 20; i++) { bit[1 << i] = i; } for (int i = 0; i < m; i++) { memset(state, 0, sizeof(state)); memset(sum, 0, sizeof(sum)); memset(maxN, 0, sizeof(maxN)); for (int j = 0; j < n; j++) { int ch = (int)s[j][i] - a ; state[ch] |= 1 << j; sum[ch] += x[j][i]; maxN[ch] = max(maxN[ch], x[j][i]); } for (int j = 0; j < 26; j++) { for (int k = 0; k < n; k++) if ((state[j] >> k) & 1) { sta[k].push_back(state[j]); cost[k].push_back(sum[j] - maxN[j]); } } } for (int i = 0; i < n; i++) { int minCost = INF; for (int j = 0; j < m; j++) { minCost = min(minCost, x[i][j]); } sta[i].push_back(1 << i); cost[i].push_back(minCost); } } int lowBit(int x) { return x & (-x); } void calcIt() { for (int i = 0; i < (1 << n); i++) { dp[i] = INF; } dp[0] = 0; for (int i = 0; i < (1 << n); i++) { int curI = bit[lowBit(i)], curS = i; for (int j = 0; j < (int)cost[curI].size(); j++) { int C = cost[curI][j], S = sta[curI][j]; int preS = curS - (curS & S); if (dp[preS] + C < dp[curS]) { dp[curS] = dp[preS] + C; } } } int final = (1 << n) - 1; printf( %d n , dp[final]); } int main() { scanf( %d%d , &n, &m); for (int i = 0; i < n; i++) { scanf( %s , s[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf( %d , &x[i][j]); } } initialIt(); calcIt(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, n; char s[200050]; map<int, map<int, int>> vis, cnt; scanf( %d , &t); while (t--) { vis.clear(); cnt.clear(); int l = -5, r = 0x3f3f3f3f; scanf( %d%s , &n, s); int x = 0, y = 0; vis[x][y] = 1; cnt[x][y] = 0; for (int i = 0; i < n; i++) { if (s[i] == L ) { x -= 1; } else if (s[i] == R ) { x += 1; } else if (s[i] == D ) { y -= 1; } else { y += 1; } if (vis[x][y] && i - cnt[x][y] < r - l) { l = cnt[x][y] + 1; r = i + 1; } cnt[x][y] = i + 1; vis[x][y] = 1; } if (l != -5) printf( %d %d n , l, r); else printf( -1 n ); } } |
#include <bits/stdc++.h> using namespace std; long sz(char *a) { long i = 0; while (*(a + i) != 0 ) i++; return i; } int main() { char *a = (char *)malloc(sizeof(char) * 1000001); char *b = (char *)malloc(sizeof(char) * 1000001); scanf( %1000000s , a); scanf( %1000000s , b); long a_size = sz(a); long b_size = sz(b); long j, i, a_adj, b_adj; for (i = 0; i < a_size; i++) if (a[i] != 0 ) break; a_size -= i; a_adj = i; i = 0; for (i = 0; i < b_size; i++) if (b[i] != 0 ) break; b_size -= i; b_adj = i; if (a_size > b_size) { printf( > n ); return 0; ; } if (b_size > a_size) { printf( < n ); return 0; ; } for (i = a_adj, j = b_adj; i <= a_size + a_adj, j <= b_size + b_adj; i++, j++) { if (a[i] > b[j]) { printf( > n ); return 0; ; } if (a[i] < b[j]) { printf( < n ); return 0; ; } } printf( = n ); free(a); free(b); return 0; ; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000000; int aa[N + 1]; int main() { int n; cin >> n; long long ans = 0; for (int i = 0; i <= n; i++) aa[i] = i; int n_ = n; for (int h = 20; n_ > 0;) { while (h >= 0 && (n_ & 1 << h) == 0) h--; int m = n_ - ((1 << h) - 1); reverse(aa + n_ - m * 2 + 1, aa + n_ + 1); n_ -= m * 2; ans += m * 2LL * ((1 << h + 1) - 1); } cout << ans << n ; for (int i = 0; i <= n; i++) cout << aa[i] << ; cout << n ; return 0; } |
#include <bits/stdc++.h> int main() { using namespace std; int q; cin >> q; while (q--) { long long n; cin >> n; int len = 0; char num[40] = {0}; while (n) { num[len] = n % 3 + 0 ; n /= 3; len++; } int x; for (x = len - 1; x >= 0; x--) { if (num[x] == 2 ) { break; } } if (x >= 0) { for (int i = 0; i <= x; i++) { num[i] = 0 ; } if (num[x + 1]) { num[x + 1]++; } else { num[x + 1] = 1 ; } for (int i = x + 1; i < len; i++) { if (num[i] == 2 ) { num[i] = 0 ; if (num[i + 1]) { num[i + 1]++; } else { num[i + 1] = 1 ; } } } } long long m = 0, t = 1; for (int i = 0; num[i]; i++) { m += t * (num[i] - 0 ); t *= 3; } cout << m << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { char a[5][5]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) scanf( %c , &a[i][j]); getchar(); } int flag = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (a[i][j] != a[2 - i][2 - j]) flag = 1; } if (flag == 0) printf( YES n ); else printf( NO n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; int n; int a[maxn], f[maxn]; long long b[maxn]; int main() { int i, ans = 0, j; long long w; scanf( %d , &n); for (i = 1; i <= n; ++i) { scanf( %I64d , &w); while (w % 2 == 0) ++a[i], w /= 2; b[i] = w; } for (i = 1; i <= n; ++i) { for (j = 1; j < i; ++j) if ((a[i] == a[j] + i - j || a[i] < i - j) && b[j] % b[i] == 0) f[i] = max(f[i], f[j]); ans = max(ans, ++f[i]); } printf( %d , n - ans); return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pi = pair<int, int>; using pl = pair<ll, ll>; using pld = pair<ld, ld>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using vld = vector<ld>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpld = vector<pld>; int clz(unsigned long long N) { return N ? 63 - __builtin_clzll(N) : -1; } int arr[300010]; int acum[300010]; int n, q; void solvetask() { cin >> n >> q; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == + ) arr[i] = (i & 1) ? -1 : 1; else arr[i] = (i & 1) ? 1 : -1; } for (int i = 1; i <= n; i++) acum[i] = acum[i - 1] + arr[i - 1]; while (q--) { int l, r; cin >> l >> r; int soma = acum[r] - acum[l - 1]; if (soma == 0) cout << 0 n ; else if (abs(soma) % 2 == 1) cout << 1 n ; else cout << 2 n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; while (t--) solvetask(); } |
#include <bits/stdc++.h> using namespace std; using namespace std; bool debug = 1; int main() { int n, m; scanf( %d %d , &n, &m); int amn[n + 1]; vector<vector<int> > v; v.resize(n + 1); for (int i = 0; i < m; i++) { int a, b; scanf( %d %d , &a, &b); v[a].push_back(b); v[b].push_back(a); amn[a]++; amn[b]++; } bool fs[n + 1]; fill(fs, fs + n + 1, 0); if (0) for (int i = 1; i <= n; i++) if (v[i].size() == 1) fs[i] = 1; int k = 0; bool is; do { bool nfs[n + 1]; for (int i = 1; i <= n; i++) nfs[i] = fs[i]; is = 0; k++; for (int i = 1; i <= n; i++) { if (fs[i]) continue; int amnoth = 0; for (int j = 0; j < v[i].size(); j++) { int oth = v[i][j]; if (!fs[oth]) amnoth++; } if (amnoth == 1) { nfs[i] = 1; is = 1; } } for (int i = 1; i <= n; i++) fs[i] = nfs[i]; } while (is); printf( %d n , k - 1); return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; const int MAXN = 200010; vector<int> x[MAXN], y[MAXN]; int v[MAXN], out[MAXN], sg[MAXN]; int xr[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> v[i]; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; x[a].push_back(b); y[b].push_back(a); ++out[a]; } queue<int> Q; for (int i = 0; i < n; ++i) { if (out[i] == 0) Q.push(i); } int maxsg = 0; while (!Q.empty()) { int a = Q.front(); Q.pop(); for (int b : y[a]) { --out[b]; if (out[b] == 0) { Q.push(b); } } set<int> s; for (int b : x[a]) s.insert(sg[b]); for (int i = 0; i < n; ++i) { if (s.find(i) == s.end()) { sg[a] = i; break; } } maxsg = max(maxsg, sg[a]); } for (int i = 0; i < n; ++i) { xr[sg[i]] ^= v[i]; } bool win = false; for (int i = maxsg; i >= 0; --i) { if (xr[i] != 0) { win = true; int p = -1; for (int j = 0; j < n; ++j) { if (sg[j] != i) continue; if (p == -1) { p = j; continue; } if ((v[j] ^ xr[i]) <= v[j]) p = j; } xr[i] ^= v[p]; v[p] = xr[i]; xr[i] = 0; for (int b : x[p]) { if (xr[sg[b]] != 0) { v[b] ^= xr[sg[b]]; xr[sg[b]] = 0; } } break; } } if (win) { cout << WIN n ; for (int i = 0; i < n; ++i) cout << v[i] << ; cout << n ; } else { cout << LOSE n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int N, i, sum = 0, max; cin >> N; int A[N]; for (i = 0; i <= N - 1; i++) cin >> A[i]; max = *max_element(A, A + N); for (i = 0; i <= N - 1; i++) sum += (max - A[i]); cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; mt19937 rnd(339264); const long long MOD = 998244353; const double eps = 1e-7; int main() { cout << fixed << setprecision(20); int n, w, m; cin >> n >> w >> m; if (2 * n < m) { cout << NO ; return 0; } int left = m; int j = 0; vector<vector<pair<int, int>>> ans(m); for (int i = 0; i < m; i++) { int need = n; if (left < m && left > n) { cout << NO ; return 0; } while (need >= left) { ans[i].push_back({j, left}); need -= left; left = m; j++; } if (need > 0) { ans[i].push_back({j, need}); left -= need; } } cout << YES << endl; for (int i = 0; i < m; i++) { for (auto &p : ans[i]) { cout << p.first + 1 << << (p.second * w) / ((double)m) << ; } cout << endl; } 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 ( 0 <= ch && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } struct node { int v, w; node() {} node(int x, int y) : v(x), w(y) {} bool operator<(const node& tl) const { return (v - w) < (tl.v - tl.w); } } z[105], f[105]; bool cmp(node aa, node bb) { return aa.v < bb.v; } int dp[30005 * 2] = {0}; int main() { int n, m, tot1, tot2; cin >> n >> m; tot1 = tot2 = 1; for (int i = 1; i < n + 1; ++i) { int x, y; cin >> x >> y; if (y < 0) f[tot2++] = node(x, -y); else z[tot1++] = node(x, y); } sort(z + 1, z + tot1, cmp); int ans = 0; for (int i = 1; i < tot1; ++i) { if (z[i].v <= m) { m += z[i].w; ++ans; } } sort(f + 1, f + tot2); int sum = 0; for (int i = 1; i < tot2; ++i) { for (int j = m; j >= max(f[i].v, f[i].w); --j) { dp[j] = max(dp[j], dp[j - f[i].w] + 1); } } cout << ans + dp[m] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int test, n, mx, i; double l, r, m, eps = 0.000001, a[100005]; double pref(double tm) { double pos = 0, speed = 1; for (int i = 0; i < n; i++) { if ((a[i] - pos) <= tm * speed) tm -= (a[i] - pos) / speed, pos = a[i], speed += 1; else { pos += speed * tm; tm = 0; break; } } pos += speed * tm; return pos; } double suff(double tm) { double pos = mx, speed = 1; for (int i = n - 1; i >= 0; i--) { if ((pos - a[i]) <= tm * speed) tm -= (pos - a[i]) / speed, pos = a[i], speed += 1; else { pos -= speed * tm; tm = 0; break; } } pos -= speed * tm; return pos; } int main() { scanf( %d , &test); while (test--) { cin >> n >> mx; for (i = 0; i < n; i++) cin >> a[i]; l = 0., r = 2000000000.; while (r - l > eps) { m = (l + r) / 2.; if (pref(m) <= suff(m)) l = m; else r = m - eps; } cout << fixed << setprecision(9) << l << n ; } } |
#include <bits/stdc++.h> using namespace std; long long a, b, s, c, ans, t; int main() { cin >> t; while (t--) { cin >> s >> a >> b >> c; ans = ((s / (a * c)) * (a + b)) + (s % (a * c)) / c; cout << ans << endl; } } |
#include <bits/stdc++.h> char str[260][260]; int main(void) { int t, n, m; scanf( %d , &t); while (t--) { int ans = 0; scanf( %d%d , &n, &m); getchar(); for (int i = 0; i < n; i++) gets(str[i]); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (str[i][j] == 1 ) { if (str[i][j + 1] == 1 ) { if (i >= 1 && str[i - 1][j] == 1 ) continue; if (j >= 1 && str[i][j - 1] == 1 ) continue; if (i >= 1 && j >= 1 && str[i - 1][j - 1] == 1 ) continue; for (int k = 1; k + i < n && k + j < m; k++) { if (str[i + k][j] == 0 ) break; if (str[i][j + k] == 0 ) break; if (j >= 1 && str[i + k][j - 1] == 1 ) break; if (i >= 1 && str[i - 1][j + k] == 1 ) break; if (str[i + k][j + k] == 0 ) continue; if (j + k + 1 < m && str[i + k][j + k + 1] == 1 ) continue; if (i + k + 1 < n && str[i + k + 1][j + k] == 1 ) continue; if (i + k + 1 < n && j + k + 1 < m && str[i + k + 1][j + k + 1] == 1 ) continue; if (i + k + 1 < n && str[i + k + 1][j] == 1 ) continue; if (j + k + 1 < m && str[i][j + k + 1] == 1 ) continue; if (i + k + 1 < n && j >= 1 && str[i + k + 1][j - 1] == 1 ) continue; if (j + k + 1 < m && i >= 1 && str[i - 1][j + k + 1] == 1 ) continue; int flag = 0; for (int p = 1; p < k && flag == 0; p++) { if (str[i + k][j + p] == 0 ) flag = 1; if (str[i + p][j + k] == 0 ) flag = 1; if (j + k + 1 < m && str[i + p][j + k + 1] == 1 ) flag = 1; if (i + k + 1 < n && str[i + k + 1][j + p] == 1 ) flag = 1; if (str[i + k - 1][j + p] == 1 ) flag = 1; if (str[i + p][j + k - 1] == 1 ) flag = 1; if (str[i + 1][j + p] == 1 ) flag = 1; if (str[i + p][j + 1] == 1 ) flag = 1; } if (!flag) { ans++; break; } } } else { int flag = 1; for (int k = 2; k + i < n; k += 2) { if (j + k / 2 >= m || j - k / 2 < 0) break; if (str[i + k][j] == 1 ) { flag = 1; if (i >= 1 && str[i - 1][j] == 1 ) break; if (j >= 1 && str[i][j - 1] == 1 ) break; if (j < m && str[i][j + 1] == 1 ) break; if (i >= 1 && j + 1 < m && str[i - 1][j + 1] == 1 ) break; if (i >= 1 && j >= 1 && str[i - 1][j - 1] == 1 ) break; if (str[i + 1][j] == 1 ) break; if (str[i + k - 1][j] == 1 ) continue; if (i + k + 1 < n && str[i + k + 1][j] == 1 ) continue; if (j >= 1 && str[i + k][j - 1] == 1 ) continue; if (j + 1 < m && str[i + k][j + 1] == 1 ) continue; if (i + k + 1 < n && j >= 1 && str[i + k + 1][j - 1] == 1 ) continue; if (i + k + 1 < n && j + 1 < m && str[i + k + 1][j + 1] == 1 ) continue; if (j - k / 2 - 1 >= 0 && str[i + k / 2][j - k / 2 - 1] == 1 ) continue; if (str[i + k / 2][j - k / 2 + 1] == 1 ) continue; if (str[i + k / 2 - 1][j - k / 2] == 1 ) continue; if (str[i + k / 2 + 1][j - k / 2] == 1 ) continue; if (j >= 1 && str[i + k / 2 - 1][j - k / 2 - 1] == 1 ) continue; if (j >= 1 && str[i + k / 2 + 1][j - k / 2 - 1] == 1 ) continue; if (str[i + k / 2 - 1][j + k / 2] == 1 ) continue; if (str[i + k / 2 + 1][j + k / 2] == 1 ) continue; if (str[i + k / 2][j + k / 2 - 1] == 1 ) continue; if (j + k / 2 + 1 < m && str[i + k / 2][j + k / 2 + 1] == 1 ) continue; if (j + k / 2 + 1 < m && str[i + k / 2 - 1][j + k / 2 + 1] == 1 ) continue; if (j + k / 2 + 1 < m && str[i + k / 2 + 1][j + k / 2 + 1] == 1 ) continue; flag = 0; for (int p = 1; p <= k / 2 && flag == 0; p++) { if (str[i + p][j + p] == 0 || str[i + p + 1][j + p] == 1 || str[i + p - 1][j + p] == 1 ) flag = 2; if (str[i + p][j - p] == 0 || str[i + p + 1][j - p] == 1 || str[i + p - 1][j - p] == 1 ) flag = 2; if (str[i + k - p][j + p] == 0 || str[i + k - p + 1][j + p] == 1 || str[i + k - p - 1][j + p] == 1 ) flag = 1; if (str[i + k - p][j - p] == 0 || str[i + k - p + 1][j - p] == 1 || str[i + k - p - 1][j - p] == 1 ) flag = 1; } if (flag == 2) break; if (flag == 1) continue; for (int p = 1; p < k / 2 && flag == 0; p++) { if (str[i + p - 1][j + p + 1] == 1 ) flag = 2; if (str[i + p + 1][j + p - 1] == 1 ) flag = 2; if (str[i + k - p - 1][j - p + 1] == 1 ) flag = 1; if (str[i + k - p + 1][j - p - 1] == 1 ) flag = 1; if (str[i + p - 1][j - p - 1] == 1 ) flag = 2; if (str[i + p + 1][j - p + 1] == 1 ) flag = 2; if (str[i + k - p - 1][j + p - 1] == 1 ) flag = 1; if (str[i + k - p + 1][j + p + 1] == 1 ) flag = 1; } if (flag == 2) break; if (flag == 1) continue; } if (!flag) { ans++; break; } } } } } } printf( %d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 77; int T, n, L, R, par[N], fa[N][20]; int dep[N], son[N], tid[N], sz[N]; pair<int, int> mx[N][3]; vector<int> G[N]; void dfs1(int v, int fa) { mx[v][0] = {0, v}, mx[v][1] = mx[v][2] = {-1, 0}; for (int u : G[v]) if (u ^ fa) { dfs1(u, v); pair<int, int> p = {mx[u][0].first + 1, u}; if (p > mx[v][0]) swap(p, mx[v][0]); if (p > mx[v][1]) swap(p, mx[v][1]); if (p > mx[v][2]) swap(p, mx[v][2]); } }; int main() { scanf( %d , &T); while (T--) { scanf( %d %d %d , &n, &L, &R); for (int i = 1; i <= n; i++) { G[i].clear(); } for (int i = 1, u, v; i < n; i++) { scanf( %d %d , &u, &v); G[u].push_back(v), G[v].push_back(u); } dfs1(1, 0); function<void(int, int)> dfs2 = [&](int v, int fa) { for (int u : G[v]) if (u ^ fa) { par[u] = max(par[v], mx[v][0].second == u ? mx[v][1].first : mx[v][0].first) + 1, dfs2(u, v); } }; memset(par, 0, sizeof(par)), dfs2(1, 0); auto find = [&](int u, int v) { static int dist[N]; fill(dist + 1, dist + n + 1, -1); queue<int> q; q.push(u), dist[u] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int y : G[x]) if (!~dist[y]) { q.push(y), dist[y] = dist[x] + 1; } } return dist[v]; }; int rt = -1, len = find(L, R); for (int i = 1; i <= n; i++) { int cnt = 0; if (mx[i][0].first >= len) cnt++; if (mx[i][1].first >= len) cnt++; if (mx[i][2].first >= len) cnt++; if (par[i] >= len) cnt++; if (cnt >= 3) { rt = i; break; } } if (!~rt) { printf( NO n ); continue; } int tim = 0; function<void(int)> dfs3 = [&](int v) { son[v] = v, tid[v] = ++tim, sz[v] = 1; for (int i = 1; i < 20; i++) { fa[v][i] = fa[fa[v][i - 1]][i - 1]; } for (int u : G[v]) if (u ^ fa[v][0]) { fa[u][0] = v, dep[u] = dep[v] + 1; dfs3(u), sz[v] += sz[u]; if (dep[son[u]] > dep[son[v]]) son[v] = son[u]; } }; fa[rt][0] = rt, dep[rt] = 0, dfs3(rt); bool flag = 0; for (int t = 1; t <= n; t++) { auto chk = [&](int u, int v) { return tid[v] >= tid[u] && tid[v] < tid[u] + sz[u]; }; if (chk(L, R) || chk(R, L)) { flag = 1; break; } auto get = [&](int v, int k) { for (int i = 19; ~i; i--) { if (k >> i & 1) v = fa[v][i]; } return v; }; int num = dep[son[L]] - dep[L]; L = son[L], R = get(R, num), swap(L, R); } printf( %s n , flag ? YES : NO ); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> void maximize(T &a, T b) { if (a < b) a = b; } const int maxN = 305; int n, a[maxN][maxN], f[2][maxN][maxN]; void solve() { for (int i = 1; i <= n; ++i) fill(f[0][i] + 1, f[0][i] + n + 1, INT_MIN); f[0][1][1] = a[1][1]; for (int d = 2; d < n + n; ++d) { for (int i = 1; i <= n; ++i) fill(f[(d + 1) & 1][i] + 1, f[(d + 1) & 1][i] + n + 1, INT_MIN); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (f[d & 1][i][j] > INT_MIN) for (int ni = i; ni < i + 2; ++ni) if (ni <= n && d - ni + 1 <= n) for (int nj = j; nj < j + 2; ++nj) if (nj <= n && d - nj + 1 <= n) { int t = f[d & 1][i][j] + a[ni][d - ni + 1]; if (ni != nj) t += a[nj][d - nj + 1]; maximize(f[(d + 1) & 1][ni][nj], t); } } } int main() { ios_base::sync_with_stdio(false); scanf( %d , &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]); solve(); printf( %d n , f[0][n][n]); return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pi = pair<int, int>; void setIO(string name = ) { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { setIO(); int a, b, c, d; cin >> a >> b >> c >> d; int pv = max(3 * a / 10, a - a / 250 * c); int pm = max(3 * b / 10, b - b / 250 * d); if (pv == pm) { cout << Tie ; } else if (pv > pm) { cout << Misha ; } else { cout << Vasya ; } return 0; } |
#include <bits/stdc++.h> double ans, f[10][1000]; int n, k, a, v[10], p[10], g[10], ki[10], l[10]; void calc() { int b = a; for (int i = 1; i <= n; ++i) { l[i] = p[i] + g[i]; if (l[i] > 100) l[i] = 100; } f[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j <= (1 << i - 1) - 1; ++j) { f[i][j] = f[i - 1][j] * (100 - l[i]) / 100; f[i][j + (1 << i - 1)] = f[i - 1][j] * l[i] / 100; } double res = 0; for (int i = 0; i <= (1 << n) - 1; ++i) { int tot = 0, b = a; for (int j = 1; j <= n; ++j) if (i & (1 << j - 1)) ++tot; else b += v[j]; if (tot * 2 > n) res += f[n][i]; else res += f[n][i] * a / b; } if (res > ans) ans = res; } void dfs(int now, int left) { if (now == n + 1) { calc(); return; } if (left == 0) dfs(now + 1, 0); else { for (int i = 0; i <= left; ++i) { if (p[now] + i * 10 >= 110) break; g[now] = i * 10; dfs(now + 1, left - i); g[now] = 0; } } } int main() { scanf( %d%d%d , &n, &k, &a); for (int i = 1; i <= n; ++i) scanf( %d%d , &v[i], &p[i]); dfs(1, k); printf( %.6lf n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > vec; void update() { int x; scanf( %d , &x); vector<pair<int, int> > a, b; a.push_back({1000010, 0}); b.push_back({1000010, 0}); a.push_back({0, 0}); b.push_back({0, 0}); int sz = vec.size(); for (int i = 0; i + 1 < sz; i++) { int p = x - vec[i].first; if (p > 0) a.push_back({p, vec[i + 1].second}); if (p < 0) b.push_back({-p, vec[i].second}); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vec.clear(); int i = 0, j = 0; int as = a.size(), bs = b.size(); while (i < as || j < bs) { if (a[i].first == b[j].first) { vec.push_back({a[i].first, a[i].second + b[j].second}); i++, j++; } else if (a[i].first < b[j].first) { vec.push_back({a[i].first, a[i].second + b[j].second}); i++; } else { vec.push_back({b[j].first, a[i].second + b[j].second}); j++; } } vec[0].second = 0; return; } void qry() { int s, e, ans = 0; scanf( %d %d , &s, &e); int sz = vec.size(); for (auto cur : vec) { if (e > cur.first) { if (cur.first > s) { ans += cur.second * (cur.first - s); s = cur.first; } } else { ans += cur.second * (e - s); break; } } printf( %d n , ans); return; } int main() { int n, q; scanf( %d %d , &n, &q); vec.push_back({0, 0}); vec.push_back({n, 1}); vec.push_back({1000010, 0}); while (q--) { int type; scanf( %d , &type); if (type == 1) update(); else qry(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int in, x = 0; int cnt = 0, p = 1, ans = 0; cin >> in; for (int i = in; i > 0; i /= 10) { if (i % 10 == 7) ans += p; p *= 2; cnt++; } if (cnt != 0) x = pow(2, cnt) - 2; cout << ans + x + 1; } |
#include <bits/stdc++.h> using namespace std; int a[100 + 5]; int b[100 + 5]; int main() { int m, n; int l, r; while (scanf( %d%d , &m, &n) != EOF) { int sum = 0; for (int i = 1; i <= m; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> l >> r; int sum1 = 0; for (int i = l; i <= r; i++) { sum1 += a[i]; } if (sum1 > 0) sum += sum1; } cout << sum << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int xx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int yy[] = {1, -1, 0, 0, 1, -1, -1, 1}; double PI = 3.1415926535897932384626433832795; const long long oo = (long long)1e9 + 1; const double eps = 1e-9; const long long mod = 1000000009; string a, b; bool good(int l, string s) { for (int i = 0; i < ((long long)s.size()); i++) { if (a[i % l] != s[i]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin >> a >> b; int res = 0; for (int i = 1; i <= min(((long long)a.size()), ((long long)b.size())); i++) { if (((long long)a.size()) % i == 0 && ((long long)b.size()) % i == 0 && good(i, a) && good(i, b)) res++; } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; string t; long long cnt1[3000001]; int main() { ios_base::sync_with_stdio(0); cin >> t; cnt1[0] = 0; int n = (int)t.length(); for (int i = 0; i <= n - 4; ++i) { if (t.substr(i, 5) == heavy ) ++cnt1[i + 1]; cnt1[i + 1] += cnt1[i]; } long long ans = 0; for (int i = 0; i <= n - 4; ++i) if (t.substr(i, 5) == metal ) ans += cnt1[i]; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; class wwj { public: wwj(int x); int klz(); int pdly(int k); private: int x; }; wwj::wwj(int x) { this->x = x; } int wwj::klz() { int i, sum = 0; double SM = 0; if (x == 0) return 0; SM = (-1 + sqrt(1.0 + 8 * abs(x))) / 2; if (int(SM) == SM) i = int(SM); else i = int(SM + 1); while (1) { if (pdly(i)) return i; else ++i; } } int wwj::pdly(int k) { int s; s = k * (1 + k) / 2; x = abs(x); if (x % 2 == s % 2) return 1; return 0; } int main() { int x; cin >> x; wwj LR(x); cout << LR.klz() << endl; ; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long power(long long x, long long y, long long m) { if (y == 0) return 1; long long p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long modInverse(long long a, long long m) { long long g = gcd(a, m); if (g != 1) cout << Inverse doesn t exist ; else { return power(a, m - 2, m); } } long long log_a_b(long long a, long long b) { return (a > b - 1) ? 1 + log_a_b(a / b, b) : 0; } int t; vector<vector<long long> > graph; vector<long long> tin, tout; vector<bool> vis; vector<vector<long long> > up; void dfs(int v, int p, int l) { vis[v] = true; tin[v] = ++t; up[v][0] = p; for (int(i) = (1); (i) < (l + 1); (i)++) { up[v][i] = up[v][i - 1] < 0 ? -1 : up[up[v][i - 1]][i - 1]; } for (int x : graph[v]) if (!vis[x]) dfs(x, v, l); tout[v] = ++t; } bool isan(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } int lca(int u, int v, int l) { if (isan(u, v)) return u; if (isan(v, u)) return v; for (int(i) = (l); (i) >= (0); (i)--) { if (up[u][i] == -1) continue; if (!isan(up[u][i], v)) u = up[u][i]; } return up[u][0]; } bool sum(long long n, long long i, long long s, long long cnt, vector<long long> &a) { if (i >= n && cnt > 0) { if (s != 0) return 0; else return 1; } if (i >= n && cnt == 0) return 0; bool a1 = sum(n, i + 1, s, cnt, a); bool b = sum(n, i + 1, s - a[i], cnt + 1, a); bool c = sum(n, i + 1, s + a[i], cnt + 1, a); bool d = (a1 | b | c); return d; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.precision(15); long long T; cin >> T; while (T--) { long long n, q, k, m, u, v, w, x; string s, t; cin >> n >> m; long long ans = 0; m++; vector<long long> a(31), b(31); for (int(i) = (0); (i) < (31); (i)++) { a[i] = n % 2; b[i] = m % 2; n /= 2; m /= 2; } reverse((a).begin(), (a).end()); reverse((b).begin(), (b).end()); for (int(i) = (0); (i) < (31); (i)++) { if (a[i] && !b[i]) break; if (!a[i] && b[i]) ans = ans | (1 << (30 - i)); } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; inline int two(int n) { return 1 << n; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } 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); } int dr[] = {-1, 0, 0, 1}, dc[] = {0, -1, 1, 0}; const int md = 1000000007; int dcmp(double a, double b) { return fabs(a - b) <= 1e-9 ? 0 : (a > b) ? 1 : -1; } int arr[1009], tmp[1009] = {0}; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, r, sum = 0; cin >> n >> r; for (int i = 1; i <= n; ++i) { cin >> arr[i]; sum += arr[i]; if (arr[i] == 1) { for (int j = i - r + 1; j <= i + r - 1; ++j) { if (j >= 1 && j <= n) tmp[j]++; } } } int cnt = 0; for (int i = 1; i <= n; ++i) { if (tmp[i] < 1) return cout << -1, 0; } for (int i = 1; i <= n; ++i) { bool ok = false; if (arr[i] == 1) { for (int j = i - r + 1; j <= i + r - 1; ++j) { if (j >= 1 && j <= n) if (tmp[j] == 1) { ok = true; break; } } if (!ok) { cnt++; for (int j = i - r + 1; j <= i + r - 1; ++j) { if (j >= 1 && j <= n) tmp[j]--; } } } } cout << sum - cnt; } |
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0), cout.tie(0); ios::sync_with_stdio(false); string a, b; while (cin >> a >> b) { unsigned int len = a.size(); int ans = 0; bool OK = true; map<int, int> dic; for (unsigned int i = 0; i < len; ++i) { if (a.at(i) != b.at(i)) { if (!dic.count(a.at(i)) && !dic.count(b.at(i))) { ++ans; dic[a.at(i)] = b.at(i); dic[b.at(i)] = a.at(i); } else if (dic[a.at(i)] != b.at(i) || dic[b.at(i)] != a.at(i)) { cout << -1 << endl; OK = false; break; } } else { if (!dic.count(a.at(i))) { dic[a.at(i)] = a.at(i); } else if (dic[a.at(i)] != a.at(i)) { cout << -1 << endl; OK = false; break; } } } if (OK) { cout << ans << endl; int vis[1000] = {0}; if (ans != 0) { for (char c = a ; c <= z ; ++c) { if (!vis[(int)c] && dic[c] != 0 && dic[c] != c) { cout << c << << (char)dic[c] << endl; vis[dic[c]] = c; } } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 * 100 + 15; int n, mark[maxn], h[maxn], d[maxn], g[2], cnt, c, w; vector<int> ad[maxn]; void dfs(int v) { mark[v] = 1; for (auto u : ad[v]) { if (mark[u] == 0) { h[u] = h[v] + 1; dfs(u); } } } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; ad[u].push_back(v); ad[v].push_back(u); d[u]++; d[v]++; } dfs(1); for (int i = 1; i <= n; i++) { cnt = 0; if (d[i] <= 1) { g[h[i] % 2] = 1; } for (auto u : ad[i]) { if (d[u] == 1) cnt++; } if (cnt > 1) { c++; w += cnt; } } if (g[0] && g[1]) cout << 3; else cout << 1; cout << ; cout << n - 1 + c - w; } |
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long n, k; cin >> n >> k; unsigned long long lo = 0, hi = min(n / 2, n / k + 1), mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (mid + mid * k <= n / 2) lo = mid; else hi = mid - 1; } cout << lo << << lo * k << << n - (lo + lo * k); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a[72] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n; cin >> n; vector<int> b(n); for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < 72 - n + 1; i++) { int u = 0; for (int j = i; j < i + n; j++) { if (a[j] == b[j - i]) { u++; } else break; } if (u == n) { cout << Yes << endl; return 0; } } cout << No << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<long long> lol1; vector<long long> lol2; int main() { long long n, m; cin >> n >> m; for (long long i = 0; i < n; i++) { long long x; cin >> x; lol1.push_back(x); } for (long long i = 0; i < m; i++) { long long x; cin >> x; lol2.push_back(x); } long long s1 = lol1[0], s2 = lol2[0]; long long x = 1, y = 1; long long ans = 0; while (true) { if (s1 < s2) { if (x == n) break; s1 += lol1[x]; x++; } if (s2 < s1) { if (y == m) break; s2 += lol2[y]; y++; } if (s1 == s2) { ans++; s1 = lol1[x]; s2 = lol2[y]; x++; y++; if (x > n && y > m) break; } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1 << 28; const long long LINF = 1ll << 61; inline long long getnum() { register long long r = 0; register bool ng = 0; register char c; c = getchar(); while (c != - && (c < 0 || c > 9 )) c = getchar(); if (c == - ) ng = 1, c = getchar(); while (c >= 0 && c <= 9 ) r = r * 10 + c - 0 , c = getchar(); if (ng) r = -r; return r; } template <class T> inline void putnum(T x) { if (x < 0) putchar( - ), x = -x; register short a[20] = {}, sz = 0; while (x > 0) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar( 0 ); for (int i = sz - 1; i >= 0; i--) putchar( 0 + a[i]); } inline void putsp() { putchar( ); } inline void putendl() { putchar( n ); } inline char mygetchar() { register char c = getchar(); while (c == || c == n ) c = getchar(); return c; } int n, c; int a[200111]; set<pair<int, int> > s; vector<pair<int, int> > tmps; bool used[200111]; int cnt[200111]; int main() { c = getnum(), n = getnum(); s.insert(make_pair(-c, 0)); for (int i = 1; i <= n; i++) { a[i] = getnum(); } sort(a + 1, a + n + 1); for (int i = n; i >= 1; i--) { tmps.clear(); while (s.size() > 0 && -s.begin()->first > a[i]) { int x = s.begin()->second, y = -s.begin()->first + x; s.erase(s.begin()); tmps.push_back(make_pair(-a[i], x)); tmps.push_back(make_pair(-(y - (x + a[i])), x + a[i])); } for (int j = 0; j < tmps.size(); j++) { s.insert(tmps[j]); } } for (auto x : s) { used[x.second] = 1; } int tmp = c; for (int i = n; i >= 1; i--) { if (tmp >= a[i]) { cnt[a[i]]++; cnt[tmp + 1]--; tmp -= a[i]; } } int sum = 0; for (int i = 1; i <= c; i++) { sum += cnt[i]; if (sum > 0 && !used[c - i]) { cout << i << endl; return 0; } } puts( Greed is good ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> g; while (cin >> n) { int xi, xf; xi = -1; xf = -1; bool init = false; g = vector<int>(n); for (int i = 0; i < n; i++) { cin >> g[i]; if (init == false && g[i] == 1) { xi = i; xf = i; init = true; } else if (init == true && g[i] == 1) xf = max(xf, i); } if (xi == -1 || xf == -1) cout << 0 << endl; else { int ceros = 0; for (int i = xi; i < xf;) { if (g[i] == 0 && g[i + 1] == 0) { i += 2; ceros += 2; int b = i; for (int q = b; q <= xf; q++) if (g[q] == 0) { ceros++; b = max(b, q); } else break; i = b + 1; } else i++; } cout << (xf - xi + 1) - ceros << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } bool Check(int x, int y) { if (gcd(x, y) > 1) return false; if (gcd(x + 1, y) > 1) return false; if (gcd(x + 2, y) > 1) return false; return true; } vector<int> ans[10010]; int main() { int n, k; cin >> n >> k; int x = 1; for (int i = 0; i < (n); i++) { ans[i].push_back(x * k); ans[i].push_back((x + 1) * k); ans[i].push_back((x + 2) * k); int y = x + 4; while (!Check(x, y)) y += 2; ans[i].push_back(y * k); x = y + 2; } cout << (x - 2) * k << endl; for (int i = 0; i < (n); i++) cout << ans[i][0] << << ans[i][1] << << ans[i][2] << << ans[i][3] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int N = 1e5 + 10, INFI = 1e9 + 10; int val[12]; unordered_set<int> s; int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < 12; i++) { s.clear(); for (int a = 0; a <= i + 1; a++) { for (int b = 0; b <= i + 1 - a; b++) { for (int c = 0; c <= i + 1 - a - b; c++) { for (int d = 0; d <= i + 1 - a - b - c; d++) { if (a + b + c + d == i + 1) { s.insert((a * 1) + (b * 5) + (c * 10) + (d * 50)); } } } } } val[i] = s.size(); } if (n <= 12) { cout << val[n - 1] << endl; } else { cout << val[11] + 1LL * 49 * (n - 12) << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[200010], n, S, T; vector<int> A; int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , a + i), S += a[i]; for (int i = 0; i < n; i++) if (S - a[i] == (long long)a[i] * (n - 1)) A.push_back(i + 1); printf( %d n , (int)A.size()); for (int i = 0; i < (int)A.size(); i++) printf( %d%c , A[i], i == (int)A.size() - 1 ? n : ); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<bool> primes(100006, false); void sieve() { for (long long i = 3; i <= 100006; i += 2) { primes[i] = 1; } for (long long i = 3; i <= 100006; i += 2) { if (primes[i] == 1) { for (long long j = i * i; j <= 100006; j += i) { primes[j] = 0; } } } primes[2] = 1; primes[0] = primes[1] = 0; } int recpow(int x, int n) { if (n == 0) return 1; else if (n % 2 == 0) return recpow((x * x), n / 2); else return (x * recpow((x * x), (n - 1) / 2)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long k, a, b; cin >> k >> a >> b; long long count = 0; count = a / k; count += b / k; if (count == 0) cout << -1 << endl; else if (a % k > 0 and b / k == 0) cout << -1 << endl; else if (b % k > 0 and a / k == 0) cout << -1 << endl; else cout << count << endl; } |
#include <bits/stdc++.h> using namespace std; struct node { int r, c, p; node() {} node(int _r, int _c, int _p) { r = _r, c = _c, p = _p; } }; const int MAXN = 1005; char g[MAXN][MAXN]; int dist[MAXN][MAXN]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int to_int(const char& c) { return c - 0 ; } bool is_digit(const char& c) { if ( 1 <= c && c <= 9 ) return true; return false; } void bfs(const int& r, const int& c, node s) { queue<node> q; q.push(s); memset(dist, -1, sizeof dist); dist[s.r][s.c] = 0; int xx, yy; while (!q.empty()) { s = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { xx = dx[i] + s.r; yy = dy[i] + s.c; if (xx < 0 || xx >= r || yy < 0 || yy >= c || g[xx][yy] == T || dist[xx][yy] != -1) continue; dist[xx][yy] = dist[s.r][s.c] + 1; q.emplace(xx, yy, s.p); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int r, c, best, ans; vector<node> b; node s, f; while (cin >> r >> c) { for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { cin >> g[i][j]; if (is_digit(g[i][j])) b.emplace_back(i, j, to_int(g[i][j])); if (g[i][j] == S ) s = node(i, j, 0); if (g[i][j] == E ) f = node(i, j, 0); } } bfs(r, c, f); ans = 0; best = dist[s.r][s.c]; for (node i : b) if (dist[i.r][i.c] <= best && dist[i.r][i.c] != -1) ans += i.p; cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100005; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) w = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { s = s * 10 + ch - 0 ; ch = getchar(); } return s * w; } int n; int a[N]; void solve() { n = read(); int sum = 0; for (int i = 1; i <= n; i++) a[i] = read(), sum += a[i]; for (int i = 2; i * i <= sum; i++) { if (sum % i == 0) { printf( %d n , n); for (int i = 1; i <= n; i++) printf( %d , i); puts( ); return; } } printf( %d n , n - 1); bool ok = 0; for (int i = 1; i <= n; i++) { if (!ok && a[i] % 2 == 1) { ok = 1; continue; } printf( %d , i); } puts( ); } int main() { int T = read(); while (T--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const int base = 1e9; const int MAX = 1e5; const double EPS = 1e-9; const double PI = acos(-1.); int main() { char x; string s1, s2, res; vector<char> v(10000); int c1, c2, cnt = 0, i = 0; cin >> s1 >> s2; if ((s1.length() - 1 + s2.length()) % 2 == 1) { cout << Impossible ; return 0; } for (int i = 0; i < s1.length(); i++) { if (s1.substr(i, 1) == | ) { c1 = cnt; cnt = 0; } else { cnt++; } } c2 = cnt; if (max(c1, c2) > (s1.length() - 1 + s2.length()) / 2) { cout << Impossible ; return 0; } if (c1 > c2) { res.append(s2, 0, (s2.length() - (c1 - c2)) / 2); res.append(s1); res.append(s2, (s2.length() - (c1 - c2)) / 2, (s2.length() + (c1 - c2)) / 2); } else { res.append(s2, 0, (s2.length() + (c2 - c1)) / 2); res.append(s1); res.append(s2, (s2.length() + (c2 - c1)) / 2, (s2.length() - (c2 - c1)) / 2); } cout << res; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; long long int arr[100050]; vector<vector<long long int>> v(100050); int main() { cin >> n >> m; for (int i = 1; i <= m; i++) cin >> arr[i]; for (int i = 1; i <= m; i++) { if (i > 1 && arr[i - 1] != arr[i]) v[arr[i]].push_back(arr[i - 1]); if (i < m && arr[i] != arr[i + 1]) v[arr[i]].push_back(arr[i + 1]); } long long int sum = 0, ans; for (int i = 1; i < m; i++) sum += abs(arr[i] - arr[i + 1]); ans = sum; for (int i = 1; i <= n; i++) { if (v[i].size() == 0) continue; sort(v[i].begin(), v[i].end()); long long int y = v[i][v[i].size() / 2]; long long int before = 0, after = 0; for (int x : v[i]) { before += abs(x - i); after += abs(y - x); } ans = min(ans, sum - before + after); } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> prices; int main() { long long int n, p, s = 0; cin >> n; for (long long int a = 0; a < n; a++) { cin >> p; prices.push_back(p); s += p; } sort(prices.rbegin(), prices.rend()); cin >> n; for (long long int a = 0; a < n; a++) { cin >> p; cout << s - prices[p - 1] << n ; } } |
#include <bits/stdc++.h> using namespace std; vector<int> g[212345]; int color[212345]; bool vis[212345]; map<int, int> m; int r = 0; int dfs(int i) { if (vis[i]) return 0; vis[i] = true; m[color[i]]++; if (m[r] < m[color[i]]) r = color[i]; int cont = 0; for (int v : g[i]) { cont += dfs(v); } return 1 + cont; } int solve(int i) { r = -1; m[-1] = 0; int cont = dfs(i); int rr = cont - m[r]; m.clear(); return rr; } int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; ++i) cin >> color[i]; for (int i = 0; i < m; ++i) { int l, r; cin >> l >> r; l--; r--; g[l].push_back(r); g[r].push_back(l); } memset(vis, false, sizeof vis); int cont = 0; for (int i = 0; i < n; ++i) { if (vis[i]) continue; cont += solve(i); } cout << cont << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long base = 71; const long long mod = 1e9 + 9; int convert(char a) { if (a >= a && a <= z ) return (a - a ) + 1; if (a >= A && a <= Z ) return (a - A ) + 27; return (a - 0 ) + 53; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = (int)s.length(); long long ans = 0; vector<long long> V(n); long long H = 0; long long I = 0; long long p = 1; V[0] = 1ll; for (int i = 0; i < n; ++i) { H = (H * base % mod + (long long)convert(s[i])) % mod; I = (I + p * (long long)convert(s[i]) % mod) % mod; p = (p * base) % mod; if (i && H == I) V[i] = V[(i - 1) / 2] + 1ll; ans += V[i]; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, q, a[N], c[N], l[N], r[N]; vector<int> s[N], p[N], b; set<int> s1, s2, s3, s4, s5, s6, s7; int main() { scanf( %d%d , &n, &q); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= q; i++) { scanf( %d%d , &l[i], &r[i]); p[l[i]].push_back(i); } for (int i = n; i; i--) { int x; while (s1.size() && a[x = *s1.begin()] < a[i]) { s6.insert(x); c[x]--; if (!c[x]) s5.insert(x); s1.erase(x); } while (s2.size() && a[x = *s2.begin()] > a[i]) { s7.insert(x); c[x]--; if (!c[x]) s5.insert(x); s2.erase(x); } while (s3.size() && a[x = *s3.begin()] <= a[i]) s3.erase(x); while (s4.size() && a[x = *s4.begin()] >= a[i]) s4.erase(x); if (s3.size() && s4.size()) { auto it = s5.lower_bound(max(*s3.begin(), *s4.begin())); if (it != s5.end() && (!b.size() || *it < b.back())) { b = {i, *it}; auto it2 = s1.lower_bound(*it); it2--; b.push_back(*it2); it2 = s2.lower_bound(*it); it2--; b.push_back(*it2); sort(b.begin(), b.end()); } } for (int j : p[i]) { if (b.size() && b.back() <= r[j]) s[j] = b; else { if (s3.size()) { auto it = s6.lower_bound(*s3.begin()); if (it != s6.end() && *it <= r[j]) s[j] = {i, *it - 1, *it}; } if (s4.size()) { auto it = s7.lower_bound(*s4.begin()); if (it != s7.end() && *it <= r[j]) s[j] = {i, *it - 1, *it}; } } } c[i] = 2; s1.insert(i), s2.insert(i), s3.insert(i), s4.insert(i); } for (int i = 1; i <= q; i++) { printf( %d n , s[i].size()); for (auto j : s[i]) printf( %d , j); puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int K = 10; const int NQ = 160000; int n, k, q; int v[NQ]; pair<int, int> lg[K]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); cin >> n >> k >> q; for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < k; i++) { lg[i].first = -1; lg[i].second = -1; } for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; y--; if (x == 1) { if (lg[0].first == -1 || lg[0].first < v[y]) { lg[0] = make_pair(v[y], y); sort(lg, lg + k); } } else { bool flag = false; for (int i = 0; i < k; i++) { if (lg[i].second == y) { flag = true; } } cout << (flag ? YES : NO ) << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct __timestamper {}; vector<vector<int>> ga; vector<vector<int>> gb; vector<int> comp; vector<bool> used; void dfsa(int v) { used[v] = true; comp.push_back(v); for (int u : ga[v]) { if (!used[u]) { dfsa(u); } } } int main() { int n, m, a, b; while (scanf( %d%d%d%d , &n, &m, &a, &b) == 4) { ga = gb = vector<vector<int>>(n); for (int i = 0; i < m; i++) { int x, y, c; scanf( %d%d%d , &x, &y, &c); --x, --y; auto& g = (c == a) ? ga : gb; g[x].push_back(y); g[y].push_back(x); } vector<pair<int, int>> order; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { order.emplace_back(i, j); } } sort(order.begin(), order.end(), [&](const pair<int, int>& x, const pair<int, int>& y) { return x.first * a + x.second * b < y.first * a + y.second * b; }); vector<int> comp_id(n, -1); used = vector<bool>(n, false); int cc = 0; int cc2 = -2; for (int i = 0; i < n; i++) { if (comp_id[i] == -1) { comp.clear(); dfsa(i); int cid = (comp.size() >= 4 ? cc++ : cc2--); for (int x : comp) { comp_id[x] = cid; } } } vector<vector<vector<pair<int, int>>>> vs( n, vector<vector<pair<int, int>>>(n)); vector<vector<int>> dp((1 << cc), vector<int>(n, 1 << 30)); int init_id = (comp_id[0] < 0) ? 0 : (1 << comp_id[0]); dp[init_id][0] = 0; vs[0][0].emplace_back(init_id, 0); vector<int> ans(n, 1 << 30); for (auto& [cnta, cntb] : order) { for (auto& [mask, v] : vs[cnta][cntb]) { if (dp[mask][v] != cnta * a + cntb * b) continue; ans[v] = min(ans[v], dp[mask][v]); for (int u : ga[v]) { if (dp[mask][u] > dp[mask][v] + a) { assert(comp_id[u] == comp_id[v]); dp[mask][u] = dp[mask][v] + a; vs[cnta + 1][cntb].emplace_back(mask, u); } } for (int u : gb[v]) { if (comp_id[u] == comp_id[v]) continue; int nmask = mask; if (comp_id[u] >= 0) { nmask |= (1 << comp_id[u]); if (mask == nmask) continue; } if (dp[nmask][u] > dp[mask][v] + b) { dp[nmask][u] = dp[mask][v] + b; vs[cnta][cntb + 1].emplace_back(nmask, u); } } } } for (int i = 0; i < n; i++) { printf( %d , ans[i]); } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool d[400]; void input() { int i, n, nk, nc, x; scanf( %d %d , &n, &nk); scanf( %d , &nc); for ((i) = 0; (i) < (nc); (i)++) scanf( %d , &x), d[x] = true; int res = 0, cnt = 0; for ((i) = (1); (i) <= (n); (i)++) { cnt++; if (cnt == nk || d[i]) { res++; cnt = 0; } } printf( %d , res); } int main() { input(); return 0; } |
#include <bits/stdc++.h> using namespace std; struct edge { int v, next; }; bool islucky[100010]; bool vis[100010]; int A[100010], C[100010]; edge e[2 * 100010]; int beg[100010], f[100010]; int n, m, cnt, tot, ne; void dfs(int x) { if (x >= 100010) return; islucky[x] = true; dfs(x * 10 + 4); dfs(x * 10 + 7); } void pre_cal() { memset(islucky, 0, sizeof(islucky)); dfs(0); islucky[0] = false; } void dfs2(int x) { vis[x] = true; cnt++; for (int i = beg[x]; i != -1; i = e[i].next) if (!vis[e[i].v]) { dfs2(e[i].v); } } void add(int x, int y) { e[ne].v = y; e[ne].next = beg[x]; beg[x] = ne++; } void input() { int x, y; ne = 0; memset(beg, 255, sizeof(beg)); for (int i = 0; i < m; i++) { scanf( %d%d , &x, &y); add(x, y); add(y, x); } memset(vis, 0, sizeof(vis)); memset(C, 0, sizeof(C)); for (int i = 1; i <= n; i++) if (!vis[i]) { cnt = 0; dfs2(i); C[cnt]++; } tot = 0; for (int i = 1; i <= n; i++) if (C[i]) { A[tot] = i; C[tot++] = C[i]; } } void dp() { fill(f, f + n + 1, 0x3fffffff); f[0] = 0; for (int j = 0; j < tot; j++) { int num = C[j]; int k = 0; for (k = 0; (1 << k) <= num; k++) { for (int i = n; i >= (1 << k) * A[j]; i--) f[i] = min(f[i], f[i - (1 << k) * A[j]] + (1 << k)); num -= (1 << k); } if (num > 0) for (int i = n; i >= num * A[j]; i--) f[i] = min(f[i], f[i - num * A[j]] + num); } int ans = 0x3fffffff; for (int i = 1; i <= n; i++) if (islucky[i] && f[i] < ans) ans = f[i]; if (ans == 0x3fffffff) printf( -1 n ); else printf( %d n , ans - 1); } int main() { pre_cal(); while (scanf( %d%d , &n, &m) != EOF) { input(); dp(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; long long sum; vector<int> v; ios::sync_with_stdio(0); cin >> n; sum = (1LL + n) * n / 2LL; if (sum % 2LL == 0LL) { sum /= 2LL; for (int i = n; i >= 1; i--) if (sum >= i) { v.push_back(i); sum -= i; } cout << 0 << endl; cout << v.size(); for (int i = 0; i < v.size(); i++) cout << << v[i]; cout << endl; } else { sum /= 2LL; for (int i = n; i >= 1; i--) if (sum >= i) { v.push_back(i); sum -= i; } cout << 1 << endl; cout << v.size(); for (int i = 0; i < v.size(); i++) cout << << v[i]; cout << endl; } return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.