結果

問題 No.1521 Playing Musical Chairs Alone
ユーザー tentententen
提出日時 2021-11-29 13:40:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 2,211 bytes
コンパイル時間 4,577 ms
コンパイル使用メモリ 78,076 KB
実行使用メモリ 41,908 KB
最終ジャッジ日時 2024-07-02 11:22:22
合計ジャッジ時間 7,756 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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 n = sc.nextInt();
        int k = sc.nextInt();
        int l = sc.nextInt();
        long[][][] matrix = new long[20][n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= l; j++) {
                matrix[0][i][(i - j + n) % n] = 1;
            }
        }
        for (int i = 1; i < 20; 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 = 19; 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;
        }
        StringBuilder sb = new StringBuilder();
        for (long x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0