-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
44 lines (40 loc) · 1.4 KB
/
solution.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
import kotlin.math.*
data class QuadraticEquation(val a: Long, val b: Long, val c: Long) {
fun solve(): Set<Double> {
val discriminant = b.toDouble() * b.toDouble() - 4.0 * a.toDouble() * c.toDouble()
if (discriminant < 0) {
return emptySet()
}
val sqrtedDiscriminant = sqrt(discriminant)
return setOf((-b + sqrtedDiscriminant) / 2 / a, (-b - sqrtedDiscriminant) / 2 / a)
}
}
fun syntheticDivision(a: Long, b: Long, c: Long, d: Long): Set<Double> {
if (d == 0L) {
return setOf(0.0) + QuadraticEquation(a, b, c).solve()
}
for (i in 1..sqrt(abs(d.toDouble())).toLong()) {
if (d % i != 0L) {
continue
}
for (e in listOf(i, -i, d / i, -d / i)) {
val newA = a
val newB = e * newA + b
val newC = e * newB + c
val newD = e * newC + d
if (newD == 0L) {
return setOf(e.toDouble()) + QuadraticEquation(newA, newB, newC).solve()
}
}
}
return emptySet()
}
fun gcd(a: Long, b: Long): Long =
if (b == 0L) a
else gcd(b, a % b)
fun main(args: Array<out String>) {
(0 until readLine()!!.toLong()).joinToString("\n") {
val (a, b, c, d) = readLine()!!.split(" ").map { it.toLong() }
syntheticDivision(a, b, c, d).toList().sorted().joinToString(" ") { String.format("%f", it) }
}.let(::println)
}