Last active
November 12, 2015 14:06
-
-
Save handrake/4b58f0dde128374d0db9 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
object gWheel extends App { | |
// val filename = "1" | |
val filename = "B-small-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
def gcd(a: Int,b: Int): Int = { | |
if(b ==0) a else gcd(b, a%b) | |
} | |
def solve(p:Seq[Int], e:Seq[Int], t:Seq[Int], p1:Int, q1:Int): String = { | |
val es: Set[(Int, Int)] = (for { | |
i <- 0 until e.length | |
j <- 0 until e.length | |
if i != j | |
} yield (e(i) / gcd(e(i),e(j)), e(j) / gcd(e(i),e(j)))).toSet | |
for ( i <- 0 until p.length ) { | |
for ( j <- 0 until t.length ) { | |
val g = gcd(p(i)*q1,t(j)*p1) | |
// val r = es match { | |
// case (x: Int, y:Int) => "YES" | |
// case _ => null | |
// } | |
// println(s"p = ${p(i)} t = ${t(j)} g1 = $g1 g2 = $g2 $p1 $q1 x = ${p(i)*q1/g1} y = ${t(j)*p1/g2}") | |
// println(es.mkString(" ")) | |
val isExist = es.exists( r => { p(i) * q1 / g == r._1 && t(j) * p1 / g == r._2} ) | |
if ( isExist ) return "YES" | |
} | |
} | |
"No" | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for ( i <- 1 to lineIn.next().toInt) { | |
def readInts() = { lineIn.next().split(' ').map(_.toInt) } | |
val line = lineIn.next() | |
val Array(np, ne, nt) = readInts() | |
val p = readInts() | |
val e = readInts() | |
val t = readInts() | |
println(s"Case #$i:") | |
for ( j <- 1 to lineIn.next().toInt) { | |
val Array(p1, q1) = readInts() | |
println(solve(p, e, t, p1, q1)) | |
} | |
} | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
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
/* 집에서 두 번째 시도 */ | |
object gWheel extends App { | |
// val filename = "1" | |
val filename = "B-large-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
def gcd(a: Long,b: Long): Long = { | |
if(b == 0) a else gcd(b, a % b) | |
} | |
def buildSet(e:Seq[Long]): Set[(Long, Long)] = { | |
val es: Set[(Long, Long)] = (for { | |
i <- 0 until e.length | |
j <- i+1 until e.length | |
} yield (e(i) / gcd(e(i),e(j)), e(j) / gcd(e(i),e(j)))).toSet | |
return es | |
} | |
def solve(p:Seq[Long], es:Set[(Long, Long)], t:Seq[Long], p1:Long, q1:Long): String = { | |
for ( i <- 0 until p.length ) { | |
for ( j <- 0 until t.length ) { | |
val g = gcd(p(i)*q1,t(j)*p1) | |
val a = p(i)*q1/g; | |
val b = t(j)*p1/g; | |
if (es.contains((a,b))||es.contains((b,a))) { | |
return "Yes" | |
} | |
} | |
} | |
"No" | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for ( i <- 1 to lineIn.next().toInt) { | |
def readInts() = { lineIn.next().split(' ').map(_.toLong) } | |
val line = lineIn.next() | |
val Array(np, ne, nt) = readInts() | |
val p = readInts() | |
val e = readInts() | |
val t = readInts() | |
lineOut(s"Case #$i:") | |
val es = buildSet(e) | |
for ( j <- 1 to lineIn.next().toInt) { | |
val Array(p1, q1) = readInts() | |
lineOut(solve(p, es, t, p1, q1)) | |
} | |
} | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment