結果
| 問題 |
No.1112 冥界の音楽
|
| コンテスト | |
| ユーザー |
IKyopro
|
| 提出日時 | 2020-07-10 22:20:58 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,837 bytes |
| コンパイル時間 | 2,431 ms |
| コンパイル使用メモリ | 208,948 KB |
| 最終ジャッジ日時 | 2025-01-11 18:43:21 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 TLE * 1 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
constexpr ll mod = 1e9+7;
struct mint {
ll x;
mint(ll x=0):x((x%mod+mod)%mod){}
friend ostream &operator<<(ostream& os,const mint& a){
return os << a.x;
}
friend istream &operator>>(istream& is,mint& a){
ll t;
is >> t;
a = mint(t);
return (is);
}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
using mat = vvec<mint>;
mat mult(mat& A,mat& B){
int N = A.size();
mat C(N,vec<mint>(N));
for(int i=0;i<N;i++) for(int k=0;k<N;k++) for(int j=0;j<N;j++) C[i][j] += A[i][k]*B[k][j];
return C;
}
mat pow(mat& A,ll n,mat& e){
if(n==0) return e;
mat R = pow(A,n>>1,e);
R = mult(R,R);
if(n&1) R = mult(A,R);
return R;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll K,M,N;
cin >> K >> M >> N;
vec<int> P(M),Q(M),R(M);
for(int i=0;i<M;i++){
cin >> P[i] >> Q[i] >> R[i];
}
if(N==3){
int ans = 0;
for(int i=0;i<M;i++) if(P[i]==1 && R[i]==1) ans++;
cout << ans << "\n";
return 0;
}
mat A(M,vec<mint>(M));
mat E = A;
mat e = A;
for(int i=0;i<M;i++) e[i][i] = 1;
for(int i=0;i<M;i++) for(int j=0;j<M;j++){
if(Q[i]==P[j] && R[i]==Q[j]){
A[i][j] = 1;
if(R[j]==1) E[i][j] = 1;
}
}
vec<mat> B(60,e);
B[0] = A;
for(int i=0;i<59;i++) B[i+1] = mult(B[i],B[i]);
N -= 4;
mat res = e;
for(int i=0;i<60;i++) if(N>>i&1) res = mult(res,B[i]);
res = mult(res,E);
mint ans = 0;
// for(int i=0;i<M;i++) for(int j=0;j<M;j++) cerr << res[i][j] << (j!=M-1? " ":"\n");
for(int i=0;i<M;i++) if(P[i]==1) for(int j=0;j<M;j++) ans += res[i][j];
cout << ans << "\n";
}
IKyopro