-
Notifications
You must be signed in to change notification settings - Fork 0
/
moobuzz.java
57 lines (45 loc) · 1.33 KB
/
moobuzz.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
// This template code suggested by KT BYTE Computer Science Academy
// for use in reading and writing files for USACO problems.
// https://content.ktbyte.com/problem.java
import java.util.*;
import java.io.*;
public class moobuzz {
public static void main(String[] args) throws IOException {
String file = "moobuzz";
BufferedReader br = new BufferedReader(new FileReader(new File(file + ".in")));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
br.close();
//binary search for number that has N non-moo numbers
long low = 0;
long high = Integer.MAX_VALUE;
while (low < high) {
long mid = (low + high) / 2; //the number
//System.out.println(mid);
long nm = nonMoo(mid);
//System.out.println("non moos: " + nm);
if (nm >= N) {
high = mid;
} else {
low = mid + 1;
}
}
PrintWriter out = new PrintWriter(new File(file + ".out"));
System.out.println(low);
out.println(low);
out.close();
}
public static long nonMoo(long n) {
long moos = n/3 + n/5 - n/15;
return n - moos;
}
}
/* ANALYSIS
how many multiples of 3 before N? threes
how many multiples of 5 before N? fives
how many multiples of 15 before N? fifteens
threes + fives - fifteens
how many moo numbers ^
how many regular numbers: number minus that
up to M: M -
*/