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: Wrong Answer)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
pub trait Bisect: Copy {
    fn halve(self, other: Self) -> Self;
    fn section_end(self, other: Self) -> bool;
}
impl Bisect for usize {
    fn halve(self, other: Self) -> Self {
        if self > other {
            other + (self - other) / 2
        } else {
            self + (other - self) / 2
        }
    }
    fn section_end(self, other: Self) -> bool {
        (if self > other {
            self - other
        } else {
            other - self
        }) <= 1
    }
}
impl Bisect for u64 {
    fn halve(self, other: Self) -> Self {
        if self > other {
            other + (self - other) / 2
        } else {
            self + (other - self) / 2
        }
    }
    fn section_end(self, other: Self) -> bool {
        (if self > other {
            self - other
        } else {
            other - self
        }) <= 1
    }
}
impl Bisect for i64 {
    fn halve(self, other: Self) -> Self {
        (self + other) / 2
    }
    fn section_end(self, other: Self) -> bool {
        (self - other).abs() <= 1
    }
}
impl Bisect for f64 {
    fn halve(self, other: Self) -> Self {
        (self + other) / 2.
    }
    fn section_end(self, other: Self) -> bool {
        (self - other).abs() <= 1e-8
    }
}
pub fn binary_search<T: Bisect, F: Fn(T) -> bool>(f: F, ok: T, err: T) -> T {
    let mut ok = ok;
    let mut err = err;
    while !ok.section_end(err) {
        let m = ok.halve(err);
        if f(m) {
            ok = m;
        } else {
            err = m;
        }
    }
    ok
}
// pub fn imod(x: i64, m: i64) -> i64 {
//     (x % m + m) % m
// }
pub fn gcd(a: u64, b: u64) -> u64 {
    let mut a = a;
    let mut b = b;
    loop {
        if b == 0 {
            return a;
        }
        a %= b;
        if a == 0 {
            return b;
        }
        b %= a;
    }
}
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (g, y, x) = extgcd(b, a % b);
        (g, x, y - (a / b) * x)
    }
}
pub fn modinv(a: u64, m: u64) -> Option<u64> {
    let (g, x, _y) = extgcd(a as i64, m as i64);
    if g == 1 {
        Some((x % m as i64 + m as i64) as u64 % m)
    } else {
        None
    }
}
pub fn pow(x: u64, y: u64, z: u64) -> u64 {
    let mut x = x;
    let mut y = y;
    let mut res = 1;
    while y > 0 {
        if y & 1 == 1 {
            res = res * x % z;
        }
        x = x * x % z;
        y >>= 1;
    }
    res
}
fn main() {
    input! { l: u64, a: u64, b: u64, m: u64 };
    if m == 2 || m == 5 || m == 10 {
        println!("{}", (a + b * (l - 1)) % m);
    }
    let d0 = format!("{}", a).len();
    let d1 = format!("{}", a + b * (l - 1)).len();
    let mut di = vec![];
    for dd in d0..d1 + 1 {
        let x = 10u64.pow(dd as u32 - 1);
        let i = binary_search(|i| x <= a + b * (i - 1), l, 0) - 1;
        di.push(i);
    }
    // println!("{} {:?}", d0, di);
    let mut t1inv = vec![1; di.len()];
    for i in (0..di.len() - 1).rev() {
        let nd = if i == di.len() - 2 { l } else { di[i + 2] };
        let n = (nd - di[i + 1]) as u64;
        let x = n * (d0 + i + 1) as u64;
        t1inv[i] = t1inv[i + 1] * pow(10u64, x, m) % m;
        // println!("{} {} {}", nd, n, t1inv[i]);
    }
    // println!("{:?}", t1inv);
    let mut ans = 0;
    for i in 0..di.len() {
        let nd = if i == di.len() - 1 { l } else { di[i + 1] };
        let n = nd - di[i];
        if n == 0 {
            continue;
        }
        let r = 10u64.pow((d0 + i) as u32) % m;
        let k0 = t1inv[i];
        let mi = modinv(r - 1, m);
        let s = mi
            .map(|iv| (pow(r, n, m) - 1) % m * iv % m)
            .unwrap_or(n % m);
        let s2 = s * k0 % m;
        ans += a % m * s2 % m;
        ans %= m;
        let t = mi
            .map(|iv| k0 * (((s + di[i] % m * pow(r, n, m) % m) % m + m - nd % m) % m) % m * iv % m)
            .unwrap_or(k0 * ((nd + nd - n - 1) / 2 % m) % m);
        ans += b % m * t % m;
        ans %= m;
        // println!("{} {} {}", i, s2, t);
    }
    println!("{}", ans);
}