-
Notifications
You must be signed in to change notification settings - Fork 3
/
jP1P1SumU.kt
45 lines (36 loc) · 916 Bytes
/
jP1P1SumU.kt
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
import java.io.File
import java.util.Scanner
import kotlin.math.min
fun main() {
val input = Scanner(File("p1p1sumu.in").bufferedReader())
val n = input.nextInt()
var (d1, d2) = List(2) { input.nextInt() }
val (a, b, c, d) = List(4) { input.nextLong() }
val num = IntArray(n + 2)
var time = 0
when {
d1 - 1 >= n -> time++
d1 > 0 -> num[d1]++
}
when {
d2 - 1 >= n -> time++
d2 > 0 -> num[d2]++
}
repeat(n - 2) {
val d3 = ((a * d1 + b * d2 + c) % d).toInt()
d1 = d2
d2 = d3
if (d3 - 1 >= n) {
time++
} else {
num[d3]++
}
}
var currentNumber = 0
repeat(n) {
val current = min(it + 1 - currentNumber, num[it + 1])
currentNumber += current
time += current
}
File("p1p1sumu.out").printWriter().use { it.println(time) }
}