結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
53,788 KB
testcase_01 AC 95 ms
52,240 KB
testcase_02 AC 86 ms
52,552 KB
testcase_03 AC 85 ms
51,656 KB
testcase_04 AC 85 ms
51,776 KB
testcase_05 AC 86 ms
52,420 KB
testcase_06 AC 85 ms
52,836 KB
testcase_07 AC 88 ms
52,032 KB
testcase_08 AC 89 ms
52,476 KB
testcase_09 AC 162 ms
52,120 KB
testcase_10 AC 155 ms
51,876 KB
testcase_11 AC 123 ms
52,444 KB
testcase_12 AC 87 ms
53,032 KB
testcase_13 AC 146 ms
51,584 KB
testcase_14 AC 142 ms
51,756 KB
testcase_15 AC 149 ms
53,740 KB
testcase_16 AC 125 ms
52,260 KB
testcase_17 AC 134 ms
52,420 KB
testcase_18 AC 119 ms
53,188 KB
testcase_19 AC 120 ms
52,308 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 p = nck(M, i) * powmod(M-i, N, MOD) % MOD;
        if (i % 2 == 0)
            ans = (ans + p) % MOD;
        else {
            ans = (ans - p) % MOD;
            ans = (ans + MOD) % MOD;
        }
    }

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