結果

問題 No.260 世界のなんとか3
ユーザー 0w1
提出日時 2016-02-07 01:08:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 159,856 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-11-07 18:11:07
合計ジャッジ時間 4,139 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int MAXP = 1e4 + 4;
const int MOD = 1e9 + 7;
#define rep( i, a ) for(int i = 0; i < (a); ++i)

string a, b;
int n, m;

int way[MAXP][2][2][3][8];

int dp(const string &s, int n){
    memset( way, 0, sizeof(way) );
    way[0][0][0][0][0] = 1;
    rep( i, n ) rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, 8 ){
        int lim = j ? 9 : s[i] - '0';
        rep( d, lim + 1 )
            ( way[i + 1][j || d < lim][k || d == 3][ ( l + d ) % 3 ][ ( m * 10 + d ) % 8 ]
            += way[i][j][k][l][m] ) %= MOD;
    }
    int res = 0;
    rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, 8 )
        if( ( k || l == 0 ) && m != 0 )
            ( res += way[n][j][k][l][m] ) %= MOD;
    return res;
}

int match(const string &s, int n){
    int sum = 0, has3 = 0;
    for(int i = 0; i < n; ++i){
        sum += s[i] - '0';
        if( s[i] == '3' ) has3 = 1;
    }
    if( sum % 3 != 0 && has3 == 0 ) return 0;
    int k = 0;
    for(int i = n - 3; i < n; ++i){
        if( i < 0 ) continue;
        k = k * 10 + s[i] - '0';
    }
    return k % 8 != 0;
}

void solve(){
    int cnt_a = dp( a, n );
    int cnt_b = dp( b, m );
    int ans = cnt_b - cnt_a + match( a, n );
    cout << ( ans + MOD ) % MOD << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> a >> b;
    n = a.size(), m = b.size();
    solve();
    return 0;
}
0