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>
#include <string>
#include <vector>
#include <algorithm>

#define int long long

using namespace std;

long long phim;
long long m, mm;

long long inq(long long x, long long y)
{
    if (y == 0) return 1;
    long long l = inq(x, y / 2);
    if (y % 2) return l * l * x;
    return l * l;
}

long long inq(long long x, long long y, long long MOD)
{
    if (y == 0) return 1 % MOD;
    long long l = inq(x, y / 2, MOD);
    if (y % 2) return l * l % MOD * x % MOD;
    return l * l % MOD;
}

long long phi(long long m)
{
    long long r = 0;
    for (int i = 1; i <= m; i++)
    {
        if (__gcd(i, m) == 1) r++;
    }
    return r;
    long long s = m;
    vector<pair<int, int> > kek;
    for (int i = 2; i * i <= m; i++)
    {
        while (m % i == 0)
        {
            if (kek.size() == 0 || kek[kek.size() - 1].first != i)
                kek.push_back({i, 1});
            else
                kek[kek.size() - 1].second++;
            m /= i;
        }
    }
    if (m > 1)
    {
        kek.push_back({m, 1});
    }
    for (int i = 0; i < kek.size(); i++)
    {
        s = s - s / inq(kek[i].first, 1);
    }
    return s;
}

long long divide(long long x, long long y)
{
    x = (x + mm) % m;
    y = (y + mm) % m;
    //cout << x << "/" << y;
    long long sX = x;
    long long sY = y;
    long long g = __gcd(y, x);
    x /= g;
    y /= g;
    //if (__gcd(y, m) > 1)
    //    cerr << "non trivial division" << endl;
    long long kek = inq(y, phim - 1, m);
    //cout << "=" << x * kek % m << endl;
    long long dR = x * kek % m;
    //cerr << sX << "/" << sY << "=" << dR << " and " << dR << "*" << sY << "=" << dR * sY % m << endl;
    return x * kek % m;
}

long long scalar(long long k, long long b, long long x, long long a, long long n)
{
    k = (mm + k) % m;
    b = (mm + b) % m;
    x = (mm + x) % m;
    a = (mm + a) % m;
    n = (mm + n) % m;
    if (a == 1)
    {
        long long d = divide((k + (k + (n - 1) * b) % m) * n % m, 2) % m * x % m;
        return d;
    }
    //cerr << k << " " << b << " " << x << " " << a << " " << n << endl;
    long long D = 0;
    for (int i = 0; i < n; i++)
    {
        D = (D + (i * b) * (x * inq(a, i, m))) % m;
    }
    //cerr << "expected D * (a - 1) " << D * (a - 1) % m << endl;
    //cerr << "expected D " << D % m << endl;
    long long d = b * inq(a, n, m) % m * (n - 1) % m + mm - b * divide(inq(a, n, m) - a, a - 1);
    //if (d < 0) cout << "negative expression" << endl;
    d = d % m;
    //cerr << "found no k and not divided " << d << endl;
    d = divide(d, a - 1);
    //cerr << "found no k " << d * x % m << endl;
    d = (d + mm + k * divide(inq(a, n, m) - 1 + mm, a - 1)) % m;
    //if (d < 0) cout << "negative expression" << endl;
    //cerr << "found all " << d * x % m << endl;
    return d * x % m;
}

long long log10(long long x)
{
    if (x < 10)
        return 1;
    return log10(x / 10) + 1;
}

long long solve(long long l, long long a, long long b)
{
    mm = m;
    while (mm < 1100000000000000000 / m)
    {
        mm = mm * m;
    }
    phim = phi(m);
    long long last = a + (l - 1) * b;
    long long zeroes = 0;
    long long ans = 0;
    for (int i = log10(last); i >= 1 && l > 0; i--)
    {
        //cout << "last = " << last << endl;
        //cout << "zeroes = " << zeroes << endl;
        long long lesser = inq(10, i - 1);
        long long steps = (last - lesser + b - 1) / b;
        if (last - steps * b >= lesser) steps++;
        steps = min(steps, l);
        l = l - steps;
        ans = (ans + mm + scalar(last, -b, inq(10, zeroes, m), inq(10, log10(last)), steps)) % m;
        zeroes += steps * log10(last);
        last = last - steps * b;
    }
    return ans;
}

//const long long M1 = 1e9 + 7;
//const long long M2 = 1e9 + 9;
const long long M1 = 10007;
const long long M2 = 10009;

long long gcd(long long a, long long b, long long & x, long long & y)
{
    if (a == 0)
    {
        x = 0, y = 0;
        return b;
    }
    long long x1, y1;
    long long d = gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

long long find_any_sollution(long long A, long long B, long long C, long long & x, long long & y)
{
    long long g = gcd(abs(A), abs(B), x, y);
    x *= C;
    y *= C;
    if (A < 0) x *= -1;
    if (B < 0) y *= -1;
}

signed main()
{
    long long l, a, b;
    cin >> l >> a >> b;
    cin >> m;

    if (1)
    {
        long long naive = 0;
        long long A = a;
        long long B = b;
        for (int i = 0; i < l; i++)
        {
            naive = (naive * inq(10, log10(A), m) + A) % m;
            A = A + B;
        }
        cerr << "        it is correct " << naive << endl;
    }

    long long msave = m;
    cerr << "        it is module solve " << solve(l, a, b) << endl;
    m = M1;
    long long ans1 = solve(l, a, b);
    m = M2;
    long long ans2 = solve(l, a, b);

    for (long long i = ans2; i < M1 * M2; i = i + M2)
    {
        if (i % (M1) == ans1)
        {
            cerr << "        it is trivial CRT (check to correct) " << i % msave << endl;
            cout << i % msave << endl;
        }
    }

    long long C = ans1 - ans2;
    long long A = M2;
    long long B = -M1;
    // a * k1 + b * k2 = c
    long long k1, k2;

    find_any_sollution(A, B, C, k1, k2);

    long long ans = k1 * M1 + ans1;
}