結果

問題 No.315 世界のなんとか3.5
ユーザー mamekinmamekin
提出日時 2015-12-20 13:24:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 97,588 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 14:22:48
合計ジャッジ時間 7,043 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
4,348 KB
testcase_01 AC 35 ms
4,348 KB
testcase_02 AC 46 ms
4,348 KB
testcase_03 AC 59 ms
4,348 KB
testcase_04 AC 66 ms
4,348 KB
testcase_05 AC 50 ms
4,348 KB
testcase_06 AC 55 ms
4,348 KB
testcase_07 AC 50 ms
4,348 KB
testcase_08 AC 51 ms
4,348 KB
testcase_09 AC 48 ms
4,348 KB
testcase_10 AC 43 ms
4,348 KB
testcase_11 AC 57 ms
4,348 KB
testcase_12 AC 105 ms
4,348 KB
testcase_13 AC 91 ms
4,348 KB
testcase_14 AC 155 ms
4,348 KB
testcase_15 AC 154 ms
4,348 KB
testcase_16 AC 145 ms
4,348 KB
testcase_17 AC 143 ms
4,348 KB
testcase_18 AC 104 ms
4,348 KB
testcase_19 AC 114 ms
4,348 KB
testcase_20 AC 99 ms
4,348 KB
testcase_21 AC 107 ms
4,348 KB
testcase_22 AC 161 ms
4,348 KB
testcase_23 AC 153 ms
4,348 KB
testcase_24 AC 158 ms
4,348 KB
testcase_25 AC 146 ms
4,348 KB
testcase_26 AC 154 ms
4,348 KB
testcase_27 AC 156 ms
4,348 KB
testcase_28 AC 146 ms
4,348 KB
testcase_29 AC 229 ms
4,348 KB
testcase_30 AC 234 ms
4,348 KB
testcase_31 AC 227 ms
4,348 KB
testcase_32 AC 253 ms
4,348 KB
testcase_33 AC 150 ms
4,348 KB
testcase_34 AC 251 ms
4,348 KB
testcase_35 AC 298 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#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>
using namespace std;

const int MOD = 1000000007;

int solve(const string& s, int p)
{
    int n = s.size();

    vector<vector<int> > dp(4, vector<int>(3, 0));
    dp[0][0] = 1;
    for(int i=0; i<n-5; ++i){
        vector<vector<int> > nextDp(4, vector<int>(3, 0));
        for(int a=0; a<4; ++a){
            for(int b=0; b<3; ++b){
                for(int x=0; x<=9; ++x){
                    if(!(a & 2) && x > s[i] - '0')
                        continue;

                    int a2 = a;
                    if(x == 3)
                        a2 |= 1;
                    if(x < s[i] - '0')
                        a2 |= 2;
                    int b2 = (b + x) % 3;

                    nextDp[a2][b2] += dp[a][b];
                    nextDp[a2][b2] %= MOD;
                }
            }
        }
        dp.swap(nextDp);
    }

    int y = stoi(s.substr(max(0, n - 5)));
    int ans = 0;
    for(int a=0; a<4; ++a){
        for(int b=0; b<3; ++b){
            for(int x=0; x<100000; ++x){
                if(!(a & 2) && x > y)
                    continue;

                bool isUse3 = a & 1;
                if(to_string(x).find('3') != string::npos)
                    isUse3 = true;
                int b2 = (b + x) % 3;

                if((isUse3 || b2 == 0) && x % p != 0){
                    ans += dp[a][b];
                    ans %= MOD;
                }
            }
        }
    }
    return ans;
}

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

    int i = a.size() - 1;
    while(a[i] == '0'){
        a[i] = '9';
        -- i;
    }
    -- a[i];

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

    return 0;
}
0