#include #include using namespace std; const int MAX_DIGIT = 15; const int NOT_EXIST = 10; bool isKadomatsu(int a, int b, int c) { return a != b && a != c && b != c && (b > max(a, c) || b < min(a, c)); } // dp[nTh][prepre][pre]: // nTh 番目の桁, 二つ前の数字が prepre, 一つ前の数字が pre で, // これ以降、門松ナンバーが作られるように数字を決めたとき、何通りの門松ナンバーが作られるか long long dp[MAX_DIGIT][11][11]; long long calcRec(int nTh, int prepre, int pre) { if (nTh == -1) { return 1; } long long &ret = dp[nTh][prepre][pre]; if (ret != -1) return ret; ret = 0; if (nTh >= 3 && pre == NOT_EXIST) { ret += calcRec(nTh - 1, NOT_EXIST, NOT_EXIST); } for (int digit = 0; digit <= 9; ++digit) { if (digit == 0) { if (pre != NOT_EXIST && (prepre == NOT_EXIST || isKadomatsu(prepre, pre, 0))) { ret += calcRec(nTh - 1, pre, digit); } continue; } if (prepre == NOT_EXIST || pre == NOT_EXIST || isKadomatsu(prepre, pre, digit)) { ret += calcRec(nTh - 1, pre, digit); } } return ret; } long long K; void read() { cin >> K; --K; } void work() { long long curK = 0; int nTh = MAX_DIGIT - 1; int prepre = NOT_EXIST; int pre = NOT_EXIST; while (nTh >= 0) { #define get(a, b, c) (a == -1 ? 1 : dp[a][b][c]) int toPrint = -1; if (nTh >= 3 && pre == NOT_EXIST) { if (curK + get(nTh - 1, NOT_EXIST, NOT_EXIST) <= K) { curK += get(nTh - 1, NOT_EXIST, NOT_EXIST); } else { toPrint = NOT_EXIST; goto _finish; } } for (int digit = 0; digit <= 9; ++digit) { if (digit == 0) { if (pre != NOT_EXIST && (prepre == NOT_EXIST || isKadomatsu(prepre, pre, 0))) { if (curK + get(nTh - 1, pre, digit) <= K) { curK += get(nTh - 1, pre, digit); } else { toPrint = 0; goto _finish; } } continue; } if (prepre == NOT_EXIST || pre == NOT_EXIST || isKadomatsu(prepre, pre, digit)) { if (curK + get(nTh - 1, pre, digit) <= K) { curK += get(nTh - 1, pre, digit); } else { toPrint = digit; goto _finish; } } } _finish: if (toPrint != NOT_EXIST) { cout << toPrint; } --nTh; prepre = pre; pre = toPrint; } cout << endl; } int main() { memset(dp, -1, sizeof(dp)); calcRec(MAX_DIGIT - 1, NOT_EXIST, NOT_EXIST); int cases; cin >> cases; for (int i = 0; i < cases; ++i) { read(); work(); } return 0; }