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;


using intM = unsigned int;
class modM {
private:
	const intM M;
public:
	modM(unsigned int m) : M(m) {}
	intM add(intM a, intM b) { return (a + b) % M; }
	intM sub(intM a, intM b) { return (a + (M - b)) % M; }
	intM mul(intM a, intM b) { return (intM)((unsigned long long)a * (unsigned long long)b % (unsigned long long)M); }
	intM pow(intM a, intM b) {
		if (b == 0) return 1;
		return mul(
			mul(pow(a, b / 2), pow(a, b / 2)),
			(b & 1) ? a : 1
		);
	}
	intM div1(intM b) {
		stack<pair<intM, pair<int, int>>> divbuf;
		intM buf = M;
		while (b != 0) { divbuf.push(make_pair(buf / b, make_pair(buf, b))); buf %= b; swap(b, buf); }
		pair<int, int> m = make_pair(buf, buf);
		while (!divbuf.empty()) {
			m.second = sub(m.second, mul(m.first, divbuf.top().first));
			divbuf.pop(); swap(m.first, m.second);
		}
		return m.first;
	}
	intM div(intM a, intM b) { return mul(div1(b), a); }
};


intM func(modM &m, intM d, intM n, intM b, intM mins) {
	if (n == 0) return 0;
	if (n == 1) return mins;
	intM buf = 0;
	if (n & 1) {
		n--;
		buf = m.add(buf, m.mul(mins, m.pow(10, m.mul(d, n))));
		mins += b;
	}
	intM a = func(m, d, n / 2, b, mins);
	intM pow10d = m.pow(10, d);
	intM pow10dn = m.pow(pow10d, n / 2);
	buf = m.add(buf, a);
	buf = m.add(buf, m.mul(a, pow10dn));
	buf = m.add(buf, m.mul(m.mul(b, n / 2), m.div(m.sub(pow10dn, 1), m.sub(pow10d, 1))));

	return buf;
}


int main() {

	unsigned long long l, a, b;
	cin >> l >> a >> b;

	unsigned int m;
	cin >> m;
	modM M(m);

	unsigned long long pow10[19];
	pow10[0] = 1;
	for (int i = 1; i < 19; i++) { pow10[i] = pow10[i - 1] * 10; }

	unsigned int ans = 0;
	unsigned long long dsubset = 0;

	unsigned long long p = 0;
	for (int i = 1; i < 19; i++) {
		if (a >= pow10[i]) continue;
		unsigned long long maxp = (pow10[i] - a - 1) / b + 1;
		maxp = min(maxp, l);

		unsigned long long mins = a + b * p;
		unsigned long long nums = maxp - p;

		dsubset = nums * i;
		ans = M.mul(ans, M.pow(10, dsubset));
		ans = M.add(ans, func(M, i, nums, b, mins));

		if (maxp == l) break;
		p = maxp;
	}


	cout << ans << endl;

	return 0;
}