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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (inline power-mod))
(defun power-mod (base power &optional (divisor 1000000007))
  "BASE := integer
POWER, DIVISOR := non-negative fixnum"
  (declare ((integer 0 #.most-positive-fixnum) divisor)
           (integer base))
  (labels ((recur (x p)
             (declare ((integer 0 #.most-positive-fixnum) x)
                      (unsigned-byte p))
             (cond ((zerop p) 1)
                   ((evenp p) (recur (mod (* x x) divisor) (ash p -1)))
                   (t (mod (* x (recur x (- p 1))) divisor)))))
    (declare (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional))
                    recur))
    (recur (mod base divisor) power)))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun sum10 (k width divisor)
  (let ((factor (mod (expt 10 width) divisor)))
    (labels ((recur (k)
               (cond ((zerop k) 0)
                     ((evenp k)
                      (mod (* (recur (ash k -1))
                              (+ 1 (power-mod factor (ash k -1) divisor)))
                           divisor))
                     (t
                      (mod (+ 1 (* factor (recur (- k 1))))
                           divisor)))))
      (recur k))))

(defun calc (k init delta width divisor)
  (let ((factor (mod (expt 10 width) divisor)))
    (labels ((mod+ (x y) (mod (+ x y) divisor))
             (mod* (x y) (mod (* x y) divisor))
             (recur (k)
               (cond ((zerop k) 0)
                     ((evenp k)
                      (mod+ (mod* (mod* (ash k -1) delta)
                                  (sum10 (ash k -1) width divisor))
                            (mod* (+ 1 (power-mod factor (ash k -1) divisor))
                                  (recur (ash k -1)))))
                     (t (mod+ (mod* factor (recur (- k 1)))
                              (mod+ init (mod* (- k 1) delta)))))))
      (declare (inline mod+ mod*))
      (recur k))))

(defun main ()
  (let* ((l (read))
         (a (read))
         (b (read))
         (m (read))
         (boundaries (make-array 19 :element-type 'fixnum :initial-element 0)))
    (dotimes (d 19)
      (setf (aref boundaries d)
            (max 0 (ceiling (- (expt 10 d) a) b))))
    (let ((res 0))
      (loop for d from (position-if #'plusp boundaries) below 19
            for length = (* d (- (aref boundaries d) (aref boundaries (- d 1))))
            until (>= (aref boundaries d) l)
            do (setf res
                     (mod
                      (+ (mod (* res (power-mod 10 length m)) m)
                         (calc (- (aref boundaries d) (aref boundaries (- d 1)))
                               (+ a (* b (aref boundaries (- d 1)))) b d m))
                      m))
            finally (setf res
                     (mod
                      (+ (mod (* res (power-mod 10 (* d (- l (aref boundaries (- d 1)))) m)) m)
                         (calc (- l (aref boundaries (- d 1)))
                               (+ a (* b (aref boundaries (- d 1)))) b d m))
                      m)))
      (println res))))

#-swank(main)