結果
問題 | No.2443 特殊線形群の標準表現 |
ユーザー | Aeren |
提出日時 | 2023-08-25 21:42:24 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 193 ms / 3,000 ms |
コード長 | 19,755 bytes |
コンパイル時間 | 4,208 ms |
コンパイル使用メモリ | 367,784 KB |
実行使用メモリ | 8,944 KB |
最終ジャッジ日時 | 2024-06-06 16:05:14 |
合計ジャッジ時間 | 6,754 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 3 ms
6,940 KB |
testcase_14 | AC | 18 ms
6,940 KB |
testcase_15 | AC | 180 ms
8,840 KB |
testcase_16 | AC | 186 ms
8,836 KB |
testcase_17 | AC | 192 ms
8,804 KB |
testcase_18 | AC | 193 ms
8,740 KB |
testcase_19 | AC | 191 ms
8,944 KB |
testcase_20 | AC | 131 ms
8,728 KB |
ソースコード
#include <x86intrin.h> #include <bits/stdc++.h> using namespace std; #if __cplusplus > 201703L #include <ranges> using namespace numbers; #endif template<int id> struct modular_unfixed_base{ static unsigned int _mod; static unsigned long long _inverse_mod; static unsigned int &mod(){ return _mod; } static void precalc_barrett(){ _inverse_mod = (unsigned long long)-1 / _mod + 1; } static void setup(unsigned int mod = 0){ if(!mod) cin >> mod; _mod = mod; assert(_mod >= 1); precalc_barrett(); } template<class T> static vector<modular_unfixed_base> precalc_power(T base, int SZ){ vector<modular_unfixed_base> res(SZ + 1, 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base; return res; } static vector<modular_unfixed_base> _INV; static void precalc_inverse(int SZ){ if(_INV.empty()) _INV.assign(2, 1); for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]); } // _mod must be a prime static modular_unfixed_base _primitive_root; static modular_unfixed_base primitive_root(){ if(_primitive_root) return _primitive_root; if(_mod == 2) return _primitive_root = 1; if(_mod == 998244353) return _primitive_root = 3; unsigned int divs[20] = {}; divs[0] = 2; int cnt = 1; unsigned int x = (_mod - 1) / 2; while(x % 2 == 0) x /= 2; for(auto i = 3; 1LL * i * i <= x; i += 2){ if(x % i == 0){ divs[cnt ++] = i; while(x % i == 0) x /= i; } } if(x > 1) divs[cnt ++] = x; for(auto g = 2; ; ++ g){ bool ok = true; for(auto i = 0; i < cnt; ++ i){ if((modular_unfixed_base(g).power((_mod - 1) / divs[i])) == 1){ ok = false; break; } } if(ok) return _primitive_root = g; } } constexpr modular_unfixed_base(): data(){ } modular_unfixed_base(const double &x){ data = normalize(llround(x)); } modular_unfixed_base(const long double &x){ data = normalize(llround(x)); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base(const T &x){ data = normalize(x); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> static unsigned int normalize(const T &x){ if(_mod == 1) return 0; assert(_inverse_mod); int sign = x >= 0 ? 1 : -1; unsigned int v = _mod <= sign * x ? sign * x - ((__uint128_t)(sign * x) * _inverse_mod >> 64) * _mod : sign * x; if(v >= _mod) v += _mod; if(sign == -1 && v) v = _mod - v; return v; } const unsigned int &operator()() const{ return data; } template<class T> operator T() const{ return data; } modular_unfixed_base &operator+=(const modular_unfixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; } modular_unfixed_base &operator-=(const modular_unfixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base &operator+=(const T &otr){ return *this += modular_unfixed_base(otr); } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base &operator-=(const T &otr){ return *this -= modular_unfixed_base(otr); } modular_unfixed_base &operator++(){ return *this += 1; } modular_unfixed_base &operator--(){ return *this += _mod - 1; } modular_unfixed_base operator++(int){ modular_unfixed_base result(*this); *this += 1; return result; } modular_unfixed_base operator--(int){ modular_unfixed_base result(*this); *this += _mod - 1; return result; } modular_unfixed_base operator-() const{ return modular_unfixed_base(_mod - data); } modular_unfixed_base &operator*=(const modular_unfixed_base &rhs){ data = normalize((unsigned long long)data * rhs.data); return *this; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base &inplace_power(T e){ if(e < 0) *this = 1 / *this, e = -e; modular_unfixed_base res = 1; for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base power(T e) const{ return modular_unfixed_base(*this).inplace_power(e); } modular_unfixed_base &operator/=(const modular_unfixed_base &otr){ int a = otr.data, m = _mod, u = 0, v = 1; if(a < _INV.size()) return *this *= _INV[a]; while(a){ int t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return *this *= u; } unsigned int data; }; template<int id> unsigned int modular_unfixed_base<id>::_mod; template<int id> unsigned long long modular_unfixed_base<id>::_inverse_mod; template<int id> vector<modular_unfixed_base<id>> modular_unfixed_base<id>::_INV; template<int id> modular_unfixed_base<id> modular_unfixed_base<id>::_primitive_root; template<int id> bool operator==(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data == rhs.data; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(const modular_unfixed_base<id> &lhs, T rhs){ return lhs == modular_unfixed_base<id>(rhs); } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator==(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) == rhs; } template<int id> bool operator!=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return !(lhs == rhs); } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(const modular_unfixed_base<id> &lhs, T rhs){ return !(lhs == rhs); } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> bool operator!=(T lhs, const modular_unfixed_base<id> &rhs){ return !(lhs == rhs); } template<int id> bool operator<(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data < rhs.data; } template<int id> bool operator>(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data > rhs.data; } template<int id> bool operator<=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data <= rhs.data; } template<int id> bool operator>=(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return lhs.data >= rhs.data; } template<int id> modular_unfixed_base<id> operator+(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) += rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator+(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) += rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator+(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) += rhs; } template<int id> modular_unfixed_base<id> operator-(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) -= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator-(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) -= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator-(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) -= rhs; } template<int id> modular_unfixed_base<id> operator*(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) *= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator*(const modular_unfixed_base<id> &lhs, T rhs){ return modular_unfixed_base<id>(lhs) *= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator*(T lhs, const modular_unfixed_base<id> &rhs){ return modular_unfixed_base<id>(lhs) *= rhs; } template<int id> modular_unfixed_base<id> operator/(const modular_unfixed_base<id> &lhs, const modular_unfixed_base<id> &rhs) { return modular_unfixed_base<id>(lhs) /= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator/(const modular_unfixed_base<id> &lhs, T rhs) { return modular_unfixed_base<id>(lhs) /= rhs; } template<int id, class T, typename enable_if<is_integral<T>::value>::type* = nullptr> modular_unfixed_base<id> operator/(T lhs, const modular_unfixed_base<id> &rhs) { return modular_unfixed_base<id>(lhs) /= rhs; } template<int id> istream &operator>>(istream &in, modular_unfixed_base<id> &number){ long long x; in >> x; number.data = modular_unfixed_base<id>::normalize(x); return in; } // #define _PRINT_AS_FRACTION template<int id> ostream &operator<<(ostream &out, const modular_unfixed_base<id> &number){ #ifdef LOCAL #ifdef _PRINT_AS_FRACTION out << number(); cerr << "("; for(auto d = 1; ; ++ d){ if((number * d).data <= 1000000){ cerr << (number * d).data << "/" << d; break; } else if((-number * d).data <= 1000000){ cerr << "-" << (-number * d).data << "/" << d; break; } } cerr << ")"; return out; #else return out << number(); #endif #else return out << number(); #endif } #undef _PRINT_AS_FRACTION using modular = modular_unfixed_base<0>; // T must support +=, -=, *, *=, ==, and != template<class T, size_t N, size_t M> struct matrix_fixed{ using ring_t = T; using domain_t = array<T, M>; using range_t = array<T, N>; static constexpr int n = N, m = M; array<array<T, M>, N> data; array<T, M> &operator()(int i){ assert(0 <= i && i < n); return data[i]; } const array<T, M> &operator()(int i) const{ assert(0 <= i && i < n); return data[i]; } T &operator()(int i, int j){ assert(0 <= i && i < n && 0 <= j && j < m); return data[i][j]; } const T &operator()(int i, int j) const{ assert(0 <= i && i < n && 0 <= j && j < m); return data[i][j]; } bool operator==(const matrix_fixed &a) const{ assert(n == a.n && m == a.m); return data == a.data; } bool operator!=(const matrix_fixed &a) const{ assert(n == a.n && m == a.m); return data != a.data; } matrix_fixed &operator+=(const matrix_fixed &a){ assert(n == a.n && m == a.m); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] += a(i, j); return *this; } matrix_fixed operator+(const matrix_fixed &a) const{ assert(n == a.n && m == a.m); return matrix_fixed(*this) += a; } matrix_fixed &operator-=(const matrix_fixed &a){ assert(n == a.n && m == a.m); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] += a(i, j); return *this; } matrix_fixed operator-(const matrix_fixed &a) const{ assert(n == a.n && m == a.m); return matrix_fixed(*this) += a; } template<size_t N2, size_t M2> matrix_fixed<T, N, M2> operator*(const matrix_fixed<T, N2, M2> &a) const{ assert(m == a.n); int l = M2; matrix_fixed<T, N, M2> res; for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) for(auto k = 0; k < l; ++ k) res(i, k) += data[i][j] * a(j, k); return res; } template<size_t N2, size_t M2> matrix_fixed &operator*=(const matrix_fixed<T, N2, M2> &a){ return *this = *this * a; } matrix_fixed &operator*=(T c){ for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] *= c; return *this; } matrix_fixed operator*(T c) const{ return matrix_fixed(*this) *= c; } template<class U, typename enable_if<is_integral<U>::value>::type* = nullptr> matrix_fixed &inplace_power(U e){ assert(n == m && e >= 0); matrix_fixed res(1, 0); for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class U> matrix_fixed power(U e) const{ return matrix_fixed(*this).inplace_power(e); } matrix_fixed<T, M, N> transposed() const{ matrix_fixed<T, M, N> res; for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res(j, i) = data[i][j]; return res; } matrix_fixed &transpose(){ return *this = transposed(); } // Multiply a column vector v on the right range_t operator*(const domain_t &v) const{ range_t res; res.fill(T(0)); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res[i] += data[i][j] * v[j]; return res; } // Assumes T is a field // find_inverse() must return optional<T> // O(n) find_inverse() calls along with O(n^3) operations on T T determinant(auto find_inverse) const{ assert(n == m); if(n == 0) return T(1); auto a = data; T res = T(1); for(auto j = 0; j < n; ++ j){ int pivot = -1; for(auto i = j; i < n; ++ i) if(a[i][j] != T(0)){ pivot = i; break; } if(!~pivot) return T(0); swap(a[j], a[pivot]); res *= a[j][j] * (j != pivot ? -1 : 1); auto invp = find_inverse(a[j][j]); assert(invp); T inv = *invp; for(auto i = j + 1; i < n; ++ i) if(i != j && a[i][j] != T(0)){ T d = a[i][j] * inv; for(auto jj = j; jj < n; ++ jj) a[i][jj] -= d * a[j][jj]; } } return res; } // Assumes T is a field // find_inverse() must return optional<T> // O(n) find_inverse() calls along with O(n^3) operations on T optional<matrix_fixed> inverse(auto find_inverse) const{ assert(n == m); if(n == 0) return *this; auto a = data; array<array<T, M>, N> res; for(auto i = 0; i < n; ++ i) res[i].fill(T(0)), res[i][i] = T(1); for(auto j = 0; j < n; ++ j){ int pivot = -1; for(auto i = j; i < n; ++ i) if(a[i][j] != T(0)){ pivot = i; break; } if(!~pivot) return {}; swap(a[j], a[pivot]), swap(res[j], res[pivot]); auto invp = find_inverse(a[j][j]); assert(invp); T inv = *invp; for(auto jj = 0; jj < n; ++ jj) a[j][jj] *= inv, res[j][jj] *= inv; for(auto i = 0; i < n; ++ i) if(i != j && a[i][j] != T(0)){ T d = a[i][j]; for(auto jj = 0; jj < n; ++ jj) a[i][jj] -= d * a[j][jj], res[i][jj] -= d * res[j][jj]; } } return matrix_fixed(n, n, res); } template<class output_stream> friend output_stream &operator<<(output_stream &out, const matrix_fixed &a){ out << "{"; for(auto i = 0; i < a.n; ++ i){ out << "{"; for(auto j = 0; j < a.m; ++ j){ out << a(i, j); if(j != a.m - 1) out << ", "; } out << "}"; if(i != a.n - 1) out << ", "; } return out << "}"; } matrix_fixed(): matrix_fixed(T(0), T(0)){ } matrix_fixed(const T &init_diagonal, const T &init_off_diagonal){ for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] = i == j ? init_diagonal : init_off_diagonal; } matrix_fixed(const array<array<T, M>, N> &arr): data(arr){ } static matrix_fixed additive_identity(){ return matrix_fixed(T(1), T(0)); } static matrix_fixed multiplicative_identity(){ return matrix_fixed(T(0), T(0)); } }; template<class T, size_t N, size_t M> matrix_fixed<T, N, M> operator*(T c, matrix_fixed<T, N, M> a){ for(auto i = 0; i < a.n; ++ i) for(auto j = 0; j < a.m; ++ j) a(i, j) = c * a(i, j); return a; } // Multiply a row vector v on the left template<class T, size_t N, size_t M> matrix_fixed<T, N, M>::domain_t operator*(const typename matrix_fixed<T, N, M>::range_t &v, const matrix_fixed<T, N, M> &a){ typename matrix_fixed<T, N, M>::domain_t res; res.fill(T(0)); for(auto i = 0; i < a.n; ++ i) for(auto j = 0; j < a.m; ++ j) res[j] += v[i] * a(i, j); return res; } template<class T, class F> struct segment_tree{ int n, size, log; vector<T> data; F TT; // monoid operation (always adjacent) T T_id; // monoid identity // O(n) segment_tree(int n, F TT, T T_id): TT(TT), T_id(T_id){ init(n); } // O(n) segment_tree(int n, T x, F TT, T T_id): TT(TT), T_id(T_id){ init(n, x); } // O(n) segment_tree(const vector<T> &a, F TT, T T_id): TT(TT), T_id(T_id){ init(a); } segment_tree &operator=(const segment_tree &otr){ n = otr.n; size = otr.size; log = otr.log; data = otr.data; return *this; } // O(n) void init(int n){ init(n, T_id); } // O(n) void init(int n, T x){ this->n = n; size = 1; while(size < n) size <<= 1; log = __lg(size); data.assign(size << 1, T_id); fill(data.begin() + size, data.begin() + size + n, x); for(auto i = size - 1; i >= 1; -- i) refresh(i); } // O(n) void init(const vector<T> &a){ n = (int)a.size(); size = 1; while(size < n) size <<= 1; log = __lg(size); data.assign(size << 1, T_id); copy(a.begin(), a.end(), data.begin() + size); for(auto i = size - 1; i >= 1; -- i) refresh(i); } // O(1) void refresh(int i){ data[i] = TT(data[i << 1], data[i << 1 | 1]); } // O(log(n)) void set(int p, T x){ assert(0 <= p && p < n); data[p += size] = x; for(auto i = 1; i <= log; ++ i) refresh(p >> i); } // O(log(n)) void update(int p, T x){ assert(0 <= p && p < n); p += size; data[p] = TT(data[p], x); for(auto i = 1; i <= log; ++ i) refresh(p >> i); } // O(1) T query(int p) const{ assert(0 <= p && p < n); return data[p + size]; } // O(log(n)) T query(int l, int r) const{ assert(0 <= l && l <= r && r <= n); T res_left = T_id, res_right = T_id; for(l += size, r += size; l < r; l >>= 1, r >>= 1){ if(l & 1) res_left = TT(res_left, data[l ++]); if(r & 1) res_right = TT(data[-- r], res_right); } return TT(res_left, res_right); } // O(1) T query_all() const{ return data[1]; } // pred(sum[l, r)) is T, T, ..., T, F, F, ..., F // Returns max r with T // O(log(n)) int max_pref(int l, auto pred) const{ assert(0 <= l && l <= n && pred(T_id)); if(l == n) return n; l += size; T sm = T_id; do{ while(~l & 1) l >>= 1; if(!pred(TT(sm, data[l]))){ while(l < size){ l = l << 1; if(pred(TT(sm, data[l]))) sm = TT(sm, data[l ++]); } return l - size; } sm = TT(sm, data[l ++]); }while((l & -l) != l); return n; } // pred(sum[l, r)) is F, F, ..., F, T, T, ..., T // Returns min l with T // O(log(n)) int min_suff(int r, auto pred) const{ assert(0 <= r && r <= n && pred(T_id)); if(r == 0) return 0; r += size; T sm = T_id; do{ -- r; while(r > 1 && r & 1) r >>= 1; if(!pred(TT(data[r], sm))){ while(r < size){ r = r << 1 | 1; if(pred(TT(data[r], sm))) sm = TT(data[r --], sm); } return r + 1 - size; } sm = TT(data[r], sm); }while((r & -r) != r); return 0; } template<class output_stream> friend output_stream &operator<<(output_stream &out, const segment_tree<T, F> &seg){ out << "["; for(auto i = 0; i < seg.n; ++ i){ out << seg.query(i); if(i != seg.n - 1) out << ", "; } return out << ']'; } }; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n, qn; cin >> n; modular::setup(); cin >> qn; vector<matrix_fixed<modular, 2, 2>> init(n); for(auto i = 0; i < n; ++ i){ for(auto x = 0; x < 2; ++ x){ for(auto y = 0; y < 2; ++ y){ cin >> init[i](x, y); } } } segment_tree seg(init, [&](const auto &x, const auto &y){ return y * x; }, matrix_fixed<modular, 2, 2>(1, 0)); for(auto qi = 0; qi < qn; ++ qi){ int ql, qr; modular x, y; cin >> ql >> qr >> x >> y; auto res = seg.query(ql, qr) * array{x, y}; cout << res[0] << " " << res[1] << "\n"; } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////