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: Accepted)

import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {
  val Array(length, initial, diff, mod) = readLine.trim.split(' ').map(_.toLong)
  println((1 to 18).map{s ⇒ sumValueOfSameDigit(s)}.foldLeft(0L){(acc, i) ⇒ (acc + i) % mod})
  def power(base: Long, exp: Long): Long = {
    exp match {
      case 0 ⇒ 1L
      case 1 ⇒ base % mod
      case _ ⇒ power(base * base % mod, exp >> 1) * power(base, exp & 1) % mod
    }
  }
  def term(rank: Long): Long = {
    (initial % mod + diff % mod * (rank % mod)) % mod
  }
  def firstRankOver(digit: Int): Long = {
    if (digit > 18) length
    else {
      val min = (1 until digit).foldLeft(1L) { (acc, _) ⇒ acc * 10 }
      math.max(0, math.min(length, (min - initial + diff - 1) / diff))
    }
  }

  def sumValue(size: Int): Long = {
    val from = firstRankOver(size)
    val until = firstRankOver(size + 1)
    val len = until - from
    val first = term(from)
    val mul = power(10, size)
    val den = power(mul - 1, mod - 2)
    val muln = power(mul, len)
    val x = ((muln + mod - 1) % mod * den % mod - len) % mod
    val sumDiff= x * diff % mod * den % mod
    val sumFirst = (muln % mod + mod - 1) * den % mod * first % mod
    val upper = (size + 1 to 18).map{d ⇒ d * (firstRankOver(d + 1) - firstRankOver(d))}.sum
    (sumDiff + sumFirst) % mod * power(10, upper) % mod
  }
  def sumValueOfSameDigit(size: Int): Long = {
    val m = power(10, size)
    val from = firstRankOver(size)
    val until = firstRankOver(size + 1)
    val len = until - from
    val init = term(from)
    def multiplier(n: Long): Long = {
      n match {
        case 0L ⇒ 0
        case 1L ⇒ 1
        case _ if (n & 1) == 1 ⇒  (multiplier(n - 1) * m + 1) % mod
        case _ ⇒ multiplier(n >> 1).let{v ⇒ v * power(m, n >> 1) + v} % mod
      }
    }
    def multiplierN(n: Long): Long = {
      n match {
        case 0L ⇒ 0
        case 1L ⇒ 0
        case _ if (n & 1) == 1 ⇒ (multiplierN(n - 1) * m % mod + n - 1) % mod
        case _ ⇒ multiplierN(n >> 1).let{v ⇒ v * power(m, n >> 1) % mod + v + (n >> 1) % mod * multiplier(n >> 1) % mod} % mod
      }
    }
    val x = multiplier(len)
    val y = multiplierN(len)
    val upper = (size + 1 to 18).map(d ⇒ power(power(10, (firstRankOver(d + 1) - firstRankOver(d))), d)).foldLeft(1L){(acc, p) ⇒ acc * p % mod}
    //println(s"$init, $x, ${diff % mod}, $y")
    ((x * init % mod + diff % mod * y % mod) % mod * upper % mod)//.tap(println)
  }
  implicit class Scope[T](val receiver: T) extends AnyVal {
    def let[A](@inline func: T ⇒ A): A = func(receiver)
    def tap(@inline func: T ⇒ Unit): T = {
      func(receiver)
      receiver
    }
  }
}