CSES - Shared codeLink to this code:
https://cses.fi/paste/2f252c3072f765342c1e43/
//chtholly and emilia will carry me to cm
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cassert>
#include <algorithm>
#include <vector>
#include <array>
#include <tuple>
#define ll long long
#define lb long double
#define sz(vec) ((int)(vec.size()))
#define all(x) x.begin(), x.end()
const lb eps = 1e-9;
const ll mod = 1e9 + 7, ll_max = 1e18;
//const ll mod = (1 << (23)) * 119 +1;
const int MX = 8e5 +10, int_max = 0x3f3f3f3f;
using namespace std;
//i will learn from moo.
/* Input */
template<class T> void read(T &x) { cin >> x; }
template<class H, class T> void read(pair<H, T> &p) { cin >> p.first >> p.second; }
template<class T, size_t S> void read(array<T, S> &a) { for (T &i : a) read(i); }
template<class T> void read(vector<T> &v) { for (T &i : v) read(i); }
template<class H, class... T> void read(H &h, T &...t) { read(h); read(t...); }
/* Output */
template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.first << " " << p.second; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; for (T i : a) o << s << i, s = " "; return o; }
template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; for (T i : v) o << s << i, s = " "; return o; }
template<class T> void write(T x) { cout << x; }
template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }
void print() { write('\n'); }
template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
/* Misc */
//bool ckmin(auto &a, const auto &b) { return (a > b) ? a = b, 1 : 0; }
//bool ckmax(auto &a, const auto &b) { return (a < b) ? a = b, 1 : 0; }
int sum[MX], n, s = 1;
inline int LC(int k){ return k*2 +1; }
inline int RC(int k){ return k*2 +2; }
int S(int qL, int qR, int k, int L, int R){
if(qR <= L || R <= qL || R <= L) return 0;
if(qL <= L && R <= qR) return sum[k];
int mid = (L + R)/2;
return S(qL, qR, LC(k), L, mid) + S(qL, qR, RC(k), mid, R);
}
void U(int p, int v, int k, int L, int R){
if(R <= p || p < L) return ;
if(L +1 == R){
sum[k] += v;
return ;
}
int mid = (L + R)/2;
U(p, v, LC(k), L, mid);
U(p, v, RC(k), mid, R);
sum[k] = sum[LC(k)] + sum[RC(k)];
}
int Q(int k, int kth, int L, int R){
if(L + 1 == R) return L;
int mid = (L + R)/2, dist = sum[LC(k)];
if(kth < dist) return Q(LC(k), kth, L, mid);
return Q(RC(k), kth - dist, mid, R);
}
void solve(){
int k;
read(n, k);
while(s <= n) s *= 2;
for(int i = 1; i<=n; i++) U(i, 1, 0, 0, s);
int c = 1;
while(n >= 1){
int t = k%n, p = S(1, c, 0, 0, s);
//print(n, k, c, t, p);
if(p + t >= n){
c = Q(0, p + t - n, 0, s);
}else{
c = Q(0, p + t, 0, s);
}
write(c, ' ');
U(c, -1, 0, 0, s);
n--;
}
print();
//print(c);
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int T = 1;
//cin >> T;
while(T--){
solve();
}
return 0;
}