結果

問題 No.1112 冥界の音楽
ユーザー T1610T1610
提出日時 2020-07-12 23:47:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,987 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 174,000 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-04-24 01:19:25
合計ジャッジ時間 15,800 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
10,624 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 260 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 188 ms
5,376 KB
testcase_05 AC 150 ms
5,376 KB
testcase_06 AC 150 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 76 ms
5,376 KB
testcase_10 AC 151 ms
5,376 KB
testcase_11 AC 190 ms
5,376 KB
testcase_12 AC 184 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 926 ms
5,376 KB
testcase_15 AC 740 ms
5,376 KB
testcase_16 AC 878 ms
5,376 KB
testcase_17 AC 982 ms
5,376 KB
testcase_18 AC 998 ms
5,376 KB
testcase_19 AC 778 ms
5,376 KB
testcase_20 AC 934 ms
5,376 KB
testcase_21 AC 882 ms
5,376 KB
testcase_22 AC 852 ms
5,376 KB
testcase_23 AC 668 ms
5,376 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) 
#define dumpL(x) 
#define LINE 
#define LINEL 
#define dumpV(v) 
#define dumpVL(v) 
#define STOP assert(false)
#endif
#define mp make_pair
 
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
    out << '(' << a.fi << ", " << a.se << ')';
    return out;
}
}

template <typename T, typename U> T pow_fast(T x, U n) {
    T ret = (T)1;
    while(n) {
        if(n & (U)1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <typename ModType, ModType MOD> struct ModInt {
    ModType val;
    ModInt() : val(0) {}
    ModInt(ModType x) : val(x % MOD) {
        if(val < 0) val += MOD;
    };
    operator ModType() const { return val; }
    ModInt &operator+=(const ModInt &m) {
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    ModInt &operator-=(const ModInt &m) {
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    ModInt &operator*=(const ModInt &m) {
        (val *= m.val) %= MOD;
        return *this;
    }
    ModInt &operator/=(const ModInt &m) {
        *this *= m.inverse();
        return *this;
    }
    ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; }
    ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; }
    ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; }
    ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; }
    ModInt inverse() const { return pow_fast(*this, MOD - 2); }
    ModInt pow(ModType n) const {return pow_fast(*this, n); }

    friend std::ostream &operator<<(std::ostream &os, const ModInt &rhs) {
        os << rhs.val;
        return os;
    }
    friend std::istream &operator>>(std::istream &is, ModInt &rhs) {
        ModType value;
        is >> value;
        rhs = ModInt(value);
        return is;
    }
};

// https://yukicoder.me/submissions/510918
template<class T>

struct Mat{
    vector<vector<T>> A;
    Mat(){}
    Mat(size_t n,size_t m):A(n,vector<T>(m,0)){}
    Mat(size_t n):A(n,vector<T>(n,0)){};
    size_t height() const{
        return A.size();
    }
    size_t width() const{
        return A[0].size();
    }
    inline const vector<T> &operator[](int k) const{
        return A.at(k);
    }
    inline vector<T> &operator[](int k){
        return A.at(k);
    }
    static Mat I(size_t n){
        Mat mat(n);
        for(int i=0;i<n;i++){
            mat[i][i]=1;
        }
        return mat;
    }
    Mat &operator+=(const Mat &B){
        size_t n=height(),m=width();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                (*this)[i][j]=((*this)[i][j]+B[i][j]);
            }
        }
        return (*this);
    }
    Mat &operator-=(const Mat &B){
        size_t n=height(),m=width();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                (*this)[i][j]=((*this)[i][j]-B[i][j]);
            }
        }
        return (*this);
    }
    Mat &operator*=(const Mat &B){
        int n=height(),m=B.width(),p=width();
        assert(p==B.height());
        vector<vector<T>> C(n,vector<T>(m,0));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                for(int k=0;k<p;k++){
                    C[i][j]=(C[i][j]+(*this)[i][k]*B[k][j]);
                }
            }
        }
        A.swap(C);
        return (*this);
    }
    Mat &operator^=(ll k){
        Mat B=Mat::I(height());
        while(k){
            if(k%2)B*=(*this);
            (*this)*=(*this);
            k>>=1LL;
        }
        A.swap(B.A);
        return (*this);
    }
    Mat operator+(const Mat &B) const{
        return (Mat(*this)+=B);
    }
    Mat operator-(const Mat &B) const{
        return (Mat(*this)-=B);
    }
    Mat operator*(const Mat &B) const{
        return (Mat(*this)*=B);
    }
    Mat operator^(const ll &B) const{
        return (Mat(*this)^=B);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll k, m, n;
    cin >> k >> m >> n;
    vector<vi> a(m, vi(3));
    rep(i, m) {
        rep(j, 3) {
            cin >> a[i][j];
            --a[i][j];
        }
    }
    if(n == 3) {
        int ans = 0;
        rep(i, m) {
            if(a[i].front() == 0 && a[i].front() == a[i].back()) ++ans;
        }
        cout << ans << '\n';
        return 0;
    }
    using mint = ModInt<ll, MOD>;
    const int K = 6;
    const int K3 = 216;
    Mat<mint> mt(K3);
    rep(i, m) rep(j, m) if(a[i][1] == a[j][0] && a[i][2] == a[j][1]) {
        int x = 0, y = 0;
        rep(k, 3) {
            x *= K;
            x += a[i][k];
        }
        rep(k, 3) {
            y *= K;
            y += a[j][k];
        }
        mt[x][y] = mint(1);
    }
    
    auto d = mt^(n-3);
    mint ans = 0;
    rep(i, K*K) {
        int x = i;
        rep(j, K*K) {
            int y = j * K;
            ans += d[x][y];
        }
    }
    cout << ans << '\n';
    return 0;
}
0