結果

問題 No.1973 Divisor Sequence
ユーザー kokatsukokatsu
提出日時 2022-06-10 22:24:06
言語 D
(dmd 2.107.1)
結果
TLE  
実行時間 -
コード長 1,696 bytes
コンパイル時間 2,430 ms
コンパイル使用メモリ 207,120 KB
実行使用メモリ 8,764 KB
最終ジャッジ日時 2023-09-04 17:24:12
合計ジャッジ時間 6,525 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,764 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 9 ms
4,384 KB
testcase_03 AC 5 ms
4,384 KB
testcase_04 AC 10 ms
4,380 KB
testcase_05 AC 10 ms
4,384 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 8 ms
4,380 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 14 ms
4,384 KB
testcase_11 AC 8 ms
4,384 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 4 ms
4,384 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;
import core.checkedint;

void main() {
    long N, M;
    readf("%d %d\n", N, M);

    long MOD = 10 ^^ 9 + 7;

    long S = M.to!real.sqrt.floor.to!long;
    long[] divisors;
    bool[long] isDivisor;
    foreach (i; 1 .. S+1) {
        if (M % i != 0) continue;

        divisors ~= i;
        isDivisor[i] = true;
        if (i * i != M) {
            divisors ~= M / i;
            isDivisor[M/i] = true;
        }
    }

    divisors.sort;

    auto len = divisors.length;
    auto mat = new long[][](len, len);
    foreach (i, d1; divisors) {
        foreach (j, d2; divisors) {
            bool isOver;
            long num = muls(d1, d2, isOver);
            if (isOver || num > M) break;

            if (num in isDivisor) {
                mat[i][j] = 1;
            }
        }
    }

    auto A = matPowMod(mat, N-1, MOD);

    long res;
    foreach (a; A) {
        foreach (x; a) res = (res + x) % MOD;
    }

    res.writeln;
}

long[][] matMulMod(long[][] a, long[][] b, long m) {
    long l = a[0].length.to!long;

    assert(l == b.length);

    auto res = new long[][](a.length, b[0].length);
    foreach (i, r; res) {
        foreach (k; 0 .. l) {
            foreach (j, ref e; r) {
                e = (e + a[i][k] * b[k][j]) % m;
            }
        }
    }

    return res;
}

long[][] matPowMod(long[][] mat, long p, long m) {
    long l = mat[0].length.to!long;

    assert(l == mat.length);

    auto res = new long[][](l, l);
    foreach (i; 0 .. l) {
        res[i][i] = 1;
    }

    while (p > 0) {
        if (p & 1) {
            res = matMulMod(res, mat, m);
        }
        mat = matMulMod(mat, mat, m);
        p >>= 1;
    }

    return res;
}
0