結果
問題 |
No.3053 $((0 \And 1)\mathop{|}2)\oplus 3$
|
ユーザー |
|
提出日時 | 2025-03-07 23:55:56 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 23,481 bytes |
コンパイル時間 | 3,944 ms |
コンパイル使用メモリ | 293,436 KB |
実行使用メモリ | 8,608 KB |
最終ジャッジ日時 | 2025-03-07 23:56:12 |
合計ジャッジ時間 | 15,053 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 WA * 7 |
ソースコード
// #include <bits/allocator.h> // Temp fix for gcc13 global pragma // #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> // #include <x86intrin.h> using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif #ifdef LOCAL #include "Debug.h" #else #define debug_endl() 42 #define debug(...) 42 #define debug2(...) 42 #define debugbin(...) 42 #endif template<class data_t, data_t _mod> struct modular_fixed_base{ #define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) #define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>) static_assert(IS_UNSIGNED(data_t)); static_assert(1 <= _mod && _mod < data_t(1) << 8 * sizeof(data_t) - 1); static constexpr bool VARIATE_MOD_FLAG = false; static constexpr data_t mod(){ return _mod; } template<class T> static vector<modular_fixed_base> precalc_power(T base, int SZ){ vector<modular_fixed_base> res(SZ + 1, 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base; return res; } template<class T> static vector<modular_fixed_base> precalc_geometric_sum(T base, int SZ){ vector<modular_fixed_base> res(SZ + 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base + base; return res; } static vector<modular_fixed_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_fixed_base _primitive_root; static modular_fixed_base primitive_root(){ if(_primitive_root) return _primitive_root; if(_mod == 2) return _primitive_root = 1; if(_mod == 998244353) return _primitive_root = 3; data_t divs[20] = {}; divs[0] = 2; int cnt = 1; data_t 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_fixed_base(g).power((_mod - 1) / divs[i]) == 1){ ok = false; break; } } if(ok) return _primitive_root = g; } } constexpr modular_fixed_base(){ } modular_fixed_base(const double &x){ data = _normalize(llround(x)); } modular_fixed_base(const long double &x){ data = _normalize(llround(x)); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base(const T &x){ data = _normalize(x); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){ int sign = x >= 0 ? 1 : -1; data_t v = _mod <= sign * x ? sign * x % _mod : sign * x; if(sign == -1 && v) v = _mod - v; return v; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data; } modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; } modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); } modular_fixed_base &operator++(){ return *this += 1; } modular_fixed_base &operator--(){ return *this += _mod - 1; } modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; } modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; } modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); } modular_fixed_base &operator*=(const modular_fixed_base &rhs){ if constexpr(is_same_v<data_t, unsigned int>) data = (unsigned long long)data * rhs.data % _mod; else if constexpr(is_same_v<data_t, unsigned long long>){ long long res = data * rhs.data - _mod * (unsigned long long)(1.L / _mod * data * rhs.data); data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod); } else data = _normalize(data * rhs.data); return *this; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &inplace_power(T e){ if(e == 0) return *this = 1; if(data == 0) return *this = {}; if(data == 1 || e == 1) return *this; if(data == mod() - 1) return e % 2 ? *this : *this = -*this; if(e < 0) *this = 1 / *this, e = -e; if(e == 1) return *this; modular_fixed_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)>::type* = nullptr> modular_fixed_base power(T e) const{ return modular_fixed_base(*this).inplace_power(e); } // c + c * x + ... + c * x^{e-1} template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &inplace_geometric_sum(T e, modular_fixed_base c = 1){ if(e == 0) return *this = {}; if(data == 0) return *this = {}; if(data == 1) return *this = c * e; if(e == 1) return *this = c; if(data == mod() - 1) return *this = c * abs(e % 2); modular_fixed_base res = 0; if(e < 0) return *this = geometric_sum(-e + 1, -*this) - 1; if(e == 1) return *this = c * *this; for(; e; c *= 1 + *this, *this *= *this, e >>= 1) if(e & 1) res += c, c *= *this; return *this = res; } // c + c * x + ... + c * x^{e-1} template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base geometric_sum(T e, modular_fixed_base c = 1) const{ return modular_fixed_base(*this).inplace_geometric_sum(e, c); } modular_fixed_base &operator/=(const modular_fixed_base &otr){ make_signed_t<data_t> a = otr.data, m = _mod, u = 0, v = 1; if(a < _INV.size()) return *this *= _INV[a]; while(a){ make_signed_t<data_t> t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return *this *= u; } #define ARITHMETIC_OP(op, apply_op)\ modular_fixed_base operator op(const modular_fixed_base &x) const{ return modular_fixed_base(*this) apply_op x; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ modular_fixed_base operator op(const T &x) const{ return modular_fixed_base(*this) apply_op modular_fixed_base(x); }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ friend modular_fixed_base operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x) apply_op y; } ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=) #undef ARITHMETIC_OP #define COMPARE_OP(op)\ bool operator op(const modular_fixed_base &x) const{ return data op x.data; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ bool operator op(const T &x) const{ return data op modular_fixed_base(x).data; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ friend bool operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x).data op y.data; } COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=) #undef COMPARE_OP friend istream &operator>>(istream &in, modular_fixed_base &number){ long long x; in >> x; number.data = modular_fixed_base::_normalize(x); return in; } friend ostream &operator<<(ostream &out, const modular_fixed_base &number){ out << number.data; #ifdef LOCAL cerr << "("; for(auto d = 1; ; ++ d){ if((number * d).data <= 1000000){ cerr << (number * d).data; if(d != 1) cerr << "/" << d; break; } else if((-number * d).data <= 1000000){ cerr << "-" << (-number * d).data; if(d != 1) cerr << "/" << d; break; } } cerr << ")"; #endif return out; } data_t data = 0; #undef IS_INTEGRAL #undef IS_UNSIGNED }; template<class data_t, data_t _mod> vector<modular_fixed_base<data_t, _mod>> modular_fixed_base<data_t, _mod>::_INV; template<class data_t, data_t _mod> modular_fixed_base<data_t, _mod> modular_fixed_base<data_t, _mod>::_primitive_root; const unsigned int mod = (119 << 23) + 1; // 998244353 // const unsigned int mod = 1e9 + 7; // 1000000007 // const unsigned int mod = 1e9 + 9; // 1000000009 // const unsigned long long mod = (unsigned long long)1e18 + 9; using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>; modular operator""_m(const char *x){ return stoll(x); } template<class T, size_t N, size_t M> struct matrix_fixed_base{ int n, 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]; } matrix_fixed_base &inplace_slice(int il, int ir, int jl, int jr){ assert(0 <= il && il <= ir && ir <= n); assert(0 <= jl && jl <= jr && jr <= m); n = ir - il, m = jr - jl; if(il > 0) for(auto i = 0; i < n; ++ i) swap(data[i], data[il + i]); data.resize(n); for(auto &row: data){ row.erase(row.begin(), row.begin() + jl); row.resize(m); } return *this; } matrix_fixed_base slice(int il, int ir, int jl, int jr) const{ return matrix_fixed_base(*this).inplace_slice(il, ir, jl, jr); } matrix_fixed_base &inplace_row_slice(int il, int ir){ assert(0 <= il && il <= ir && ir <= n); n = ir - il; if(il > 0) for(auto i = 0; i < n; ++ i) swap(data[i], data[il + i]); data.resize(n); return *this; } matrix_fixed_base row_slice(int il, int ir) const{ return matrix_fixed_base(*this).inplace_row_slice(il, ir); } matrix_fixed_base &inplace_column_slice(int jl, int jr){ assert(0 <= jl && jl <= jr && jr <= m); m = jr - jl; for(auto &row: data){ row.erase(row.begin(), row.begin() + jl); row.resize(m); } return *this; } matrix_fixed_base column_slice(int jl, int jr) const{ return matrix_fixed_base(*this).inplace_column_slice(jl, jr); } bool operator==(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return data == a.data; } bool operator!=(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return data != a.data; } matrix_fixed_base &operator+=(const matrix_fixed_base &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_base operator+(const matrix_fixed_base &a) const{ return matrix_fixed_base(*this) += a; } matrix_fixed_base &operator-=(const matrix_fixed_base &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_base operator-(const matrix_fixed_base &a) const{ return matrix_fixed_base(*this) -= a; } matrix_fixed_base operator*=(const matrix_fixed_base &a){ assert(m == a.n); int l = a.m; matrix_fixed_base res(n, l); 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 *this = res; } matrix_fixed_base operator*(const matrix_fixed_base &a) const{ return matrix_fixed_base(*this) *= a; } matrix_fixed_base &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_base operator*(T c) const{ return matrix_fixed_base(*this) *= c; } friend matrix_fixed_base operator*(T c, matrix_fixed_base 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; } template<class U, typename enable_if<is_integral<U>::value>::type* = nullptr> matrix_fixed_base &inplace_power(U e){ assert(n == m && e >= 0); matrix_fixed_base res(n, n, T{1}); for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class U> matrix_fixed_base power(U e) const{ return matrix_fixed_base(*this).inplace_power(e); } matrix_fixed_base &inplace_transpose(){ assert(n == m); for(auto i = 0; i < n; ++ i) for(auto j = i + 1; j < n; ++ j) swap(data[i][j], data[j][i]); return *this; } matrix_fixed_base transpose() const{ if(n == m) return matrix_fixed_base(*this).inplace_transpose(); matrix_fixed_base res(m, n); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res[j][i] = data[i][j]; return res; } vector<T> operator*(const vector<T> &v) const{ assert(m == (int)v.size()); vector<T> res(n, 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; } friend vector<T> operator*(const vector<T> &v, const matrix_fixed_base &a){ vector<T> res(a.m, 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; } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Otherwise, O(n * up_to * log(size)) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base &, T, int> inplace_REF(int up_to = -1){ if(n == 0) return {*this, T{1}, 0}; if(!~up_to) up_to = m; T det = 1; int rank = 0; for(auto j = 0; j < up_to; ++ j){ if constexpr(is_floating_point_v<T>){ static const T eps = 1e-9; int pivot = rank; for(auto i = rank + 1; i < n; ++ i) if(abs(data[pivot][j]) < abs(data[i][j])) pivot = i; if(rank != pivot){ swap(data[rank], data[pivot]); det *= -1; } if(abs(data[rank][j]) <= eps) continue; det *= data[rank][j]; T inv = 1 / data[rank][j]; for(auto i = rank + 1; i < n; ++ i) if(abs(data[i][j]) > eps){ T coef = data[i][j] * inv; for(auto k = j; k < m; ++ k) data[i][k] -= coef * data[rank][k]; } ++ rank; } else{ for(auto i = rank + 1; i < n; ++ i) while(data[i][j]){ T q; if constexpr(is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) q = data[rank][j] / data[i][j]; else q = data[rank][j].data / data[i][j].data; if(q) for(auto k = j; k < m; ++ k) data[rank][k] -= q * data[i][k]; swap(data[rank], data[i]); det *= -1; } if(rank == j) det *= data[rank][j]; else det = T(0); if(data[rank][j]) ++ rank; } if(rank == n) break; } return {*this, det, rank}; } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Otherwise, O(n * up_to * log(size)) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base, T, int> REF(int up_to = -1) const{ return matrix_fixed_base(*this).inplace_REF(up_to); } // Assumes T is a field. // O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base &, T, int> inplace_REF_field(int up_to = -1){ if(n == 0) return {*this, T{1}, 0}; if(!~up_to) up_to = m; T det = T{1}; int rank = 0; for(auto j = 0; j < up_to; ++ j){ int pivot = -1; for(auto i = rank; i < n; ++ i) if(data[i][j] != T(0)){ pivot = i; break; } if(!~pivot){ det = T(0); continue; } if(rank != pivot){ swap(data[rank], data[pivot]); det *= -1; } det *= data[rank][j]; T inv = 1 / data[rank][j]; for(auto i = rank + 1; i < n; ++ i) if(data[i][j] != T(0)){ T coef = data[i][j] * inv; for(auto k = j; k < m; ++ k) data[i][k] -= coef * data[rank][k]; } ++ rank; if(rank == n) break; } return {*this, det, rank}; } // Assumes T is a field. // O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base, T, int> REF_field(int up_to = -1) const{ return matrix_fixed_base(*this).inplace_REF_field(up_to); } // Assumes T is a field. // O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base &, T, int> inplace_RREF_field(int up_to = -1){ if(n == 0) return {*this, T{1}, 0}; auto [mat, det, rank] = inplace_REF_field(up_to); for(auto i = rank - 1; i >= 0; -- i){ int pivot = find_if(mat[i].begin(), mat[i].end(), [&](const T &x){ return x != T{0}; }) - mat[i].begin(); T inv = T{1} / mat[i][pivot]; for(auto t = 0; t < i; ++ t){ T coef = mat[t][pivot] * inv; for(auto j = 0; j < m; ++ j) mat[t][j] -= coef * mat[i][j]; } } return {mat, det, rank}; } // Assumes T is a field. // O(up_to) divisions with O(n * m * up_to) additions, subtractions, and multiplications. // Returns {REF matrix_fixed_base, determinant, rank} tuple<matrix_fixed_base, T, int> RREF_field(int up_to = -1) const{ return matrix_fixed_base(*this).inplace_RREF_field(up_to); } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. T determinant() const{ assert(n == m); return get<1>(REF()); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. T determinant_field() const{ assert(n == m); return get<1>(REF_field()); } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. int rank() const{ return get<2>(REF()); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. int rank_field() const{ return get<2>(REF_field()); } // Regarding the matrix as a system of linear equations by separating first m-1 columns, // find a solution of the system along with the basis for the nullspace // Assumes T is a field // O(n * m^2) optional<pair<vector<T>, vector<vector<T>>>> solve_right() const{ assert(m >= 1); auto [rref, _, rank] = RREF_field(m - 1); for(auto i = rank; i < n; ++ i) if(rref[i][m - 1] != T{0}) return {}; vector<T> res(m - 1); vector<int> pivot(rank), appear_as_pivot(m - 1); for(auto i = rank - 1; i >= 0; -- i){ pivot[i] = find_if(rref[i].begin(), rref[i].end(), [&](const T &x){ return x != T{0}; }) - rref[i].begin(); assert(pivot[i] < m - 1); appear_as_pivot[pivot[i]] = true; res[pivot[i]] = rref[i][m - 1] / rref[i][pivot[i]]; } vector<vector<T>> basis; for(auto j = 0; j < m - 1; ++ j){ if(appear_as_pivot[j]) continue; vector<T> b(m - 1); b[j] = T{1}; for(auto i = 0; i < rank; ++ i){ if(rref[i][j] == T{0}) continue; b[pivot[i]] = -rref[i][j] / rref[i][pivot[i]]; } basis.push_back(b); } return pair{res, basis}; } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. optional<matrix_fixed_base> inverse() const{ assert(n == m); if(n == 0) return *this; auto a = data; auto res = multiplicative_identity(); for(auto j = 0; j < n; ++ j){ int rank = j, pivot = -1; if constexpr(is_floating_point_v<T>){ static const T eps = 1e-9; pivot = rank; for(auto i = rank + 1; i < n; ++ i) if(abs(a[pivot][j]) < abs(a[i][j])) pivot = i; if(abs(a[pivot][j]) <= eps) return {}; } else{ for(auto i = rank; i < n; ++ i) if(a[i][j] != T(0)){ pivot = i; break; } if(!~pivot) return {}; } swap(a[rank], a[pivot]), swap(res[rank], res[pivot]); T inv = 1 / a[rank][j]; for(auto k = 0; k < n; ++ k) a[rank][k] *= inv, res[rank][k] *= inv; for(auto i = 0; i < n; ++ i){ if constexpr(is_floating_point_v<T>){ static const T eps = 1e-9; if(i != rank && abs(a[i][j]) > eps){ T d = a[i][j]; for(auto k = 0; k < n; ++ k) a[i][k] -= d * a[rank][k], res[i][k] -= d * res[rank][k]; } } else if(i != rank && a[i][j] != T(0)){ T d = a[i][j]; for(auto k = 0; k < n; ++ k) a[i][k] -= d * a[rank][k], res[i][k] -= d * res[rank][k]; } } } return res; } // O(n * 2^n) T permanent() const{ assert(n <= 30 && n == m); T perm = n ? 0 : 1; array<T, N> sum; sum.fill(T{0}); for(auto order = 1; order < 1 << n; ++ order){ int j = __lg(order ^ order >> 1 ^ order - 1 ^ order - 1 >> 1), sign = (order ^ order >> 1) & 1 << j ? 1 : -1; T prod = order & 1 ? -1 : 1; if((order ^ order >> 1) & 1 << j) for(auto i = 0; i < n; ++ i) prod *= sum[i] += data[i][j]; else for(auto i = 0; i < n; ++ i) prod *= sum[i] -= data[i][j]; perm += prod; } return perm * (n & 1 ? -1 : 1); } template<class output_stream> friend output_stream &operator<<(output_stream &out, const matrix_fixed_base &a){ out << "\n"; for(auto i = 0; i < a.n; ++ i){ for(auto j = 0; j < a.m; ++ j) out << a[i][j] << " "; if(i < a.n - 1) out << "\n"; } return out; } matrix_fixed_base(int n, int m, T init_diagonal = T{0}, T init_off_diagonal = T{0}): n(n), m(m){ assert(0 <= n && n <= N && 0 <= m && m <= M); 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_base(int n, int m, const array<array<T, M>, N> &a): n(n), m(m), data(a){ assert(0 <= n && n <= N && 0 <= m && m <= M); } matrix_fixed_base(int n, int m, const array<T, N * M> &a): n(n), m(m){ assert(0 <= n && n <= N && 0 <= m && m <= M); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] = a[m * i + j]; } static matrix_fixed_base additive_identity(int n, int m){ return matrix_fixed_base(n, m, T{0}, T{0}); } static matrix_fixed_base multiplicative_identity(int n, int m){ return matrix_fixed_base(n, m, T{1}, T{0}); } }; template<class T> using matrix = matrix_fixed_base<T, 2, 2>; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); using M = matrix<modular>; string n; cin >> n; { ranges::reverse(n); int i = 0; ++ n[i]; while(i < (int)n.size() - 1 && n[i] == '2'){ n[i] = '0'; ++ n[i + 1]; } if(n.back() == '2'){ n.back() = '0'; n += '1'; } } modular res = 0, base = 1; const M m0(2, 2, array{3_m, 1_m, 0_m, 2_m}); const M m1(2, 2, array{1_m, 1_m, 2_m, 2_m}); M p0 = m0; M p1 = m1; vector<int> p_exp((int)n.size()); for(auto i = (int)n.size() - 2; i >= 0; -- i){ p_exp[i] = (p_exp[i + 1] * 2 + (n[i + 1] - '0')) % (modular::mod() - 1); } M extra0 = M::multiplicative_identity(2, 2), extra1 = extra0; for(auto bit = 0; bit < (int)n.size(); ++ bit, base *= 2){ M p = p1 * p0; assert(p[0][0] == p[0][1] && p[1][0] == p[1][1]); if(p_exp[bit] == 0){ p = M::multiplicative_identity(2, 2); } else{ p *= (p[0][0] + p[1][0]).power(p_exp[bit] - 1); } if(n[bit] == '0'){ p = extra0 * p; } else{ p = extra1 * p0 * p; } res += base * p[1][0] / 3; if(n[bit] == '1'){ extra0 *= p0; extra1 *= p1; } p0 *= p0; p1 *= p1; } cout << res << "\n"; return 0; } /* */