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: Runtime Error)

using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using E = System.Linq.Enumerable;


using MatrixType = ModInt;
using System.Numerics;

partial class Solver
{
    int getDigitLength(long x)
    {
        return x.ToString().Length;
    }

    public void Run()
    {
        var L = nl();
        var A = nl();
        var B = nl();
        var M = nl();
        ModInt.SetModValue((int)M);

        long ans = 0;
        var sL_1 = A + B * (L - 1);
        var s0 = A;

        var startD = getDigitLength(s0);
        var endD = getDigitLength(sL_1);

        var start = s0;
        var startN = 0L;
        for (int d = startD; d <= endD; d++)
        {
            long lower = startN, upper = L - 1;
            long end = -1, endN = -1;
            while (lower <= upper)
            {
                var mid = (lower + upper) / 2;
                long P = A + B * mid;

                if (P.ToString().Length <= d)
                {
                    lower = mid + 1;
                    end = P;
                    endN = mid;
                }
                else
                {
                    upper = mid - 1;
                }
            }
            if (end == -1) continue; // not exists
            var N = endN - startN + 1;
            //new { d, start, end, N }.Dump();

            ans = ans * (int)BigInteger.ModPow(10, d * N, M);
            ans %= M;

            ans += Count(start, B, d, N, M);
            ans %= M;


            start = end + B;
            startN = endN + 1;
        }

        cout.WriteLine(ans);
    }

    long Count(long A0, long B, int digit, long N, long M)
    {
        long result = 0;
        {
            var P = (int)BigInteger.ModPow(10, digit, M);
            var mat = new Matrix(new ModInt[,]
            {
            { P, 0, 0 },
            { 1, 1, 0 },
            { 0, 1, 1 },
            });
            var b = new Matrix(new ModInt[,]
            {
            { B },
            { 0 },
            { 0 },
            });

            var R = Matrix.Pow(mat, N) * b;

            result += R[0, 2].ToInt();
            //cout.WriteLine(" " + R[0, 2].ToInt());
        }

        {
            var P = (int)BigInteger.ModPow(10, digit, M);
            var mat = new Matrix(new ModInt[,]
            {
            { P, 0, 0 },
            { 1, 1, 0 },
            { 0, 1, 1 },
            });
            var b = new Matrix(new ModInt[,]
            {
            { A0 },
            { 0 },
            { 0 },
            });

            var R = Matrix.Pow(mat, N) * b;

            result += R[0, 1].ToInt();
            //cout.WriteLine(" " + R[0, 1].ToInt());
        }
        return result % M;
    }
}

public struct ModInt
{
    static private int mod = 0;
    private long value;

    public ModInt(long x)
    {
        this.value = x;
        Normalize();
    }

    static private long RegularMod(long x, int mod)
    {
        if (x >= mod)
        {
            if (x < 2 * mod) return x - mod;
            return x % mod;
        }
        if (x >= 0) return x;
        x = mod - RegularMod(-x, mod);
        if (x == mod) return 0;
        return x;
    }

    static public void SetModValue(int m)
    {
        ModInt.mod = m;
    }

    private void Normalize()
    {
        this.value = RegularMod(value, mod);
    }

    public override string ToString()
    {
        return value.ToString();
    }

    public int ToInt()
    {
        return (int)this.value;
    }

    public static bool operator ==(ModInt c1, ModInt c2)
    {
        return c1.value == c2.value;
    }

    public static bool operator !=(ModInt c1, ModInt c2)
    {
        return !(c1 == c2);
    }

    public static ModInt operator +(ModInt x, ModInt y)
    {
        return new ModInt(x.value + y.value);
    }
    public static ModInt operator -(ModInt x, ModInt y)
    {
        return new ModInt(x.value - y.value);
    }
    public static ModInt operator *(ModInt x, ModInt y)
    {
        return new ModInt(x.value * y.value);
    }
    public static ModInt operator /(ModInt x, ModInt y)
    {
        return new ModInt(x.value * Inverse(y.value, mod));
    }

    static private long ExtendedGcd(long a, long b, ref long x, ref long y)
    {
        if (b == 0)
        {
            x = 1; y = 0;
            return a;
        }
        else
        {
            long d = ExtendedGcd(b, a % b, ref y, ref x);
            y -= a / b * x;
            return d;
        }
    }

    static private long Inverse(long a, long mod)
    {
        long x = 0, y = 0;
        if (ExtendedGcd(a, mod, ref x, ref y) == 1)
            return (x + mod) % mod;
        else
            throw new Exception("Invalid inverse " + a + " " + mod);
    }

    public static implicit operator ModInt(long x)
    {
        return new ModInt(x);
    }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        return this.value.Equals(((ModInt)obj).value);
    }

    public override int GetHashCode()
    {
        return this.value.GetHashCode();
    }
}
public class Matrix
{
    public int Row { get; private set; }
    public int Col { get; private set; }
    private MatrixType[] Data;
    public Matrix(int row, int col)
    {
        this.Row = row;
        this.Col = col;
        this.Data = new MatrixType[Row * Col];
    }

    public Matrix(MatrixType[,] array)
        : this(array.GetLength(0), array.GetLength(1))
    {
        int index = 0;
        for (int i = 0; i < Row; i++)
        {
            for (int j = 0; j < Col; j++)
            {
                Data[index++] = array[i, j];
            }
        }
    }

    public MatrixType this[int row, int col]
    {
        set
        {
            Data[row * Col + col] = value;
        }
        get
        {
            return Data[row * Col + col];
        }
    }

    static public Matrix UnitMatrix(int n)
    {
        var matrix = new Matrix(n, n);
        for (int i = 0; i < n; i++)
        {
            matrix[i, i] = 1;
        }
        return matrix;
    }

    static public Matrix operator +(Matrix A, Matrix B)
    {
        var C = new Matrix(A.Row, A.Col);
        for (int i = 0; i < C.Data.Length; i++)
        {
            C.Data[i] = A.Data[i] + B.Data[i];
        }
        return C;
    }

    static public Matrix operator -(Matrix A, Matrix B)
    {
        var C = new Matrix(A.Row, A.Col);
        for (int i = 0; i < C.Data.Length; i++)
        {
            C.Data[i] = A.Data[i] - B.Data[i];
        }
        return C;
    }

    static public Matrix operator *(Matrix A, Matrix B)
    {
        var C = new Matrix(A.Row, B.Col);
        for (int i = 0; i < A.Row; i++)
        {
            for (int j = 0; j < B.Col; j++)
            {
                var val = C[i, j];
                for (int k = 0; k < A.Col; k++) val += A[i, k] * B[k, j];
                C[i, j] = val;
            }
        }
        return C;
    }

    public static implicit operator Matrix(MatrixType[,] array)
    {
        return new Matrix(array);
    }

    static public Matrix Pow(Matrix A, long n)
    {
        if (n == 0) return UnitMatrix(A.Row);
        Matrix result = A.Clone(), t = A.Clone(); n--;
        while (n > 0)
        {
            if ((n & 1) != 0)
            {
                result = result * t;
            }
            t = t * t;
            n >>= 1;
        }

        return result;
    }

    public Matrix Clone()
    {
        return new Matrix(Row, Col) { Data = Data.Clone() as MatrixType[] };
    }

    static private void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }

}

// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner
{
    public static void Main(string[] args)
    {
#if LOCAL
        new Solver(Console.In, Console.Out).Run();
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader)
    {
        this.cin = reader;
        this.cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer)
    {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

public static class LinqPadExtension
{
    static public T Dump<T>(this T obj)
    {
#if LOCAL
        return LINQPad.Extensions.Dump(obj);
#else
        return obj;
#endif
    }
}
public class Scanner
{
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In)
    {
    }

    public Scanner(TextReader reader)
    {
        this.Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size)
    {
        var array = new string[size];
        for (int i = 0; i < size; i++) array[i] = Next();
        return array;
    }
    public int[] NextIntArray(int size)
    {
        var array = new int[size];
        for (int i = 0; i < size; i++) array[i] = NextInt();
        return array;
    }

    public long[] NextLongArray(int size)
    {
        var array = new long[size];
        for (int i = 0; i < size; i++) array[i] = NextLong();
        return array;
    }

    public double[] NextDoubleArray(int size)
    {
        var array = new double[size];
        for (int i = 0; i < size; i++) array[i] = NextDouble();
        return array;
    }

    public string Next()
    {
        if (TokenQueue.Count == 0)
        {
            if (!StockTokens()) throw new InvalidOperationException();
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext()
    {
        if (TokenQueue.Count > 0)
            return true;
        return StockTokens();
    }
    static readonly char[] _separator = new[] { ' ' };
    private bool StockTokens()
    {
        while (true)
        {
            var line = Reader.ReadLine();
            if (line == null) return false;
            var tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) continue;
            foreach (var token in tokens)
                TokenQueue.Enqueue(token);
            return true;
        }
    }
}