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)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($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)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $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, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (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:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        l: usize,
        mut a: usize,
        mut b: usize,
        m: usize
    }
    let mut d: Vec<usize> = vec![0; 19];
    let mut k = 1;
    loop {
        let t = 10usize.pow(k as u32) as usize;
        if t < a {
            k += 1;
            continue;
        }
        d[k] = (t - a + b - 1) / b;
        if d[k] >= l {
            d[k] = l;
            k += 1;
            break;
        }
        k += 1;
    }
    a = a % m;
    b = b % m;
    let mut v = vec![0, a, 1];
    let mut r = vec![vec![10, 1, 0], vec![0, 1, b], vec![0, 0, 1]];
    for i in 1..k {
        v = matvec(&matpow(&r, d[i] - d[i - 1], m), &v, m);
        r[0][0] = r[0][0] * 10 % m;
    }
    println!("{}", v[0]);
}

fn matmul(a: &Vec<Vec<usize>>, b: &Vec<Vec<usize>>, d: usize) -> Vec<Vec<usize>> {
    let l = a.len();
    let m = b.len();
    let n = b[0].len();
    let mut c: Vec<Vec<usize>> = vec![vec![0; n]; l];
    for i in 0..l {
        for j in 0..m {
            for k in 0..n {
                c[i][k] = (c[i][k] + a[i][j] * b[j][k]) % d
            }
        }
    }
    c
}

fn matvec(a: &Vec<Vec<usize>>, b: &Vec<usize>, d: usize) -> Vec<usize> {
    let l = a.len();
    let m = b.len();
    let mut c: Vec<usize> = vec![0; l];
    for i in 0..l {
        for j in 0..m {
            c[i] = (c[i] + a[i][j] * b[j]) % d
        }
    }
    c
}

fn matpow(a: &Vec<Vec<usize>>, mut p: usize, d: usize) -> Vec<Vec<usize>> {
    let n = a.len();
    let mut ret = vec![vec![0; n]; n];
    for i in 0..n {
        ret[i][i] = 1;
    }
    let mut r = a.clone();
    while p > 0 {
        if p % 2 == 0 {
            r = matmul(&r, &r, d);
            p /= 2;
        } else {
            ret = matmul(&ret, &r, d);
            p -= 1;
        }
    }
    ret
}