結果

問題 No.260 世界のなんとか3
ユーザー kurenai3110kurenai3110
提出日時 2016-08-18 21:36:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,302 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 57,560 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-25 09:02:34
合計ジャッジ時間 3,151 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 133 ms
11,008 KB
testcase_04 AC 131 ms
10,752 KB
testcase_05 AC 26 ms
5,376 KB
testcase_06 AC 18 ms
5,376 KB
testcase_07 AC 92 ms
8,576 KB
testcase_08 AC 64 ms
7,040 KB
testcase_09 AC 37 ms
5,376 KB
testcase_10 AC 99 ms
9,088 KB
testcase_11 AC 96 ms
8,704 KB
testcase_12 AC 57 ms
6,528 KB
testcase_13 AC 18 ms
5,376 KB
testcase_14 AC 87 ms
8,192 KB
testcase_15 AC 24 ms
5,376 KB
testcase_16 AC 75 ms
7,808 KB
testcase_17 AC 57 ms
6,784 KB
testcase_18 AC 57 ms
6,528 KB
testcase_19 AC 74 ms
7,552 KB
testcase_20 AC 54 ms
6,144 KB
testcase_21 AC 47 ms
6,016 KB
testcase_22 AC 87 ms
8,192 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 68 ms
7,168 KB
testcase_25 AC 85 ms
7,168 KB
testcase_26 AC 49 ms
7,296 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 133 ms
11,008 KB
testcase_29 AC 171 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
using namespace std;

#define MOD (int)1e9+7
#define rep(i,n) for(int i=0;i<n;i++)

//桁DP
int DP[2][101010][2][2][3][8];	//ab, position, less, has3, mod3, mod8
								//DP[ab][i][j][k][l][m]
								//aかbか
								//i:上からi桁目まで見ている
								//j:A未満であることが確定している
								//k:すでに3を選んでいる
								//l:mod3の値
								//m:mod8の値

int ketaDP(int ab, string s) {
	int n = s.length();

	DP[ab][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');
		for (int d = 0; d <= lim; d++) {
			(DP[ab][i + 1][j || (d < lim)][k || (d == 3)][(l + d) % 3][(m * 10 + d) % 8] += DP[ab][i][j][k][l][m]) %= MOD;
		}
	}

	int ans = 0;
	rep(j, 2) rep(k, 2) rep(l, 3) {
		if (k || (l == 0)) {
			for (int m = 1; m < 8; m++) {
				(ans += DP[ab][n][j][k][l][m]) %= MOD;
			}
		}
	}

	return ans;
}

int main()
{
	string a, b;
	cin >> a >> b;

	int fb, fa, ans;
	fb = ketaDP(1, b);

	for (int i = 1; i <= a.size(); i++) {
		if (a[a.size() - i] == '0') {
			a[a.size() - i] = '9';
		}
		else {
			a[a.size() - i] -= 1;
			break;
		}
	}
	if (a[0] == '0') a.erase(a.begin());

	fa = ketaDP(0, a);
	if (fa > fb) fb += MOD;

	cout << fb - fa << endl;

	return 0;
}
0