結果

問題 No.1112 冥界の音楽
ユーザー IKyoproIKyopro
提出日時 2020-07-10 21:56:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,677 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 202,200 KB
実行使用メモリ 49,256 KB
最終ジャッジ日時 2023-08-01 17:49:38
合計ジャッジ時間 38,288 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
6,672 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 168 ms
8,032 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 143 ms
7,360 KB
testcase_05 AC 119 ms
6,576 KB
testcase_06 AC 119 ms
6,784 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 26 ms
5,244 KB
testcase_10 AC 96 ms
6,632 KB
testcase_11 AC 143 ms
7,364 KB
testcase_12 AC 120 ms
7,344 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 614 ms
17,588 KB
testcase_15 AC 493 ms
14,772 KB
testcase_16 AC 588 ms
17,592 KB
testcase_17 AC 609 ms
16,908 KB
testcase_18 AC 611 ms
16,832 KB
testcase_19 AC 589 ms
15,440 KB
testcase_20 AC 589 ms
17,688 KB
testcase_21 AC 565 ms
17,560 KB
testcase_22 AC 609 ms
16,160 KB
testcase_23 AC 446 ms
13,904 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 1,990 ms
48,340 KB
testcase_31 AC 1,988 ms
48,168 KB
testcase_32 TLE -
testcase_33 TLE -
testcase_34 AC 1,961 ms
45,208 KB
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = array<array<mint,216>,216>;

mat mult(mat& A,mat& B){
    int N = A.size();
    mat C{};
    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{};
    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;
        }
    }
    mat res = pow(A,N-4,e);
    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";
}
0