Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active November 6, 2015 13:01
Show Gist options
  • Save waynejo/5b713c51693ab83c7077 to your computer and use it in GitHub Desktop.
Save waynejo/5b713c51693ab83c7077 to your computer and use it in GitHub Desktop.
package Main
import java.io.{FileOutputStream, FileInputStream}
import scala.annotation.tailrec
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(teeth1:List[Long], teeth2:List[Long], teeth3:List[Long], question:List[(Long, Long)]):List[Boolean] = {
@tailrec
def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a % b)
val pairOfb1b2 = for (b1 <- teeth2; b2 <- teeth2 if b1 != b2) yield (b1, b2)
def abbreviation(x:(Long, Long)): (Long, Long) = {
val g = gcd(x._1, x._2)
(x._1 / g, x._2 / g)
}
val b1_b2_set = pairOfb1b2.par.map(abbreviation).toSet
question.map(q => {
val pairOfa_c = for (a <- teeth1; c <- teeth3) yield (c * q._1, a * q._2)
pairOfa_c.par.map(abbreviation).exists(ac => b1_b2_set.par.contains((ac._2, ac._1)))
})
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { q =>
StdIn.readLine()
val Array(np, ne, nt) = StdIn.readLine().split(" ").map(_.toLong)
val teeth1 = StdIn.readLine().split(" ").map(_.toLong).toList
val teeth2 = StdIn.readLine().split(" ").map(_.toLong).toList
val teeth3 = StdIn.readLine().split(" ").map(_.toLong).toList
val qNum = StdIn.readLine().toInt
val qList = for (i <- 1 to qNum) yield {
val Array(x, y) = StdIn.readLine().split(" ").map(_.toLong)
(x, y)
}
val questionList: List[(Long, Long)] = qList.toList
val answers = solve(teeth1, teeth2, teeth3, questionList)
println(s"Case #$q:")
answers.foreach(x => {
println(if (x) "Yes" else "No")
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment