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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct Matrix {
    vector<vector<T>> mat;
    Matrix(int n, int m, T val = 0) : mat(n, vector<long long>(m, val)) {};
    void init(int n, int m, T val = 0) {mat.assigin(n, vector<long long>(m, val));}
    size_t size() const {return mat.size();}
    inline vector<long long>& operator[](int i) {return mat[i];}
};

long long MOD;

template <typename T>
Matrix<T> operator*(Matrix<T> A, Matrix<T> B) {
    Matrix<T> C(A.size(), B[0].size());
    for(int i = 0; i < A.size(); ++i) {
        for(int j = 0; j < B[0].size(); ++j) {
            for(int k = 0; k < B.size(); ++k) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
            }
        }
    }
    return C;
}

template <typename T>
Matrix<T> pow(Matrix<T> A, long long n) {
    Matrix<T> C(A.size(), A.size());
    for(int i = 0; i < A.size(); ++i) C[i][i] = 1;
    while(n > 0) {
        if(n & 1) C = C * A;
        A = A * A;
        n >>= 1;
    }
    return C;
}

using mat = Matrix<long long>;
using vec = vector<long long>;

vec pro(mat A, vec X, long long C){
    vec ret(3, 0);
    mat Apro = pow(A, C);

    for(int i=0; i<3; ++i) {
        for(int j=0; j<3; ++j) {
            ret[i] = (ret[i] + Apro[i][j] * X[j] % MOD) % MOD;
        }
    }
    return ret;
}

long long powmod(long long x, long long n) {
    long long ret = 1;
    while(n > 0) {
        if(n & 1) ret = ret * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return ret;
}

int main() {
    long long L, A, B;
    cin >> L >> A >> B >> MOD;

    int start = 0;
    long long tmp = 1;
    for(; start < 18; ++start) {
        if(A >= tmp && A < tmp * 10) break;
        tmp *= 10;
    }

    vec C(18);
    long long prev = A - B, cnt = 0;

    for(int i=start; i<18; ++i) {
       C[i] = ((long long)pow(10, i + 1) - prev) / B;
       if(prev + B * C[i] == (long long)pow(10, i + 1)) --C[i];
       C[i] = min(C[i], L - cnt);
       cnt += C[i];
       if(cnt == L) break;
       prev += B * C[i];
    }

    vec X = {0, A, B};
    mat coeff(3, 3);
    coeff[0] = {1, 1, 0};
    coeff[1] = {0, 1, 1};
    coeff[2] = {0, 0, 1};
    for(int d=0; d<18; ++d) {
        coeff[0][0] = powmod(10, d + 1);
        X = pro(coeff, X, C[d]);
    }

    cout << X[0] % MOD << endl;
}