結果

問題 No.665 Bernoulli Bernoulli
ユーザー nebukuro09nebukuro09
提出日時 2019-04-18 21:09:28
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,558 ms / 2,000 ms
コード長 2,050 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 102,252 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 00:38:51
合計ジャッジ時間 26,197 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 1,558 ms
4,376 KB
testcase_03 AC 1,554 ms
4,380 KB
testcase_04 AC 1,463 ms
4,376 KB
testcase_05 AC 1,365 ms
4,376 KB
testcase_06 AC 1,346 ms
4,376 KB
testcase_07 AC 1,304 ms
4,376 KB
testcase_08 AC 1,306 ms
4,376 KB
testcase_09 AC 1,484 ms
4,380 KB
testcase_10 AC 1,309 ms
4,380 KB
testcase_11 AC 1,526 ms
4,376 KB
testcase_12 AC 1,478 ms
4,376 KB
testcase_13 AC 1,543 ms
4,380 KB
testcase_14 AC 1,553 ms
4,380 KB
testcase_15 AC 1,342 ms
4,380 KB
testcase_16 AC 1,403 ms
4,380 KB
testcase_17 AC 1,365 ms
4,380 KB
testcase_18 AC 1,316 ms
4,380 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;

void main() {
    immutable long MOD = 10^^9 + 7;
    
    auto s = readln.split.map!(to!long);
    auto N = s[0];
    auto K = s[1].to!int;
    auto B = new long[](K+2);
    B[0] = 1;
    auto Comb = new Combination();

    foreach (i; 1..K+2) {
        long S = 0;
        foreach (j; 0..i) {
            S += Comb.comb(i+1, j) * B[j] % MOD;
            S %= MOD;
        }
        B[i] = S * powmod(i+1, MOD-2, MOD) % MOD;
        B[i] = B[i] * (MOD-1) % MOD;
    }

    long ber(int n, long x) {
        long ans = 0;
        foreach (i; 0..n+1) {
            long a = Comb.comb(n, i);
            long b = B[n-i];
            long c = powmod(x % MOD, i, MOD);
            ans += a * b % MOD * c % MOD;
            ans %= MOD;
        }
        return ans;
    }

    long ans = ber(K+1, N+1) - ber(K+1, 0);
    ans = (ans % MOD + MOD) % MOD;
    ans = ans * powmod(K+1, MOD-2, MOD) % MOD;
    ans.writeln;
}

class Combination {
    immutable int MAX = 2*10^^4+1;
    immutable long MOD = 10^^9+7;
    long[] modinv;
    long[] f_mod;
    long[] f_modinv;
    
    this() {
        modinv = new long[](MAX);
        modinv[0] = modinv[1] = 1;
        foreach(i; 2..MAX) {
            modinv[i] = modinv[MOD.to!int % i] * (MOD - MOD / i) % MOD;
        }

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

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

    long comb(int n, int k) {
        if (n < k) return 0;
        return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
    }
}

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