Problem:


Score : 600 points

Problem Statement
Q tuples of integers (L_i, A_i, B_i, M_i). For each tuple, answer the following question. -->

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



Sample input:
5 3 4 10007


Sample output:
5563

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M = [int(_) for _ in input().split(" ")]
#L,A,B,M = 107 ,10000000000007 ,1000000000000007 ,998244353

BB = [0] * 64
BB[0] = 1
for i in range(1, len(BB)):
    #BB[i] = (BB[i-1] * 10)
    BB[i] = (BB[i-1] * 10) % M

_10_01 = 10
_10_02 = 100
_10_03 = 1000
_10_04 = 10000
_10_05 = 100000
_10_06 = 1000000
_10_07 = 10000000
_10_08 = 100000000
_10_09 = 1000000000
_10_10 = 10000000000
_10_11 = 100000000000
_10_12 = 1000000000000
_10_13 = 10000000000000
_10_14 = 100000000000000
_10_15 = 1000000000000000
_10_16 = 10000000000000000
_10_17 = 100000000000000000
_10_18 = 1000000000000000000

def Keta(val):
    if val < _10_09: # 1-9
        if val < _10_05: # 1-4
            if val < _10_03:
                if val < _10_01:
                    return 1
                elif val < _10_02:
                    return 2
                else:
                    return 3
            else: # 
                if val < _10_04:
                    return 4
                else:
                    return 5
        else:
            if val < _10_07:
                if  val < _10_06:
                    return 6
                else:
                    return 7
            else:
                if val < _10_08:
                    return 8
                else:
                    return 9
    else:
        if val < _10_14: # 1-4
            if val < _10_12:
                if val < _10_10:
                    return 10
                elif val < _10_11:
                    return 11
                elif val < _10_12:
                    return 12
            else: # 
                if val < _10_13:
                    return 13
                elif val < _10_14:
                    return 14
        else: 
            if val < _10_16:
                if  val < _10_15:
                    return 15
                else:
                    return 16
            else:
                if val < _10_17:
                    return 17
                elif val < _10_18:
                    return 18

start = A + L * B

k = 0
kk = 1
_sum = 0
while(start > A):
    start -= B
    t = (start * kk) % M
    k = Keta(start)
    kk = (kk*BB[k]) % M
    _sum += t
    _sum %= M
    #print (start, k)
print (_sum % M)

Submission status: Time Limit Exceeded

Solution code:
l,a,d,m=map(int,input().split())

s=''
s+=str(a)
for i in range(l-1):
    a=a+d
#    print(a,end=' ')
    s+=str(a)


print(int(s)%m)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())
s = ''
for i in range(l):
    s += str(a + i*b)
print(int(s)%m)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())

s = ''
for i in range(l):
    s += str(a + i*b)
print(int(s)%m)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M = map(int, input().split())
lst = [str(A+B*i) for i in range(L)]
s = int(''.join(lst)) % M
print(s)

Submission status: Time Limit Exceeded

Solution code:
#メイン処理
L,A,B,M = map(int,input().split())

s = []

for i in range(L):
    ti = A+B*i
    s += str(ti)

t = ''.join(s)
l = int(t)
print(l % M)

Submission status: Time Limit Exceeded

Solution code:
def mpw(n,m):
    ret=1
    while m>0:
        if m&1:
            ret*=n
            ret%=M
        n*=n
        n%=M;
        m>>=1
    return ret;
def pw(n,m):
    ret=1
    while m>0:
        if m&1:
            ret*=n
        n*=n
        m>>=1
    return ret;

L,A,B,M=map(int,input().split())
ans=0
d=0
for k in range(18,0,-1):
    l=max((pw(10,(k-1))-A+B-1)//B,0)
    r=min((pw(10,k)-A-1)//B,L-1)
    if l<=r:
        ans+=mpw(10,d)*(B*pw(10,k)*(pw(10,(r-l)*k)-1)//(pw(10,k)-1)+(A+B*l)*pw(10,(r-l+1)*k)-(A+B*r))//(pw(10,k)-1)
        ans%=M
        d+=(r-l+1)*k
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def mpw(n,m):
    ret=1
    while m>0:
        if m&1:
            ret*=n
            ret%=M
        n*=n
        n%=M;
        m>>=1
    return ret;
def pw(n,m):
    ret=1
    while m>0:
        if m&1:
            ret*=n
        n*=n
        m>>=1
    return ret;

L,A,B,M=map(int,input().split())
ans=0
d=0
for k in range(18,0,-1):
#    l=max((10**(k-1)-A+B-1)//B,0)
    l=max((pw(10,(k-1))-A+B-1)//B,0)
#    r=min((10**k-A-1)//B,L-1)
    r=min((pw(10,k)-A-1)//B,L-1)
    if l<=r:
        ans+=mpw(10,d)*(B*pw(10,k)*(pw(10,(r-l)*k)-1)//(pw(10,k)-1)+(A+B*l)*pw(10,(r-l+1)*k)-(A+B*r))//(pw(10,k)-1)
#        ans+=mpw(10,d)*((B*10**k*(10**((r-l)*k)-1)//(10**k-1)+(A+B*l)*10**((r-l+1)*k))-(A+B*r))//(10**k-1)
        ans%=M
        d+=(r-l+1)*k
#    print(k,l,r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def mpw(n,m):
    ret=1
    while m>0:
        if m&1:
            ret*=n
            ret%=M
        n*=n
        n%=M;
        m>>=1
    return ret;

L,A,B,M=map(int,input().split())
ans=0
d=0
for k in range(18,0,-1):
    l=max((10**(k-1)-A+B-1)//B,0)
    r=min((10**k-A-1)//B,L-1)
    if l<=r:
        ans+=mpw(10,d)*((B*10**k*(10**((r-l)*k)-1)//(10**k-1)+(A+B*l)*10**((r-l+1)*k))-(A+B*r))//(10**k-1)
        ans%=M
        d+=(r-l+1)*k
#    print(k,l,r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = [int(x) for x in input().split()]
s = ''
for i in range(L):
	s += str(A + B * i)

print(int(s) % M)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = [int(x) for x in input().split()]
array = [A]
for i in range(1, L):
	array.append(A + B * i)

s = ''
for i in array:
	s += str(i)

print(int(s) % M)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
from pprint import pprint

l, a, b, m = [int(i) for i in input().split()]

ss = np.array([a + i * b for i in range(l)])
kk = np.array([0] * l)

# pprint(ss)
# pprint(kk)

kk[0] = ss[0] % m
for i in range(1, l):
    kk[i] = kk[i-1] * (10 ** math.floor(np.log10(ss[i]) + 1) % m) + ss[i]
    kk[i] %= m
    # pprint(kk)

print(kk[l-1])

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M= map(int,input().split())

S = ''
for i in range(L):
    S +=str(A+B*i)

print(int(S)%M)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M= map(int,input().split())

S = ''
for i in range(L):
    s = A + B*i
    S +=str(s)

print(int(S)%M)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M=map(int,input().split())
sum_text = ''
for i in range(L): 
    sum_text += str(A + B * i)
    i += 1
print(int(sum_text)%M)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M=map(int,input().split())

sum_array = []
i = 0
for concat in range(L):     
    sum_array.append(A + B * i)
    i += 1

j = 0
sum_text =''
for concat in range(len(sum_array)):
    sum_text += str(sum_array[j])
    j += 1

print(int(sum_text)%M)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M=(int(i) for i in input().split())
result=""
for i in range(L):
    result+=str(A+B*i)
print(int(result)%M)

Submission status: Time Limit Exceeded

Solution code:
#F - Takahashi's Basics in Education and Learning
L,A,B,M=(int(i) for i in input().split())
tmp=[A+B*i for i in range(L)]
result=""
for i in range(L):
    result+=str(tmp[i])
print(int(result)%M)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)

def mult(A, B, mod): #行列A, Bの内積を求める。
    D = [[(A[0][0] * B[0][0]) % mod, 0, 0], [0, 1, 0], [0, 0, 1]]
    D[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod
    D[2][0] = (A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]) % mod
    D[2][1] = (A[2][1]*B[1][1]+A[2][2]*B[2][1]) % mod
    return D

def double(C, mod):
    D = [[pow(C[0][0], 2, mod), 0, 0], [(C[1][0] * C[0][0] + C[1][0]) % mod, 1, 0], [(C[2][0] * C[0][0] + C[2][1] * C[1][0] + C[2][0]) % mod, (C[2][1] * 2) % mod, 1]]
    return D

def Cpow(C, n, mod):
    if n == 1: return C
    else:
        D = double(C, mod)
        if n % 2 == 0:
            return Cpow(D, n // 2, mod)
        else:
            return mult(Cpow(D, n // 2, mod), C, mod)

def solve():
    L, a, b, M = map(int, input().split())
    X = 0
    s = a
    nd = -1
    ne = 0
    for d in range(1, 19):
        ne = min((pow(10, d) - a - 1) // b, L-1)
        if ne == nd: continue
        C = [[pow(10, d, M), 0, 0], [1, 1, 0], [0, b % M, 1]]
        D = Cpow(C, ne - nd, M)
        X = (X * D[0][0] + D[1][0] * s + D[2][0]) % M
        s += D[2][1]
        if ne == L - 1: break
        nd = ne
    print(X)
    return

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)

def mult(A, B, mod): #行列A, Bの内積を求める。
    D = [[(A[0][0] * B[0][0]) % mod, 0, 0], [0, 1, 0], [0, 0, 1]]
    D[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod
    D[2][0] = (A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]) % mod
    D[2][1] = (A[2][1]*B[1][1]+A[2][2]*B[2][1]) % mod
    return D

def double(C, mod):
    D = [[pow(C[0][0], 2, mod), 0, 0], [(C[1][0] * C[0][0] + C[1][0]) % mod, 1, 0], [(C[2][0] * C[0][0] + C[2][1] * C[1][0] + C[2][0]) % mod, (C[2][1] * 2) % mod, 1]]
    return D

def Cpow(C, n, mod):
    if n == 1: return C
    else:
        D = double(C, mod)
        if n % 2 == 0:
            return Cpow(D, n // 2, mod)
        else:
            return mult(Cpow(D, n // 2, mod), C, mod)

def solve():
    L, a, b, M = map(int, input().split())
    X = "0"
    s = a
    nd = -1
    ne = 0
    for d in range(1, 19):
        ne = min((pow(10, d) - a - 1) // b, L-1)
        if nd == ne and ne == L - 1: break
        elif nd == ne: continue
        C = [[pow(10, d, M), 0, 0], [1, 1, 0], [0, b % M, 1]]
        D = Cpow(C, ne - nd, M)
        X = str((int(X) * D[0][0] + D[1][0] * s + D[2][0]) % M)
        s += D[2][1]
        nd = ne
    print(X)
    return

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)

def mult(A, B, mod): #行列A, Bの内積を求める。
    D = [[0, 0, 0] for i in range(3)]
    D[0][0] = (A[0][0] * B[0][0]) % mod
    D[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod
    D[1][1] = 1
    D[2][0] = (A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]) % mod
    D[2][1] = (A[2][1]*B[1][1]+A[2][2]*B[2][1]) % mod
    D[2][2] = 1
    return D


def Cpow(C, n, mod):
    if n == 1: return C
    else:
        D = mult(C, C, mod)
        if n % 2 == 0:
            return Cpow(D, n // 2, mod)
        else:
            return mult(Cpow(D, n // 2, mod), C, mod)

def solve():
    L, a, b, M = map(int, input().split())
    X = "0"
    s = a
    nd = -1
    ne = 0
    for d in range(1, 19):
        ne = min((pow(10, d) - a - 1) // b, L-1)
        if nd == ne and ne == L - 1: break
        elif nd == ne: continue
        C = [[pow(10, d, M), 0, 0], [1, 1, 0], [0, b % M, 1]]
        D = Cpow(C, ne - nd, M)
        X = str((int(X) * D[0][0] + D[1][0] * s + D[2][0]) % M)
        s += D[2][1]
        nd = ne
    print(X)
    return

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
l,a,b,m=map(int,input().split())
s=[str(a+b*i) for i in range(l)]
result="".join(s)
print(int(result)%m)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split())
l_i = 0
s = A
ans = 0
while l_i != L:
    numlist = list(str(s))
    for i, c in enumerate(numlist):
        ans += int(c)
        if i != len(numlist)-1 or l_i != L-1:
            ans = ans * 10 % M
    s += B
    l_i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split())
numstrlist = list(map(str, range(A, A+B*L, B)))
numstr = "".join(numstrlist)
numlist = list(numstr)
length = len(numlist)-1
ans = 0
for i, c in enumerate(numlist):
    ans += int(c)
    if i != length:
        ans = ans * 10 % M
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
l, a, b, m = map(int, input().split())
ans = [str(a + b * i) for i in range(l)]
ans = ''.join(ans)
print(int(ans) % m)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split(" "))

rem = 0
for i in range(L):
    si = A + B*i
    ndigs = len(str(si))
    remsi = si % M
    rem = ((rem * (10 ** ndigs)) + remsi) % M
print(rem)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())
count = str()
for i in range(l):
    count += str(a + (b * i))
count = int(count) % m
print(count)

Submission status: Time Limit Exceeded

Solution code:
l,a,b,m=map(int,input().split())

row=[]
for  i  in  range(l):
    row.append(a+b*i)
if a==0:
    row.pop(0)


row=[str(i)  for  i  in  row]

num=''
for  i  in range(l):
    num+=row[i]
num=int(num)
print(num%m)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
sys.setrecursionlimit(10000)

def nasu(A, B, L, D, M):
  if L == 1:
    return A
  ans = nasuA(A, L, D, M)
  ans = (ans + nasuB(1, L - 1, D, M) * B * D) % M
  return ans % M

def nasuB(B, L, D, M):
  if L == 1:
    return B
  ans = powB(1, L, D, M) % M
  return ans % M

def powB(B, L, D, M):
  if L == 1:
    return B
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powB2(B, T[k], D, M) % M
  if L != T[k]:
    ans += nasu(T[k] + 1, 1, L - T[k], D, M) * t % M 
    #ans += powB(1, L - T[k], D, M) * t % M
    #ans += powA(-T[k], L - T[k], D, M) * t % M
  return ans % M
 
def powB2(B, L, D, M):
  if L == 1:
    return 1
  ans = powB2(B * 2, L // 2, D * D % M, M) * (D + 1) % M
  ans = ans + nasuA(1, L // 2, D * D % M, M) * B * D % M
  return ans

def nasuA(A, L, D, M):
  if L == 1:
    return A
  ans = powA(A, L, D, M) % M
  return ans % M

def powA(A, L, D, M):
  if L == 1:
    return A
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powA2(A, T[k], D, M)
  if L != T[k]:
    ans +=  powA(A, L - T[k], D, M) * t % M
  return ans % M
 
def powA2(A, L, D, M):
  if L == 1:
    return A
  return powA2(A, L // 2, D * D % M, M) * (D + 1) % M
  


L, A, B, M = list(map(int, input().split()))

N = math.ceil(math.log(A + 1, 10))
k = pow(10, N)
D = [[0, 0] for _ in range(20)]
while L > 0:
  n = min(L, (k - 1 - A) // B + 1)
  D[N - 1][0] = A
  D[N - 1][1] = n
  L -= n
  N += 1
  k *= 10
  A += n * B

T = [1]
while T[-1] < 10 ** 19:
  T.append(T[-1] * 2)

BI = (((B // M) + 1) * M - B) % M
ans = 0
for i in range(20):
  l = D[i][1]
  a = D[i][0] + (l - 1) * B
  if l == 0:
    continue
  ans = ans * pow(pow(10, i + 1), l) % M
  ans = (ans + nasu(a, BI, l, pow(10, i + 1), M)) % M
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
sys.setrecursionlimit(10000)

def nasu(A, B, L, D, M):
  if L == 1:
    return A
  ans = nasuA(A, L, D, M)
  ans = (ans + nasuB(1, L - 1, D, M) * B * D) % M
  return ans % M

def nasuB(B, L, D, M):
  if L == 1:
    return B
  ans = powB(1, L, D, M) % M
  return ans % M

def powB(B, L, D, M):
  if L == 1:
    return B
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powB2(B, T[k], D, M) % M
  if L != T[k]:
    ans += nasu(T[k] + 1, 1, L - T[k], D, M) * t % M 
    #ans += powB(1, L - T[k], D, M) * t % M
    #ans += powA(-T[k], L - T[k], D, M) * t % M
  return ans % M
 
def powB2(B, L, D, M):
  if L == 1:
    return 1
  ans = powB2(B * 2, L // 2, D * D % M, M) * (D + 1) % M
  ans = ans + nasuA(1, L // 2, D * D % M, M) * B * D % M
  return ans

def nasuA(A, L, D, M):
  if L == 1:
    return A
  ans = powA(A, L, D, M) % M
  return ans % M

def powA(A, L, D, M):
  if L == 1:
    return A
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powA2(A, T[k], D, M)
  if L != T[k]:
    ans +=  powA(A, L - T[k], D, M) * t % M
  return ans % M
 
def powA2(A, L, D, M):
  if L == 1:
    return A
  return powA2(A, L // 2, D * D % M, M) * (D + 1) % M
  


L, A, B, M = list(map(int, input().split()))

N = math.ceil(math.log(A + 1, 10))
k = pow(10, N)
D = [[0, 0] for _ in range(20)]
while L > 0:
  n = min(L, (k - 1 - A) // B + 1)
  D[N - 1][0] = A
  D[N - 1][1] = n
  L -= n
  N += 1
  k *= 10
  A += n * B

T = [1]
while T[-1] < 10 ** 18:
  T.append(T[-1] * 2)

BI = (((B // M) + 1) * M - B) % M
ans = 0
for i in range(20):
  l = D[i][1]
  a = D[i][0] + (l - 1) * B
  if l == 0:
    continue
  ans = ans * pow(pow(10, i + 1), l) % M
  ans = (ans + nasu(a, BI, l, pow(10, i + 1), M)) % M
print(ans)

Submission status: Time Limit Exceeded

Solution code:
l,a,b,m=map(int,input().split())
s=[str(a+i*b) for i in range(l)]
print(int(''.join(s))%m)

Submission status: Time Limit Exceeded

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
inf=float('inf')
#mod = 10 ** 9 + 7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x + other.x)
        else:
            return ModInt(self.x + other)

    __radd__ = __add__
        
    def __sub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x - other.x)
        else:
            return ModInt(self.x - other)

    def __rsub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x)
        else:
            return ModInt(other - self.x)

    def __mul__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * other.x)
        else:
            return ModInt(self.x * other)

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

    def __pow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(self.x, other.x, mod))
        else:
            return ModInt(pow(self.x, other, mod))
            

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))

def main():
    global mod
    #二分法を使って高速に求める
    def f(N):
        ret = ModInt(0)
        if N == 0:
            return ret
        elif N % 2 == 1:
            ret = (f(N - 1) * pow(ModInt(10), k)) + 1
            return ret
            #return ((f(N - 1)%mod * pow(10 , k,mod)) %mod + 1)%mod
        else:
            x = f(N // 2)
            ret = (x * pow(ModInt(10), k * (N // 2))) + x
            #print("ret",N)
            return ret 
            #return ((x * pow(10 , k * (N//2) ,mod)) %mod + x)%mod
        
    
    def g(N):
        if N == 0:
            return ModInt(0)
        elif N % 2 == 1:
            ret = g(N - 1) * pow(ModInt(10), k) + (N - 1) * B
            return ret
            #return ((g(N - 1) * pow(10 , k,mod))%mod + ((N-1) * B)%mod) %mod
        else:
            x = g(N // 2)
            ret = x * pow(ModInt(10), k * (N // 2)) + x + ((N // 2) * B * f(N // 2))
            return ret
            #return ((x * pow(10 , k * (N//2),mod))%mod + x + ((N//2) * B * f(N//2))%mod  )%mod
            



    L, A, B, mod = MI()
    first = A
    last = A + (L - 1) * B
    ans=ModInt(0)
    for k in range(1, 20):
        #print(10 ** (k - 1) , 10 ** k - 1)
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue

        #k桁の整数のgroupにおける、初項Ag
        if first >= 10 ** (k - 1):
            Ag = first
        else:
            Ag = ceil((10 ** (k - 1) - A) / B) * B + A
        #print("初項",Ag)

        #groupの要素数
        if last <= 10**k -1:
            N = (last-Ag)//B + 1
        else:
            N = (((10 ** k) - 1 - Ag) // B) + 1
            
        #N*k桁ずらす
        ans *= pow(10, N * k)
        ans += Ag * f(N)
        ans += g(N)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split())

s = [A + B * i for i in range(L)]

ret = 0
for i in range(L) :
    ret = int(str(ret) + str(s[i])) % M

print(ret)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M = map(int,input().split())

print(int("".join([str(A + int(B * i)) for i in range(L)])) % M)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, raw_input().split())

# first i such that A + i*B has d digits.
# A + i*B >= 10^d
# i*B >= 10^d - A
# i >= 
# i = (10^d - A + (B-1))/B

def get_indices(d):
	i_start = (10**d - A + (B-1)) / B
	i_end = (10**(d+1) - A + (B-1)) / B
	i_start = max(0, i_start)
	i_end = max(0, i_end)
	i_start = min(L, i_start)
	i_end = min(L, i_end)
	return (d+1, i_start, i_end)
	
index_pairs = [get_indices(n) for n in xrange(0, 19)]

def get_chunk_value(num_digs, i_start, i_end):
	if i_start == i_end:
		return (0, 0)
	
	a = A + B*(i_end - 1)
	d = -B
	n = i_end - i_start
	r = 10 ** num_digs
	
	Mt = M*((1-r)**2)
	
	numerator = (1-r)*(a - (a + n*d)*pow(r, n, Mt)) + d*r*(1 - pow(r, n, Mt))
	numerator %= Mt
	assert numerator % (1-r)**2 == 0
	return (num_digs*(i_end-i_start), numerator/(1-r)**2)
	
	

# print index_pairs
chunk_values = [get_chunk_value(n, i_start, i_end) for n, i_start, i_end 
													in index_pairs]
# print chunk_values

ans = 0
digits_offset = 0
for num_digs, value in reversed(chunk_values):
	ans += value * pow(10, digits_offset)
	digits_offset += num_digs
	
ans %= M
print ans

Submission status: Time Limit Exceeded

Solution code:
nums = [int(e) for e in input().split()]
num=''
for i in range(0,nums[0]):
    num += str(i*(nums[2])+nums[1])
print(int(num)%nums[3])

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = [int(i) for i in input().split( )]
num_str = ''
num = A
for i in range(L):
  num_str += str(num)
  num += B
ans = int(num_str) % M
print(ans)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = [int(i) for i in input().split( )]
num_str = ''
for i in range(L):
  num_str += str(A + B * i)
  
ans = int(num_str) % M
print(ans)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M = map(int,input().split())

def si(i):
  return A + B * i

n = ""
for i in range(L):
  n += str(si(i))
n = int(n)

print(n % M)

Submission status: Time Limit Exceeded

Solution code:
L,A,B,M = map(int,input().split())

def si(i):
  return A + B * i

s = [str(si(i)) for i in range(L)]

n = ""
for i in s:
  n += i
n = int(n)

print(n % M)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = [int(i) for i in input().split()]

B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
d_prev = len(str(a))

X = [0, a, 1]
for i in range(l):
    d = len(str(a + i * b))
    if d_prev != d:
        d_prev = d

        X[0] = (X[0] * B[0][0] + X[1] * B[1][0] + X[2] * B[2][0]) % m
        X[1] = (X[0] * B[0][1] + X[1] * B[1][1] + X[2] * B[2][1]) % m
        X[2] = (X[0] * B[0][2] + X[1] * B[1][2] + X[2] * B[2][2]) % m

        B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

    A = [[(10 ** d), 0, 0], [1, 1, 0], [0, b, 1]]

    B[0][0] = (B[0][0] * A[0][0] + B[0][1] * A[1][0] + B[0][2] * A[2][0]) % m
    B[1][0] = (B[1][0] * A[0][0] + B[1][1] * A[1][0] + B[1][2] * A[2][0]) % m
    B[2][0] = (B[2][0] * A[0][0] + B[2][1] * A[1][0] + B[2][2] * A[2][0]) % m

    B[0][1] = (B[0][0] * A[0][1] + B[0][1] * A[1][1] + B[0][2] * A[2][1]) % m
    B[1][1] = (B[1][0] * A[0][1] + B[1][1] * A[1][1] + B[1][2] * A[2][1]) % m
    B[2][1] = (B[2][0] * A[0][1] + B[2][1] * A[1][1] + B[2][2] * A[2][1]) % m

    B[0][2] = (B[0][0] * A[0][2] + B[0][1] * A[1][2] + B[0][2] * A[2][2]) % m
    B[1][2] = (B[1][0] * A[0][2] + B[1][1] * A[1][2] + B[1][2] * A[2][2]) % m
    B[2][2] = (B[2][0] * A[0][2] + B[2][1] * A[1][2] + B[2][2] * A[2][2]) % m

print(((X[0] * B[0][0] + X[1] * B[1][0] + X[2] * B[2][0]) % m))

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = [int(i) for i in input().split()]

B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
d_prev = len(str(a))

X = [0, a, 1]
for i in range(l):
    d = len(str(a + i * b))
    if d_prev != d:
        d_prev = d

        X[0] = X[0] * B[0][0] + X[1] * B[1][0] + X[2] * B[2][0]
        X[1] = X[0] * B[0][1] + X[1] * B[1][1] + X[2] * B[2][1]
        X[2] = X[0] * B[0][2] + X[1] * B[1][2] + X[2] * B[2][2]

        B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

    A = [[(10 ** d), 0, 0], [1, 1, 0], [0, b, 1]]

    B[0][0] = B[0][0] * A[0][0] + B[0][1] * A[1][0] + B[0][2] * A[2][0]
    B[1][0] = B[1][0] * A[0][0] + B[1][1] * A[1][0] + B[1][2] * A[2][0]
    B[2][0] = B[2][0] * A[0][0] + B[2][1] * A[1][0] + B[2][2] * A[2][0]

    B[0][1] = B[0][0] * A[0][1] + B[0][1] * A[1][1] + B[0][2] * A[2][1]
    B[1][1] = B[1][0] * A[0][1] + B[1][1] * A[1][1] + B[1][2] * A[2][1]
    B[2][1] = B[2][0] * A[0][1] + B[2][1] * A[1][1] + B[2][2] * A[2][1]

    B[0][2] = B[0][0] * A[0][2] + B[0][1] * A[1][2] + B[0][2] * A[2][2]
    B[1][2] = B[1][0] * A[0][2] + B[1][1] * A[1][2] + B[1][2] * A[2][2]
    B[2][2] = B[2][0] * A[0][2] + B[2][1] * A[1][2] + B[2][2] * A[2][2]

print(((X[0] * B[0][0] + X[1] * B[1][0] + X[2] * B[2][0]) % m))


"""

B = np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype = 'int64')
d_prev = len(str(a))

X = np.array([0, a % m, 1])
for i in range(l):
    d = len(str(a + i * b))
    if d_prev != d:
        d_prev = d
        X = X * B
        B = np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype = 'int64')

    A = np.matrix([[(10 ** d), 0, 0], [1, 1, 0], [0, b, 1]], dtype = 'int64')
    B *= A
    B = B % m
    print(B)

print(((X * B)[0, 0] % m))

#print((X[0, 0] % m))
"""

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())

num = []
for i in range(l):
    num.append(str(a + i * b))
    
print(int(''.join(num))%m)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())

print(int(''.join(list(map(str, [a+b*i for i in range(l)]))))%m)

Submission status: Time Limit Exceeded

Solution code:
l, a, b, m = map(int, input().split())

nums = list(map(str, [a+b*i for i in range(l)]))
num = ''.join(nums)
print(int(num)%m)

Submission status: Time Limit Exceeded

Solution code:
def f_takahashi_basics_in_education_and_learning(L, A, B, M, DIGIT_MAX=18):
    import numpy
    # 数列に対して、d桁の要素の個数を求める
    num_pow_d = [0] * (DIGIT_MAX + 1)
    for d in range(DIGIT_MAX + 1):
        if 10**d - 1 < A:
            continue
        # dに対してA + B*n <= 10**d - 1 となる最大のn(L以下しか取りえない)
        num_pow_d[d] = min(((10**d - 1 - A) // B) + 1, L)  # +1はnが0スタートのため

    ans = 0
    array = numpy.array([0, A, 1])
    for d in range(1, DIGIT_MAX + 1):
        c_d = num_pow_d[d] - num_pow_d[d - 1]
        matrix = numpy.array([[(10**d), 0, 0], [1, 1, 0], [0, B, 1]], dtype='object')
        m = numpy.eye(3, dtype='object') if c_d == 0 else numpy.linalg.matrix_power(matrix, c_d)
        array = array.dot(m)
        array %= M
    return array[0]

L, A, B, M = [int(i) for i in input().split()]
print(f_takahashi_basics_in_education_and_learning(L, A, B, M))

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split())

ten = 1

def f(l):
  if l == 0:
    return 0
  if l % 2 == 1:
    pl = l - 1
    x = f(pl)
    return (x * ten + 1) % M
  else:
    pl = l // 2
    x = f(pl)
    return (x * ten ** pl + x) % M

def g(l):
  if l == 0:
    return 0
  if l % 2 == 1:
    pl = l - 1
    x = g(pl)
    return (x * ten + B * pl) % M
  else:
    pl = l // 2
    x = g(pl)
    return (x * ten ** pl + x + B * pl * f(pl)) % M

last = A + B * (L - 1)
ans = 0

for i in range(1, 19):
  l = ten
  r = ten * 10 - 1
  
  if last < l:
    ten *= 10
    continue
  if A > r:
    ten *= 10
    continue
  na = 0 #初項
  nl = 0 #長さ

  if A >= l:
    na = A
  else:
    na = (l - A + B - 1) // B * B + A
    na = min(na, last)
  
  nlast = 0
  if last <= r:
    nlast = last
  else:
    nlast = (r - A) // B * B + A
  nl = (nlast - na) // B + 1

  ten *= 10
  ans *= ten ** nl % M
  ans += (na * f(nl)) % M
  ans += g(nl) % M

print(ans % M)

Submission status: Time Limit Exceeded

Solution code:
L, A, B, M = map(int, input().split())

ten = 1

def f(l):
  if l == 0:
    return 0
  if l % 2 == 1:
    pl = l - 1
    x = f(pl)
    return (x * (ten % M) + 1) % M
  else:
    pl = l // 2
    x = f(pl)
    return (x * (ten % M) ** pl + x) % M

def g(l):
  if l == 0:
    return 0
  if l % 2 == 1:
    pl = l - 1
    x = g(pl)
    return (x * (ten % M) + B * (pl % M)) % M
  else:
    pl = l // 2
    x = g(pl)
    return (x * (ten % M) ** pl + x + B % M * (pl % M) % M * f(pl)) % M

last = A + B * (L - 1)
ans = 0

for i in range(1, 19):
  l = ten
  r = ten * 10 - 1
  
  if last < l:
    ten *= 10
    continue
  if A > r:
    ten *= 10
    continue
  na = 0 #初項
  nl = 0 #長さ

  if A >= l:
    na = A
  else:
    na = (l - A + B - 1) // B * B + A
    na = min(na, last)
  
  nlast = 0
  if last <= r:
    nlast = last
  else:
    nlast = (r - A) // B * B + A
  nl = (nlast - na) // B + 1

  ten *= 10
  ans *= (ten % M) ** nl % M
  ans += (na % M * f(nl) % M) % M
  ans += g(nl) % M

print(ans % M)