fork download
  1. #include <cstdint>
  2. #include <climits>
  3. #include <cmath>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. typedef uint64_t Integer;
  9.  
  10. constexpr Integer power(Integer a, int n) {
  11. return (n == 0) ? 1 : a*power(a, n-1);
  12. }
  13.  
  14. constexpr Integer power13[10] = {
  15. power(0,13), power(1,13), power(2,13), power(3,13), power(4,13),
  16. power(5,13), power(6,13), power(7,13), power(8,13), power(9,13),
  17. };
  18.  
  19. template<unsigned K> inline void crack(const int from, const Integer acc) {
  20. for (int n = from; n < 10; n++) {
  21. crack<K-1>(n, acc + power13[n]);
  22. }
  23. }
  24.  
  25. template<> inline void crack<0>(const int from, const Integer acc) {
  26. Integer a = acc;
  27. Integer sum = 0;
  28. while (a != 0) {
  29. sum += power13[a%10];
  30. a /= 10;
  31. }
  32. if (acc == sum) {
  33. cout << acc << endl;
  34. }
  35. }
  36.  
  37. int main() {
  38. constexpr int N = 15;
  39. constexpr int BITS = sizeof(Integer)*CHAR_BIT;
  40. static_assert(BITS > log2(N) + 13*log2(9), "Integer is too small!");
  41. static_assert(BITS > (N-1)*log2(10), "Integer is too small!");
  42.  
  43. crack<N>(0,0);
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.9s 3296KB
stdin
Standard input is empty
stdout
0
1
564240140138