結果

問題 No.1112 冥界の音楽
ユーザー kappybarkappybar
提出日時 2021-01-24 12:10:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,828 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 172,768 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-30 19:54:01
合計ジャッジ時間 4,240 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const 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;
        ModInt b=*this;
        while(n>0){
            if(n&1) a*=b;
            b*=b;
            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>;


template<typename T>
struct Matrix{
    int n,m;
    vector<vector<T>> Mat;
    Matrix(int n,int m,T val = 0):n(n),m(m){
        Mat.assign(n,vector<T>(m,val));
    }
    Matrix(vector<vector<T>> Mat):Mat(Mat){
        n = (int)Mat.size();
        m = (int)(Mat[0].size());
    }
    inline const vector<T> &operator[](int i)const{ return Mat[i];}
    inline vector<T> &operator[](int i){ return Mat[i];}
};

template<typename T>
Matrix<T> mul(Matrix<T> &A, Matrix<T> &B) {
    assert(A.m == B.n);
    Matrix<T> C(A.n,B.m);
    for(int i=0;i<A.n;i++){
        for(int k=0;k<B.n;k++){
            for(int j=0;j<B.m;j++){
                C[i][j] = C[i][j] + A[i][k] * B[k][j];
            }
        }
    }
    return C;
}

template<typename T>
Matrix<T> pow(Matrix<T> &A,long long n){
    assert(A.n == A.m);
    Matrix<T> B(A.n,A.n);
    for(int i=0;i<A.n;i++) B[i][i] = 1;
    while(n > 0){
        if(n & 1) B = mul(B,A);
        A = mul(A,A);
        n >>= 1;
    }
    return B;
}

template<typename T>
struct Vector{
    int n;
    vector<T> Vec;
    Vector(int n,T val = 0):n(n){
        Vec.assign(n,val);
    }
    Vector(vector<T> Vec):Vec(Vec){
        int n = (int)Vec.size();
    }
    inline const T &operator[](int i)const{ return Vec[i];}
    inline T &operator[](int i){ return Vec[i];}
};

template<typename T>
Vector<T> mul(Matrix<T> &A,Vector<T> &x){
    assert(A.m == x.n);
    Vector<T> Ax(A.n);
    for(int i=0;i<A.n;i++){
        for(int j=0;j<x.n;j++){
            Ax[i] = Ax[i] + A[i][j] * x[j]; 
        }
    }
    return Ax;
}

int main(){
    ll k,m,n;
    cin >> k >> m >> n;
    Matrix<mint> mat(k*k,k*k);
    auto id = [&](int i,int j){
        return i+j*k;
    };
    rep(i,m){
        int p,q,r;
        cin >> p >> q >> r;
        --p;--q;--r;
        mat[id(p,q)][id(q,r)] = 1;
    }
    mat = pow(mat,n-2);
    mint ans = 0;
    rep(i,k)rep(j,k){
        ans += mat[id(0,i)][id(j,0)];
    }
    cout << ans << endl;
    return 0;
}
0