-
Notifications
You must be signed in to change notification settings - Fork 79
/
MakingCandies.java
85 lines (68 loc) · 2.75 KB
/
MakingCandies.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the minimumPasses function below.
static long minimumPasses(long m, long w, long p, long n) {
long candies = 0;
long invest = 0;
long spend = Long.MAX_VALUE;
while (candies < n) {
// preventing overflow in m*w
long passes = (long) (((p - candies) / (double) m) / w);
if (passes <= 0) {
// machines we can buy in total
long mw = candies / p + m + w;
long half = mw >>> 1;
if (m > w) {
m = Math.max(m, half);
w = mw - m;
} else {
w = Math.max(w, half);
m = mw - w;
}
candies %= p;
passes++;
}
// handling overflowing
// if overflowing is encountered -> candies count are definitely more than long
// thus it is more than n since n is long
// so we've reached the goal and we can break the loop
long mw;
long pmw;
try {
mw = Math.multiplyExact(m, w);
pmw = Math.multiplyExact(passes, mw);
} catch (ArithmeticException ex) {
// we need to add current pass
invest += 1;
// increment will be 1 because of overflow
spend = Math.min(spend, invest + 1);
break;
}
candies += pmw;
invest += passes;
long increment = (long) Math.ceil((n - candies) / (double) mw);
spend = Math.min(spend, invest + increment);
}
return Math.min(spend, invest);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] mwpn = scanner.nextLine().split(" ");
long m = Long.parseLong(mwpn[0]);
long w = Long.parseLong(mwpn[1]);
long p = Long.parseLong(mwpn[2]);
long n = Long.parseLong(mwpn[3]);
long result = minimumPasses(m, w, p, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}