結果

問題 No.315 世界のなんとか3.5
ユーザー pythontanukipythontanuki
提出日時 2023-01-05 11:45:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 8,624 bytes
コンパイル時間 4,614 ms
コンパイル使用メモリ 271,336 KB
実行使用メモリ 8,772 KB
最終ジャッジ日時 2023-08-19 09:44:54
合計ジャッジ時間 34,096 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,772 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 877 ms
4,380 KB
testcase_13 AC 879 ms
4,380 KB
testcase_14 AC 1,755 ms
4,380 KB
testcase_15 AC 1,755 ms
4,380 KB
testcase_16 AC 1,754 ms
4,376 KB
testcase_17 AC 1,757 ms
4,380 KB
testcase_18 AC 885 ms
4,380 KB
testcase_19 AC 886 ms
4,380 KB
testcase_20 AC 887 ms
4,380 KB
testcase_21 AC 886 ms
4,384 KB
testcase_22 AC 1,751 ms
4,380 KB
testcase_23 AC 1,756 ms
4,380 KB
testcase_24 AC 1,757 ms
4,380 KB
testcase_25 AC 1,754 ms
4,380 KB
testcase_26 AC 1,758 ms
4,380 KB
testcase_27 AC 1,749 ms
4,376 KB
testcase_28 AC 1,752 ms
4,376 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using mint = modint1000000007;
using C = complex<double>;
const int mod = 998244353;
const long long LINF = 1001002003004005006;
const int INF = 1001001001;
const double PI = acos(-1);
const double EPS = 1e-10;
const int di[4] = {-1,0,1,0};
const int dj[4] = {0,-1,0,1};
const int dx[8] = {1,1,1,0,0,-1,-1,-1};
const int dy[8] = {1,0,-1,1,-1,1,0,-1};
# define sz(x) (int)(x).size()
# define rsz(x,n) x.resize(n)
# define yosupo(x) {cout << (x) << endl; return 0;}
# define ll long long
# define fi first
# define se second
# define pb push_back
# define pf push_front
# define eb emplace_back
# define ef emplace_front
# define pob pop_back
# define pof pop_front
# define GET_MACRO(_1, _2, _3, NAME, ...) NAME
# define _rep(i, n) _rep2(i, 0, n)
# define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
# define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
# define srep(i, a, b) for(int i = a; i <= b; ++i)
# define all(obj) (obj).begin(), (obj).end()
# define rall(obj) (obj).rbegin(), (obj).rend()
inline void YesNo(bool f) { std::cout << (f? "Yes": "No") << std::endl; }
void read() {}
template <typename T, class... U> void read(T &t, U &...u) { cin >> t; read(u...); }
void writeln() { cout << endl; }
template <typename T, class... U, char sep = ' '> void writeln(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; writeln(u...); }
# define Pll pair<ll, ll>
# define P pair<int,int>
# define bit(x,i) (((x) >> (i)) & 1)
# define equals(a, b) (fabs((a) - (b)) < EPS) // 誤差を考慮した同値判定
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << "P(" << p.first << " " << p.second << ")";}
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vi = vc<int>; using vvi = vv<int>;
using vl = vc<ll>; using vvl = vv<ll>;
using vm = vc<mint>; using vvm = vv<mint>;

mint f(string a, int modP) {
    while(a.size() < 10) a = '0'+a;
    int n = sz(a);
    bool is3 = false;
    bool ok3 = false;
    ll tot = 0;
    rep(i,n) {
        if(a[i] == '3') is3 = true;
        tot += (a[i]-'0');
    }
    if(tot % 3 == 0) ok3 = true;
    bool isaho = false;
    string tt;
    reverse(all(a));
    rep(i,5) tt += a[i];
    reverse(all(a));
    reverse(all(tt));
    ll kk = stoll(tt);
    bool is8 = false;
    if(kk%800 != (modP%800)) is8 = true;
    return ((ok3 || is3) && is8);
}

mint solve(string s, int modP) {
    //8の倍数は下3桁が8の倍数かどうか?
    //80の倍数は下4桁が80の倍数かどうか?
    //800の倍数は下5桁が800の倍数かどうか?
    //よって高々下5桁を管理すればいい?
    //桁DPは上から桁を決めていくから, 最後の3-5桁まではdp[i][flag][is3][mod3][0]みたいにしてまとめておく
    //最後の3-5桁だけdp[i][flag][is3][mod3][下3-5桁のmod(8,80,800)]に遷移させる。
    //inline化する
    mint dp[2][2][3][800] = {};
    dp[0][0][0][0] = 1;
    while(s.size() < 10) s = '0'+s;
    int n = sz(s);
    rep(i,n-5) {
        mint p[2][2][3][800] = {};
        swap(dp,p);
        int nd = s[i]-'0';
        rep(j,2) {
            if(j == 0) {
                rep(mod3,3) {
                    if(nd == 3) {
                        dp[0][1][(mod3+nd)%3][0] += p[0][0][mod3][0];
                        dp[0][1][(mod3+nd)%3][0] += p[0][1][mod3][0];
                    } else {
                        dp[0][0][(mod3+nd)%3][0] += p[0][0][mod3][0];
                        dp[0][1][(mod3+nd)%3][0] += p[0][1][mod3][0];
                    }
                }
            } else {
                rep(keta,nd) {
                    if(keta == 3) {
                        rep(mod3,3) {
                            dp[1][1][(mod3+keta)%3][0] += p[0][0][mod3][0];
                            dp[1][1][(mod3+keta)%3][0] += p[0][1][mod3][0];
                        }
                    } else {
                        rep(mod3,3) {
                            dp[1][0][(mod3+keta)%3][0] += p[0][0][mod3][0];
                            dp[1][1][(mod3+keta)%3][0] += p[0][1][mod3][0];
                        }
                    }
                }
                rep(keta,10) {
                    if(keta == 3) {
                        rep(mod3,3) {
                            dp[1][1][(mod3+keta)%3][0] += p[1][0][mod3][0];
                            dp[1][1][(mod3+keta)%3][0] += p[1][1][mod3][0];
                        }
                    } else {
                        rep(mod3,3) {
                            dp[1][1][(mod3+keta)%3][0] += p[1][1][mod3][0];
                            dp[1][0][(mod3+keta)%3][0] += p[1][0][mod3][0];
                        }
                    }
                }
            }
        }
    }

    //2遷移目を考える
    rep(i,n-5,n) {
        mint p[2][2][3][800] = {};
        swap(dp,p);
        int nd = s[i]-'0';
        rep(j,2) {
            if(j == 0) {
                if(nd == 3) {
                    rep(mod3,3) {
                        rep(mod800,800) {
                            dp[0][1][(mod3+nd)%3][(mod800*10+nd)%800] += p[0][0][mod3][mod800];
                            dp[0][1][(mod3+nd)%3][(mod800*10+nd)%800] += p[0][1][mod3][mod800];
                        }
                    }
                } else {
                    rep(mod3,3) {
                        rep(mod800,800) {
                            dp[0][0][(mod3+nd)%3][(mod800*10+nd)%800] += p[0][0][mod3][mod800];
                            dp[0][1][(mod3+nd)%3][(mod800*10+nd)%800] += p[0][1][mod3][mod800];
                        }
                    }
                }
            } else {
                rep(keta,nd) {
                    rep(mod3,3) {
                        rep(mod800,800) {
                            if(keta == 3) {
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[0][0][mod3][mod800];
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[0][1][mod3][mod800];
                            } else {
                                dp[1][0][(mod3+keta)%3][(mod800*10+keta)%800] += p[0][0][mod3][mod800];
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[0][1][mod3][mod800];
                            }
                        }
                    }
                }
                rep(keta,10) {
                    rep(mod3,3) {
                        rep(mod800,800) {
                            if(keta == 3) {
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[1][0][mod3][mod800];
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[1][1][mod3][mod800];
                            } else {
                                dp[1][1][(mod3+keta)%3][(mod800*10+keta)%800] += p[1][1][mod3][mod800];
                                dp[1][0][(mod3+keta)%3][(mod800*10+keta)%800] += p[1][0][mod3][mod800];
                            }
                        }
                    }
                }
            }
        }
    }
    mint ans = 0;
    if(modP == 8) {
        rep(isUnder, 2) rep(is3, 2) rep(mod3, 3) rep(mod800, 800) {
            if ((is3 || !mod3) && ((mod800 != 0) && mod800 % 8 != 0)) ans += dp[isUnder][is3][mod3][mod800];
        }
        return ans;
    } else if(modP == 80) {
        rep(isUnder, 2) rep(is3, 2) rep(mod3, 3) rep(mod800, 800) {
            if ((is3 || !mod3) && ((mod800 != 0) && mod800 % 80 != 0)) ans += dp[isUnder][is3][mod3][mod800];
        }
        return ans;
    }
    rep(isUnder, 2) rep(is3, 2) rep(mod3, 3) rep(mod800, 800) {
        if ((is3 || !mod3) && ((mod800 != 0))) ans += dp[isUnder][is3][mod3][mod800];
    }
    return ans;
}

int main() {
    string a,b; ll p;
    read(a,b,p);
    mint ans = solve(b,p) - solve(a,p) + f(a,p);
    cout << ans.val() << endl;
}
















0