This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/541
#include <cassert>
#include <iostream>
#include <optional>
#include <vector>
using uint = unsigned;
using ull = unsigned long long;
using Matrix = std::vector<std::vector<uint>>;
constexpr uint MOD = 1'000'000'007;
constexpr uint PowMod(uint a, ull e) {
for (uint res = 1;; a = (ull)a * a % MOD) {
if (e & 1) res = (ull)res * a % MOD;
if ((e /= 2) == 0) return res;
}
}
constexpr uint InvMod(uint a) { return PowMod(a, MOD - 2); }
struct Basis {
const int dim;
Matrix vectors; // v_0, v_1, ...
Matrix a;
Matrix r; // upper triangular matrix, diagonal of r = (1, ..., 1)
// a * vectors = r
explicit Basis(int dim) : dim(dim), a(dim), r(dim) {}
int size() const { return (int)std::size(vectors); }
// if V is linear combination of v_0, ..., v_(k-1) then
// returns coefficients (a_0, ..., a_(k-1)) s.t. -(a_0v_0 + ... + a_(k-1)v_(k-1)) = V
std::optional<std::vector<uint>> insert(const std::vector<uint> &V) {
std::vector<uint> A(dim), RV = V;
for (int i = 0; i < dim; ++i) {
if (RV[i] == 0) continue;
if (r[i].empty()) {
A[size()] = 1;
const uint inv = InvMod(RV[i]);
for (int j = i; j < dim; ++j) RV[j] = (ull)RV[j] * inv % MOD;
for (int j = 0; j < dim; ++j) A[j] = (ull)A[j] * inv % MOD;
a[i] = A, r[i] = RV, vectors.push_back(V);
return {};
}
const uint v = RV[i];
for (int j = i; j < dim; ++j) RV[j] = (RV[j] + MOD - (ull)v * r[i][j] % MOD) % MOD;
for (int j = 0; j < dim; ++j) A[j] = (A[j] + MOD - (ull)v * a[i][j] % MOD) % MOD;
}
return A;
}
};
int width(const Matrix &A) { return empty(A) ? 0 : (int)size(A[0]); }
int height(const Matrix &A) { return (int)size(A); }
Matrix mat_mul(const Matrix &A, const Matrix &B) {
const int wA = width(A);
const int hA = height(A);
assert(height(B) == wA);
const int wB = width(B);
Matrix res(hA, std::vector<uint>(wB));
for (int i = 0; i < hA; ++i)
for (int k = 0; k < wA; ++k)
for (int j = 0; j < wB; ++j) res[i][j] = (res[i][j] + (ull)A[i][k] * B[k][j]) % MOD;
return res;
}
Matrix mat_pow(Matrix A, long long k) {
const int n = width(A);
assert(n == height(A));
Matrix res(n, std::vector<uint>(n));
for (int i = 0; i < n; ++i) res[i][i] = 1;
for (;; A = mat_mul(A, A)) {
if (k & 1) res = mat_mul(res, A);
if ((k /= 2) == 0) return res;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// dp[width i connected][mask]
uint dp[52][0b1000] = {};
/* x x
0b001 = . , 0b111 = x , ...
. x */
dp[1][0b001] = 1;
dp[1][0b010] = 1;
dp[1][0b100] = 1;
dp[1][0b011] = 1;
dp[1][0b110] = 1;
dp[1][0b111] = 1;
for (int i = 2; i < (int)std::size(dp); ++i) {
for (int j = 0b001; j <= 0b111; ++j) {
if (j == 0b101) {
/* ?xx ?xx
?x. or ?.. , ? connected
?xx ?xx */
dp[i][j] = ((ull)dp[i][j] + dp[i - 1][0b111] + dp[i - 1][0b101]) % MOD;
continue;
}
for (int k = 0b001; k <= 0b111; ++k) {
if (k == 0b101) {
if (j == 0b011 || j == 0b110) {
/* x. xx
.x or .x invalid
xx x. */
continue;
}
if (j == 0b111) {
/* _xxx
_..x
_xxx */
dp[i][j] = (dp[i][j] + 1) % MOD;
/* ?xx x .
?.x + width (i - 2) and type = . or type = . ,
?xx . x
---------------------------------------------------
?xxx x .
?..x + width (i - 3) and type = . or type = . ,
?xxx . x */
for (int l = 1; l <= i - 2; ++l) {
dp[i][j] = ((ull)dp[i][j] + dp[l][0b001] + dp[l][0b100]) % MOD;
}
continue;
}
}
if ((j & k) > 0) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
}
}
}
std::vector<uint> A;
for (int i = 1; i < (int)std::size(dp); ++i) {
uint res = 0;
for (int j = 1; j <= i; ++j) {
uint sum = 0;
for (int k = 0b001; k <= 0b111; ++k) sum = (sum + dp[j][k]) % MOD;
res = (res + (ull)sum * (i - j + 1)) % MOD;
}
A.push_back(res);
}
Basis b((int)size(A) / 2);
std::optional<std::vector<uint>> minpoly;
for (int i = 0; i <= (int)size(A) / 2; ++i)
if (const auto c = b.insert(std::vector(begin(A) + i, begin(A) + (i + (int)size(A) / 2)))) {
minpoly.emplace(begin(*c), begin(*c) + i).emplace_back(1);
break;
}
assert(minpoly);
const int deg = (int)size(*minpoly) - 1;
// M = transposed(C_minpoly)
Matrix M(deg, std::vector<uint>(deg));
for (int i = 0; i < deg - 1; ++i) M[i][i + 1] = 1;
for (int i = 0; i < deg; ++i) M[deg - 1][i] = (MOD - minpoly->at(i)) % MOD;
long long N;
std::cin >> N;
M = mat_pow(M, N - 1);
uint res = 0;
for (int i = 0; i < deg; ++i) res = (res + (ull)M[0][i] * A[i]) % MOD;
std::cout << res << '\n';
return 0;
}
#line 1 "standalone_test/other/yukicoder541.basis.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/541
#include <cassert>
#include <iostream>
#include <optional>
#include <vector>
using uint = unsigned;
using ull = unsigned long long;
using Matrix = std::vector<std::vector<uint>>;
constexpr uint MOD = 1'000'000'007;
constexpr uint PowMod(uint a, ull e) {
for (uint res = 1;; a = (ull)a * a % MOD) {
if (e & 1) res = (ull)res * a % MOD;
if ((e /= 2) == 0) return res;
}
}
constexpr uint InvMod(uint a) { return PowMod(a, MOD - 2); }
struct Basis {
const int dim;
Matrix vectors; // v_0, v_1, ...
Matrix a;
Matrix r; // upper triangular matrix, diagonal of r = (1, ..., 1)
// a * vectors = r
explicit Basis(int dim) : dim(dim), a(dim), r(dim) {}
int size() const { return (int)std::size(vectors); }
// if V is linear combination of v_0, ..., v_(k-1) then
// returns coefficients (a_0, ..., a_(k-1)) s.t. -(a_0v_0 + ... + a_(k-1)v_(k-1)) = V
std::optional<std::vector<uint>> insert(const std::vector<uint> &V) {
std::vector<uint> A(dim), RV = V;
for (int i = 0; i < dim; ++i) {
if (RV[i] == 0) continue;
if (r[i].empty()) {
A[size()] = 1;
const uint inv = InvMod(RV[i]);
for (int j = i; j < dim; ++j) RV[j] = (ull)RV[j] * inv % MOD;
for (int j = 0; j < dim; ++j) A[j] = (ull)A[j] * inv % MOD;
a[i] = A, r[i] = RV, vectors.push_back(V);
return {};
}
const uint v = RV[i];
for (int j = i; j < dim; ++j) RV[j] = (RV[j] + MOD - (ull)v * r[i][j] % MOD) % MOD;
for (int j = 0; j < dim; ++j) A[j] = (A[j] + MOD - (ull)v * a[i][j] % MOD) % MOD;
}
return A;
}
};
int width(const Matrix &A) { return empty(A) ? 0 : (int)size(A[0]); }
int height(const Matrix &A) { return (int)size(A); }
Matrix mat_mul(const Matrix &A, const Matrix &B) {
const int wA = width(A);
const int hA = height(A);
assert(height(B) == wA);
const int wB = width(B);
Matrix res(hA, std::vector<uint>(wB));
for (int i = 0; i < hA; ++i)
for (int k = 0; k < wA; ++k)
for (int j = 0; j < wB; ++j) res[i][j] = (res[i][j] + (ull)A[i][k] * B[k][j]) % MOD;
return res;
}
Matrix mat_pow(Matrix A, long long k) {
const int n = width(A);
assert(n == height(A));
Matrix res(n, std::vector<uint>(n));
for (int i = 0; i < n; ++i) res[i][i] = 1;
for (;; A = mat_mul(A, A)) {
if (k & 1) res = mat_mul(res, A);
if ((k /= 2) == 0) return res;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// dp[width i connected][mask]
uint dp[52][0b1000] = {};
/* x x
0b001 = . , 0b111 = x , ...
. x */
dp[1][0b001] = 1;
dp[1][0b010] = 1;
dp[1][0b100] = 1;
dp[1][0b011] = 1;
dp[1][0b110] = 1;
dp[1][0b111] = 1;
for (int i = 2; i < (int)std::size(dp); ++i) {
for (int j = 0b001; j <= 0b111; ++j) {
if (j == 0b101) {
/* ?xx ?xx
?x. or ?.. , ? connected
?xx ?xx */
dp[i][j] = ((ull)dp[i][j] + dp[i - 1][0b111] + dp[i - 1][0b101]) % MOD;
continue;
}
for (int k = 0b001; k <= 0b111; ++k) {
if (k == 0b101) {
if (j == 0b011 || j == 0b110) {
/* x. xx
.x or .x invalid
xx x. */
continue;
}
if (j == 0b111) {
/* _xxx
_..x
_xxx */
dp[i][j] = (dp[i][j] + 1) % MOD;
/* ?xx x .
?.x + width (i - 2) and type = . or type = . ,
?xx . x
---------------------------------------------------
?xxx x .
?..x + width (i - 3) and type = . or type = . ,
?xxx . x */
for (int l = 1; l <= i - 2; ++l) {
dp[i][j] = ((ull)dp[i][j] + dp[l][0b001] + dp[l][0b100]) % MOD;
}
continue;
}
}
if ((j & k) > 0) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
}
}
}
std::vector<uint> A;
for (int i = 1; i < (int)std::size(dp); ++i) {
uint res = 0;
for (int j = 1; j <= i; ++j) {
uint sum = 0;
for (int k = 0b001; k <= 0b111; ++k) sum = (sum + dp[j][k]) % MOD;
res = (res + (ull)sum * (i - j + 1)) % MOD;
}
A.push_back(res);
}
Basis b((int)size(A) / 2);
std::optional<std::vector<uint>> minpoly;
for (int i = 0; i <= (int)size(A) / 2; ++i)
if (const auto c = b.insert(std::vector(begin(A) + i, begin(A) + (i + (int)size(A) / 2)))) {
minpoly.emplace(begin(*c), begin(*c) + i).emplace_back(1);
break;
}
assert(minpoly);
const int deg = (int)size(*minpoly) - 1;
// M = transposed(C_minpoly)
Matrix M(deg, std::vector<uint>(deg));
for (int i = 0; i < deg - 1; ++i) M[i][i + 1] = 1;
for (int i = 0; i < deg; ++i) M[deg - 1][i] = (MOD - minpoly->at(i)) % MOD;
long long N;
std::cin >> N;
M = mat_pow(M, N - 1);
uint res = 0;
for (int i = 0; i < deg; ++i) res = (res + (ull)M[0][i] * A[i]) % MOD;
std::cout << res << '\n';
return 0;
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| clang++ | 11 |
|
12 ms | 9 MB |
| clang++ | 110 |
|
13 ms | 9 MB |
| clang++ | 111 |
|
14 ms | 9 MB |
| clang++ | 112 |
|
14 ms | 9 MB |
| clang++ | 113 |
|
14 ms | 8 MB |
| clang++ | 114 |
|
13 ms | 8 MB |
| clang++ | 115 |
|
15 ms | 9 MB |
| clang++ | 116 |
|
11 ms | 9 MB |
| clang++ | 117 |
|
14 ms | 9 MB |
| clang++ | 118 |
|
12 ms | 8 MB |
| clang++ | 119 |
|
15 ms | 9 MB |
| clang++ | 12 |
|
14 ms | 9 MB |
| clang++ | 120 |
|
13 ms | 9 MB |
| clang++ | 13 |
|
11 ms | 8 MB |
| clang++ | 14 |
|
14 ms | 9 MB |
| clang++ | 15 |
|
13 ms | 9 MB |
| clang++ | 16 |
|
14 ms | 9 MB |
| clang++ | 17 |
|
14 ms | 9 MB |
| clang++ | 18 |
|
11 ms | 8 MB |
| clang++ | 19 |
|
14 ms | 9 MB |
| clang++ | 20 |
|
15 ms | 9 MB |
| clang++ | 21 |
|
13 ms | 8 MB |
| clang++ | 210 |
|
12 ms | 9 MB |
| clang++ | 211 |
|
12 ms | 9 MB |
| clang++ | 212 |
|
16 ms | 8 MB |
| clang++ | 213 |
|
14 ms | 9 MB |
| clang++ | 214 |
|
11 ms | 9 MB |
| clang++ | 215 |
|
15 ms | 9 MB |
| clang++ | 216 |
|
15 ms | 9 MB |
| clang++ | 217 |
|
15 ms | 9 MB |
| clang++ | 218 |
|
14 ms | 9 MB |
| clang++ | 219 |
|
14 ms | 9 MB |
| clang++ | 22 |
|
16 ms | 9 MB |
| clang++ | 220 |
|
14 ms | 9 MB |
| clang++ | 23 |
|
12 ms | 9 MB |
| clang++ | 24 |
|
15 ms | 9 MB |
| clang++ | 25 |
|
11 ms | 8 MB |
| clang++ | 26 |
|
13 ms | 9 MB |
| clang++ | 27 |
|
16 ms | 9 MB |
| clang++ | 28 |
|
16 ms | 9 MB |
| clang++ | 29 |
|
14 ms | 9 MB |
| clang++ | 30 |
|
15 ms | 9 MB |
| clang++ | 31 |
|
15 ms | 9 MB |
| clang++ | 310 |
|
16 ms | 9 MB |
| clang++ | 311 |
|
16 ms | 9 MB |
| clang++ | 312 |
|
16 ms | 9 MB |
| clang++ | 313 |
|
16 ms | 9 MB |
| clang++ | 314 |
|
12 ms | 9 MB |
| clang++ | 315 |
|
16 ms | 9 MB |
| clang++ | 316 |
|
16 ms | 9 MB |
| clang++ | 317 |
|
16 ms | 9 MB |
| clang++ | 318 |
|
16 ms | 9 MB |
| clang++ | 319 |
|
13 ms | 9 MB |
| clang++ | 32 |
|
17 ms | 9 MB |
| clang++ | 320 |
|
14 ms | 9 MB |
| clang++ | 33 |
|
11 ms | 9 MB |
| clang++ | 34 |
|
13 ms | 9 MB |
| clang++ | 35 |
|
11 ms | 9 MB |
| clang++ | 36 |
|
17 ms | 9 MB |
| clang++ | 37 |
|
15 ms | 9 MB |
| clang++ | 38 |
|
12 ms | 9 MB |
| clang++ | 39 |
|
16 ms | 9 MB |