結果

問題 No.260 世界のなんとか3
ユーザー purple_jwlpurple_jwl
提出日時 2016-07-13 21:56:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 1,435 ms
コンパイル使用メモリ 163,216 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-25 07:52:24
合計ジャッジ時間 2,681 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
10,880 KB
testcase_01 AC 7 ms
10,880 KB
testcase_02 AC 7 ms
11,008 KB
testcase_03 AC 41 ms
11,008 KB
testcase_04 AC 41 ms
10,880 KB
testcase_05 AC 14 ms
10,880 KB
testcase_06 AC 11 ms
10,880 KB
testcase_07 AC 30 ms
11,008 KB
testcase_08 AC 23 ms
10,880 KB
testcase_09 AC 17 ms
10,880 KB
testcase_10 AC 31 ms
10,880 KB
testcase_11 AC 30 ms
10,880 KB
testcase_12 AC 21 ms
10,880 KB
testcase_13 AC 11 ms
10,880 KB
testcase_14 AC 29 ms
10,880 KB
testcase_15 AC 13 ms
11,008 KB
testcase_16 AC 26 ms
10,752 KB
testcase_17 AC 24 ms
10,880 KB
testcase_18 AC 21 ms
10,880 KB
testcase_19 AC 25 ms
10,880 KB
testcase_20 AC 20 ms
10,880 KB
testcase_21 AC 18 ms
10,880 KB
testcase_22 AC 29 ms
10,880 KB
testcase_23 AC 10 ms
11,008 KB
testcase_24 AC 22 ms
10,880 KB
testcase_25 AC 29 ms
11,008 KB
testcase_26 AC 18 ms
10,880 KB
testcase_27 AC 6 ms
11,008 KB
testcase_28 AC 41 ms
11,008 KB
testcase_29 AC 50 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP (i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back

using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll MOD = 1e9 + 7;

ll dp[10000 + 5][2][2][3][8]; // dp[桁][未満][3を含む][mod 3][mod 8]

ll func(string s) {
    memset(dp, 0, sizeof(dp));
    dp[0][0][0][0][0] = 1;

    rep (i, sz(s)) rep (j, 2) {
        int d = j ? 9 : s[i] - '0';
        rep (k, d + 1) rep (p, 2) rep (q, 3) rep (r, 8) {
            dp[i + 1][j | (int)(k < d)][p | (int)(k == 3)][(q + k) % 3][(r * 10 + k) % 8] += dp[i][j][p][q][r];
            dp[i + 1][j | (int)(k < d)][p | (int)(k == 3)][(q + k) % 3][(r * 10 + k) % 8] %= MOD;
        }
    }

    ll ans = 0;
    rep (j, 2) rep (k, 2) rep (p, 3) rep (q, 8) {
        if ((k == 1 || p == 0) && q != 0) {
            ans += dp[sz(s)][j][k][p][q];
            ans %= MOD;
        }
    }

    return ans;
}

string decrement(string s) {
    reverse(all(s));

    vector<int> tmp;

    rep (i, sz(s)) {
        tmp.eb(s[i] - '0');
    }

    tmp[0]--;

    string res = "";
    rep (i, sz(tmp)) {
        if (tmp[i] < 0) {
            tmp[i] += 10;
            tmp[i + 1]--;
        }
        res += (char)(tmp[i] + '0');
    }

    if (sz(res) > 1 && res.back() == '0') {
        res.pop_back();
    }

    reverse(all(res));

    return res;
}

int main() {
    string A, B;
    cin >> A >> B;
    ll ans = (func(B) - func(decrement(A)) + MOD) % MOD;
    cout << ans << endl;
}
0