結果

問題 No.1973 Divisor Sequence
ユーザー kokatsu
提出日時 2022-06-10 22:24:06
言語 D
(dmd 2.109.1)
結果
TLE  
実行時間 -
コード長 1,696 bytes
コンパイル時間 2,675 ms
コンパイル使用メモリ 210,324 KB
実行使用メモリ 18,148 KB
最終ジャッジ日時 2024-06-22 15:25:48
合計ジャッジ時間 6,544 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 TLE * 1 -- * 10
権限があれば一括ダウンロードができます

ソースコード

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