This commit is contained in:
arielherself 2024-07-02 19:19:33 +08:00
parent 5a8b80dc21
commit 3ece47e33d
Signed by: arielherself
SSH Key Fingerprint: SHA256:AK3cyo9tFsp7Mox7K0sYphleC8hReXhnRKxwuDT5LBc
22 changed files with 7086 additions and 240 deletions

View File

@ -1,3 +1,6 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
@ -480,7 +483,7 @@ array<T, N> __initarray(const T& init) {
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -492,22 +495,8 @@ void prep() {
}
void solve() {
read(int, n);
read(string, a);
using mll = MLL<MDL>;
mll res = 1;
int cnt = 1;
for (int i = 1; i < n; ++i) {
if (a[i] != a[i - 1]) {
cnt += 1;
} else {
res *= (cnt + 1) / 2;
cnt = 1;
}
}
res *= (cnt + 1) / 2;
cout << res << '\n';
read(int, n, k);
cout << k * (n - 1) + 1 << '\n';
}
int main() {

Binary file not shown.

521
src/bin/abc-338f.cc Normal file
View File

@ -0,0 +1,521 @@
#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 optimize("Ofast")
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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() {
}
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
}

524
src/bin/abc-339f.cc Normal file
View File

@ -0,0 +1,524 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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() {
}
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
}

579
src/bin/abc-339g.cc Normal file
View File

@ -0,0 +1,579 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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);
readvec(int, a, n);
auto [N, M] = discretize<ll>(a.begin(), a.end());
int blk = sqrt(n);
vector ps((n + blk - 1) / blk, vector<ll>(N + 1));
// debug((n + blk - 1) / blk);
for (int i = 0; i < n; i += blk) {
int j = min(n, i + blk);
vector<ll> curr(N + 1);
for (int k = i; k < j; ++k) {
curr[M[a[k]]] += a[k];
}
for (int k = 1; k <= N; ++k) {
ps[i / blk][k] = ps[i / blk][k - 1] + curr[k];
}
}
read(int, q);
ll b = 0;
while (q--) {
read(ll, u, v, w);
ll l = u xor b, r = v xor b, x = w xor b;
--l, --r;
auto it = M.upper_bound(x);
b = 0;
if (it == M.begin()) {
;;
} else {
int y = (--it)->second;
int start_idx = (l + blk - 1) / blk;
int end_idx = r / blk;
if (start_idx < end_idx) {
for (int i = start_idx; i < end_idx; ++i) {
b += ps[i][y];
}
for (int i = l; i < start_idx * blk; ++i) {
if (a[i] <= x) {
b += a[i];
}
}
for (int i = end_idx * blk; i <= r; ++i) {
if (a[i] <= x) {
b += a[i];
}
}
} else {
for (int i = l; i <= r; ++i) {
if (a[i] <= x) {
b += a[i];
}
}
}
}
cout << b << '\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
}

549
src/bin/abc-343e.cc Normal file
View File

@ -0,0 +1,549 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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, v1, v2, v3);
constexpr int N = 15;
constexpr int x1 = 0, y1 = 0, z1 = 0;
constexpr int a1 = 7 * 7 * 7, a2 = a1, a3 = a2;
for (int x2 = -N + 1; x2 < N; ++x2) {
for (int y2 = -N + 1; y2 < N; ++y2) {
for (int z2 = 0; z2 < N; ++z2) {
for (int x3 = -N + 1; x3 < N; ++x3) {
for (int y3 = -N + 1; y3 < N; ++y3) {
for (int z3 = 0; z3 < N; ++z3) {
int a12 = max(0, min(x1 + 7, x2 + 7) - max(x1, x2)) * max(0, min(y1 + 7, y2 + 7) - max(y1, y2)) * max(0, min(z1 + 7, z2 + 7) - max(z1, z2));
int a23 = max(0, min(x2 + 7, x3 + 7) - max(x2, x3)) * max(0, min(y2 + 7, y3 + 7) - max(y2, y3)) * max(0, min(z2 + 7, z3 + 7) - max(z2, z3));
int a13 = max(0, min(x1 + 7, x3 + 7) - max(x1, x3)) * max(0, min(y1 + 7, y3 + 7) - max(y1, y3)) * max(0, min(z1 + 7, z3 + 7) - max(z1, z3));
int a123 = max(0, min(x1 + 7, (min(x2 + 7, x3 + 7))) - max(x1, max(x2, x3))) * max(0, min(y1 + 7, min(y2 + 7, y3 + 7)) - max(y1, max(y2, y3))) * max(0, min(z1 + 7, min(z2 + 7, z3 + 7)) - max(z1, max(z2, z3)));
if (v1 == a1 + a2 + a3 - 2 * a12 - 2 * a13 - 2 * a23 + 3 * a123 and v2 == a12 + a23 + a13 - 3 * a123 and v3 == a123) {
cout << "Yes\n" << x1 << ' ' << y1 << ' ' << z1 << ' ' << x2 << ' ' << y2 << ' ' << z2 << ' ' << x3 << ' ' << y3 << ' ' << z3 << '\n';
return;
}
}
}
}
}
}
}
cout << "No\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
}

586
src/bin/abc-346f.cc Normal file
View File

@ -0,0 +1,586 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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(ll, n);
read(string, s, t);
int m = s.size();
vector pos(26, vector<int>());
vector nxt(26, vector<int>(m + 1, -1));
vector<int> cnt(26);
vector<int> rev(m);
for (int i = m - 1; ~i; --i) {
for (int j = 0; j < 26; ++j) {
if (s[i] - 'a' == j) {
nxt[j][i] = i;
} else {
nxt[j][i] = nxt[j][i + 1];
}
}
}
for (int i = 0; i < m; ++i) {
rev[i] = pos[s[i] - 'a'].size();
pos[s[i] - 'a'].push_back(i);
cnt[s[i] - 'a'] += 1;
}
auto check = [&] (ll tm) {
ll use = 1;
int ptr = 0;
for (auto&& x : t) {
int nx = nxt[x - 'a'][ptr];
if (nx == -1) {
nx = nxt[x - 'a'][0];
if (use == LLONG_MAX) return false;
++use;
}
if (nx == -1) {
return false;
}
ll rep = (tm + rev[nx] - 1) / cnt[x - 'a'];
if (use > LLONG_MAX - rep) return false;
use += rep;
ptr = pos[x - 'a'][tm + rev[nx] - rep * cnt[x - 'a'] - 1] + 1;
if (ptr == m) {
ptr = 0;
if (use == LLONG_MAX) return false;
++use;
}
}
return (ptr == 0 ? use - 1 : use) <= n;
};
ll l = 0, r = INFLL;
while (l < r) {
ll mid = (l + r + 1) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
cout << l << '\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
}

558
src/bin/arc-179c.cc Normal file
View File

@ -0,0 +1,558 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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);
vector<int> a(n);
iota(a.begin(), a.end(), 1);
unordered_map<pii, int, pair_hash> cache;
auto cmp = [&] (int i, int j) -> bool {
if (cache.count({i, j})) return cache[{i, j}] == 0;
else if (cache.count({j, i})) return cache[{j, i}] == 1;
cout << "? " << i << ' ' << j << endl;
read(int, x);
if (x == -1) exit(0);
cache[{i, j}] = x;
return x == 0;
};
while (a.size() > 1) {
vector<int> b;
partial_sort(a.begin(), a.end(), a.end(), cmp);
int i = 0, j = a.size() - 1;
while (i < j) {
cout << "+ " << a[i] << ' ' << a[j] << endl;
read(int, x);
if (x == -1) exit(0);
b.emplace_back(x);
++i, --j;
}
if (i == j) {
b.emplace_back(a[i]);
}
swap(a, b);
}
cout << "!" << 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
}

View File

@ -1,3 +1,6 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
@ -480,7 +483,7 @@ array<T, N> __initarray(const T& init) {
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -492,34 +495,26 @@ void prep() {
}
void solve() {
read(int, n, k);
read(int, n);
readvec(int, a, n);
vector<pii> res;
while (1) {
int pos = -1;
vector<int> ss(n + 1, INF);
for (int i = n - 1; ~i; --i) {
ss[i] = min(ss[i + 1], a[i]);
}
for (int i = n - k - 1; i >= 0; --i) {
if (ss[i + k] < a[i] and (pos == -1 or a[i] < a[pos])) {
pos = i;
int prev = 0;
vector<int> b;
for (int i = 0; i < n; ++i) {
if (a[i] < prev) {
b.emplace_back(prev - a[i]);
} else {
prev = a[i];
}
}
if (pos == -1) break;
int j = -1;
for (int i = pos + k; i < n; ++i) {
if (a[i] < a[pos] and (j == -1 or a[i] > a[j])) {
j = i;
}
}
swap(a[pos], a[j]);
res.emplace_back(pos + 1, j + 1);
}
cout << res.size() << '\n';
for (auto&& [x, y] : res) {
cout << x << ' ' << y << '\n';
sort(b.begin(), b.end());
int m = b.size();
ll res = 0;
prev = 0;
for (int i = 0; i < m; ++i) {
res += ll(1) * (b[i] - prev) * (m - i + 1);
prev = b[i];
}
cout << res << '\n';
}
int main() {

View File

@ -1,4 +1,6 @@
#include <numeric>
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
@ -481,7 +483,7 @@ array<T, N> __initarray(const T& init) {
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -495,33 +497,14 @@ void prep() {
void solve() {
read(int, n);
readvec(int, a, n);
constexpr int Z = 1000;
using mll = MLL<MDL>;
vector dp(n + 1, array<mll, 2 * Z + 1>());
mll res = 1;
dp[0][Z] = 1;
unordered_set<int> oc;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= 2 * Z; ++j) {
if (j - a[i - 1] == Z) {
res -= dp[i - 1][j - a[i - 1]];
if (oc.count(a[i - 1])) {
;;
int res = 0;
for (int i = n - 1; ~i; --i) {
if (i == n - 1 or res < a[i]) {
res = a[i];
} else {
oc.emplace(a[i - 1]);
dp[i][j] += dp[i - 1][j - a[i - 1]];
}
} else if (j - a[i - 1] >= 0 and j - a[i - 1] <= 2 * Z) {
dp[i][j] += dp[i - 1][j - a[i - 1]];
}
dp[i][j] += dp[i - 1][j];
res += 1;
}
}
debug(accumulate(dp[n].begin(), dp[n].end(), mll(0)));
res += accumulate(dp[n].begin(), dp[n].end(), mll(0));
cout << res << '\n';
}

690
src/bin/cf-1520f2.cc Normal file
View File

@ -0,0 +1,690 @@
#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 optimize("Ofast")
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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() {
}
int query(int l, int r) {
cout << "? " << l + 1 << ' ' << r + 1 << endl;
read(int, x);
return r - l + 1 - x;
}
void claim(int x) {
cout << "! " << x + 1 << endl;
}
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree {
private:
using size_type = uint64_t;
using info_type = Addable_Info_t;
using tag_type = Tag_t;
size_type _max;
vector<info_type> d;
vector<tag_type> b;
void pull(size_type p) {
d[p] = d[p * 2] + d[p * 2 + 1];
}
void push(size_type p, size_type left_len, size_type right_len) {
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
b[p] = tag_type();
}
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
if (s == t) {
d[p] = c;
return;
}
size_type m = s + (t - s >> 1);
if (s != t) push(p, m - s + 1, t - m);
if (x <= m) set(s, m, p * 2, x, c);
else set(m + 1, t, p * 2 + 1, x, c);
pull(p);
}
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
if (l <= s && t <= r) {
d[p].apply(c, t - s + 1);
b[p].apply(c);
return;
}
size_type m = s + (t - s >> 1);
push(p, m - s + 1, t - m);
if (l <= m) range_apply(s, m, p * 2, l, r, c);
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
pull(p);
}
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
if (l <= s && t <= r) {
return d[p];
}
size_type m = s + (t - s >> 1);
info_type res = {};
push(p, m - s + 1, t - m);
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
return res;
}
void build(const Sequence& a, size_type s, size_type t, size_type p) {
if (s == t) {
d[p] = a[s];
return;
}
int m = s + (t - s >> 1);
build(a, s, m, p * 2);
build(a, m + 1, t, p * 2 + 1);
pull(p);
}
public:
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
segtree(const Sequence& a) : segtree(a.size()) {
build(a, {}, _max, 1);
}
void set(size_type i, const info_type& c) {
set({}, _max, 1, i, c);
}
void range_apply(size_type l, size_type r, const tag_type& c) {
range_apply({}, _max, 1, l, r, c);
}
void apply(size_type i, const tag_type& c) {
range_apply(i, i, c);
}
info_type range_query(size_type l, size_type r) {
return range_query({}, _max, 1, l, r);
}
info_type query(size_type i) {
return range_query(i, i);
}
Sequence serialize() {
Sequence res = {};
for (size_type i = 0; i <= _max; ++i) {
res.push_back(query(i));
}
return res;
}
const vector<info_type>& get_d() {
return d;
}
};
struct Tag {
int val = 0;
void apply(const Tag& rhs) {
val += rhs.val;
}
};
struct Info {
int val = 0;
void apply(const Tag& rhs, size_t len) {
val += rhs.val * len;
}
};
Info operator+(const Info &a, const Info &b) {
return {a.val + b.val};
}
void solve() {
read(int, n, t);
int T = min(n, 16);
int P = (n + T - 1) / T;
segtree<Info, Tag> ps(P + 1);
int initialized = false;
auto init = [&] () {
for (int i = 0, start = 0; start < n; ++i, start += T) {
int end = min(n, start + T) - 1;
ps.set(i + 1, {query(start, end)});
}
};
while (t--) {
read(int, k);
if (not initialized) {
init();
initialized = true;
}
int start, pred;
{
int l = 0, r = P;
while (l < r) {
int mid = l + r + 1 >> 1;
if (ps.range_query(0, mid).val < k) {
l = mid;
} else {
r = mid - 1;
}
}
start = l * T;
pred = ps.range_query(0, l).val;
}
{
int l = start, r = min(n, start + T) - 1;
while (l < r) {
int mid = l + r >> 1;
int curr = pred + query(start, mid);
if (curr < k) {
l = mid + 1;
} else {
r = mid;
}
}
claim(l);
ps.apply(l / T + 1, { -1 });
}
}
}
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
}

585
src/bin/cf-1520g.cc Normal file
View File

@ -0,0 +1,585 @@
#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 optimize("Ofast")
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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, w);
vector a(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
vector ch(n * m, pair<int, array<pii, 5>>());
vector<pii> ch_ul;
auto add_edge = [&] (int v, int u, int w) {
if (v == m * n) {
ch_ul.emplace_back(u, w);
} else {
int pos = ch[v].first++;
ch[v].second[pos] = {u, w};
}
};
#define ser(i, j) ((i) * m + (j))
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == -1) continue;
if (j + 1 < m and a[i][j + 1] != -1) {
add_edge(ser(i, j), ser(i, j + 1), w);
add_edge(ser(i, j + 1), ser(i, j), w);
}
if (i + 1 < n and a[i + 1][j] != -1) {
add_edge(ser(i, j), ser(i + 1, j), w);
add_edge(ser(i + 1, j), ser(i, j), w);
}
if (a[i][j] != 0) {
add_edge(ser(i, j), n * m, a[i][j]);
add_edge(n * m, ser(i, j), a[i][j]);
}
}
}
min_heap<pli> pq;
vector<ll> dis(n * m + 1, INFLL);
vector<bool> vis(n * m + 1);
pq.emplace(0, 0);
dis[0] = 0;
while (pq.size()) {
poptop(pq, d, v);
continue_or(vis[v], 1);
if (v == n * m) {
for (auto&& [u, w] : ch_ul) {
if (d + w < dis[u]) {
dis[u] = d + w;
pq.emplace(d + w, u);
}
}
} else {
for (int i = 0; i < ch[v].first; ++i) {
auto&& [u, w] = ch[v].second[i];
if (d + w < dis[u]) {
dis[u] = d + w;
pq.emplace(d + w, u);
}
}
}
}
cout << (dis[n * m - 1] == INFLL ? -1 : dis[n * m - 1]) << 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
}

548
src/bin/cf-1525e.cc Normal file
View File

@ -0,0 +1,548 @@
#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 optimize("Ofast")
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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() {
}
using mll = MLL<PRIME>;
void solve() {
read(int, n, m);
vector dis(m, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> dis[j][i];
}
}
mll res = 0;
for (auto&& v : dis) {
vector<int> open(n + 1);
for (auto&& x : v) {
open[n + 1 - x] += 1;
}
int curr = n - v.size();
mll rev = 1;
for (int i = 0; i < n; ++i) {
curr += open[i];
rev *= mll(1) * curr / (n - i);
curr -= 1;
}
res += 1 - rev;
}
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
}

587
src/bin/cf-1535e.cc Normal file
View File

@ -0,0 +1,587 @@
#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 optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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, q, a0, c0);
vector<vector<int>> fa = {{}};
vector<int> depth = { 0 };
vector<pii> info = {{ a0, c0 }};
while (q--) {
read(int, op);
if (op == 1) {
read(int, p, a, c);
vector<int> curr = { p };
depth.emplace_back(depth[p] + 1);
info.emplace_back(a, c);
for (int i = 1; (1 << i) <= depth[p] + 1; ++i) {
curr.emplace_back(fa[curr.back()][i - 1]);
}
fa.emplace_back(curr);
} else {
depth.emplace_back();
info.emplace_back();
fa.emplace_back();
auto kth_ancestor = [&] (int v, int k) -> int {
int ptr = v;
int b = 0;
while (k) {
if (k & 1) {
ptr = fa[ptr][b];
}
k >>= 1;
b += 1;
}
return ptr;
};
read(int, v, w);
int ptr = v;
int l = 0;
while (1) {
int nxt = 0;
while (nxt + 1 < fa[ptr].size() and info[fa[ptr][nxt + 1]].first != 0) ++nxt;
if (nxt < fa[ptr].size() and info[fa[ptr][nxt]].first != 0) {
l += 1 << nxt;
ptr = fa[ptr][nxt];
} else {
break;
}
}
int rem = w;
int got = 0;
ll cost = 0;
while (l >= 0 and rem > 0) {
int ptr = kth_ancestor(v, l);
int use = min(info[ptr].first, rem);
info[ptr].first -= use;
rem -= use;
got += use;
cost += ll(1) * info[ptr].second * use;
--l;
}
cout << got << ' ' << cost << 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
}

558
src/bin/cf-1987d.cc Normal file
View File

@ -0,0 +1,558 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> 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);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
read(int, x);
a[x - 1] += 1;
}
vector<int> b;
for (int i = 0; i < n; ++i) {
if (a[i] != 0) {
b.emplace_back(a[i]);
}
}
int m = b.size();
vector dp(m + 1, vector<pii>(m + 1));
dp[0][0] = { 1, 0 };
for (int i = 1; i <= m; ++i) {
for (int j = 0; j + 1 <= m; ++j) {
if (dp[i - 1][j].first) {
dp[i][j + 1] = dp[i - 1][j];
}
}
for (int j = 0; j + b[i - 1] <= m; ++j) {
if (dp[i - 1][j + b[i - 1]].first) {
chmax(dp[i][j], make_pair(1, dp[i - 1][j + b[i - 1]].second + 1));
}
}
}
int res = 0;
for (int i = 0; i <= m; ++i) {
if (dp[m][i].first) {
chmax(res, dp[m][i].second);
}
}
cout << m - 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
}

582
src/bin/cf-1987e.cc Normal file
View File

@ -0,0 +1,582 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
#include<bits/stdc++.h>
using namespace std;
/* macro helpers */
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::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<decltype(container)>::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<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
template <typename T> using ii = istream_iterator<T>;
/* 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<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(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<upair>
#define ugt std::greater<upair>
#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 <typename T, typename U>
size_t operator()(const pair<T, U>& 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<mt19937::result_type> dist(PRIME);
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
struct array_hash {
template <typename Sequence>
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<vector<int>> 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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> 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<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> 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<type> a(n); for (auto& x : a) cin >> x;)
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<type>(cin), (n), a.begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& 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;
}
/* 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 <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(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)])
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<tuple<int, int, ll>> decompose(ll x) {
// return (factor, count, factor ** count)
vector<tuple<int, int, ll>> 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<pii> decompose_prime(int N) {
// return (factor, count)
vector<pii> 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<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> 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<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> 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<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> 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 <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, mdl)) {}
MLL(const MLL<mdl>& 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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> 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 <typename Func, typename RandomIt, typename Compare> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> 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 <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> 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 <typename T>
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<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
template <typename T, size_t N>
array<T, N> __initarray(const T& init) {
array<T, N> res;
for (size_t i = 0; i < N; ++i) {
res[i] = init;
}
return res;
}
/////////////////////////////////////////////////////////
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 483
// #define TOT_TEST_CASE 1000
void dump() {
read(int, n);
readvec1(int, a, n);
putvec1(a);
for (int i = 2; i <= n; ++i) {
read(int, j);
cout << j << " \n"[i == n];
}
}
void dump_ignore() {
read(int, n);
readvec1(int, a, n);
for (int i = 2; i <= n; ++i) {
read(int, j);
}
}
void prep() {
}
void solve() {
read(int, n);
readvec1(int, a, n);
adj(ch, n);
for (int i = 2; i <= n; ++i) {
read(int, j);
edge(ch, i, j);
}
vector dp(n + 1, vector<ll>(n + 1));
ll res = 0;
auto dfs = [&] (auto dfs, int v, int pa) -> void {
ll child_sum = 0;
bool has_child = 0;
for (auto&& u : ch[v]) {
if (u == pa) continue;
dfs(dfs, u, v);
child_sum += a[u];
has_child = 1;
}
if (a[v] > child_sum and has_child) {
ll rem = a[v] - child_sum;
for (int ptr = 0; rem; ++ptr) {
for (auto&& u : ch[v]) {
ll curr = min(rem, dp[u][ptr]);
if (dp[u][ptr] != INFLL) dp[u][ptr] -= curr;
rem -= curr;
res += curr * (ptr + 1);
}
}
}
for (int i = 1; i <= n; ++i) {
for (auto&& u : ch[v]) {
if (u == pa) continue;
dp[v][i] = min(dp[v][i] + dp[u][i - 1], INFLL);
}
}
if (not has_child) {
dp[v][0] = INFLL;
} else {
dp[v][0] = max(ll(0), child_sum - a[v]);
}
};
dfs(dfs, 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
}

View File

@ -1,13 +1,9 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
// #pragma GCC diagnostic ignored "-Wreorder-ctor"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wshift-op-parentheses"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
@ -482,9 +478,9 @@ array<T, N> __initarray(const T& init) {
}
return res;
}
/////////////////////////////////////////////////////////
/*******************************************************/
// #define SINGLE_TEST_CASE
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -496,6 +492,61 @@ void prep() {
}
void solve() {
read(int, n, m, w);
vector a(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
vector ch(n * m + 1, pair<int, array<pii, 7>>());
auto add_edge = [&] (int v, int u, int w) {
int pos = ch[v].first++;
ch[v].second[pos] = {u, w};
};
#define ser(i, j) ((i) * m + (j))
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == -1) continue;
if (j + 1 < m and a[i][j + 1] != -1) {
add_edge(ser(i, j), ser(i, j + 1), w);
cnt += 1;
}
if (i + 1 < n and a[i + 1][j] != -1) {
add_edge(ser(i, j), ser(i + 1, j), w);
cnt += 1;
}
if (a[i][j] != 0) {
add_edge(ser(i, j), n * m, a[i][j]);
cnt += 1;
}
}
}
min_heap<pli> pq;
vector<ll> dis(n * m + 1, INFLL);
vector<bool> vis(n * m + 1);
pq.emplace(0, 0);
dis[0] = 0;
int mxs = 0;
while (pq.size()) {
chmax(mxs, pq.size());
poptop(pq, d, v);
continue_or(vis[v], 1);
for (int i = 0; i < ch[v].first; ++i) {
auto&& [u, w] = ch[v].second[i];
if (d + w < dis[u]) {
dis[u] = d + w;
pq.emplace(d + w, u);
}
}
}
debug(mxs);
cout << (dis[n * m - 1] == INFLL ? -1 : dis[n * m - 1]) << endl;
}
int main() {

View File

@ -1,3 +1,6 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
@ -480,7 +483,7 @@ array<T, N> __initarray(const T& init) {
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -492,43 +495,40 @@ void prep() {
}
void solve() {
constexpr ll N = 1e6 + 10;
read(int, n, m);
readvec(int, b, n);
vector<int> a(N, INF);
read(int, n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
read(int, x);
chmin(a[b[i]], b[i] - x);
a[x - 1] += 1;
}
vector<int> ps(N, 0);
for (int i = 1; i < N; ++i) {
if (a[i] < a[ps[i - 1]]) {
ps[i] = i;
} else {
ps[i] = ps[i - 1];
vector<int> b;
for (int i = 0; i < n; ++i) {
if (a[i] != 0) {
b.emplace_back(a[i]);
}
}
vector<int> dp(N);
for (int i = 1; i < N; ++i) {
if (a[ps[i]] == INF) {
dp[i] = 0;
} else {
dp[i] = dp[i - a[ps[i]]] + 1;
int m = b.size();
vector dp(m + 1, vector<pii>(m + 1));
dp[0][0] = { 1, 0 };
for (int i = 1; i <= m; ++i) {
for (int j = 0; j + 1 <= m; ++j) {
if (dp[i - 1][j].first) {
dp[i][j + 1] = dp[i - 1][j];
}
}
ll res = 0;
while (m--) {
read(int, x);
ll curr = 0;
if (x >= N) {
curr += (x - N + 1) / a[ps[N - 1]] + 1;
x -= curr * a[ps[N - 1]];
for (int j = 0; j + b[i - 1] <= m; ++j) {
if (dp[i - 1][j + b[i - 1]].first) {
chmax(dp[i][j], make_pair(1, dp[i - 1][j + b[i - 1]].second + 1));
}
curr += dp[x];
res += curr;
}
cout << res * 2 << '\n';
}
int res = 0;
for (int i = 0; i <= m; ++i) {
if (dp[m][i].first) {
chmax(res, dp[m][i].second);
}
}
cout << m - res << '\n';
}
int main() {

View File

@ -1,3 +1,6 @@
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
@ -23,47 +26,22 @@ using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using pid = pair<int, ld>;
using pdi = pair<ld, int>;
using pld = pair<ll, ld>;
using pdl = pair<ld, ll>;
using pdd = pair<ld, ld>;
using tlll = tuple<ll, ll, ll>;
using tlld = tuple<ll, ll, ld>;
using tlli = tuple<ll, ll, int>;
using tldl = tuple<ll, ld, ll>;
using tldd = tuple<ll, ld, ld>;
using tldi = tuple<ll, ld, int>;
using tlil = tuple<ll, int, ll>;
using tlid = tuple<ll, int, ld>;
using tlii = tuple<ll, int, int>;
using tdll = tuple<ld, ll, ll>;
using tdld = tuple<ld, ll, ld>;
using tdli = tuple<ld, ll, int>;
using tddl = tuple<ld, ld, ll>;
using tddd = tuple<ld, ld, ld>;
using tddi = tuple<ld, ld, int>;
using tdil = tuple<ld, int, ll>;
using tdid = tuple<ld, int, ld>;
using tdii = tuple<ld, int, int>;
using till = tuple<int, ll, ll>;
using tild = tuple<int, ll, ld>;
using tili = tuple<int, ll, int>;
using tidl = tuple<int, ld, ll>;
using tidd = tuple<int, ld, ld>;
using tidi = tuple<int, ld, int>;
using tiil = tuple<int, int, ll>;
using tiid = tuple<int, int, ld>;
using tiii = tuple<int, int, int>;
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
template <typename T> using max_heap = priority_queue<T>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
template <typename T> using oi = ostream_iterator<T>;
@ -211,6 +189,7 @@ template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __
#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__))
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
return in >> p.first >> p.second;
}
@ -429,6 +408,23 @@ istream& operator>>(istream& in, MLLd& num) {
}
// miscancellous
template <typename T, typename U>
bool chmax(T& lhs, const U& rhs) {
bool ret = lhs < rhs;
if (ret) {
lhs = rhs;
}
return ret;
}
template <typename T, typename U>
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<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
@ -488,74 +484,75 @@ array<T, N> __initarray(const T& init) {
/////////////////////////////////////////////////////////
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
// #define DUMP_TEST_CASE 483
// #define TOT_TEST_CASE 1000
void dump() {}
void dump() {
read(int, n);
readvec1(int, a, n);
putvec1(a);
for (int i = 2; i <= n; ++i) {
read(int, j);
cout << j << " \n"[i == n];
}
}
void dump_ignore() {}
void dump_ignore() {
read(int, n);
readvec1(int, a, n);
for (int i = 2; i <= n; ++i) {
read(int, j);
}
}
void prep() {
}
void solve() {
read(int, n);
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
read(char, c);
a[i] = c - '0';
readvec1(int, a, n);
adj(ch, n);
for (int i = 2; i <= n; ++i) {
read(int, j);
edge(ch, i, j);
}
for (int i = 0; i < n; ++i) {
read(char, c);
b[i] = c - '0';
vector dp(n + 1, vector<ll>(n + 1));
ll res = 0;
auto dfs = [&] (auto dfs, int v, int pa) -> void {
ll child_sum = 0;
bool has_child = 0;
for (auto&& u : ch[v]) {
if (u == pa) continue;
dfs(dfs, u, v);
child_sum += a[u];
has_child = 1;
}
vector<int> c = b;
vector<bool> mc(n);
for (int i = 0; i + 2 < n; ++i) {
if (a[i] == a[i + 2] and a[i] == 0) {
c[i + 1] = 1;
if (b[i + 1] != 1) {
mc[i + 1] = 1;
if (a[v] > child_sum and has_child) {
ll rem = a[v] - child_sum;
for (int ptr = 0; rem; ++ptr) {
for (auto&& u : ch[v]) {
ll curr = min(rem, dp[u][ptr]);
if (dp[u][ptr] != INFLL) dp[u][ptr] -= curr;
rem -= curr;
res += curr * (ptr + 1);
}
}
}
vector<int> d = a;
vector<bool> md(n);
vector<int> ps(n + 1);
for (int i = 0; i + 2 < n; ++i) {
if (c[i] == c[i + 2] and c[i] == 1) {
d[i + 1] = 1;
if (a[i + 1] != 1) {
md[i + 1] = 1;
}
}
}
for (int i = 1; i <= n; ++i) {
ps[i] = ps[i - 1] + d[i - 1];
for (auto&& u : ch[v]) {
if (u == pa) continue;
dp[v][i] = min(dp[v][i] + dp[u][i - 1], INFLL);
}
read(int, q);
while (q--) {
read(int, l, r);
--l, --r;
int res = ps[r + 1] - ps[l];
unordered_set<int, safe_hash> del;
if (l + 1 <= r and mc[l] and md[l + 1]) {
del.emplace(l + 1);
}
if (r - 1 >= l and mc[r] and md[r - 1]) {
del.emplace(r - 1);
}
if (md[l]) {
del.emplace(l);
}
if (md[r]) {
del.emplace(r);
}
cout << res - del.size() << '\n';
if (not has_child) {
dp[v][0] = INFLL;
} else {
dp[v][0] = max(ll(0), child_sum - a[v]);
}
};
dfs(dfs, 1, 0);
cout << res << '\n';
}
int main() {

View File

@ -1,8 +0,0 @@
4 4
1 2 6
2 3 5
2 4 4
1 3 3
1 2 3
1 4 10
3 4 1

View File

@ -1,12 +1,9 @@
#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 optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with C++17.
*/
/************* This code requires C++17. ***************/
#include<bits/stdc++.h>
using namespace std;
@ -481,7 +478,7 @@ array<T, N> __initarray(const T& init) {
}
return res;
}
/////////////////////////////////////////////////////////
/*******************************************************/
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219

View File

@ -1,4 +1,4 @@
from math import lcm, ceil, log
from math import lcm, ceil, log2
from random import choice, randint
from os import system
from collections import defaultdict
@ -7,32 +7,7 @@ import io
PRIME = 998_244_353
if __name__ == '__main__':
n = 4
k = 5
for i in range(1 << n * k):
j = i
cnt = (j & ((1 << n) - 1)).bit_count()
f = 1
for _ in range(k):
if (j & ((1 << n) - 1)).bit_count() != cnt:
f = 0
break
j >>= n
for j in range(n):
l = i >> j
c = 0
while l:
c += l & 1
l >>= n
if (c & 1) == 0:
f = 0
break
if f:
m = i
for j in range(k):
for l in range(n):
print(m & 1, end='')
m >>= 1
print()
print()
N = 2000
print(N, N, 10 ** 9)
for _ in range(N * N):
print(10 ** 9)