結果

問題 No.362 門松ナンバー
ユーザー pekempeypekempey
提出日時 2016-04-17 23:35:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 29 ms / 3,000 ms
コード長 1,835 bytes
コンパイル時間 1,377 ms
コンパイル使用メモリ 163,720 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 02:30:34
合計ジャッジ時間 2,608 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
6,812 KB
testcase_01 AC 8 ms
6,944 KB
testcase_02 AC 8 ms
6,944 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 12 ms
6,940 KB
testcase_06 AC 20 ms
6,944 KB
testcase_07 AC 12 ms
6,940 KB
testcase_08 AC 16 ms
6,944 KB
testcase_09 AC 24 ms
6,940 KB
testcase_10 AC 29 ms
6,940 KB
testcase_11 AC 28 ms
6,940 KB
testcase_12 AC 29 ms
6,944 KB
testcase_13 AC 29 ms
6,940 KB
testcase_14 AC 28 ms
6,944 KB
testcase_15 AC 29 ms
6,940 KB
testcase_16 AC 28 ms
6,944 KB
testcase_17 AC 26 ms
6,944 KB
testcase_18 AC 29 ms
6,944 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 29 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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