結果

問題 No.3457 Fibo-shrink
コンテスト
ユーザー InTheBloom
提出日時 2026-02-28 13:47:37
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 1,564 ms / 2,000 ms
コード長 1,609 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,036 ms
コンパイル使用メモリ 187,952 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-02-28 13:47:58
合計ジャッジ時間 10,962 ms
ジャッジサーバーID
(参考情報)
judge3 / judge7
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std;

void main () {
    int K, S, N;
    readln.read(K, S, N);

    const int MOD = 10007;
    auto fib = new int[](N + 1);
    fib[0] = 1;
    fib[1] = 1;
    foreach (i; 2 .. N + 1) {
        fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
    }

    auto A = new int[](N + 1);
    A[0] = 0;
    A[1] = S;
    foreach (i; 2 .. N + 1) {
        foreach (k; 0 .. K + 1) {
            if (i - k - 1 < 0) {
                break;
            }

            A[i] += A[i - k - 1] * mod_inv(fib[k], MOD) % MOD;
        }
        A[i] %= MOD;
    }

    writeln(A[N]);
}

void read (T...) (string S, ref T args) {
    import std.conv : to;
    import std.array : split;
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

long mod_pow (long a, long x, const long MOD)
in {
    assert(0 <= x, "x must satisfy 0 <= x");
    assert(1 <= MOD, "MOD must satisfy 1 <= MOD");
    assert(MOD <= int.max, "MOD must satisfy MOD*MOD <= long.max");
}
do {
    // normalize
    a %= MOD; a += MOD; a %= MOD;

    long res = 1L;
    long base = a;
    while (0 < x) {
        if (0 < (x&1)) (res *= base) %= MOD;
        (base *= base) %= MOD;
        x >>= 1;
    }

    return res % MOD;
}

// check mod_pow
static assert(__traits(compiles, mod_pow(2, 10, 998244353)));

long mod_inv (const long x, const long MOD)
in {
    import std.format : format;
    assert(1 <= MOD, format("MOD must satisfy 1 <= MOD. Now MOD =  %s.", MOD));
    assert(MOD <= int.max, format("MOD must satisfy MOD*MOD <= long.max. Now MOD = %s.", MOD));
}
do {
    return mod_pow(x, MOD-2, MOD);
}
0