Link to this code:
https://cses.fi/paste/fdec157914d7ddebdb8561/#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define PB push_back
#define EB emplace_back
#define ff first
#define ss second
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) (x).begin(), (x).end()
#define take(arr) for(auto &x:arr) cin>>x;
#define print(arr) for(auto &x:arr) cout<<x<<" "; cout<<endl;
#define printpi(arr) for(auto &x:arr) cout<<x.ff<<" "<<x.ss<<endl;
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pair<int,int>> vpi;
typedef vector<vector<int>> vvi;
pair<int,int> tree[1000000];
void build(int ind,int low,int high){
if(low==high){
tree[ind]={1,low+1};
return;
}
int mid=(low+high)/2;
build(2*ind+1,low,mid);
build(2*ind+2,mid+1,high);
tree[ind]={tree[2*ind+1].first+tree[2*ind+2].first,-1};
}
void query(int ind,int x){
tree[ind].first-- ;
if (tree[ind].second != -1) {
cout << tree[ind].second << ' ';
return ;
}
else if (tree[2*ind+1].first >= x){
return query(2*ind+1,x) ;
}
else return query(2*ind+2,x-tree[2*ind+1].first) ;
}
void solve(){
int n,k ;cin >> n>>k ;
build(0,0,n-1) ;
// cout << tree[0].ff ;
int x = k + 1 ;
for (int i = 0 ; i < n ; i++){
x = x % (n-i) ;
if ( x == 0 ) x = n - i ;
query(0,x) ;
x = x + k ;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("C:/Users/sumit/Desktop/projects/input.txt","r",stdin);
freopen("C:/Users/sumit/Desktop/projects/output.txt","w",stdout);
#endif
ios_base :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
int _=1;
// cin>>_;
while(_--){
solve();
}
return 0;
}