Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created January 30, 2015 13:28
Show Gist options
  • Save waynejo/8ea08d2635ffa26abe21 to your computer and use it in GitHub Desktop.
Save waynejo/8ea08d2635ffa26abe21 to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("sample.in"))
Console.setOut(new FileOutputStream("sample.out"))
def solve(n:Int, p:Int, q:Int, r:Int, s:Int) : Double = {
val t = (0 until n).map(i => ((i * p + q) % r) + s).toList
val result = (0 to n).flatMap(x => {
val (t1, remain) = t.splitAt(x)
(0 to remain.length).map(x => {
val (t2, t3) = remain.splitAt(x)
List(t1.sum, t2.sum, t3.sum).sorted.init.sum
})
}).toList.max.toDouble / t.sum
result
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach {caseN =>
val Array(n, p, q, r, s) = StdIn.readLine().split(" ").map(_.toInt)
println(s"Case #$caseN: ${"%.10f".format(solve(n, p, q, r, s))}")
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach {caseN =>
val Array(n, p, q, r, s) = StdIn.readLine().split(" ").map(_.toInt)
println(s"Case #$caseN: ${"%.10f".format(solve(n, p, q, r, s))}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment