diff --git a/src/bin/a.cc b/src/bin/a.cc index 77478fb..a3dc34e 100644 --- a/src/bin/a.cc +++ b/src/bin/a.cc @@ -25,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -96,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -132,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -173,7 +176,7 @@ template pair istream& operator>>(istream&, pair&); template ostream& operator<<(ostream&, const pair&); template istream& operator>>(istream&, array&); @@ -239,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : a) cin >> x;) -#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ @@ -293,7 +296,7 @@ ll qpow(ll b, ll p, ll mod) { #pragma GCC diagnostic ignored "-Wparentheses" // Accurately find `i` 'th root of `n` (taking the floor) inline ll root(ll n, ll i) { - ll l = 0, r = pow(LLONG_MAX, ld(1) / i); + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); while (l < r) { ll mid = l + r + 1 >> 1; if (qpow(mid, i) <= n) { @@ -393,7 +396,7 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s +vector kmp(const string& s, const string& t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; @@ -403,7 +406,7 @@ vector kmp(string s, string t) { // find all t in s } return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -421,8 +424,8 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } @@ -537,26 +540,13 @@ void prep() { // __attribute__((target("popcnt"))) void solve() { - read(int, n, k); - readvec(int, a, n); - for (int i = 0; i < n; ++i) { - int has = 0; - for (int j = 0; j < n; ++j) { - if (i == j) continue; - has |= abs(a[i] - a[j]) % k == 0; - } - if (has == 0) { - cout << "YES\n"; - cout << i + 1 << '\n'; - return; - } - } - cout << "NO\n"; + read(int, x, y, z); + cout << min({x / 2, y, z}) << '\n'; } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/b.cc b/src/bin/b.cc index 21a6553..070288a 100644 --- a/src/bin/b.cc +++ b/src/bin/b.cc @@ -25,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -96,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -132,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -173,7 +176,7 @@ template pair istream& operator>>(istream&, pair&); template ostream& operator<<(ostream&, const pair&); template istream& operator>>(istream&, array&); @@ -239,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : a) cin >> x;) -#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ @@ -293,7 +296,7 @@ ll qpow(ll b, ll p, ll mod) { #pragma GCC diagnostic ignored "-Wparentheses" // Accurately find `i` 'th root of `n` (taking the floor) inline ll root(ll n, ll i) { - ll l = 0, r = pow(LLONG_MAX, ld(1) / i); + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); while (l < r) { ll mid = l + r + 1 >> 1; if (qpow(mid, i) <= n) { @@ -393,7 +396,7 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s +vector kmp(const string& s, const string& t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; @@ -403,7 +406,7 @@ vector kmp(string s, string t) { // find all t in s } return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -421,8 +424,8 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } @@ -538,18 +541,17 @@ void prep() { // __attribute__((target("popcnt"))) void solve() { read(int, n); - int res = 1; - int curr = 1; - while (curr < n) { - res += 1; - curr += curr + 2; + read(string, a); + if (a[0] == '1' or a[n - 1] == '1') { + cout << "Alice\n"; + } else { + cout << "Bob\n"; } - cout << res << '\n'; } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/c.cc b/src/bin/c.cc index eec5749..592ecd7 100644 --- a/src/bin/c.cc +++ b/src/bin/c.cc @@ -25,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -96,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -132,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -173,7 +176,7 @@ template pair istream& operator>>(istream&, pair&); template ostream& operator<<(ostream&, const pair&); template istream& operator>>(istream&, array&); @@ -239,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : a) cin >> x;) -#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ @@ -293,7 +296,7 @@ ll qpow(ll b, ll p, ll mod) { #pragma GCC diagnostic ignored "-Wparentheses" // Accurately find `i` 'th root of `n` (taking the floor) inline ll root(ll n, ll i) { - ll l = 0, r = pow(LLONG_MAX, ld(1) / i); + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); while (l < r) { ll mid = l + r + 1 >> 1; if (qpow(mid, i) <= n) { @@ -393,7 +396,7 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s +vector kmp(const string& s, const string& t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; @@ -403,7 +406,7 @@ vector kmp(string s, string t) { // find all t in s } return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -421,8 +424,8 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } @@ -524,7 +527,7 @@ constexpr std::array __initarray(const T& value) { } /*******************************************************/ -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 7219 // #define TOT_TEST_CASE 10000 @@ -535,42 +538,92 @@ void dump_ignore() {} void prep() { } -// __attribute__((target("popcnt"))) -void solve() { - read(int, n); - read(ll, k); - k -= 1; - vector res(n); - int l = 0, r = n - 1; - for (int i = n - 2; ~i; --i) { - int bit; - if (i > 63) { - bit = 0; - } else { - bit = k >> i & 1; - k ^= ll(bit) << i; - } - if (bit == 0) { - res[l++] = n - 1 - i; - } else { - res[r--] = n - 1 - i; +template +struct BIT { + int n; + vector c; + BIT(size_t n) : n(n), c(n + 1) {} + void add(size_t i, const T& k) { + while (i <= n) { + c[i] += k; + i += lowbit(i); } } - if (k) { - cout << -1 << '\n'; - } else { - assert(l == r); - res[l] = n; - for (auto&& x : res) { - cout << x << ' '; + T getsum(size_t i) { + T res = {}; + while (i) { + res += c[i]; + i -= lowbit(i); + } + return res; + } +}; + + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + n += 1; + vector cntrj(n, vector(26)); + vector ac(n, vector(26)); + vector status(n); + + vector> events; + vector oc = { ll(26) << 32 }; + for (int i = 0; i < m; ++i) { + ll time = 0; + read(string, raw_time); + time += stoi(raw_time.substr(0, 2)) * 60 + stoi(raw_time.substr(3, 2)); + read(int, team); + read(char, raw_no); + int no = raw_no - 'A'; + read(string, raw_stat); + int stat = raw_stat == "AC"; + bool upd = team == 0 and stat and not ac[team][no]; + if (stat) { + if (ac[team][no]) { + ;; + } else { + ac[team][no] = 1; + status[team].first += 1; + status[team].second += time + ll(1) * 20 * cntrj[team][no]; + } + } else { + cntrj[team][no] += 1; + } + // deb(team, status[team]); + ll currstatus = (26 - ll(1) * status[team].first) << 32 bitor status[team].second; + oc.emplace_back(currstatus); + events.emplace_back(raw_time, team, currstatus, upd ? no : -1); + } + + sort(oc.begin(), oc.end()); + int t = unique(oc.begin(), oc.end()) - oc.begin(); + oc.resize(t); + auto get = [&] (ll x) { return lower_bound(oc.begin(), oc.end(), x) - oc.begin(); }; + + BIT tr(t); + + vector curr(n, (ll(1) * 26) << 32); + tr.add((ll(1) * 26) << 32, n); + + for (auto&& [raw_time, team, penalty, no] : events) { + int old; + if (no != -1) { + old = tr.getsum(get(curr[0])) + 1; + } + tr.add(get(curr[team]) + 1, -1); + curr[team] = penalty; + tr.add(get(curr[team]) + 1, 1); + if (no != -1) { + cout << raw_time << ' ' << char(no + 'A') << " #" << old << " -> #" << tr.getsum(get(curr[0])) + 1 << '\n'; } - cout << '\n'; } } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/cf-815c.cc b/src/bin/cf-815c.cc new file mode 100644 index 0000000..e553711 --- /dev/null +++ b/src/bin/cf-815c.cc @@ -0,0 +1,632 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +constexpr int N = 5010; +ll dp[N][N][2]; + +// __attribute__((target("popcnt"))) +void solve() { + memset(dp, 0x3f, sizeof(dp)); + + read(int, n, tot); + vector a(n + 1), b(n + 1); + adj(ch, n); + + for (int i = 1; i <= n; ++i) { + read(int, c, d); + a[i] = c, b[i] = c - d; + if (i >= 2) { + read(int, j); + edge(ch, i, j); + } + } + + vector sz(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + sz[v] = 1; + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + sz[v] += sz[u]; + } + }; + dfs(dfs, 1, 0); + } + + // vector dp(n + 1, vector(n + 1, vector(2, INFLL))); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + } + int curr = 1; + dp[v][0][0] = 0; + dp[v][1][0] = a[v]; + dp[v][1][1] = b[v]; + for (auto&& u : ch[v]) { + if (u == pa) continue; + for (int j = curr; ~j; --j) { + for (int k = 0; k <= sz[u]; ++k) { + chmin(dp[v][j + k][0], dp[u][k][0] + dp[v][j][0]); + chmin(dp[v][j + k][1], min(dp[u][k][0], dp[u][k][1]) + dp[v][j][1]); + } + } + curr += sz[u]; + } + }; + dfs(dfs, 1, 0); + } + + for (int i = n; ~i; --i) { + if (min(dp[1][i][0], dp[1][i][1]) <= tot) { + cout << i << '\n'; + return; + } + } + +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/cf-830c.cc b/src/bin/cf-830c.cc new file mode 100644 index 0000000..177cf9e --- /dev/null +++ b/src/bin/cf-830c.cc @@ -0,0 +1,636 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n); + read(ll, k); + readvec(int, a, n); + + map, greater<>> events; + for (int i = 0; i < n; ++i) { + int x = a[i]; + for (ll l = 1, r; l <= x; l = r + 1) { + ll val = (x + l - 1) / l; + r = val == 1 ? x : (x - 1) / (val - 1); + if (val > 1) { + events[r].emplace_back(i); + } + } + } + + int mx = *max_element(a.begin(), a.end()); + ll sum = accumulate(a.begin(), a.end(), ll(0)); + ll tot = ll(1) * mx * n - sum; + + if (tot <= k) { + ll extra = (k - tot) / n; + cout << mx + extra << '\n'; + // debug(1); + return; + } + + vector last(n, mx + 1); + tot = ll(1) * (mx + 1) * n; + + vector diff(n, 1); + ll tot_diff = n; + + int lastpos = mx + 1; + while (events.size()) { + auto [r, v] = *events.begin(); + // deb(r, v); + tot -= tot_diff * (lastpos - r); + lastpos = r; + for (auto&& i : v) { + tot -= (diff[i] == 1 ? last[i] : ll(1) * diff[i] * last[i]) - ll(1) * diff[i] * (last[i] - r); + tot_diff -= diff[i]; + diff[i] = (a[i] + r - 1) / r; + tot_diff += diff[i]; + tot += ll(1) * diff[i] * r; + last[i] = r; + } + // debug(tot); + // debug(diff); + events.erase(events.begin()); + // debugvec(events); + if (events.empty()) { + cout << 1 << '\n'; + return; + } else if (tot_diff != 0) { + int nxt = events.begin()->first; + ll t = (tot - sum - k + tot_diff - 1) / tot_diff; + if (r - t > nxt) { + // deb(r, t, nxt); + cout << r - t << '\n'; + return; + } + } + } + + assert(false); + +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/d.cc b/src/bin/d.cc index 1a7e0e4..0c0583a 100644 --- a/src/bin/d.cc +++ b/src/bin/d.cc @@ -25,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -96,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -132,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -173,7 +176,7 @@ template pair istream& operator>>(istream&, pair&); template ostream& operator<<(ostream&, const pair&); template istream& operator>>(istream&, array&); @@ -239,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : a) cin >> x;) -#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ @@ -293,7 +296,7 @@ ll qpow(ll b, ll p, ll mod) { #pragma GCC diagnostic ignored "-Wparentheses" // Accurately find `i` 'th root of `n` (taking the floor) inline ll root(ll n, ll i) { - ll l = 0, r = pow(LLONG_MAX, ld(1) / i); + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); while (l < r) { ll mid = l + r + 1 >> 1; if (qpow(mid, i) <= n) { @@ -393,7 +396,7 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s +vector kmp(const string& s, const string& t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; @@ -403,7 +406,7 @@ vector kmp(string s, string t) { // find all t in s } return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -421,8 +424,8 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } @@ -524,7 +527,7 @@ constexpr std::array __initarray(const T& value) { } /*******************************************************/ -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 7219 // #define TOT_TEST_CASE 10000 @@ -532,25 +535,7 @@ void dump() {} void dump_ignore() {} -constexpr int N = 4e5 + 10; -bool not_prime[N + 1]; - -void soe(int n) { - vector res; - for (int i = 2; i <= n; ++i) { - if (not not_prime[i]) { - res.emplace_back(i); - } - for (auto&& x : res) { - if (i * x > n) break; - not_prime[i * x] = 1; - if (i % x == 0) break; - } - } -} - void prep() { - soe(N); } // __attribute__((target("popcnt"))) @@ -561,44 +546,87 @@ void solve() { read(int, u, v); edge(ch, u, v); } - array, 2> bk; - vector fa(n + 1); + + vector sz(n + 1); { - auto dfs = [&] (auto dfs, int v, int pa, int par) -> void { - fa[v] = pa; - bk[par].emplace_back(v); + auto dfs = [&] (auto dfs, int v, int pa) -> void { + sz[v] = 1; for (auto&& u : ch[v]) { if (u == pa) continue; - dfs(dfs, u, v, 1 - par); + dfs(dfs, u, v); + sz[v] += sz[u]; } }; - dfs(dfs, 1, 0, 0); + dfs(dfs, 1, 0); } - vector res(n + 1); - int curr = 2; - for (auto&& v : bk[0]) { - res[v] = curr; - curr += 2; + + vector down(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + down[v].second = sz[v] - 1; + int tot_sz = 0; + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + down[v].first += down[u].first + down[u].second; + down[v].second += down[u].second; + down[v].second += ll(1) * tot_sz * sz[u]; + tot_sz += sz[u]; + } + }; + dfs(dfs, 1, 0); } - curr = 2 * n; - for (auto&& v : bk[1]) { - res[v] = curr; - curr -= 2; - } - for (int i = 2; i <= n; ++i) { - if (abs(res[fa[i]] - res[i]) == 2) { - res[i] = res[fa[i]] > res[i] ? res[i] + 1 : res[i] - 1; - break; - } + + vector up(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + // up[v].second += n - sz[v]; + for (auto&& u : ch[v]) { + if (u == pa) continue; + // up[v].first += down[u].first; + // up[v].second += down[u].second; + } + up[v].second += n - sz[v]; + up[v].second += ll(1) * (n - sz[v]) * (sz[v] - 1); + // if (v == 1) { + // debug(up[v]); + // debug(down[v]); + // } + for (auto&& u : ch[v]) { + if (u == pa) continue; + up[v].second -= ll(1) * sz[u] * (n - sz[v]); + down[v].second -= ll(1) * sz[u] * (sz[v] - sz[u]); + down[v].first -= down[u].first + down[u].second; + down[v].second -= down[u].second; + // if (v == 1) { + // debug(u); + // debug(up[v]); + // debug(down[v]); + // } + + up[u].first += up[v].first + up[v].second; + up[u].first += down[v].first + down[v].second; + up[u].second += up[v].second; + up[u].second += down[v].second; + dfs(dfs, u, v); + + up[v].second += ll(1) * sz[u] * (n - sz[v]); + down[v].second += ll(1) * sz[u] * (sz[v] - sz[u]); + down[v].first += down[u].first + down[u].second; + down[v].second += down[u].second; + } + }; + dfs(dfs, 1, 0); } + for (int i = 1; i <= n; ++i) { - cout << res[i] << " \n"[i == n]; + cout << up[i].first + down[i].first << '\n'; } } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/g.cc b/src/bin/g.cc index 55ee89f..aea29ad 100644 --- a/src/bin/g.cc +++ b/src/bin/g.cc @@ -1,5 +1,5 @@ // #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") -#pragma GCC optimize("Ofast") +#pragma GCC optimize("Ofast,unroll-loops") /************* This code requires C++17. ***************/ #include @@ -25,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -96,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -132,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -173,7 +176,7 @@ template pair istream& operator>>(istream&, pair&); template ostream& operator<<(ostream&, const pair&); template istream& operator>>(istream&, array&); @@ -239,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : a) cin >> x;) -#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ @@ -289,11 +292,11 @@ ll qpow(ll b, ll p, ll mod) { } -// Accurately find `i` 'th root of `n` (taking the floor) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) inline ll root(ll n, ll i) { - ll l = 0, r = pow(LLONG_MAX, ld(1) / i); + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); while (l < r) { ll mid = l + r + 1 >> 1; if (qpow(mid, i) <= n) { @@ -393,7 +396,7 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s +vector kmp(const string& s, const string& t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; @@ -403,7 +406,7 @@ vector kmp(string s, string t) { // find all t in s } return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -421,8 +424,8 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } @@ -524,7 +527,7 @@ constexpr std::array __initarray(const T& value) { } /*******************************************************/ -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 7219 // #define TOT_TEST_CASE 10000 @@ -535,52 +538,52 @@ void dump_ignore() {} void prep() { } +constexpr int M = 500; +constexpr int N = 1e5 + 1; +constexpr int S = 1 << 17; +int pre[(N + M) / M][S], a[N]; + // __attribute__((target("popcnt"))) void solve() { - read(int, n); - adj(ch, n); - for (int i = 0; i < n - 1; ++i) { - read(int, u, v); - edge(ch, u, v); + read(int, n, k); + for (int i = 0; i < n; ++i) { + cin >> a[i]; } - vector f(n + 1); - { - auto dfs = [&] (auto dfs, int v, int pa) -> void { - for (auto&& u : ch[v]) { - if (u == pa) continue; - dfs(dfs, u, v); - f[v] += 1; + int t = (n + M - 1) / M; + for (int i = 0; i < n; ++i) { + int c = i / M; + for (int j = i; j < min(n, i + M); ++j) { + for (int k = 0; k < S; ++k) { + pre[c][k] xor_eq a[j] - k; } - f[v] -= 1; - }; - dfs(dfs, 1, 0); + } + i += M - 1; } - int res = 0; - { - auto dfs = [&] (auto dfs, int v, int pa, int up) -> int { - int curr = up + f[v]; - int mx = curr; - multiset sub; - for (auto&& u : ch[v]) { - if (u == pa) continue; - sub.emplace(dfs(dfs, u, v, curr)); - } - if (sub.size()) chmax(mx, *sub.rbegin()); - chmax(res, mx - up + 1 + (v != 1)); - if (sub.size() >= 2) { - int l = *sub.rbegin(), r = *next(sub.rbegin()); - chmax(res, (l - curr + 1) + (r - curr + 1) + (f[v] - 1) + (v != 1)); - } - return mx; - }; - dfs(dfs, 1, 0, 0); + + while (k--) { + read(int, d, l, r); + --l, --r; + int left = l; + int res = 0; + while (left <= r and left % M != 0) { + res xor_eq a[left] - d; + left += 1; + } + while (r - left + 1 >= M) { + res xor_eq pre[left / M][d]; + left += M; + } + for (int j = left; j <= r; ++j) { + res xor_eq a[j] - d; + } + cout << res << '\n'; } - cout << res << '\n'; + } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/gym-105631a.cc b/src/bin/gym-105631a.cc new file mode 100644 index 0000000..a3dc34e --- /dev/null +++ b/src/bin/gym-105631a.cc @@ -0,0 +1,571 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, x, y, z); + cout << min({x / 2, y, z}) << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631b.cc b/src/bin/gym-105631b.cc new file mode 100644 index 0000000..070288a --- /dev/null +++ b/src/bin/gym-105631b.cc @@ -0,0 +1,576 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n); + read(string, a); + if (a[0] == '1' or a[n - 1] == '1') { + cout << "Alice\n"; + } else { + cout << "Bob\n"; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631c.cc b/src/bin/gym-105631c.cc new file mode 100644 index 0000000..592ecd7 --- /dev/null +++ b/src/bin/gym-105631c.cc @@ -0,0 +1,648 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +template +struct BIT { + int n; + vector c; + BIT(size_t n) : n(n), c(n + 1) {} + void add(size_t i, const T& k) { + while (i <= n) { + c[i] += k; + i += lowbit(i); + } + } + T getsum(size_t i) { + T res = {}; + while (i) { + res += c[i]; + i -= lowbit(i); + } + return res; + } +}; + + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + n += 1; + vector cntrj(n, vector(26)); + vector ac(n, vector(26)); + vector status(n); + + vector> events; + vector oc = { ll(26) << 32 }; + for (int i = 0; i < m; ++i) { + ll time = 0; + read(string, raw_time); + time += stoi(raw_time.substr(0, 2)) * 60 + stoi(raw_time.substr(3, 2)); + read(int, team); + read(char, raw_no); + int no = raw_no - 'A'; + read(string, raw_stat); + int stat = raw_stat == "AC"; + bool upd = team == 0 and stat and not ac[team][no]; + if (stat) { + if (ac[team][no]) { + ;; + } else { + ac[team][no] = 1; + status[team].first += 1; + status[team].second += time + ll(1) * 20 * cntrj[team][no]; + } + } else { + cntrj[team][no] += 1; + } + // deb(team, status[team]); + ll currstatus = (26 - ll(1) * status[team].first) << 32 bitor status[team].second; + oc.emplace_back(currstatus); + events.emplace_back(raw_time, team, currstatus, upd ? no : -1); + } + + sort(oc.begin(), oc.end()); + int t = unique(oc.begin(), oc.end()) - oc.begin(); + oc.resize(t); + auto get = [&] (ll x) { return lower_bound(oc.begin(), oc.end(), x) - oc.begin(); }; + + BIT tr(t); + + vector curr(n, (ll(1) * 26) << 32); + tr.add((ll(1) * 26) << 32, n); + + for (auto&& [raw_time, team, penalty, no] : events) { + int old; + if (no != -1) { + old = tr.getsum(get(curr[0])) + 1; + } + tr.add(get(curr[team]) + 1, -1); + curr[team] = penalty; + tr.add(get(curr[team]) + 1, 1); + if (no != -1) { + cout << raw_time << ' ' << char(no + 'A') << " #" << old << " -> #" << tr.getsum(get(curr[0])) + 1 << '\n'; + } + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631d.cc b/src/bin/gym-105631d.cc new file mode 100644 index 0000000..0c0583a --- /dev/null +++ b/src/bin/gym-105631d.cc @@ -0,0 +1,651 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n); + adj(ch, n); + for (int i = 0; i < n - 1; ++i) { + read(int, u, v); + edge(ch, u, v); + } + + vector sz(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + sz[v] = 1; + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + sz[v] += sz[u]; + } + }; + dfs(dfs, 1, 0); + } + + vector down(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + down[v].second = sz[v] - 1; + int tot_sz = 0; + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + down[v].first += down[u].first + down[u].second; + down[v].second += down[u].second; + down[v].second += ll(1) * tot_sz * sz[u]; + tot_sz += sz[u]; + } + }; + dfs(dfs, 1, 0); + } + + vector up(n + 1); + { + auto dfs = [&] (auto dfs, int v, int pa) -> void { + // up[v].second += n - sz[v]; + for (auto&& u : ch[v]) { + if (u == pa) continue; + // up[v].first += down[u].first; + // up[v].second += down[u].second; + } + up[v].second += n - sz[v]; + up[v].second += ll(1) * (n - sz[v]) * (sz[v] - 1); + // if (v == 1) { + // debug(up[v]); + // debug(down[v]); + // } + for (auto&& u : ch[v]) { + if (u == pa) continue; + up[v].second -= ll(1) * sz[u] * (n - sz[v]); + down[v].second -= ll(1) * sz[u] * (sz[v] - sz[u]); + down[v].first -= down[u].first + down[u].second; + down[v].second -= down[u].second; + // if (v == 1) { + // debug(u); + // debug(up[v]); + // debug(down[v]); + // } + + up[u].first += up[v].first + up[v].second; + up[u].first += down[v].first + down[v].second; + up[u].second += up[v].second; + up[u].second += down[v].second; + dfs(dfs, u, v); + + up[v].second += ll(1) * sz[u] * (n - sz[v]); + down[v].second += ll(1) * sz[u] * (sz[v] - sz[u]); + down[v].first += down[u].first + down[u].second; + down[v].second += down[u].second; + } + }; + dfs(dfs, 1, 0); + } + + for (int i = 1; i <= n; ++i) { + cout << up[i].first + down[i].first << '\n'; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631g.cc b/src/bin/gym-105631g.cc new file mode 100644 index 0000000..aea29ad --- /dev/null +++ b/src/bin/gym-105631g.cc @@ -0,0 +1,608 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +constexpr int M = 500; +constexpr int N = 1e5 + 1; +constexpr int S = 1 << 17; +int pre[(N + M) / M][S], a[N]; + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, k); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + int t = (n + M - 1) / M; + for (int i = 0; i < n; ++i) { + int c = i / M; + for (int j = i; j < min(n, i + M); ++j) { + for (int k = 0; k < S; ++k) { + pre[c][k] xor_eq a[j] - k; + } + } + i += M - 1; + } + + while (k--) { + read(int, d, l, r); + --l, --r; + int left = l; + int res = 0; + while (left <= r and left % M != 0) { + res xor_eq a[left] - d; + left += 1; + } + while (r - left + 1 >= M) { + res xor_eq pre[left / M][d]; + left += M; + } + for (int j = left; j <= r; ++j) { + res xor_eq a[j] - d; + } + cout << res << '\n'; + } + +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631j.cc b/src/bin/gym-105631j.cc new file mode 100644 index 0000000..1a62b1a --- /dev/null +++ b/src/bin/gym-105631j.cc @@ -0,0 +1,590 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n); + read(ll, l); + readvec(ll, a, n); + ll raw = 0; + for (int i = 0; i < n; ++i) { + raw or_eq a[i]; + } + for (int i = 60; ~i; --i) { + int cnt = 0; + for (int j = 0; j < n; ++j) { + cnt += a[j] >> i bitand 1; + } + if (cnt < 2) continue; + for (int j = 0; j < n; ++j) { + if ((a[j] >> i bitand 1) and ((a[j] bitor ((ll(1) << i) - 1)) xor ll(1) << i) >= l) { + cout << (raw bitor ((ll(1) << i) - 1)) << '\n'; + return; + } + } + } + cout << raw << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631k.cc b/src/bin/gym-105631k.cc new file mode 100644 index 0000000..fc1bf1f --- /dev/null +++ b/src/bin/gym-105631k.cc @@ -0,0 +1,586 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() {} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + if (m == 1) { + cout << 0 << '\n'; + return; + } + vector md(n + 1); + for (int i = 1; i <= n; ++i) { + md[i] = qpow(i, m, PRIME); + } + ll res = 0; + for (int i = 1; i < n; ++i) { + ll all = md[i + 1]; + ll one = 2 * md[i] % PRIME; + ll none = i - 1 <= 0 ? 0 : md[i - 1]; + res += ll(1) * ((n - i) * mod(all - one + none, PRIME) % PRIME) * (ll(1) * i * i % PRIME) % PRIME; + res %= PRIME; + } + cout << res << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/gym-105631l.cc b/src/bin/gym-105631l.cc new file mode 100644 index 0000000..9fe9dcd --- /dev/null +++ b/src/bin/gym-105631l.cc @@ -0,0 +1,627 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + readvec(int, a, n); + readvec(int, b, m); + + vector res(n, vector(m)); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + res[i][j] = b[j]; + } + } + int g = 0; + for (int i = 0; i < n; ++i) { + g = g == 0 ? a[i] : gcd(g, a[i]); + } + ll l = 1; + for (int j = 0; j < m; ++j) { + l = lcm(l, ll(1) * b[j]); + if (l > 1e9) { + cout << "No\n"; + return; + } + } + if (g % l != 0) { + cout << "No\n"; + return; + } + int col = 0, dir = 1; + for (int i = 0; i < n; ++i) { + if (l == a[i]) continue; + res[i][col] = a[i]; + col += dir; + if (col == m) { + col = max(0, m - 2); + dir = -dir; + } else if (col == -1) { + col = min(m - 1, 1); + dir = -dir; + } + } + + for (int j = 0; j < m; ++j) { + int g = 0; + for (int i = 0; i < n; ++i) { + g = g == 0 ? res[i][j] : gcd(g, res[i][j]); + } + if (g != b[j]) { + cout << "No\n"; + return; + } + } + + cout << "Yes\n"; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + cout << res[i][j] << ' '; + } + cout << '\n'; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/j.cc b/src/bin/j.cc index c807c2d..1a62b1a 100644 --- a/src/bin/j.cc +++ b/src/bin/j.cc @@ -1,5 +1,5 @@ // #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") -#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") +#pragma GCC optimize("Ofast,unroll-loops") /************* This code requires C++17. ***************/ #include @@ -12,6 +12,8 @@ using namespace std; constexpr void __() {} #define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() #define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; /* type aliases */ #if LONG_LONG_MAX != INT64_MAX @@ -23,7 +25,7 @@ using ull = unsigned long long; #endif using int128 = __int128_t; using uint128 = __uint128_t; -using ld = long double; +using ld = __float128; // up to 1e-9 precision in binary search using pii = pair; using pil = pair; using pid = pair; using pli = pair; using pll = pair; using pld = pair; using pdi = pair; using pdl = pair; using pdd = pair; @@ -46,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); @@ -94,9 +97,9 @@ struct igt { /* conditions */ #define loop while (1) -#define if_or(var, val) if (!(var == val)) var = val; else -#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) -#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) /* hash */ struct safe_hash { @@ -130,13 +133,15 @@ struct pair_hash { uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { + safe_hash hasher; template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { - res1 = (res1 + x * pw1) % __array_hash_mdl1; - res2 = (res2 + x * pw2) % __array_hash_mdl2; + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } @@ -170,6 +175,17 @@ template pair istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + template istream& operator>>(istream& in, pair& p) { return in >> p.first >> p.second; } @@ -177,6 +193,14 @@ template ostream& operator<<(ostream& out, const pair istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} template void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { using swallow = int[]; // guaranties left to right order @@ -217,21 +241,36 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) { } template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } -#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) -#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (auto& x : a) cin >> x;) -#define readvec1(type, a, n) __AS_PROCEDURE(vector a((n) + 1); copy_n(ii(cin), (n), a.begin() + 1);) -#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) -#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) -#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) -#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) #define deb(...) debug(make_tuple(__VA_ARGS__)) /* pops */ -#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) -#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) -#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} /* math */ template @@ -243,11 +282,43 @@ return_t qpow(ll b, ll p) { else return half * half; } +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + #define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) #define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) +__attribute__((target("lzcnt"))) constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + void __exgcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1, y = 0; @@ -298,15 +369,15 @@ vector decompose_prime(int N) { /* string algorithms */ vector calc_next(string t) { // pi function of t - int n = (int)t.length(); - vector pi(n); - for (int i = 1; i < n; i++) { - int j = pi[i - 1]; - while (j > 0 && t[i] != t[j]) j = pi[j - 1]; - if (t[i] == t[j]) j++; - pi[i] = j; - } - return pi; + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; } vector calc_z(string t) { // z function of t int m = t.length(); @@ -325,17 +396,17 @@ vector calc_z(string t) { // z function of t } return z; } -vector kmp(string s, string t) { // find all t in s - string cur = t + '#' + s; - int sz1 = s.size(), sz2 = t.size(); - vector v; - vector lps = calc_next(cur); - for (int i = sz2 + 1; i <= sz1 + sz2; i++) { - if (lps[i] == sz2) v.push_back(i - 2 * sz2); - } - return v; +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; } -int period(string s) { // find the length of shortest recurring period +int period(const string& s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { @@ -353,16 +424,16 @@ template struct MLL { MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } - friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } - friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } - void operator+=(const MLL& rhs) { val = (*this + rhs).val; } - void operator-=(const MLL& rhs) { val = (*this - rhs).val; } - void operator*=(const MLL& rhs) { val = (*this * rhs).val; } - void operator/=(const MLL& rhs) { val = (*this / rhs).val; } - void operator%=(const MLL& rhs) { val = (*this % rhs).val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } }; template @@ -456,7 +527,7 @@ constexpr std::array __initarray(const T& value) { } /*******************************************************/ -#define SINGLE_TEST_CASE +// #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 7219 // #define TOT_TEST_CASE 10000 @@ -470,28 +541,31 @@ void prep() { // __attribute__((target("popcnt"))) void solve() { read(int, n); - ll tot = 0; + read(ll, l); + readvec(ll, a, n); + ll raw = 0; for (int i = 0; i < n; ++i) { - read(char, c); - if (c == 'B') { - read(int, x); - ll use = min(x, tot); - tot -= use; - if (x - use > 0) { - cout << "YES\n"; - } else { - cout << "NO\n"; + raw or_eq a[i]; + } + for (int i = 60; ~i; --i) { + int cnt = 0; + for (int j = 0; j < n; ++j) { + cnt += a[j] >> i bitand 1; + } + if (cnt < 2) continue; + for (int j = 0; j < n; ++j) { + if ((a[j] >> i bitand 1) and ((a[j] bitor ((ll(1) << i) - 1)) xor ll(1) << i) >= l) { + cout << (raw bitor ((ll(1) << i) - 1)) << '\n'; + return; } - } else { - read(int, x); - tot += x; } } + cout << raw << '\n'; } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) - assert(false && "incompatible compiler variant detected."); + static_assert(false, "incompatible compiler variant detected."); #endif untie; prep(); diff --git a/src/bin/k.cc b/src/bin/k.cc new file mode 100644 index 0000000..fc1bf1f --- /dev/null +++ b/src/bin/k.cc @@ -0,0 +1,586 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() {} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + if (m == 1) { + cout << 0 << '\n'; + return; + } + vector md(n + 1); + for (int i = 1; i <= n; ++i) { + md[i] = qpow(i, m, PRIME); + } + ll res = 0; + for (int i = 1; i < n; ++i) { + ll all = md[i + 1]; + ll one = 2 * md[i] % PRIME; + ll none = i - 1 <= 0 ? 0 : md[i - 1]; + res += ll(1) * ((n - i) * mod(all - one + none, PRIME) % PRIME) * (ll(1) * i * i % PRIME) % PRIME; + res %= PRIME; + } + cout << res << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/l.cc b/src/bin/l.cc new file mode 100644 index 0000000..9fe9dcd --- /dev/null +++ b/src/bin/l.cc @@ -0,0 +1,627 @@ +// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2") +#pragma GCC optimize("Ofast,unroll-loops") +/************* This code requires C++17. ***************/ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) remove_reference::type +template struct argument_type; +template struct argument_type { using type = U; }; + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using ld = __float128; // up to 1e-9 precision in binary search +using pii = pair; using pil = pair; using pid = pair; +using pli = pair; using pll = pair; using pld = pair; +using pdi = pair; using pdl = pair; using pdd = pair; +using tiii = tuple; using tiil = tuple; using tiid = tuple; +using tili = tuple; using till = tuple; using tild = tuple; +using tidi = tuple; using tidl = tuple; using tidd = tuple; +using tlii = tuple; using tlil = tuple; using tlid = tuple; +using tlli = tuple; using tlll = tuple; using tlld = tuple; +using tldi = tuple; using tldl = tuple; using tldd = tuple; +using tdii = tuple; using tdil = tuple; using tdid = tuple; +using tdli = tuple; using tdll = tuple; using tdld = tuple; +using tddi = tuple; using tddl = tuple; using tddd = tuple; +template using max_heap = priority_queue; +template using min_heap = priority_queue, greater<>>; +template using oi = ostream_iterator; +template using ii = istream_iterator; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll PRIMELL = 901017227882342239LL; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937_64 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!((var) == (val))) (var) = (val); else +#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);) +#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +uniform_int_distribution dist(PRIME); +const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); +struct array_hash { + safe_hash hasher; + template + size_t operator()(const Sequence& arr) const { + size_t pw1 = 1, pw2 = 1; + size_t res1 = 0, res2 = 0; + for (auto&& x : arr) { + auto h = hasher(x); + res1 = (res1 + h * pw1) % __array_hash_mdl1; + res2 = (res2 + h * pw2) % __array_hash_mdl2; + pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; + pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; + } + return res1 + res2; + } +}; + +/* build data structures */ +#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) + +// add declarations to avoid circular dependency +template istream& operator>>(istream&, pair&); +template ostream& operator<<(ostream&, const pair&); +template istream& operator>>(istream&, array&); +template ostream& operator<<(ostream&, const array&); +template +decltype(auto) operator<<(std::basic_ostream&, const std::tuple&); +template ostream& operator<<(ostream&, const vector&); +std::ostream& operator<<(std::ostream&, const int128&); + +template istream& operator>>(istream& in, pair& p) { + return in >> p.first >> p.second; +} +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template istream& operator>>(istream& in, array& a) { + for (size_t i = 0; i < N; ++i) in >> a[i]; + return in; +} +template ostream& operator<<(ostream& out, const array& a) { + for (auto&& i : a) out << i << ' '; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(t, ...) __AS_PROCEDURE(argument_type::type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(t, a, n) __AS_PROCEDURE(vector::type> a(n); for (auto& x : (a)) cin >> x;) +#define readvec1(t, a, n) __AS_PROCEDURE(vector::type> a((n) + 1); copy_n(ii::type>(cin), (n), (a).begin() + 1);) +#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) +#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;) +#define deb(...) debug(make_tuple(__VA_ARGS__)) + +/* pops */ +template +inline auto poptop(Container& q) { + auto ret = q.top(); + q.pop(); + return ret; +} +template +inline auto popback(Container& q) { + auto ret = q.back(); + q.pop_back(); + return ret; +} +template +inline auto popfront(Container& q) { + auto ret = q.front(); + q.pop_front(); + return ret; +} + +/* math */ +template +return_t qpow(ll b, ll p) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + return_t half = qpow(b, p / 2); + if (p % 2 == 1) return half * half * b; + else return half * half; +} + +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wparentheses" +// Accurately find `i` 'th root of `n` (taking the floor) +inline ll root(ll n, ll i) { + ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i); + while (l < r) { + ll mid = l + r + 1 >> 1; + if (qpow(mid, i) <= n) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +#pragma GCC diagnostic pop + + +#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +__attribute__((target("lzcnt"))) +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +template +T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + // return (factor, count, factor ** count) + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +vector decompose_prime(int N) { + // return (factor, count) + vector result; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + int cnt = 0; + while (N % i == 0) N /= i, ++cnt; + result.emplace_back(i, cnt); + } + } + if (N != 1) { + result.emplace_back(N, 1); + } + return result; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(const string& s, const string& t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(const string& s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} + +/* modular arithmetic */ +template struct MLL { + ll val; + MLL(ll v = 0) : val(mod(v, mdl)) {} + MLL(const MLL& other) : val(other.val) {} + friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } + friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } + friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); } + friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); } + friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } + friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } + friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } + MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; } + MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; } + MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; } + MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; } + MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; } +}; + +template +ostream& operator<<(ostream& out, const MLL& num) { + return out << num.val; +} + +template +istream& operator>>(istream& in, MLL& num) { + return in >> num.val; +} + +// miscancellous +template +bool chmax(T& lhs, const U& rhs) { + bool ret = lhs < rhs; + if (ret) { + lhs = rhs; + } + return ret; +} +template +bool chmin(T& lhs, const U& rhs) { + bool ret = lhs > rhs; + if (ret) { + lhs = rhs; + } + return ret; +} + +#define functor(func) ([&](auto&&... val) \ +noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ +{return func(std::forward(val)...);}) +#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); }) +template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); +} +template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { + std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); +} +template +vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { + vector> res; + auto a_it = a_first; + auto b_it = b_first; + for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { + res.emplace_back(*a_it, *b_it); + } + return res; +} +template +vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { + vector> res; + if (n > 0) { + res.emplace_back(*a_first, *b_first); + for (size_t i = 1; i != n; ++i) { + res.emplace_back(*++a_first, *++b_first); + } + } + return res; +} +template +class ArithmeticIterator : bidirectional_iterator_tag { +public: + using difference_type = ptrdiff_t; + using value_type = T; +private: + value_type value; +public: + ArithmeticIterator(const T& value) : value(value) {} + value_type operator*() const { return value; } + ArithmeticIterator& operator++() { ++value; return *this; } + ArithmeticIterator& operator--() { --value; return *this; } + bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } +}; +template vector> enumerate(const vector& container) { + return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); +} +#define initarray(init, N) (__initarray::type, (N)>(init)) +namespace detail { + template + constexpr std::array + make_array(const T& value, std::index_sequence) { + return {{(static_cast(Is), value)...}}; + } +} + +template +constexpr std::array __initarray(const T& value) { + return detail::make_array(value, std::make_index_sequence()); +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +// __attribute__((target("popcnt"))) +void solve() { + read(int, n, m); + readvec(int, a, n); + readvec(int, b, m); + + vector res(n, vector(m)); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + res[i][j] = b[j]; + } + } + int g = 0; + for (int i = 0; i < n; ++i) { + g = g == 0 ? a[i] : gcd(g, a[i]); + } + ll l = 1; + for (int j = 0; j < m; ++j) { + l = lcm(l, ll(1) * b[j]); + if (l > 1e9) { + cout << "No\n"; + return; + } + } + if (g % l != 0) { + cout << "No\n"; + return; + } + int col = 0, dir = 1; + for (int i = 0; i < n; ++i) { + if (l == a[i]) continue; + res[i][col] = a[i]; + col += dir; + if (col == m) { + col = max(0, m - 2); + dir = -dir; + } else if (col == -1) { + col = min(m - 1, 1); + dir = -dir; + } + } + + for (int j = 0; j < m; ++j) { + int g = 0; + for (int i = 0; i < n; ++i) { + g = g == 0 ? res[i][j] : gcd(g, res[i][j]); + } + if (g != b[j]) { + cout << "No\n"; + return; + } + } + + cout << "Yes\n"; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + cout << res[i][j] << ' '; + } + cout << '\n'; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + static_assert(false, "incompatible compiler variant detected."); +#endif + untie; + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t != (TOT_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/std.in b/src/bin/std.in index 5693fed..4df20da 100644 --- a/src/bin/std.in +++ b/src/bin/std.in @@ -1 +1,7 @@ -aaaaaaaaaa +6 16 +10 9 +10 5 1 +12 2 1 +20 18 3 +10 2 3 +2 1 5 diff --git a/src/bin/test.py b/src/bin/test.py index ac433d4..b9ddb1f 100644 --- a/src/bin/test.py +++ b/src/bin/test.py @@ -8,8 +8,10 @@ import io PRIME = 998_244_353 if __name__ == '__main__': - N = 50 - print(N) + N = 5000 + M = 10 ** 9 + print(N, M) + print(10, 9) for i in range(2, N + 1): - print(f'{randint(1, i - 1)} {i} {randint(100000000, 999999999)}') - print(f'1 50 {randint(100000000, 999999999)}') + print(10, 9, 1) +