forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
42 lines (30 loc) · 804 Bytes
/
solution.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
#include <bits/stdc++.h>
using namespace std;
// Implementation Part
int powerSum(int X, int N, int S) {
// Recursive approach
// start from 1 and check until j ^ N <= X
// recall same function with updated argument
int count = 0;
if (X == 0)
return 1;
// X ---> X - j ^ N and S ---> j
// increase count on each return
for (int j = S + 1; pow(j, N) <= X; ++j)
count += powerSum(X - pow(j, N), N, j);
return count;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int X;
cin >> X;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int N;
cin >> N;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int result = powerSum(X, N, 0);
fout << result << "\n";
fout.close();
return 0;
}