結果

問題 No.315 世界のなんとか3.5
ユーザー parukiparuki
提出日時 2016-06-27 15:17:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 2,728 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 167,552 KB
実行使用メモリ 58,140 KB
最終ジャッジ日時 2024-04-20 03:29:40
合計ジャッジ時間 5,477 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
23,288 KB
testcase_01 AC 8 ms
23,148 KB
testcase_02 AC 8 ms
23,168 KB
testcase_03 AC 7 ms
23,168 KB
testcase_04 AC 7 ms
23,248 KB
testcase_05 AC 8 ms
23,260 KB
testcase_06 AC 8 ms
22,912 KB
testcase_07 AC 9 ms
23,192 KB
testcase_08 AC 8 ms
23,040 KB
testcase_09 AC 7 ms
23,112 KB
testcase_10 AC 8 ms
23,040 KB
testcase_11 AC 9 ms
23,168 KB
testcase_12 AC 102 ms
40,576 KB
testcase_13 AC 101 ms
40,704 KB
testcase_14 AC 103 ms
40,704 KB
testcase_15 AC 102 ms
40,536 KB
testcase_16 AC 108 ms
40,704 KB
testcase_17 AC 104 ms
40,576 KB
testcase_18 AC 101 ms
40,664 KB
testcase_19 AC 101 ms
40,704 KB
testcase_20 AC 99 ms
40,792 KB
testcase_21 AC 102 ms
40,668 KB
testcase_22 AC 105 ms
40,800 KB
testcase_23 AC 103 ms
40,812 KB
testcase_24 AC 103 ms
40,784 KB
testcase_25 AC 110 ms
40,624 KB
testcase_26 AC 106 ms
40,800 KB
testcase_27 AC 104 ms
40,832 KB
testcase_28 AC 37 ms
40,932 KB
testcase_29 AC 189 ms
58,072 KB
testcase_30 AC 193 ms
57,812 KB
testcase_31 AC 189 ms
58,140 KB
testcase_32 AC 204 ms
58,048 KB
testcase_33 AC 193 ms
57,860 KB
testcase_34 AC 42 ms
57,940 KB
testcase_35 AC 42 ms
58,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int MOD=(int)1e9+7;
void add(int &x, int y){
    x+=y;
    if(x>=MOD)x-=MOD;
}

const int MAX = (int)2e5 + 1;
int dp1[MAX][2][2][2][3], dp2[10][2][2][2][3][25][32];

char SA_[MAX], SB_[MAX];
string A, B;
int P, LEN, pow5, pow2;

int f(int pos, int greaterThanA, int lessThanB, int use3, int m3, int m25, int m32){
    if(LEN - pos < 6){
        int cur = pos - LEN + 5;
        int &res = dp2[cur][greaterThanA][lessThanB][use3][m3][m25][m32];
        if(res != -1)return res;
        if(pos == LEN){
            if(m3 != 0 && use3 == 0)return res=0;
            else if(m25%pow5==0 && m32%pow2==0) return res=0;
            else return res = 1;
        }
        
        res = 0;
        rep(digit, 10){
            if(greaterThanA == 0 && digit < A[pos])continue;
            if(lessThanB == 0 && digit > B[pos])continue;
            int n3 = (m3 * 10 + digit) % 3;
            int n25 = (m25 * 10 + digit) % 25;
            int n32 = (m32 * 10 + digit) % 32;
            add(res,
                f(pos + 1, greaterThanA | (digit>A[pos]), lessThanB | (digit < B[pos]), use3 | (digit == 3), n3, n25, n32));
        }
        return res;
    } else{
        int & res = dp1[pos][greaterThanA][lessThanB][use3][m3];
        if(res != -1)return res;
        res = 0;
        rep(digit, 10){
            if(greaterThanA == 0 && digit < A[pos])continue;
            if(lessThanB == 0 && digit > B[pos])continue;
            int n3 = (m3 * 10 + digit) % 3;
            add(res,
                f(pos + 1, greaterThanA | (digit>A[pos]), lessThanB | (digit < B[pos]), use3 | (digit == 3), n3, 0, 0));
        }
        return res;
    }
}

int main(){
    while(~scanf("%s%s%d", SA_, SB_, &P)){
        A = string(SA_);
        B = string(SB_);
        int dif = sz(B) - sz(A);
        if(dif)A = string(dif, '0') + A;
        each(c, A)c -= '0';
        each(c, B)c -= '0';
        LEN = sz(A);
        MEM(dp1, -1);
        MEM(dp2, -1);

        for(int i = 25; i; i /= 5)if(P%i == 0){
            pow5 = i;
            break;
        }
        for(int i = 32; i; i /= 2)if(P%i == 0){
            pow2 = i;
            break;
        }

        int ans = f(0, 0, 0, 0, 0, 0, 0);
        cout << ans << endl;
    }
}
0