結果

問題 No.260 世界のなんとか3
ユーザー pekempeypekempey
提出日時 2015-07-31 23:52:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,671 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 148,944 KB
実行使用メモリ 11,208 KB
最終ジャッジ日時 2023-08-07 13:57:45
合計ジャッジ時間 3,411 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,004 KB
testcase_01 AC 5 ms
10,964 KB
testcase_02 AC 5 ms
10,920 KB
testcase_03 AC 71 ms
10,988 KB
testcase_04 AC 71 ms
11,004 KB
testcase_05 AC 18 ms
10,964 KB
testcase_06 AC 14 ms
10,960 KB
testcase_07 AC 50 ms
10,996 KB
testcase_08 AC 36 ms
10,988 KB
testcase_09 AC 22 ms
11,024 KB
testcase_10 AC 56 ms
10,952 KB
testcase_11 AC 53 ms
10,996 KB
testcase_12 AC 34 ms
10,952 KB
testcase_13 AC 13 ms
10,940 KB
testcase_14 AC 48 ms
10,984 KB
testcase_15 AC 17 ms
11,068 KB
testcase_16 AC 42 ms
11,112 KB
testcase_17 AC 34 ms
10,956 KB
testcase_18 AC 33 ms
11,168 KB
testcase_19 AC 43 ms
10,988 KB
testcase_20 AC 31 ms
10,972 KB
testcase_21 AC 28 ms
10,944 KB
testcase_22 AC 49 ms
11,028 KB
testcase_23 AC 9 ms
11,148 KB
testcase_24 AC 38 ms
11,208 KB
testcase_25 AC 47 ms
10,964 KB
testcase_26 AC 30 ms
10,972 KB
testcase_27 AC 5 ms
10,956 KB
testcase_28 AC 70 ms
11,048 KB
testcase_29 AC 91 ms
11,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

ll dp[10002][2][2][3][8]; // ind, below, use3, mod3, mod8

ll pow8[10002];

ll solve(string N) {
    memset(dp, 0, sizeof(dp));

    dp[0][0][0][0][0] = 1;
    rep (i, N.length()) {
        rep (j, 2) rep (k, 2) rep (l, 3) rep (m, 8) {
            if (j == 0) {
                int ub = N[i] - '0';

                rep (n, ub + 1) {
                    if (n == ub) {
                        if (n == 3) {
                            (dp[i + 1][0][1][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                        } else {
                            (dp[i + 1][0][k][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                        }
                    } else {
                        if (n == 3) {
                            (dp[i + 1][1][1][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                        } else {
                            (dp[i + 1][1][k][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                        }
                    }
                }
            } else {
                rep (n, 10) {
                    if (n == 3) {
                        (dp[i + 1][1][1][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                    } else {
                        (dp[i + 1][1][k][(l + n) % 3][(m + n * pow8[N.length() - i - 1]) % 8] += dp[i][j][k][l][m]) %= mod;
                    }
                }
            }
        }
    }

    ll ans = 0;
    rep (j, 2) rep (k, 2) rep (l, 3) rep2 (m, 1, 8) {
        if (k || l == 0) {
            (ans += dp[N.length()][j][k][l][m]) %= mod;
        }
    }
    return ans;
}

ll modulo(ll a) {
    a %= mod; a += mod; a %= mod;
    return a;
}

string decrement(string S) {
    int bollow = 1;
    repr (i, S.length()) {
        int p = S[i] - '0';
        if (p - bollow < 0) {
            S[i] = '9';
            bollow = 1;
        } else {
            S[i] = (p - bollow) + '0';
            break;
        }
    }

    return S;
}

int main() {
    pow8[0] = 1;
    rep2 (i, 1, 10002) (pow8[i] = pow8[i - 1] * 10) %= 8;

    string A, B;
    cin >> A >> B;

    ll b = solve(B);
    ll a = solve(decrement(A));

    cout << modulo(b - a) << endl;
}
0