結果

問題 No.1050 Zero (Maximum)
ユーザー silviasetitechsilviasetitech
提出日時 2020-05-10 10:25:42
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,121 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 74,700 KB
実行使用メモリ 69,788 KB
最終ジャッジ日時 2023-09-21 12:18:28
合計ジャッジ時間 7,365 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
60,380 KB
testcase_01 AC 150 ms
55,908 KB
testcase_02 AC 700 ms
58,592 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        No1050ZeroMaximum solver = new No1050ZeroMaximum();
        solver.solve(1, in, out);
        out.close();
    }

    static class No1050ZeroMaximum {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            final int MOD = (int) 1e9 + 7;
            int m = in.nextInt();
            int k = in.nextInt();

            // 最右項, 始めは1,0,0,0...0
            long[][] res = new long[m][1];
            res[0][0] = 1;

            // i->jに行く組み合わせ.addがあるので全て1からスタートする.
            long[][] mt = new long[m][m];
            ArrayUtil.fill(mt, 1);

            for (int from = 0; from < m; from++) {
                for (int mul = 0; mul < m; mul++) {
                    mt[from][(from * mul) % m]++;
                }
            }

            Matrix ans = Matrix.mul(Matrix.pow(new Matrix(mt), k, MOD), new Matrix(res), MOD);
            out.println(ans.m[0][0]);

        }

    }

    static class ArrayUtil {
        public static void fill(long[][] array, long val) {
            for (long[] a : array) Arrays.fill(a, val);
        }

    }

    static class Matrix {
        public long[][] m;
        public int h;
        public int w;

        public Matrix(int h, int w, long[][] m) {
            this.h = h;
            this.w = w;
            this.m = m;
        }

        public Matrix(long[][] m) {
            this.m = m;
            this.h = m.length;
            this.w = m[0].length;
        }

        public static Matrix identity(int n) {
            long[][] mat = new long[n][n];
            for (int i = 0; i < n; i++) {
                mat[i][i] = 1;
            }
            return new Matrix(mat);
        }

        public static Matrix mul(Matrix m1, Matrix m2, long MOD) {
            long[][] res = new long[m1.h][m2.w];
            long[][] mat1 = m1.m;
            long[][] mat2 = m2.m;

            for (int h = 0; h < m1.h; h++) {
                for (int w = 0; w < m2.w; w++) {
                    for (int x = 0; x < m1.w; x++) {
                        res[h][w] += mat1[h][x] * mat2[x][w];
                        res[h][w] %= MOD;
                    }
                }
            }
            return new Matrix(res);
        }

        public static Matrix pow(Matrix m, long a, long MOD) {
            return a == 0 ? identity(m.h)
                    : mul(mul(pow(m, a / 2, MOD),
                    pow(m, a / 2, MOD), MOD),
                    a % 2 == 0
                            ? identity(m.h)
                            : m, MOD);
        }

    }
}

0