QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#403364 | #2073. Knowledge-Oriented Problem | qwerasdfzxcl | TL | 1ms | 3768kb | C++20 | 29.9kb | 2024-05-02 09:33:04 | 2024-05-02 09:33:05 |
Judging History
answer
#include <bits/stdc++.h>
#include <utility>
namespace atcoder {
namespace internal {
// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;
// @param m `1 <= m < 2^31`
barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
// @return m
unsigned int umod() const { return _m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay
// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned int v = (unsigned int)(z - x * _m);
if (_m <= v) v += _m;
return v;
}
};
// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}
// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(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 (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
} // namespace internal
} // namespace atcoder
#include <cassert>
#include <numeric>
#include <type_traits>
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#include <cassert>
#include <numeric>
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
using namespace std;
using mint = atcoder::modint1000000007;
typedef long long ll;
template<class T>
struct matrix{
int n, m;
vector<vector<T>> data;
vector<T> &operator[](int i){
assert(0 <= i && i < n);
return data[i];
}
const vector<T> &operator[](int i) const{
assert(0 <= i && i < n);
return data[i];
}
matrix &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 slice(int il, int ir, int jl, int jr) const{
return matrix(*this).inplace_slice(il, ir, jl, jr);
}
matrix &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 row_slice(int il, int ir) const{
return matrix(*this).inplace_row_slice(il, ir);
}
matrix &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 column_slice(int jl, int jr) const{
return matrix(*this).inplace_column_slice(jl, jr);
}
bool operator==(const matrix &a) const{
assert(n == a.n && m == a.m);
return data == a.data;
}
bool operator!=(const matrix &a) const{
assert(n == a.n && m == a.m);
return data != a.data;
}
matrix &operator+=(const matrix &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 operator+(const matrix &a) const{
return matrix(*this) += a;
}
matrix &operator-=(const matrix &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 operator-(const matrix &a) const{
return matrix(*this) -= a;
}
matrix operator*=(const matrix &a){
assert(m == a.n);
int l = a.m;
matrix 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 operator*(const matrix &a) const{
return matrix(*this) *= a;
}
matrix &operator*=(T c){
for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] *= c;
return *this;
}
matrix operator*(T c) const{
return matrix(*this) *= c;
}
template<class U, typename enable_if<is_integral<U>::value>::type* = nullptr>
matrix &inplace_power(U e){
assert(n == m && e >= 0);
matrix res(n, n, T{1});
for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
return *this = res;
}
template<class U>
matrix power(U e) const{
return matrix(*this).inplace_power(e);
}
matrix &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 transpose() const{
if(n == m) return matrix(*this).inplace_transpose();
matrix 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;
}
// 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, determinant, rank}
tuple<matrix &, 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]!=0){
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].val() / data[i][j].val();
if(q!=0) 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]!=0) ++ 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, determinant, rank}
tuple<matrix, T, int> REF(int up_to = -1) const{
return matrix(*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, determinant, rank}
tuple<matrix &, 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, determinant, rank}
tuple<matrix, T, int> REF_field(int up_to = -1) const{
return matrix(*this).inplace_REF_field(up_to);
}
// Assumes T is a field.
// O(n) divisions with O(n^3) additions, subtractions, and multiplications.
optional<matrix> 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;
}
// 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 linear equation.
// Assumes T is a field
// O(n * m^2)
optional<vector<T>> find_a_solution() const{
assert(m >= 1);
auto [ref, _, rank] = REF_field(m - 1);
for(auto i = rank; i < n; ++ i) if(ref[i][m - 1] != T{0}) return {};
vector<T> res(m - 1);
for(auto i = rank - 1; i >= 0; -- i){
int pivot = 0;
while(pivot < m - 1 && ref[i][pivot] == T{0}) ++ pivot;
assert(pivot < m - 1);
res[pivot] = ref[i][m - 1];
for(auto j = pivot + 1; j < m - 1; ++ j) res[pivot] -= ref[i][j] * res[j];
res[pivot] /= ref[i][pivot];
}
return res;
}
// O(n * 2^n)
T permanent() const{
assert(n <= 30 && n == m);
T perm = n ? 0 : 1;
vector<T> sum(n);
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 &a){
out << "\n";
for(auto i = 0; i < a.n; ++ i){
for(auto j = 0; j < a.m; ++ j) out << a[i][j].val() << " ";
out << "\n";
}
return out;
}
matrix(){}
matrix(int n, int m, T init_diagonal = T{0}, T init_off_diagonal = T{0}): n(n), m(m){
assert(n >= 0 && m >= 0);
data.assign(n, vector<T>(m, T{0}));
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(int n, int m, const vector<vector<T>> &a): n(n), m(m), data(a){ }
static matrix additive_identity(int n, int m){
return matrix(n, m, T{0}, T{0});
}
static matrix multiplicative_identity(int n, int m){
return matrix(n, m, T{1}, T{0});
}
};
template<class T>
matrix<T> operator*(T c, matrix<T> 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>
vector<T> operator*(const vector<T> &v, const matrix<T> &a){
assert(a.n == (int)size(v));
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;
}
mint calc(vector<vector<mint>> &G, int t){
int n = G.size();
vector<vector<mint>> fuck(n*t, vector<mint>(n*t, 0));
for (int z=0;z<t;z++){
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
fuck[z*n + i][z*n + j] = G[i][j];
}
}
for (int i=0;i<n;i++) if (z<t-1) fuck[z*n+i][(z+1)*n+i] = -1;
for (int i=0;i<n;i++) if (z<t-1) fuck[(z+1)*n+i][z*n+i] = -1;
}
fuck.pop_back();
for (auto &x:fuck) x.pop_back();
// cout << matrix<mint>(n*t-1, n*t-1, fuck);
return matrix<mint>(n*t-1, n*t-1, fuck).determinant();
}
struct MM{
matrix<mint> d[2][2];
MM(const matrix<mint> &F, const matrix<mint> &G, const matrix<mint> &H, const matrix<mint> &I){
d[0][0] = F;
d[0][1] = G;
d[1][0] = H;
d[1][1] = I;
}
MM operator * (const MM &M) const{
return MM(d[0][0]*M.d[0][0]+d[0][1]*M.d[1][0], d[0][0]*M.d[0][1]+d[0][1]*M.d[1][1], d[1][0]*M.d[0][0]+d[1][1]*M.d[1][0], d[1][0]*M.d[0][1]+d[1][1]*M.d[1][1]);
}
};
MM pw(const MM &M, ll t){
int n = M.d[0][0].n;
if (!t) return MM(matrix<mint>(n, n, 1), matrix<mint>(n, n), matrix<mint>(n, n), matrix<mint>(n, n, 1));
auto ret = pw(M, t/2);
if (t&1) return ret * ret * M;
return ret * ret;
}
void fuuck(vector<matrix<mint>> &F, ll &t){
auto M = MM(F[1], matrix<mint>(F[1].n, F[1].n)-F[0], F[0], matrix<mint>(F[1].n, F[1].n));
auto N = pw(M, t-4);
F[4] = N.d[0][0] * F[1] + N.d[0][1] * F[0];
F[3] = N.d[1][0] * F[1] + N.d[1][1] * F[0];
F[5] = F[4]*F[1] - F[3];
t = 7;
}
mint guess(vector<vector<mint>> G, ll t){
int n = G.size();
vector<matrix<mint>> F(min(6LL, t+1), matrix<mint>(n, n));
F[0] = matrix<mint>(n, n, mint(1));
F[1] = matrix<mint>(n, n, G);
// cout << F[0];
if (t <= 5) for (int i=2;i<=t;i++) F[i] = F[i-1]*F[1] - F[i-2];
else fuuck(F, t);
// cout << F[t-2] << F[t-3] << F[t-4];
// return F[t].determinant();
for (auto &x:G) x.back() = 0;
for (auto &x:G.back()) x = 0;
G.back().back() = 1;
auto G2 = matrix<mint>(n, n, G);
auto I2 = matrix<mint>(n, n, mint(1));
I2[n-1][n-1] = 0;
// cout << "ok " << t << '\n';
// cout << F[t-1].n << F[t-1].m << G2.n << G2.m << F[t-2].n << F[t-2].m << I2.n << I2.m << '\n';
if (t==1) return G2.determinant();
if (t==2) return (F[t-1]*G2 - F[t-2]*I2).determinant();
auto FUCK = F[1];
for (int i=0;i<n;i++) FUCK[i][i] -= 1;
auto H1 = F[t-2]*FUCK - F[t-3];
auto H2 = F[t-3]*FUCK;
if (t>3) H2 -= F[t-4];
auto FUCK2 = FUCK;
for (int i=0;i<n;i++) FUCK2[i][n-1] = 0;
for (int j=0;j<n;j++) FUCK2[n-1][j] = 0;
FUCK2[n-1][n-1] = 1;
// cout << FUCK2 << "Hello\n";
// cout << FUCK2[n-1][n-1].val() << '\n';
return (H1*FUCK - H2*I2).determinant();
}
int main(){
int n, m;
ll t;
scanf("%d %d %lld", &n, &m, &t);
if (n==1){
printf("1\n");
return 0;
}
vector<vector<mint>> G(n, vector<mint>(n));
for (int i=1;i<=m;i++){
int x, y;
scanf("%d %d", &x, &y);
--x, --y;
G[x][y] -= 1;
G[y][x] -= 1;
G[x][x] += 1;
G[y][y] += 1;
}
for (int i=0;i<n;i++){
if (t==2) G[i][i] += 1;
else if (t>2) G[i][i] += 2;
}
cout << guess(G, t).val() << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3768kb
input:
5 6 2 3 2 5 1 3 4 2 4 5 3 1 3
output:
4725
result:
ok single line: '4725'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 1 200 1 2
output:
272581704
result:
ok single line: '272581704'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
5 10 1000000000000000000 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
output:
569698435
result:
ok single line: '569698435'
Test #4:
score: -100
Time Limit Exceeded
input:
500 62366 1000000000000000000 1 4 1 7 1 8 1 11 1 13 1 15 1 18 1 20 1 21 1 22 1 23 1 24 1 28 1 29 1 30 1 32 1 33 1 34 1 36 1 37 1 40 1 41 1 43 1 44 1 45 1 46 1 50 1 51 1 52 1 53 1 55 1 57 1 59 1 61 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 75 1 76 1 80 1 81 1 83 1 84 1 85 1 87 1 88 1 9...