This commit is contained in:
subcrip 2024-04-22 23:50:15 +08:00
parent 1e7c9990e4
commit 8a3ae25e03
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
5 changed files with 822 additions and 183 deletions

Binary file not shown.

401
src/bin/cf-1837e.cc Normal file
View File

@ -0,0 +1,401 @@
#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#endif
/////////////////////////////////////////////////////////
/**
* Useful Macros
* by subcrip
* (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) decltype(container)::value_type
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
/* constants */
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll MDL = 1e9 + 7;
constexpr ll PRIME = 998'244'353;
constexpr ll MDL1 = 8784491;
constexpr ll MDL2 = PRIME;
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
/* bit-wise operations */
#define lowbit(x) ((x) & -(x))
#define popcount(x) (__builtin_popcountll(ll(x)))
#define parity(x) (__builtin_parityll(ll(x)))
#define msp(x) (63LL - __builtin_clzll(ll(x)))
#define lsp(x) (__builtin_ctzll(ll(x)))
/* arithmetic operations */
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
/* fast pairs */
#define upair ull
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
#define u1(p) ((p) >> 32)
#define u2(p) ((p) & ((1ULL << 32) - 1))
#define ult std::less<upair>
#define ugt std::greater<upair>
#define ipair ull
#define imake(x, y) (umake(x, y))
#define i1(p) (int(u1(ll(p))))
#define i2(p) (ll(u2(p) << 32) >> 32)
struct ilt {
bool operator()(const ipair& a, const ipair& b) const {
if (i1(a) == i1(b)) return i2(a) < i2(b);
else return i1(a) < i1(b);
}
};
struct igt {
bool operator()(const ipair& a, const ipair& b) const {
if (i1(a) == i1(b)) return i2(a) > i2(b);
else return i1(a) > i1(b);
}
};
/* conditions */
#define loop while (1)
#define if_or(var, val) if (!(var == val)) var = val; else
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
/* hash */
struct safe_hash {
// https://codeforces.com/blog/entry/62393
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct pair_hash {
template <typename T, typename U>
size_t operator()(const pair<T, U>& a) const {
auto hash1 = safe_hash()(a.first);
auto hash2 = safe_hash()(a.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
return hash1;
}
};
/* build data structures */
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
/* io */
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
template<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << 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;)
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
for (auto&& i : vec) out << i << ' ';
return out;
}
std::ostream& operator<<(std::ostream& dest, const int128& value) {
// https://stackoverflow.com/a/25115163/23881100
std::ostream::sentry s( dest );
if ( s ) {
uint128 tmp = value < 0 ? -value : value;
char buffer[ 128 ];
char* d = std::end( buffer );
do {
-- d;
*d = "0123456789"[ tmp % 10 ];
tmp /= 10;
} while ( tmp != 0 );
if ( value < 0 ) {
-- d;
*d = '-';
}
int len = std::end( buffer ) - d;
if ( dest.rdbuf()->sputn( d, len ) != len ) {
dest.setstate( std::ios_base::badbit );
}
}
return dest;
}
/* pops */
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
/* math */
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<tuple<int, int, ll>> decompose(ll x) {
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;
}
/* string algorithms */
vector<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
if (t[i] == t[j]) j++;
pi[i] = j;
}
return pi;
}
vector<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> prev = {1, -1};
for (int i = 1; i < m; ++i) {
if (z[i - prev.first] + i <= prev.second) {
z.push_back(z[i - prev.first]);
} else {
int j = max(i, prev.second + 1);
while (j < m && t[j] == t[j - i]) ++j;
z.push_back(j - i);
prev = {i, j - 1};
}
}
return z;
}
vector<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> lps = calc_next(cur);
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
}
return v;
}
int period(string s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
void dump() {}
void dump_ignore() {}
template <ll mdl> struct MLL {
ll val;
MLL(ll v = 0) : val(mod(v, 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 * mod(inverse(rhs.val, mdl), mdl), mdl); }
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
};
template <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;
}
constexpr int MAXN = 6e5 + 10;
MLL<PRIME> fact[MAXN], pw[MAXN];
void prep() {
fact[0] = 1, pw[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = fact[i - 1] * i;
pw[i] = pw[i - 1] * 2;
}
}
void solve() {
read(int, k);
int n = 1 << k;
vector<int> a(n, -1);
for (int i = 0; i < n; ++i) {
read(int, x);
if (x == -1) {
;;
} else {
a[x - 1] = i;
}
}
MLL<PRIME> res = 1;
for (int i = 0; i < k; ++i) {
int tot = 1 << k - i;
unordered_set<int, safe_hash> left, right;
int cnt = 0;
for (int j = 0; j < tot / 2; ++j) {
if (a[j] == -1) continue;
if (left.count(a[j] >> i) or left.count((a[j] >> i) ^ 1)) {
cout << 0 << '\n';
return;
}
left.emplace(a[j] >> i);
}
for (int j = tot / 2; j < tot; ++j) {
if (a[j] == -1) continue;
if (left.count(a[j] >> i) or right.count(a[j] >> i) or right.count((a[j] >> i) ^ 1)) {
cout << 0 << '\n';
return;
}
right.emplace(a[j] >> i);
cnt += 1;
}
int to_swap = tot / 2;
for (int j = 0; j < tot / 2; ++j) {
if (left.count(j << 1) or left.count((j << 1) + 1) or right.count(j << 1) or right.count((j << 1) + 1)) {
to_swap -= 1;
}
}
res = (res * fact[tot / 2 - cnt]) * pw[to_swap];
}
cout << res << '\n';
}
int main() {
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie, cout.tie(NULL);
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t < (DUMP_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

351
src/bin/cf-1839d.cc Normal file
View File

@ -0,0 +1,351 @@
#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#endif
/////////////////////////////////////////////////////////
/**
* Useful Macros
* by subcrip
* (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) decltype(container)::value_type
/* type aliases */
#if LONG_LONG_MAX != INT64_MAX
using ll = int64_t;
using ull = uint64_t;
#else
using ll = long long;
using ull = unsigned long long;
#endif
using int128 = __int128_t;
using uint128 = __uint128_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
/* constants */
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll MDL = 1e9 + 7;
constexpr ll PRIME = 998'244'353;
constexpr ll MDL1 = 8784491;
constexpr ll MDL2 = PRIME;
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
/* random */
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
/* bit-wise operations */
#define lowbit(x) ((x) & -(x))
#define popcount(x) (__builtin_popcountll(ll(x)))
#define parity(x) (__builtin_parityll(ll(x)))
#define msp(x) (63LL - __builtin_clzll(ll(x)))
#define lsp(x) (__builtin_ctzll(ll(x)))
/* arithmetic operations */
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
/* fast pairs */
#define upair ull
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
#define u1(p) ((p) >> 32)
#define u2(p) ((p) & ((1ULL << 32) - 1))
#define ult std::less<upair>
#define ugt std::greater<upair>
#define ipair ull
#define imake(x, y) (umake(x, y))
#define i1(p) (int(u1(ll(p))))
#define i2(p) (ll(u2(p) << 32) >> 32)
struct ilt {
bool operator()(const ipair& a, const ipair& b) const {
if (i1(a) == i1(b)) return i2(a) < i2(b);
else return i1(a) < i1(b);
}
};
struct igt {
bool operator()(const ipair& a, const ipair& b) const {
if (i1(a) == i1(b)) return i2(a) > i2(b);
else return i1(a) > i1(b);
}
};
/* conditions */
#define loop while (1)
#define if_or(var, val) if (!(var == val)) var = val; else
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
/* hash */
struct safe_hash {
// https://codeforces.com/blog/entry/62393
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct pair_hash {
template <typename T, typename U>
size_t operator()(const pair<T, U>& a) const {
auto hash1 = safe_hash()(a.first);
auto hash2 = safe_hash()(a.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
return hash1;
}
};
/* build data structures */
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
map<T, size_t> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
set<T> st(__first, __last);
size_t N = 0;
unordered_map<T, size_t, safe_hash> mp;
for (auto&& x : st) mp[x] = ++N;
return {N, mp};
}
/* io */
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
template<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << 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;)
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
using swallow = int[]; // guaranties left to right order
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
}
template<typename Char, typename Traits, typename... Args>
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
os << "{";
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
return os << "}";
}
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
for (auto&& i : vec) out << i << ' ';
return out;
}
std::ostream& operator<<(std::ostream& dest, const int128& value) {
// https://stackoverflow.com/a/25115163/23881100
std::ostream::sentry s( dest );
if ( s ) {
uint128 tmp = value < 0 ? -value : value;
char buffer[ 128 ];
char* d = std::end( buffer );
do {
-- d;
*d = "0123456789"[ tmp % 10 ];
tmp /= 10;
} while ( tmp != 0 );
if ( value < 0 ) {
-- d;
*d = '-';
}
int len = std::end( buffer ) - d;
if ( dest.rdbuf()->sputn( d, len ) != len ) {
dest.setstate( std::ios_base::badbit );
}
}
return dest;
}
/* pops */
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
/* math */
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
void __exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1, y = 0;
return;
}
__exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll inverse(ll a, ll b) {
ll x, y;
__exgcd(a, b, x, y);
return mod(x, b);
}
vector<tuple<int, int, ll>> decompose(ll x) {
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;
}
/* string algorithms */
vector<int> calc_next(string t) { // pi function of t
int n = (int)t.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
if (t[i] == t[j]) j++;
pi[i] = j;
}
return pi;
}
vector<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> prev = {1, -1};
for (int i = 1; i < m; ++i) {
if (z[i - prev.first] + i <= prev.second) {
z.push_back(z[i - prev.first]);
} else {
int j = max(i, prev.second + 1);
while (j < m && t[j] == t[j - i]) ++j;
z.push_back(j - i);
prev = {i, j - 1};
}
}
return z;
}
vector<int> kmp(string s, string t) { // find all t in s
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> lps = calc_next(cur);
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
}
return v;
}
int period(string s) { // find the length of shortest recurring period
int n = s.length();
auto z = calc_z(s);
for (int i = 1; i <= n / 2; ++i) {
if (n % i == 0 && z[i] == n - i) {
return i;
}
}
return n;
}
/////////////////////////////////////////////////////////
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
void dump() {}
void dump_ignore() {}
void prep() {}
void solve() {
read(int, n);
vector<int> a(n + 2);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
a[n + 1] = INF;
vector<vector<int>> dp(n + 2, vector<int>(n + 1, INF));
dp[0][0] = 0;
for (int i = 1; i <= n + 1; ++i) {
if (a[i] > a[i - 1]) {
for (int k = 0; k <= n; ++k) {
dp[i][k] = min(dp[i][k], dp[i - 1][k]);
}
}
for (int j = i - 1; ~j; --j) {
for (int k = 0; k < n; ++k) {
if (a[i] > a[j]) {
dp[i][k + 1] = min(dp[i][k + 1], dp[j][k] + i - j - 1);
}
}
}
}
int res = INF;
for (int i = 1; i <= n; ++i) {
res = min(res, dp[n + 1][i]);
cout << res << " \n"[i == n];
}
}
int main() {
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie, cout.tie(NULL);
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t < (DUMP_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

View File

@ -292,197 +292,86 @@ void dump() {}
void dump_ignore() {} void dump_ignore() {}
void prep() {} template <ll mdl> struct MLL {
ll val;
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree { MLL(ll v = 0) : val(mod(v, mdl)) {}
private: friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
using size_type = uint64_t; friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
using info_type = Addable_Info_t; friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
using tag_type = Tag_t; friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
size_type _max; friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
vector<info_type> d; friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
vector<tag_type> b; friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
void pull(size_type p) { void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
d[p] = d[p * 2] + d[p * 2 + 1]; void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
} void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
void push(size_type p, size_type left_len, size_type right_len) {
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
b[p] = tag_type();
}
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
if (s == t) {
d[p] = c;
return;
}
size_type m = s + (t - s >> 1);
if (s != t) push(p, m - s + 1, t - m);
if (x <= m) set(s, m, p * 2, x, c);
else set(m + 1, t, p * 2 + 1, x, c);
d[p] = d[p * 2] + d[p * 2 + 1];
}
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
if (l <= s && t <= r) {
d[p].apply(c, t - s + 1);
b[p].apply(c);
return;
}
size_type m = s + (t - s >> 1);
push(p, m - s + 1, t - m);
if (l <= m) range_apply(s, m, p * 2, l, r, c);
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
pull(p);
}
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
if (l <= s && t <= r) {
return d[p];
}
size_type m = s + (t - s >> 1);
info_type res = {};
push(p, m - s + 1, t - m);
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
return res;
}
void build(const Sequence& a, size_type s, size_type t, size_type p) {
if (s == t) {
d[p] = a[s];
return;
}
int m = s + (t - s >> 1);
build(a, s, m, p * 2);
build(a, m + 1, t, p * 2 + 1);
pull(p);
}
public:
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
segtree(const Sequence& a) : segtree(a.size()) {
build(a, {}, _max, 1);
}
void set(size_type i, const info_type& c) {
set({}, _max, 1, i, c);
}
void range_apply(size_type l, size_type r, const tag_type& c) {
range_apply({}, _max, 1, l, r, c);
}
void apply(size_type i, const tag_type& c) {
range_apply(i, i, c);
}
info_type range_query(size_type l, size_type r) {
return range_query({}, _max, 1, l, r);
}
info_type query(size_type i) {
return range_query(i, i);
}
Sequence serialize() {
Sequence res = {};
for (size_type i = 0; i <= _max; ++i) {
res.push_back(query(i));
}
return res;
}
const vector<info_type>& get_d() {
return d;
}
}; };
struct Tag { template <ll mdl>
int val = 0; ostream& operator<<(ostream& out, const MLL<mdl>& num) {
void apply(const Tag& rhs) { return out << num.val;
val += rhs.val; }
}
};
struct Info { template <ll mdl>
int val = INT_MAX; istream& operator>>(istream& in, MLL<mdl>& num) {
void apply(const Tag& rhs, size_t len) { return in >> num.val;
val += rhs.val; }
}
};
Info operator+(const Info &a, const Info &b) { constexpr int MAXN = 6e5 + 10;
return {min(a.val, b.val)}; MLL<PRIME> fact[MAXN], pw[MAXN];
void prep() {
fact[0] = 1, pw[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = fact[i - 1] * i;
pw[i] = pw[i - 1] * 2;
}
} }
void solve() { void solve() {
unordered_map<char, int> mp = {{'(', 1}, {')', -1}}; read(int, k);
read(int, n, q); int n = 1 << k;
read(string, s); vector<int> a(n, -1);
int curr = 0;
segtree<Info, Tag> ps(n);
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
curr += mp[s[i]]; read(int, x);
ps.set(i, { curr }); if (x == -1) {
} ;;
set<int> st1, st2;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == '(' && s[i + 1] == '(') {
st1.emplace(i);
}
if (s[i] == ')' and s[i + 1] == ')') {
st2.emplace(i);
}
}
while (q--) {
read(int, i), --i;
if (s[i] == '(') {
if (i && st1.count(i - 1)) st1.erase(i - 1);
if (i + 1 < n && st1.count(i)) st1.erase(i);
if (i and s[i - 1] == ')') st2.emplace(i - 1);
if (i + 1 < n and s[i + 1] == ')') st2.emplace(i);
s[i] = ')';
ps.range_apply(i, n - 1, {-2});
} else { } else {
if (i && s[i - 1] == '(') st1.emplace(i - 1); a[x - 1] = i;
if (i + 1 < n && s[i + 1] == '(') st1.emplace(i);
if (i and st2.count(i - 1)) st2.erase(i - 1);
if (i + 1 < n and st2.count(i)) st2.erase(i);
s[i] = '(';
ps.range_apply(i, n - 1, {2});
} }
// debug(s);
int f = ps.query(n - 1).val % 2 == 0;
int j = st1.size() ? *st1.begin() - 1 : n - 1;
if (j >= 0 and ps.range_query(0, j).val < 0) {
f = 0;
}
j = st2.size() ? *st2.rbegin() + 2 : 0;
if (j <= n - 1 and ps.query(n - 1).val - (j ? ps.query(j - 1).val : 0) > 0) {
f = 0;
}
if (f) {
cout << "YES\n";
} else {
cout << "NO\n";
}
// if (st.size()) {
// int j = *st.begin();
// if (j == 0 or ps.range_query(0, j - 1).val >= 0) {
// cout << "YES\n";
// } else {
// cout << "NO\n";
// }
// } else {
// if (ps.range_query(0, n - 1).val >= 0) {
// cout << "YES\n";
// } else {
// cout << "NO\n";
// }
// }
} }
MLL<PRIME> res = 1;
for (int i = 0; i < k; ++i) {
int tot = 1 << k - i;
unordered_set<int, safe_hash> left, right;
int cnt = 0;
for (int j = 0; j < tot / 2; ++j) {
if (a[j] == -1) continue;
if (left.count(a[j] >> i) or left.count((a[j] >> i) ^ 1)) {
cout << 0 << '\n';
return;
}
left.emplace(a[j] >> i);
}
for (int j = tot / 2; j < tot; ++j) {
if (a[j] == -1) continue;
if (left.count(a[j] >> i) or right.count(a[j] >> i) or right.count((a[j] >> i) ^ 1)) {
cout << 0 << '\n';
return;
}
right.emplace(a[j] >> i);
cnt += 1;
}
int to_swap = tot / 2;
for (int j = 0; j < tot / 2; ++j) {
if (left.count(j << 1) or left.count((j << 1) + 1) or right.count(j << 1) or right.count((j << 1) + 1)) {
to_swap -= 1;
}
}
res = (res * fact[tot / 2 - cnt]) * pw[to_swap];
}
cout << res << '\n';
} }
int main() { int main() {

View File

@ -1,4 +1,2 @@
3 2
(()
2
3 3
-1 7 -1 8 2 -1 -1 5