結果

問題 No.315 世界のなんとか3.5
ユーザー tsutajtsutaj
提出日時 2018-07-05 18:21:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,359 ms / 2,000 ms
コード長 3,083 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 167,792 KB
実行使用メモリ 154,004 KB
最終ジャッジ日時 2023-09-13 17:58:56
合計ジャッジ時間 18,965 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
153,420 KB
testcase_01 AC 61 ms
153,388 KB
testcase_02 AC 60 ms
153,348 KB
testcase_03 AC 61 ms
153,532 KB
testcase_04 AC 61 ms
153,352 KB
testcase_05 AC 61 ms
153,388 KB
testcase_06 AC 61 ms
153,456 KB
testcase_07 AC 61 ms
153,348 KB
testcase_08 AC 61 ms
153,368 KB
testcase_09 AC 61 ms
153,488 KB
testcase_10 AC 61 ms
153,348 KB
testcase_11 AC 62 ms
153,392 KB
testcase_12 AC 322 ms
153,556 KB
testcase_13 AC 321 ms
153,500 KB
testcase_14 AC 580 ms
153,556 KB
testcase_15 AC 583 ms
153,592 KB
testcase_16 AC 583 ms
153,520 KB
testcase_17 AC 584 ms
153,484 KB
testcase_18 AC 325 ms
153,536 KB
testcase_19 AC 326 ms
153,596 KB
testcase_20 AC 324 ms
153,624 KB
testcase_21 AC 324 ms
153,548 KB
testcase_22 AC 584 ms
153,480 KB
testcase_23 AC 581 ms
153,524 KB
testcase_24 AC 582 ms
153,528 KB
testcase_25 AC 581 ms
153,504 KB
testcase_26 AC 581 ms
153,608 KB
testcase_27 AC 583 ms
153,760 KB
testcase_28 AC 574 ms
153,584 KB
testcase_29 AC 969 ms
153,752 KB
testcase_30 AC 969 ms
153,772 KB
testcase_31 AC 968 ms
153,752 KB
testcase_32 AC 1,103 ms
154,004 KB
testcase_33 AC 587 ms
153,488 KB
testcase_34 AC 1,101 ms
153,796 KB
testcase_35 AC 1,359 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, cP = P;
    while(cP % 10 == 0) d++, cP /= 10;

    long long int N = s.length(), ret = 0;
    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++) {
                        if(N - i == d) {
                            int nmod3 = mod3, nmod8 = mod8;
                            for(int k=0; k<d; k++) nmod3 *= 10, nmod8 *= 10;

                            bool ok = true;
                            if(nmod3 % 3 != 0 && inc3 == 0) ok = false;
                            if(i != 0 && f == 0 && !include_equal) ok = false;
                            if(nmod8 % P != 0) ok = false;
                            
                            if(ok) {
                                // fprintf(stderr, "invalid value = %lld\n", dp[i][mod3][mod8][inc3][f]);
                                fastsub(ret, dp[i][mod3][mod8][inc3][f]);
                            }
                            // dp[i][mod3][mod8][inc3][f] = 0;
                        }

                        if(i == N) continue;
                        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]);
                        }
                    }
                }
            }
        }
    }

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

void trans(string &s) {
    int idx = (int)s.length() - 1;
    while(1) {
        if(s[idx] != '0') {
            int c = s[idx] - '0' - 1;
            s[idx] = (char)('0' + c);
            break;
        }
        else {
            s[idx] = '9';
            idx--;
        }
    }
}

int main() {
    string A, B; int P; cin >> A >> B >> P;
    trans(A);

    long long int rr = solve(B, P, 1);
    // fprintf(stderr, "rr = %lld\n", rr);
    long long int rl = solve(A, P, 1);
    // fprintf(stderr, "rl = %lld\n", rl);
    cout << (rr - rl + MOD) % MOD << endl;
    return 0;
}
0