Problem:
短歌数
願はくは 花の下にて 春死なむ そのきさらぎの 望月のころ
これは，西行法師が詠んだとされる，有名な短歌の一つである．
短歌は，日本において古くから親しまれている和歌の一種であり，その多くは 5・7・5・7・7 の五句三十一音からなる．
ところで，57577 という数は，5 と 7 の二種類の数字からなる．
このような，十進表記がちょうど二種類の数字からなる正の整数を，短歌数と呼ぶことにする．
例えば，10, 12, 57577, 25252 などは短歌数であるが，5, 11, 123, 20180701 などは短歌数ではない．
正の整数
N
が与えられる．
N
番目に小さい短歌数を求めよ．
Input
入力は最大で 100 個のデータセットからなる．各データセットは次の形式で表される．
N
整数
N
は
1 ≤ N ≤ 10
18
を満たす．
入力の終わりは 1 つのゼロからなる行で表される．
Output
各データセットについて，
N
番目に小さい短歌数を 1 行に出力せよ．
Sample Input
1
2
3
390
1124
1546
314159265358979323
0
Output for the Sample Input
10
12
13
2020
25252
57577
7744444777744474777777774774744777747477444774744744

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = (ll)1e9 * (ll)1e9 + 100;

ll calc(const vector<int>& v) {
	int d = v.size();
	ll dp[d + 1][10][10][4][2] = {};
	for (int i = 0; i < 10; i++) {
		for (int j = i + 1; j < 10; j++) {
			dp[0][i][j][0][0] = 1;
		}
	}
	for (int i = 0; i < d; i++) {
		for (int d1 = 0; d1 < 10; d1++) {
			for (int d2 = d1 + 1; d2 < 10; d2++) {
				if (i != 0 || d1 != 0) {
					if (d1 < v[i]) {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][0]);
						}
					}
					else if (d1 == v[i]) {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
							dp[i + 1][d1][d2][s | 1][0] = min(INF, dp[i + 1][d1][d2][s | 1][0] + dp[i][d1][d2][s][0]);
						}
					}
					else {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
						}
					}
				}
				if (d2 < v[i]) {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][0]);
					}
				}
				else if (d2 == v[i]) {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
						dp[i + 1][d1][d2][s | 2][0] = min(INF, dp[i + 1][d1][d2][s | 2][0] + dp[i][d1][d2][s][0]);
					}
				}
				else {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
					}
				}
			}
		}
	}
	ll all = 0;
	for (int i = 0; i < 10; i++) {
		for (int j = i + 1; j < 10; j++) {
			all = min(INF, all + dp[d][i][j][3][1]);
		}
	}
	return all;
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	ll N;
	while (cin >> N, N) {
		--N;
		ll d = 2;
		while (true) {
			ll cnt = calc(vector<int>(d, 9));
			if (N < cnt) break;
			N -= cnt;
			++d;
		}
		vector<int> res(d);
		for (int p = 0; p < d; p++) {
			while (res[p] < 9) {
				++res[p];
				if (N < calc(res)) {
					--res[p];
					break;
				}
			}
		}
		for (int i = 0; i < d; i++) {
			cout << res[i];
		}
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3688
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll INF=1ll<<61;
int main(){
    vector<int>a(1<<10,-1);
    int now=0;
    vector<int>inv;
    for(int i=0;i<(1<<10);i++)if(__builtin_popcount(i)<=2){
        a[i]=now++;
        inv.push_back(i);
    }
    while(true){
        ll n;
        cin>>n;
        if(n==0)break;
        string ng="9";
        string ok="1111111111111111111111111111111111111111111111111111111";
        auto rm0=[](string &s){
            while(s.back()=='0')s.pop_back();
        };
        auto middle=[&](string s,string t)->string {
            rm0(s),rm0(t);
            if(s.size()>t.size())swap(s,t);
            string ret;
            int ten=0;
            for(int i=0;i<s.size();i++){
                int l=s[i]-'0',r=t[i]-'0';
                ret+='0'+((l+r+ten)%10);
                ten=(l+r+ten)>=10;
            }
            for(int i=s.size();i<t.size();i++){
                int l=t[i]-'0';
                ret+='0'+((l+ten)%10);
                ten=(l+ten)>=10;
            }
            if(ten)ret+='1';
            reverse(ret.begin(),ret.end());
            string a;
            int div2=0;
            for(int i=0;i<ret.size();i++){
                int l=ret[i]-'0';
                l+=div2*10;
                a+='0'+(l/2);
                div2=l&1;
            }
            reverse(a.begin(),a.end());
            rm0(a);
            return a;
        };
        auto inc=[](string s)->string {
            for(int i=0;i<s.size();i++){
                if(s[i]=='9'){
                    s[i]='0';
                    continue;
                }
                s[i]++;
                return s;
            }
            s+='1';
            return s;
        };
        while(true){
            rm0(ok),rm0(ng);
            if(inc(ng)==ok)break;
            // cerr<<ok<<' '<<ng<<endl;
            // if(ok.size()==ng.size()&&ok.substr(1)==ng.substr(1)&&abs(ok[0]-ng[0])<=1)break;
            // if(ok.size()==ng.size()&&)
            string mid=middle(ok,ng);
            reverse(mid.begin(),mid.end());
            int z=mid.size();
            vector<vector<vector<ll>>>dp(2,vector<vector<ll>>(2,vector<ll>(now,0)));
            dp[0][1][0]=1;
            for(int i=0;i<z;i++){
                vector<vector<vector<ll>>>ep(2,vector<vector<ll>>(2,vector<ll>(now,0)));
                for(int j=0;j<2;j++)for(int k=0;k<2;k++){
                    for(int m=0;m<now;m++)if(dp[j][k][m]){
                        for(int l=0;l<(j?10:mid[i]-'0'+1);l++){
                            int nj=j||l<mid[i]-'0';
                            int nk=k&&l==0;
                            int mask=inv[m];
                            if(!nk)mask|=1<<l;
                            if(a[mask]==-1)continue;
                            int nm=a[mask];
                            ep[nj][nk][nm]+=dp[j][k][m];
                            ep[nj][nk][nm]=min(ep[nj][nk][nm],INF);
                        }
                    }
                }
                swap(dp,ep);
            }
            ll ans=0;
            for(int i=0;i<2;i++)for(int j=0;j<2;j++)for(int k=0;k<now;k++){
                if(__builtin_popcount(inv[k])==2)ans+=dp[i][j][k];
            }
            reverse(mid.begin(),mid.end());
            (ans>=n?ok:ng)=mid;
        }
        reverse(ok.begin(),ok.end());
        cout<<ok<<endl;
    }
}
accuracy: 1
time_ms: 770
memory_kb: 3380
final_rank: 8

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

using i128 = __int128_t;

ostream& operator<<(ostream& os, i128 x) { return os << (long long)x; }

void solve(long long nll) {
    i128 N = nll;
    // the number of tanka number (the length of tanka() <= i)
    // dp[i][bit]
    int B = 1 << 10;
    vector dp(60, vector<i128>(B));
    dp[0][0] = 1;
    REP(i, 59) {
        REP(bit, B) {
            if (__builtin_popcount(bit) >= 3) continue;
            REP(d, 10) {
                if (bit == 0 and d == 0) {
                    dp[i + 1][bit] += dp[i][bit];
                } else {
                    dp[i + 1][bit | (1 << d)] += dp[i][bit];
                }
            }
        }
    }
    vector<i128> cnt(60);
    REP(i, 60) {
        REP(bit, B) {
            if (__builtin_popcount(bit) == 2) {
                cnt[i] += dp[i][bit];
            }
        }
    }
    // calculate the length of tanka(N)
    int L = 0;
    while (true) {
        if (N > cnt[L]) {
            L++;
        } else {
            break;
        }
    }
    N -= cnt[L - 1];
    string ans;
    auto f = [](string s, int L) -> long long {
        set<char> chset;
        int curlen = int(s.size());
        for (auto& c : s) chset.insert(c);
        if (chset.size() == 2) {
            return 1LL << (L - curlen);
        } else if (chset.size() == 1) {
            return 9LL * ((1LL << (L - curlen)) - 1);
        } else if (chset.size() != 0) {
            return 0;
        } else {
            assert(0);
        }
    };
    REP(_, L) {
        for (char nex = '0'; nex <= '9'; nex++) {
            if (_ == 0 and nex == '0') continue;
            ans += nex;
            long long curcnt = f(ans, L);
            if (curcnt >= N) {
                break;
            } else {
                N -= curcnt;
                ans.pop_back();
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long N;
    while (cin >> N, N != 0) solve(N);
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4088
final_rank: 4

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

using i128 = __int128_t;

ostream& operator<<(ostream& os, i128 x) { return os << (long long)x; }

void solve(long long nll) {
    i128 N = nll;
    // the number of tanka number (the length of tanka() = i)
    // dp[i][bit]
    int B = 1 << 10;
    vector dp(60, vector<i128>(B));
    dp[0][0] = 1;
    REP(i, 59) {
        REP(bit, B) {
            if (__builtin_popcount(bit) >= 3) continue;
            REP(d, 10) {
                if (bit == 0 and d == 0) {
                    dp[i + 1][bit] += dp[i][bit];
                } else {
                    dp[i + 1][bit | (1 << d)] += dp[i][bit];
                }
            }
        }
    }
    vector<i128> cnt(60);
    REP(i, 60) {
        REP(bit, B) {
            if (__builtin_popcount(bit) == 2) {
                cnt[i] += dp[i][bit];
            }
        }
    }
    // calculate the length of tanka(N)
    int L = 0;
    while (true) {
        if (N > cnt[L]) {
            L++;
        } else {
            break;
        }
    }
    N -= cnt[L - 1];
    string ans;
    auto f = [](string s, int L) -> long long {
        set<char> chset;
        int curlen = int(s.size());
        for (auto& c : s) chset.insert(c);
        if (chset.size() == 2) {
            return 1LL << (L - curlen);
        } else if (chset.size() == 1) {
            return 9LL * ((1LL << (L - curlen)) - 1);
        } else if (chset.size() != 0) {
            return 0;
        } else {
            assert(0);
        }
    };
    REP(_, L) {
        for (char nex = '0'; nex <= '9'; nex++) {
            if (_ == 0 and nex == '0') continue;
            ans += nex;
            long long curcnt = f(ans, L);
            if (curcnt >= N) {
                break;
            } else {
                N -= curcnt;
                ans.pop_back();
            }
        }
        // show(ans);
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long N;
    while (cin >> N, N != 0) solve(N);
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4064
final_rank: 3

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= (int)s; i--)
#define all(v) v.begin(),v.end()

using ll = long long;
using ull = unsigned long long;
using ld = long double;

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

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

using namespace std;

ll solve(vector<int> &s, int sizmoto){
    set<int> nowset;
    vector<int> nowarr;
    int nowsiz = 0;

    bool mode = false;
    vector<int> ss;
    for (int i=0; i<sizmoto; i++){
        if (s[i] != 0){
            mode = true;
        }
        if (mode){
            ss.push_back(s[i]);
        }
    }

    int siz = ss.size();

    vector<vector<vector<ll>>> dp(10, vector<vector<ll>>(10, vector<ll>(4)));
    for (int num=0; num<siz; num++){
        vector<vector<vector<ll>>> ndp(10, vector<vector<ll>>(10, vector<ll>(4)));
        // keizoku

        /*
        for (int i=0; i<10; i++){
            for (int x=0; x<10; x++){
                for (int y=x+1; y<10; y++){
                    if (x==i){
                        for (int r=1; r<4; r++) ndp[x][y][r|1] += dp[x][y][r];
                    }else if(y==i){
                        for (int r=1; r<4; r++) ndp[x][y][r|2] += dp[x][y][r];
                    }
                }
            }
        }
        */

        for (int x=0; x<10; x++){
            // left side
            for (int y=x+1; y<10; y++){
                for (int r=1; r<4; r++) ndp[x][y][r|1] += dp[x][y][r];
            }
        }
        for (int y=0; y<10; y++){
            // right side
            for (int x=0; x<y; x++){
                for (int r=1; r<4; r++) ndp[x][y][r|2] += dp[x][y][r];
            }
        }


        

        // shinki
        if (num > 0){
            for (int i=1; i<10; i++){
                // left side
                for (int y=i+1; y<10; y++){
                    ndp[i][y][1]++;
                }
            }
            for (int i=1; i<10; i++){
                for (int x=0; x<i; x++){
                    ndp[x][i][2]++;
                }
            }

            /*
            for (int i=1; i<10; i++){
                for (int x=0; x<10; x++){
                    for (int y=x+1; y<10; y++){
                        if (x==i){
                            ndp[x][y][1]++;
                        }else if(y==i){
                            ndp[x][y][2]++;
                        }
                    }
                }
            }
            */
        }
        // hajimete
        if (nowsiz == 0){
            for (int i=1; i<ss[num]; i++){
                // left side
                for (int y=i+1; y<10; y++){
                    ndp[i][y][1]++;
                }
                for (int x=0; x<i; x++){
                    ndp[x][i][2]++;
                }
                /*
                for (int x=0; x<10; x++){
                    for (int y=x+1; y<10; y++){
                        if (x==i){
                            ndp[x][y][1]++;
                        }else if(y==i){
                            ndp[x][y][2]++;
                        }
                    }
                }
                */
            }
        }else if(nowsiz == 1){
            int var = nowarr[0];
            for (int i=0; i<ss[num]; i++){
                if (i < var){
                    ndp[i][var][3]++;
                }else if (i == var){
                    for (int j=i+1; j<10; j++){
                        ndp[i][j][1]++;
                    }
                    for (int j=0; j<i; j++){
                        ndp[j][i][2]++;
                    }
                }else if (i > var){
                    ndp[var][i][3]++;
                }
            }
        }else if(nowsiz == 2){
            int var1 = nowarr[0];
            int var2 = nowarr[1];
            if(var1 > var2) swap(var1, var2);
            for (int i=0; i<ss[num]; i++){
                if (i == var1 || i == var2){
                    ndp[var1][var2][3]++;
                }
            }
        }

        if (nowset.find(ss[num]) == nowset.end()){
            nowset.insert(ss[num]);
            nowarr.push_back(ss[num]);
            nowsiz++;
        }

        swap(dp,ndp);

    }
    ll ans = 0;
    //if (nowsiz == 2) ans++;
    for (int i=0; i<10; i++){
        for (int j=i+1; j<10; j++){
            ans += dp[i][j][3];
        }
    }
    //cout << ans << endl;
    return ans;
}

int main(){
    const int siz = 55;
    while (true){
        ll n; cin >> n;
        if (n == 0) break;
        vector<int> ans(siz);
        for (int i=0; i<siz; i++){
            for (int j=1; j<10; j++){
                ans[i] = j;
                ll tmp = solve(ans, siz);
                //cout << tmp << endl;
                if (tmp >= n){
                    ans[i] = j-1;
                    break;
                }
            }
        }
        bool mode = false;
        for (int i=0; i<siz; i++){
            if (!mode){
                if (ans[i] != 0){
                    mode = true;
                    cout << ans[i];
                }
            }else{
                cout << ans[i];
            }
        }
        cout << endl;
    }  
    
}
accuracy: 1
time_ms: 1220
memory_kb: 3464
final_rank: 10

code_snippet: //#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
  auto tmp = v;
  for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
  rearrange(ord, head);
  rearrange(ord, tail...);
}
template<typename T> vector<int> ascend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});
  return ord;
}
template<typename T> vector<int> descend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});
  return ord;
}
ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
template< typename T = int >
struct edge {
  int to;
  T cost;
  int id;
  edge():id(-1){};
  edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
  operator int() const { return to; }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;
template<typename T>
Graph<T>revgraph(const Graph<T> &g){
  Graph<T>ret(g.size());
  for(int i=0;i<g.size();i++){
    for(auto e:g[i]){
      int to = e.to;
      e.to = i;
      ret[to].push_back(e);
    }
  }
  return ret;
}
template<typename T>
Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
  Graph<T> ret(n);
  for(int es = 0; es < m; es++){
    int u,v;
    T w=1;
    cin>>u>>v;u-=indexed,v-=indexed;
    if(weighted)cin>>w;
    ret[u].emplace_back(v,w,es);
    if(!directed)ret[v].emplace_back(u,w,es);
  }
  return ret;
}
template<typename T>
Graph<T> readParent(int n,int indexed=1,bool directed=true){
  Graph<T>ret(n);
  for(int i=1;i<n;i++){
    int p;cin>>p;
    p-=indexed;
    ret[p].emplace_back(i);
    if(!directed)ret[i].emplace_back(p);
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  while(1){
    ll n;cin>>n;
    if(n==0)break;
    auto check=[&](string s){
      ll ret=0;
      ll sz=s.size();
      rep(i,0,10)rep(j,i+1,10){
        ll add=0;
        rep(o,1,sz){
          add+=1LL<<o;
          if(i==0)add-=(1LL<<o-1);
          else add--;
          add--;
          //cout<<o spa add<<endl;
        }
        //cout<<add<<endl;
        rep(o,0,sz+1){
          if(o==sz)add++;
          else{
            ll tmp=s[o]-'0';
            ll cf=0;
            if((i>0||o>0)&&i<tmp)cf++;
            if((j>0||o>0)&&j<tmp)cf++;
            add+=(1LL<<sz-o-1)*cf;
          }
          if(s[o]-'0'!=i&&s[o]-'0'!=j)break;
          //cout<<i spa j spa o spa add<<endl;
        }
        //cout<<add<<endl;
        if(i!=0&&string(sz,i+'0')<=s)add--;
        if(j!=0&&string(sz,j+'0')<=s)add--;
        ret+=add;
        //cout<<i spa j spa add<<endl;
      }
      return ret;
    };
    string s="1";
    while(check(s+"0")<=n)s+="0";
    rep(i,0,s.size()){
      while(s[i]<'9'){
        string ts=s.substr(0,i+1)+string(s.size()-i-1,'9');
        //cout<<ts spa check(ts)<<endl;
        if(check(ts)<n){
          s[i]++;
        }
        else break;
      }
    }
    /*rep(i,1,30300){
      set<char>st;auto si=to_string(i);for(auto c:si)st.insert(c);
      ll dif=check(to_string(i))-check(to_string(i-1));
      if(dif<0||dif>=2)cout<<i spa dif<<endl;
      if((st.size()==2)^(dif!=0))cout<<i<<endl;
    }*/
    //cout<<check("1000") spa ":ch"<<endl;;
    //cout<<check("2020") spa check("25252")<<endl;
    //cout<<check("100") spa check("108") spa check("110")<<endl;
    cout<<s<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3316
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;
    template<class t>
    void output(t a){
        if(was_output)cout << " ";
        cout << a;
        was_output = true;
    }
    void outendl(){
        was_output = false;
        cout << endl;
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    void out(t x){
        cout << x;
    }


    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }
}

using namespace templates;

string add(string a,string b){
    reverse(all(a));
    reverse(all(b));
    string res = "";
    int sum = 0;
    rep(i,max(a.size(),b.size())){
        if(i<a.size())sum += a[i] - '0';
        if(i<b.size())sum += b[i] - '0';
        res += (sum % 10) + '0';
        sum /= 10;
    }
    if(sum)res += sum + '0';
    reverse(all(res));
    return res;
}

string div2(string str){
    int sum = 0;
    string res = "";
    foreach(i,str){
        sum += i - '0';
        if(res.size() or sum/2){
            res += sum / 2 + '0';
        }
        sum %= 2;
        sum *= 10;
    }
    return res;
}

string func(ll v,string lp = "1",string rp = "999999999999999999999999999999999999999999999999999999"){

    vector<ll> pow2(100,1);
    rep(i,100){
        pow2[i+1] = min(pow2[i]*2,v);
    }

    method(check,bool,string n){
        method(rec,ll,int digit,bool hat,char v1,char v2,bool used1,bool used2){
            if(!hat and used1 and used2){
                return pow2[n.size()-digit];
            }
            if(digit==n.size())return 0;
            ll res = 0;
            if(!hat or v1 <= n[digit]){
                if(v1 != '0' or used2){
                    res += rec(digit+1,hat and n[digit]==v1,v1,v2,true,used2);
                }
            }
            if(!hat or v2 <= n[digit]){
                if(v2 != '0' or used1){
                    res += rec(digit+1,hat and n[digit]==v2,v1,v2,used1,true);
                }
            }
            return min(res,v);
        };
        ll sum = 0;
        rep(digit,n.size()){
            rep(v1,10){
                rep(v2,v1+1,10){
                    sum += rec(digit,!digit,v1+'0',v2+'0',false,false);
                    sum = min(sum,v);
                }
            }
        }
        return sum < v;
    };
    while(lp != rp){
        string mid = div2(add(lp,rp));
        if(check(add(mid,"1"))){
            lp = add(mid,"1");
        }else{
            rp = mid;
        }
    }
    return lp;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<pll> line;
    int cnt = 0;
    while(true){
        ll n = in();
        if(n==0)break;
        line.emplace_back(n,cnt++);
    }
    sort(all(line));
    int n = line.size();
    string last = "1";
    vector<string> answer(n);
    method(rec,void,int l,int r,string lp,string rp){
        if(l==r)return;
        int mid = (l + r) / 2;
        string ans = func(line[mid].first,lp,rp);
        answer[line[mid].second] = ans;
        rec(l,mid,lp,ans);
        rec(mid+1,r,ans,rp);
    };
    rec(0,n,"1", "999999999999999999999999999999999999999999999999999999");
    foreach(i,answer){
        cout << i << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 4770
memory_kb: 3372
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const ll MX = 1000000000000000005ll;
string s;
int S;
ll dp[100][4][2][2];
int table[10];
ll rec(int pos,int T,int tight,int lz){
	if(pos==s.size()){
		if(T==3)return 1ll;
		else return 0ll;
	}
	if(dp[pos][T][tight][lz]!=-1ll)return dp[pos][T][tight][lz];
	ll res = 0ll;
	if(!tight){
		if(lz){
			res = min(MX,res+rec(pos+1,T,0,1)); // use 0
			for(int i=1;i<10;i++){
				if((S&(1<<i))==0)continue;
				res = min(MX,res+rec(pos+1,T|(1<<table[i]),0,0));
			}
		}else{
			for(int i=0;i<10;i++){
				if((S&(1<<i))==0)continue;
				res = min(MX,res+rec(pos+1,T|(1<<table[i]),0,0));
			}
		}
	}else{
		int a = (s[pos]-'0');
		if(lz){
			res = min(MX,res+rec(pos+1,T,0,1));  // use 0
			for(int i=1;i<=a;i++){
				if((S&(1<<i))==0)continue;
				if(i==a)res = min(MX,res+rec(pos+1,T|(1<<table[i]),1,0));
				else res = min(MX,res+rec(pos+1,T|(1<<table[i]),0,0));
			}
		}else{
			for(int i=0;i<=a;i++){
				if((S&(1<<i))==0)continue;
				if(i==a)res = min(MX,res+rec(pos+1,T|(1<<table[i]),1,0));
				else res = min(MX,res+rec(pos+1,T|(1<<table[i]),0,0));
			}
		}
	}
	//cout << pos << ' ' << tight<< ' ' <<  lz << ' ' << res << endl;
	return dp[pos][T][tight][lz]=res;
}
ll f(string str){
	s = str;
	ll ret = 0ll;
	for(int i=0;i<(1<<10);i++){
		int cnt = 0;
		memset(table,0,sizeof(table));
		for(int j=0;j<10;j++){
			if((i>>j)&1){
				table[j]=cnt;
				cnt++;
			}
		}
		if(cnt!=2)continue;
		for(int a=0;a<100;a++){
			for(int b=0;b<4;b++){
				for(int c=0;c<2;c++){
					for(int d=0;d<2;d++){
						dp[a][b][c][d]=-1ll;
					}
				}
			}
		}
		S = i;
		//if(i==3)cout << rec(0,i,1,1) << endl;
		ret = min(MX,ret+rec(0,0,1,1));
	}
	return ret;
}
ll N;
string zero[105];
string plus1(string s){
	int carry = 1;
	for(int i=s.size()-1;i>=0;i--){
		if(!carry)break;
		if(s[i]=='9'){
			s[i]='0';
		}else{
			s[i]+=1;
			carry=0;
		}
	}
	if(carry)return "1"+s;
	else return s;
}
bool solve(){
	cin >> N;
	if(N==0)return false;
	//cout << f("57577") << endl;
	string cur="";
	for(int i=61;i>=0;i--){
		int l = -1,r = 10;
		while(r-l>1){
			int mid = (l+r)/2;
			string t = cur+((char)('0'+mid))+zero[i];
			//cout << t << ' ' << f(t) << endl;
			if(f(cur+((char)('0'+mid))+zero[i])<=N-1)l=mid;
			else r = mid;
		}
		if(!(cur==""&&r==0))cur+=(char)('0'+l);
	}
	cout << plus1(cur) << endl;
	return true;
}
int main(){
	zero[0]="";
	for(int i=1;i<70;i++)zero[i]=zero[i-1]+"0";
	while(solve()){}
	return 0;
}
accuracy: 1
time_ms: 2640
memory_kb: 3188
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = (ll)1e9 * (ll)1e9 + 100;

ll calc(const vector<int>& v) {
	int d = v.size();
	ll dp[d + 1][10][10][4][2] = {};
	for (int i = 0; i < 10; i++) {
		for (int j = i + 1; j < 10; j++) {
			dp[0][i][j][0][0] = 1;
		}
	}
	for (int i = 0; i < d; i++) {
		for (int d1 = 0; d1 < 10; d1++) {
			for (int d2 = d1 + 1; d2 < 10; d2++) {
				if (i != 0 || d1 != 0) {
					if (d1 < v[i]) {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][0]);
						}
					}
					else if (d1 == v[i]) {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
							dp[i + 1][d1][d2][s | 1][0] = min(INF, dp[i + 1][d1][d2][s | 1][0] + dp[i][d1][d2][s][0]);
						}
					}
					else {
						for (int s = 0; s < 4; s++) {
							dp[i + 1][d1][d2][s | 1][1] = min(INF, dp[i + 1][d1][d2][s | 1][1] + dp[i][d1][d2][s][1]);
						}
					}
				}
				if (d2 < v[i]) {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][0]);
					}
				}
				else if (d2 == v[i]) {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
						dp[i + 1][d1][d2][s | 2][0] = min(INF, dp[i + 1][d1][d2][s | 2][0] + dp[i][d1][d2][s][0]);
					}
				}
				else {
					for (int s = 0; s < 4; s++) {
						dp[i + 1][d1][d2][s | 2][1] = min(INF, dp[i + 1][d1][d2][s | 2][1] + dp[i][d1][d2][s][1]);
					}
				}
			}
		}
	}
	ll all = 0;
	for (int i = 0; i < 10; i++) {
		for (int j = i + 1; j < 10; j++) {
			all = min(INF, all + dp[d][i][j][3][1]);
		}
	}
	return all;
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	ll N;
	while (cin >> N, N) {
		--N;
		ll d = 2;
		while (true) {
			ll cnt = calc(vector<int>(d, 9));
			if (N < cnt) break;
			N -= cnt;
			++d;
		}
		vector<int> res(d);
		for (int p = 0; p < d; p++) {
			while (res[p] < 9) {
				++res[p];
				if (N < calc(res)) {
					--res[p];
					break;
				}
			}
		}
		for (int i = 0; i < d; i++) {
			cout << res[i];
		}
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 3476
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

ll n,cou;
int str[1111];
ll nijou[1111];

bool dfs(int keta,int now,int fir,int sec){
	
	if(keta == now){
		cou++;
		if(n == cou)return true;
		else return false;
	}
	
	if(fir == -1){
		for(int i = 1;i < 10;i++){
			str[now] = i;
			if(dfs(keta,now+1,i,sec))return true;
		}
	}
	else if(sec == -1){
		REP(i,10){
			if(i == fir){
				if(now + 1 < keta){
					str[now] = fir;
					if(dfs(keta,now+1,fir,sec))return true;
				}
			}
			else{
				str[now] = i;
				if(dfs(keta,now+1,fir,i))return true;
			}
		}
	}
	else{
		if(cou + nijou[keta - now] < n){
			cou += nijou[keta - now];
			return false;
		}
		
		str[now] = min(fir,sec);
		if(dfs(keta,now+1,fir,sec))return true;
		str[now] = max(fir,sec);
		if(dfs(keta,now+1,fir,sec))return true;
	}
	return false;
}

int main(){
	
	nijou[0] = 1;
	REP(i,1110)nijou[i+1] = nijou[i] * 2LL;

	while(cin >> n,n){
		cou = 0;
		
		for(int i = 2;i < 10000;i++){
			if(dfs(i,0,-1,-1)){
				REP(j,i){
					cout << str[j];
				}
				cout << endl;
				break;
			}
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3104
final_rank: 1

code_snippet: #include<bits/stdc++.h>

using namespace std;

using int64 = long long;

string div2(string x) {
  string ret;
  int add = 0;
  for(int i = 0; i < x.size(); i++) {
    add += x[i] - '0';
    ret += (add / 2) + '0';
    add %= 2;
    add *= 10;
  }
  return ret;
}

string add(string x, string y) {
  string z = x;
  int add = 0;
  for(int i = x.size() - 1; i >= 0; i--) {
    add += x[i] - '0';
    add += y[i] - '0';
    z[i] = add % 10 + '0';
    add /= 10;
  }
  return z;
}

int64 beet(int64 x, int64 y) {
  return min(x + y, 1LL << 60);
}


string S;
int64 dp[61][10][10][2][2][2];

int64 rec(int idx, int x, int y, bool x_used, bool y_used, bool lim, bool zero) {
  if(idx == S.size()) return x_used && y_used;
  if(!lim && ~dp[idx][x][y][x_used][y_used][zero]) return dp[idx][x][y][x_used][y_used][zero];
  int64 ret = 0;
  for(int i = 0; i <= (lim ? S[idx] - '0' : 9); i++) {
    if(x != i && (i > 0 || !zero) && y != i) continue;
    ret = beet(ret, rec(idx + 1, x, y, x_used | (i == x && (x > 0 || !zero)), y_used | (i == y), lim & (i == S[idx] - '0'), zero & (i == 0)));
  }
  if(!lim) dp[idx][x][y][x_used][y_used][zero] = ret;
  return ret;
}

int64 check(string x) {
  S = x;
  int64 sub = 0;
  for(int i = 0; i < 10; i++) {
    for(int j = i + 1; j < 10; j++) {
      sub = beet(sub, rec(0, i, j, false, false, true, true));
    }
  }
  return sub;
}

int main() {
  memset(dp, -1, sizeof(dp));
  int64 N;
  while(cin >> N, N) {
    string ok = string(60, '0'), ng = string(60, '9');
    string one = string(60, '0');
    one.back() = '1';
    while(add(ok, one) < ng) {
      string mid = div2(add(ok, ng));
      if(check(mid) < N) ok = mid;
      else ng = mid;
    }
    int ptr = 0;
    ok = add(ok, one);
    while(ptr < ok.size() && ok[ptr] == '0') ++ptr;
    while(ptr < ok.size()) cout << ok[ptr++];
    cout << endl;
  }
}
accuracy: 1
time_ms: 240
memory_kb: 3528
final_rank: 5

code_snippet: //数学をしたほうが実装は楽だと思うけど、さきほどやってみたらバグ死したので、桁DP + 2分探索します。
#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

//a[i] … 10進数における下位i(>=0)桁目.
vector<int> add(vector<int> a, vector<int> b, int n) {
	for (int i = 0; i < n; i++) {
		a[i] += b[i];
		if (i + 1 < n) { a[i + 1] += a[i] / 10; a[i] %= 10; }
	}
	return a;
}

vector<int> sub(vector<int> a, vector<int> b, int n) {
	for (int i = 0; i < n; i++) {
		a[i] -= b[i];
		if (i + 1 < n && a[i] < 0) {
			int kariru = ((-a[i]) + 9) / 10;
			a[i] += 10 * kariru;
			a[i + 1] -= kariru;
		}
	}
	return a;
}

//2で割る
vector<int> half(vector<int> a, int n) {
	vector<int> ret(n, 0);
	int val = 0;
	for (int i = n - 1; i >= 0; i--) {
		val *= 10;
		val += a[i];
		ret[i] = val / 2;
		val -= ret[i] * 2;
	}
	return ret;
}

int bitCnt[1024];
void initBitCnt() {
	for (int i = 0; i < 1024; i++) {
		bitCnt[i] = 0;
		for (int j = 0; j < 10; j++) {
			if ((i >> j) & 1) bitCnt[i]++;
		}
	}
}

//桁DPで, a以下の短歌数を数える.
int INF = 1e+18;
int dp[101][1024][2];	//dp[残り桁数][今までに使った数の集合(leading 0はカウントしない)][aと一致しているか]

int dfs(int ketasu, int used, bool flag, vector<int> &a) {
	if (bitCnt[used] > 2) return 0;
	if (ketasu == 0) return (bitCnt[used] == 2);
	if (dp[ketasu][used][flag] != -1) return dp[ketasu][used][flag];
	
	int ret = 0;
	for (int i = 0; i < 10; i++) {	//次の桁をiにする
		if (flag && a[ketasu - 1] < i) break;
		
		int nUsed;
		if (used == 0 && i == 0) nUsed = 0;
		else nUsed = (used | (1 << i));
		ret += dfs(ketasu - 1, nUsed, flag && a[ketasu - 1] == i, a);
	}
	return dp[ketasu][used][flag] = ret;
}

//メモの初期化
void initDp() {
	for (int i = 0; i < 101; i++) for (int j = 0; j < 1024; j++) for (int k = 0; k < 2; k++) dp[i][j][k] = -1;
}

//2分探索の終了条件
bool isContinue(vector<int> st, vector<int> ed, int n) {
	vector<int> res = sub(ed, st, n);
	
	if (res[0] >= 2) return true;
	for (int i = 1; i < n; i++) {
		if (res[i] != 0) return true;
	}
	return false;
}

int n;

void solve() {
	initBitCnt();
	
	int i, cnt = 0;
	for (i = 1; ; i++) {
		cnt += 81 * ((1LL << (i - 1)) - 1);
		if (cnt >= n) break;
	}
	int ketasu = i;
	
	vector<int> st(ketasu), ed(ketasu), mid(ketasu);
	for (i = 0; i < ketasu; i++) st[i] = 0;
	for (i = 0; i < ketasu; i++) ed[i] = 9;
	
	//2分探索. xxxooo, (st, ed]
	while (isContinue(st, ed, ketasu)) {
		mid = add(st, ed, ketasu);
		mid = half(mid, ketasu);
		initDp();
		int res = dfs(ketasu, 0, true, mid);
		if (res >= n) ed = mid;
		else st = mid;
	}
	
	for (i = ketasu - 1; i >= 0; i--) {
		if (ed[i] != 0) break;
	}
	for (; i >= 0; i--) {
		cout << ed[i];
	}
	cout << endl;
}

signed main() {	
	while (cin >> n) {
		if (!n) break;
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 4780
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define int long long
#define pb push_back
using namespace std;
vector<int> ue,si,me;
int keta;

vector<string> ve;

vector<int> hal(vector<int> ue,vector<int> si){
	vector<int> e;
	for(int i=0;i<keta;i++)e.pb(ue[i]+si[i]);
	
	for(int i=keta-1;i>=1;i--){
		while(e[i]>=10){
			e[i]-=10;
			e[i-1]++;
		}
	}
	for(int i=0;i<keta;i++){
		if(e[i]%2){
			e[i]--;
			if(i!=keta-1)e[i+1]+=10;
		}
		e[i]/=2;
	}
	return e;
}

int ch(vector<int> V){
	int ans=0;
	//for(auto v:V)cout<<v<<" ";
	//cout<<endl;
	for(int i=0;i<10;i++)for(int j=i+1;j<10;j++){
		bool ok=false;
		
	//	if(j<V[0]) continue;
		//cout<<i<<" "<<j<<endl;
		
		string st="";
		ve.clear();
		for(int d=0;d<keta;d++){
			if(ok){
				st+="1";
			}
			else{
				if(V[d]>j){
					ve.pb(st);
					while(ve.back().size()<keta)ve.back()+="1";
					goto lll;
				}
				else if(V[d]==j){
					ve.pb(st+"0");
					while(ve.back().size()<keta)ve.back()+="1";
					st+="1";
				}
				else if(V[d]>i){
				ve.pb(st+"0");
					while(ve.back().size()<keta)ve.back()+="1";
					goto lll;
				}
				else if(V[d]==i){
					st+="0";
				}
				else goto lll;
			}
		}
		if(st.size()==keta)ve.pb(st);
		lll:;
		if(ve.size()==0) continue;
		sort(ve.begin(),ve.end());
		string s=ve.back();
		
		int ko=0;
		for(int i=0;i<keta;i++)if(s[i]=='1')ko+=1ll<<(keta-1-i);
		if(i!=0){
			if(ko==(1ll<<keta)-1) ko--;
		}
		if(i==0 && s[0]=='0') continue;
		if(i==0){
			if(ko==(1ll<<keta)-1) {
				ko=(1ll<<(keta-1))-1;
			//cout<<"d"<<endl;
			}
			else{
				ko=ko-(1ll<<(keta-1))+1;
			}
			
		}
	//	cout<<i<<" "<<j<<" "<<s<<" "<<ko<<endl;
	ans+=ko;
	}
return ans;
}


void solve(){

	int n;
	cin>>n;
	if(n==0) exit(0);
	for(int i=2;;i++){
		if(n<=(1ll<<(i-1))*81-81){
			keta=i;
			break;
		}
		n-=(1ll<<(i-1))*81-81;
	}
	//cout<<keta<<" "<<n<<endl;
	ue.clear();
	si.clear();
	
	si.pb(0);
	for(int i=1;i<=keta;i++)ue.pb(9),si.pb(9);
	si.pop_back();
	
	for(int i=1;i<=1000;i++){
		me=hal(ue,si);
		
		if(ch(me)>=n) ue=me;
		else si=me;
		
	}
	//ue.back()--;
	for(int i=0;i<keta;i++)cout<<ue[i];
	
	cout<<endl;
	//cout<<ch(ue)<<endl;
}

    signed main(){
    	while(1){
    		solve();
    	//	cout<<d<<endl;
    	}
    	
return 0;
    	
            }
accuracy: 1
time_ms: 1360
memory_kb: 3176
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
const Int MAX = 2e18;

void chmin(Int &a,Int b){if(a>b) a=b;}

Int dp[10][10][100][2][4][100];

Int dfs(Int a,Int b,Int t,Int f,Int u,Int k){
  //cout<<t<<":"<<k<<endl;
  Int &res=dp[a][b][t][f][u][k];
  if(~res) return res;
  if(t==k) return res=(u==3);
  res=0;
  res+=dfs(a,b,t,f,u|1,k+1); //use a
  res+=dfs(a,b,t,f,u|2,k+1); //use b
  chmin(res,MAX);
  return res;
}

void restore(Int n){
  //cout<<n<<":::"<<endl;
  Int k=1;
  for(;;k++){
    Int tmp=0;
    for(Int a=0;a<10;a++){
      for(Int b=a+1;b<10;b++){
	if(a!=0) tmp+=dfs(a,b,k,0,1,1); //use a
	tmp+=dfs(a,b,k,1,2,1);          //use b > 0
	chmin(tmp,MAX);
      }
    }
    if(tmp>n) break;
    n-=tmp;
  }
  // cout<<k<<endl;
  Int c,d;
  string ans;
  for(c=1;c<10;c++){
    Int tmp=0;
    for(Int a=0;a<c;a++){
      tmp+=dfs(a,c,k,1,2,1);
      chmin(tmp,MAX);
    }
    for(Int b=c+1;b<10;b++){
      tmp+=dfs(c,b,k,0,1,1);
      chmin(tmp,MAX);
    }
    if(tmp>n){
      ans+=char('0'+c);
      break;
    }
    n-=tmp;
  }
  //cout<<k<<" "<<c<<endl;
  Int l=k-1;
  while(l--){
    Int flg=0;
    for(d=0;d<c;d++){
      Int tmp=dfs(d,c,k,1,3,k-l);
      if(tmp>n){
	ans+=char('0'+d);
	flg=1;
	break;
      }
      n-=tmp;
    }
    if(flg) break;
    Int tmp=0;
    for(Int e=0;e<c;e++){
      tmp+=dfs(e,c,k,1,2,k-l);
      chmin(tmp,MAX);
    }
    for(Int e=c+1;e<10;e++){
      tmp+=dfs(c,e,k,0,1,k-l);
      chmin(tmp,MAX);
    }
    if(tmp>n){
      ans+=char('0'+c);
      continue;
    }
    n-=tmp;
    for(d=c+1;d<10;d++){
      Int tmp=dfs(c,d,k,0,3,k-l);
      if(tmp>n){
	ans+=char('0'+d);
	flg=1;
	break;
      }
      n-=tmp;
    }
    if(flg) break;
  }
  //cout<<c<<":"<<d<<endl;
  while(l--){
    if(c<d){
      Int tmp=dfs(c,d,k,0,3,k-l);
      if(tmp>n){
	ans+=char('0'+c);
      }else{	
	ans+=char('0'+d);
	n-=tmp;
      }
    }else{
      Int tmp=dfs(d,c,k,1,3,k-l);
      if(tmp>n){
	ans+=char('0'+d);
      }else{	
	ans+=char('0'+c);
	n-=tmp;
      }
    }
  }
  cout<<ans<<endl;
}
signed main(){
  memset(dp,-1,sizeof(dp));
  Int n;
  while(cin>>n,n){
    restore(n-1);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 65648
final_rank: 13