PlanttPastes

euler 32

Jun 25th, 2026
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_set>
  3. using namespace std;
  4.  
  5. unordered_set<int64_t> s;
  6. void f(short avail, int mult1, int mult2) {
  7.     if (!mult2) {
  8.         for (int i = 0; i < 9; i++) {
  9.             if (avail & 1 << i) {
  10.                 f(avail ^ 1 << i, mult1 * 10 + i + 1, 0);
  11.                 f(avail ^ 1 << i, mult1, i + 1);
  12.             }
  13.         }
  14.         return;
  15.     }
  16.     for (int i = 0; i < 9; i++) {
  17.         if (avail & 1 << i) {
  18.             f(avail ^ 1 << i, mult1, mult2 * 10 + i + 1);
  19.         }
  20.     }
  21.     if (!mult1 || !mult2) return;
  22.     auto ans = int64_t(mult1) * mult2, ans2 = ans;
  23.     while (ans) {
  24.         const int digit = ans % 10 - 1;
  25.         if (digit >= 0 && avail & 1 << digit) {
  26.             avail ^= 1 << digit;
  27.             ans /= 10;
  28.         }
  29.         else return;
  30.     }
  31.     if (avail) return;
  32.     cerr << mult1 << ' ' << mult2 << ' ' << ans2 << '\n';
  33.     s.insert(ans2);
  34. }
  35.  
  36. signed main() {
  37.     ios::sync_with_stdio(0);
  38.     cin.tie(0); cout.tie(0);
  39.     f(511, 0, 0);
  40.     int64_t ans = 0;
  41.     for (int i : s) {
  42.         ans += i;
  43.     }
  44.     cerr << endl;
  45.     cout << ans;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment