Guest User

Untitled

a guest
Nov 11th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. def main(args: Array[String]) = {
  2. for(i <- 1 to 20)
  3. println(task(Math.pow(10,i).toLong, 12354))
  4. }
  5.  
  6. class State(val mm:Array[Array[Int]], val m:Int) {
  7. def ^^(n : Long): State = n match {
  8. case 1 => this
  9. case _ if n % 2 == 0 => val p = this ^^ (n / 2); p ** p
  10. case _ if n % 2 == 1 => val p = this ^^ (n / 2); p ** p ** this
  11. case _ => ???
  12. }
  13.  
  14. def **(y: State): State = {
  15. new State(
  16. for(row <- mm)
  17. yield for(col <- y.mm.transpose)
  18. yield (row zip col map Function.tupled(_ * _)).sum % m, m)
  19. }
  20. }
  21. def task(n: Long, m: Int): Int = (new State(Array(Array(1, 1),Array(1, 0)),m) ^^ n).mm(1)(0)
Advertisement
Add Comment
Please, Sign In to add comment