結果

問題 No.1112 冥界の音楽
ユーザー tokusakuraitokusakurai
提出日時 2020-07-10 22:07:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 3,244 bytes
コンパイル時間 2,405 ms
コンパイル使用メモリ 212,840 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-01 18:05:38
合計ジャッジ時間 3,842 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

using vec = vector<ll>;
using mat = vector<vec>;

mat mat_mul(mat A, mat B){
    //A(l×m行列)、 B(m×n行列)
    int l = sz(A), m = sz(B), n = sz(B[0]);
    //C(l×n行列)
    mat C(l, vec(n, 0));
    rep(i, l){
        rep(k, m){
            rep(j, n){
                C[i][j] += A[i][k]*B[k][j];
                C[i][j] %= MOD;
            }
        }
    }
    return C;
}

mat mat_pow(mat A, ll k){
    //A(n次正方行列)
    int n = sz(A);
    //B(n次正方行列)
    mat B(n, vec(n, 0));
    rep(i, n) B[i][i] = 1;
    while(k > 0){
        if(k&1) B = mat_mul(B, A);
        A = mat_mul(A, A);
        k >>= 1;
    }
    return B;
}

using vec2 = vector<ld>;
using mat2 = vector<vec2>;

//AをO(m*n^2)で行標準化し、rankを出力する
int standard_mat(mat2 &A){
    int m = sz(A), n = sz(A[0]);
    //今どの行を埋めていきたいか
    int now = 0, ret = 0;
    rep(j, n){
        int pivot = now;
        rep2(i, now, m-1){
            if(abs(A[i][j]) > abs(A[pivot][j])) pivot = i;
        }
        swap(A[now], A[pivot]);
        if(abs(A[now][j]) < EPS) continue;
        ret++;
        rep2(k, j+1, n-1) A[now][k] /= A[now][j];
        A[now][j] = 1;
        rep(i, m){
            if(i == now) continue;
            rep2(k, j+1, n-1) A[i][k] -= A[i][j]*A[now][k];
            A[i][j] = 0;
        }
        now++;
        if(now == m) break;
    }
    return ret;
}

//n元連立一次方程式Ax=bをO(n^3)で解く(解がないまたは一意に定まらない場合は空の配列を出力)
vec2 gausiann_elimination(mat2 A, vec2 b){
    int n = sz(A);
    mat2 B(n, vec2(n+1));
    rep(i, n){
        rep(j, n) B[i][j] = A[i][j];
        B[i][n] = b[i];
    }
    standard_mat(B);
    vec2 x(n);
    rep(i, n){
        if(abs(B[i][i]) < EPS) return vec2(0);
        x[i] = B[i][n];
    }
    return x;
}

int main(){
    int K, M; ll N;
    cin >> K >> M >> N;
    int e[K*K*K];
    fill(e, e+K*K*K, false);
    rep(i, M){
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--, c--;
        e[a*K*K+b*K+c] = true;
    }
    mat A(K*K, vec(K*K, 0));
    rep(i, K*K){
        rep(j, K){
            if(e[i*K+j]) A[i][(i%K)*K+j] = 1;
        }
    }
    A = mat_pow(A, N-2);
    mat x(K*K, vec(1, 0));
    rep(i, K*K) if(i%K == 0) x[i][0] = 1;
    mat res = mat_mul(A, x);
    ll ans = 0;
    rep(i, K*K) if(i/K == 0) ans += res[i][0];
    cout << ans%MOD << endl;
}
0