結果

問題 No.503 配列コレクション
ユーザー nebukuro09nebukuro09
提出日時 2017-04-08 00:41:03
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,806 bytes
コンパイル時間 623 ms
コンパイル使用メモリ 103,152 KB
実行使用メモリ 63,008 KB
最終ジャッジ日時 2023-09-03 12:43:49
合計ジャッジ時間 4,553 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
52,324 KB
testcase_01 AC 78 ms
52,944 KB
testcase_02 AC 75 ms
52,244 KB
testcase_03 AC 78 ms
52,616 KB
testcase_04 WA -
testcase_05 AC 92 ms
52,252 KB
testcase_06 AC 77 ms
51,980 KB
testcase_07 AC 75 ms
53,148 KB
testcase_08 AC 80 ms
53,268 KB
testcase_09 AC 75 ms
52,916 KB
testcase_10 AC 78 ms
52,844 KB
testcase_11 AC 87 ms
58,252 KB
testcase_12 AC 95 ms
59,376 KB
testcase_13 AC 93 ms
54,828 KB
testcase_14 AC 87 ms
55,024 KB
testcase_15 AC 82 ms
58,376 KB
testcase_16 AC 90 ms
59,256 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 72 ms
56,364 KB
testcase_23 WA -
testcase_24 AC 79 ms
61,428 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.string;


immutable long MOD = 10^^9 + 7;

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

void main() {
    immutable long MAX = 2*10^^6+1;
    immutable long MOD = 10^^9+7;

    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 H(long a, long b) {
        if (a == 0 && b == 0) return 1;
        if (a == 0) return 0;
        if (b == 0) return 1;
        return f_mod[a+b-1] * f_modinv[a-1] % MOD * f_modinv[b] % MOD;
    }

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

    auto P = new long[](N+1);
    P[0] = 1;
    foreach (i; 0..N) P[i+1] = P[i] * D % MOD;

    auto X = N / (K-1);
    auto Y = N % (K-1);
    if (Y == 0) X -= 1, Y = K;

    if (D == 1) {
        writeln(Y);
        return;
    }

    long ans = 0;
    foreach (i; 0..X+1) { // D^i
        auto nokori = X - i; // nokori 個を (Y-1) 個 に分配する場合の数
        auto tmp = Y * powmod(D, i, MOD) % MOD * H(Y-1, X-i) % MOD;
        ans += Y * powmod(D, i, MOD) % MOD * H(Y-1, X-i) % MOD;
        ans %= MOD;
        //writeln(i, " ", tmp);
    }

    ans.writeln;
}
0