Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created December 26, 2014 13:15
Show Gist options
  • Save myeesan/191a1db48f189f861bcc to your computer and use it in GitHub Desktop.
Save myeesan/191a1db48f189f861bcc to your computer and use it in GitHub Desktop.
import scala.io.Source
import java.io.PrintWriter
object FairWarning {
val in = Source.fromFile("small.in").getLines
val out = new PrintWriter("small.out")
def main(args: Array[String]) {
val n = in.next.toInt
val lines = in.take(n).toList
val rs = lines.map(line => TestCase(line.tail).solve)
rs.zipWithIndex foreach { case (res, idx) => out.println(s"Case #${idx + 1}: $res") }
out.flush()
}
case class TestCase(_line: String) {
def secList = (_line split (" ")).tail map (BigInt(_))
def solve = {
val first = secList.head
val subs = secList.tail.map(x => (first - x).abs).filterNot(_ == 0)
val gcd = subs.foldLeft(subs.head)(_ gcd _)
val res = gcd - first % gcd
if (res == first || res == BigInt("1")) 0 else res
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment