Skip to content

Instantly share code, notes, and snippets.

@dobrokot dobrokot/f(f(x))=-x.cpp
Last active Aug 29, 2015

Embed
What would you like to do?
#include <iostream>
#include <algorithm>
int f[100];
int mx[100]; // mx[x] = (-x modulo N)
int main() {
for (int i = 0; i < N; ++i) {
f[i] = i;
mx[i] = (N - i) % N;
}
do {
bool found = true;
for (int i = 0; i < N; ++i) {
if (f[f[i]] != mx[i]) {
found = false;
break;
}
}
if (found) {
std::cout << N << ": ";
for (int i = 0; i < N; ++i) {
std::cout << f[i] << ';';
}
std::cout << std::endl;
exit(0);
}
} while(N <= 1 || std::next_permutation(f+1, f+N));
std::cout << N << ": not found." << std::endl;
}
for i in {1..16}; do g++ -DN="$i" -O3 -DNDEBUG x.cpp && ./a.out ; done
1: 0;
2: 0;1;
3: not found.
4: not found.
5: 0;2;4;1;3;
6: 0;2;5;3;1;4;
7: not found.
8: not found.
9: 0;2;8;4;6;3;5;1;7;
10: 0;2;9;4;7;5;3;6;1;8;
11: not found.
12: not found.
13: 0;2;12;4;10;6;8;5;7;3;9;1;11;
14: 0;2;13;4;11;6;9;7;5;8;3;10;1;12;
15: not found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.