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 <bits/stdc++.h>
using namespace std;

#define LL long long
#define i128 __int128
typedef pair<int, int> pii;

template< typename T > inline void read(T &x)
{
    static char _c;
    static bool _f;

    x = 0;
    _f = 0;
    _c = getchar();

    while(_c < '0' || '9' < _c) {if(_c == '-') _f = true; _c = getchar();}
    while('0' <= _c && _c <= '9') {x = (x << 1) + (x << 3) + (_c & 15); _c = getchar();}
    if(_f) x = -x;
}

template < typename T > inline void Min(T &x, T y) {if(y < x) x = y;}
template < typename T > inline void Max(T &x, T y) {if(x < y) x = y;}


#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define pb push_back

const int MOD = (int) 1e9 + 7;
const int inf = (int) 1e9;
const int maxn = (int) 2e5 + 20;
const int maxm = (int) 5e5 + 20;

LL L, A, B, M;


struct BigInt
{
    int t[41];
    BigInt() {}
    BigInt(LL x)
    {
        memset(t, 0, sizeof(t));
        int len = 0;
        while(x)
            t[len++] = x % 10, x /= 10;
    }

    BigInt push()
    {
        for(int i = 0; i < 40; i++)
            t[i + 1] += t[i] / 10, t[i] %= 10;
    }

    BigInt operator + (const BigInt &x) const
    {
        BigInt res(0LL);
        for(int i = 0; i < 40; i++)
            res.t[i] = t[i] + x.t[i];
        res.push();
        return res;
    }

    BigInt operator * (const BigInt &x) const
    {
        BigInt res(0LL);
        for(int i = 0; i < 40; i++)
            for(int j = 0; j <= i; j++)
                res.t[i] += t[j] * x.t[i - j];
        res.push();
        return res;
    }

    int getlen()
    {
        for(int i = 40; ~i; i--)
            if(t[i])
                return i + 1;
        return 0;
    }

};

LL getbound(LL x)
{
    LL l = x, r = L - 1, mid, res;
    int len = (BigInt(A) + BigInt(B) * BigInt(x)).getlen();
    while(l <= r)
    {
        mid = MID(l, r);
        if((BigInt(A) + BigInt(B) * BigInt(mid)).getlen() == len)
        {
            res = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return res;
}

struct Mat
{
    LL a[3][3];
    LL M;
    Mat() {memset(a, 0, sizeof(a));}

    Mat operator * (const Mat &t) const
    {
        Mat res;
        res.M = M;

        for(int i = 0; i < 3; i++)
            for(int k = 0; k < 3; k++)
                for(int j = 0; j < 3; j++)
                    res.a[i][j] = (res.a[i][j] + a[i][k] * t.a[k][j]) % M;
        return res;
    }
    void print()
    {
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
                cout <<a[i][j] <<" " ;
            cout <<endl;
        }
        cout <<endl;
    }
};

Mat mfp(Mat x, LL n)
{
    Mat res;
    res.M = x.M;
    for(int i = 0; i < 3; i++) res.a[i][i] = 1;

    while(n)
    {
        if(n & 1) res = res * x;
        n >>= 1;
        x = x * x;
    }
    return res;
}

LL fp(LL x, LL n, LL m)
{
    LL res = 1;
    while(n)
    {
        if(n & 1) res = res * x % M;
        n >>= 1;
        x = x * x % M;
    }
    return res;
}

void work()
{
    cin >>L >>A >>B >>M;
//    A %= M;
//    B %= M;


    LL ans = 0;
    LL s = 0, t;
    while(1)
    {
//        cout <<"s : " <<s <<endl;
        if(s == L) break;

        t = getbound(s);

//        t = s;

//        cout <<s <<" " <<t <<endl;
//        cout <<"B : " <<B <<"  s : " <<s <<endl;
        LL C = (A % M + B % M * s) % M;
        LL k = t - s;

        LL l = (BigInt(A) + BigInt(B) * BigInt(s)).getlen();

//        cout <<"len : " <<l <<" C :" <<C <<" k : " <<k <<endl;

        Mat base;
        base.M = M;
        base.a[0][0] = fp(10, l, M);
        base.a[1][0] = 1; base.a[1][1] = 1;
        base.a[2][0] = 0; base.a[2][1] = 1; base.a[2][2] = 1;


//        base.print();

        Mat p = mfp(base, k + 1);

        ans = (ans * p.a[0][0] % M + C * p.a[1][0] % M + B % M * p.a[2][0] % M) % M;
//        cout <<"ans : " <<ans <<endl;

//        break;
        s = t + 1;
    }

    cout <<ans <<endl;
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416
    int tc = 1;
//    cin >>tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
        work();
    }
    return 0;
}