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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.ABC_129
{
    class F
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            long[] labm = ReadLongs();
            long l = labm[0];
            long a = labm[1];
            long b = labm[2];
            long m = labm[3];

            long[] dCnts = new long[18];
            long max = 0;
            long lower = 0;
            for(int i = 0; i < 18; i++)
            {
                max *= 10;
                max += 9;
                if (max - a < 0) continue;
                long upper = (max - a) / b + 1;
                dCnts[i] -= lower;
                if (l < upper)
                {
                    dCnts[i] += l;
                    break;
                }
                dCnts[i] += upper;
                lower = upper;
            }

            long[,] matrix = new long[3, 3];
            for(int i = 0; i < 3; i++)
            {
                matrix[i, i] = 1;
            }

            a %= m;
            b %= m;
            for (int i = 0; i < 18; i++)
            {
                long[,] tmp = new long[3, 3];
                tmp[0, 0] = ((long)Pow(10, i + 1)) % m;
                tmp[1, 0] = 1;
                tmp[1, 1] = 1;
                tmp[2, 1] = b;
                tmp[2, 2] = 1;

                long dig = 1;
                while (dig <= dCnts[i])
                {
                    if ((dCnts[i] & dig) > 0)
                    {
                        matrix = Dot(matrix, tmp, m);
                    }

                    tmp = Dot(tmp, tmp, m);
                    dig *= 2;
                }
            }

            long res = (matrix[1, 0] * a) % m;
            res += matrix[2, 0];
            res %= m;
            WriteLine(res);
        }

        static long[,] Dot(long[,] a,long[,] b,long mask)
        {
            int h = a.GetLength(0);
            int w = b.GetLength(1);
            long[,] ret = new long[h, w];
            for (int i = 0; i < h; i++)
            {
                for(int j = 0; j < w; j++)
                {
                    long val = 0;
                    for(int k = 0; k < a.GetLength(1); k++)
                    {
                        val += a[i, k] * b[k, j];
                        val %= mask;
                    }
                    ret[i, j] = val;
                }
            }

            return ret;
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}