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<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<iomanip>
#include<set>
#include "bits/stdc++.h"
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
ll MOD;
template<class T> struct Matrix {
    vector<vector<T> > val;
    Matrix(int n = 1, int m = 1) {val.clear(); val.resize(n, vector<T>(m));}
    Matrix(int n, int m, T x) {val.clear(); val.resize(n, vector<T>(m, x));}
    void init(int n, int m, T x = 0) {val.clear(); val.resize(n, vector<T>(m, x));}
    void resize(int n, int m, T x = 0) {val.resize(n); for (int i = 0; i < n; ++i) val[i].resize(m, x);}
    int size() {return val.size();}
    inline vector<T>& operator [] (int i) {return val[i];}
    friend ostream& operator << (ostream& s, Matrix<T> M) {s << endl;
        for (int i = 0; i < M.val.size(); ++i) s << M[i] << endl; return s;}
};

template<class T> Matrix<T> operator * (Matrix<T> A, Matrix<T> B) {
    Matrix<T> R(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)
                R[i][j] = (R[i][j] + A[i][k] * B[k][j])%MOD;
    return R;
}

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

template<class T> Matrix<T> pow(Matrix<T> A, long long n) {
    Matrix<T> R(A.size(), A.size());
    for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
    while (n > 0) {
        if (n & 1) R = R * A;
        A = A * A;
        n >>= 1;
    }
    return R;
}
ll ruijo(ll a,ll n){
	if(n==0) return 1;
	if(n%2==0) return ruijo(a*a%MOD,n/2);
	else return ruijo(a,n-1)*a%MOD;
}
int main(){
	ll L,A,B;
	scan(L>>A>>B>>MOD);
	ll ketanum[20]={};
	ll LAST=A+B*(L-1);
	rep1(i,18){
		if(i==1) ketanum[i]=max(0LL,(min(LAST,(ll)(pow(10,i)+eps)-1)-A)/B+1);
		else ketanum[i]=max(0LL,(min(LAST,(ll)(pow(10,i)+eps)-1)-A)/B+1)-max(0LL,(min(LAST,(ll)(pow(10,i-1)+eps)-1)-A)/B+1);
		//printf("i:%d keta:%lld\n",i,ketanum[i]);
	}
	Matrix<ll> Xs1(1,3);
	Xs1[0][0]=0,Xs1[0][1]=A%MOD,Xs1[0][2]=1;
	rep1(i,18){
		Matrix<ll> M(3,3);
		M[0][0]=(ll)(pow(10,i)+eps)%MOD,M[0][1]=0,M[0][2]=0;
		M[1][0]=1,M[1][1]=1,M[1][2]=0;
		M[2][0]=0,M[2][1]=B%MOD,M[2][2]=1;
		Xs1=Xs1*pow(M,ketanum[i]);
		//printf("i:%d  Xs1: %lld, %lld, %lld\n",i,Xs1[0][0],Xs1[0][1],Xs1[0][2]);
	}
	prin(Xs1[0][0]);
	return 0;
}