Problem:
監獄
無限人の囚人たちがいる。はじめ、囚人たちは
0, 1, 2,
... と番号が振られている。
次の操作を
N
回行う。
0
番目の囚人を釈放し、
k, 2k, 3k,
... 番目の囚人たちを処刑する。
その後、残った囚人たちに番号を振り直す。このとき、元の番号が小さい囚人から順に
0, 1, 2,
... と番号を振る。
N
回目の操作で釈放される囚人がはじめに振られていた番号を求めよ。
Constraints
1
≤
N
≤
10^5
2
≤
k
≤
10^5
答えは
10^{18}
以下である。
Input Format
入力は以下の形式で標準入力から与えられる。
N
k
Output Format
答えを一行に出力せよ。
Sample Input 1
4 2
Sample Output 1
7
Sample Input 2
1 3
Sample Output 2
0
Sample Input 3
100000 100000
Sample Output 3
99999

Submitted solutions:

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

signed main(void)
{
	int n, k;
	cin >> n >> k;
	
	ll low = 0, high = 1e18;
	while(high-low>1) {
		ll mid = (high+low)/2, tmp = mid;
		//cout << high << " " << mid << " " << low << endl;
		bool flag = false;
		REP(i, n-1) {
			tmp -= (tmp-1)/k+1;
			if(tmp <= 0) flag = true;
		}
		if(flag) low = mid;
		else high = mid;
	}
	cout << low << endl;

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3452
final_rank: 8

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

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	ll ok = 0;
	ll ng = 1e18 + 100;
	auto isOk = [&](ll X) -> bool {
		ll cur = X;
		for(int _ = 0; _ < N - 1; _++) {
			cur--;
			cur -= cur / K;
		}
		return cur <= 0;
	};
	while(ng - ok > 1) {
		ll mid = (ng + ok) / 2;
		if(isOk(mid))
			ok = mid;
		else
			ng = mid;
	}
	cout << ok << endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 9

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

int ok(ll mid, ll &N, ll &K){
    for (int i = 0; i < N - 2; i++){
        mid -= (mid / K) + 1;
        if (mid <= 0) return 1;
    }
    return 0;
}

int main(){
    ll N, K; cin >> N >> K;
    ll top = 1001001001001001001, bottom = -1;
    while (top - bottom > 1){
        ll mid = (top + bottom) / 2;
        if (ok(mid, N, K)) bottom = mid;
        else top = mid;
    }
    cout << top << endl;
}
accuracy: 0.0784313725490196
time_ms: 50
memory_kb: 3440
final_rank: 20

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

long long updiv(long long a, long long b) {
    assert(b > 0);
    return (a + b - 1) / b;
}

int main() {
    int N, K; cin >> N >> K;

    auto get = [&](long long v) -> long long { // get i satisfy i become v

        auto f = [&](long long p) -> bool {
            return p - updiv(p, K) <= v;
        };

        long long l = 0LL, r = 1e18 + 100;
        while (r - l > 1) {
            long long mid = l + (r - l) / 2;
            (f(mid) ? l : r) = mid;
        }

        return l;
    };

    long long ans = 0LL;
    for (int i = 0 ; i < N - 1 ; i++) {
        ans = get(ans);
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3440
final_rank: 13

code_snippet: #include <iostream>
using namespace std;
using ll = long long;

int main(){
    ll n, k; cin >> n >> k;
    
    ll ac = 1LL << 60, wa = -1;
    auto f = [&](ll x){
        for(int i = 0; i < n-1; i++){
            x -= (x/k)+1;
        }
        return x >= 0;
    };
    while(ac-wa > 1){
        ll wj = (ac+wa)/2;
        if(f(wj)) ac = wj;
        else wa = wj;
    }
    cout << ac << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3440
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
#include <numeric>
#include <string>
#include <bitset>
#include <assert.h>

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define popcnt(x) __builtin_popcountll(x)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};


int main() {
    int n, k; cin >> n >> k;

    ll l = -1, r = 2e18;
    while(r - l > 1) {
        ll m = (l + r) / 2;
        ll total = m + 1;
        bool ok = true;
        rep(i, n) {
            if (total <= 0) ok = false;
            ll d = (total + k - 1) / k;
            total -= d;
        }
        if (ok) r = m;
        else l = m;
    }
    cout << r << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3440
final_rank: 13

code_snippet: #include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
#define all(a) a.begin(), a.end()
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
  return os << '\n';
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (T &x : a) { is >> x; }
  return is;
}

[[maybe_unused]] static constexpr long long MOD = 998244353;
[[maybe_unused]] static constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] static constexpr long long INFL = 0x3f3f3f3f3f3f3f3fLL;

ll n, k;
ll a(ll x) {
  // n回目でx番目だったら、 n-1回では?
  // kの倍数でないもののうち、x番目
  ll l = 0, r = INFL;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    ll km = m / k + 1; // include 0

    if (m - km >= x)
      r = m;
    else
      l = m;
  }
  return r;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> k;
  ll ans = 0;
  rep(i, 0, n - 1) ans = a(ans);
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3440
final_rank: 13

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

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

ll func(){
	ll n = in();
	ll k = in();
	ll res = 0;
	for(int i=0;i<n-1;++i){
		ll lp = 1;
		ll rp = 2e18;
		while(lp < rp){
			ll mid = (lp + rp) / 2;
			if((res + mid -1) / k + 1 < mid){
				rp = mid;
			}else{
				lp = mid + 1;
			}

		}
		for(ll add=max(1LL,lp-2);add<rp+3;++add){
			if((res+add-1)/k+1==add and (res+add)%k!=0){
				res += add;
				break;
			}
		}
	}
	return res;
}

int main(){
	cout << func() << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3424
final_rank: 3

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

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

ll func(){
	ll n = in();
	ll k = in();
	ll res = 0;
	for(int i=0;i<n-1;++i){
		ll lp = 1;
		ll rp = 2e18;
		while(lp < rp){
			ll mid = (lp + rp) / 2;
			if((res + mid -1) / k + 1 < mid){
				rp = mid;
			}else{
				lp = mid + 1;
			}

		}
		for(ll add=max(1LL,lp-2);add<rp+3;++add){
			if((res+add-1)/k+1==add and (res+add)%k!=0){
				res += add;
				break;
			}
		}
	}
	return res;
}

int main(){
	cout << func() << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3420
final_rank: 2

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

int main(void) {
    sonic();
    int n, k;
    cin >> n >> k;

    ll l = 0, r = INF;
    while (r - l > 1) {
        ll mid = (l + r) >> 1;
        ll m = mid;
        rep(i, n - 1) {
            m -= (m - 1) / k + 1;
        }
        if (m > 0)
            r = mid;
        else
            l = mid;
    }
    co(l);

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3448
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	
	ll N,K; cin >> N >> K;

	auto f = [&](ll x) {
		for(int i = 0; i < N - 1; i++) {
			if(x % K == 0) x++;
			x -= x / K + 1;
		}
		return x > 0;
	};

	ll hi = 2e18, lo = 0;
	while(hi - lo > 1) {
		ll mid = (lo + hi) / 2;
		(f(mid) ? hi : lo) = mid;
	}
	cout << lo << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 3460
final_rank: 17

code_snippet: // #include "atcoder/all"
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <functional>
#include <cassert>
using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e15;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
// constexpr ll mod = 998244353;
// 右下左上
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define eol "\n"
// ---------------------------------------------------------------------------



bool solve(){
    ll N,K;
    cin >> N >> K;
    ll ng=-1,ok=1e18;
    while(ok-ng>1){
        ll m = (ok+ng)/2;
        ll rem = 0;
        bool can = true;
        for(int i=0; i<N-1; i++){
            if(m < rem) can = false;            
            rem += (m-rem)/K + 1;
        }
        if(m < rem) can = false;
        if(can){
            ok = m;
        }else{
            ng = m;
        }
    }
    cout << ok << endl;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // while(solve());
    solve();
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3448
final_rank: 16

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

void solve(int N, int K){
  int64_t ok = 2e18;
  int64_t ng = -1;
  while(ok - ng > 1){
    int64_t mid = (ok+ng)/2;
    int64_t M = mid+1;
    for(int i=0; i<N-1; i++){
      M -= 1;
      M -= M/K;
    }
    if(M <= 0){
      ng = mid;
    }
    else{
      ok = mid;
    }
  }
  cout << ok << endl;
}

int main(){
  while(true){
    int N, K;
    cin >> N >> K;
    solve(N,K);
    break;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3420
final_rank: 4

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

int main() {
    int N, K;
    cin >> N >> K;

    auto check = [&](ll x) -> bool {
        ll now = x;
        REP(i, N-1){
            now -= now / K + 1;
            if(now < 0) return false;
        }
        return (now >= 0);
    };

    ll ok = 1000000000000000000LL + 1, ng = -1;
    while(ok - ng > 1) {
        ll mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3472
final_rank: 12

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
using namespace std;

const int max_n = 103002;

int main(){
fcin;
	ll n,k;
	cin>>n>>k;
	ll x=1;
	rep(i,n-1){
		// int ad=0;
		// while((x+ad)/k>ad){
		// 	ad++;
		// }
		ll l=0,r=lnf,c=0;
		while(l<=r){
			ll m=(l+r)/2;
			ll ad=m;
			if((x+ad)/k>ad){
				l=m+1;
			}else{
				c=m;
				r=m-1;
			}
		}
		// cout<<c<<"\n";
		x+=c;
		x++;
	}
	cout<<x-1<<"\n";
/*
*/
    return 0;   
}
accuracy: 1
time_ms: 40
memory_kb: 3456
final_rank: 4

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
using namespace std;

const int max_n = 103002;

int main(){
fcin;
	ll n,k;
	cin>>n>>k;
	ll x=1;
	rep(i,n-1){
		// int ad=0;
		// while((x+ad)/k>ad){
		// 	ad++;
		// }
		ll l=0,r=inf,c=0;
		while(l<=r){
			ll m=(l+r)/2;
			ll ad=m;
			if((x+ad)/k>ad){
				l=m+1;
			}else{
				c=m;
				r=m-1;
			}
		}
		// cout<<c<<"\n";
		x+=c;
		x++;
	}
	cout<<x-1<<"\n";
/*
*/
    return 0;   
}
accuracy: 0.1568627450980392
time_ms: 10
memory_kb: 3452
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main(){
    ll N, K; cin >> N >> K;

    ll l = 0, r = INF; //[l,r)
    while(r-l > 1){
        ll m = (l+r)/2;
        ll now = m;
        rep(i, N-1){
            now -= (now+K-1)/K;
        }
        (now == 0? l : r) = m;
    }

    cout << l << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 9

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template <typename F>
class
FixPoint : private F
{
public:
  explicit constexpr FixPoint(F&& f) noexcept
    : F{std::forward<F>(f)}
  {}

  template <typename... Args>
  constexpr decltype(auto)
  operator()(Args&&... args) const
  {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};  // class FixPoint


namespace
{
  template <typename F>
  inline constexpr decltype(auto)
  makeFixPoint(F&& f) noexcept
  {
    return FixPoint<F>{std::forward<F>(f)};
  }
}  // namespace

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64 N, k;
  cin >> N >> k;
  int64 l = -1, r = 2*INF_LL, m;

  auto ok = [&](int64 x) {
    int64 sum = 0, inix = x;
//    cout << x << endl;
    REP(i, N-1) {
      sum += x / k + 1;
      x = x - (x / k + 1);
//      cout << "    " << i << ": " << sum << " " << x << " " << endl;
    }
//    cout << endl;
    return sum < inix + 1;
  };

  while (r - l > 1) {
    m = (l + r) / 2;

    if (ok(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
constexpr Int inf = 1e18;
template<class F, class T>
auto  minimize(T imin,T imax,F &f){
    while(imax - imin > 1){
        T mid = imin + (imax - imin)/2;
        if(f(mid)) imax = mid;
        else imin = mid;
    }
    return imax;
}

bool solve(){
    Int N,K; cin >> N >> K;
    Int ans = 0;
    if(N == 1){
        cout << 0 << endl; return false;
    }
    auto f = [&](Int mid){
        return mid - (mid / K + 1) >= ans;
    };
    for(int i = 0; i < N - 1; ++i){
        Int add = minimize(-1LL,inf,f);
        assert(add - (add / K + 1) == ans);
        ans = add;
    }
    cout << ans << endl;
    return false;
}
int main(){
    while(solve());
}
accuracy: 1
time_ms: 80
memory_kb: 3112
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
constexpr Int inf = 1e18;
template<class F, class T>
auto  minimize(T imin,T imax,F &f){
    while(imax - imin > 1){
        T mid = imin + (imax - imin)/2;
        if(f(mid)) imax = mid;
        else imin = mid;
    }
    return imax;
}

bool solve(){
    Int N,K; cin >> N >> K;
    Int ans = 0;
    if(N == 1){
        cout << 0 << endl; return false;
    }
    auto f = [&](Int mid){
        return mid - (mid / K + 1) >= ans;
    };
    for(int i = 0; i < N - 1; ++i){
        int add = minimize(-1LL,inf,f);
        assert(add - (add / K + 1) == ans);
        ans = add;
    }
    cout << ans << endl;
    return false;
}
int main(){
    while(solve());
}
accuracy: 0.1568627450980392
time_ms: 70
memory_kb: 3112
final_rank: 18