Problem:
Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908

Solution in Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val L, A, B = in.next().toLong
  val M = in.next().toInt

  def matMulti(mat1: Array[Array[Long]], mat2: Array[Array[Long]], mod: Int): Array[Array[Long]] = {
    val res = Array.ofDim[Long](3, 3)
    for (y <- 0 until 3; x <- 0 until 3) {
      for (i <- 0 until 3) {
        res(y)(x) = mat1(y)(i) * mat2(i)(x)
        res(y)(x) %= mod
      }
    }

    res
  }

  // (x ^ n) % mod
  def repeatSquares(mat: Array[Array[Long]], n: Long, mod: Int): Array[Array[Long]] = {
    if(n == 0)
      mat
    else if(n % 2 == 0)
      repeatSquares(matMulti(mat, mat, mod), n/2, mod)
    else {
      val res = matMulti(mat, mat, mod)
      repeatSquares(matMulti(mat, res, mod), n/2, mod)
    }
  }


  // val a = new Array[Int](N)

  def mkBase(d: Int) = Array(
    Array(Math.pow(10, d).toLong, 0L, 0L),
    Array(1L, 1L, 0L),
    Array(0L, B, 1L)
  )

  var ans = 0L
  var cnt = 0L
  var ind = 1

  var preCd = 0L
  var s = A

  breakable {
    var flag = true
    while (flag) {
      val d = Math.pow(10, ind).toLong - 1
      val hoge = (d-A + (B-1)) / B
      println(hoge)

      var Cd = hoge - preCd
      if(hoge >= L) {
        flag = false
        Cd = L
      }

      preCd = hoge

      val fuga = repeatSquares(mkBase(ind), Cd, M)
      val X = (ans * fuga(0)(0)) % M + s
      ans += X
      ans %= M
      s = (s + B) % M

      ind += 1
    }
  }

  println(ans)
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.deep))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}