結果

問題 No.391 CODING WAR
ユーザー nebukuro09nebukuro09
提出日時 2017-03-10 00:28:29
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 103,092 KB
実行使用メモリ 53,852 KB
最終ジャッジ日時 2023-09-03 02:02:47
合計ジャッジ時間 4,813 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
51,512 KB
testcase_01 AC 87 ms
52,212 KB
testcase_02 AC 91 ms
53,568 KB
testcase_03 AC 90 ms
52,548 KB
testcase_04 AC 89 ms
51,648 KB
testcase_05 AC 95 ms
51,484 KB
testcase_06 AC 96 ms
51,908 KB
testcase_07 AC 89 ms
52,820 KB
testcase_08 AC 86 ms
53,164 KB
testcase_09 AC 160 ms
53,060 KB
testcase_10 AC 151 ms
51,752 KB
testcase_11 AC 123 ms
53,852 KB
testcase_12 AC 84 ms
52,872 KB
testcase_13 AC 150 ms
52,532 KB
testcase_14 AC 143 ms
53,576 KB
testcase_15 AC 146 ms
52,512 KB
testcase_16 AC 126 ms
52,100 KB
testcase_17 AC 131 ms
53,532 KB
testcase_18 AC 119 ms
53,076 KB
testcase_19 AC 118 ms
52,628 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