Advertisement
am1x

interestinga.cpp

Sep 11th, 2023
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | Haiku | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <inttypes.h>
  4. #include <vector>
  5.  
  6. typedef unsigned int uint_t;
  7. const uint_t D = 10;
  8.  
  9.  
  10. class Solver {
  11.  
  12.     void do_solve(std::vector<uint8_t> &v, uint64_t sum, uint64_t prod, uint64_t n, uint8_t d)
  13.     {
  14.         uint_t l = v.size();
  15.         for (uint8_t j = d; j >= 2; j--) {
  16.             v.push_back(j);
  17.             uint64_t sumj = sum + j;
  18.             uint64_t prodj = prod * j;
  19.             uint64_t nj = (prodj - sumj) + l + 1;
  20.             if (nj == n) {
  21.                 printf("%" PRIu64 ": ", n);
  22.                 for (uint8_t x: v)
  23.                     printf("%u ", (uint_t) x);
  24.                 printf("\n");
  25.             }
  26.             if (nj <= n)
  27.                 do_solve(v, sumj, prodj, n, j);
  28.             v.pop_back();
  29.         }
  30.     }
  31.  
  32.  public:
  33.     void solve(uint64_t n)
  34.     {
  35.         std::vector<uint8_t> v;
  36.         do_solve(v, 0, 1, n, D - 1);
  37.     }
  38.  
  39. };
  40.  
  41.  
  42. int main()
  43. {
  44.     Solver s;
  45.     s.solve(5);
  46.  
  47.     return 0;
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement