結果

問題 No.1112 冥界の音楽
ユーザー tentententen
提出日時 2022-08-31 09:02:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 2,428 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 77,840 KB
実行使用メモリ 53,320 KB
最終ジャッジ日時 2024-04-25 13:52:18
合計ジャッジ時間 6,645 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
52,532 KB
testcase_01 AC 55 ms
50,412 KB
testcase_02 AC 54 ms
50,032 KB
testcase_03 AC 53 ms
50,032 KB
testcase_04 AC 53 ms
50,312 KB
testcase_05 AC 53 ms
50,132 KB
testcase_06 AC 132 ms
53,080 KB
testcase_07 AC 53 ms
49,988 KB
testcase_08 AC 57 ms
50,004 KB
testcase_09 AC 117 ms
52,320 KB
testcase_10 AC 53 ms
49,908 KB
testcase_11 AC 116 ms
52,788 KB
testcase_12 AC 53 ms
50,424 KB
testcase_13 AC 134 ms
52,968 KB
testcase_14 AC 134 ms
53,320 KB
testcase_15 AC 119 ms
52,836 KB
testcase_16 AC 53 ms
50,324 KB
testcase_17 AC 122 ms
52,816 KB
testcase_18 AC 120 ms
52,796 KB
testcase_19 AC 53 ms
50,052 KB
testcase_20 AC 69 ms
50,860 KB
testcase_21 AC 133 ms
53,152 KB
testcase_22 AC 134 ms
53,104 KB
testcase_23 AC 119 ms
52,840 KB
testcase_24 AC 53 ms
50,404 KB
testcase_25 AC 52 ms
50,484 KB
testcase_26 AC 53 ms
50,248 KB
testcase_27 AC 55 ms
50,060 KB
testcase_28 AC 58 ms
49,904 KB
testcase_29 AC 53 ms
50,248 KB
testcase_30 AC 122 ms
52,812 KB
testcase_31 AC 57 ms
50,528 KB
testcase_32 AC 121 ms
52,828 KB
testcase_33 AC 53 ms
50,460 KB
testcase_34 AC 54 ms
50,548 KB
testcase_35 AC 53 ms
50,012 KB
testcase_36 AC 135 ms
53,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int k = sc.nextInt();
        int m = sc.nextInt();
        long n = sc.nextLong() - 2;
        long[][][] matrix = new long[60][k * k][k * k];
        for (int i = 0; i < m; i++) {
            int p = sc.nextInt() - 1;
            int q = sc.nextInt() - 1;
            int r = sc.nextInt() - 1;
            matrix[0][q * k + r][p * k + q] = 1;
        }
        for (int i = 1; i < 60; i++) {
            for (int a = 0; a < k * k; a++) {
                for (int b = 0; b < k * k; b++) {
                    for (int c = 0; c < k * k; c++) {
                        matrix[i][a][c] += matrix[i - 1][a][b] * matrix[i - 1][b][c];
                        matrix[i][a][c] %= MOD;
                    }
                }
            }
        }
        long[] ans = new long[k * k];
        for (int i = 0; i < k; i++) {
            ans[i] = 1;
        }
        for (int i = 59; i >= 0; i--) {
            if (n < (1L << i)) {
                continue;
            }
            n -= (1L << i);
            long[] nexts = new long[k * k];
            for (int a = 0; a < k * k; a++) {
                for (int b = 0; b < k * k; b++) {
                    nexts[a] += matrix[i][a][b] * ans[b] % MOD;
                    nexts[a] %= MOD;
                }
            }
            ans = nexts;
        }
        long result = 0;
        for (int i = 0; i < k * k; i += k) {
            result += ans[i];
            result %= MOD;
        }
        System.out.println(result);
    }
    
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0