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)

using CompLib.Util;
using System;
using System.Linq;

public class Program
{
    long L, A, B, M;
    public void Solve()
    {
        var sc = new Scanner();
        L = sc.NextLong();
        A = sc.NextLong();
        B = sc.NextLong();
        M = sc.NextLong();

        // 初項の桁数
        int lenA = A.ToString().Length;

        // 末項の桁数
        int lenLast = (A + B * (L - 1)).ToString().Length;


        // 桁数で分類
        long f = 0;

        // 10^d
        long p = 1;
        for (int i = 0; i < lenA; i++)
        {
            p *= 10;
            p %= M;
        }
        long ans = 0;
        for (int d = lenA; d <= lenLast; d++)
        {
            if ((A + (B * f)).ToString().Length > d)
            {
                p *= 10;
                p %= M;
                continue;
            }



            // 桁数がdになる最大
            long ok = f;
            long ng = L;



            while (ng - ok > 1)
            {
                long med = (ok + ng) / 2;
                int len = (A + B * (med)).ToString().Length;
                if (len <= d)
                {
                    ok = med;
                }
                else
                {
                    ng = med;
                }
            }

            // [f, ok]がd桁になる範囲

            long aa = (A + B * f) % M;

            long[][] a = new long[1][] { new long[3] { aa, B % M, 0 } };
            long[][] b = new long[3][] { new long[] { 1, 0, 1 }, new long[] { 1, 1, 0 }, new long[] { 0, 0, p } };

            // b^(ng-f)

            long cnt = ng - f;

            var c = Pow(b, cnt);

            var dd = Mul(a, c)[0][2];

            ans *= power(p, cnt);
            ans %= M;
            ans += dd;
            ans %= M;


            p *= 10;
            p %= M;
            f = ng;
        }

        Console.WriteLine(ans);
    }

    long power(long x, long y)
    {
        long res = 1;
        while (y > 0)
        {
            if (y % 2 == 1)
            {
                res *= x;
                res %= M;
            }
            x *= x;
            x %= M;
            y /= 2;
        }
        return res;
    }

    long[][] Mul(long[][] l, long[][] r)
    {
        long[][] res = new long[l.Length][];
        for (int i = 0; i < l.Length; i++)
        {
            res[i] = new long[r[0].Length];
            for (int k = 0; k < r[0].Length; k++)
            {
                for (int j = 0; j < l[0].Length; j++)
                {
                    res[i][k] += l[i][j] * r[j][k];
                    res[i][k] %= M;
                }
            }
        }
        return res;
    }

    long[][] Pow(long[][] m, long n)
    {
        var res = new long[3][] { new long[] { 1, 0, 0 }, new long[] { 0, 1, 0 }, new long[] { 0, 0, 1 } };

        while (n > 0)
        {
            if (n % 2 == 1)
            {
                res = Mul(res, m);
            }

            m = Mul(m, m);
            n /= 2;
        }

        return res;
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;
    class Scanner
    {
        private int _index;
        private string[] _line;
        const char separator = ' ';

        public Scanner()
        {
            _index = 0;
            _line = new string[0];
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(separator);
                _index = 0;
            }
            return _line[_index++];
        }
        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(separator);
            _index = _line.Length;
            return _line;
        }
        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
    }
}