結果

問題 No.315 世界のなんとか3.5
ユーザー Yang33Yang33
提出日時 2018-10-01 14:46:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 3,138 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 176,448 KB
実行使用メモリ 94,488 KB
最終ジャッジ日時 2024-04-20 14:15:02
合計ジャッジ時間 7,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
31,516 KB
testcase_01 AC 13 ms
31,476 KB
testcase_02 AC 14 ms
31,872 KB
testcase_03 AC 11 ms
31,676 KB
testcase_04 AC 13 ms
31,740 KB
testcase_05 AC 12 ms
31,580 KB
testcase_06 AC 13 ms
31,884 KB
testcase_07 AC 12 ms
31,640 KB
testcase_08 AC 12 ms
31,660 KB
testcase_09 AC 12 ms
31,564 KB
testcase_10 AC 11 ms
31,584 KB
testcase_11 AC 12 ms
31,760 KB
testcase_12 AC 139 ms
63,044 KB
testcase_13 AC 138 ms
63,076 KB
testcase_14 AC 150 ms
63,048 KB
testcase_15 AC 152 ms
63,068 KB
testcase_16 AC 151 ms
63,088 KB
testcase_17 AC 149 ms
63,044 KB
testcase_18 AC 140 ms
63,104 KB
testcase_19 AC 139 ms
63,108 KB
testcase_20 AC 146 ms
63,004 KB
testcase_21 AC 148 ms
63,276 KB
testcase_22 AC 159 ms
63,196 KB
testcase_23 AC 164 ms
63,224 KB
testcase_24 AC 156 ms
63,180 KB
testcase_25 AC 165 ms
63,324 KB
testcase_26 AC 158 ms
62,956 KB
testcase_27 AC 153 ms
63,148 KB
testcase_28 AC 161 ms
63,264 KB
testcase_29 AC 302 ms
94,424 KB
testcase_30 AC 286 ms
94,272 KB
testcase_31 AC 287 ms
94,252 KB
testcase_32 AC 311 ms
94,436 KB
testcase_33 AC 290 ms
94,356 KB
testcase_34 AC 303 ms
94,408 KB
testcase_35 AC 328 ms
94,488 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  ----- */
/* ------問題------

ある芸人のモノマネをする芸人が現れました。
彼は「3の倍数 もしくは 3のつく数」のとき「アホ」になり、「Pの倍数(Pは 8, 80, 800 のいずれか)」のとき「恥じらい」を見せます。
A 以上 B 以下の整数のうち、「アホ」になりかつ「恥じらい」を見せない数がいくつあるかを 109+7 で割った時の余りで求めてください。

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


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


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