結果

問題 No.391 CODING WAR
ユーザー cedretabercedretaber
提出日時 2020-06-28 05:38:52
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,424 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 95,872 KB
実行使用メモリ 4,412 KB
最終ジャッジ日時 2023-09-04 08:21:58
合計ジャッジ時間 2,421 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 6 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 6 ms
4,408 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 94 ms
4,376 KB
testcase_10 AC 84 ms
4,412 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 81 ms
4,376 KB
testcase_14 AC 71 ms
4,384 KB
testcase_15 AC 79 ms
4,380 KB
testcase_16 AC 51 ms
4,380 KB
testcase_17 AC 58 ms
4,376 KB
testcase_18 AC 41 ms
4,380 KB
testcase_19 AC 42 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;

long P = 10^^9+7;
long[10^^5+50] F, RF;

long pow(long x, long n) {
    long y = 1;
    while (n) {
        if (n%2 == 1) y = (y * x) % P;
        x = x^^2 % P;
        n /= 2;
    }
    return y;
}

long inv(long x)
{
    return pow(x, P-2);
}

void init()
{
    F[0] = F[1] = 1;
    foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;

    {
        RF[$-1] = 1;
        auto x = F[$-1];
        auto k = P-2;
        while (k) {
            if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
            x = x^^2 % P;
            k /= 2;
        }
    }
    foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}

long comb(N)(N n, N k)
{
    if (k > n) return 0;
    auto n_b = F[n];    // n!
    auto nk_b = RF[n-k]; // 1 / (n-k)!
    auto k_b = RF[k];    // 1 / k!

    auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!

    return (n_b * nk_b_k_b) % P;  // n! / (n-k)!k!
}

long perm(N)(N n, N k)
{
    if (k > n) return 0;
    auto n_b = F[n];
    auto n_k_b = RF[n-k];
    return (n_b * n_k_b) % P;
}

void main()
{
    init();
    auto nm = readln.split.to!(long[]);
    auto N = nm[0];
    auto M = nm[1];
    if (M > N) {
        writeln(0);
        return;
    }
    long r;
    foreach (e; 0..M) {
        (r += comb(M, e) * pow(M-e, N) % P * (-1)^^e + P) %= P;
    }
    writeln(r);
}
0