結果

問題 No.315 世界のなんとか3.5
ユーザー pekempeypekempey
提出日時 2015-12-08 12:02:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 487 ms / 2,000 ms
コード長 1,793 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 148,472 KB
実行使用メモリ 23,700 KB
最終ジャッジ日時 2023-10-12 21:35:14
合計ジャッジ時間 8,207 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
22,972 KB
testcase_01 AC 11 ms
22,868 KB
testcase_02 AC 18 ms
22,920 KB
testcase_03 AC 10 ms
22,884 KB
testcase_04 AC 10 ms
22,872 KB
testcase_05 AC 11 ms
22,880 KB
testcase_06 AC 19 ms
22,948 KB
testcase_07 AC 11 ms
22,944 KB
testcase_08 AC 11 ms
22,868 KB
testcase_09 AC 11 ms
22,968 KB
testcase_10 AC 11 ms
22,916 KB
testcase_11 AC 11 ms
22,880 KB
testcase_12 AC 102 ms
23,052 KB
testcase_13 AC 102 ms
23,108 KB
testcase_14 AC 192 ms
23,024 KB
testcase_15 AC 192 ms
23,144 KB
testcase_16 AC 192 ms
23,332 KB
testcase_17 AC 193 ms
23,088 KB
testcase_18 AC 104 ms
23,088 KB
testcase_19 AC 104 ms
23,116 KB
testcase_20 AC 112 ms
23,080 KB
testcase_21 AC 112 ms
23,052 KB
testcase_22 AC 200 ms
23,012 KB
testcase_23 AC 202 ms
23,368 KB
testcase_24 AC 194 ms
23,024 KB
testcase_25 AC 201 ms
23,056 KB
testcase_26 AC 193 ms
23,108 KB
testcase_27 AC 192 ms
23,000 KB
testcase_28 AC 197 ms
23,060 KB
testcase_29 AC 329 ms
23,412 KB
testcase_30 AC 323 ms
23,400 KB
testcase_31 AC 323 ms
23,480 KB
testcase_32 AC 382 ms
23,548 KB
testcase_33 AC 207 ms
23,256 KB
testcase_34 AC 278 ms
23,700 KB
testcase_35 AC 487 ms
23,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
ll dp[202020][2][3][2]; // pos, less, mod 3, three
ll dp2[8][2][3][2][800]; // pos, less, mod 3, three, mod P

ll f(string N, int P, bool less) {
	memset(dp, 0, sizeof(dp));
	memset(dp2, 0, sizeof(dp2));

	int s = max<int>(0, N.length() - 6);
	int t = N.length() - s;

	dp[0][0][0][0] = 1;
	rep (i, s) rep (j, 2) rep (k, 3) rep (l, 2) {
		int lim = j ? 9 : N[i] - '0';
		rep (x, lim + 1) {
			(dp[i + 1][j || x < lim][(k + x) % 3][l || x == 3] += dp[i][j][k][l]) %= mod;
		}
	}
	rep (j, 2) rep (k, 3) rep (l, 2) {
		dp2[0][j][k][l][0] = dp[s][j][k][l];
	}

	rep (i, t) rep (j, 2) rep (k, 3) rep (l, 2) rep (m, P) {
		int lim = j ? 9  : N[s + i] - '0';
		rep (x, lim + 1) {
			(dp2[i + 1][j || x < lim][(k + x) % 3][l || x == 3][(m * 10 + x) % P] += dp2[i][j][k][l][m]) %= mod;
		}
	}

	ll res = 0;
	rep (j, 2) rep (k, 3) rep (l, 2) rep (m, P) {
		if (less && j == 0) continue;
		if ((k == 0 || l) && m != 0) {
			(res += dp2[t][j][k][l][m]) %= mod;
		}
	}
	return res;
}

int main() {
	string A, B;
	int P;
	cin >> A >> B >> P;

	ll ans = f(B, P, false);
	ans -= f(A, P, true);
	ans %= mod; ans += mod; ans %= mod;
	cout << ans << endl;
	return 0;
}
0