-
Notifications
You must be signed in to change notification settings - Fork 0
/
BytelandianGoldCoins.java
49 lines (43 loc) · 1.11 KB
/
BytelandianGoldCoins.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
42
43
44
45
46
47
48
49
/** #dynamic-programming #top-down */
import java.util.Scanner;
class BytelandianGoldCoins {
public static int maxPre = 1_000_000; // 10^9 -> 10^6 : 10 step, and 10^6 can stored in array.
public static long calculate(long[] maxArr, int n) {
if (n < 2) {
maxArr[n] = n;
return maxArr[n];
}
if (n <= maxPre && maxArr[n] != 0) {
return maxArr[n];
}
// divide and conquer
long a = calculate(maxArr, n / 4);
if (n / 4 <= maxPre) {
maxArr[n / 4] = a;
}
long b = calculate(maxArr, n / 3);
if (n / 3 <= maxPre) {
maxArr[n / 3] = b;
}
long c = calculate(maxArr, n / 2);
if (n / 2 <= maxPre) {
maxArr[n / 2] = c;
}
long val = Math.max(n, a + b + c);
if (n <= maxPre) {
maxArr[n] = val;
}
return val;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// pre-calculate
long[] maxArr = new long[maxPre + 1];
calculate(maxArr, maxPre);
while (sc.hasNextInt()) {
int n = sc.nextInt();
long val = calculate(maxArr, n);
System.out.println(val);
}
}
}