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 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

    k = np.identity(3, dtype=np.int64)
    for d in range(18):
        if digits[d] == 0:
            continue

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

    s = np.array([0, A, 1])
    seq = np.mod(np.dot(s, k), M)
    return seq[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
    k_ = k
    while cnt * 2 <= n:
        k = np.dot(k, k)
        k = np.mod(k, mod)
        cnt *= 2

    if cnt == n:
        return k
    else:
        return np.mod(np.dot(k, rec_pow(k_, 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):
            cnt += 1
        cnt += max(0, (10**(d+1) - 1 - A) // B)
        if cnt >= L:
            cnt = L

        digits[d] += cnt - sum(digits[:d])
        if cnt >= L:
            break

    #print(digits)
    k = np.identity(3, dtype=np.int64)
    for d in range(18):
        if digits[d] == 0:
            continue

        kd = np.array([[10**(d+1), 0, 0],\
                       [1, 1, 0],\
                       [0, B, 1]],\
                       dtype=np.int64)
        kd = np.mod(kd, M)
        k = np.dot(k, rec_pow(kd, digits[d], M))
        k = np.mod(k, M)
        #print(k, '\n')

    s = np.array([0, A, 1])
    seq = np.mod(np.dot(s, k), M)
    return seq[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
    k_ = k
    while cnt * 2 <= n:
        k = np.dot(k, k)
        k = np.mod(k, mod)
        cnt *= 2

    if cnt == n:
        return k
    else:
        return np.dot(k, rec_pow(k_, n-cnt, 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):
            cnt += 1
        cnt += max(0, (10**(d+1) - 1 - A) // B)
        if cnt >= L:
            cnt = L

        digits[d] += cnt - sum(digits[:d])
        if cnt >= L:
            break

    #print(digits)
    k = np.identity(3, dtype=np.int64)
    for d in range(18):
        if digits[d] == 0:
            continue

        kd = np.array([[10**(d+1), 0, 0],\
                       [1, 1, 0],\
                       [0, B, 1]],\
                       dtype=np.int64)
        kd = np.mod(kd, M)
        k = np.dot(k, rec_pow(kd, digits[d], M))
        k = np.mod(k, M)
        #print(k, '\n')

    s = np.array([0, A, 1])
    seq = np.mod(np.dot(s, k), M)
    return seq[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
    k_ = k
    while cnt * 2 <= n:
        k = np.dot(k, k)
        k = np.mod(k, mod)
        cnt *= 2

    if cnt == n:
        return k
    else:
        return np.dot(k, rec_pow(k_, n-cnt, 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):
            cnt += 1
        cnt += max(0, (10**(d+1) - 1 - A) // B)
        if cnt >= L:
            cnt = L

        digits[d] += cnt - sum(digits[:d])
        if cnt >= L:
            break

    k = np.identity(3, dtype=np.int64)
    for d in range(18):
        if digits[d] == 0:
            continue

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

    s = np.array([0, A, 1])
    seq = np.mod(np.dot(s, k), M)
    return seq[0]

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

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
from decimal import *
L,A,B,M=map(int, input().split())

def matpow(X,n,mod):
  E = np.eye(N=X.shape[0],dtype=int)
  Y = X%mod
  if n == 1:
    return Y
  else:
    return np.dot(matpow(np.dot(Y,Y)%mod,n//2,mod), (Y if n % 2 else E))%mod
  
a = 0
for d in range(1,19):
  left = math.ceil(Decimal(10**(d-1)-A)/Decimal(B))
  right = math.ceil(Decimal(10**d-A)/Decimal(B))-1
  if right < 0:
    continue
  if left > L-1:
    break
  if left > right:
    continue
  if left < 0:
    left = 0
  if right > L-1:
    right = L-1
  print(d,left,right)
  mat = np.matrix([[10**d,0,0],[1,1,0],[0,1,1]])
  result = np.dot(np.array([a,A+B*left,B])%M, matpow(mat,right-left+1,M))
  a = result[0,0]%M
  
print(a)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
L,A,B,M=map(int, input().split())

def matpow(X,n,mod):
  E = np.eye(N=X.shape[0],dtype=int)
  Y = X%mod
  if n == 1:
    return Y
  else:
    return np.dot(matpow(np.dot(Y,Y)%mod,n//2,mod), (Y if n % 2 else E))%mod
  
a = 0
for d in range(1,19):
  left = math.ceil(float(10**(d-1)-A)/float(B))
  right = math.ceil(float(10**d-A)/float(B))-1
  if right < 0:
    continue
  if left > L-1:
    break
  if left > right:
    continue
  if left < 0:
    left = 0
  if right > L-1:
    right = L-1
  mat = np.matrix([[10**d,0,0],[1,1,0],[0,1,1]])
  result = np.dot(np.array([a,A+B*left,B])%M, matpow(mat,right-left+1,M))
  a = result[0,0]%M
  
print(a)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
L,A,B,M=map(int, input().split())

def matpow(X,n,mod):
  E = np.eye(N=X.shape[0],dtype=int)
  Y = X%mod
  if n == 1:
    return Y
  else:
    return np.dot(matpow(np.dot(Y,Y)%mod,n//2,mod), (Y if n % 2 else E))%mod

a = 0
for d in range(1,19):
  left = math.ceil((10**(d-1)-A)/B)
  right = math.floor((10**d-A-1)/B)
  if right < 0:
    continue
  if left > L-1:
    break
  if left > right:
    continue
  if left < 0:
    left = 0
  if right > L-1:
    right = L-1
    
  mat = np.matrix([[10**d,0,0],[1,1,0],[0,1,1]])
  result = np.dot(np.array([a,A+B*left,B])%M, matpow(mat,right-left+1,M))
  a = result[0,0]%M
  
print(a)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
L,A,B,M=map(int, input().split())

def matpow(X,n,mod):
  E = np.eye(N=X.shape[0],dtype=int)
  Y = X%mod
  if n == 1:
    return Y
  else:
    return np.dot(matpow(np.dot(Y,Y)%mod,n//2,mod), (Y if n % 2 else E))%mod

a = 0
for d in range(1,19):
  left = math.ceil((10**(d-1)-A)/B)
  right = math.ceil((10**d-A)/B)-1
  if right < 0:
    continue
  if left > L-1:
    break
  if left > right:
    continue
  if left < 0:
    left = 0
  if right > L-1:
    right = L-1
    
  mat = np.matrix([[10**d,0,0],[1,1,0],[0,1,1]])
  result = np.dot(np.array([a,A+B*left,B])%M, matpow(mat,right-left+1,M))
  a = result[0,0]%M
  
print(a)

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())
sys.exit()

def tousa(y, z):
    if z == 1:
        return 1
    if z % 2 == 1:
        return (1 + y * tousa(y, z - 1)) % M
    if z % 2 == 0:
        return ((1 + pow(y, z // 2, M)) * tousa(y, z // 2)) % M


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


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
print(dig)
dig=[0]+dig
fd=0
ans=0
for d in range(18,0,-1):
    if dig[d]==0:
        continue
    num=tousa(10**d,dig[d])
    kai=(A+B*dig[d])*num
    kai%=M
    kai+=B*(-num + (dig[d]-1)*pow(10**d,dig[d],M) +1)
    kai %= M
    print(kai)
    kai*=pow(10,fd,M)
    kai%=M
    ans+=kai
    ans%=M
    fd+=d*dig[d]
    print(d,fd,ans,kai)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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) % 10007
print(count)

Submission status: Wrong Answer

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, mod)
        ans += Ag * f(N)
        ans += g(N)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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, 19):
        #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, mod)
        ans += Ag * f(N)
        ans += g(N)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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):
        ret = ModInt(0)
        if N == 0:
            return ret
        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, 19):
        #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:
            Lastg = last
            N = (Lastg-Ag)//B + 1
        else:
            N = (((10 ** k) - 1 - Ag) // B) + 1
            
        #N*k桁ずらす
        #print(pow(10, N * k, mod))
        ans *= pow(10, N * k, mod)
        #print("         " ,ans)
        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np


def not_over_max_element(d):
    return (10 ** d - 1 - a) // b


def exp_np(a, e, m):
    r = np.identity(a.shape[0], dtype=np.int64)
    t = a.copy()
    while e:
        if e % 2:
            np.dot(r, t, r)
            r %= m
        np.dot(t, t, t)
        t %= m
        e >>= 1
    return r


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

first_d = len(str(a))
tail = a + b * (l - 1)

lo = -1
state = np.array([[0, a % m, 1]])

for d in range(first_d, 19):
    hi = min(not_over_max_element(d), l - 1)
    cnt = hi - lo
    coe = np.array([
        [pow(10, d, m), 0, 0],
        [1, 1, 0],
        [0, b % m, 1]
    ], dtype=np.int64)
    coe = exp_np(coe, cnt, m)
    state = np.dot(state, coe) % m

    lo = hi
    if hi == l - 1:
        break

print(state[0][0])

Submission status: Wrong Answer

Solution code:
from math import log10
import numpy as np

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

ans = 0
B = np.matrix([0, a, b], dtype=np.int64)

def b_search(ok, ng, d):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if int(log10(a + b * mid)) < d:
            ok = mid
        else:
            ng = mid

    return ok

def power_of_matrix(A, n, m):
    for i in range(A.shape[0] * A.shape[1]):
        assert A.item(i) >= 0
    if n == 0:
        return A ** 0
    if n == 1:
        return A
    if n % 2 == 0:
        return power_of_matrix(A ** 2 % m, n // 2, m) % m
    return A * power_of_matrix(A, n - 1, m) % m

for d in range(int(log10(a)), int(log10(a + b * (l - 1)) + 1)):
    left = b_search(-1, l, d) + 1
    right = b_search(-1, l, d + 1) + 1

    A = np.matrix([[pow(10, d + 1, m), 0, 0], [1, 1, 0], [0, 1, 1]], dtype=np.int64)
    B = B * power_of_matrix(A, right - left, m) % m
    for i in range(B.size):
        assert B.item(i) >= 0

print(B.item(0))

Submission status: Wrong Answer

Solution code:
from math import log10
import numpy as np

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

ans = 0
B = np.matrix([0, a, b], dtype=np.int64)

def b_search(ok, ng, d):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if int(log10(a + b * mid)) < d:
            ok = mid
        else:
            ng = mid

    return ok

def power_of_matrix(A, n, m):
    for i in range(A.shape[0] * A.shape[1]):
        assert A.item(i) >= 0
    if n == 0:
        return A ** 0
    if n == 1:
        return A
    if n % 2 == 0:
        return power_of_matrix(A ** 2 % m, n // 2, m) % m
    return A * power_of_matrix(A, n - 1, m) % m

for d in range(int(log10(a)), int(log10(a + b * (l - 1)) + 1)):
    left = b_search(-1, l, d) + 1
    right = b_search(-1, l, d + 1) + 1

    A = np.matrix([[pow(10, d + 1, m), 0, 0], [1, 1, 0], [0, 1, 1]], dtype=np.int64)
    B = B * power_of_matrix(A, right - left, m) % m

print(B.item(0))

Submission status: Wrong Answer

Solution code:
L,A,B,M=map(int,input().split())
mod=M
C=A+B*(L-1)
ans=0
def S(x,l,r,mod):
    if l==r:
        return pow(x,l,mod)
    rev=pow((x-1)%mod,mod-2,mod)
    return ((pow(x,r+1,mod)-pow(x,l,mod))*rev)%mod
def SS(x,l,r,mod):
    if l==r:
        return (l*pow(x,l,mod))%mod
    rev=pow((x-1)%mod,mod-2,mod)
    return (rev*(r*pow(x,r+1,mod)-l*pow(x,l,mod)-S(x,l+1,r,mod)))%mod
now=0
Sx=1
times=len(str(C))
while True:
    if now>=L:
        break
    if len(str(C))==1:
        r=C//B
    else:
        r=(C-pow(10,len(str(C))-1))//B
    if r==0:
        ans=(ans+C*Sx)%mod
        Sx=Sx*pow(10,len(str(C)),mod)%mod
        C-=B
        now+=1
    else:
        if now+r>=L:
            r=L-now-1
        x=pow(10,len(str(C)),mod)
        y=Sx*(C*(S(x,1,r,mod)+1)-B*SS(x,1,r,mod))%mod
        ans=(ans+y)%mod
    Sx=Sx*pow(10,(r+1)*len(str(C)),mod)%mod
    C-=(r+1)*B
    now+=r+1
print(ans)

Submission status: Wrong Answer

Solution code:
L,A,B,M=map(int,input().split())
mod=M
C=A+B*(L-1)
ans=0
def S(x,l,r,mod):
    if l==r:
        return pow(x,l,mod)
    rev=pow((x-1)%mod,mod-2,mod)
    return ((pow(x,r+1,mod)-pow(x,l,mod))*rev)%mod
def SS(x,l,r,mod):
    if l==r:
        return (l*pow(x,l,mod))%mod
    rev=pow((x-1)%mod,mod-2,mod)
    return (rev*(r*pow(x,r+1,mod)-l*pow(x,l,mod)-S(x,l+1,r,mod)))%mod
now=0
Sx=1
times=len(str(C))
while True:
    if now>=L:
        break
    if len(str(C))==1:
        r=C//B
    else:
        r=(C%pow(10,len(str(C))-1))//B
    if r==0:
        ans=(ans+C*Sx)%mod
        Sx=Sx*pow(10,len(str(C)),mod)%mod
        C-=B
        now+=1
    else:
        if now+r>=L:
            r=L-now-1
        x=pow(10,len(str(C)),mod)
        y=Sx*(C*(S(x,1,r,mod)+1)-B*SS(x,1,r,mod))%mod
        ans=(ans+y)%mod
    Sx=Sx*pow(10,(r+1)*len(str(C)),mod)%mod
    C-=(r+1)*B
    now+=r+1
print(ans)

Submission status: Wrong Answer

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', '')
def main():
    #二分法を使って高速に求める
    def f(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return ((f(N - 1)%mod * pow(10 , k,mod)) %mod + 1)%mod
        else:
            x = f(N//2)
            return ((x * pow(10 , k * (N//2) ,mod)) %mod + x)%mod
        
    
    def g(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return ((g(N - 1) * pow(10 , k,mod))%mod + ((N-1) * B)%mod) %mod
        else:
            x = g(N // 2)
            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=0
    for k in range(1, 19):
        #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
            Ag = min(Ag,last)
        #print("初項",Ag)

        #groupの要素数
        if last <= 10**k -1:
            Lastg = last
            N = (Lastg-Ag)//B + 1
        else:
            N = (((10 ** k) - 1 - Ag) // B) + 1
            
        #N*k桁ずらす
        ans *= pow(10, N * k, mod)
        ans %= mod

        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
        ans %= mod
    print(ans)
    
if __name__ == '__main__':
    main()