結果

問題 No.362 門松ナンバー
ユーザー pekempey
提出日時 2016-04-17 23:35:45
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 29 ms / 3,000 ms
コード長 1,835 bytes
コンパイル時間 1,284 ms
コンパイル使用メモリ 163,980 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-04 11:02:20
合計ジャッジ時間 2,372 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T1, class T2> bool chmin(T1 &a, T2 b) { if (b < a) { a = b; return true; } return false; }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; }

bool isKadomatsu(long long x, long long y, long long z) {
	if (x == y || y == z || x == z) return false;
	if (x > y && y < z) return true;
	if (x < y && y > z) return true;
	return false;
}

const long long inf = 1e14;
static long long dp[50][2][11][11];

long long count(long long L) {
	memset(dp, 0, sizeof(dp));
	dp[0][0][10][10] = 1;

	string s = to_string(L);
	s = string(40 - s.size(), '0') + s;
	int n = s.size();

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 2; j++) {
			for (int k = 0; k <= 10; k++) {
				for (int l = 0; l <= 10; l++) {
					if (dp[i][j][k][l] == 0) continue;
					int D = s[i] - '0';
					int lim = j ? 9 : D;
					for (int d = 0; d <= lim; d++) {
						if (l == 10) {
							if (d == 0) {
								chmin(dp[i + 1][j || d < lim][10][10] += dp[i][j][k][l], inf);
							} else {
								chmin(dp[i + 1][j || d < lim][10][d] += dp[i][j][k][l], inf);
							}
						} else {
							if (k == 10 || isKadomatsu(k, l, d)) {
								chmin(dp[i + 1][j || d < lim][l][d] += dp[i][j][k][l], inf);
							}
						}
					}
				}
			}
		}
	}

	long long result = 0;
	for (int j = 0; j < 2; j++) {
		for (int k = 0; k < 10; k++) {
			for (int l = 0; l < 10; l++) if (k != l) {
				chmin(result += dp[n][j][k][l], inf);
			}
		}
	}
	return result - 81;
}

void solve() {
	long long K;
	cin >> K;

	long long low = 100, high = 37294859064823 + 10;
	while (high - low > 1) {
		long long mid = (low + high) / 2;
		if (count(mid) >= K) high = mid;
		else low = mid;
	}

	cout << high << endl;
}

int main() {
	long long T;
	cin >> T;

	while (T--) solve();
}
0