結果

問題 No.260 世界のなんとか3
ユーザー mamekinmamekin
提出日時 2016-10-09 22:18:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,115 bytes
コンパイル時間 1,179 ms
コンパイル使用メモリ 108,824 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-14 05:48:40
合計ジャッジ時間 4,594 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 109 ms
4,376 KB
testcase_04 AC 111 ms
4,380 KB
testcase_05 AC 22 ms
4,380 KB
testcase_06 AC 15 ms
4,376 KB
testcase_07 AC 75 ms
4,380 KB
testcase_08 AC 52 ms
4,384 KB
testcase_09 AC 30 ms
4,380 KB
testcase_10 AC 85 ms
4,384 KB
testcase_11 AC 80 ms
4,376 KB
testcase_12 AC 49 ms
4,376 KB
testcase_13 AC 15 ms
4,380 KB
testcase_14 AC 72 ms
4,376 KB
testcase_15 AC 20 ms
4,380 KB
testcase_16 AC 63 ms
4,376 KB
testcase_17 AC 50 ms
4,380 KB
testcase_18 AC 48 ms
4,380 KB
testcase_19 AC 62 ms
4,376 KB
testcase_20 AC 44 ms
4,376 KB
testcase_21 AC 39 ms
4,380 KB
testcase_22 AC 71 ms
4,384 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 56 ms
4,376 KB
testcase_25 AC 64 ms
4,380 KB
testcase_26 AC 47 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 108 ms
4,376 KB
testcase_29 AC 125 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 1000000007;

int encode(int a, int b, int c, int d)
{
    return ((a * 2 + b) * 3 + c) * 8 + d;
}

tuple<int, int, int, int> decode(int x)
{
    tuple<int, int, int, int> ans;
    get<3>(ans) = x % 8;
    x /= 8;
    get<2>(ans) = x % 3;
    x /= 3;
    get<1>(ans) = x % 2;
    x /= 2;
    get<0>(ans) = x;
    return ans;
}

int solve(const string& s)
{
    int n = s.size();
    vector<int> dp(96, 0);
    dp[encode(1, 0, 0, 0)] = 1;

    for(int i=0; i<n; ++i){
        vector<int> nextDp(96, 0);
        for(int x=0; x<96; ++x){
            int a, b, c, d;
            tie(a, b, c, d) = decode(x);

            for(int j=0; j<=9; ++j){
                if(a == 1 && s[i] - '0' < j)
                    continue;

                int a2 = (a == 1 && s[i] - '0' == j) ? 1 : 0;
                int b2 = (b == 1 || j == 3) ? 1 : 0;
                int c2 = (c * 10 + j) % 3;
                int d2 = (d * 10 + j) % 8;

                int y = encode(a2, b2, c2, d2);
                nextDp[y] += dp[x];
                nextDp[y] %= MOD;
            }
        }
        dp.swap(nextDp);
    }

    int ans = 0;
    for(int x=0; x<96; ++x){
        int a, b, c, d;
        tie(a, b, c, d) = decode(x);
        if((b == 1 || c == 0) && d != 0){
            ans += dp[x];
            ans %= MOD;
        }
    }
    return ans;
}

int main()
{
    string a, b;
    cin >> a >> b;

    unsigned i = a.find_last_not_of('0');
    -- a[i];
    while(++ i < a.size())
        a[i] = '9';
    if(a[0] == '0')
        a = a.substr(1);

    int ans = solve(b) - solve(a);
    ans %= MOD;
    ans += MOD;
    ans %= MOD;
    cout << ans << endl;

    return 0;
}
0