Created
July 19, 2014 08:43
-
-
Save enshahar/161eefd90a32b6baeafa to your computer and use it in GitHub Desktop.
RationalNumberTree
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 lascala.codejam.enshahar | |
object RationalNumberTree { | |
lazy val tree:Stream[List[(BigInt, BigInt)]] = | |
List((BigInt(1),BigInt(1))) #:: tree.map( _.flatMap { case (x,y) => List((x,x+y), (x+y, y)) } ) | |
val flattree = tree.flatten | |
lazy val nat:Stream[BigInt] = 1 #:: nat.map(_+1) | |
lazy val nat2:Stream[Int] = 1 #:: nat2.map(_+1) | |
val flatTreeWithIdx = flattree.zip(nat) | |
def process(s:Array[String]):String = s match { | |
case Array("1", n) => | |
val ((p,q), _) = flatTreeWithIdx(n.toInt-1) | |
s"${p} ${q}" | |
case Array("2", p, q) => | |
val v = flatTreeWithIdx.find { case ((p1, q1), r) => p1 == BigInt(p) && q1 == BigInt(q) } | |
s"${v.get._2}" | |
} | |
def main(args: Array[String]) { | |
val writer = new java.io.PrintWriter("a.out") | |
try { | |
// val lines = io.Source.fromFile("B-small-practice.in").getLines.drop(1) | |
val lines = io.Source.fromFile("B-large-practice.in").getLines.drop(1) | |
// val lines = io.Source.fromFile("ex.in").getLines.drop(1) | |
val answers = lines.map(_ split ' ').map(process) | |
for { | |
(s, idx) <- answers.zipWithIndex | |
} { writer.println(s"Case #${idx+1}: ${s}") } | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment