QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#290093 | #6344. The Best Problem of 2021 | ucup-team087# | AC ✓ | 139ms | 82048kb | C++20 | 27.9kb | 2023-12-24 13:09:05 | 2023-12-24 13:09:05 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define FOR_subset(t, s) \
for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T, typename U>
T ceil(T x, U y) {
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sum = 0;
for (auto &&a: A) sum += a;
return sum;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
assert(!que.empty());
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
assert(!que.empty());
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>
namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
template <class T>
static auto check(T &&x) -> decltype(x.write(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};
struct has_read_impl {
template <class T>
static auto check(T &&x) -> decltype(x.read(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};
struct Scanner {
FILE *fp;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) {
bool sep = false;
for (size_t i = st; i < ed; i++) {
if (isspace(line[i])) {
sep = true;
break;
}
}
if (!sep) reread();
}
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
if (neg) ref = -ref;
return true;
}
template <typename T,
typename enable_if<has_read<T>::value>::type * = nullptr>
inline bool read_single(T &x) {
x.read();
return true;
}
bool read_single(double &ref) {
string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
bool read_single(char &ref) {
string s;
if (!read_single(s) || s.size() != 1) return false;
ref = s[0];
return true;
}
template <class T>
bool read_single(vector<T> &ref) {
for (auto &d: ref) {
if (!read_single(d)) return false;
}
return true;
}
template <class T, class U>
bool read_single(pair<T, U> &p) {
return (read_single(p.first) && read_single(p.second));
}
template <size_t N = 0, typename T>
void read_single_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
read_single(x);
read_single_tuple<N + 1>(t);
}
}
template <class... T>
bool read_single(tuple<T...> &tpl) {
read_single_tuple(tpl);
return true;
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE *fp) : fp(fp) {}
};
struct Printer {
Printer(FILE *_fp) : fp(_fp) {}
~Printer() { flush(); }
static constexpr size_t SIZE = 1 << 15;
FILE *fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write(const char val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write('0');
return;
}
if (val < 0) {
write('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char(0x30 | (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
pos += len;
}
void write(const string s) {
for (char c: s) write(c);
}
void write(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write(s[i]);
}
void write(const double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
void write(const long double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
template <typename T,
typename enable_if<has_write<T>::value>::type * = nullptr>
inline void write(T x) {
x.write();
}
template <class T>
void write(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
template <class T, class U>
void write(const pair<T, U> val) {
write(val.first);
write(' ');
write(val.second);
}
template <size_t N = 0, typename T>
void write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { write(' '); }
const auto x = std::get<N>(t);
write(x);
write_tuple<N + 1>(t);
}
}
template <class... T>
bool write(tuple<T...> tpl) {
write_tuple(tpl);
return true;
}
template <class T, size_t S>
void write(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
void write(i128 val) {
string s;
bool negative = 0;
if (val < 0) {
negative = 1;
val = -val;
}
while (val) {
s += '0' + int(val % 10);
val /= 10;
}
if (negative) s += "-";
reverse(all(s));
if (len(s) == 0) s = "0";
write(s);
}
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
printer.write(head);
if (sizeof...(Tail)) printer.write(' ');
print(forward<Tail>(tail)...);
}
void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
scanner.read(head);
read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"
#line 2 "library/mod/modint_common.hpp"
struct has_mod_impl {
template <class T>
static auto check(T &&x) -> decltype(x.get_mod(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {};
template <typename mint>
mint inv(int n) {
static const int mod = mint::get_mod();
static vector<mint> dat = {0, 1};
assert(0 <= n);
if (n >= mod) n %= mod;
while (len(dat) <= n) {
int k = len(dat);
int q = (mod + k - 1) / k;
dat.eb(dat[k * q - mod] * mint(q));
}
return dat[n];
}
template <typename mint>
mint fact(int n) {
static const int mod = mint::get_mod();
assert(0 <= n);
if (n >= mod) return 0;
static vector<mint> dat = {1, 1};
while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint(len(dat)));
return dat[n];
}
template <typename mint>
mint fact_inv(int n) {
static vector<mint> dat = {1, 1};
if (n < 0) return mint(0);
while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
return dat[n];
}
template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
return (mint(1) * ... * fact_inv<mint>(xs));
}
template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}
template <typename mint>
mint C_dense(int n, int k) {
static vvc<mint> C;
static int H = 0, W = 0;
auto calc = [&](int i, int j) -> mint {
if (i == 0) return (j == 0 ? mint(1) : mint(0));
return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
};
if (W <= k) {
FOR(i, H) {
C[i].resize(k + 1);
FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
}
W = k + 1;
}
if (H <= n) {
C.resize(n + 1);
FOR(i, H, n + 1) {
C[i].resize(W);
FOR(j, W) { C[i][j] = calc(i, j); }
}
H = n + 1;
}
return C[n][k];
}
template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
assert(n >= 0);
if (k < 0 || n < k) return 0;
if (dense) return C_dense<mint>(n, k);
if (!large) return multinomial<mint>(n, k, n - k);
k = min(k, n - k);
mint x(1);
FOR(i, k) x *= mint(n - i);
return x * fact_inv<mint>(k);
}
template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
assert(n >= 0);
assert(0 <= k && k <= n);
if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
return mint(1) / C<mint, 1>(n, k);
}
// [x^d] (1-x) ^ {-n} の計算
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
assert(n >= 0);
if (d < 0) return mint(0);
if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "library/mod/modint.hpp"
template <int mod>
struct modint {
static_assert(mod < (1 << 30));
int val;
constexpr modint(const ll val = 0) noexcept
: val(val >= 0 ? val % mod : (mod - (-val) % mod) % mod) {}
bool operator<(const modint &other) const {
return val < other.val;
} // To use std::map
modint &operator+=(const modint &p) {
if ((val += p.val) >= mod) val -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((val += mod - p.val) >= mod) val -= mod;
return *this;
}
modint &operator*=(const modint &p) {
val = (int)(1LL * val * p.val % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-val); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return val == p.val; }
bool operator!=(const modint &p) const { return val != p.val; }
modint inverse() const {
int a = val, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b), swap(u -= t * v, v);
}
return modint(u);
}
modint pow(ll n) const {
assert(n >= 0);
modint ret(1), mul(val);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
#ifdef FASTIO
void write() { fastio::printer.write(val); }
void read() { fastio::scanner.read(val); }
#endif
static constexpr int get_mod() { return mod; }
// (n, r), r は 1 の 2^n 乗根
static constexpr pair<int, int> ntt_info() {
if (mod == 167772161) return {25, 17};
if (mod == 469762049) return {26, 30};
if (mod == 754974721) return {24, 362};
if (mod == 880803841) return {23, 211};
if (mod == 998244353) return {23, 31};
if (mod == 1045430273) return {20, 363};
if (mod == 1051721729) return {20, 330};
if (mod == 1053818881) return {20, 2789};
return {-1, -1};
}
static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};
using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "library/nt/primetable.hpp"
template <typename T = long long>
vc<T> primetable(int LIM) {
++LIM;
const int S = 32768;
static int done = 2;
static vc<T> primes = {2}, sieve(S + 1);
if (done < LIM) {
done = LIM;
primes = {2}, sieve.assign(S + 1, 0);
const int R = LIM / 2;
primes.reserve(int(LIM / log(LIM) * 1.1));
vc<pair<int, int>> cp;
for (int i = 3; i <= S; i += 2) {
if (!sieve[i]) {
cp.eb(i, i * i / 2);
for (int j = i * i; j <= S; j += 2 * i) sieve[j] = 1;
}
}
for (int L = 1; L <= R; L += S) {
array<bool, S> block{};
for (auto& [p, idx]: cp)
for (int i = idx; i < S + L; idx = (i += p)) block[i - L] = 1;
FOR(i, min(S, R - L)) if (!block[i]) primes.eb((L + i) * 2 + 1);
}
}
int k = LB(primes, LIM + 1);
return {primes.begin(), primes.begin() + k};
}
#line 3 "library/mod/powertable.hpp"
// a^0, ..., a^N
template <typename mint>
vc<mint> powertable_1(mint a, ll N) {
// table of a^i
vc<mint> f(N + 1, 1);
FOR(i, N) f[i + 1] = a * f[i];
return f;
}
// 0^e, ..., N^e
template <typename mint>
vc<mint> powertable_2(ll e, ll N) {
auto primes = primetable(N);
vc<mint> f(N + 1, 1);
f[0] = mint(0).pow(e);
for (auto&& p: primes) {
if (p > N) break;
mint xp = mint(p).pow(e);
ll pp = p;
while (pp <= N) {
ll i = pp;
while (i <= N) {
f[i] *= xp;
i += pp;
}
pp *= p;
}
}
return f;
}
#line 2 "library/linalg/xor/transpose.hpp"
// n x m 行列の transpose。O((n+m)log(n+m)) 時間。
// https://github.com/dsnet/matrix-transpose
template <typename UINT>
vc<UINT> transpose(int n, int m, vc<UINT>& A, bool keep_A = 1) {
assert(max(n, m) <= numeric_limits<UINT>::digits);
assert(len(A) == n);
vc<UINT> tmp;
if (keep_A) tmp = A;
int LOG = 0;
while ((1 << LOG) < max(n, m)) ++LOG;
A.resize(1 << LOG);
int width = 1 << LOG;
UINT mask = 1;
FOR(i, LOG) mask = mask | (mask << (1 << i));
FOR(t, LOG) {
width >>= 1;
mask = mask ^ (mask >> width);
FOR(i, 1 << t) {
FOR(j, width) {
UINT* x = &A[width * (2 * i + 0) + j];
UINT* y = &A[width * (2 * i + 1) + j];
*x = ((*y << width) & mask) ^ *x;
*y = ((*x & mask) >> width) ^ *y;
*x = ((*y << width) & mask) ^ *x;
}
}
}
A.resize(m);
if (!keep_A) return A;
swap(A, tmp);
return tmp;
}
#line 2 "library/linalg/xor/vector_space.hpp"
template <typename UINT>
struct Vector_Space {
#define SP Vector_Space
vc<UINT> dat;
Vector_Space() {}
Vector_Space(vc<UINT> dat, bool is_reduced = false) : dat(dat) {
if (!is_reduced) reduce();
}
int size() { return dat.size(); }
bool add_element(UINT v) {
for (auto&& e: dat) {
if (e == 0 || v == 0) break;
chmin(v, v ^ e);
}
if (v) {
dat.eb(v);
return true;
}
return false;
}
bool contain(UINT v) {
for (auto&& w: dat) {
if (v == 0) break;
chmin(v, v ^ w);
}
return v == 0;
}
UINT get_max(UINT xor_val = 0) {
UINT res = xor_val;
for (auto&& x: dat) chmax(res, res ^ x);
return res;
}
UINT get_min(UINT xor_val) {
UINT res = xor_val;
for (auto&& x: dat) chmin(res, res ^ x);
return res;
}
static SP merge(SP x, SP y) {
if (len(x) < len(y)) swap(x, y);
for (auto v: y.dat) { x.add_element(v); }
return x;
}
static SP intersection(SP& x, SP& y, int max_dim) {
SP xx = x.orthogonal_space(max_dim);
SP yy = y.orthogonal_space(max_dim);
xx = merge(xx, yy);
return xx.orthogonal_space(max_dim);
}
SP orthogonal_space(int max_dim) {
normalize();
int m = max_dim;
// pivot[k] == k となるように行の順番を変える
vc<u64> tmp(m);
FOR(i, len(dat)) tmp[topbit(dat[i])] = dat[i];
tmp = transpose(m, m, tmp, 0);
SP res;
FOR(j, m) {
if (tmp[j] >> j & 1) continue;
res.add_element(tmp[j] | UINT(1) << j);
}
return res;
}
void normalize(bool dec = true) {
int n = len(dat);
// 三角化
FOR(j, n) FOR(i, j) chmin(dat[i], dat[i] ^ dat[j]);
sort(all(dat));
if (dec) reverse(all(dat));
}
private:
void reduce() {
SP y;
for (auto&& e: dat) y.add_element(e);
(*this) = y;
}
#undef SP
};
#line 7 "main.cpp"
using BS = bitset<2000>;
using mint = modint998;
void out(BS b, int n) {
string x;
FOR(i, n) x += (b[i] ? '1' : '0');
print(x);
}
void solve() {
LL(N, M);
auto get = [&]() -> BS {
BS x;
STR(S);
FOR(i, M) x[i] = (S[i] == '1');
return x;
};
vc<BS> dat(N);
FOR(i, N) dat[i] = get();
BS Y = get();
// dat を基本変形
int rk = 0;
FOR(j, M) {
if (rk == N) break;
FOR(i, rk, N) if (dat[i][j]) {
if (i != rk) { swap(dat[rk], dat[i]); }
break;
}
if (!dat[rk][j]) continue;
FOR(i, N) if (i != rk) {
if (dat[i][j]) { dat[i] ^= dat[rk]; }
}
++rk;
}
if (rk != N) return print(0);
BS X;
BS now;
FOR(i, N) {
// Y >= dat[i] xor now なら使う
BS now1 = dat[i] ^ now;
bool ok = 1;
FOR(j, M) if (Y[j] != now1[j]) {
if (Y[j]) break;
ok = 0;
break;
}
if (ok) {
X[i] = 1;
now = now1;
}
}
if (!X[0]) return print(0);
// out(X, N);
vc<mint> POW = powertable_1<mint>(2, N * N + 100);
vc<mint> POW2 = {2};
FOR(i, N + 10) POW2.eb(POW2.back() * POW2.back());
/*
1**0***0**0*
0001***0**0*
00000001**0*
00000000001*
右下隅が (N-1,N=1) になるように基底を配置
*/
/*
そのマスを埋めた時点で
いま見ている行の基底を選ぶかどうか:
・不採用
・採用
・未定だが将来不採用になるもの
・未定だが将来採用になるもの
不採用 / 採用のとき:選ぶと決めたものの xor が small であることは確定済
未定のとき:xor はここまで X と同じ
状態 → 答への寄与
*/
using ARR = array<mint, 4>;
vv(ARR, dp, N, N);
dp[N - 1][N - 1][0] = 1;
dp[N - 1][N - 1][1] = 1;
dp[N - 1][N - 1][2] = 0; // 不採用にならなかった
dp[N - 1][N - 1][3] = 1;
FOR_R(i, N) FOR_R(j, i + 1) {
if (j == N - 1) continue;
dp[i][j] = {mint(0), mint(0), mint(0), mint(0)};
if (!X[j + 1]) {
// asterisk を 0 でとる
dp[i][j][0] += dp[i][j + 1][0];
dp[i][j][1] += dp[i][j + 1][1];
dp[i][j][2] += dp[i][j + 1][2];
dp[i][j][3] += dp[i][j + 1][3];
// asterisk を 1 でとる
// 未定 → 不採用が確定
dp[i][j][0] += dp[i][j + 1][0];
dp[i][j][1] += dp[i][j + 1][1];
dp[i][j][2] += dp[i][j + 1][0];
dp[i][j][3] += mint(0);
if (i + 1 < N) {
// add new basis
// すでに small になっているならば、採用確定
// いま持っている基底の右側を埋める / 採用した係数をかける
dp[i][j][0] += dp[i + 1][j + 1][1] * POW[i - j] * POW2[N - 2 - i];
dp[i][j][1] += dp[i + 1][j + 1][1] * POW[i - j] * POW2[N - 2 - i];
// いま持っている基底の採用が未定のとき
// いまの基底が将来的に不採用なら、次の基底は確定採用
// 次の基底の右側は埋めてしまう
dp[i][j][2] += dp[i + 1][j + 1][2] * POW[i - j] * POW2[N - 2 - i];
// いまの基底が将来的に採用なら、次の基底は不採用確定
// 次の基底の右側は埋めてしまう
dp[i][j][3] += dp[i + 1][j + 1][3] * POW[i - j];
}
}
if (X[j + 1]) {
// asterisk を 0 でとる
// 未定 → 採用が確定
dp[i][j][0] += dp[i][j + 1][0];
dp[i][j][1] += dp[i][j + 1][1];
dp[i][j][2] += mint(0);
dp[i][j][3] += dp[i][j + 1][1];
// asterisk を 1 でとる
dp[i][j][0] += dp[i][j + 1][0];
dp[i][j][1] += dp[i][j + 1][1];
dp[i][j][2] += dp[i][j + 1][2];
dp[i][j][3] += dp[i][j + 1][3];
if (i + 1 < N) {
// add new basis
// すでに small になっているならば、採用確定
// いま持っている基底の右側を埋める / 採用した係数をかける
dp[i][j][0] += dp[i + 1][j + 1][1] * POW[i - j] * POW2[N - 2 - i];
dp[i][j][1] += dp[i + 1][j + 1][1] * POW[i - j] * POW2[N - 2 - i];
// いま持っている基底の採用が未定のとき
// この基底は採用確定、この基底の右側は埋めてしまう
// 次の基底の採用判断は保留
dp[i][j][3] += dp[i + 1][j + 1][2] * POW[i - j];
dp[i][j][3] += dp[i + 1][j + 1][3] * POW[i - j] * POW2[N - 2 - i];
}
}
}
vc<mint> F(N + 1);
F[0] = mint(1);
FOR(i, N) FOR(j, i + 1) {
int dim = N - i;
mint x = 0;
if (j == 0) {
x = dp[i][j][2] + dp[i][j][3] * POW2[N - 1 - i];
} else {
x += dp[i][j][1] * POW2[N - 1 - i];
}
F[dim] += x;
}
mint ANS = 0;
FOR(i, N + 1) {
mint cf = POW[(N - i) * (N - i - 1) / 2];
if ((N - i) % 2 == 1) cf = -cf;
ANS += cf * F[i];
}
print(ANS);
}
signed main() {
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3864kb
input:
4 4 0001 0010 0100 1000 1101
output:
7364
result:
ok 1 number(s): "7364"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
3 2 00 00 00 11
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
2 3 110 101 101
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
3 10 1111100110 0011110100 0101100001 1110000001
output:
38
result:
ok 1 number(s): "38"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
7 13 1000101001000 1000000010000 1010101011111 1001100100111 1111111101100 0101010101110 1101100010100 1000010011001
output:
744450298
result:
ok 1 number(s): "744450298"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3748kb
input:
100 100 1111010111010001011111110010101001001101000000000000011000001100101000100011100011000000110000001010 1001001110111010100100010111100010111110101100101000010111001011111010111100111000000011101010100111 000011010111000100110100010010011101001111100110111000100101010001101100101011000111101101...
output:
19562313
result:
ok 1 number(s): "19562313"
Test #7:
score: 0
Accepted
time: 7ms
memory: 6500kb
input:
400 500 1011011011010010111110101001010011000100001111000111111111001111100010101011110011010010011100100100111111000111001110111100101010000000100100011011011001011100100000000100001100001010100010111000110011000100101001010110110100110101000011011011011100111110010100101000011001100000001100001000...
output:
681985268
result:
ok 1 number(s): "681985268"
Test #8:
score: 0
Accepted
time: 41ms
memory: 23124kb
input:
999 1997 011101110101100100111101100000000100001110010001010100011010111010101101011100001000010001110100110111101101010111010011101111011001011100110110101011111011000111101011011000010101100101001110000111010101111100000100100101110000111001010101110110000001111111100110110011110100101000011100011...
output:
435150194
result:
ok 1 number(s): "435150194"
Test #9:
score: 0
Accepted
time: 115ms
memory: 74264kb
input:
1901 2000 10000111000111010000000000100110001100110010011001101110001011000001011000010111101110111001111000010110110100010100010101011111011101100111010101010001010010111010001011000001011010100011000101101010001110111100000101110110011001101111101111000100001010011101011110001100110001100000111110...
output:
9254020
result:
ok 1 number(s): "9254020"
Test #10:
score: 0
Accepted
time: 127ms
memory: 80584kb
input:
1984 2000 11111101001011101001011011010011000001100000101000001001111000100010011011000110110110100000001100000000001111101001111010111110000000010000000111111001010111101101110000111110010111001011011111010010110001011100110101000110000100010100100101010111101100000011110010010100101011101001110110...
output:
870006511
result:
ok 1 number(s): "870006511"
Test #11:
score: 0
Accepted
time: 51ms
memory: 3728kb
input:
2000 2000 00010001100000101100000110010101010101110010001000000100010010110010001100110000001110100111010110100110101010101111011100001110100011001000010001000011100111010100110101000111010010010111001001101100100000101001111111001111101001000101001011101001010010010101011110111001101101101001001000...
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
11 11 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 10111110000
output:
312889397
result:
ok 1 number(s): "312889397"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
100 100 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
554554135
result:
ok 1 number(s): "554554135"
Test #14:
score: 0
Accepted
time: 22ms
memory: 23068kb
input:
1000 1000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
944188930
result:
ok 1 number(s): "944188930"
Test #15:
score: 0
Accepted
time: 94ms
memory: 81792kb
input:
1999 1999 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
753324940
result:
ok 1 number(s): "753324940"
Test #16:
score: 0
Accepted
time: 95ms
memory: 81872kb
input:
2000 2000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
489943678
result:
ok 1 number(s): "489943678"
Test #17:
score: 0
Accepted
time: 96ms
memory: 82048kb
input:
2000 2000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
458543942
result:
ok 1 number(s): "458543942"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
37 100 0000000000100000010101011000010111111000000000000000000000000000000000001110100110101000000010110000 0111100011011110101011110100101001011000000000110010001010110100000000010000100000111011000000100000 0001110011110000001100011000010011001000000000000000000000000000000000000000000000000000010...
output:
807297668
result:
ok 1 number(s): "807297668"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
71 93 100010100111111001011100000000011001101101010001011001110001101110010000001000001000000000011 110100111110010001000110000101111010111111000111011010100001010000010110011000000100000000000 110010010110000000010001010100000011000100000010011100100000100100101100010100001100000010000 000101010010...
output:
50935767
result:
ok 1 number(s): "50935767"
Test #20:
score: 0
Accepted
time: 1ms
memory: 3724kb
input:
101 114 010101111101011101100001000100001001000100011100111111110010001111101001100100000110100101010110000000000000001000 101010000011100100000001100000110000111001111011000010010101001011110110100101011101111111111110111010000000000000 11011100100101010100101100000101100000010100000010010110101010...
output:
0
result:
ok 1 number(s): "0"
Test #21:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
17 2000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010010101111100001010111000001111010110101100011000011011111110001011001011010111110111110000110011101111011010110101011010100110001111110110110101000101110100110011001000010000001010...
output:
526829746
result:
ok 1 number(s): "526829746"
Test #22:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
30 1999 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
708057099
result:
ok 1 number(s): "708057099"
Test #23:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
54 2000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
0
result:
ok 1 number(s): "0"
Test #24:
score: 0
Accepted
time: 19ms
memory: 15664kb
input:
791 1999 101011000010100101111110010001001001000000000001111100001000101100011110001010010011111110111010100011111010001110000001100010000011001110110011011011110110101100010010110111011000101111010100101110110010100011100100011001100000000001000001010100000000100111011000101111100100001000011010001...
output:
64451741
result:
ok 1 number(s): "64451741"
Test #25:
score: 0
Accepted
time: 28ms
memory: 20876kb
input:
944 2000 110101011100010000001010011011000001001001101001001011101110100000001000000010101011111001101010110100011101110100110010110000011100011111000001011110100111011101110010100100110111001110110001101010011101100010100100000010110101000101111000101110011000111000111111101110001101101110111110001...
output:
996119909
result:
ok 1 number(s): "996119909"
Test #26:
score: 0
Accepted
time: 40ms
memory: 27456kb
input:
1102 1999 10001010010010110101001000110000010000000110101001010111100100000110001111111100111001000111000111110100101101010110111110110001111011110101111001101100101110001011100001100000000000111111111000111010000111100010011010100001001011110111011110100001000100111000100001000010111001111011110001...
output:
855516290
result:
ok 1 number(s): "855516290"
Test #27:
score: 0
Accepted
time: 48ms
memory: 3652kb
input:
1931 2000 01101111100011101110100101000110011110000111000001010001011111010110011001111110110010111000100010111101001100001100001100010101000011001000110001011101111001000111101011011100011110010100001111111000101111000110000000000010110101110001000001011111001000001001100110101100100010101010010000...
output:
0
result:
ok 1 number(s): "0"
Test #28:
score: 0
Accepted
time: 116ms
memory: 77644kb
input:
1944 1999 10000110101111110110111101101000111110100010000011101101000011101100110000110001110000100000100101011111100100111000100110011111110111011100100000111000101110011100101011000011101101010001111110110100101101001011010011111111011111001101010000101011110010010110110101110000000111010100111101...
output:
52786402
result:
ok 1 number(s): "52786402"
Test #29:
score: 0
Accepted
time: 50ms
memory: 3652kb
input:
1977 2000 10011110110110110011001100011011011110111110001001101000101110100111011100011101001100100101110101010001100011001110111011101101100010000010100010001011011011001111011010111011100010101010101111101100001000111011100010111000000101111110100001111011111111111111110101101001101001111100011100...
output:
0
result:
ok 1 number(s): "0"
Test #30:
score: 0
Accepted
time: 115ms
memory: 81808kb
input:
2000 2000 01110010111000111001110000110111011110010010111001000001100101101011000001010111111010001100110000101001001000000000011001101101010001010011010000111011111101001110110010110101110101100100000011110100100010110001110010101001000110100100001001001111000111001011110110101100101011011011101010...
output:
3964016
result:
ok 1 number(s): "3964016"
Test #31:
score: 0
Accepted
time: 50ms
memory: 3608kb
input:
1994 1999 00001100011011001011110111100001111001010110101100010010111001011000100111011110000010100111100001101101100000101111101111001001011001000111111011010110001111001100110000100001101001011001001011100101000101010000110001101000100110110010011010000001000110001010101101111100010110001010101000...
output:
0
result:
ok 1 number(s): "0"
Test #32:
score: 0
Accepted
time: 50ms
memory: 3644kb
input:
1997 2000 01111000101001101011000100101111110000011100111100101100111100110000100101010001011110111100101000001001000110100110111101011101010100010110101011010010000100000110011111100000100011000110001110000111011010110001001001000001110101011011011000101111010000010101101111110100110011110100101000...
output:
0
result:
ok 1 number(s): "0"
Test #33:
score: 0
Accepted
time: 139ms
memory: 81872kb
input:
2000 2000 00110111110110101110100001011000000101001011010011111001100000001001101100111111100001100010111101001110101001101010111011111000000110101101101110100111110101111110010000001001010100111101011010100000010001110100100010010100101010100110011101011110111100111110101110111010010111010010100010...
output:
385756366
result:
ok 1 number(s): "385756366"
Test #34:
score: 0
Accepted
time: 122ms
memory: 81252kb
input:
1988 1999 10101010110010100000101100110010001110101111010010001011010100100010101111001010100110100101100100111101000001000110001011110000001110000000110000001111000100101100000000111011011011110111101000001110111111000110011011111011010000010011100010010110111010000011010001011100010001011001100000...
output:
534584509
result:
ok 1 number(s): "534584509"
Test #35:
score: 0
Accepted
time: 53ms
memory: 3864kb
input:
2000 2000 00100000011001010111100000100001011101010011001010100100100001100000000100010110000110010111010000100110010000010001110100010100111001000110111001011101101110111000000111000000111101011010000110010011110011010000010101000111100100100001101001100010101101011111001010001011101001100001011010...
output:
0
result:
ok 1 number(s): "0"
Test #36:
score: 0
Accepted
time: 47ms
memory: 3856kb
input:
1995 2000 10010000010101100111011100110001101101000100010011110100110111110000000111010000111101011000101000111000010010010111000010101110110100011100111010110110110100111101010100000110001010010001011001000100011110101100110111010110101010100100011010101100001010111111000100101110001101000000010010...
output:
0
result:
ok 1 number(s): "0"
Test #37:
score: 0
Accepted
time: 135ms
memory: 81860kb
input:
1999 1999 11011110110011001100010110011001000101010101001001010101111111000111100000001101101101101010001111010000011101100110100111011010011011001101010011001100011111010100001000100100100111010111001000000010101100100010001111100100101110100010010010101000000110101011101001110010111111011101000101...
output:
678369480
result:
ok 1 number(s): "678369480"
Test #38:
score: 0
Accepted
time: 51ms
memory: 3736kb
input:
1999 2000 11000110010001011100111010011110110100101101000010111101001010100000100100011101011000110000101011011111110101001101111010010010010001011110101001010000010100110110100110011110011101001100000001110100001100110111011001110000110110000101110011110010110100110011010111011001011010111000000110...
output:
0
result:
ok 1 number(s): "0"