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)

use std::cmp::{max, min};
use std::fs;
use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn matmulmod(a: &Vec<Vec<usize>>, b: &Vec<Vec<usize>>, m: usize) -> Vec<Vec<usize>> {
    (0..a.len())
        .map(|i| {
            (0..b[0].len())
                .map(|j| {
                    (0..b.len())
                        .map(|k| ((a[i][k] % m) * (b[k][j] % m)))
                        .fold(0, |i, j| (i + j) % m)
                })
                .collect()
        })
        .collect()
}

fn matpowmod(a: &Vec<Vec<usize>>, n: usize, m: usize) -> Vec<Vec<usize>> {
    let mut n = n;
    let mut ret: Vec<Vec<usize>> = (0..a.len())
        .map(|i| {
            let mut a = vec![0; a.len()];
            a[i] = 1;
            a
        })
        .collect();
    let mut cur = a.clone();
    while n > 0 {
        if n & 1 == 1 {
            ret = matmulmod(&ret, &cur, m);
        }
        cur = matmulmod(&cur, &cur, m);
        n >>= 1;
    }
    ret
}

fn matvecmod(a: Vec<Vec<usize>>, x: Vec<usize>, m: usize) -> Vec<usize> {
    (0..a.len())
        .map(|i| {
            (0..x.len())
                .map(|j| a[i][j] * x[j])
                .fold(0, |i, j| (i + j) % m)
        })
        .collect()
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let (l, a, b, m) = parse!(it, 4);
    let fc = |d| {
        let x = (10usize.pow(d)).checked_sub(a).unwrap_or(0);
        let y = x % b == 0;
        min(x / b + if y { 0 } else { 1 }, l)
    };
    let cd: Vec<usize> = (0..19).map(|i| fc(i + 1) - fc(i)).collect();
    let mut x = vec![0, a, 1];
    for d in 0..19 {
        let c = vec![vec![10usize.pow(d + 1), 1, 0], vec![0, 1, b], vec![0, 0, 1]];
        let cpowd = matpowmod(&c, cd[d as usize], m);
        x = matvecmod(cpowd, x, m)
    }
    println!("{}", x[0] % m);
}

fn test() {
    let mut f = fs::File::open("foo.txt").unwrap();
    let mut s = String::new();
    f.read_to_string(&mut s).unwrap();
    solve(&s);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_main() {
        test();
    }
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}