From 3957d3db767037db6bfc3b355943ebb67aeae4ca Mon Sep 17 00:00:00 2001 From: subcrip Date: Sun, 14 Jul 2024 16:59:45 +0000 Subject: [PATCH] backup --- src/bin/cf-1371e2.cc | 541 ++++++++++++++++++++++++++++++++++++ src/bin/cf-1385f.cc | 588 +++++++++++++++++++++++++++++++++++++++ src/bin/cf-1399e2.cc | 566 +++++++++++++++++++++++++++++++++++++ src/bin/ctext_out.cc | 164 +++++------ src/bin/gym-105229a.cc | 517 ++++++++++++++++++++++++++++++++++ src/bin/gym-105229d.cc | 615 +++++++++++++++++++++++++++++++++++++++++ src/bin/gym-105229g.cc | 562 +++++++++++++++++++++++++++++++++++++ src/bin/gym-105229j.cc | 528 +++++++++++++++++++++++++++++++++++ src/bin/gym-105229k.cc | 569 ++++++++++++++++++++++++++++++++++++++ src/bin/gym-105229l.cc | 566 +++++++++++++++++++++++++++++++++++++ src/bin/gym-105229m.cc | 507 +++++++++++++++++++++++++++++++++ src/bin/std.in | 33 +-- src/bin/test.py | 8 +- 13 files changed, 5643 insertions(+), 121 deletions(-) create mode 100644 src/bin/cf-1371e2.cc create mode 100644 src/bin/cf-1385f.cc create mode 100644 src/bin/cf-1399e2.cc create mode 100644 src/bin/gym-105229a.cc create mode 100644 src/bin/gym-105229d.cc create mode 100644 src/bin/gym-105229g.cc create mode 100644 src/bin/gym-105229j.cc create mode 100644 src/bin/gym-105229k.cc create mode 100644 src/bin/gym-105229l.cc create mode 100644 src/bin/gym-105229m.cc diff --git a/src/bin/cf-1371e2.cc b/src/bin/cf-1371e2.cc new file mode 100644 index 0000000..8bc6bbb --- /dev/null +++ b/src/bin/cf-1371e2.cc @@ -0,0 +1,541 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n, p); + readvec(int, a, n); + + int mx = *max_element(a.begin(), a.end()); + int init = mx - n + 1; + + vector b(n); + for (int i = 0; i < n; ++i) { + b[max(0, a[i] - init)] += 1; + } + // assert(count(b.begin(), b.end(), 1) == n); + // debug(b); + + vector ps(n + 1); + vector cnt(p); + int sum = 0; + for (int i = 0; i < n; ++i) { + sum = (sum + b[i]) % p; + // deb(i + 1, sum - i); + ps[i + 1] = mod(sum - i, p); + cnt[ps[i + 1]] += 1; + } + + vector res; + int delta = 0; + for (int i = 0; i < n; ++i) { + if (cnt[delta] == 0) { + res.emplace_back(init + i); + } else { + // debug(i); + // exit(825); + } + + // remove the previous element + delta = mod(delta - 1, p); + cnt[ps[i + 1]] -= 1; + + // add the tail element + cnt[mod(sum - n - i, p)] += 1; + } + + cout << res.size() << '\n'; + putvec(res); +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-1385f.cc b/src/bin/cf-1385f.cc new file mode 100644 index 0000000..64db6f3 --- /dev/null +++ b/src/bin/cf-1385f.cc @@ -0,0 +1,588 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 85 +// #define TOT_TEST_CASE 7035 + +void dump() { + read(int, n, k); + cout << n << ' ' << k << '\n'; + for (int i = 0; i < n - 1; ++i) { + read(int, u, v); + cout << u << ' ' << v << '\n'; + } +} + +void dump_ignore() { + read(int, n, k); + for (int i = 0; i < n - 1; ++i) { + read(int, u, v); + } +} + +void prep() { +} + +void solve() { + read(int, n, k); + adj(ch, n); + + for (int i = 0; i < n - 1; ++i) { + read(int, u, v); + edge(ch, u, v); + } + + vector dp(n + 1); + vector nv(n + 1); + auto dfs = [&] (auto dfs, int v, int pa) -> void { + for (auto&& u : ch[v]) { + if (u == pa) continue; + dfs(dfs, u, v); + if (dp[u] % k == 0 and nv[u] == 0) { + dp[v] += 1; + } else { + nv[v] += 1; + } + } + }; + dfs(dfs, 1, 0); + // debug(dp); + + int curr = 0; + for (int i = 1; i <= n; ++i) { + curr += dp[i] / k; + } + + int res = 0; + auto dfs2 = [&] (auto dfs2, int v, int pa) -> void { + chmax(res, curr); + // deb(v, curr); + for (auto&& u : ch[v]) { + if (u == pa) continue; + int changed_u = 0, changed_v = 0; + + if (dp[u] % k == 0 and nv[u] == 0) { + // remove one active child from node v + // always doesn't count as a unavailble vertex. + changed_v = 1; + curr -= dp[v] / k; + dp[v] -= 1; + curr += dp[v] / k; + } else { + nv[v] -= 1; + } + if (dp[v] % k == 0 and nv[v] == 0) { + // add one active child to node u + changed_u = 1; + curr -= dp[u] / k; + dp[u] += 1; + curr += dp[u] / k; + } else { + nv[u] += 1; + } + + dfs2(dfs2, u, v); + + if (changed_u) { + curr -= dp[u] / k; + dp[u] -= 1; + curr += dp[u] / k; + } else { + nv[u] -= 1; + } + if (changed_v) { + curr -= dp[v] / k; + dp[v] += 1; + curr += dp[v] / k; + } else { + nv[v] += 1; + } + } + }; + dfs2(dfs2, 1, 0); + + cout << res << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-1399e2.cc b/src/bin/cf-1399e2.cc new file mode 100644 index 0000000..ed47e6a --- /dev/null +++ b/src/bin/cf-1399e2.cc @@ -0,0 +1,566 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n); + read(ll, target); + + vector val; + vector sub(n - 1); + vector cost; + vector> ch(n + 1); + for (int i = 0; i < n - 1; ++i) { + read(int, u, v, w, c); + edgew(ch, u, v, i); + val.emplace_back(w); + cost.emplace_back(c); + } + + ll sum = 0; + auto dfs = [&] (auto dfs, int v, int pa) -> int { + int ret = 0; + for (auto&& [u, i] : ch[v]) { + if (u == pa) continue; + int curr = dfs(dfs, u, v); + ret += curr; + sub[i] = curr; + sum += curr * val[i]; + } + return ret == 0 ? 1 : ret; + }; + dfs(dfs, 1, 0); + + target = sum - target; + + array, 2> cand; + for (int i = 0; i < n - 1; ++i) { + while (val[i] > 0) { + cand[cost[i] - 1].emplace_back((val[i] - val[i] / 2) * sub[i]); + val[i] /= 2; + } + } + + for (int i = 0; i < 2; ++i) { + sort(cand[i].begin(), cand[i].end(), greater()); + } + + int p = cand[0].size(), q = cand[1].size(); + array, 2> ps = { vector(p + 1), vector(q + 1) }; + for (int i = 1; i <= p; ++i) { + ps[0][i] = ps[0][i - 1] + cand[0][i - 1]; + } + for (int i = 1; i <= q; ++i) { + ps[1][i] = ps[1][i - 1] + cand[1][i - 1]; + } + + int res = INF; + for (int i = 0; i <= p; ++i) { + int l = 0, r = q; + while (l < r) { + int mid = l + r >> 1; + if (ps[0][i] + ps[1][mid] >= target) { + r = mid; + } else { + l = mid + 1; + } + } + if (ps[0][i] + ps[1][l] >= target) { + chmin(res, i + 2 * l); + } + } + + cout << res << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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/ctext_out.cc b/src/bin/ctext_out.cc index f816162..05b0bf9 100644 --- a/src/bin/ctext_out.cc +++ b/src/bin/ctext_out.cc @@ -469,103 +469,77 @@ void prep() { } void solve() { - read(int, n); - set> primary, candidates; - set> regular_candidates; + read(int, n, m); + vector a(n); - int curr = 0; - int r = 0; - ll sum = 0; - ll res = 0; - while (n--) { - read(int, tp, d); - sum += d; - if (d < 0) { - curr -= tp; - if (primary.count({-d, tp})) { - primary.erase({-d, tp}); - res += d; - r -= tp; - } else { - candidates.erase({-d, tp}); - if (tp == 0) { - regular_candidates.erase(-d); - } - } - } else { - curr += tp; - candidates.emplace(d, tp); - if (tp == 0) { - regular_candidates.emplace(d); - } - } - - if (candidates.size() and primary.size() < curr) { - auto it = candidates.begin(); - primary.emplace(*it); - res += it->first; - r += it->second; - if (it->second == 0) { - regular_candidates.erase(it->first); - } - candidates.erase(it); - } - - if (primary.size() == curr and curr != 0 and candidates.size() and candidates.begin()->first > primary.rbegin()->first) { - auto out = prev(primary.end()); - res -= out->first; - r -= out->second; - candidates.emplace(*out); - if (out->second == 0) { - regular_candidates.emplace(out->first); - } - primary.erase(out); - auto in = candidates.begin(); - res += in->first; - r += in->second; - primary.emplace(*in); - if (in->second == 0) { - regular_candidates.erase(in->first); - } - candidates.erase(in); - } - - if (primary.size() > curr) { - auto out = prev(primary.end()); - res -= out->first; - r -= out->second; - candidates.emplace(*out); - if (out->second == 0) { - regular_candidates.emplace(out->first); - } - primary.erase(out); - } - - // deb(r, curr); - if (r == curr and curr > 0) { - auto out = prev(primary.end()); - res -= out->first; - r -= out->second; - candidates.emplace(*out); - if (out->second == 0) { - regular_candidates.emplace(out->first); - } - primary.erase(out); - if (regular_candidates.size()) { - auto in = regular_candidates.begin(); - res += *in; - primary.emplace(*in, 0); - candidates.erase({*in, 0}); - regular_candidates.erase(*in); - } - } - - deb(r, curr); - debugvec(primary); - debugvec(candidates); - debugvec(regular_candidates); - cout << res + sum << '\n'; + for (int i = 0; i < n; ++i) { + cin >> a[i].first; } + for (int i = 0; i < n; ++i) { + cin >> a[i].second; + } + + sort_by_key(a.begin(), a.end(), [] (const pii& p) { return -p.second; }); + + int k = min(n, 15); + vector> info(1 << k); + set tm; + for (int i = 0; i < (1 << k); ++i) { + int t = popcount(i); + + for (int j = 0; j < k; ++j) { + if (i & 1 << j) { + info[i][0] += a[j].first; + info[i][1] += a[j].second * --t; + info[i][2] += a[j].second; + } + } + + if (info[i][0] <= m) { + tm.emplace(info[i][0]); + } + } + + map mp; + int N = 0; + for (auto&& x : tm) mp[x] = ++N; + + vector ps(n - k + 1, vector(N + 1)); + for (int i = 0; i <= n - k; ++i) { + vector bk(N + 1); + for (int j = 0; j < (1 << k); ++j) { + if (info[j][0] <= m) { + chmax(bk[mp[info[j][0]]], info[j][1] + info[j][2] * i); + } + } + + for (int j = 1; j <= N; ++j) { + ps[i][j] = max(ps[i][j - 1], bk[j]); + } + } + + ll res = 0; + + for (int i = 0; i < (1 << n - k); ++i) { + int t = popcount(i); + ll tot = 0, sum = 0; + + for (int j = 0; j < n - k; ++j) { + if (i & 1 << j) { + tot += a[k + j].first; + sum += a[k + j].second * --t; + } + } + + if (tot > m) continue; + auto it = mp.upper_bound(m - tot); + if (it == mp.begin()) continue; + --it; + + chmax(res, sum + ps[popcount(i)][it->second]); + } + + cout << res << '\n'; } int main() { diff --git a/src/bin/gym-105229a.cc b/src/bin/gym-105229a.cc new file mode 100644 index 0000000..a62227a --- /dev/null +++ b/src/bin/gym-105229a.cc @@ -0,0 +1,517 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n, m); + for (int i = 0; i <= n; ++i) { + for (int j = 0; j <= m; ++j) { + int res = 0; + for (int k = 0; k <= n; ++k) { + for (int l = 0; l <= m; ++l) { + if (i == k and j == l) continue; + int diam = max(abs(i - k), abs(j - l)); + if (i + k - diam >= 0 and i + k + diam <= 2 * n and + j + l - diam >= 0 and j + l + diam <= 2 * m and + (i + k - diam) % 2 == 0 and (j + l - diam) % 2 == 0) { + res += 1; + } + } + } + cout << res << ' '; + } + cout << '\n'; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229d.cc b/src/bin/gym-105229d.cc new file mode 100644 index 0000000..0b30759 --- /dev/null +++ b/src/bin/gym-105229d.cc @@ -0,0 +1,615 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +struct choice_t { + char op; + ll val; +}; + +void solve() { + using mll = MLL; + + constexpr char plus = '+'; + constexpr char multiply = '*'; + + read(int, n); + vector> a(n); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < 2; ++j) { + read(char, op); + read(int, val); + a[i][j] = { + .op = op, + .val = val, + }; + } + } + + vector b(n); + int m = 0; + ll curr = 0; + for (int i = 0; i < n; ++i) { + if (a[i][0].op == plus and a[i][1].op == plus) { + b[i] = max(a[i][0].val, a[i][1].val); + } else if (a[i][0].op == plus and a[i][1].val == 1) { + b[i] = a[i][0].val; + } else if (a[i][0].val == 1 and a[i][1].op == plus) { + b[i] = a[i][1].val; + } else if (a[i][0].op == multiply and a[i][0].val == 1 and a[i][1].op == multiply and a[i][1].val == 1) { + b[i] = 0; + } else { + b[i] = -1; + } + } + vector ps(n + 1); + for (int i = 1; i <= n; ++i) { + ps[i] = ps[i - 1] + (b[i - 1] == -1 ? 0 : b[i - 1]); + } + vector nxt(n + 1, n); + int last = n; + for (int i = n - 1; ~i; --i) { + if (b[i] == -1) { + nxt[i] = i + 1; + last = i; + } else { + nxt[i] = last; + } + } + + int sz = min(n, 333); + vector> pp(n + 1); + pp[0] = {1, 0}; + mll multiplier = 1, sum = 0; + for (int i = 0; i < n; ++i) { + int curr = 1; + if (a[i][0].op == multiply and a[i][1].op == multiply) { + curr = max(a[i][0].val, a[i][1].val); + } else if (a[i][0].op == plus and a[i][1].op == multiply) { + if (a[i][1].val == 1) { + sum += a[i][0].val; + } else { + curr = a[i][1].val; + } + } else if (a[i][0].op == multiply and a[i][1].op == plus) { + if (a[i][0].val == 1) { + sum += a[i][1].val; + } else { + curr = a[i][0].val; + } + } else { + sum += max(a[i][0].val, a[i][1].val); + } + + multiplier *= curr; + sum *= curr; + pp[i + 1] = {multiplier, sum}; + } + + read(int, q); + while (q--) { + read(int, init, l, r); + --l, --r; + + ll bootstrap = init; + int curr = l; + while (curr <= r and nxt[curr] <= r + 1 and bootstrap <= 1e9) { + if (nxt[curr] == curr + 1) { + ll tmp[2] = {}; + for (int j = 0; j < 2; ++j) { + tmp[j] = bootstrap; + if (a[curr][j].op == plus) { + tmp[j] += a[curr][j].val; + } else { + tmp[j] *= a[curr][j].val; + } + } + bootstrap = max(tmp[0], tmp[1]); + } else { + bootstrap += ps[nxt[curr]] - ps[curr]; + } + curr = nxt[curr]; + } + + mll res = bootstrap; + if (bootstrap <= 1e9 and curr <= r) { + res += ps[r + 1] - ps[curr]; + } else { + res = res * (pp[r + 1].first / pp[curr].first) + pp[r + 1].second - pp[curr].second * (pp[r + 1].first / pp[curr].first); + } + + cout << res << '\n'; + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229g.cc b/src/bin/gym-105229g.cc new file mode 100644 index 0000000..64381c9 --- /dev/null +++ b/src/bin/gym-105229g.cc @@ -0,0 +1,562 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + using mll = MLL; + + read(int, n, m); + vector a(n + 1, vector(n + 1, -1)); + for (int i = 0; i < m; ++i) { + read(int, x, y); + a[x][y] = i; + } + + vector dp(n + 1, vector(n + 1, vector(1 << m))); + for (int i = 0; i <= n; ++i) { + for (int j = 0; j <= n; ++j) { + vector cond; + if (i - 2 >= 0 and j - 1 >= 0 and a[i - 2][j - 1] != -1) { + cond.emplace_back(a[i - 2][j - 1], a[i - 1][j - 1]); + } + if (i - 2 >= 0 and j + 1 <= n and a[i - 2][j + 1] != -1) { + cond.emplace_back(a[i - 2][j + 1], a[i - 1][j + 1]); + } + if (i - 1 >= 0 and j - 2 >= 0 and a[i - 1][j - 2] != -1) { + cond.emplace_back(a[i - 1][j - 2], a[i - 1][j - 1]); + } + if (i - 1 >= 0 and j + 2 <= n and a[i - 1][j + 2] != -1) { + cond.emplace_back(a[i - 1][j + 2], a[i - 1][j + 1]); + } + if (i + 2 <= n and j - 1 >= 0 and a[i + 2][j - 1] != -1) { + cond.emplace_back(a[i + 2][j - 1], a[i + 1][j - 1]); + } + if (i + 2 <= n and j + 1 <= n and a[i + 2][j + 1] != -1) { + cond.emplace_back(a[i + 2][j + 1], a[i + 1][j + 1]); + } + if (i + 1 <= n and j - 2 >= 0 and a[i + 1][j - 2] != -1) { + cond.emplace_back(a[i + 1][j - 2], a[i + 1][j - 1]); + } + if (i + 1 <= n and j + 2 <= n and a[i + 1][j + 2] != -1) { + cond.emplace_back(a[i + 1][j + 2], a[i + 1][j + 1]); + } + + for (int mask = 0; mask < (1 << m); ++mask) { + int new_mask = a[i][j] != -1 ? (mask | 1 << a[i][j]) : mask; + int f = 1; + for (auto&& [x, y] : cond) { + if ((mask & 1 << x) or y != -1 and not (mask & 1 << y)) { + ;; + } else { + f = 0; + break; + } + } + + if (f == 1) { + if (i == 0 and j == 0 and mask == 0) dp[0][0][0] = 1; + if (i - 1 >= 0) { + dp[i][j][new_mask] += dp[i - 1][j][mask]; + } + if (j - 1 >= 0) { + dp[i][j][new_mask] += dp[i][j - 1][mask]; + } + } + } + } + } + + cout << accumulate(dp[n][n].begin(), dp[n][n].end(), mll(0)) << endl; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229j.cc b/src/bin/gym-105229j.cc new file mode 100644 index 0000000..a896904 --- /dev/null +++ b/src/bin/gym-105229j.cc @@ -0,0 +1,528 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +constexpr int N = 1e6 + 5; +vector not_prime(N + 1); +void prep() { + 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 solve() { + read(int, n); + readvec(int, a, n); + vector ps(n + 1); + for (int i = 1; i <= n; ++i) { + ps[i] = ps[i - 1] + a[i - 1]; + } + + int res = INF; + for (int l = 1; l <= n; ++l) { + for (int r = l; r <= n; ++r) { + if (not_prime[ps[r] - ps[l - 1]]) { + chmin(res, r - l); + } + } + } + + cout << (res == INF ? -1 : res) << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229k.cc b/src/bin/gym-105229k.cc new file mode 100644 index 0000000..05b0bf9 --- /dev/null +++ b/src/bin/gym-105229k.cc @@ -0,0 +1,569 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n, m); + vector a(n); + + for (int i = 0; i < n; ++i) { + cin >> a[i].first; + } + for (int i = 0; i < n; ++i) { + cin >> a[i].second; + } + + sort_by_key(a.begin(), a.end(), [] (const pii& p) { return -p.second; }); + + int k = min(n, 15); + vector> info(1 << k); + set tm; + for (int i = 0; i < (1 << k); ++i) { + int t = popcount(i); + + for (int j = 0; j < k; ++j) { + if (i & 1 << j) { + info[i][0] += a[j].first; + info[i][1] += a[j].second * --t; + info[i][2] += a[j].second; + } + } + + if (info[i][0] <= m) { + tm.emplace(info[i][0]); + } + } + + map mp; + int N = 0; + for (auto&& x : tm) mp[x] = ++N; + + vector ps(n - k + 1, vector(N + 1)); + for (int i = 0; i <= n - k; ++i) { + vector bk(N + 1); + for (int j = 0; j < (1 << k); ++j) { + if (info[j][0] <= m) { + chmax(bk[mp[info[j][0]]], info[j][1] + info[j][2] * i); + } + } + + for (int j = 1; j <= N; ++j) { + ps[i][j] = max(ps[i][j - 1], bk[j]); + } + } + + ll res = 0; + + for (int i = 0; i < (1 << n - k); ++i) { + int t = popcount(i); + ll tot = 0, sum = 0; + + for (int j = 0; j < n - k; ++j) { + if (i & 1 << j) { + tot += a[k + j].first; + sum += a[k + j].second * --t; + } + } + + if (tot > m) continue; + auto it = mp.upper_bound(m - tot); + if (it == mp.begin()) continue; + --it; + + chmax(res, sum + ps[popcount(i)][it->second]); + } + + cout << res << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229l.cc b/src/bin/gym-105229l.cc new file mode 100644 index 0000000..b4ac06f --- /dev/null +++ b/src/bin/gym-105229l.cc @@ -0,0 +1,566 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#define comb(n, t) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) +#define fastcomb(n, t) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n, m, k); + adj(ch, n); + for (int i = 1; i <= n; ++i) { + read(int, t); + while (t--) { + read(int, j); + edge(ch, i, j); + } + } + + vector> cand(n + 1); + while (m--) { + read(int, u, v); + if (u > v) swap(u, v); + cand[u].emplace(v); + } + + vector mark(n + 1); + while (k--) { + read(int, i); + mark[i] = 1; + } + + auto dfs = [&] (auto dfs, int v, int pa) -> int { + if (mark[v]) return 0; + else if (ch[v].size() == 1 and ch[v][0] == pa) return INF; + + vector x, y; + for (auto&& u : ch[v]) { + if (u == pa) continue; + + if (cand[v].count(u)) { + x.emplace_back(dfs(dfs, u, v)); + } else { + y.emplace_back(dfs(dfs, u, v)); + } + } + + sort(x.begin(), x.end()); + sort(y.begin(), y.end()); + + int ret = INF; + if (x.size() and x[0] != INF) { + chmin(ret, x[0] + 1); + } + + int sum = 0; + for (auto&& i : x) { + if (i != INF and sum != INF) { + sum += i; + } else { + sum = INF; + } + } + for (auto&& i : y) { + if (i != INF and sum != INF) { + sum += i; + } else { + sum = INF; + } + } + chmin(ret, sum); + + return ret; + }; + + int res = dfs(dfs, 1, 0); + cout << (res == INF? -1 : res) << '\n'; +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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-105229m.cc b/src/bin/gym-105229m.cc new file mode 100644 index 0000000..5cd4b4f --- /dev/null +++ b/src/bin/gym-105229m.cc @@ -0,0 +1,507 @@ +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#pragma GCC diagnostic ignored "-Wreorder" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#pragma GCC diagnostic ignored "-Wshift-op-parentheses" +#pragma GCC diagnostic ignored "-Wlogical-op-parentheses" +#pragma GCC optimize("Ofast") +/************* 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 + +/* 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 = long double; +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 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 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 { + 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; + 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#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)) +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 +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(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 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 */ +#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();) + +/* 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; +} + +#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)]) + +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +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(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; +} +int period(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(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(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; } +}; + +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)...);} +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)) +template +array __initarray(const T& init) { + array res; + for (size_t i = 0; i < N; ++i) { + res[i] = init; + } + return res; +} +/*******************************************************/ + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 +// #define TOT_TEST_CASE 10000 + +void dump() {} + +void dump_ignore() {} + +void prep() { +} + +void solve() { + read(int, n); + if (n == 2) { + cout << 0 << endl; + } else if (n == 3) { + cout << "1\n1 2\n1 3" << endl; + } else if (n == 5) { + cout << "2\n1 2\n3 4\n1 3\n2 4" << endl; + } else { + } +} + +int main() { +#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) + 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 7a508ea..ad7c4cb 100644 --- a/src/bin/std.in +++ b/src/bin/std.in @@ -1,24 +1,9 @@ -3 -0 124587329 -1 95720501 -0 -124587329 -1 514657693 -1 -514657693 -1 -95720501 -0 367146977 -0 -367146977 -0 913300866 -0 788937177 -0 -788937177 -1 807916218 -1 -807916218 -0 -913300866 -0 901311455 -0 -901311455 -0 180039378 -1 547750560 -0 -180039378 -1 -547750560 -1 981295007 -1 -981295007 -1 392151481 +5 ++3 +1 ++4 *1 +*5 *9 +*2 +6 ++5 +3 +2 +1 1 4 +5 4 5 diff --git a/src/bin/test.py b/src/bin/test.py index 9348c7e..14fbe28 100644 --- a/src/bin/test.py +++ b/src/bin/test.py @@ -7,7 +7,11 @@ import io PRIME = 998_244_353 if __name__ == '__main__': - N = 1000000 + N = 100000 + Q = 100000 print(N) for i in range(N): - print(999999000001 + i) + print("+3 *2") + print(Q) + for i in range(Q): + print(f"2 1 {N}")