結果

問題 No.315 世界のなんとか3.5
ユーザー 0w1
提出日時 2016-02-23 13:18:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 461 ms / 2,000 ms
コード長 2,276 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 161,484 KB
実行使用メモリ 22,892 KB
最終ジャッジ日時 2024-09-22 13:08:52
合計ジャッジ時間 7,424 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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