結果

問題 No.315 世界のなんとか3.5
ユーザー nebukuro09
提出日時 2017-03-22 21:02:58
言語 D
(dmd 2.109.1)
結果
MLE  
実行時間 -
コード長 2,930 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 115,456 KB
実行使用メモリ 786,104 KB
最終ジャッジ日時 2024-06-12 18:26:49
合計ジャッジ時間 5,270 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 MLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

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