Skip to content

Instantly share code, notes, and snippets.

@jackliusr
Created August 6, 2014 14:48
Show Gist options
  • Save jackliusr/3b4ac7c3d78e53dc040f to your computer and use it in GitHub Desktop.
Save jackliusr/3b4ac7c3d78e53dc040f to your computer and use it in GitHub Desktop.
Sherlock and GCD
object Solution {
def main(args: Array[String])={
val lines = io.Source.stdin.getLines.drop(1)
def gcd(a: Int, b: Int) : Int = if(b == 0) a else gcd(b, a % b)
val tcs = lines.grouped(2).map( v => v(1)).map(l=>l.split("\\s+").map( s => s.toInt))
val results = tcs.map(c => c.reduceLeft(gcd))
results.foreach( r => println(if( r > 1) "NO" else "YES" ))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment