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 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
        #print("初項",Ag)

        #groupの要素数
        if last <= 10**k -1:
            Lastg = last
            N = (Lastg-Ag)//B + 1
        else:
            #N = (((10 ** k) - 1 - Ag) // B) + 1
            nlast = (10**k-1 - A) // B * B + A
            N = (nlast - Ag) // B + 1
            #print(nlast,N)

        #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()

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) * 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, 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:
            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()

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) * 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
        #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()

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
        #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%mod)
    
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', '')
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
        #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()

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
        #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()

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):
    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:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
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):
        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%=mod

        #N*k桁ずらす
        ans *= pow(10, N * k, mod)
        ans %= mod

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

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

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):
    if n == 0:
        return A ** 0
    if n == 1:
        return A
    if n % 2 == 0:
        return power_of_matrix(A ** 2, 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([[10 ** (d + 1), 0, 0], [1, 1, 0], [0, 1, 1]])
    # B = B * power_of_matrix(A, right - left, m) % m
    B = (B * A ** (right - left)) % m

print(B.item(0))

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):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        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%=mod

        #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()

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):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        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%=mod

        #N*k桁ずらす
        ans *=   pow(10 , N * k, mod)

        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
        ans%=mod
    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', '')
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):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        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%=mod

        #N*k桁ずらす
        ans *= (10 ** (N * k)) % mod
        ans%=mod

        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
        ans%=mod
    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', '')
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):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        Ag = (ceil((pow(10 , (k - 1), mod ) - A) / B)*B + A)%mod
        #print("初項",Ag)

        #groupの要素数
        if last <= 10**k -1:
            Lastg = last
            N = (Lastg-Ag)//B + 1
        else:
            N = (((10 ** k) - 1 - Ag) // B) + 1
        N%=mod

        #N*k桁ずらす
        ans *= (10 ** (N * k)) % mod
        ans%=mod

        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
        ans%=mod
    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', '')
def main():
    #二分法を使って高速に求める
    def f(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return ((f(N - 1)%mod * (10 ** k)%mod)%mod + 1)%mod
        else:
            x = f(N//2)
            return (x * (10 ** (k * (N//2))%mod)%mod + x)%mod
        
    
    def g(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return (g(N - 1) * (10 ** k) + (N-1) * B)%mod
        else:
            x = g(N // 2)
            return (x * (10 ** (k * (N//2))%mod)%mod + x + (N//2) * B * f(N//2))%mod
            




    L, A, B, mod = MI()
    first = A
    last = A + (L - 1) * B
    ans=0
    for k in range(1, 19):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        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
        #print("N",N)

        #N*k桁ずらす
        ans *= (10 ** (N * k)) % mod
        ans%=mod

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