結果
| 問題 |
No.1112 冥界の音楽
|
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-07-10 23:24:03 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 11 ms / 2,000 ms |
| コード長 | 8,915 bytes |
| コンパイル時間 | 3,507 ms |
| コンパイル使用メモリ | 123,952 KB |
| 最終ジャッジ日時 | 2025-01-11 19:15:22 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#line 1 "main.cpp"
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) { lhs = rhs; return true; }
return false;
}
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) { lhs = rhs; return true; }
return false;
}
struct range {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { ++i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
constexpr iterator begin() const noexcept { return l; }
constexpr iterator end() const noexcept { return r; }
};
struct revrange {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { --i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
constexpr iterator begin() const noexcept { return r; }
constexpr iterator end() const noexcept { return l; }
};
template <class SemiRing>
class matrix {
public:
using structure = SemiRing;
using value_structure = typename SemiRing::value_structure;
using value_type = typename SemiRing::value_structure::type;
using size_type = size_t;
private:
std::vector<std::vector<value_type>> M_matrix;
public:
matrix() = default;
explicit matrix(size_type H, size_type W,
const value_type &value = value_structure::addition_identity()) { initialize(H, W, value); }
explicit matrix(const std::vector<std::vector<value_type>> &cont) { construct(cont); }
explicit matrix(const std::initializer_list<std::initializer_list<value_type>> &cont) { construct(cont); }
void initialize(size_type H, size_type W, const value_type &value = value_structure::addition_identity()) {
clear();
M_matrix.assign(H, std::vector<value_type>(W, value));
}
void construct(const std::vector<std::vector<value_type>> &cont) {
clear();
M_matrix = cont;
}
void construct(const std::initializer_list<std::initializer_list<value_type>> &cont) {
clear();
if (cont.size() > 0) {
M_matrix.reserve(cont.size());
std::transform(cont.begin(), cont.end(), std::back_inserter(M_matrix), [](const auto &vec) {
return std::vector<value_type>(vec.begin(), vec.end());
});
}
}
void fill(const value_type &value) {
for (auto &vec: M_matrix) {
std::fill(vec.begin(), vec.end(), value);
}
}
matrix operator + (const matrix &rhs) const { return matrix(*this) += rhs; }
matrix& operator += (const matrix &rhs) {
for (size_type i = 0; i < height(); ++i) {
for (size_type j = 0; j < width(); ++j) {
M_matrix[i][j] = value_structure::addition(M_matrix[i][j], rhs.M_matrix[i][j]);
}
}
return *this;
}
matrix& operator *= (const matrix &rhs) { *this = (*this) * rhs; return *this; }
matrix operator * (const matrix &rhs) const {
matrix res(height(), rhs.width());
for (size_type i = 0; i < height(); ++i) {
for (size_type k = 0; k < width(); ++k) {
for (size_type j = 0; j < rhs.width(); ++j) {
res.M_matrix[i][j] = value_structure::addition(res.M_matrix[i][j],
value_structure::multiplication(M_matrix[i][k], rhs.M_matrix[k][j]));
}
}
}
return res;
}
matrix operator * (const value_type &rhs) const { return matrix(*this) *= rhs; }
matrix& operator *= (const value_type &rhs) {
for (auto &vec: M_matrix) {
for (auto &x: vec) {
x = value_structure::multiplication(x, rhs);
}
}
return *this;
}
matrix power(uint64_t exp) const {
matrix res(height(), width()), use(*this);
for (size_type i = 0; i < height(); ++i) {
res[i][i] = value_structure::multiplication_identity();
}
while (exp > 0) {
if (exp & 1) {
res *= use;
}
use *= use;
exp >>= 1;
}
return res;
}
std::vector<value_type>& operator [] (size_type index) {
return M_matrix[index];
}
size_type height() const {
return M_matrix.size();
}
size_type width() const {
if (M_matrix.empty()) return 0;
return M_matrix.front().size();
}
bool empty() const {
return M_matrix.empty();
}
void clear() {
M_matrix.clear();
M_matrix.shrink_to_fit();
}
};
#line 2 "/Users/kodamankod/Desktop/Programming/Library/algebraic/modular.cpp"
#include <cstdint>
#line 5 "/Users/kodamankod/Desktop/Programming/Library/algebraic/modular.cpp"
template <uint32_t Modulus>
class modular {
public:
using value_type = uint32_t;
using max_type = uint64_t;
static constexpr value_type mod = Modulus;
static constexpr value_type get_mod() noexcept { return mod; }
static_assert(mod >= 2, "invalid mod :: smaller than 2");
static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");
template <class T>
static constexpr value_type normalize(T value_) noexcept {
if (value_ < 0) {
value_ = -value_;
value_ %= mod;
if (value_ == 0) return 0;
return mod - value_;
}
return value_ % mod;
}
private:
value_type value;
public:
constexpr modular() noexcept : value(0) { }
template <class T>
explicit constexpr modular(T value_) noexcept : value(normalize(value_)) { }
template <class T>
explicit constexpr operator T() const noexcept { return static_cast<T>(value); }
constexpr value_type get() const noexcept { return value; }
constexpr modular operator - () const noexcept { return modular(mod - value); }
constexpr modular operator ~ () const noexcept { return inverse(); }
constexpr value_type &extract() noexcept { return value; }
constexpr modular inverse() const noexcept { return power(mod - 2); }
constexpr modular power(max_type exp) const noexcept {
modular res(1), mult(*this);
while (exp > 0) {
if (exp & 1) res *= mult;
mult *= mult;
exp >>= 1;
}
return res;
}
constexpr modular operator + (const modular &rhs) const noexcept { return modular(*this) += rhs; }
constexpr modular& operator += (const modular &rhs) noexcept {
if ((value += rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator - (const modular &rhs) const noexcept { return modular(*this) -= rhs; }
constexpr modular& operator -= (const modular &rhs) noexcept {
if ((value += mod - rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator * (const modular &rhs) const noexcept { return modular(*this) *= rhs; }
constexpr modular& operator *= (const modular &rhs) noexcept {
value = (max_type) value * rhs.value % mod;
return *this;
}
constexpr modular operator / (const modular &rhs) const noexcept { return modular(*this) /= rhs; }
constexpr modular& operator /= (const modular &rhs) noexcept { return (*this) *= rhs.inverse(); }
constexpr bool zero() const noexcept { return value == 0; }
constexpr bool operator == (const modular &rhs) const noexcept { return value == rhs.value; }
constexpr bool operator != (const modular &rhs) const noexcept { return value != rhs.value; }
friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
return stream << rhs.value;
}
};
/**
* @title Modint
*/
#line 162 "main.cpp"
using m32 = modular<1000000007>;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
struct semiring {
struct value_structure {
using type = m32;
static type addition_identity() { return m32(0); }
static type addition(const type& v1, const type& v2) {
return v1 + v2;
}
static type multiplication_identity() { return m32(1); }
static type multiplication(const type& v1, const type& v2) {
return v1 * v2;
}
};
};
int main() {
i32 K, M;
std::cin >> K >> M;
i64 N;
std::cin >> N;
using matrix_t = matrix<semiring>;
matrix_t mult(K * K, K * K);
for (auto i: range(0, M)) {
i32 p, q, r;
std::cin >> p >> q >> r;
--p; --q; --r;
mult[K * p + q][K * q + r] = m32(1);
}
matrix_t start(1, K * K);
for (auto i: range(0, K)) {
start[0][K * 0 + i] = m32(1);
}
matrix_t goal = start * mult.power(N - 2);
m32 ans;
for (auto i: range(0, K)) {
ans += goal[0][K * i + 0];
}
std::cout << ans << '\n';
return 0;
}
KoD