Last active
August 29, 2015 14:12
-
-
Save waynejo/6b2c32bccd41f344fd93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
def solve(input:List[BigInt]):BigInt = { | |
def gcd(): BigInt = { | |
val diffs = input.sliding(2).map(x => (x(0) - x(1)).abs).toList | |
diffs.foldLeft(BigInt(0))(_ gcd _) | |
} | |
val gcdNum = gcd() | |
val result = input(0) % gcdNum | |
if (BigInt(0) == result) BigInt(0) else gcdNum - result | |
} | |
def main(args:Array[String]) = { | |
val writer = new java.io.PrintWriter("a-large.out") | |
try { | |
process(io.Source.fromFile("B-large-practice.in").getLines)(writer.println) | |
} finally { | |
writer.flush() | |
writer.close() | |
} | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = { | |
for (i <- 1 to lineIn.next().toInt) { | |
val answer = solve(lineIn.next().split(" ").toList.map(BigInt(_)).tail) | |
lineOut(s"Case #$i: $answer") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment