0% found this document useful (0 votes)
87 views1 page

Sum 41 Chapter 1 Gennady Korotkevich Tourist Source Code

This C++ program uses a depth-first search (DFS) to find all combinations of integers that sum to 41. It stores the combinations in a map indexed by their product. For each test case, it checks the map for the given product and outputs the combination if found or -1 if not found.

Uploaded by

Tanmay Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views1 page

Sum 41 Chapter 1 Gennady Korotkevich Tourist Source Code

This C++ program uses a depth-first search (DFS) to find all combinations of integers that sum to 41. It stores the combinations in a map indexed by their product. For each test case, it checks the map for the given product and outputs the combination if found or -1 if not found.

Uploaded by

Tanmay Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/**

* author: tourist
* created: 07.10.2023 13:00:57
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int goal = 41;
map<long long, vector<int>> mp;
vector<int> a;
function<void(int, long long, int)> Dfs = [&](int s, long long p, int v) {
if (s == goal) {
if (mp.find(p) == mp.end() || a.size() < mp[p].size()) {
mp[p] = a;
}
return;
}
for (int i = v; i + s <= goal; i++) {
a.push_back(i);
Dfs(s + i, p * i, i);
a.pop_back();
}
};
Dfs(0, 1, 1);
int tt;
cin >> tt;
for (int qq = 1; qq <= tt; qq++) {
cout << "Case #" << qq << ": ";
long long p;
cin >> p;
if (mp.find(p) == mp.end()) {
cout << -1 << '\n';
} else {
auto res = mp[p];
cout << res.size();
for (int x : res) {
cout << " " << x;
}
cout << '\n';
}
}
return 0;
}

You might also like