結果

問題 No.315 世界のなんとか3.5
ユーザー tsutajtsutaj
提出日時 2018-07-05 17:32:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,260 bytes
コンパイル時間 1,624 ms
コンパイル使用メモリ 167,148 KB
実行使用メモリ 154,000 KB
最終ジャッジ日時 2023-09-13 17:57:06
合計ジャッジ時間 18,568 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
153,360 KB
testcase_01 AC 64 ms
153,432 KB
testcase_02 AC 61 ms
153,432 KB
testcase_03 AC 61 ms
153,400 KB
testcase_04 AC 61 ms
153,408 KB
testcase_05 AC 61 ms
153,352 KB
testcase_06 AC 62 ms
153,568 KB
testcase_07 AC 62 ms
153,348 KB
testcase_08 AC 61 ms
153,352 KB
testcase_09 AC 61 ms
153,408 KB
testcase_10 AC 61 ms
153,392 KB
testcase_11 AC 61 ms
153,408 KB
testcase_12 AC 316 ms
153,488 KB
testcase_13 AC 317 ms
153,540 KB
testcase_14 AC 573 ms
153,532 KB
testcase_15 AC 572 ms
153,480 KB
testcase_16 AC 573 ms
153,480 KB
testcase_17 AC 574 ms
153,532 KB
testcase_18 AC 319 ms
153,564 KB
testcase_19 AC 319 ms
153,488 KB
testcase_20 AC 321 ms
153,484 KB
testcase_21 AC 321 ms
153,524 KB
testcase_22 WA -
testcase_23 AC 571 ms
153,520 KB
testcase_24 AC 573 ms
153,484 KB
testcase_25 WA -
testcase_26 AC 572 ms
153,480 KB
testcase_27 AC 574 ms
153,544 KB
testcase_28 AC 564 ms
153,532 KB
testcase_29 AC 946 ms
153,732 KB
testcase_30 AC 946 ms
153,824 KB
testcase_31 AC 948 ms
154,000 KB
testcase_32 AC 1,087 ms
153,688 KB
testcase_33 AC 578 ms
153,632 KB
testcase_34 AC 808 ms
153,756 KB
testcase_35 AC 1,351 ms
153,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const long long int MOD = 1000000007LL;
long long int dp[200010][3][8][2][2];

void fastadd(long long int &A, long long int B) {
    A += B;
    if(A > MOD) A -= MOD;
}

void fastsub(long long int &A, long long int B) {
    A = A - B + MOD;
    if(A > MOD) A -= MOD;
}

long long int solve(string s, int P, bool include_equal) {
    int d = 0;
    while(P % 10 == 0) d++, P /= 10;

    int N = s.length();
    memset(dp, 0, sizeof(dp));
    dp[0][0][0][0][0] = 1;
    for(int i=0; i<N; i++) {
        for(int mod3=0; mod3<3; mod3++) {
            for(int mod8=0; mod8<8; mod8++) {
                for(int inc3=0; inc3<2; inc3++) {
                    for(int f=0; f<2; f++) {
                        int lim = f ? 9 : s[i] - '0';
                        for(int x=0; x<=lim; x++) {
                            int nmod3 = (mod3 * 10 + x) % 3;
                            int nmod8 = (mod8 * 10 + x) % 8;
                            int ninc3 = (inc3 || x == 3);
                            fastadd(dp[i+1][nmod3][nmod8][ninc3][f || x < lim], dp[i][mod3][mod8][inc3][f]);
                        }
                    }
                }
            }
        }
    }

    long long int ret = 0, maybe_ng = 0;
    for(int mod3=0; mod3<3; mod3++) {
        for(int inc3=0; inc3<2; inc3++) {
            if(mod3 != 0 && inc3 == 0) continue;
            for(int mod8=1; mod8<8; mod8++) {
                fastadd(ret, dp[N][mod3][mod8][inc3][1]);
                if(include_equal) {
                    fastadd(ret, dp[N][mod3][mod8][inc3][0]);
                }
            }

            fastadd(maybe_ng, dp[N][mod3][0][inc3][1]);
            if(N - d >= 1) fastsub(maybe_ng, dp[N-d][mod3][0][inc3][1] - (mod3 == 0 && inc3 == 0));

            if(include_equal) {
                fastadd(maybe_ng, dp[N][mod3][0][inc3][0]);
                if(N - d >= 1) fastsub(maybe_ng, dp[N-d][mod3][0][inc3][0]);
            }
        }
    }
    return (ret + maybe_ng - 1) % MOD;
}

int main() {
    string A, B; int P; cin >> A >> B >> P;
    long long int rr = solve(B, P, 1);
    long long int rl = solve(A, P, 0);
    // printf("rr = %lld, rl = %lld\n", rr, rl);
    cout << (rr - rl + MOD) % MOD << endl;
    return 0;
}
0