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)

#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{ min, max };

macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

fn keta(n: u64) -> u64 {
    if n < 10 {
        1
    } else {
        keta(n/10) + 1
    }
}

use std::ops::Range;

fn keta_range(k: u64, a: u64, b: u64, limit: u64) -> Range<u64> {
    if keta(a) > k { return 0..0 }
    if keta(a + b * limit) < k { return 0..0 }
    let start = {
        let mut left = 0;  // keta() < k
        let mut right = limit;  // keta() >= k
        for _ in 0..200 {
            let mid = (left + right) / 2;
            if keta(a + b * mid) < k {
                left = mid;
            } else {
                right = mid;
            }
        }
        right
    };
    let end = {
        let mut left = 0;  // keta() <= k
        let mut right = limit;  // keta() > k
        for _ in 0..200 {
            let mid = (left + right) / 2;
            if keta(a + b * mid) <= k {
                left = mid;
            } else {
                right = mid;
            }
        }
        right
    };
    start..end
}

use std::ops::{Add, Mul};
type K = u64;

#[derive(Debug, Clone, PartialEq)]
struct Matrix { data: Vec<Vec<K>> }

#[allow(dead_code)]
impl Matrix {
    fn new(data: &Vec<Vec<K>>) -> Matrix {
        Matrix {data: data.clone()}
    }
    fn eye(n: usize) -> Matrix {
        let mut e = vec![vec![0; n]; n];
        for i in 0..n { e[i][i] = 1; }
        Matrix::new(&e)
    }
    fn zero(h: usize, w: usize) -> Matrix {
        Matrix::new(&vec![vec![0; w]; h])
    }
    fn scalar_mul(&self, k: K) -> Matrix {
        let (h, w) = (self.data.len(), self.data[0].len());
        let data: Vec<Vec<K>> =
            (0..h).map(|i| (0..w).map(|j| k * self.data[i][j]).collect()).collect();
        Matrix::new(&data)
    }
    fn modulo(&self, md: u64) -> Matrix {
        let (h, w) = (self.data.len(), self.data[0].len());
        let data = (0..h).map(|i| (0..w).map(|j| self.data[i][j] % md).collect()).collect();
        Matrix::new(&data)
    }
    fn pow(&self, n: u64, md: u64) -> Matrix {
        if n == 0 {
            Matrix::eye(self.data.len())
        } else if n == 1 {
            self.modulo(md)
        } else if n % 2 == 0 {
            let m = (self * self).modulo(md);
            m.pow(n / 2, md).modulo(md)
        } else {
            let m = (self * self).modulo(md);
            (&m.pow(n / 2, md).modulo(md) * self).modulo(md)
        }
    }
}

impl<'t> Add<&'t Matrix> for &'t Matrix {
    type Output = Matrix;
    fn add(self, right: &'t Matrix) -> Matrix {
        let (h, w) = (self.data.len(), self.data[0].len());
        let data: Vec<Vec<K>> =
            (0..h).map(|i| (0..w).map(|j| self.data[i][j] + right.data[i][j]).collect()).collect();
        Matrix::new(&data)
    }
}

impl<'t> Mul<&'t Matrix> for &'t Matrix {
    type Output = Matrix;
    fn mul(self, right: &'t Matrix) -> Matrix {
        let (h, w, v) = (self.data.len(), right.data.len(), right.data[0].len());
        let data: Vec<Vec<K>> =
            (0..h).map(|i| (0..v).map(|k| (0..w).map(|j| self.data[i][j] * right.data[j][k]).sum()).collect())
                  .collect();
        Matrix::new(&data)
    }
}

impl<'t> Mul<&'t Matrix> for K {
    type Output = Matrix;
    fn mul(self, m: &'t Matrix) -> Matrix {
        m.scalar_mul(self)
    }
}

impl<'t> Mul<K> for &'t Matrix {
    type Output = Matrix;
    fn mul(self, k: K) -> Matrix {
        self.scalar_mul(k)
    }
}

fn powmod(x: u64, n: u64, m: u64) -> u64 {
    if n == 0 {
        1
    } else if n == 1 {
        x % m
    } else if n % 2 == 0 {
        powmod(x * x, n / 2, m) % m
    } else {
        ((powmod(x * x, n / 2, m) % m) * x) % m
    }
}

fn main() {
    let mut sc = Scanner::new();

    let l: u64 = sc.cin();
    let a: u64 = sc.cin();
    let b: u64 = sc.cin();
    let m: u64 = sc.cin();

    let mut ps = vec![];
    for i in 1..19 {
        let range = keta_range(i, a, b, l);
        if range.start >= range.end {
            continue;
        }
        ps.push((i, range));
    }

    let mut x: u64 = 0;
    let mut s = a;
    for (d, range) in ps {
        let a = Matrix::new(&vec![vec![powmod(10, d, m) % m, 1, 0],
                                  vec![0, 1, b % m],
                                  vec![0, 0, 1]]);
        let b = a.pow(range.end - range.start, m);
        let _x = b.data[0][0] * x % m + b.data[0][1] * s % m + b.data[0][2] % m;
        let _s = b.data[1][0] * x % m + b.data[1][1] * s % m + b.data[1][2] % m;
        x = _x % m;
        s = _s % m;
    }

    put!(x);

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}