-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject Euler #24.cpp
64 lines (53 loc) · 1.65 KB
/
Project Euler #24.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
//#define ORIGINAL
#ifdef ORIGINAL
unsigned int numPermutation = 1000000;
std::string current = "0123456789";
while (--numPermutation)
std::next_permutation(current.begin(), current.end());
std::cout << current << std::endl;
#else
const std::string abc = "abcdefghijklm";
unsigned int tests;
std::cin >> tests;
while (tests--)
{
// to find the permutation we treat the input number as something written
// in a "factorial" system:
// x = pos0 * 12! + pos1 * 11! + pos2 * 10! + ... + pos12 * 1!
// (we have 13 letters, therefore the position of the first letter is in 12! radix)
// precomputed 0! .. 12!
const unsigned long long factorials[13+1] =
{ 1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800 };
// 13! which exceed 32 bits
unsigned long long x;
std::cin >> x;
// reduce to a single cycle (repeats after 13! iterations)
x %= factorials[abc.size()];
// that factorial system is zero-based ...
x--;
// strip off single letters (until empty)
auto remain = abc;
// our wanted permutation
std::string result;
while (!remain.empty())
{
// get next digit in that strange number system :-)
auto currentFactorial = factorials[remain.size() - 1];
auto pos = x / currentFactorial;
// store the associated letter
result += remain[pos];
// and remove it from the still unprocessed data
remain.erase(pos, 1);
// eliminate the processed digit
x %= currentFactorial;
}
std::cout << result << std::endl;
}
#endif
return 0;
}