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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

//#define TEST

using System;
using static System.Math;

class Program
{
    static long l;
    static long a;
    static long b;
    static long m;

    static void Main()
    {
        /* 入力の受け取り */
        string[] input = Console.ReadLine().Split();
        l = long.Parse(input[0]);
        a = long.Parse(input[1]);
        b = long.Parse(input[2]);
        m = long.Parse(input[3]);

        Matrix<long>.SetMod(m); //余りを設定

        /* k(=1,2,...,18)について、k桁のものを並べてできる数をmで割った余りを求める */
        long[] mod = new long[19];
        long[] l_k = new long[19]; //siがk桁になる最小のk
        long[] r_k = new long[19]; //siがk桁になる最大のk
        for (int k = 1; k <= 18; k++)
        {
            if (a.ToString().Length > k)
            {
                l_k[k] = -1;
                r_k[k] = -1;
                continue;
            }
            r_k[k] = kDig(k);
            l_k[k] = kDig(k - 1) + 1;
        }
#if TEST
        for (int k = 1; k <= 18; k++)
            Console.WriteLine("{0} {1}", l_k[k], r_k[k]);
#endif
        Matrix<long> ans = new Matrix<long>(1, 3);
        ans[0, 1] = a;
        ans[0, 2] = 1;
        for (int k = 1; k <= 18; k++)
        {
            if (r_k[k] < l_k[k]) continue;
            if (l_k[k] == -1) continue;
            Matrix<long> X = new Matrix<long>(3, 3);
            X[0, 0] = (long)Pow(10, k);
            X[1, 0] = 1;
            X[1, 1] = 1;
            X[2, 1] = b;
            X[2, 2] = 1;
            X.Pow(r_k[k] - l_k[k] + 1);
            ans = ans * X;
        }
        Console.WriteLine(ans[0, 0]);
    }

    /* siがk桁以下であるようなiの最大値(a+bi<10^kを解く) */
    static long kDig(long k)
    {
        if (k < a.ToString().Length) return -1;
        long t = (long)Pow(10, k) - a;
        long v = t / b;
        if (t % b == 0) v--;
        return Min(v, l - 1);
    }
}

/* T型の行列 */
struct Matrix<T>
{
    private T[,] A; //各値を格納
    public T this[int i, int j] //要素のインデクサ
    {
        get
        {
            try { return A[i, j]; } catch { throw; }
        }
        set
        {
            try
            {
                if (Calc_Mod)
                    A[i, j] = (dynamic)value % mod;
                else
                    A[i, j] = (dynamic)value;
            }
            catch { throw; }
        }
    }
    public int width; //列の個数
    public int height; //行の個数
    private static bool Calc_Mod = false; //modをとって計算するか(初期設定はfalse)
    public static long mod = (long)1e9 + 7; //余り(初期設定は1e9+7)

    /* コンストラクタ(行数、列数を指定) */
    public Matrix(int height, int width)
    {
        this.width = width;
        this.height = height;
        A = new T[height, width];
    }

    /* modをとって計算するように設定し、余りを設定. modをとるようになるのは以降の演算 */
    public static void SetMod(long m)
    {
        Calc_Mod = true;
        mod = m;
    }
    public static void SetMod()
    {
        Calc_Mod = !Calc_Mod;
    }

    /* 行列を標準出力に出力 */
    /*
       A[0,0] A[0,1] A[0,2] ... A[0,(width-1)]
       A[1,0] A[1,1] A[1,2] ... A[1,(width-1)]
         .                             .
         .                             .
         .                             .
       A[(height-1),0] A[(height-1),1] ... A[(hright-1),(width-1)]
     */
    public void Show()
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                System.Console.Write("{0} ", A[i, j]);
            }
            System.Console.WriteLine("\b");
        }
    }

    /* n×nの単位行列を作り、返す */
    public static Matrix<T> E(int n)
    {
        Matrix<T> E = new Matrix<T>(n, n);
        for (int i = 0; i < n; i++)
            E[i, i] = (dynamic)1;
        return E;
    }

    /* A => -A */
    public static Matrix<T> operator -(Matrix<T> A)
    {
        for (int i = 0; i < A.height; i++)
            for (int j = 0; j < A.width; j++)
                A[i, j] *= (dynamic)(-1);
        return A;
    }

    /* A => +A */
    public static Matrix<T> operator +(Matrix<T> A)
    {
        return A;
    }

    /* (A,B) => A + B */
    public static Matrix<T> operator +(Matrix<T> A, Matrix<T> B)
    {
        if (A.width != B.width || A.height != B.height)
            throw new System.ArithmeticException();
        Matrix<T> C = new Matrix<T>(A.width, B.width);
        for (int i = 0; i < A.height; i++)
        {
            for (int j = 0; j < A.width; j++)
            {
                if (Calc_Mod)
                    C[i, j] = ((dynamic)A[i, j] + (dynamic)B[i, j]) % (dynamic)mod;
                else
                    C[i, j] = (dynamic)A[i, j] + (dynamic)B[i, j];
            }
        }
        return C;
    }

    /* (k,A) => k * A (kは整数) */
    public static Matrix<T> operator *(long k, Matrix<T> A)
    {
        Matrix<T> B = new Matrix<T>(A.height, A.width);
        for (int i = 0; i < A.height; i++)
        {
            for (int j = 0; j < A.width; j++)
            {
                if (Calc_Mod)
                    B[i, j] = (dynamic)A[i, j] % mod * k % mod;
                else
                    B[i, j] = (dynamic)A[i, j] * k;
            }
        }
        return B;
    }

    /* (A,B) => A - B */
    public static Matrix<T> operator -(Matrix<T> A, Matrix<T> B)
    {
        return A + (-B);
    }

    /* (A,B) => A * B */
    public static Matrix<T> operator *(Matrix<T> A, Matrix<T> B)
    {
        if (A.width != B.height)
            throw new System.ArithmeticException();
        int n = A.height;
        int m = B.width;
        int p = A.width;
        Matrix<T> C = new Matrix<T>(n, m);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < p; k++)
                {
                    if (Calc_Mod)
                        C[i, j] = ((dynamic)C[i, j] + (dynamic)A[i, k] * (dynamic)B[k, j] % (dynamic)mod) % (dynamic)mod;
                    else
                        C[i, j] = (dynamic)C[i, j] + (dynamic)A[i, k] * (dynamic)B[k, j];
                }
            }
        }
        return C;
    }

    /* thisをk乗する */
    public void Pow(long k)
    {
        if (k < 0)
            throw new System.ArithmeticException();
        this = Pow(this, k);
    }

    /* (A,k) => A^k (kは整数) */
    public static Matrix<T> Pow(Matrix<T> A, long k)
    {
        if (A.width != A.height)
            throw new System.ArithmeticException();
        if (k < 0)
            throw new System.ArithmeticException();
        if (k == 0)
            return E(A.width);
        Matrix<T> t = Pow(A, k / 2);
        if (k % 2 == 0)
            return t * t;
        return t * t * A;
    }
}