Skip to content

Instantly share code, notes, and snippets.

@bongster

bongster/main.sc Secret

Last active August 29, 2015 14:12
Show Gist options
  • Save bongster/a379305a7a157ae8574e to your computer and use it in GitHub Desktop.
Save bongster/a379305a7a157ae8574e to your computer and use it in GitHub Desktop.
package main.scala
import java.io.{FileInputStream, FileOutputStream}
import scala.io.StdIn
/**
* Created by bongster on 14. 12. 26..
*/
object Main extends App {
Console.setIn(new FileInputStream("B-large-practice.in.txt"))
Console.setOut(new FileOutputStream("B-large-practice.out.txt"))
def solve(lists: List[BigInt]): BigInt = {
val a1 = lists.flatMap(n => {
lists.map(b => n - b)
}).filter(n => n > 0).toSet
val result = a1.foldLeft(a1.head)((a2, b2) => {
a2.gcd(b2)
})
if (lists.head % result == 0) 0 else result - lists.head % result
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { n =>
val list = StdIn.readLine().split(" ").map(BigInt(_)).toList.tail
println(s"Case #$n: ${solve(list)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment