結果

問題 No.1050 Zero (Maximum)
ユーザー silviasetitechsilviasetitech
提出日時 2020-05-10 10:29:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 3,044 bytes
コンパイル時間 2,498 ms
コンパイル使用メモリ 74,576 KB
実行使用メモリ 58,168 KB
最終ジャッジ日時 2023-09-21 12:21:59
合計ジャッジ時間 6,451 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,736 KB
testcase_01 AC 117 ms
55,716 KB
testcase_02 AC 174 ms
55,908 KB
testcase_03 AC 167 ms
56,080 KB
testcase_04 AC 223 ms
56,164 KB
testcase_05 AC 245 ms
57,936 KB
testcase_06 AC 191 ms
55,900 KB
testcase_07 AC 188 ms
55,792 KB
testcase_08 AC 147 ms
56,248 KB
testcase_09 AC 156 ms
56,188 KB
testcase_10 AC 263 ms
57,996 KB
testcase_11 AC 235 ms
58,168 KB
testcase_12 AC 120 ms
55,508 KB
testcase_13 AC 116 ms
56,164 KB
testcase_14 AC 114 ms
55,912 KB
testcase_15 AC 116 ms
55,656 KB
testcase_16 AC 271 ms
58,076 KB
testcase_17 AC 261 ms
57,968 KB
権限があれば一括ダウンロードができます

ソースコード

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) {
            if (a == 0) return identity(m.h);
            Matrix half = pow(m, a / 2, MOD);
            return mul(mul(half, half, MOD), a % 2 == 0 ? identity(m.h) : m, MOD);
        }

    }
}

0