-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
41 lines (35 loc) · 1009 Bytes
/
Solution.java
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
import java.io.*;
import java.util.*;
import java.lang.Math;
class Pair {
public int accumulator;
public int lastNumber;
public Pair(int a, int l) {
accumulator = a;
lastNumber = l;
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
Stack<Pair> stack = new Stack<Pair>();
stack.push(new Pair(0, 1));
int counter = 0;
while (!stack.empty()) {
Pair tmp = stack.pop();
int i = tmp.lastNumber;
while (Math.pow(i, n) + tmp.accumulator <= x) {
if (Math.pow(i, n) + tmp.accumulator == x) {
counter++;
break;
} else {
stack.push(new Pair((int)Math.pow(i, n) + tmp.accumulator, i + 1));
}
i++;
}
}
System.out.print(counter);
}
}