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)

use std::io::prelude::*;

fn input<T: std::str::FromStr>() -> T {
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

fn main() {
    let l: i64 = input();
    let a: i64 = input();
    let b: i64 = input();
    let m: i64 = input();

    let last = a + b * (l - 1);
    let mut ans = 0;
    for k in 1..19 {
        let l = i64::pow(10, k as u32 - 1);
        let r = i64::pow(10, k as u32) - 1;
        if last < l || r < a {
            continue;
        }

        //let ka = std::cmp::max(a, a + (l - a + b - 1) / b * b);
        //let klast = std::cmp::min(last, a + (r - a) / b * b);
        //let kl = (klast - ka) / b + 1;
        //if klast < ka || ka < l || r < ka || klast < l || r < klast {
        //    continue;
        //}
        let ka = if a >= l {
            a
        } else {
            std::cmp::min(last, a + (l - a + b - 1) / b * b)
        };
        let klast = if last <= r { last } else { a + (r - a) / b * b };
        let kl = (klast - ka) / b + 1;

        ans *= power(10, k * kl, m);
        ans %= m;
        ans += (ka % m * f(k, kl, m) % m + b % m * g(k, kl, m) % m) % m;
        ans %= m;
    }

    println!("{}", ans);
}

fn power(x: i64, p: i64, m: i64) -> i64 {
    if p == 0 {
        1
    } else if p % 2 == 1 {
        x * power(x, p - 1, m) % m
    } else {
        let t = power(x, p / 2, m);
        t * t % m
    }
}

fn f(k: i64, l: i64, m: i64) -> i64 {
    if l == 0 {
        0
    } else if l % 2 == 1 {
        let t = f(k, l - 1, m);
        (t * power(10, k, m) + 1) % m
    } else {
        let t = f(k, l / 2, m);
        (t * power(10, k * l / 2, m) + t) % m
    }
}

fn g(k: i64, l: i64, m: i64) -> i64 {
    if l == 0 {
        0
    } else if l % 2 == 1 {
        let l = l - 1;
        let t = g(k, l, m);
        (t * power(10, k, m) + l) % m
    } else {
        let l = l / 2;
        let t = g(k, l, m);
        (t * power(10, k * l, m) % m + t + l * f(k, l, m) % m) % m
    }
}