結果

問題 No.1112 冥界の音楽
ユーザー IKyoproIKyopro
提出日時 2020-07-10 22:10:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,671 bytes
コンパイル時間 1,955 ms
コンパイル使用メモリ 198,928 KB
実行使用メモリ 49,152 KB
最終ジャッジ日時 2024-04-19 17:22:10
合計ジャッジ時間 15,766 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
6,816 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 194 ms
8,320 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 165 ms
7,680 KB
testcase_05 AC 143 ms
6,912 KB
testcase_06 AC 141 ms
6,912 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 30 ms
5,376 KB
testcase_10 AC 115 ms
6,784 KB
testcase_11 AC 175 ms
7,424 KB
testcase_12 AC 143 ms
7,424 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 719 ms
17,792 KB
testcase_15 AC 587 ms
14,720 KB
testcase_16 AC 698 ms
17,792 KB
testcase_17 AC 739 ms
17,024 KB
testcase_18 AC 728 ms
17,152 KB
testcase_19 AC 689 ms
15,616 KB
testcase_20 AC 698 ms
17,664 KB
testcase_21 AC 674 ms
17,664 KB
testcase_22 AC 724 ms
16,256 KB
testcase_23 AC 530 ms
14,080 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
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;
    }
};

constexpr int X = 216;

using mat = array<array<mint,X>,X>;

mat mult(mat& A,mat& B){
    mat C{};
    for(int i=0;i<X;i++) for(int k=0;k<X;k++) for(int j=0;j<X;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{};
    mat e{};
    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