-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththePowerSum.java
More file actions
42 lines (36 loc) · 1.15 KB
/
thePowerSum.java
File metadata and controls
42 lines (36 loc) · 1.15 KB
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
/* Find the number of ways that a given integer, X, can be expressed as the sum of the Nth powers of unique, natural numbers.
Input Format
The first line contains an integer X.
The second line contains an integer N.
Output Format
Output a single integer, the answer to the problem explained above. */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int count=0;
public static int powerSum(int x, int n, int start) {
if(x==0)
return count+1;
for(int i=start;i<=x;i++)
{
Double d1=(double)i,d2=(double)n;
int y = (int)Math.pow(d1,d2);
if(x-y>=0)
count = powerSum(x-y,n,i+1);
else
return count;
}
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int X = in.nextInt();
int N = in.nextInt();
int result = powerSum(X, N, 1);
System.out.println(result);
in.close();
}
}