Skip to content

Instantly share code, notes, and snippets.

@komakai
Created February 8, 2025 03:46
Show Gist options
  • Save komakai/51fc9f84b2098e1b6d6d7080ae246eed to your computer and use it in GitHub Desktop.
Save komakai/51fc9f84b2098e1b6d6d7080ae246eed to your computer and use it in GitHub Desktop.
Shuffles
#include <iostream>
#include <vector>
int getAt(long i, int n, int m) {
long long tmpI = i;
for (int tmpN = n; tmpN > 0; tmpN--) {
long long half = m * /*pow(2, tmpN - 1)*/ (1LL << (tmpN - 1));
long long quarter = (half + 1) / 2;
long long adjustedI = tmpI % half;
if (adjustedI < quarter) {
tmpI = adjustedI * 2;
}
else {
tmpI = ((adjustedI - quarter) * 2 + 1);
}
}
return static_cast<int>(tmpI);
}
std::vector<int> solve(int n, int m, long a, long b) {
std::vector<int> output(b - a);
for (long i = a; i < b; i++) {
output[static_cast<std::size_t>(i - a)] = getAt(i, n, m);
}
return output;
}
int main()
{
auto solution = solve(58, 57, 0, 104);
for (auto s : solution) {
std::cout << s << " ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment