結果

問題 No.315 世界のなんとか3.5
ユーザー nebukuro09nebukuro09
提出日時 2017-03-23 00:10:23
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 2,162 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 100,320 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 12:34:16
合計ジャッジ時間 11,241 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 10 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 176 ms
4,380 KB
testcase_13 AC 178 ms
4,380 KB
testcase_14 AC 331 ms
4,380 KB
testcase_15 AC 363 ms
4,380 KB
testcase_16 AC 336 ms
4,380 KB
testcase_17 AC 332 ms
4,376 KB
testcase_18 AC 181 ms
4,376 KB
testcase_19 AC 171 ms
4,376 KB
testcase_20 AC 168 ms
4,380 KB
testcase_21 AC 172 ms
4,380 KB
testcase_22 AC 351 ms
4,380 KB
testcase_23 AC 343 ms
4,380 KB
testcase_24 AC 339 ms
4,380 KB
testcase_25 AC 356 ms
4,376 KB
testcase_26 AC 349 ms
4,376 KB
testcase_27 AC 338 ms
4,380 KB
testcase_28 AC 348 ms
4,380 KB
testcase_29 AC 617 ms
4,380 KB
testcase_30 AC 605 ms
4,380 KB
testcase_31 AC 631 ms
4,380 KB
testcase_32 AC 666 ms
4,376 KB
testcase_33 AC 328 ms
4,380 KB
testcase_34 AC 563 ms
4,380 KB
testcase_35 AC 759 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, core.stdc.string;

immutable int MOD = 10^^9+7;
int[800][2][3][2][2] dp;

int solve(ref string N, int P, bool less) {
    auto M = N.length;

    memset(dp[0].ptr, 0, dp[0].sizeof);
    dp[0][0][0][0][0] = 1;
    int cur, tar, dig;

    foreach (a; 0..M) {
        cur = a % 2;
        tar = 1 - cur;
        dig = N[a]-'0';
        memset(dp[tar].ptr, 0, dp[tar].sizeof);
        foreach (b; 0..2) {
            int digit = b ? 10 : dig+1;
            foreach (c; 0..3) {
                foreach (d; 0..2) {
                    if (M - a < 6) {
                        foreach (e; 0..P) {
                            foreach (f; 0..digit) {
                                dp[tar][b||(f<dig)][(c+f)%3][d||(f==3)][(10*e+f)%P] +=
                                    dp[cur][b][c][d][e];
                                dp[tar][b||(f<dig)][(c+f)%3][d||(f==3)][(10*e+f)%P] %= MOD;
                            }
                        }
                    }
                    else {
                        foreach (f; 0..digit) {
                            dp[tar][b||(f<dig)][(c+f)%3][d||(f==3)][0] +=
                                dp[cur][b][c][d][0];
                            dp[tar][b||(f<dig)][(c+f)%3][d||(f==3)][0] %= MOD;
                        }
                    }
                }
            }
        }
    }

    int ret = 0;
    foreach (a; 0..2) {
        if (less && (a == 0)) continue;
        foreach (b; 0..3) {
            foreach (c; 0..2) {
                foreach (d; 1..P) {
                    if (b == 0 || c == 1) {
                        ret += dp[M%2][a][b][c][d];
                        ret %= MOD;
                    }
                }
            }
        }
    }

    return ret;
}

void main() {
    auto s = readln.split;
    auto A = s[0];
    auto B = s[1];
    auto P = s[2].to!int;

    auto a = solve(A, P, true);
    auto b = solve(B, P, false);
    auto ans = (b - a) % MOD;
    ans = (ans + MOD) % MOD;

    ans.writeln;
}
0