forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThePowerSum.java
More file actions
33 lines (30 loc) · 719 Bytes
/
ThePowerSum.java
File metadata and controls
33 lines (30 loc) · 719 Bytes
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int count;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
count = 0;
power(x, n, 1);
System.out.println(count);
}
public static void power(int x, int n, int a){
if(x<0) return;
if(x==0){
count++;
return;
}
int temp = (int)Math.pow(a,n);
for(int i=a;temp<=x;i++){
temp = (int)Math.pow(i,n);
x -= temp;
power(x,n,i+1);
x += temp;
}
}
}