結果

問題 No.260 世界のなんとか3
ユーザー 0w10w1
提出日時 2016-02-07 01:08:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 159,596 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-04-25 07:51:09
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,168 KB
testcase_01 AC 4 ms
7,040 KB
testcase_02 AC 4 ms
7,168 KB
testcase_03 AC 123 ms
7,168 KB
testcase_04 AC 124 ms
7,168 KB
testcase_05 AC 27 ms
7,168 KB
testcase_06 AC 19 ms
7,168 KB
testcase_07 AC 85 ms
7,168 KB
testcase_08 AC 60 ms
7,168 KB
testcase_09 AC 37 ms
7,040 KB
testcase_10 AC 96 ms
7,040 KB
testcase_11 AC 90 ms
7,040 KB
testcase_12 AC 58 ms
7,168 KB
testcase_13 AC 20 ms
7,168 KB
testcase_14 AC 81 ms
7,040 KB
testcase_15 AC 26 ms
7,040 KB
testcase_16 AC 74 ms
7,168 KB
testcase_17 AC 60 ms
7,040 KB
testcase_18 AC 57 ms
7,040 KB
testcase_19 AC 72 ms
7,168 KB
testcase_20 AC 52 ms
7,168 KB
testcase_21 AC 46 ms
7,040 KB
testcase_22 AC 83 ms
7,040 KB
testcase_23 AC 11 ms
7,168 KB
testcase_24 AC 63 ms
7,168 KB
testcase_25 AC 81 ms
7,168 KB
testcase_26 AC 47 ms
7,168 KB
testcase_27 AC 5 ms
7,168 KB
testcase_28 AC 88 ms
7,040 KB
testcase_29 AC 156 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

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