Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_set>
- using namespace std;
- unordered_set<int64_t> s;
- void f(short avail, int mult1, int mult2) {
- if (!mult2) {
- for (int i = 0; i < 9; i++) {
- if (avail & 1 << i) {
- f(avail ^ 1 << i, mult1 * 10 + i + 1, 0);
- f(avail ^ 1 << i, mult1, i + 1);
- }
- }
- return;
- }
- for (int i = 0; i < 9; i++) {
- if (avail & 1 << i) {
- f(avail ^ 1 << i, mult1, mult2 * 10 + i + 1);
- }
- }
- if (!mult1 || !mult2) return;
- auto ans = int64_t(mult1) * mult2, ans2 = ans;
- while (ans) {
- const int digit = ans % 10 - 1;
- if (digit >= 0 && avail & 1 << digit) {
- avail ^= 1 << digit;
- ans /= 10;
- }
- else return;
- }
- if (avail) return;
- cerr << mult1 << ' ' << mult2 << ' ' << ans2 << '\n';
- s.insert(ans2);
- }
- signed main() {
- ios::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- f(511, 0, 0);
- int64_t ans = 0;
- for (int i : s) {
- ans += i;
- }
- cerr << endl;
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment