結果

問題 No.315 世界のなんとか3.5
ユーザー nebukuro09nebukuro09
提出日時 2017-03-22 21:02:58
言語 D
(dmd 2.107.1)
結果
MLE  
実行時間 -
コード長 2,930 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 100,536 KB
実行使用メモリ 608,288 KB
最終ジャッジ日時 2023-09-03 12:32:26
合計ジャッジ時間 6,877 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,388 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 MLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
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[][][][][][][](M+1, 2, 3, 8, 2, 2, 2);
    // 桁, N未満か, mod3, 3がつくか, mod8, 最後から2番目の桁が0か, 最後の桁が0か

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

    foreach (a; 0..M) {
        foreach (b; 0..2) {
            foreach (c; 0..3) {
                foreach (d; 0..8) {
                    foreach (e; 0..2) {
                        foreach (f; 0..2) {
                            foreach (g; 0..2) {
                                int digit = b ? 10 : N[a]-'0'+1;
                                foreach (h; 0..digit) {
                                    int new_d;
                                    if (P == 800 && a >= M-2) new_d = d;
                                    else if (P == 80 && a == M-1) new_d = d;
                                    else new_d = (10*d+h) % 8;
                                    dp[a+1][b||(h<N[a]-'0')][(c+h)%3][new_d][e||(h==3)][g][h>0] +=
                                        dp[a][b][c][d][e][f][g];
                                    dp[a+1][b||(h<N[a]-'0')][(c+h)%3][new_d][e||(h==3)][g][h>0] %= MOD;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    int ret = 0;
    foreach (a; 0..2) {
        if (less && a == 0) continue;
        foreach (b; 0..3) {
            foreach (c; 0..8) {
                foreach (d; 0..2) {
                    foreach (e; 0..2) {
                        foreach (f; 0..2) {
                            if (b == 0 || d == 1) {
                                if (P == 8 && c > 0) {
                                    ret += dp[M][a][b][c][d][e][f];
                                    ret %= MOD;
                                }
                                else if (P == 80 && (c > 0 || (c == 0 && f != 0))) {
                                    ret += dp[M][a][b][c][d][e][f];
                                    ret %= MOD;
                                }
                                else if (P == 800 && (c > 0 || (c == 0 && (e != 0 || f != 0)))) {
                                    ret += dp[M][a][b][c][d][e][f];
                                    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