結果

問題 No.260 世界のなんとか3
ユーザー PachicobuePachicobue
提出日時 2017-12-31 04:35:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 3,250 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 215,204 KB
実行使用メモリ 18,080 KB
最終ジャッジ日時 2023-08-23 12:07:52
合計ジャッジ時間 6,212 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 139 ms
18,080 KB
testcase_04 AC 138 ms
18,000 KB
testcase_05 AC 29 ms
8,716 KB
testcase_06 AC 20 ms
6,808 KB
testcase_07 AC 97 ms
15,712 KB
testcase_08 AC 71 ms
16,096 KB
testcase_09 AC 39 ms
9,704 KB
testcase_10 AC 107 ms
14,464 KB
testcase_11 AC 102 ms
16,500 KB
testcase_12 AC 63 ms
13,368 KB
testcase_13 AC 18 ms
5,372 KB
testcase_14 AC 93 ms
15,716 KB
testcase_15 AC 24 ms
6,032 KB
testcase_16 AC 80 ms
13,544 KB
testcase_17 AC 63 ms
10,932 KB
testcase_18 AC 59 ms
9,956 KB
testcase_19 AC 83 ms
15,848 KB
testcase_20 AC 57 ms
11,288 KB
testcase_21 AC 51 ms
11,836 KB
testcase_22 AC 91 ms
13,744 KB
testcase_23 AC 10 ms
4,416 KB
testcase_24 AC 71 ms
14,780 KB
testcase_25 AC 85 ms
17,964 KB
testcase_26 AC 54 ms
17,848 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 107 ms
17,992 KB
testcase_29 AC 167 ms
18,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
vector<T> Vec(int n, T v)
{
    return vector<T>(n, v);
}
template <class... Args>
auto Vec(int n, Args... args)
{
    auto val = Vec(args...);
    return vector<decltype(val)>(n, move(val));
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

constexpr ll MOD = (ll)1e9 + 7LL;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string A, B;
    cin >> A >> B;
    int N = B.size();
    auto dp = Vec(N + 1, 2, 2, 3, 8, 0LL);  //digit, upper, used '3', mod3, mod8
    auto ope = [&](const string& s) {
        dp[0][1][0][0][0] = 1;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    for (int l = 0; l < 3; l++) {
                        for (int m = 0; m < 8; m++) {
                            for (int n = 0; n < 10; n++) {
                                const int mod3 = (l + n) % 3;
                                const int mod8 = (2 * m + n) % 8;
                                const int has3 = ((k == 1 or n == 3) ? 1 : 0);
                                const int upper = ((j == 0 or n != s[i] - '0') ? 0 : 1);
                                if (j == 1 and n > s[i] - '0') {
                                    break;
                                }
                                dp[i + 1][upper][has3][mod3][mod8] += dp[i][j][k][l][m];
                                dp[i + 1][upper][has3][mod3][mod8] %= MOD;
                            }
                        }
                    }
                }
            }
        }
    };
    ope(B);

    ll ans = 0;
    for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
            for (int l = 0; l < 3; l++) {
                for (int m = 1; m < 8; m++) {
                    if (k == 1 or l == 0) {
                        ans += dp[N][j][k][l][m];
                        ans %= MOD;
                    }
                }
            }
        }
    }

    dp.clear();
    N = A.size();
    dp = Vec(N + 1, 2, 2, 3, 8, 0LL);  //digit, upper, used '3', mod3, mod8
    ope(A);
    for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
            for (int l = 0; l < 3; l++) {
                for (int m = 1; m < 8; m++) {
                    if (k == 1 or l == 0) {
                        ans += (MOD - dp[N][j][k][l][m]);
                        ans %= MOD;
                    }
                }
            }
        }
    }

    auto aho = [&]() {
        ll sum = 0;
        for (const char c : A) {
            if (c == '3') {
                return true;
            }
            sum += (c - '0');
        }
        return sum % 3 == 0;
    };
    auto seishun = [&]() {
        const ll n = stoll(A.substr(A.size() - 3, 3));
        return n % 8 == 0;
    };
    if (aho() and not seishun()) {
        ans = (ans + 1) % MOD;
    }

    cout << ans << endl;

    return 0;
}
0