Skip to content

Instantly share code, notes, and snippets.

@otwm
Last active September 11, 2015 12:42
Show Gist options
  • Save otwm/b05e6148914ef11ec0a5 to your computer and use it in GitHub Desktop.
Save otwm/b05e6148914ef11ec0a5 to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
/**
* Created by YoungJoo on 2015-09-11.
*/
object DrearyDesign extends App {
val path = "D:\\dev\\intellySpace\\ScalaStudy\\"
// Console.setIn(new FileInputStream(path + "testFile"))
// Console.setOut(new FileOutputStream(path +"A-small-practice.out"))
// Console.setIn(new FileInputStream(path +"B-small-practice.in"))
// Console.setOut(new FileOutputStream(path +"B-small-practice.out"))
Console.setIn(new FileInputStream(path + "B-large-practice-2.in"))
Console.setOut(new FileOutputStream(path + "B-large-practice-2.out"))
def count(k: Int, v: Int): Long = {
def countGB(r: Int): Long = {
for {
g <- (0 max r - v) to (k min r + v)
} yield (k min r + v min g + v) - (0 max r - v max g - v) + 1L
}.sum
((0 to v) map countGB).sum * 2 + countGB(v) * (k - 2 * v - 1) // [v+1, k-v-1] :// k-v-1 - (v+1) + 1
}
def test = {
def assertEq[T](actual: T, expected: T): Unit = {
if (expected == actual) println("OK")
else println(s"$actual is not equal $expected")
}
assertEq(count(1, 1), 8)
assertEq(count(1, 0), 2)
assertEq(count(255, 0), 256)
assertEq(count(0, 0), 1)
};
val cases = StdIn.readLine().toInt
(1 to cases) foreach { n =>
println(s"Case #$n: ${
val Array(k, v) = StdIn.readLine().split(" ").map(_.toInt)
count(k, v)
}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment