結果

問題 No.260 世界のなんとか3
ユーザー mayoko_mayoko_
提出日時 2015-08-01 09:36:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,894 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 41,344 KB
最終ジャッジ日時 2024-04-25 07:47:53
合計ジャッジ時間 2,564 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
41,344 KB
testcase_01 AC 34 ms
41,216 KB
testcase_02 AC 35 ms
41,216 KB
testcase_03 AC 41 ms
41,216 KB
testcase_04 AC 41 ms
41,344 KB
testcase_05 AC 35 ms
41,344 KB
testcase_06 AC 35 ms
41,344 KB
testcase_07 AC 40 ms
41,344 KB
testcase_08 AC 37 ms
41,344 KB
testcase_09 AC 36 ms
41,344 KB
testcase_10 AC 38 ms
41,216 KB
testcase_11 AC 39 ms
41,344 KB
testcase_12 AC 36 ms
41,344 KB
testcase_13 AC 35 ms
41,344 KB
testcase_14 AC 39 ms
41,216 KB
testcase_15 AC 35 ms
41,344 KB
testcase_16 AC 37 ms
41,344 KB
testcase_17 AC 37 ms
41,344 KB
testcase_18 AC 36 ms
41,344 KB
testcase_19 AC 38 ms
41,216 KB
testcase_20 AC 35 ms
41,344 KB
testcase_21 AC 36 ms
41,344 KB
testcase_22 AC 38 ms
41,216 KB
testcase_23 AC 33 ms
41,344 KB
testcase_24 AC 36 ms
41,344 KB
testcase_25 AC 37 ms
41,344 KB
testcase_26 AC 37 ms
41,344 KB
testcase_27 AC 33 ms
41,088 KB
testcase_28 AC 41 ms
41,344 KB
testcase_29 AC 42 ms
41,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef complex<double> C;

const int MAXN = 100100;
const int MOD = 1e9+7;
int mod10[MAXN];

int dp[MAXN][2][2][3][8]; // big 3exist mod3 mod8

ll calc(const string& s) {
    memset(dp, 0, sizeof(dp));
    int N = s.size();
    dp[0][0][0][0][0] = 1;
    for (int n = 0; n < N; n++) for (int flag = 0; flag < 2; flag++) for (int e = 0; e < 2; e++) for (int m3 = 0; m3 < 3; m3++) for (int m8 = 0; m8 < 8; m8++) {
        if (dp[n][flag][e][m3][m8] == 0) continue;
        if (flag == 0) {
            int num = s[n]-'0';
            for (int i = 0; i < num; i++) {
                // bigは1
                int nm3 = (m3 + i) % 3;
                int nm8 = (m8 + i * mod10[N-1-n]) % 8;
                int ne = e;
                if (i == 3) ne = 1;
                (dp[n+1][1][ne][nm3][nm8] += dp[n][flag][e][m3][m8]) %= MOD;
            }
            // bigは0
            int nm3 = (m3 + num) % 3;
            int nm8 = (m8 + num * mod10[N-1-n]) % 8;
            int ne = e;
            if (num == 3) ne = 1;
            (dp[n+1][0][ne][nm3][nm8] += dp[n][flag][e][m3][m8]) %= MOD;
        } else {
            for (int i = 0; i < 10; i++) {
                // bigは1
                int nm3 = (m3 + i) % 3;
                int nm8 = (m8 + i * mod10[N-1-n]) % 8;
                int ne = e;
                if (i == 3) ne = 1;
                (dp[n+1][1][ne][nm3][nm8] += dp[n][flag][e][m3][m8]) %= MOD;
            }
        }
    }
    ll ans = 0;
    for (int flag = 0; flag < 2; flag++) for (int e = 0; e < 2; e++) for (int m3 = 0; m3 < 3; m3++) for (int m8 = 1; m8 < 8; m8++) {
        if (m3 == 0 || e == 1) (ans += dp[N][flag][e][m3][m8]) %= MOD;
    }
    return (ans % MOD);
}

int check(const string& s) {
    int sum = 0;
    int n = s.size();
    int tmp = 0;
    if (n < 3) tmp = stoi(s);
    else tmp = stoi(s.substr(n-3, 3));
    if (tmp % 8 == 0) return 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '3') return 1;
        sum += (s[i]-'0');
    }
    if (sum%3 == 0) return 1;
    return 0;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string A, B;
    cin >> A >> B;
    mod10[0] = 1;
    for (int i = 1; i < MAXN; i++) mod10[i] = (mod10[i-1]*10) % 8;
    ll ans = calc(B) - calc(A) + check(A);
    ans %= MOD;
    if (ans < 0) ans += MOD;
    cout << ans << endl;
    return 0;
}
0