結果

問題 No.315 世界のなんとか3.5
ユーザー t98slidert98slider
提出日時 2023-03-06 05:04:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 4,476 bytes
コンパイル時間 1,670 ms
コンパイル使用メモリ 174,172 KB
実行使用メモリ 10,424 KB
最終ジャッジ日時 2023-10-18 05:07:37
合計ジャッジ時間 4,573 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 17 ms
6,644 KB
testcase_13 AC 18 ms
6,644 KB
testcase_14 AC 33 ms
7,180 KB
testcase_15 AC 33 ms
7,180 KB
testcase_16 AC 33 ms
7,188 KB
testcase_17 AC 33 ms
7,188 KB
testcase_18 AC 18 ms
6,644 KB
testcase_19 AC 18 ms
6,644 KB
testcase_20 AC 20 ms
6,644 KB
testcase_21 AC 21 ms
6,644 KB
testcase_22 AC 35 ms
7,328 KB
testcase_23 AC 35 ms
7,328 KB
testcase_24 AC 32 ms
7,188 KB
testcase_25 AC 35 ms
7,328 KB
testcase_26 AC 33 ms
7,188 KB
testcase_27 AC 32 ms
7,180 KB
testcase_28 AC 34 ms
7,328 KB
testcase_29 AC 56 ms
10,424 KB
testcase_30 AC 55 ms
10,284 KB
testcase_31 AC 55 ms
10,276 KB
testcase_32 AC 63 ms
10,424 KB
testcase_33 AC 34 ms
10,072 KB
testcase_34 AC 63 ms
10,424 KB
testcase_35 AC 76 ms
10,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<const unsigned int MOD> struct prime_modint {
    using mint = prime_modint;
    unsigned int v;
    prime_modint() : v(0) {}
    prime_modint(unsigned int a) { a %= MOD; v = a; }
    prime_modint(unsigned long long a) { a %= MOD; v = a; }
    prime_modint(int a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    prime_modint(long long a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    static constexpr int mod() { return MOD; }
    mint& operator++() {v++; if(v == MOD)v = 0; return *this;}
    mint& operator--() {if(v == 0)v = MOD; v--; return *this;}
    mint operator++(int) { mint result = *this; ++*this; return result; }
    mint operator--(int) { mint result = *this; --*this; return result; }
    mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; }
    mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; }
    mint& operator*=(const mint& rhs) {
        v = (unsigned int)((unsigned long long)(v) * rhs.v % MOD);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    mint pow(long long n) const {
        assert(0 <= n);
        mint r = 1, x = *this;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const { assert(v); return pow(MOD - 2); }
    friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; }
    friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; }
    friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; }
    friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; }
    friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); }
    friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); }
    friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; }
};
using mint = prime_modint<1000000007>;
//using mint = prime_modint<998244353>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string A, B;
    int P, C;
    cin >> A >> B >> P;
    if(P == 8) C = 0;
    if(P == 80) C = 1;
    if(P == 800) C = 2;
    //末尾の0がP個以上のときダメ
    //P未満ならOK
    reverse(A.begin(), A.end());
    A[0]--;
    for(int i = 0; i + 1 < A.size(); i++){
        if(A[i] < '0'){
            A[i] += 10;
            A[i + 1]--;
        }
    }
    reverse(A.begin(), A.end());
    auto f = [&](string s){
        int n = s.size(), st = max(0, n - (C + 3)), n2 = min(C + 3, n);
        //桁数,未満,3がつく+3の倍数,8の倍数
        vector<array<array<mint,4>,2>> dp(st + 1);
        dp[0][1][0] = 1;
        for(int i = 0; i < st; i++){
            for(int j = 0; j < 2; j++){
                int r = j ? s[i] - '0' : 9;
                for(int nv = 0; nv <= r; nv++){
                    for(int k = 0; k < 3; k++){
                        dp[i + 1][j & (nv == r)][nv == 3 ? 3 : (k + nv) % 3] += dp[i][j][k];
                    }
                    dp[i + 1][j & (nv == r)][3] += dp[i][j][3];
                }
            }
        }
        mint dp2[n2 + 1][2][4][P];
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 4; j++){
                dp2[0][i][j][0] += dp[st][i][j];
            }
        }
        for(int i = 0; i < n2; i++){
            for(int j = 0; j < 2; j++){
                int r = j ? s[st + i] - '0' : 9;
                for(int l = 0; l < P; l++){
                    for(int nv = 0; nv <= r; nv++){
                        for(int k = 0; k < 3; k++){
                            dp2[i + 1][j & (nv == r)][nv == 3 ? 3 : (k + nv) % 3][(l * 10 + nv) % P] 
                                    += dp2[i][j][k][l];
                        }
                        dp2[i + 1][j & (nv == r)][3][(l * 10 + nv) % P] += dp2[i][j][3][l];
                    }
                }
            }
        }
        mint res;
        for(int i = 0; i < 2; i++){
            for(int j = 0; j <= 3; j += 3){
                for(int k = 1; k < P; k++){
                    res += dp2[n2][i][j][k];
                }
            }
        }
        return res;
    };

    cout << f(B) - f(A) << '\n';
}
0