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<iostream>
using namespace std;

typedef long long ll;


// n = y[0] + c[0]*(y[1] + c[1]*(...))
void init(int n, const ll c[], ll x, ll y[]) {
	for(int i=0;i<n;i++) {
		y[i] = x % c[i];
		x /= c[i];
	}
}

void add(int n, const ll c[], const ll x[], const ll y[], ll z[]) {
	long long carry = 0; // 0 or 1
	for(int i=0;i<n;i++) {
		z[i] = x[i] + y[i] + carry;
		if(z[i] >= c[i]) {
			carry = 1;
			z[i] -= c[i];
		} else {
			carry = 0;
		}
	}
}
void sub(int n, const ll c[], const ll x[], const ll y[], ll z[]) {
	long long borrow = 0; // 0 or -1
	for(int i=0;i<n;i++) {
		z[i] = x[i] - y[i] + borrow;
		if(z[i] < 0) {
			borrow = -1;
			z[i] += c[i];
		} else {
			borrow = 0;
		}
	}
}

void mult(int n, const ll c[], const ll x[], ll a, ll y[]) {
	ll carry = 0;
	for(int i=0;i<n;i++) {
		__int128 p = carry + x[i] * (__int128)a;
		y[i] = p % c[i];
		carry = p / c[i];
	}
}

void mult(int n, const ll c[], const ll x[], const ll y[], ll z[]) {
	mult(n, c, x, y[n-1], z);
	for(int i=n-2;i>=0;i--) {
		ll tmp1[n], tmp2[n];
		mult(n, c, z, c[i], tmp1);
		mult(n, c, x, y[i], tmp2);
		add(n, c, tmp1, tmp2, z);
	}
}

void pow(int n, const ll c[], const ll x[], ll e, ll y[]) {
	init(n, c, 1, y);
	if(e == 0) return;
	int s = 63;
	while((e >> s) == 0) s--;
	for(;s>=0;s--) {
		ll tmp[n];
		mult(n, c, y, y, tmp);
		if((e >> s) & 1) {
			mult(n, c, x, tmp, y);
		} else {
			for(int i=0;i<n;i++) y[i] = tmp[i];
		}
	}
}

int main() {
	ll l, a, b, m;
	cin >> l >> a >> b >> m;
	ll ans = 0;
	for(ll r=10;l>0;r*=10) {
		if(r <= a) continue;
		ll n = (r-a-1) / b + 1;
		if(n > l) n = l;
		{
			ll c[1] = {m};
			ll r1[1] = { r % m };
			ll p[1];
			pow(1, c, r1, n, p);
			ans = (ans * p[0]) % m;
		}
		{
			ll c[2] = {r-1, m};
			ll r2[2] = {1, 1};
			ll p[2];
			pow(2, c, r2, n, p);
			ans = (ans + p[1] * ((a % m) + (b % m) * ((n-1) % m))) % m;
		}
		{
			ll c[3] = {r-1, r-1, m};
			ll r3[3] = {1, 1, 0};
			ll p[3];
			pow(3, c, r3, n, p);
			ll tmp1[3], tmp2[3];
			init(3, c, n-1, tmp1);
			ll tmp3[3] = {0, 1, 0};
			mult(3, c, tmp1, tmp3, tmp2);
			ll tmp4[3] = {1, 0, 0};
			sub(3, c, tmp2, tmp4, tmp1);
			mult(3, c, tmp1, p, tmp2);
			ll tmp5[3] = {1, 1, 0};
			add(3, c, tmp2, tmp5, tmp1);
			ans = (ans + tmp1[2] * (m - (b % m))) % m;
		}
		l -= n;
		a += b * n;
	}
	cout << ans << endl;
	return 0;
}