結果

問題 No.260 世界のなんとか3
ユーザー ふーらくたるふーらくたる
提出日時 2016-06-29 14:03:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,661 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 63,092 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-25 07:52:18
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 44 ms
11,008 KB
testcase_04 AC 44 ms
11,008 KB
testcase_05 AC 18 ms
6,144 KB
testcase_06 AC 11 ms
5,376 KB
testcase_07 AC 37 ms
9,856 KB
testcase_08 AC 38 ms
10,112 KB
testcase_09 AC 19 ms
6,656 KB
testcase_10 AC 33 ms
9,216 KB
testcase_11 AC 38 ms
10,368 KB
testcase_12 AC 30 ms
8,704 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 37 ms
9,984 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 31 ms
8,448 KB
testcase_17 AC 23 ms
7,296 KB
testcase_18 AC 19 ms
6,784 KB
testcase_19 AC 36 ms
9,984 KB
testcase_20 AC 23 ms
7,424 KB
testcase_21 AC 25 ms
7,808 KB
testcase_22 AC 30 ms
8,832 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 35 ms
9,344 KB
testcase_25 AC 42 ms
11,008 KB
testcase_26 AC 42 ms
11,008 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 10 ms
11,136 KB
testcase_29 AC 8 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
 * No.260 世界のなんとか3
 *
 * 解法)
 * ways[i+1][a_greater][b_greater][mod3][mod8][has_three]
 */

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int kMOD = 1000 * 1000 * 1000 + 7;
const int kMAX_INDEX = 10010;
string A, B;

int ways[kMAX_INDEX][2][2][3][8][2];

void Solve() {
    vector<int> int_A, int_B;
    // AとBを数列に変換する
    for (int i = 0; i < B.size(); i++) {
        if (i + A.size() < B.size()) int_A.push_back(0);
        else int_A.push_back(A[i - B.size() + A.size()] - '0');
        int_B.push_back(B[i] - '0');
    }

    // 桁dp
    ways[0][0][0][0][0][0] = 1;
    for (int i = 0; i < int_A.size(); i++) {
        for (int greater = 0; greater < 2; greater++) {
            for (int smaller = 0; smaller < 2; smaller++) {
                for (int mod3 = 0; mod3 < 3; mod3++) {
                    for (int mod8 = 0; mod8 < 8; mod8++) {
                        for (int has_three = 0; has_three < 2; has_three++) {
                            if (ways[i][greater][smaller][mod3][mod8][has_three] == 0) continue;

                            for (int digit = 0; digit < 10; digit++) {
                                if (!greater && digit < int_A[i]) continue;
                                if (!smaller && digit > int_B[i]) break;

                                ways[i + 1][greater | digit > int_A[i]][smaller | digit < int_B[i]][(mod3 * 10 + digit) % 3][(mod8 * 10 + digit) % 8][has_three | digit == 3] += ways[i][greater][smaller][mod3][mod8][has_three];
                                ways[i + 1][greater | digit > int_A[i]][smaller | digit < int_B[i]][(mod3 * 10 + digit) % 3][(mod8 * 10 + digit) % 8][has_three | digit == 3] %= kMOD;
                            }
                        }
                    }
                }
            }
        }
    }
    int answer = 0;
    for (int greater = 0; greater < 2; greater++) {
        for (int smaller = 0; smaller < 2; smaller++) {
            // 3で割りきれる
            for (int mod8 = 1; mod8 < 8; mod8++) {
                for (int has_three = 0; has_three < 2; has_three++) {
                    answer = (answer + ways[int_A.size()][greater][smaller][0][mod8][has_three]) % kMOD;
                }
            }
            // 3で割り切れない
            for (int mod3 = 1; mod3 < 3; mod3++) {
                for (int mod8 = 1; mod8 < 8; mod8++) {
                    answer = (answer + ways[int_A.size()][greater][smaller][mod3][mod8][1]) % kMOD;
                }
            }
        }
    }
    cout << answer << endl;
}

int main() {
    cin >> A >> B;

    Solve();
    return 0;
}
0