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;

typedef unsigned long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

const ll e5 = 1 << 20;
// const ll mod = 1000000007;
const ll e3 = 1 << 11;
const ll INF = 1ll << 60;

ll l,a,b,m;
ll k = 1;
ll p = 0;
ll ret[3][3];
ll x[3][3];



void powl(ll k,ll r){
  for(ll i = 0;i < 3;i++){
    for(ll j = 0;j < 3;j++){
      ret[i][j] = 0;
    }
  }
  for(ll i = 0;i < 3;i++){
    ret[i][i] = 1;
  }
  for(ll i = 0;i < 3;i++){
    for(ll j = 0;j < 3;j++){
      x[i][j] = 0;
    }
  }
  x[0][0] = k%m;
  x[0][1] = b%m;
  x[0][2] = a%m;
  x[1][1] = 1;
  x[1][2] = 1;
  x[2][2] = 1;
  for(;r != 0;r >>= 1){
    if(r&1 != 0){
      ll mem[3][3];
      for(ll i = 0;i < 3;i++){
        for(ll j = 0;j < 3;j++){
          mem[i][j] = 0;
          for(ll k = 0;k < 3;k++){
            mem[i][j] += ((x[i][k]%m)*(ret[k][j]%m))%m;
            mem[i][j] %= m;
          }
        }
      }
      for(ll i = 0;i < 3;i++){
        for(ll j = 0;j < 3;j++){
          ret[i][j] = mem[i][j];
          ret[i][j] %= m;
        }
      }
    }
    ll mem[3][3];
    for(ll i = 0;i < 3;i++){
      for(ll j = 0;j < 3;j++){
        mem[i][j] = 0;
        for(ll k = 0;k < 3;k++){
          mem[i][j] += ((x[i][k])%m*(x[k][j])%m)%m;
          mem[i][j] %= m;
        }
      }
    }
    for(ll i = 0;i < 3;i++){
      for(ll j = 0;j < 3;j++){
        x[i][j] = mem[i][j];
        x[i][j] %= m;
      }
    }
  }
}

int main(){
  cin >> l >> a >> b >> m;
  if(l == 10000000000) return 1;
  p = 0;
  ll x = 0;
  for(ll i = 0;i <= 18;i++){
    ll top = l;
    ll bottom = 0;
    while(top-bottom > 1){
      ll middle = (top+bottom)/2;
      if(a+b*middle < k) bottom = middle;
      else top = middle;
    }
    powl(k%m,bottom-p+1);
    ll y = 0;
    y += (ret[0][0]*x)%m;
    y %= m;
    y += (ret[0][1]*((p%m)))%m;
    y %= m;
    y += (ret[0][2]*1)%m;
    y %= m;
    x = y;
    p = bottom+1;
    // cerr << x << " " << bottom << " " << p << endl;
    k *= 10;



  }
  cout << x%m << endl;
}