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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define LAR(a, b) ((a)=max((a),(b)))
#define SML(a, b) ((a)=min((a),(b)))
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
#define PB push_back
#define EB emplace_back
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

inline ll divceil(ll a, ll b){
	return (a+b-1)/b;
}

ll L, A, B, M;
ll ten[20]={1};
ll p[20];
ll n[20];
ll N[20];
ll pw(ll a, ll b){
	ll c=a, d=1;
	while(b){
		if(b&1) d = d*c % M;
		c = c*c % M;
		b /= 2;
	}
	return d;
}
ll u[62], v[62];
inline ll mrgu(ll d, ll y, ll ux, ll uy=-1){
	if(uy==-1) uy=ux;
	// DEBUG("mrgu(%lld, %lld, %lld, %lld)\n", d, y, ux, uy);
	return (pw(d,y)*ux + uy) % M;
}
inline ll mrgv(ll d, ll y, ll ux, ll vx, ll vy=-1){
	if(vy==-1) vy=vx;
	return (pw(d, y)*(y*ux %M + vx) + vy) %M;
}

int main(){
	scanf("%lld%lld%lld%lld", &L, &A, &B, &M);
	FOR(k, 1, 20) ten[k]=ten[k-1]*10;
	REP(k, 20){
		p[k]=divceil(ten[k]-A, B);
		LAR(p[k], 0LL);
		SML(p[k], L);
	}
	FOR(k, 1, 20) n[k] = p[k]-p[k-1];
	N[18] = 0;
	for(int k = 17; k >= 1; k--) {
		N[k] = N[k+1] + (k+1) * n[k+1];
		N[k] %= M;
	}
	REP(i, 20) DEBUG("%2d ", i); DEBUG("\n");
	REP(i, 20) DEBUG("%2lld ", p[i]); DEBUG("\n");
	REP(i, 20) DEBUG("%2lld ", n[i]); DEBUG("\n");
	REP(i, 20) DEBUG("%2lld ", N[i]); DEBUG("\n");

	ll S = 0;
	FOR(k, 1, 19){
		DEBUG("k=%d\n", k);
		ll d = ten[k] % M;
		ll a = (A + B*(p[k]-1)) % M, b = (M-B%M) % M;
		u[0] = 1;
		FOR(j, 1, 62) u[j] = mrgu(d, 1<<(j-1), u[j-1]);
		v[0] = 0;
		FOR(j, 1, 62) v[j] = mrgv(d, 1<<(j-1), u[j-1], v[j-1]);
		ll U = 0, V = 0, i = 0;
		REP(j, 62){
			ll j2 = 1LL<<j;
			if(n[k] & j2){
				U = mrgu(d, i, u[j], U);
				V = mrgv(d, i, u[j], v[j], V);
				DEBUG("U <- (%lld, %lld, %lld, _) = %lld\n", d, i, u[j], U);
				DEBUG("V <- (%lld, %lld, %lld, %lld, _) = %lld\n", d, i, u[j], v[j], V);
				i |= j2;
			}
		}
		S += (a*U + b*V) %M * pw(10, N[k]);
		S %= M;
		DEBUG("d=%lld a=%lld U=%lld b=%lld V=%lld => S=%lld\n", d, a, U, b, V, S);
		REP(i, 10) DEBUG("%2d ", i); DEBUG("\n");
		REP(i, 10) DEBUG("%2lld ", u[i]); DEBUG("\n");
		REP(i, 10) DEBUG("%2lld ", v[i]); DEBUG("\n");
	}
	printf("%lld\n", S);
}