結果

問題 No.260 世界のなんとか3
ユーザー pekempeypekempey
提出日時 2016-12-28 13:31:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 181,304 KB
実行使用メモリ 11,216 KB
最終ジャッジ日時 2024-12-15 07:38:58
合計ジャッジ時間 3,871 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

template<class T>
auto multiVector(T init, int n) {
	return vector<T>(n, init);
}

template<class T, class... Args>
auto multiVector(T init, int n, Args... ns) {
	return vector<decltype(multiVector<T>(init, ns...))>(n, multiVector<T>(init, ns...));
}

void upd(int &x, int y) {
	if ((x += y) >= mod) {
		x -= mod;
	}
}

int pack(int more, int less, int has3, int mod24) {
	int ret = mod24;
	ret = ret * 2 + has3;
	ret = ret * 2 + less;
	ret = ret * 2 + more;
	return ret;
}

tuple<int, int, int, int> unpack(int v) {
	int more = v % 2; v /= 2;
	int less = v % 2; v /= 2;
	int has3 = v % 2; v /= 2;
	int mod24 = v;
	return make_tuple(more, less, has3, mod24);
}

int main() {
	string A, B;
	cin >> A >> B;
	int n = max(A.size(), B.size());
	A = string(n - A.size(), '0') + A;
	B = string(n - B.size(), '0') + B;
	auto dp = multiVector<int>(0, n + 1, 2 * 2 * 2 * 24); // pos, more, less, has3, mod3, mod8

	dp[0][0] = 1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 2 * 2 * 2 * 24; j++) {
			int more, less, has3, mod24;
			tie(more, less, has3, mod24) = unpack(j);

			int L = more ? 0 : A[i] - '0';
			int R = less ? 9 : B[i] - '0';

			for (int d = L; d <= R; d++) {
				int nj = pack(more || d > L, less || d < R, has3 || d == 3, (mod24 * 10 + d) % 24);
				upd(dp[i + 1][nj], dp[i][j]);
			}
		}
	}

	int ans = 0;
	for (int j = 0; j < 2 * 2 * 2 * 24; j++) {
		int has3, mod24;
		tie(ignore, ignore, has3, mod24) = unpack(j);
		if ((has3 || mod24 % 3 == 0) && mod24 % 8 != 0) {
			upd(ans, dp[n][j]);
		}
	}
	cout << ans << endl;
}
0