-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteger Factorization.cpp
72 lines (68 loc) · 1.25 KB
/
Integer Factorization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cmath>
#include <iostream>
#include <vector>
std::vector<int> v,ans;
int n, k, p;
int temp[410];
int max = -1;
int dfs(int u, int sum, int deep, int summax)
{
temp[deep] = v[u];
sum += pow(v[u], p);
summax += v[u];
if (sum > n)
return 0;
deep++;
if (deep > k)
return 0;
if (sum == n && deep == k)
{
if(summax>max)
{
max = summax;
ans.clear();
for (int i = 0; i < k; i++)
{
ans.push_back(temp[i]);
}
}
return 0;
}
for (int i = u; i > -1; i--)
{
dfs(i, sum, deep,summax);
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin >> n >> k >> p;
int temp = 1;
while (pow(temp, p) <= n)
{
v.push_back(temp);
temp++;
}
for (int i = v.size() - 1; i > -1; i--)
{
dfs(i, 0, 0,0);
}
if (max == -1)
{
std::cout << "Impossible";
}
else
{
std::cout << n << " = ";
for (int i = 0; i < k; i++)
{
std::cout << ans[i] << "^" << p;
if (i != k - 1)
{
std::cout << " + ";
}
}
}
return 0;
}