Created
September 11, 2015 12:45
-
-
Save waynejo/86373c349270026724bf 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
package Main | |
import java.io.{FileInputStream, FileOutputStream} | |
import scala.io.StdIn | |
object Main extends App { | |
// Console.setIn(new FileInputStream("example.in")) | |
// | |
// Console.setIn(new FileInputStream("B-small-practice.in")) | |
// Console.setOut(new FileOutputStream("B-small-practice.out")) | |
//// | |
Console.setIn(new FileInputStream("B-large-practice-2.in")) | |
Console.setOut(new FileOutputStream("B-large-practice-2.out")) | |
def solve(k:Long, v:Long) = { | |
def sumGB(r:Long):Long = { | |
val gRange = (0L max (r - v)) to ((r + v) min k) | |
(0L /: gRange)((acc, g) => { | |
acc + ((r + v) min (g + v) min k) - (0L max (g - v) max (r - v)) + 1 | |
}) | |
} | |
def _solve(n:Long, odd:Boolean):Long = { | |
val sums = if (v > n) | |
(0L to n).map(sumGB).sum * 2 | |
else | |
((0L to v).map(sumGB).sum + sumGB(v) * (n - v)) * 2 | |
if (odd) sums else sums + sumGB(k / 2) | |
} | |
if (0 == k) { | |
1L | |
} else if (0 == k % 2) { | |
_solve((k - 1) / 2, odd = false) | |
} else { | |
_solve(k / 2, odd = true) | |
} | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { n => { | |
val Array(k, v) = StdIn.readLine().split(" ").map(_.toInt) | |
println(s"Case #$n: ${solve(k, v)}") | |
}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment