結果

問題 No.391 CODING WAR
ユーザー nebukuro09nebukuro09
提出日時 2017-03-10 00:32:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 1,620 ms
コンパイル使用メモリ 101,340 KB
実行使用メモリ 53,800 KB
最終ジャッジ日時 2023-09-03 02:02:56
合計ジャッジ時間 4,851 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
52,408 KB
testcase_01 AC 90 ms
52,016 KB
testcase_02 AC 95 ms
51,440 KB
testcase_03 AC 92 ms
52,312 KB
testcase_04 AC 90 ms
53,800 KB
testcase_05 AC 98 ms
53,044 KB
testcase_06 AC 96 ms
52,568 KB
testcase_07 AC 92 ms
52,956 KB
testcase_08 AC 92 ms
53,136 KB
testcase_09 AC 165 ms
52,356 KB
testcase_10 AC 159 ms
53,028 KB
testcase_11 AC 129 ms
52,464 KB
testcase_12 AC 89 ms
53,160 KB
testcase_13 AC 156 ms
51,616 KB
testcase_14 AC 150 ms
53,348 KB
testcase_15 AC 156 ms
52,664 KB
testcase_16 AC 129 ms
51,456 KB
testcase_17 AC 137 ms
52,980 KB
testcase_18 AC 124 ms
52,808 KB
testcase_19 AC 117 ms
51,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

immutable long MAX = 2*10^^6+1;
immutable long MOD = 10^^9+7;

long powmod(long a, long x, long m) {
    a %= m;
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

void main() {
    auto modinv = new long[](MAX);
    modinv[0] = modinv[1] = 1;
    foreach(i; 2..MAX) {
        modinv[i] = modinv[MOD % i] * (MOD - MOD / i) % MOD;
    }

    auto f_mod = new long[](MAX);
    auto f_modinv = new long[](MAX);
    f_mod[0] = f_mod[1] = 1;
    f_modinv[0] = f_modinv[1] = 1;

    foreach(i; 2..MAX) {
        f_mod[i] = (i * f_mod[i-1]) % MOD;
        f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
    }

    long nck(long n, long k) {
        return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
    }


    auto s = readln.split.map!(to!long);
    auto N = s[0];
    auto M = s[1];

    long ans = 0;
    foreach (i; 0..M) {
        long sign = (i % 2 == 0) ? 1 : -1;
        ans += sign * nck(M, i) * powmod(M-i, N, MOD) % MOD;
        ans %= MOD;
    }

    writeln((ans + MOD) % MOD);
}
0