結果

問題 No.1050 Zero (Maximum)
ユーザー tentententen
提出日時 2023-06-21 08:50:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 2,658 bytes
コンパイル時間 3,450 ms
コンパイル使用メモリ 85,512 KB
実行使用メモリ 53,920 KB
最終ジャッジ日時 2023-09-11 04:23:48
合計ジャッジ時間 5,666 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
47,420 KB
testcase_01 AC 49 ms
49,312 KB
testcase_02 AC 128 ms
53,920 KB
testcase_03 AC 103 ms
53,100 KB
testcase_04 AC 120 ms
53,848 KB
testcase_05 AC 125 ms
53,360 KB
testcase_06 AC 110 ms
53,856 KB
testcase_07 AC 117 ms
53,592 KB
testcase_08 AC 52 ms
49,508 KB
testcase_09 AC 115 ms
53,420 KB
testcase_10 AC 129 ms
53,912 KB
testcase_11 AC 124 ms
53,688 KB
testcase_12 AC 47 ms
49,464 KB
testcase_13 AC 46 ms
49,476 KB
testcase_14 AC 45 ms
49,508 KB
testcase_15 AC 46 ms
49,476 KB
testcase_16 AC 130 ms
53,888 KB
testcase_17 AC 126 ms
53,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[][][] matrix = new long[30][n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[0][i][(i + j) % n]++;
                matrix[0][i][(i * j) % n]++;
            }
        }
        for (int i = 1; i < 30; i++) {
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    for (int c = 0; c < n; c++) {
                        matrix[i][a][c] += matrix[i - 1][a][b] * matrix[i - 1][b][c] % MOD;
                        matrix[i][a][c] %= MOD;
                    }
                }
            }
        }
        long[] ans = new long[n];
        ans[0] = 1;
        for (int i = 29; i >= 0; i--) {
            if (k < (1 << i)) {
                continue;
            }
            k -= (1 << i);
            long[] next = new long[n];
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    next[a] += matrix[i][a][b] * ans[b] % MOD;
                    next[a] %= MOD;
                }
            }
            ans = next;
        }
        System.out.println(ans[0]);
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0