結果

問題 No.315 世界のなんとか3.5
ユーザー nebukuro09nebukuro09
提出日時 2017-03-22 23:21:20
言語 D
(dmd 2.107.1)
結果
TLE  
実行時間 -
コード長 2,221 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 100,472 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 12:33:10
合計ジャッジ時間 21,927 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 11 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 119 ms
4,380 KB
testcase_13 AC 119 ms
4,376 KB
testcase_14 AC 243 ms
4,384 KB
testcase_15 AC 255 ms
4,376 KB
testcase_16 AC 500 ms
4,380 KB
testcase_17 AC 497 ms
4,376 KB
testcase_18 AC 239 ms
4,380 KB
testcase_19 AC 238 ms
4,380 KB
testcase_20 AC 1,391 ms
4,380 KB
testcase_21 AC 1,390 ms
4,376 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 495 ms
4,380 KB
testcase_25 TLE -
testcase_26 AC 501 ms
4,380 KB
testcase_27 AC 243 ms
4,380 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

immutable int MOD = 10^^9+7;

int solve(string N, int P, bool less) {
    auto M = N.length;
    auto dp = new int[][][][][](2, 2, 3, 2, 800);
    // 桁, N未満か, mod3, 3がつくか, mod P


    dp[0][0][0][0][0] = 1;

    foreach (a; 0..M) {
        int cur = a % 2;
        int tar = 1 - cur;
        foreach (b; 0..2) foreach (c; 0..3) foreach (d; 0..2) foreach (e; 0..P) dp[tar][b][c][d][e] = 0;
        foreach (b; 0..2) {
            int digit = b ? 10 : N[a]-'0'+1;
            foreach (c; 0..3) {
                foreach (d; 0..2) {
                    if (M - a < 10) {
                        foreach (e; 0..P) {
                            foreach (f; 0..digit) {
                                dp[tar][b||(f<N[a]-'0')][(c+f)%3][d||(f==3)][(10*e+f)%P] +=
                                    dp[cur][b][c][d][e];
                                dp[tar][b||(f<N[a]-'0')][(c+f)%3][d||(f==3)][(10*e+f)%P] %= MOD;
                            }
                        }
                    }
                    else {
                        foreach (f; 0..digit) {
                            dp[tar][b||(f<N[a]-'0')][(c+f)%3][d||(f==3)][0] +=
                                dp[cur][b][c][d][0];
                            dp[tar][b||(f<N[a]-'0')][(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