#include using namespace std; const int MAX_DIGITS = 9; // compute n!/(n-k)! int Permutation (int n, int k){ int val = 1; while (k-- > 0) val *= n--; return val; } int GetNth (int n){ int val = 0, counts = 0; int numDigits; bool used[10]; for (int i = 0; i < 10; i++) used[i] = false; // compute the number of digits expected counts = 0; for (numDigits = 1; numDigits <= MAX_DIGITS; numDigits++){ // compute the number of repeatless numDigits-digit numbers // that that don't begin with 0 int ct = 9 * Permutation (9, numDigits-1); // stop if we've passed the nth number if (counts + ct >= n) break; counts += ct; } // construct the number, one digit at a time for (int i = 1; i <= numDigits; i++){ // try all possible unused digits for (int d = (i == 1 ? 1 : 0); d < 10; d++){ if (!used[d]){ // compute the number of repeatless (numDigits - i)-digit numbers // that don't use any of the digits already seen int ct = Permutation (10 - i, numDigits - i); // stop if we've passed the nth number if (counts + ct >= n){ val = val * 10 + d; used[d] = true; break; } counts += ct; } } } return val; } int main (){ while (true){ int n; cin >> n; if (n == 0) break; cout << GetNth (n) << endl; } return 0; }