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

Solution code:
#import sys
#input = sys.stdin.readline
#from numpy import matrix, eye, dot
def productMatrix(N, A, B):
    Ret = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            for k in range(N):
                Ret[i][j] += A[i][k]*B[k][j]
    return Ret
def modMatrix(N, A, Q): #N×N行列のmod
    for i in range(N):
        for j in range(N):
            A[i][j] %= Q
#            A[i,j] %= Q
    return

def powOfMatrix(N, X, n, Q): #N×N行列のn乗
#    Ret = eye(N)
    Ret = [[1,0,0],[0,1,0],[0,0,1]]
    power = '{:060b}'.format(n)[::-1] #log2(pow(10,18)) < 60
    for p in power:
        if p == "1":
            Ret = productMatrix(N,Ret, X)
#            Ret = dot(Ret, X)
            modMatrix(N, Ret, Q)
        X = productMatrix(N,X,X)
#        X = dot(X,X)
        modMatrix(N, X, Q)
#        print(Ret)
    return Ret

def main():
    L, A, B, M = map( int, input().split())
    s = A
    ANS = [[1,0,0],[0,1,0],[0,0,1]]
    for i in range(1, 37):
#        print(i, ANS)
        if s >= pow(10,i):
            continue
        P = [[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]]
#        P = matrix([[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]])
#        print((pow(10,i)-1-s)//B+1)
        if L <= (pow(10,i)-1-s)//B+1:
            ANS = productMatrix(3,ANS,powOfMatrix(3,P,L,M))
#            ANS = dot(ANS,powOfMatrix(3,P,L,M))
            modMatrix(3,ANS,M)
            break
#        print(powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
        ANS = productMatrix(3,ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
#        ANS = dot(ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
#        print( (ANS[1][0]*A + ANS[2][0])%M, (pow(10,i)-1-s)//B + 1)
        modMatrix(3,ANS, M)
        L -= (pow(10,i)-1-s)//B + 1
        s = ((pow(10,i)-1-s)//B+1)*B
    print( (ANS[1][0]*A + ANS[2][0])%M)
#    print( int((ANS[1,0]*A + ANS[2,0])%M))
        
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
#from numpy import matrix, eye, dot
def productMatrix(N, A, B):
    Ret = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            for k in range(N):
                Ret[i][j] += A[i][k]*B[k][j]
    return Ret
def modMatrix(N, A, Q): #N×N行列のmod
    for i in range(N):
        for j in range(N):
            A[i][j] %= Q
#            A[i,j] %= Q
    return

def powOfMatrix(N, X, n, Q): #N×N行列のn乗
#    Ret = eye(N)
    Ret = [[1,0,0],[0,1,0],[0,0,1]]
    power = '{:060b}'.format(n)[::-1] #log2(pow(10,18)) < 60
    for p in power:
        if p == "1":
            Ret = productMatrix(N,Ret, X)
#            Ret = dot(Ret, X)
            modMatrix(N, Ret, Q)
        X = productMatrix(N,X,X)
#        X = dot(X,X)
        modMatrix(N, X, Q)
#        print(Ret)
    return Ret

def main():
    L, A, B, M = map( int, input().split())
    s = A
    ANS = [[1,0,0],[0,1,0],[0,0,1]]
    for i in range(1, 37):
#        print(i, ANS)
        if s >= pow(10,i):
            continue
        P = [[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]]
#        P = matrix([[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]])
#        print((pow(10,i)-1-s)//B+1)
        if L <= (pow(10,i)-1-s)//B+1:
            ANS = productMatrix(3,ANS,powOfMatrix(3,P,L,M))
#            ANS = dot(ANS,powOfMatrix(3,P,L,M))
            modMatrix(3,ANS,M)
            break
#        print(powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
        ANS = productMatrix(3,ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
#        ANS = dot(ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
        print( (ANS[1][0]*A + ANS[2][0])%M, (pow(10,i)-1-s)//B + 1)
        modMatrix(3,ANS, M)
        L -= (pow(10,i)-1-s)//B + 1
        s = ((pow(10,i)-1-s)//B+1)*B
    print( (ANS[1][0]*A + ANS[2][0])%M)
#    print( int((ANS[1,0]*A + ANS[2,0])%M))
        
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
#from numpy import matrix, eye, dot
def productMatrix(N, A, B):
    Ret = [[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            for k in range(N):
                Ret[i][j] += A[i][k]*B[k][j]
    return Ret
def modMatrix(N, A, Q): #N×N行列のmod
    for i in range(N):
        for j in range(N):
            A[i][j] %= Q
#            A[i,j] %= Q
    return

def powOfMatrix(N, X, n, Q): #N×N行列のn乗
#    Ret = eye(N)
    Ret = [[1,0,0],[0,1,0],[0,0,1]]
    power = '{:060b}'.format(n)[::-1] #log2(pow(10,18)) < 60
    for p in power:
        if p == "1":
            Ret = productMatrix(N,Ret, X)
#            Ret = dot(Ret, X)
            modMatrix(N, Ret, Q)
        X = productMatrix(N,X,X)
#        X = dot(X,X)
        modMatrix(N, X, Q)
#        print(Ret)
    return Ret

def main():
    L, A, B, M = map( int, input().split())
    s = A
    ANS = [[1,0,0],[0,1,0],[0,0,1]]
    for i in range(1, 37):
#        print(i, ANS)
        if s >= pow(10,i):
            continue
        P = [[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]]
#        P = matrix([[pow(10, i, M), 0, 0], [1,1,0], [0,B,1]])
#        print((pow(10,i)-1-s)//B+1)
        if L <= (pow(10,i)-1-s)//B+1:
            ANS = productMatrix(3,ANS,powOfMatrix(3,P,L,M))
#            ANS = dot(ANS,powOfMatrix(3,P,L,M))
            modMatrix(3,ANS,M)
            break
#        print(powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
        ANS = productMatrix(3,ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
#        ANS = dot(ANS, powOfMatrix(3,P, (pow(10,i)-1-s)//B + 1,M))
        print( (ANS[1][0]*A + ANS[2][0])%M, (pow(10,i)-1-s)//B + 1)
        modMatrix(3,ANS, M)
        L -= (pow(10,i)-1-s)//B + 1
        s = ((pow(10,i)-1-s)//B+1)*B
    print( (ANS[1][0]*A + ANS[2][0])%M)
#    print( int((ANS[1,0]*A + ANS[2,0])%M))
        
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce

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

MOD = M


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

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

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

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

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

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

    def __radd__(self, other):
        return ModInt(other + self.x)

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

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


def iceil(a, b):
    return (a + b - 1) // b


# 等差数列を桁数が同一の要素ごとに区切る
# 区切った等差数列の各要素について、それを末尾に追加する操作を線形変換とみなす
# 線形変換はダブリングで高速に計算する
T = {}
x = A
while x <= A + B * (L - 1):
    k = len(str(x))
    r = iceil(
        min(
            10**k,
            A + B * (L - 1) + 1
        ) - x,
        B
    )
    T[k] = r
    x += r * B


def mut_mul(X, Y):
    return [
        [
            sum(X[i][k] * Y[k][j] for k in range(len(Y)))
            for j in range(len(Y[0]))
        ]
        for i in range(len(X))
    ]


def mut_pow(X, n):
    if n == 0:
        return [
            [1, 0, 0],
            [0, 1, 0],
            [0, 0, 1]
        ]
    else:
        Y = mut_pow(X, n // 2)
        return (
            mut_mul(Y, Y) if n % 2 == 0 else
            mut_mul(mut_mul(Y, Y), X)
        )


def f(p, q, k, r):
    Y = mut_pow(
        [
            [ModInt(10**k), ModInt(1), ModInt(0)],
            [ModInt(0), ModInt(1), ModInt(1)],
            [ModInt(0), ModInt(0), ModInt(1)]
        ],
        r
    )
    Z = mut_mul(
        Y,
        [
            [p],
            [q],
            [ModInt(B)]
        ]
    )
    return (Z[0][0], Z[1][0])


ans, _ = reduce(
    lambda acc, kr: f(acc[0], acc[1], kr[0], kr[1]),
    T.items(),
    (ModInt(0), ModInt(A))
)

print(ans)

Submission status: Wrong Answer

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

def f(j,k,d):
  j%=m
  ret=((a+j*b)*(1-10**(d*k))//(1-10**d))%m
  ret=(ret-b*(10**d)*(1-10**(d*k))//((1-10**d)**2))%m
  ret=(ret+b*k*10**(d*k)//(1-10**d))%m
  return ret

A=[0]*20
for i in range(1,20):
  A[i]+=min((10**i-1-a)//b,l-1)-min((10**(i-1)-1-a)//b,l-1)
ans,keta,kou=0,0,l-1

for i in range(1,20):
  kk=A[-i]
  if kk==0:
    continue
  else:
    dd=20-i
    ans=(ans+f(kou,kk,dd)*10**keta)%m
    kou-=kk
    keta+=kk*dd
print(ans)

Submission status: Wrong Answer

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

def f(j,k,d):
  ret=((a+j*b)*(1-10**(d*k))//(1-10**d))%m
  ret=(ret-b*(10**d)*(1-10**(d*k))//((1-10**d)**2))%m
  ret=(ret+b*k*10**(d*k)//(1-10**d))%m
  return ret

A=[0]*40
for i in range(1,40):
  A[i]+=min((10**i-1-a)//b,l-1)-min((10**(i-1)-1-a)//b,l-1)
ans,keta,kou=0,1,l-1

for i in range(1,40):
  kk=A[-i]
  if kk==0:
    continue
  else:
    dd=40-i
    ans=(ans+f(kou,kk,dd)*keta)%m
    kou-=kk
    keta=(keta*10**(kk*dd))%m
print(ans)

Submission status: Wrong Answer

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

def f(j,k,d):
  return (a+j*b)*(1-10**(d*k))//(1-10**d)-b*(10**d)*(1-10**(d*k))//((1-10**d)**2)+b*k*10**(d*k)//(1-10**d)

A=[0]*40
for i in range(1,40):
  A[i]+=min((10**i-1-a)//b,l-1)-min((10**(i-1)-1-a)//b,l-1)
ans,keta,kou=0,0,l-1

for i in range(1,40):
  kk=A[-i]
  if kk==0:
    continue
  else:
    dd=40-i
    ans=(ans+f(kou,kk,dd)*10**keta)%m
    kou-=kk
    keta+=kk*dd
print(ans)

Submission status: Wrong Answer

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

def f(j,k,d):
  ret=(a+j*b)*(1-10**(d*k))//(1-10**d)
  ret-=b*(10**d)*(1-10**(d*k))//((1-10**d)**2)
  ret+=b*k*10**(d*k)//(1-10**d)
  return ret

A=[0]*40
for i in range(1,40):
  a1=min((10**i-1-a)//b,l-1)
  a2=min((10**(i-1)-1-a)//b,l-1)
  A[i]+=a1-a2
ans=0
keta=0
kou=l-1
for i in range(1,40):
  kk=A[-i]
  if kk==0:
    continue
  else:
    dd=40-i
    ans=(ans+f(kou,kk,dd)*10**keta)%m
    kou-=kk
    keta+=kk*dd
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(pow(10, 6))
# input = sys.stdin.readline

import numpy as np

def pow_matmul(r, n, m):
  if n == 1:
    return r
  if n % 2 == 0:
    _r = pow_matmul(r, n//2, m)
    return np.mod(np.dot(_r, _r), m)
  else:
    _r = pow_matmul(r, n//2, m)
    _r = np.mod(np.dot(_r, _r), m)
    return np.mod(np.dot(r, _r), m)

def main():
  l, a, b, m = map(int, input().split())
  c = [min(max(0, (10**i - a - 1) // b + 1), l) for i in range(19)]
  ans = 0
  r = np.ones((3,3))
  s = np.array([0, a%m, 1])

  for d in range(1, 19):
    dnum = c[d] - c[d-1]
    if dnum==0:
      continue
    r_s = np.array([[pow(10, d, m), 1, 0], [0, 1, (b%m)], [0, 0, 1]])
    r = pow_matmul(r_s, dnum, m)
    s = np.mod(np.dot(r, s), m)
    print(d, r, s)
    if c[d] > l:
      break
  print(s[0])


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

Solution code:
from math import ceil,floor
import sys
#sys.setrecursionlimit(10**7)
L,A,B,M=map(int,input().split())

def digit(d):
    if 10**d<=A:
        return A
    return ceil((10**d-A)/B)*B + A

def dot(X,Y):
    L=[[0]*3 for i in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                L[i][j]+=X[i][k]*Y[k][j]
                L[i][j]%=M
    return L
def dot2(X,y):
    L=[0]*3
    for i in range(3):
        for j in range(3):
            L[i]+=X[i][j]*y[j]
            L[i]%=M
    return L

def f(x,t):
    X=[[1,x,1],[0,x,1],[0,0,1]]
    a=t-1
    T=[[1,0,0],[0,1,0],[0,0,1]]
    while a>0:
        if a%2==1:
            T=dot(T,X)
        X=dot(X,X)
        a=a//2
    #print(T,dot2(T,[1,1,1]))
    return dot2(T,[1,1,1])

dig=[0]*19
t=L
for d in range(19):
    s=( digit(d+1)-digit(d) )//B
    if s>=t:
        dig[d]=t
        break
    t-=s
    dig[d] = s
dig=[0]+dig
#print(dig)
fd=0
ans=0
for d in range(19,0,-1):
    if dig[d]==0:
        continue
    tn,sn,c=f(10**d,dig[d])
    a=digit(d-1)
    t=(a*sn+B*(tn-sn))%M
    kai=t*pow(10,fd,M)
    ans+=kai
    ans%=M
    fd+=d*dig[d]
    #print(tn,sn,c,a,fd,kai,digit(d-1))
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor
import sys
#sys.setrecursionlimit(10**7)
L,A,B,M=map(int,input().split())

def digit(d):
    if 10**d<=A:
        return A
    return ceil((10**d-A)/B)*B + A

def dot(X,Y):
    L=[[0]*3 for i in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                L[i][j]+=X[i][k]*Y[k][j]
                L[i][j]%=M
    return L
def dot2(X,y):
    L=[0]*3
    for i in range(3):
        for j in range(3):
            L[i]+=X[i][j]*y[j]
            L[i]%=M
    return L

def f(x,t):
    X=[[1,x,1],[0,x,1],[0,0,1]]
    a=t-1
    T=[[1,0,0],[0,1,0],[0,0,1]]
    while a>0:
        if a%2==1:
            T=dot(T,X)
        X=dot(X,X)
        a=a//2
    #print(T,dot2(T,[1,1,1]))
    return dot2(T,[1,1,1])

dig=[0]*18
t=L
for d in range(18):
    s=( digit(d+1)-digit(d) )//B
    if s>=t:
        dig[d]=t
        break
    t-=s
    dig[d] = s
dig=[0]+dig
#print(dig)
fd=0
ans=0
for d in range(18,0,-1):
    if dig[d]==0:
        continue
    tn,sn,c=f(10**d,dig[d])
    a=digit(d-1)
    kai=((a-B)*sn+B*tn)*pow(10,fd,M)
    ans+=kai
    ans%=M
    fd+=d*dig[d]
    #print(tn,sn,c,a,fd,kai,digit(d-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    if n == 1:
        return k

    k2 = dot(k, k, mod)
    if n % 2 == 0:
        return rec_pow(k2, n//2, mod)
    else:
        return dot(rec_pow(k2, n//2, mod), k, mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    cnt = 0
    s = [[0, A%M, 1]]
    for d in range(1, 19):
        f1 = min(max(0, (10**d - 1 - A) // B), L-1)
        f0 = min(max(0, (10**(d-1) - 1 - A) // B), L-1)
        n = f1 - f0
        if 10**(d-1) <= A <= 10**d:
            n += 1
        if n == 0:
            continue

        k = [[pow(10, d, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, n, M), M)

        if f1 >= L-1:
            break

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    if n == 1:
        return k

    k2 = dot(k, k, mod)
    if n % 2 == 0:
        return rec_pow(k2, n//2, mod)
    else:
        return dot(rec_pow(k2, n//2, mod), k, mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    cnt = 0
    s = [[0, A%M, 1]]
    for d in range(1, 19):
        div = min(max(0, (10**d - 1 - A) // B), L-1)
        n = div - cnt
        if 10**(d-1) <= A <= 10**d:
            n += 1
        if n == 0:
            continue

        k = [[pow(10, d, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, n, M), M)

        cnt = div
        if cnt >= L-1:
            break

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    if n == 1:
        return k

    k2 = dot(k, k, mod)
    if n % 2 == 0:
        return rec_pow(k2, n//2, mod)
    else:
        return dot(rec_pow(k2, n//2, mod), k, mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    cnt = 0
    s = [[0, A%M, 1]]
    for d in range(1, 19):
        n = 0
        if 10**(d-1) <= A <= 10**d:
            n += 1

        div = max(0, (10**d - 1 - A) // B)
        div = min(div, L-1)
        n += div - cnt
        if n == 0:
            continue

        k = [[pow(10, d, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, n, M), M)

        cnt = div
        if cnt >= L-1:
            break

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    if n == 1:
        return k

    k2 = dot(k, k, mod)
    if n % 2 == 0:
        return rec_pow(k2, n//2, mod)
    else:
        return dot(rec_pow(k2, n//2, mod), k, mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = [[0, A%M, 1]]
    for d in range(18):
        if digits[d] == 0:
            continue

        k = [[pow(10, d+1, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, digits[d], M), M)

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                if A[i][k] == B[k][j]:
                    res[i][j] += pow(A[i][k], 2, mod)
                else:
                    res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = dot(k, k,  mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return dot(k, rec_pow(k1, n-cnt, mod), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = [[0, A%M, 1]]
    for d in range(18):
        if digits[d] == 0:
            continue

        k = [[pow(10, d+1, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, digits[d], M), M)

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys


def dot(A, B, mod):
    r = len(A)
    c = len(B[0])
    m = len(A[0])
    res = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            for k in range(m):
                res[i][j] += (A[i][k] * B[k][j]) % mod
                res[i][j] %= mod
    return res


def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = dot(k, k,  mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return dot(k, rec_pow(k1, n-cnt, mod), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = [[0, A%M, 1]]
    for d in range(18):
        if digits[d] == 0:
            continue

        k = [[pow(10, d+1, M), 0, 0],\
             [1, 1, 0],\
             [0, B%M, 1]]
        s = dot(s, rec_pow(k, digits[d], M), M)

    return s[0][0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys


def dot_mod(a, b, mod):
    res = np.zeros((3, 3), dtype=np.int64)
    for i in range(3):
        for j in range(3):
            tmp = a[i, :] * b[:, j]
            tmp = np.mod(tmp, mod)
            for t in tmp:
                res[i, j] = (res[i, j] + t) % mod
    return res


def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = dot_mod(k, k, mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return np.mod(np.dot(k, rec_pow(k1, n-cnt, mod)), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = np.array([0, A%M, 1])
    for d in range(18):
        if digits[d] == 0:
            continue

        k = np.array([[pow(10, d+1, M), 0, 0],\
                      [1, 1, 0],\
                      [0, B%M, 1]],\
                      dtype=np.int64)
        s = np.dot(s, rec_pow(k, digits[d], M))
        s = np.mod(s, M)

    return s[0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys


def dot_mod(a, b, mod):
    res = np.zeros((3, 3), dtype=np.int64)
    for i in range(3):
        for j in range(3):
            tmp = a[i, :] * b[:, j]
            tmp = np.mod(tmp, mod)
            res[i, j] = np.sum(tmp) % mod
    return res


def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = dot_mod(k, k, mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return np.mod(np.dot(k, rec_pow(k1, n-cnt, mod)), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = np.array([0, A%M, 1])
    for d in range(18):
        if digits[d] == 0:
            continue

        k = np.array([[pow(10, d+1, M), 0, 0],\
                      [1, 1, 0],\
                      [0, B%M, 1]],\
                      dtype=np.int64)
        s = np.dot(s, rec_pow(k, digits[d], M))
        s = np.mod(s, M)

    return s[0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = np.mod(np.dot(np.mod(k, mod), np.mod(k, mod)), mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return np.mod(np.dot(k, rec_pow(k1, n-cnt, mod)), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = np.array([0, A%M, 1])
    for d in range(18):
        if digits[d] == 0:
            continue

        k = np.array([[pow(10, d+1, M), 0, 0],\
                      [1, 1, 0],\
                      [0, B%M, 1]],\
                      dtype=np.int64)
        s = np.dot(s, rec_pow(k, digits[d], M))
        s = np.mod(s, M)

    return s[0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = np.mod(np.dot(k, k), mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return np.mod(np.dot(k, rec_pow(k1, n-cnt, mod)), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = np.array([0, A%M, 1])
    for d in range(18):
        if digits[d] == 0:
            continue

        k = np.array([[pow(10, d+1, M), 0, 0],\
                      [1, 1, 0],\
                      [0, B%M, 1]],\
                      dtype=np.int64)
        s = np.dot(s, rec_pow(k, digits[d], M))
        s = np.mod(s, M)

    return s[0]

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

def rec_pow(k, n, mod):
    cnt = 1
    k1 = k
    while cnt * 2 <= n:
        k = np.mod(np.dot(k, k), mod)
        cnt *= 2

    if cnt == n:
        return k
    else: 
        return np.mod(np.dot(k, rec_pow(k1, n-cnt, mod)), mod)


def main():
    input = sys.stdin.readline
    L, A, B, M = map(int, input().split())

    digits = [0] * 18
    cnt = 0
    for d in range(18):
        if 10**d <= A <= 10**(d+1):
            digits[d] += 1

        div = max(0, (10**(d+1) - 1 - A) // B)
        if div >= L-1:
            digits[d] += L - 1 - cnt
            break
        digits[d] += div - cnt
        cnt = div

    s = np.array([0, A, 1])
    for d in range(18):
        if digits[d] == 0:
            continue

        k = np.array([[pow(10, d+1, M), 0, 0],\
                      [1, 1, 0],\
                      [0, B, 1]],\
                      dtype=np.int64)
        k = np.mod(k, M)
        s = np.dot(s, rec_pow(k, digits[d], M))
        s = np.mod(s, M)

    return s[0]

if __name__ == '__main__':
    print(main())