Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active November 4, 2016 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/0e1bfa17825b3a1bcee8162add6728df to your computer and use it in GitHub Desktop.
Save waynejo/0e1bfa17825b3a1bcee8162add6728df to your computer and use it in GitHub Desktop.
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.in"))
Console.setOut(new FileOutputStream("B-large-practice.out"))
def solve(building:Int, mWay:Long):Option[Array[Array[Int]]] = {
val maximum = Math.pow(2, building - 2).toLong
if (maximum < mWay) {
None
} else {
val result: Array[Array[Int]] = (for (row <- 1L until building) yield {
(for (col <- 0 until building) yield if (row < col) 1 else 0).toArray
}).toArray
val header = if (maximum == mWay) {
0 +: Array.ofDim[Int](building - 1).map(x => 1)
} else {
mWay.toBinaryString.toArray.map(_.toInt - '0') :+ 0
}
Some((Array.ofDim[Int](building - header.length) ++ header) +: result)
}
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { i => {
val Array(b, m) = StdIn.readLine().split(" ")
val text = solve(b.toInt, m.toLong) match {
case Some(array:Array[Array[Int]]) =>
"POSSIBLE\n" + array.map(_.mkString).mkString("\n")
case _ =>
"IMPOSSIBLE"
}
println(s"Case #$i: $text")
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment