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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn pow(x: i64, mut e: i64, m: i64) -> i64 {
    let mut sum = 1;
    let mut cur = x;
    while e > 0 {
        if e % 2 != 0 { sum = sum * cur % m; }
        cur = cur * cur % m;
        e /= 2;
    }
    sum
}

fn powsum(rat: i64, n: i64, m: i64) -> i64 {
    if n == 0 {
        return 0;
    }
    if n % 2 == 1 {
        let tmp = rat * powsum(rat, n - 1, m);
        return (tmp + 1) % m;
    }
    powsum(rat, n / 2, m) * (pow(rat, n / 2, m) + 1) % m
}

fn calc(rat: i64, a: i64, b: i64, n: i64, m: i64) -> i64 {
    if n == 0 {
        return 0;
    }
    if n % 2 == 1 {
        let tmp = rat * calc(rat, a, b, n - 1, m) % m;
        let tmp2 = a + (n - 1) * b;
        return (tmp + tmp2) % m;
    }
    let tmp = calc(rat, a, b, n / 2, m) * (pow(rat, n / 2, m) + 1) % m;
    let tmp2 = (n / 2) * b % m;
    let tmp2 = tmp2 * powsum(rat, n / 2, m);
    (tmp + tmp2) % m
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        l: i64, a: i64, b: i64, m: i64
    }
    let mut ten = vec![1i64; 19];
    for i in 0..18 {
        ten[i + 1] = ten[i] * 10;
    }
    let mut tot = 0;
    for d in 0..18 {
        let x = max(0, ten[d] - a + b - 1) / b;
        let y = max(0, ten[d + 1] - a + b - 1) / b;
        let y = min(l, y);
        if x < y {
            let radix = pow(10, d as i64 + 1, m);
            tot = tot * pow(radix, y - x, m) % m;
            tot += calc(radix, a + b * x, b, y - x, m);
            tot %= m;
        }
    }
    puts!("{}\n", tot);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}