結果

問題 No.681 Fractal Gravity Glue
ユーザー nebukuro09nebukuro09
提出日時 2018-04-28 00:52:17
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,975 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 102,300 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 20:06:07
合計ジャッジ時間 1,602 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 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, std.bitmanip;

immutable long MOD = 10^^9 + 7;


void main() {
    auto N = readln.chomp.to!long;
    auto s = readln.split.map!(to!long);
    auto n = s[0];
    auto k = s[1];
    long ans = G(n, k);

    long dfs(long n, long k, long rest) {
        if (rest == 0) return 0;
        if (n == 1) return rest;
        long cnt = 0;
        long ret = 0;
        foreach (i; 0..k*2+1) {
            if (i % 2 == 0) {
                long tmp = 1;
                foreach (_; 0..n-1) {
                    tmp *= k+1;
                    if (tmp - 1 > rest) return dfs(n-1, k, rest);
                }
                tmp -= 1;
                if (cnt + tmp >= rest) return (ret + dfs(n-1, k, rest - cnt)) % MOD;
                cnt += tmp;
                ret = (ret + G(n-1, k)) % MOD;
            } else {
                if (cnt + 1 == rest) return (ret + n) % MOD;
                ret = (ret + n) % MOD;
                cnt += 1;
            }
        }
        return ret;
    }

    n = 10000;
    outer: while (true) {
        if (n < 5) break;
        long tmp = 1;
        foreach (_; 0..n) {
            tmp *= k+1;
            if (tmp > N) {
                n -= 1;
                continue outer;
            }
        }
        if (tmp <= N) {
            n += 10;
            break;
        }
    }

    ans = (ans - dfs(n, k, N)) % MOD;
    ans = (ans + MOD) % MOD;
    ans.writeln;
}

long G(long n, long k) {
    long ans;
    ans = powmod(k+1, n+1, MOD);
    ans = (ans - k - 1) % MOD;
    ans = ans * powmod(k, MOD-2, MOD) % MOD;
    ans = (ans - n) % MOD;
    ans = (ans + MOD) % MOD;
    return ans;
}

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