CSES - Shared codeLink to this code: https://cses.fi/paste/dc02d161cd7757f66706a7/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define fr(i, n) for (int i = 0; i < n; i++)
#define frr(i, a, b) for (int i = a; i < b; i++)
#define fi(i, arr) for (auto i : arr)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector<vector<int>> arr;
    int root = sqrt(n);
    int ind1 = 0, ind2 = 0, count = 0;

    vector<int> v;
    frr(i, 1, n + 1)
    {
        if (count > root)
        {
            count = 0;
            arr.push_back(v);
            vector<int> v1;
            v = v1;
        }
        v.push_back(i);
        count++;
    }
    if (!v.empty())
        arr.push_back(v);

    fr(i, n)
    {
        int j = k % (n - i);
        while (j)
        {
            if (ind2 != 0)
            {
                if (ind2 + j < arr[ind1].size())
                {
                    ind2 += j;
                    j = 0;
                }
                else
                {
                    j -= arr[ind1].size() - ind2;
                    ind2 = 0;
                    ind1++;
                }
            }
            else if (arr[ind1].size() > j)
            {
                ind2 = j;
                j = 0;
            }
            else
            {
                j -= arr[ind1].size();
                ind1++;
            }
            if (ind1 >= arr.size())
                ind1 = 0;
        }
        while (arr[ind1].size() <= ind2)
        {
            ind2 = 0;
            ind1++;
            if (ind1 >= arr.size())
                ind1 = 0;
        }
        cout << arr[ind1][ind2] << " ";
        if (i != n - 1)
        {
            arr[ind1].erase(arr[ind1].begin() + ind2);
            while (arr[ind1].size() <= ind2)
            {
                ind2 = 0;
                ind1++;
                if (ind1 >= arr.size())
                    ind1 = 0;
            }
        }
    }

    return 0;
}