結果
問題 |
No.1112 冥界の音楽
|
ユーザー |
|
提出日時 | 2020-07-20 20:30:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 13 ms / 2,000 ms |
コード長 | 2,790 bytes |
コンパイル時間 | 1,980 ms |
コンパイル使用メモリ | 177,060 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-24 11:50:26 |
合計ジャッジ時間 | 3,021 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair<int,int> ; using pll = pair<long long,long long>; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; template<int mod> struct ModInt{ long long x=0; constexpr ModInt(long long x=0):x((x%mod+mod)%mod){} constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;} constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;} constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;} constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;} constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;} constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;} constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;} constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();} ModInt inv() const { long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1; while(s%t!=0){ long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty; s=t;sx=tx;sy=ty; t=u;tx=ux;ty=uy; } return ModInt(tx); } ModInt pow(long long n) const { ModInt a=1; while(n>0){ if(n&1) a*=*this; *this*=*this; n>>=1; } return a; } friend constexpr ostream& operator<<(ostream& os,const ModInt<mod>& a) {return os << a.x;} friend constexpr istream& operator>>(istream& is,ModInt<mod>& a) {return is >> a.x;} }; using mint = ModInt<MOD>; using vec = vector<mint> ; using mat = vector<vec>; mat mul(mat &A, mat &B) { mat C(A.size(),vec(B[0].size())); for(int i=0;i<A.size();i++){ for(int k=0;k<B.size();k++){ for(int j=0;j<B[0].size();j++){ C[i][j] = C[i][j] + A[i][k]*B[k][j]; } } } return C; } mat poww(mat A,ll n){ mat B(A.size(), vec(A.size())); for(int i=0;i<A.size();i++) B[i][i] = 1; while(n > 0){ if(n & 1) B = mul(B,A); A = mul(A,A); n >>= 1; } return B; } int main(){ int k,m; cin >> k >> m; ll n; cin >> n; vector<int> p(m),q(m),r(m); rep(i,m) cin >> p[i] >> q[i] >> r[i]; rep(i,m) --p[i],--q[i],--r[i]; auto id = [&](int a,int b){return a+b*k;}; int t = k*k; mat A(t,vector<mint>(t,0)); rep(i,m){ A[id(p[i],q[i])][id(q[i],r[i])] = 1; } A = poww(A,n-2); mint ans = 0; rep(i,k)rep(j,k){ ans += A[id(0,i)][id(j,0)]; } cout << ans << endl; return 0; }