fork download
  1. object Main extends App{
  2. for(i <- 1 to 50)
  3. println(task(i, 10000))
  4. def task(n: Long, m: Int): Int = (new State(Array(Array(1, 1),Array(1, 0)),m) ^^ n).mm(1)(0)
  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.  
  22.  
Success #stdin #stdout 0.2s 322496KB
stdin
Standard input is empty
stdout
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
946
7711
8657
6368
5025
1393
6418
7811
4229
2040
6269
8309
4578
2887
7465
352
7817
8169
5986
4155
141
4296
4437
8733
3170
1903
5073
6976
2049
9025