結果

問題 No.315 世界のなんとか3.5
ユーザー 0w10w1
提出日時 2016-02-23 13:18:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 460 ms / 2,000 ms
コード長 2,276 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 163,248 KB
実行使用メモリ 22,972 KB
最終ジャッジ日時 2023-10-23 19:24:38
合計ジャッジ時間 7,212 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
22,800 KB
testcase_01 AC 8 ms
22,800 KB
testcase_02 AC 14 ms
22,800 KB
testcase_03 AC 8 ms
22,800 KB
testcase_04 AC 8 ms
22,800 KB
testcase_05 AC 9 ms
22,800 KB
testcase_06 AC 15 ms
22,800 KB
testcase_07 AC 9 ms
22,800 KB
testcase_08 AC 9 ms
22,800 KB
testcase_09 AC 8 ms
22,800 KB
testcase_10 AC 9 ms
22,800 KB
testcase_11 AC 8 ms
22,800 KB
testcase_12 AC 95 ms
22,924 KB
testcase_13 AC 95 ms
22,924 KB
testcase_14 AC 181 ms
22,932 KB
testcase_15 AC 180 ms
22,932 KB
testcase_16 AC 181 ms
22,932 KB
testcase_17 AC 181 ms
22,932 KB
testcase_18 AC 97 ms
22,928 KB
testcase_19 AC 96 ms
22,928 KB
testcase_20 AC 101 ms
22,928 KB
testcase_21 AC 102 ms
22,928 KB
testcase_22 AC 185 ms
22,932 KB
testcase_23 AC 185 ms
22,932 KB
testcase_24 AC 181 ms
22,932 KB
testcase_25 AC 186 ms
22,932 KB
testcase_26 AC 180 ms
22,932 KB
testcase_27 AC 180 ms
22,932 KB
testcase_28 AC 182 ms
22,932 KB
testcase_29 AC 306 ms
22,972 KB
testcase_30 AC 302 ms
22,972 KB
testcase_31 AC 301 ms
22,972 KB
testcase_32 AC 357 ms
22,972 KB
testcase_33 AC 188 ms
22,932 KB
testcase_34 AC 256 ms
22,972 KB
testcase_35 AC 460 ms
22,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXLOG = 2e5 + 5;
const int MAXP = 800; // P could be 8, 80, 800
#define rep( i, a ) for(int i = 0; i < ( a ); ++i)

string a, b;
int p;

ll wayn_5[MAXLOG][2][2][3]; // first n - 5 digits: pos, less, has three, % 3
ll way5[6][2][2][3][800]; // last 5 digits: pos, less, has three, % 3, % P

ll dp(const string &s){
    memset( wayn_5, 0, sizeof(wayn_5) );
    memset( way5, 0, sizeof(way5) );
    wayn_5[0][0][0][0] = 1LL;
    int z = max<int>( 0, s.size() - 5 );
    rep( i, z ) rep( j, 2 ) rep( k, 2 ) rep( l, 3 ){
        int lim = j ? 9 : s[ i ] - '0';
        rep( d, lim + 1 )
            ( wayn_5[i + 1][j || d < lim][k || d == 3][( l + d ) % 3]
            += wayn_5[i][j][k][l] ) %= MOD;
    }
    rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) // move things to the important part
        way5[0][j][k][l][0] = wayn_5[z][j][k][l]; // % P = 0 because not yet determined
    int _z = s.size() - z; // last <= 5 digits that are crucial to mod P
    rep( i, _z ) rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, p ){
        int lim = j ? 9 : s[ z + i ] - '0';
        rep( d, lim + 1 )
            ( way5[i + 1][j || d < lim][k || d == 3 ][( l + d ) % 3][( m * 10 + d ) % p]
              += way5[i][j][k][l][m] ) %= MOD;
    }
    ll ans = 0LL;
    rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, p ){
        if( ( k || l == 0 ) && m != 0 )
            ( ans += way5[_z][j][k][l][m] ) %= MOD;
    }
    return ans;
}

int digCnt(int x){ int res = 0; while( x > 0 ) x /= 10, ++res; return res; }

bool match(const string &s){
    int dsum = 0, has3 = 0;
    rep( i, s.size() ){
        dsum += s[ i ] - '0';
        if( s[ i ] == '3' ) has3 = 1;
    }
    if( dsum % 3 != 0 && !has3 ) return false;

    int x = digCnt( p );
    if( s.size() < x ) return true;
    dsum = 0;
    for(int i = (int)s.size() - 2 - x; i < (int)s.size(); ++i){
        if( i < 0 ) continue;
        dsum = dsum * 10 + s[ i ] - '0';
    }
    return dsum % p != 0;
}

void solve(){
    ll ans = dp( b ) - dp( a );
    if( match( a ) ) ++ans;
    ans %= MOD; ans += MOD; ans %= MOD;
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> a >> b >> p;
    solve();
    return 0;
}
0