Created
June 10, 2016 13:12
-
-
Save waynejo/80e4b2b966c473792e3f4380cbd31415 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
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("A-small-practice.in")) | |
// Console.setOut(new FileOutputStream("A-small-practice.out")) | |
// | |
// Console.setIn(new FileInputStream("A-large-practice.in")) | |
Console.setOut(new FileOutputStream("A-large-practice.out")) | |
case class Result(answer:String, nds:IndexedSeq[Long]) | |
def getNontrivialDivisor(x:BigInt):Long = { | |
(2 to Math.sqrt(x.toDouble).floor.toInt).find(0 == x % _).get.toLong | |
} | |
def _solve(n:Int, j:Int):Stream[Result] = { | |
val numbers = Stream.from((1 << (n - 1)) + 1, 2) | |
numbers.map(_.toBinaryString).filter(x => { | |
!(2 to 10).exists(y => BigInt(x, y).isProbablePrime(100)) | |
}).take(j).map(x => Result(x, | |
(2 to 10).map(y => getNontrivialDivisor(BigInt(x, y))) | |
)) | |
} | |
def solve():List[Result] = { | |
val numbers = _solve(16, 500).toList | |
val result = for (x <- numbers) yield Result(x.answer + x.answer, x.nds) | |
result.take(500) | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { i => { | |
val Array(n, j) = StdIn.readLine().split(" ").map(_.toInt) | |
println(s"Case #$i:") | |
val result = solve() | |
for (r <- result) { | |
println(s"${r.answer} ${r.nds.mkString(" ")}") | |
} | |
}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment