結果

問題 No.315 世界のなんとか3.5
ユーザー Yang33Yang33
提出日時 2018-10-01 14:34:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 2,716 bytes
コンパイル時間 2,706 ms
コンパイル使用メモリ 176,940 KB
実行使用メモリ 94,304 KB
最終ジャッジ日時 2023-08-02 14:09:19
合計ジャッジ時間 8,160 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
31,476 KB
testcase_01 AC 12 ms
31,492 KB
testcase_02 AC 14 ms
31,804 KB
testcase_03 AC 13 ms
31,492 KB
testcase_04 AC 12 ms
31,808 KB
testcase_05 AC 13 ms
31,552 KB
testcase_06 AC 13 ms
31,688 KB
testcase_07 AC 13 ms
31,504 KB
testcase_08 AC 13 ms
31,520 KB
testcase_09 AC 12 ms
31,476 KB
testcase_10 AC 12 ms
31,484 KB
testcase_11 AC 12 ms
31,480 KB
testcase_12 AC 152 ms
62,840 KB
testcase_13 AC 154 ms
62,828 KB
testcase_14 AC 162 ms
63,004 KB
testcase_15 AC 163 ms
62,944 KB
testcase_16 AC 163 ms
63,236 KB
testcase_17 AC 162 ms
62,980 KB
testcase_18 AC 151 ms
62,848 KB
testcase_19 AC 152 ms
62,848 KB
testcase_20 AC 158 ms
63,020 KB
testcase_21 AC 158 ms
63,072 KB
testcase_22 AC 170 ms
63,144 KB
testcase_23 AC 170 ms
63,200 KB
testcase_24 AC 162 ms
63,088 KB
testcase_25 AC 170 ms
63,180 KB
testcase_26 AC 162 ms
63,168 KB
testcase_27 AC 163 ms
63,156 KB
testcase_28 AC 166 ms
63,328 KB
testcase_29 AC 309 ms
94,284 KB
testcase_30 AC 297 ms
94,144 KB
testcase_31 AC 297 ms
94,024 KB
testcase_32 AC 329 ms
94,300 KB
testcase_33 AC 302 ms
94,176 KB
testcase_34 AC 322 ms
94,304 KB
testcase_35 AC 345 ms
94,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/10/01  Problem: yukicoder 315  / Link: http://yukicoder.me/problems/no/315  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */


vector<LL> dp[200005][3][2];
int EIGHT;
map<int, int>eightDigit = { {8,3},{80,4},{800,5} };
int positiveDigit;
LL f(const string& s, int i, short threemod = 0, bool hasthree = 0, int eightmod = 0, bool less = 0) {
	if (i == -1) {
		return (threemod == 0 || hasthree == 1) && eightmod%EIGHT;
	}
	if (less && dp[i][threemod][hasthree].size() == 0) {
		if (i + 1 > positiveDigit) {
			dp[i][threemod][hasthree].assign(1, -1);
		}
		else {
			dp[i][threemod][hasthree].assign(EIGHT, -1);
		}
	}
	else {
		// return memo
		if (less) {
			if (i + 1 > positiveDigit) {
				if (dp[i][threemod][hasthree][0] != -1)return dp[i][threemod][hasthree][0];
			}
			else {
				if (dp[i][threemod][hasthree][eightmod] != -1)return dp[i][threemod][hasthree][eightmod];
			}
		}
	}
	LL res = 0;
	int Max = less ? 9 : s[i] - '0';
	FOR(nx, 0, Max + 1) {
		res += f(s, i - 1, (threemod + nx) % 3, hasthree || nx == 3, (eightmod * 10 + nx) % EIGHT, less || (nx < Max));
		res %= MOD;
	}
	if (less) {
		if (i + 1 > positiveDigit) 	return dp[i][threemod][hasthree][0] = res;
		else return dp[i][threemod][hasthree][eightmod] = res;
	}
	else {
		return res;
	}
}



int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	string A, B; cin >> A >> B >> EIGHT;
	::positiveDigit = eightDigit[EIGHT];
	FORR(i, SZ(A) - 1, 0 - 1) { // A--;
		if (A[i] == '0') {
			A[i] = '9';
		}
		else {
			A[i]--;
			break;
		}
	}
	if (A[0] == '0')A = A.substr(1);
	reverse(ALL(A)), reverse(ALL(B));

	LL ans = f(B, SZ(B) - 1);
	LL ret = f(A, SZ(A) - 1);
	ans -= ret;
	if (ans < 0)ans += MOD;
	cout << ans << "\n";

	return 0;
}
0