結果

問題 No.260 世界のなんとか3
ユーザー keikei
提出日時 2018-09-29 04:06:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 5,453 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 172,756 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 12:36:53
合計ジャッジ時間 2,952 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 9 ms
6,944 KB
testcase_04 AC 9 ms
6,940 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 7 ms
6,940 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 8 ms
6,944 KB
testcase_11 AC 7 ms
6,940 KB
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 7 ms
6,940 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 6 ms
6,944 KB
testcase_17 AC 6 ms
6,944 KB
testcase_18 AC 5 ms
6,940 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 5 ms
6,944 KB
testcase_22 AC 7 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 6 ms
6,944 KB
testcase_25 AC 6 ms
6,944 KB
testcase_26 AC 6 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 9 ms
6,940 KB
testcase_29 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/260>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

const ll MOD = 1e9+7;

ll dpA[2][3][2][2][1000]; // i桁目,桁和j,3を含むか否か,確実にA以下か,直近3桁
ll dpB[2][3][2][2][1000];
ll solve(){
    ll res = 0;
    string A,B; cin >> A >> B;
    
    
    int curA = 0,nextA = 1;
    for(auto& c:A) c -= '0';
    dpA[curA][0][0][0][0] = 1;
    for(int i = 0; i < A.length();i++){
        for(int j = 0; j < 3;j++){
            for(int k = 0; k < 2;k++){
                for(int l = 0; l < 2;l++){
                    if(i < A.length()-3){
                        int m = 0;
                        if(dpA[curA][j][k][l][m] == 0) continue;
                        int lim = l ? 9 : A[i];
                        for(int d = 0; d <= lim;d++){
                            (dpA[nextA][(j+d)%3][k||(d==3)][l||(d < lim)][m]
                             += dpA[curA][j][k][l][m])%=MOD;
                        }
                        dpA[curA][j][k][l][m] = 0;
                    }else{
                        for(int m = 0; m < 1000;m++){
                            if(dpA[curA][j][k][l][m] == 0) continue;
                            int lim = l ? 9 : A[i];
                            for(int d = 0; d <= lim;d++){
                                (dpA[nextA][(j+d)%3][k||(d==3)][l||(d < lim)][m%100*10+d]
                                 += dpA[curA][j][k][l][m])%=MOD;
                            }
                            dpA[curA][j][k][l][m] = 0;
                        }
                    }
                }
            }
        }
        swap(curA,nextA);
    }
    
    int curB = 0,nextB = 1;
    for(auto& c:B) c -= '0';
    dpB[curB][0][0][0][0] = 1;
    for(int i = 0; i < B.length();i++){
        for(int j = 0; j < 3;j++){
            for(int k = 0; k < 2;k++){
                for(int l = 0; l < 2;l++){
                    if(i < B.length()-3){
                        int m = 0;
                        if(dpB[curB][j][k][l][m] == 0) continue;
                        int lim = l ? 9 : B[i];
                        for(int d = 0; d <= lim;d++){
                            (dpB[nextB][(j+d)%3][k||(d==3)][l||(d < lim)][m]
                             += dpB[curB][j][k][l][m])%=MOD;
                        }
                        dpB[curB][j][k][l][m] = 0;
                    }else{
                        for(int m = 0; m < 1000;m++){
                            if(dpB[curB][j][k][l][m] == 0) continue;
                            int lim = l ? 9 : B[i];
                            for(int d = 0; d <= lim;d++){
                                (dpB[nextB][(j+d)%3][k||(d==3)][l||(d < lim)][m%100*10+d]
                                 += dpB[curB][j][k][l][m])%=MOD;
                            }
                            dpB[curB][j][k][l][m] = 0;
                        }
                    }
                }
            }
        }
        swap(curB,nextB);
    }
    
    
    bool flag = false;
    if(accumulate(A.begin(),A.end(),0LL)%3==0||count(A.begin(),A.end(),3)!=0){
        ll thdigit = 0;
        for(int i = 0; i < A.length();i++){
            thdigit = thdigit*10 + A[i];
            thdigit%=1000;
        }
        if(thdigit%8!=0){
            flag = true;
        }
    }
    
    ll sumA = 0;
    for(int j = 0; j < 3;j++){
        for(int k = 0; k < 2;k++){
            for(int l = 0; l < 2;l++){
                for(int m = 0; m < 1000;m++){
                    if(j==0 || k == 1){
                        if(m%8!=0){
                            (sumA+=dpA[curA][j][k][l][m])%=MOD;
                        }
                    }
                }
            }
        }
    }
    ll sumB = 0;
    for(int j = 0; j < 3;j++){
        for(int k = 0; k < 2;k++){
            for(int l = 0; l < 2;l++){
                for(int m = 0; m < 1000;m++){
                    if(j==0 || k == 1){
                        if(m%8!=0){
                            (sumB+=dpB[curB][j][k][l][m])%=MOD;
                        }
                    }
                }
            }
        }
    }
    
    res = (sumB - sumA + MOD)%MOD;
    (res += flag)%=MOD;
    
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0