結果

問題 No.2907 Business Revealing Dora Tiles
ユーザー ecottea
提出日時 2024-10-02 01:50:02
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 21,091 bytes
コンパイル時間 29,093 ms
コンパイル使用メモリ 362,792 KB
最終ジャッジ日時 2025-02-24 14:26:30
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42 RE * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

// QCFium
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9e18int -2^31 2^31 = 2e9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = static_modint<100>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//
/*
* Nim_product() : O(65536)
*
*
* ull prod(ull x, ull y) : O(216)
* x y
*
* ull pow(ull x, ull n) : O(216 log n)
* n x
*
* ull inv(ull x) : O(216 * 64)
* x
*/
class Nim_product {
// : https://kyopro-friends.hatenablog.com/entry/2020/04/07/195850
// :ON NUMBERS AND GAMES(John H. Conway) (pp.52-53)
// p[i][j] : i j
vector<vector<ull>> p;
// a b a, b < 2^16
ull prod16(ull a, ull b) {
constexpr ull mask = (1ULL << 8) - 1;
ull ah = a >> 8, al = a & mask;
ull bh = b >> 8, bl = b & mask;
ull val = (p[ah][bh] ^ p[al][bh] ^ p[ah][bl]) << 8;
val ^= p[p[ah][bh]][1LL << 7];
val ^= p[al][bl];
return val;
}
// a b a, b < 2^32
ull prod32(ull a, ull b) {
constexpr ull mask = (1ULL << 16) - 1;
ull ah = a >> 16, al = a & mask;
ull bh = b >> 16, bl = b & mask;
ull val = (prod16(ah, bh) ^ prod16(al, bh) ^ prod16(ah, bl)) << 16;
val ^= prod16(prod16(ah, bh), 1ULL << 15);
val ^= prod16(al, bl);
return val;
}
// a b a, b < 2^64
ull prod64(ull a, ull b) {
constexpr ull mask = (1ULL << 32) - 1;
ull ah = a >> 32, al = a & mask;
ull bh = b >> 32, bl = b & mask;
ull val = (prod32(ah, bh) ^ prod32(al, bh) ^ prod32(ah, bl)) << 32;
val ^= prod32(prod32(ah, bh), 1ULL << 31);
val ^= prod32(al, bl);
return val;
}
public:
Nim_product() : p(256, vector<ull>(256)) {
// verify : https://judge.yosupo.jp/problem/nim_product_64
p[1][1] = 1;
// [0..256) [0..256)
int pow2 = 2;
rep(k, 3) {
int K = 1 << k;
repi(a, pow2, pow2 * pow2 - 1) rep(b, pow2 * pow2) {
int ah = a >> K, al = a & (pow2 - 1);
int bh = b >> K, bl = b & (pow2 - 1);
ull val = (p[ah][bh] ^ p[al][bh] ^ p[ah][bl]) << K;
val ^= p[p[ah][bh]][1LL << (K - 1)];
val ^= p[al][bl];
p[a][b] = val;
}
rep(a, pow2) repi(b, pow2, pow2 * pow2 - 1) p[a][b] = p[b][a];
pow2 *= pow2;
}
}
// x y
ull prod(ull x, ull y) {
// verify : https://judge.yosupo.jp/problem/nim_product_64
if (x < (1ULL << 8) && y < (1ULL << 8)) return p[x][y];
else if (x < (1ULL << 16) && y < (1ULL << 16)) return prod16(x, y);
else if (x < (1ULL << 32) && y < (1ULL << 32)) return prod32(x, y);
else return prod64(x, y);
}
// n x
ull pow(ull x, ull n) {
ull res = 1, pow2 = x;
while (n > 0) {
if ((n & 1) != 0) res = prod(res, pow2);
pow2 = prod(pow2, pow2);
n /= 2;
}
return res;
}
// x
ull inv(ull x) {
// verify : https://projecteuler.net/problem=459
Assert(x > 0);
if (x < (1ULL << 1)) return 1;
if (x < (1ULL << 2)) return 5ULL - x;
if (x < (1ULL << 4)) return pow(x, (1ULL << 4) - 2);
if (x < (1ULL << 8)) return pow(x, (1ULL << 8) - 2);
if (x < (1ULL << 16)) return pow(x, (1ULL << 16) - 2);
if (x < (1ULL << 32)) return pow(x, (1ULL << 32) - 2);
return pow(x, ~0ULL - 1);
}
};
Nim_product NP;
using SC01 = ull;
SC01 addC01(SC01 x, SC01 y) { return x ^ y; }
SC01 oC01() { return 0; }
SC01 miC01(SC01 x) { return x; }
SC01 mulC01(SC01 x, SC01 y) { return NP.prod(x, y); }
SC01 eC01() { return 1; }
SC01 invC01(SC01 x) { return NP.inv(x); }
#define NimAdd_NimMul_field SC01, addC01, oC01, miC01, mulC01, eC01, invC01
//
/*
* (S, add, o, mi, mul, e, inv) add, mi, mul +, -, *, /
*/
template <class S, S(*add)(S, S), S(*o_)(), S(*mi)(S), S(*mul)(S, S), S(*e_)(), S(*inv_)(S)>
struct Field {
S v;
//
static S o() { return o_(); }
//
static S e() { return e_(); }
//
Field() : v(o()) {}
Field(S v) : v(v) {}
//
operator S() const { return v; }
//
bool operator==(const Field& b) const { return v == b.v; }
bool operator!=(const Field& b) const { return v != b.v; }
//
Field operator-() const { return Field(mi(v)); }
Field inv() const { return Field(inv_(v)); }
//
Field& operator+=(const Field& b) { v = add(v, b.v); return *this; }
Field& operator-=(const Field& b) { v = add(v, mi(b.v)); return *this; }
Field& operator*=(const Field& b) { v = mul(v, b.v); return *this; }
Field& operator/=(const Field& b) { v = mul(v, inv_(b.v)); return *this; }
friend Field operator+(Field a, const Field& b) { a += b; return a; }
friend Field operator-(Field a, const Field& b) { a -= b; return a; }
friend Field operator*(Field a, const Field& b) { a *= b; return a; }
friend Field operator/(Field a, const Field& b) { a /= b; return a; }
//
friend istream& operator>>(istream& is, Field& a) { is >> a.v; return is; }
friend ostream& operator<<(ostream& os, const Field& a) {
//#ifdef _MSC_VER
// if (a.v == o()) return os << "o";
// if (a.v == e()) return os << "e";
//#endif
return os << a.v;
}
};
//
/*
* Matrix<T>(int n, int m) : O(n m)
* n×m
*
* Matrix<T>(int n) : O(n^2)
* n×n
*
* Matrix<T>(vvT a) : O(n m)
* a[0..n)[0..m)
*
* bool empty() : O(1)
*
*
* A + B : O(n m)
* n×m A, B += 使
*
* A - B : O(n m)
* n×m A, B -= 使
*
* c * A A * c : O(n m)
* n×m A c *= 使
*
* A * x : O(n m)
* n×m A n x
*
* x * A : O(n m)
* m x n×m A
*
* A * B : O(n m l)
* n×m A m×l B
*
* Mat pow(ll d) : O(n^3 log d)
* d
*/
template <class T>
struct Matrix {
int n, m; // n m
vector<vector<T>> v; //
// n×m
Matrix(int n, int m) : n(n), m(m), v(n, vector<T>(m)) {}
// n×n
Matrix(int n) : n(n), m(n), v(n, vector<T>(n)) { rep(i, n) v[i][i] = T(1); }
// a[0..n)[0..m)
Matrix(const vector<vector<T>>& a) : n(sz(a)), m(sz(a[0])), v(a) {}
Matrix() : n(0), m(0) {}
//
Matrix(const Matrix&) = default;
Matrix& operator=(const Matrix&) = default;
//
inline vector<T> const& operator[](int i) const { return v[i]; }
inline vector<T>& operator[](int i) {
// verify : https://judge.yosupo.jp/problem/matrix_product
// inline [] v[]
return v[i];
}
//
friend istream& operator>>(istream& is, Matrix& a) {
rep(i, a.n) rep(j, a.m) is >> a.v[i][j];
return is;
}
//
void push_back(const vector<T>& a) {
Assert(sz(a) == m);
v.push_back(a);
n++;
}
//
void pop_back() {
Assert(n > 0);
v.pop_back();
n--;
}
void resize(int n_) {
v.resize(n_);
n = n_;
}
//
bool empty() const { return min(n, m) == 0; }
//
bool operator==(const Matrix& b) const { return n == b.n && m == b.m && v == b.v; }
bool operator!=(const Matrix& b) const { return !(*this == b); }
//
Matrix& operator+=(const Matrix& b) {
rep(i, n) rep(j, m) v[i][j] += b[i][j];
return *this;
}
Matrix& operator-=(const Matrix& b) {
rep(i, n) rep(j, m) v[i][j] -= b[i][j];
return *this;
}
Matrix& operator*=(const T& c) {
rep(i, n) rep(j, m) v[i][j] *= c;
return *this;
}
Matrix operator+(const Matrix& b) const { return Matrix(*this) += b; }
Matrix operator-(const Matrix& b) const { return Matrix(*this) -= b; }
Matrix operator*(const T& c) const { return Matrix(*this) *= c; }
friend Matrix operator*(const T& c, const Matrix<T>& a) { return a * c; }
Matrix operator-() const { return Matrix(*this) *= T(-1); }
// : O(m n)
vector<T> operator*(const vector<T>& x) const {
vector<T> y(n);
rep(i, n) rep(j, m) y[i] += v[i][j] * x[j];
return y;
}
// : O(m n)
friend vector<T> operator*(const vector<T>& x, const Matrix& a) {
vector<T> y(a.m);
rep(i, a.n) rep(j, a.m) y[j] += x[i] * a[i][j];
return y;
}
// O(n^3)
Matrix operator*(const Matrix& b) const {
// verify : https://judge.yosupo.jp/problem/matrix_product
Matrix res(n, b.m);
rep(i, res.n) rep(k, m) rep(j, res.m) res[i][j] += v[i][k] * b[k][j];
return res;
}
Matrix& operator*=(const Matrix& b) { *this = *this * b; return *this; }
// O(n^3 log d)
Matrix pow(ll d) const {
// verify : https://judge.yosupo.jp/problem/pow_of_matrix
Matrix res(n), pow2 = *this;
while (d > 0) {
if (d & 1) res *= pow2;
pow2 *= pow2;
d >>= 1;
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Matrix& a) {
rep(i, a.n) {
os << "[";
rep(j, a.m) os << a[i][j] << " ]"[j == a.m - 1];
if (i < a.n - 1) os << "\n";
}
return os;
}
#endif
};
//O(n m min(n, m))
/*
* n×m A n b
* A x = b x0m
* A x = 0 m xs
*/
template <class T>
pair<vector<vector<T>>, vi> gauss_jordan_elimination1(Matrix<T>& A) {
// verify : https://judge.yosupo.jp/problem/system_of_linear_equations
int n = A.n, m = A.m;
// pivots[i] : i
vi pivots;
// v[i][j]
int i = 0, j = 0;
while (i < n && j < m) {
// 0
int i2 = i;
while (i2 < n && A[i2][j] == T(0)) i2++;
//
if (i2 == n) { j++; continue; }
// i
if (i != i2) swap(A[i], A[i2]);
// v[i][j]
pivots.push_back(j);
// v[i][j] 1 i v[i][j]
T vij_inv = T(1) / A[i][j];
repi(j2, j, m - 1) A[i][j2] *= vij_inv;
// i j 0 i
rep(i2, n) {
if (A[i2][j] == T(0) || i2 == i) continue;
T mul = A[i2][j];
if (mul != T(0)) {
repi(j2, j, m - 1) A[i2][j2] -= A[i][j2] * mul;
}
}
//
i++; j++;
}
int rnk = sz(pivots);
// A x = 0 {x} 1-hot
vector<vector<T>> xs;
i = 0;
rep(j, m) {
if (i < rnk && j == pivots[i]) {
i++;
continue;
}
vector<T> x(m);
x[j] = T(1);
rep(i2, i) x[pivots[i2]] = -A[i2][j];
xs.emplace_back(move(x));
}
return { xs, pivots };
}
//O(n m min(n, m))
/*
* n×m A n b
* A x = b x0m
* A x = 0 m xs
*/
template <class T>
int gauss_jordan_elimination2(Matrix<T>& A) {
// verify : https://judge.yosupo.jp/problem/system_of_linear_equations
int n = A.n, m = A.m;
// pivots[i] : i
vi pivots;
// v[i][j]
int i = 0, j = 0;
while (i < n && j < m) {
// 0
int i2 = i;
while (i2 < n && A[i2][j] == T(0)) i2++;
//
if (i2 == n) { j++; continue; }
// i
if (i != i2) swap(A[i], A[i2]);
// v[i][j]
pivots.push_back(j);
// v[i][j] 1 i v[i][j]
T vij_inv = T(1) / A[i][j];
repi(j2, j, m - 1) A[i][j2] *= vij_inv;
// i j 0 i
rep(i2, n) {
if (A[i2][j] == T(0) || i2 == i) continue;
T mul = A[i2][j];
if (mul != T(0)) {
repi(j2, j, m - 1) A[i2][j2] -= A[i][j2] * mul;
}
}
//
i++; j++;
}
int rnk = sz(pivots);
return rnk;
}
void TLE() {
using F = Field<NimAdd_NimMul_field>;
int n, t;
cin >> n >> t;
Matrix<F> mat(t, n);
rep(i, t) rep(j, n) {
ull x;
cin >> x;
x--;
mat[i][j] = x;
}
auto [xs, pivots] = gauss_jordan_elimination1(mat);
dumpel(xs); dump(pivots);
int h = sz(xs), w = sz(pivots);
dump(h, w);
vector<vector<F>> ys(h);
rep(i, h) {
repe(j, pivots) ys[i].push_back(F(xs[i][j]));
}
dumpel(ys);
mint res = 0;
mint pow2_64 = mint(2).pow(64);
vm pow_pow2_64(20);
pow_pow2_64[0] = 1;
rep(i, 19) pow_pow2_64[i + 1] = pow_pow2_64[i] * pow2_64;
// 0
repb(set_h, h) {
int pc_h = popcount(set_h);
repb(set_w, w) {
int pc_w = popcount(set_w);
Matrix<F> mat(pc_h, pc_w); int pt_h = 0;
repis(i, set_h) {
int pt_w = 0;
repis(j, set_w) mat[pt_h][pt_w++] = ys[i][j];
pt_h++;
}
int r = gauss_jordan_elimination2(mat);
// dump(pc_h, pc_w, r, pc_h - r);
res += ((h - pc_h + pc_w) & 1 ? -1 : 1) * pow_pow2_64[pc_h - r];
}
}
EXIT(res);
}
//O(h w)
/*
* a[0..h)[0..w)
*/
template <class T>
vector<vector<T>> transpose(const vector<vector<T>>& a) {
// verify : https://yukicoder.me/problems/no/1974
int h = sz(a), w = sz(a[0]);
vector<vector<T>> b(w, vector<T>(h));
rep(i, h) rep(j, w) b[j][i] = a[i][j];
return b;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
using F = Field<NimAdd_NimMul_field>;
mint pow2_64 = mint(2).pow(64);
vm pow_pow2_64(20);
pow_pow2_64[0] = 1;
rep(i, 19) pow_pow2_64[i + 1] = pow_pow2_64[i] * pow2_64;
int n, t;
cin >> n >> t;
Matrix<F> mat(t, n);
rep(i, t) rep(j, n) {
ull x;
cin >> x;
x--;
mat[i][j] = x;
}
auto [xs, pivots] = gauss_jordan_elimination1(mat);
dumpel(xs); dump(pivots);
int w = sz(xs);
xs = transpose(xs);
dumpel(xs);
int h = sz(xs);
dump(h, w);
mint res = 0;
vector<vector<vector<F>>> dp(1LL << h);
vector<vector<int>> piv(1LL << h);
// bitDP
repb(set, h) {
dump("set:", set);
if (set == 0) {
res += pow_pow2_64[w];
continue;
}
int mb = msb(set);
int pset = set ^ (1 << mb);
dp[set] = dp[pset];
piv[set] = piv[pset];
vector<F> v = xs[mb];
int D = sz(piv[set]);
rep(i, D) {
int j = piv[set][i];
F mul = v[j];
rep(j, w) v[j] -= dp[set][i][j] * mul;
}
rep(j, w) {
if (v[j] != F(0)) {
F v_inv = v[j].inv();
rep(j2, w) v[j2] *= v_inv;
dp[set].push_back(v);
piv[set].push_back(j);
D++;
break;
}
}
int pc = popcount(set);
res += (pc & 1 ? -1 : 1) * pow_pow2_64[w - D];
dump(dp[set]); dump(piv[set]);
}
// dumpel(dp); dumpel(piv);
EXIT(res);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0