Problem:
H - N and K
1以上
N
以下の整数からなる狭義単調増加数列で，数列の要素からどの2つの異なる数を取ってきても
    他方が一方を割り切ることがないようなものを考える．
    そのような数列のうち，とりうる最大の長さを
L
として，そのような数列
(a_1,a_2,...,a_L)
を考える．
    この数列の
K
番目の要素
a_K
を求めよ．ただし，そのような数列
(a_1...a_L)
が複数個存在する場合は，
a_K
の値としてとりうる最小の値を出力せよ．
    また，
K > L
のときは-1を出力せよ．
入力形式
入力は複数の入力からなる．入力の数は
C
セットあり，
i
番目の入力は
N_i, K_i
である．
  テストケースは以下の形式で与えられる．
C
N_1
K_1
…
N_C
K_C
出力形式
出力は
C
行からなる．
i
行目の出力は，
N_i
と
K_i
についての答えである．
制約
1 \leq C \leq 10^5
1 \leq N_i \leq 10^{18}
1 \leq K_i \leq N_i
入力値はすべて整数である．
この問題の判定には，50 点分のテストケースのグループが設定されている．このグループに含まれるテストケースは上記の制約に加えて下記の制約も満たす．
C=1, N_i \leq 50
入出力例
入力例1
5
3 2
5 2
8 3
10 1
18 12
出力例1
3
3
5
4
-1
N=3
のとき，2番目に小さい要素が最小となる組み合わせの一例として
(2,3)
が挙げられる．
N=5
のとき，2番目に小さい要素が最小となる組み合わせの一例として
(2,3,5)
が挙げられる．
N=8
のとき，3番目に小さい要素が最小となる組み合わせの一例として
(3,4,5,7)
が挙げられる．
N=10
のとき，1番目に小さい要素が最小となる組み合わせの一例として
(4,6,7,9,10)
が挙げられる．

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

struct Range {
	long long int from, to;
};
long long int power(long long int base, int exp) {
	switch (exp) {
	case 0: return 1LL;
	case 1: return base;
	default: return power(base * base, exp / 2) * power(base, exp % 2);
	}
}
long long int k_th_element(const long long int N, const long long int K) {
	if (K > (N + 1) / 2) return -1;
	std::vector<Range> result;
	for (long long int exp = 1; exp <= N; exp *= 3) {
		result.push_back(Range{ N / exp / 3 + 1, N / exp });
	}
	for (auto& r : result) {
		if (r.from % 2 == 0) ++r.from;
		if (r.to % 2 == 0) --r.to;
	}
	long long int min = 1LL, max = N;
	while (min < max) {
		auto mid = (min + max) / 2;
		long long int count = 0, exp = 1;
		for (auto i = 0; i < result.size(); ++i) {
			if (mid >= result[i].from * exp) {
				count += (std::min(result[i].to, mid / exp) - result[i].from) / 2 + 1;
			}
			exp *= 2;
		}
		if (count < K) min = mid + 1;
		else max = mid;
	}
	return max;
}
int main() {
	int c; std::cin >> c;
	for (auto i = 0; i < c; ++i) {
		long long int n, k; std::cin >> n >> k;
		std::cout << k_th_element(n, k) << '\n';
	}
}
accuracy: 1
time_ms: 2000
memory_kb: 3164
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int C;
    scanf(" %d", &C);
    while(C--){
        ll n,k;
        scanf(" %lld %lld", &n, &k);

        if(n%2==0) --n;
        ll L = (n+1)/2;

        if(L < k){
            printf("-1\n");
            continue;
        }

        ll ng=0, ok=n;
        while(ok-ng>1){
            ll mid = (ng+ok)/2;

            ll t = n;
            ll ct = 0;

            ll d = 2;
            while(t>0){
                ll nt = t/3;
                if(nt%2==0) --nt;

                ll S = nt+2, E = t;
                if(S>E) S = 1;
                ll num = (E-S)/2 + 1;

                if(mid - S*d/2 >= 0){
                    ll i = (mid - S*d/2)/d;
                    ct += min(num,i+1);
                }

                t = nt;
                d *= 2;
            }

            if(ct >= k) ok = mid;
            else ng = mid;
        }
        printf("%lld\n", ok);
    }
    return 0;
}
accuracy: 1
time_ms: 2220
memory_kb: 3216
final_rank: 7

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

ll c, n, k;

ll solve();

int main() {
  cin >> c;
  while(c) {
    cin >> n >> k;
    //存在しうるか
    if((n + 1) / 2 < k)
      cout << -1 << endl;
    else
      cout << solve() << endl;
    --c;
  }

  return 0;
}

ll solve() {
  ll l = 1, r = (n + 1) / 2, now, nownum, cnt, i, bit = 0,
     bf, store, allstore = (n + 1) / 2;
  //にぶたんの範囲を決める
  while(allstore > 0) {
    r = allstore;
    allstore = (allstore + 1) / 3;
    l = allstore + 1;
    //答えを求めるためのにぶたん
    while(r - l >= 0) {
      //現在の真ん中
      nownum = (l + r) / 2;
      now = (1 << (bit + 1)) * nownum - (1 << bit);
      cnt = 0;
      store = (n + 1) / 2;
      // 2^iの段ごとに個数を数える
      for(i = 0; (1 << i) <= n && store != 0; ++i) {
        bf = (1 << (i + 1)) * store - (1 << i);
        store = (store + 1) / 3;
        ll p = ((1 << i) + min(bf, now)) / (1 << (i + 1)) -
               store;
        cnt += max((ll)0, p);
      }
      if(cnt == k)
        return now;
      else if(r - l == 1)
        l = r;
      else if(r - l == 0)
        break;
      else if(cnt > k)
        r = nownum;
      else
        l = nownum;
    }
    ++bit;
  }
  return -1;
}
accuracy: 1
time_ms: 3210
memory_kb: 3108
final_rank: 8

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

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

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll C;

int main(){
  cin>>C;
  while(C--){
    ll N,K;
    cin>>N>>K;
    if(N%2==0)N--;
    ll L=(N+1)/2;
    if(K>L){
      cout<<-1<<endl;
      continue;
    }
    ll lb=0,ub=N;
    while(ub-lb>1){
      ll X=(lb+ub)/2;
      //dbg(X);
      
      ll t=N;
      ll d=2;
      ll cnt=0;
      while(t>=1){
        ll nt = t/3;
        if(nt%2==0) nt--;

        ll hi=t,lo=nt+2;
        if(lo>hi) lo=1;
        ll num = (hi-lo)/2 + 1;

        if(X-lo*(d/2)>=0){
          ll i=(X-lo*(d/2))/d;
          //dbg(hi); dbg(lo);
          //dbg(i);
          cnt+=min(num,i+1);
        }
        t = nt;
        d*=2;
      }
      if(cnt>=K)ub=X;
      else lb=X;
    }
    cout<<ub<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 2180
memory_kb: 3068
final_rank: 6

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
	int a;
	scanf("%d",&a);
	for(int i=0;i<a;i++){
		long long b,c;
		scanf("%lld%lld",&b,&c);
		if((b+1)/2<c){
			printf("-1\n");continue;
		}
		long long left=0;
		long long right=b+1;
		while(left+1<right){
			long long M=(left+right)/2;
			long long mul=1;
			long long now=b;
			long long num=0;
			while(now>0){
				if(now%2==0)now--;
				long long tmp=now/3;
				if(tmp%2==0)tmp--;
				tmp=(tmp+1)/2;
				long long t=min((M/mul+1)/2,(now+1)/2);
				if(tmp<t)num+=t-tmp;
				now=tmp*2-1;
				mul*=2;
			}
			if(num>=c){
				right=M;
			}else left=M;
		}
		printf("%lld\n",right);
	}
}
accuracy: 1
time_ms: 2660
memory_kb: 1036
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
typedef long long LL;

// count # (<= X)
LL check(const LL N, const LL X){
    LL right = N;
    LL p2 = 1;
    LL res = 0;
    LL mid = X;
    // printf("n = %lld x = %lld\n", N, X);
    while(right > 2){
        // [left, right]
        LL left = right / 3 + 1;
        if(left <= 2) left = 3;

        // [left, mid] (mid, right]
        if(mid > right) mid = right;
        // printf("left = %lld mid = %lld right = %lld\n", left, mid, right);

        // [left, mid]のうち奇数の個数
        if(!(left & 1)) left++;
        LL m = mid;
        if(!(m & 1)) m--;
        if(left <= m){
            res += ((m - left) >> 1) + 1;
        }

        right = right / 3;
        mid >>= 1;
        p2 <<= 1;
    }
    if(p2 <= X) res++;

    // printf("-> %lld\n\n", res);
    return res;
}

LL solve(LL N, LL K){
    if(K > (N + 1) / 2) return -1;
    if(N % 2 == 0) N--;
    LL lb = 0;
    LL ub = N;
    while(ub - lb > 1){
        LL x = (ub + lb) / 2;
        if(check(N, x) >= K){
            ub = x;
        }else{
            lb = x;
        }
    }
    return ub;
}
LL f(LL x, LL n){
    LL q = x;
    LL p2 = 1;
    while(3 * x <= n){
        x *= 3;
        p2 *= 2;
    }
    return q * p2;
}
LL solve2(LL N, LL K){
    if(N <= 2){
        if(K == 1) return 1;
        else return -1;
    }
    if(N % 2 == 0) N --;
    vector<LL> v;
    v.push_back(f(2, N));
    for(LL i = 3; i <= N; i += 2){
        v.push_back(f(i, N));
    }
    sort(v.begin(), v.end());
    if(v.size() < K) return -1;
    return v[K - 1];
}
vector<vector<int>> max(int n){
    int max_l = 0;
    vector< vector<int> > res;
    REP(s, 1 << (n - 1)){
        vector<int> v;
        REP(i, n - 1) if(s >> i & 1){
            v.push_back(i + 2);
        }
        bool ok = true;
        REP(i, v.size()) REP(j, i) if(v[i] % v[j] == 0) ok = false;
        if(ok){
            if(max_l < v.size()) {
                max_l = v.size();
                res.clear();
            }
            if(max_l == v.size()){
                res.push_back(v);
            }
        }
    }
    return res;
}
LL solve3(LL N, LL K){
    cout << N << " " << K << endl;
    if(N <= 2) {
        if(K == 1) return 1;
        else return -1;
    }
    vector<vector<int>> res = max(N);
    if(res[0].size() < K) return -1;
    return res[0][K - 1];
}

int main(){
    int C;
    cin >> C;
    while(C--){
        LL N, K;
        scanf("%lld %lld", &N, &K);
        printf("%lld\n", solve(N, K));
    }
    // for(int n = 1; n < 25; n++){
    //     for(int k = 1; k <= n; k++){
    //         printf("n = %d k = %d\n", n, k);
    //         LL x = solve(n, k);
    //         LL y = solve3(n, k);
    //         printf("x = %lld y = %lld\n", x, y);
    //         if(x != y){
    //             assert(false);
    //         }
    //     }
    // }
    return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 1212
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
typedef long long LL;

// count # (<= X)
LL check(const LL N, const LL X){
    LL right = N;
    LL p2 = 1;
    LL res = 0;
    // printf("n = %lld x = %lld\n", N, X);
    while(right > 2){
        // [left, right]
        LL left = right / 3 + 1;
        if(left <= 2) left = 3;

        // [left, mid] (mid, right]
        LL mid = X / p2;
        if(mid > right) mid = right;
        // printf("left = %lld mid = %lld right = %lld\n", left, mid, right);

        // [left, mid]のうち奇数の個数
        if(left % 2 == 0) left++;
        if(mid % 2 == 0) mid--;
        if(left <= mid){
            res += (mid - left) / 2 + 1;
        }

        right = right / 3;
        p2 *= 2;
    }
    if(p2 <= X) res++;

    // printf("-> %lld\n\n", res);
    return res;
}

LL solve(LL N, LL K){
    if(K > (N + 1) / 2) return -1;
    if(N % 2 == 0) N--;
    LL lb = 0;
    LL ub = N;
    while(ub - lb > 1){
        LL x = (ub + lb) / 2;
        if(check(N, x) >= K){
            ub = x;
        }else{
            lb = x;
        }
    }
    return ub;
}
LL f(LL x, LL n){
    LL q = x;
    LL p2 = 1;
    while(3 * x <= n){
        x *= 3;
        p2 *= 2;
    }
    return q * p2;
}
LL solve2(LL N, LL K){
    if(N <= 2){
        if(K == 1) return 1;
        else return -1;
    }
    if(N % 2 == 0) N --;
    vector<LL> v;
    v.push_back(f(2, N));
    for(LL i = 3; i <= N; i += 2){
        v.push_back(f(i, N));
    }
    sort(v.begin(), v.end());
    if(v.size() < K) return -1;
    return v[K - 1];
}
vector<vector<int>> max(int n){
    int max_l = 0;
    vector< vector<int> > res;
    REP(s, 1 << (n - 1)){
        vector<int> v;
        REP(i, n - 1) if(s >> i & 1){
            v.push_back(i + 2);
        }
        bool ok = true;
        REP(i, v.size()) REP(j, i) if(v[i] % v[j] == 0) ok = false;
        if(ok){
            if(max_l < v.size()) {
                max_l = v.size();
                res.clear();
            }
            if(max_l == v.size()){
                res.push_back(v);
            }
        }
    }
    return res;
}
LL solve3(LL N, LL K){
    cout << N << " " << K << endl;
    if(N <= 2) {
        if(K == 1) return 1;
        else return -1;
    }
    vector<vector<int>> res = max(N);
    if(res[0].size() < K) return -1;
    return res[0][K - 1];
}

int main(){
    int C;
    cin >> C;
    while(C--){
        LL N, K;
        scanf("%lld %lld", &N, &K);
        printf("%lld\n", solve(N, K));
    }
    // for(int n = 1; n < 25; n++){
    //     for(int k = 1; k <= n; k++){
    //         printf("n = %d k = %d\n", n, k);
    //         LL x = solve(n, k);
    //         LL y = solve3(n, k);
    //         printf("x = %lld y = %lld\n", x, y);
    //         if(x != y){
    //             assert(false);
    //         }
    //     }
    // }
    return 0;
}
accuracy: 1
time_ms: 2240
memory_kb: 1212
final_rank: 3

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (typeof((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(typeof(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}





int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );

    
    int C;
    cin >> C;
    for (int id = 0; id < C; ++id) {
        ll N, K;
        cin >> N >> K;
    
        ll L = (N+1)/2;
        if (K > L) {
            cout << -1 << endl;
            continue;
        }
        if (N == 1 && K == 1) {cout << 1 << endl; continue;}
            
        vector<pair<ll,ll> > seg;
            
        ll temp = N;
        ll fac = 1;
        if (temp % 2 == 0) temp--;
        while (temp != 0) {
            ll p = temp;
            if (p % 2 == 0) --p;
            seg.PB(MP(p,fac));
            temp /= 3;
            fac *= 2;
        }
        reverse(ALL(seg));
        
        //COUT(seg);
        
        ll lo = 0, up = N+1, med;
        while (up-lo > 1) {
            //cout << MP(lo, up) <<", " << med <<  " : " << endl;
            
            med = (up+lo)/2;
            
            ll con = 0;
            for (int i = seg.size()-1; i >= 0; --i) {
                ll temp = 0;
                
                if (med >= seg[i].FI * seg[i].SE) {
                    con += (seg[i].FI+1)/2;
                    break;
                }
                
                ll left;
                if (i != 0) left = seg[i-1].FI + 2;
                else left = 1;
                ll right = seg[i].FI;
                
                ll fac = seg[i].SE;
                ll d = med/fac;
                if (d % 2 == 0) --d;
                
                ll p = max(1LL, left);
                ll q = min(d, right);
                
                temp = (q-p)/2 + 1;
                
                //cout << seg[i] << ": " << MP(right,left) << temp << ", " << con << endl;
                
                if (p <= q) con += temp;
            }
            
            if (con >= K) up = med;
            else lo = med;
        }

        cout << up << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 1208
final_rank: 1