regular backup

This commit is contained in:
arielherself 2025-01-02 18:21:33 +08:00
parent fcbf1cbfa2
commit 9781e95bb5
Signed by: arielherself
SSH Key Fingerprint: SHA256:AK3cyo9tFsp7Mox7K0sYphleC8hReXhnRKxwuDT5LBc
12 changed files with 3977 additions and 115 deletions

View File

@ -1,2 +1,7 @@
nnoremap <leader>l <Cmd>40vs std.in<CR> nnoremap <leader>l <Cmd>40vs std.in<CR>
nnoremap <leader>b <Cmd>term bash ./build.sh %<CR>
if has('nvim')
nnoremap <leader>b <Cmd>wa<CR><Cmd>split<CR><Cmd>term bash ./build.sh %<CR>i
else
nnoremap <leader>b <Cmd>wa<CR><Cmd>split<CR><Cmd>term ++curwin bash ./build.sh %<CR>
endif

View File

@ -1,84 +0,0 @@
vim.api.nvim_create_autocmd("FileType", {
pattern = "cpp",
callback = function()
vim.api.nvim_buf_set_keymap(0, 'n', "<leader>b", '<Cmd>wa<CR><Cmd>10sp<CR><Cmd>te ulimit -s unlimited && diff % .lastsrc >/dev/null 2>&1 && (echo -e "\\e[0;32mUsing cached binary for file % ...\\e[0m\\n" && time ./a.out < std.in) || (g++ -std=c++17 -DONLINE_JUDGE -Wl,-z,stack-size=268435456 -Wall -Wno-parentheses -Ofast -g -fsanitize=address,undefined -xc++ % && time ./a.out < std.in && cp % .lastsrc)<CR>i', {
silent = true,
noremap = true
})
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "python",
callback = function()
vim.api.nvim_buf_set_keymap(0, 'n', "<leader>b", "<Cmd>wa<CR><Cmd>10sp<CR><Cmd>te python3 %<CR>i", {
silent = true,
noremap = true
})
end,
})
-- vim.keymap.set('n', '<leader>y', '<Cmd>!ctext -i $(gcc -xc++ /dev/null -E -Wp,-v 2>&1 | sed -n "s,^ ,,p" | tr "\\n" ",") < % > ctext_out.cc<CR><Cmd>split ctext_out.cc<CR>:%%y+<CR><Cmd>q<CR>')
vim.keymap.set('n', '<leader>1', '<Cmd>CphReceive<CR>');
vim.keymap.set('n', '<leader>2', '<Cmd>CphTest<CR>');
vim.keymap.set('n', '<leader>3', '<Cmd>r ~/RustIsBestLang/src/bin/template.cc<CR>G');
vim.keymap.set('n', '<leader>l', '<Cmd>40vs std.in<CR>')
local function add_timestamp()
local bufnr = vim.api.nvim_get_current_buf()
if vim.fs.basename(vim.api.nvim_buf_get_name(bufnr)) == "template.cc" then
return
end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local current_time = os.time()
local current_time_str = os.date("%Y-%m-%d %H:%M:%S", current_time)
local create_time_str = ""
local elapsed_minutes = 0
if #lines > 0 and lines[1]:match("/**") then
create_time_str = lines[3]:match(" * Created: (.*)")
local create_time = os.time({
year = tonumber(create_time_str:sub(1, 4)),
month = tonumber(create_time_str:sub(6, 7)),
day = tonumber(create_time_str:sub(9, 10)),
hour = tonumber(create_time_str:sub(12, 13)),
min = tonumber(create_time_str:sub(15, 16)),
sec = tonumber(create_time_str:sub(18, 19)),
})
elapsed_minutes = math.floor((current_time - create_time) / 60)
else
create_time_str = current_time_str
end
local comment_string = {
"/**",
" * Author: subcrip",
" * Created: " .. create_time_str,
" * Modified: " .. current_time_str,
" * Elapsed: " .. elapsed_minutes .. " minutes",
" */",
"",
}
if #lines > 0 and lines[1]:match("/**") then
for i = 1, #comment_string do
lines[i] = comment_string[i]
end
else
for i = #comment_string, 1, -1 do
table.insert(lines, 1, comment_string[i])
end
end
vim.cmd("undojoin")
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end
-- vim.api.nvim_create_autocmd(
-- "BufWritePre",
-- {
-- pattern = "*.cc",
-- callback = add_timestamp,
-- }
-- )

612
src/bin/cf-2051g.cc Normal file
View File

@ -0,0 +1,612 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
// #pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
__attribute__((target("popcnt,lzcnt")))
void solve() {
read(int, n, q);
vector<pii> ops;
for (int i = 0; i < q; ++i) {
read(int, idx);
read(char, op);
ops.emplace_back(idx - 1, op == '+');
}
vector cost(n + 1, vector<int>(n + 1, 1));
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
// calculate the cost if placing i before j
if (i == j) continue;
int l = 0, r = 0;
for (auto&& [idx, op] : ops) {
if (idx == i and op == 1) {
r += 1;
} else if (idx == j and op == 0) {
l += 1;
}
chmax(cost[i][j], r - l + 1);
}
}
}
// tsp
vector dp(1 << n + 1, vector<ll>(n + 1, INFLL));
for (int i = 1; i < (1 << n + 1); ++i) {
if (popcount(i) == 1) {
// start from the current item
dp[i][lsp(i)] = 0;
} else {
for (int j = 0; j <= n; ++j) {
if (i >> j bitand 1) {
int rem = i xor 1 << j;
for (int k = 0; k <= n; ++k) {
if (rem >> k bitand 1) {
chmin(dp[i][j], dp[rem][k] + cost[k][j]);
}
}
}
}
}
}
cout << dp[(1 << n + 1) - 1][n] << '\n';
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

595
src/bin/cf-825e.cc Normal file
View File

@ -0,0 +1,595 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
#pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
// __attribute__((target("popcnt")))
void solve() {
read(int, n, m);
adj(ch, n);
vector<int> ind(n + 1);
for (int i = 0; i < m; ++i) {
read(int, u, v);
Edge(ch, u, v);
ind[v] += 1;
}
min_heap<int> q;
for (int i = 1; i <= n; ++i) {
if (ind[i] == 0) {
q.emplace(i);
}
}
vector<int> res(n + 1);
int tm = 0;
while (q.size()) {
int v = poptop(q);
res[v] = ++tm;
for (auto&& u : ch[v]) {
if (--ind[u] == 0) {
q.emplace(u);
}
}
}
putvec1(res);
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

699
src/bin/cf-825f.cc Normal file
View File

@ -0,0 +1,699 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
#pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
struct SA {
int n;
std::vector<int> sa, rk, lc;
vector<vector<int>> st;
SA(std::string s) {
n = s.size();
sa.resize(n);
lc.resize(n - 1);
rk.resize(n);
std::iota(sa.begin(), sa.end(), 0);
sort_by_key(sa.begin(), sa.end(), expr(s[i], int i));
rk[sa[0]] = 0;
for (int i = 1; i < n; i++) {
rk[sa[i]] = rk[sa[i - 1]] + (s[sa[i]] != s[sa[i - 1]]);
}
int k = 1;
std::vector<int> tmp, cnt(n);
tmp.reserve(n);
while (rk[sa[n - 1]] < n - 1) {
tmp.clear();
for (int i = 0; i < k; i++) {
tmp.push_back(n - k + i);
}
for (auto i : sa) {
if (i >= k) {
tmp.push_back(i - k);
}
}
std::fill(cnt.begin(), cnt.end(), 0);
for (int i = 0; i < n; i++) {
cnt[rk[i]]++;
}
for (int i = 1; i < n; i++) {
cnt[i] += cnt[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
sa[--cnt[rk[tmp[i]]]] = tmp[i];
}
std::swap(rk, tmp);
rk[sa[0]] = 0;
for (int i = 1; i < n; i++) {
rk[sa[i]] = rk[sa[i - 1]] + (tmp[sa[i - 1]] < tmp[sa[i]] || sa[i - 1] + k == n || tmp[sa[i - 1] + k] < tmp[sa[i] + k]);
}
k *= 2;
}
for (int i = 0, j = 0; i < n; i++) {
if (rk[i] == 0) {
j = 0;
} else {
for (j -= j > 0; i + j < n && sa[rk[i] - 1] + j < n && s[i + j] == s[sa[rk[i] - 1] + j]; ) {
j++;
}
lc[rk[i] - 1] = j;
}
}
int m = lc.size();
int lgm = lg2(m);
st = vector(lgm + 1, vector<int>(m));
st[0] = lc;
for (int j = 0; j < lgm; j++) {
for (int i = 0; i + (2 << j) <= m; i++) {
st[j + 1][i] = std::min(st[j][i], st[j][i + (1 << j)]);
}
}
}
int rmq(int l, int r) {
int k = lg2(r - l);
return std::min(st[k][l], st[k][r - (1 << k)]);
}
__attribute__((target("lzcnt")))
int lcp(int i, int j) {
if (i == j || i == n || j == n) {
return std::min(n - i, n - j);
}
int a = rk[i];
int b = rk[j];
if (a > b) {
std::swap(a, b);
}
return std::min({n - i, n - j, rmq(a, b)});
}
int lcs(int i, int j) {
if (i == j || i == 0 || j == 0) {
return std::min(i, j);
}
int a = rk[n + n - i];
int b = rk[n + n - j];
if (a > b) {
std::swap(a, b);
}
return std::min({i, j, rmq(a, b)});
}
};
int calc_len(int x) {
int res = 0;
while (x) {
x /= 10;
res += 1;
}
return res;
}
// __attribute__((target("popcnt")))
void solve() {
read(string, s);
SA sa(s);
int n = s.size();
vector dp(n + 1, vector<pii>(n + 1, { INF, 0 }));
vector<int> common(n + 1, INF);
dp[0][0] = { 0, 0 };
common[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
// concatenate
if (i - j >= j and sa.lcp(i - j, i - 2 * j) >= j) {
chmin(dp[i][j], pii { dp[i - j][j].first - calc_len(dp[i - j][j].second) + calc_len(dp[i - j][j].second + 1), dp[i - j][j].second + 1 });
}
// start a new segment
chmin(dp[i][j], pii { common[i - j] + j + calc_len(1), 1 });
chmin(common[i], dp[i][j].first);
}
}
int res = INF;
for (int j = 1; j <= n; ++j) {
chmin(res, dp[n][j].first);
}
cout << res << '\n';
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

674
src/bin/cf-835e.cc Normal file
View File

@ -0,0 +1,674 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
#pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
// __attribute__((target("popcnt")))
void solve() {
constexpr int N = 1024;
read(int, n, x, y);
auto ask = [&] (const vector<int>& v) {
vector<int> q;
int red = 0;
for (auto&& val : v) {
if (val < n) {
q.emplace_back(val);
} else {
red xor_eq x;
}
}
if (q.empty()) {
return red;
}
cout << "? " << q.size() << ' ';
for (auto&& val : q) {
cout << val + 1 << ' ';
}
cout << endl;
read(int, res);
if (res == -1) exit(825);
return res xor red;
};
auto par = [&] (int val, int len) {
if (len % 2 == 1) {
if (val == x) {
return 0;
} else {
return 1;
}
} else {
if (val == (x xor y)) {
return 1;
} else {
return 0;
}
}
};
int layer = 0;
for (int i = 9; i; --i) {
vector<int> q;
for (int j = 0; j < N; ++j) {
if (j >> i & 1) {
q.emplace_back(j);
}
}
assert(q.size() * 2 == N);
if (par(ask(q), N / 2)) {
layer = i;
break;
}
}
vector<int> left;
for (int i = 0; i < N; ++i) {
if (i >> layer & 1) {
left.emplace_back(i);
}
}
int y1, y2;
{
int l = 0, r = N / 2 - 1;
while (l < r) {
int mid = l + r >> 1;
vector<int> q;
for (int i = 0; i <= mid; ++i) {
q.emplace_back(left[i]);
}
if (par(ask(q), mid + 1)) {
r = mid;
} else {
l = mid + 1;
}
}
y1 = left[l];
}
{
int z = (y1 >> layer xor 1) << layer;
int l = z;
int r = z xor ((1 << layer) - 1);
while (l < r) {
int mid = l + r >> 1;
vector<int> q;
for (int i = z; i <= mid; ++i) {
q.emplace_back(i);
}
if (par(ask(q), mid - z + 1)) {
r = mid;
} else {
l = mid + 1;
}
}
y2 = l;
}
if (y1 > y2) swap(y1, y2);
cout << "! " << y1 + 1 << ' ' << y2 + 1 << endl;
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

720
src/bin/cf-835f.cc Normal file
View File

@ -0,0 +1,720 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
#pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
// __attribute__((target("popcnt")))
void solve() {
read(int, n);
readvec((array<int, 3>), edges, n);
vector<int> fa(n + 1);
vector<ll> depth(n + 1);
vector<ll> maxdepth(n + 1);
{
vector<vector<pii>> e(n + 1);
for (auto&& [u, v, w] : edges) {
edgew(e, u, v, w);
}
vector<bool> vis(n + 1);
auto dfs = [&] (auto dfs, int v, int pa) -> void {
fa[v] = pa;
maxdepth[v] = depth[v];
vis[v] = 1;
for (auto&& [u, w] : e[v]) {
if (u == pa or vis[u]) continue;
depth[u] = depth[v] + w;
dfs(dfs, u, v);
chmax(maxdepth[v], maxdepth[u]);
}
};
dfs(dfs, 1, 0);
}
vector<vector<pii>> e(n + 1);
int u1 = -1, v1 = -1, w1 = -1;
for (auto&& [u, v, w] : edges) {
if (fa[u] not_eq v and fa[v] not_eq u) {
u1 = u, v1 = v, w1 = w;
} else {
edgew(e, u, v, w);
}
}
assert(u1 != -1);
if (depth[u1] > depth[v1]) swap(u1, v1);
ll res = INFLL;
// remove one edge on the cycle
vector<ll> du(n + 1), dv(n + 1);
{
auto dfs = [&] (auto dfs, int v, int pa, ll d) -> void {
du[v] = d;
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dfs(dfs, u, v, d + w);
}
};
dfs(dfs, u1, 0, 0);
}
{
auto dfs = [&] (auto dfs, int v, int pa, ll d) -> void {
dv[v] = d;
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dfs(dfs, u, v, d + w);
}
};
dfs(dfs, v1, 0, 0);
}
// subtree max of du and dv
vector<ll> mxdu(n + 1), mxdv(n + 1);
{
auto dfs = [&] (auto dfs, int v, int pa) -> void {
mxdu[v] = du[v];
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dfs(dfs, u, v);
chmax(mxdu[v], mxdu[u]);
}
};
dfs(dfs, 1, 0);
}
{
auto dfs = [&] (auto dfs, int v, int pa) -> void {
mxdv[v] = dv[v];
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dfs(dfs, u, v);
chmax(mxdv[v], mxdv[u]);
}
};
dfs(dfs, 1, 0);
}
vector<ll> downinner(n + 1);
{
auto dfs = [&] (auto dfs, int v, int pa) -> void {
multiset<ll> d = { 0 };
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dfs(dfs, u, v);
chmax(downinner[v], downinner[u]);
d.emplace(maxdepth[u] - depth[v]);
}
if (d.size() >= 2) {
chmax(downinner[v], *d.rbegin() + *next(d.rbegin()));
}
};
dfs(dfs, 1, 0);
}
// tree diameter
chmin(res, downinner[1]);
vector<ll> updu(n + 1);
vector<ll> upinner(n + 1);
{
auto dfs = [&] (auto dfs, int v, int pa, ll uu, ll ud) -> void {
multiset<ll> dus = { uu, du[v] }, depths = { ud, 0 };
multiset<ll> downs;
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dus.emplace(mxdu[u]);
depths.emplace(maxdepth[u] - depth[v]);
downs.emplace(downinner[u]);
}
for (auto&& [u, w] : e[v]) {
if (u == pa) continue;
dus.erase(dus.find(mxdu[u]));
depths.erase(depths.find(maxdepth[u] - depth[v]));
downs.erase(downs.find(downinner[u]));
if (dus.size()) {
updu[u] = *dus.rbegin();
}
chmax(upinner[u], upinner[v]);
if (downs.size()) {
chmax(upinner[u], *downs.rbegin());
}
if (depths.size() >= 2) {
chmax(upinner[u], *depths.rbegin() + *next(depths.rbegin()));
}
dfs(dfs, u, v, dus.size() ? *dus.rbegin() : -INFLL, depths.size() ? *depths.rbegin() + w : -INFLL);
dus.emplace(mxdu[u]);
depths.emplace(maxdepth[u] - depth[v]);
downs.emplace(downinner[u]);
}
};
dfs(dfs, 1, 0, -INFLL, -INFLL);
}
// deb(u1, v1);
int v = v1;
while (v != u1) {
// deb(v, mxdv[v], updu[fa[v]], max({ downinner[v], upinner[v], w1 + mxdv[v] + updu[fa[v]] }));
chmin(res, max({ downinner[v], upinner[v], w1 + mxdv[v] + updu[v] }));
v = fa[v];
}
cout << res << '\n';
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

644
src/bin/cf-845e.cc Normal file
View File

@ -0,0 +1,644 @@
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
#pragma GCC optimize("Ofast,unroll-loops")
/************* 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
template <typename T> struct argument_type;
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<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 PRIMELL = 901017227882342239LL;
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_64 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 {
safe_hash hasher;
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) {
auto h = hasher(x);
res1 = (res1 + h * pw1) % __array_hash_mdl1;
res2 = (res2 + h * pw2) % __array_hash_mdl2;
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
}
return res1 + res2;
}
};
/* build data structures */
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define adj(ch, n) __AS_PROCEDURE(vector<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, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
template <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))
// add declarations to avoid circular dependency
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
std::ostream& operator<<(std::ostream&, const int128&);
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 T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; ++i) in >> a[i];
return in;
}
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
for (auto&& i : a) out << i << ' ';
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;
}
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(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */
template <typename Container>
inline auto poptop(Container& q) {
auto ret = q.top();
q.pop();
return ret;
}
template <typename Container>
inline auto popback(Container& q) {
auto ret = q.back();
q.pop_back();
return ret;
}
template <typename Container>
inline auto popfront(Container& q) {
auto ret = q.front();
q.pop_front();
return ret;
}
/* 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;
}
// dynamic modulus
ll qpow(ll b, ll p, ll mod) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
ll half = qpow(b, p / 2, mod);
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
else return half * half % mod;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
// Accurately find `i` 'th root of `n` (taking the floor)
inline ll root(ll n, ll i) {
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
while (l < r) {
ll mid = l + r + 1 >> 1;
if (qpow<int128>(mid, i) <= n) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
#pragma GCC diagnostic pop
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
__attribute__((target("lzcnt")))
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
template <typename T>
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<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(const string& s, const 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(const string& s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/* modular arithmetic */
template <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(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& 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)...);})
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
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))
namespace detail {
template <typename T, std::size_t...Is>
constexpr std::array<T, sizeof...(Is)>
make_array(const T& value, std::index_sequence<Is...>) {
return {{(static_cast<void>(Is), value)...}};
}
}
template <typename T, std::size_t N>
constexpr std::array<T, N> __initarray(const T& value) {
return detail::make_array(value, std::make_index_sequence<N>());
}
/*******************************************************/
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
// __attribute__((target("popcnt")))
void solve() {
read(int, n, m, k);
readvec(pii, a, k);
transform(a.begin(), a.end(), a.begin(), expr(pair(p.first - 1, p.second - 1), auto&& p));
auto check = [&] (int tm) -> bool {
vector<int> row = { 0, n - 1, n }, col = { 0, m - 1, m };
for (auto&& [x, y] : a) {
row.emplace_back(max(0, x - tm));
row.emplace_back(max(0, x - tm - 1));
row.emplace_back(min(n, x + tm));
row.emplace_back(min(n, x + tm + 1));
col.emplace_back(max(0, y - tm));
col.emplace_back(max(0, y - tm - 1));
col.emplace_back(min(m, y + tm));
col.emplace_back(min(m, y + tm + 1));
}
sort(row.begin(), row.end());
int p = unique(row.begin(), row.end()) - row.begin();
row.resize(p);
auto gr = [&] (int x) { return lower_bound(row.begin(), row.end(), x) - row.begin(); };
sort(col.begin(), col.end());
int q = unique(col.begin(), col.end()) - col.begin();
col.resize(q);
auto gc = [&] (int x) { return lower_bound(col.begin(), col.end(), x) - col.begin(); };
vector cnt(p + 1, vector<int>(q + 1));
for (auto&& [x, y] : a) {
cnt[gr(max(0, x - tm))][gc(max(0, y - tm))] += 1;
cnt[gr(max(0, x - tm))][gc(min(m, y + tm + 1))] -= 1;
cnt[gr(min(n, x + tm + 1))][gc(max(0, y - tm))] -= 1;
cnt[gr(min(n, x + tm + 1))][gc(min(m, y + tm + 1))] += 1;
}
for (int j = 1; j <= q; ++j) {
cnt[0][j] += cnt[0][j - 1];
}
// debug(tm);
// debug(cnt[0]);
for (int i = 1; i <= p; ++i) {
cnt[i][0] += cnt[i - 1][0];
for (int j = 1; j <= q; ++j) {
cnt[i][j] += cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1];
}
// debug(cnt[i]);
}
int mapped_n = gr(n), mapped_m = gc(m);
int minx = INF, maxx = -INF, miny = INF, maxy = -INF;
for (int i = 0; i < mapped_n; ++i) {
for (int j = 0; j < mapped_m; ++j) {
if (cnt[i][j] == 0) {
int x = row[i], y = col[j];
chmin(minx, x);
chmax(maxx, x);
chmin(miny, y);
chmax(maxy, y);
}
}
}
// deb(minx, maxx);
// deb(miny, maxy);
if (maxx - minx + 1 <= 1 + tm * 2 and maxy - miny + 1 <= 1 + tm * 2) {
return true;
} else {
return false;
}
};
int l = 0, r = INF;
while (l < r) {
int mid = l + r >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << '\n';
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
static_assert(false, "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

@ -1 +1 @@
Subproject commit a831bd62780d6d58963c16d34f1f283f560307c8 Subproject commit d4d2d86f434f7eba5cbf0fe3ba3fb8e693a30712

View File

@ -1 +1 @@
aaaaaaaaaa

View File

@ -25,7 +25,7 @@ using ull = unsigned long long;
#endif #endif
using int128 = __int128_t; using int128 = __int128_t;
using uint128 = __uint128_t; using uint128 = __uint128_t;
using ld = __float128; using ld = __float128; // up to 1e-9 precision in binary search
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>; 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 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 pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
@ -48,6 +48,7 @@ constexpr int INF = 0x3f3f3f3f;
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll MDL = 1e9 + 7; constexpr ll MDL = 1e9 + 7;
constexpr ll PRIME = 998'244'353; constexpr ll PRIME = 998'244'353;
constexpr ll PRIMELL = 901017227882342239LL;
constexpr ll MDL1 = 8784491; constexpr ll MDL1 = 8784491;
constexpr ll MDL2 = PRIME; constexpr ll MDL2 = PRIME;
constexpr int128 INT128_MAX = numeric_limits<int128>::max(); constexpr int128 INT128_MAX = numeric_limits<int128>::max();
@ -96,9 +97,9 @@ struct igt {
/* conditions */ /* conditions */
#define loop while (1) #define loop while (1)
#define if_or(var, val) if (!(var == val)) var = val; else #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 continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) #define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
/* hash */ /* hash */
struct safe_hash { struct safe_hash {
@ -241,14 +242,14 @@ std::ostream& operator<<(std::ostream& dest, const int128& value) {
template<typename T> void __read(T& x) { cin >> x; } template<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); } template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);) #define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : a) cin >> x;) #define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), a.begin() + 1);) #define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::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 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 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 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 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 debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) #define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
#define deb(...) debug(make_tuple(__VA_ARGS__)) #define deb(...) debug(make_tuple(__VA_ARGS__))
/* pops */ /* pops */
@ -395,7 +396,7 @@ vector<int> calc_z(string t) { // z function of t
} }
return z; return z;
} }
vector<int> kmp(string s, string t) { // find all t in s vector<int> kmp(const string& s, const string& t) { // find all t in s
string cur = t + '#' + s; string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size(); int sz1 = s.size(), sz2 = t.size();
vector<int> v; vector<int> v;
@ -405,7 +406,7 @@ vector<int> kmp(string s, string t) { // find all t in s
} }
return v; return v;
} }
int period(string s) { // find the length of shortest recurring period int period(const string& s) { // find the length of shortest recurring period
int n = s.length(); int n = s.length();
auto z = calc_z(s); auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) { for (int i = 1; i <= n / 2; ++i) {
@ -423,8 +424,8 @@ template <ll mdl> struct MLL {
MLL(const MLL<mdl>& other) : val(other.val) {} 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 - rhs.val, mdl); }
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend 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; }
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; }
@ -526,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
} }
/*******************************************************/ /*******************************************************/
#define SINGLE_TEST_CASE // #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219 // #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000 // #define TOT_TEST_CASE 10000
@ -539,14 +540,9 @@ void prep() {
// __attribute__((target("popcnt"))) // __attribute__((target("popcnt")))
void solve() { void solve() {
for (int i = 9; ; i += 9) { string a = "9";
if (parity(i)) { cout << a + char(48) << '\n';
debug(i); sort(a.begin(), a.end());
auto test = [&] {
};
return;
}
}
} }
int main() { int main() {

View File

@ -8,7 +8,8 @@ import io
PRIME = 998_244_353 PRIME = 998_244_353
if __name__ == '__main__': if __name__ == '__main__':
N = 500000 N = 50
print(N, N // 2) print(N)
for _ in range(2): for i in range(2, N + 1):
print(*(randint(1, 10 ** 9) for _ in range(N))) print(f'{randint(1, i - 1)} {i} {randint(100000000, 999999999)}')
print(f'1 50 {randint(100000000, 999999999)}')