結果

問題 No.260 世界のなんとか3
ユーザー pionepione
提出日時 2020-05-25 05:20:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 2,511 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 68,856 KB
実行使用メモリ 11,116 KB
最終ジャッジ日時 2023-08-03 01:25:27
合計ジャッジ時間 4,307 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
10,828 KB
testcase_01 AC 6 ms
10,836 KB
testcase_02 AC 6 ms
10,864 KB
testcase_03 AC 164 ms
10,868 KB
testcase_04 AC 164 ms
10,868 KB
testcase_05 AC 35 ms
10,956 KB
testcase_06 AC 26 ms
10,880 KB
testcase_07 AC 112 ms
11,116 KB
testcase_08 AC 80 ms
10,844 KB
testcase_09 AC 47 ms
10,824 KB
testcase_10 AC 126 ms
11,056 KB
testcase_11 AC 118 ms
11,036 KB
testcase_12 AC 74 ms
10,944 KB
testcase_13 AC 25 ms
10,840 KB
testcase_14 AC 109 ms
10,856 KB
testcase_15 AC 33 ms
10,876 KB
testcase_16 AC 96 ms
10,936 KB
testcase_17 AC 76 ms
10,932 KB
testcase_18 AC 73 ms
11,000 KB
testcase_19 AC 95 ms
10,888 KB
testcase_20 AC 69 ms
10,904 KB
testcase_21 AC 60 ms
10,860 KB
testcase_22 AC 108 ms
11,052 KB
testcase_23 AC 16 ms
10,920 KB
testcase_24 AC 84 ms
10,840 KB
testcase_25 AC 107 ms
10,848 KB
testcase_26 AC 62 ms
10,900 KB
testcase_27 AC 7 ms
10,832 KB
testcase_28 AC 118 ms
10,880 KB
testcase_29 AC 209 ms
10,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
static const int mod = 1e9 + 7;
//dp[i][j][k][l][m]
//整数pを考えた時に、i番目の桁まで考えて、j=1の時は考えている数がp未満であることが決定していて、j=0の時はp以下である。
//k=1の時はすでにi番目までに数字3を含んでいて、k=0の時は含まれていない。 lはi番目の桁まで考えた時のmod3の値
//mはi番目まで考えた時のmod8の値
long long dp[10010][2][2][3][8];
string a, b;

long long slv(string s){
    rep(i, 10010)rep(j, 2)rep(k, 2)rep(l, 3)rep(m, 8){
        dp[i][j][k][l][m] = 0;
    }

    dp[0][0][0][0][0] = 1;
    rep(i, s.size()){
        rep(j, 2){

            int lim;
            if(j == 1) lim = 9;
            else lim = s[i] - '0';
            
            rep(k, 2) rep(l, 3) rep(m, 8) rep(d, lim + 1){//dは末尾に追加する数字
                (dp[i + 1][j || d < lim][k || d == 3][(l + d) % 3][(10 * m + d) % 8] += dp[i][j][k][l][m]) %= mod;
            }
        }
    }

    long long ans = 0;
    rep(j, 2) rep(k, 2) rep(l, 3) rep(m, 8){
        if(k == 1|| l == 0){//3が含まれているまたは3の倍数
            if(m % 8 != 0)
            (ans += dp[s.size()][j][k][l][m]) %= mod;
        }
    }
    return ans;
}

bool hantei(void){
    //数字の3を含んでいるか判定
    bool flag = false;
    rep(i, a.size()){
        if(a[i] == '3'){
            flag = true; break;
        }
    }
    //3の倍数であるかをすべての桁の数字を足して3の倍数であるかで判定
    bool flag_3bai = false;
    long long sum = 0;
    rep(i, a.size()){   
        sum += (a[i] - '0');
    }
    if(sum % 3 == 0) flag_3bai = true;
    //8の倍数であるかを数字が大きい場合下三桁が8の倍数であるかで判定
    bool flag_8bai = false; int tm;
    if(a.size() > 6){
        string tmp = a.substr(a.size() - 3, 3);
        tm = stoi(tmp);
    }else{
        tm = stoi(a);
    }
    if(tm % 8 == 0) flag_8bai = true;

    if((flag || flag_3bai) && !flag_8bai) return true;
    else return false;
}

int main(void){
    cin >> a >> b;
    long long ans_a = slv(a);
    long long ans_b = slv(b);
    
    if(hantei()){//aが条件を満たす数字の時
        cout << (ans_b - ans_a + 1 + mod) % mod<< endl;
    }else{//満たさない時
        cout << (ans_b - ans_a + mod) % mod << endl;
    }
    return 0;
}
0