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) * (10 ** k)) + 1)%mod
        else:
            x = f(N//2)
            return (x * (10 ** (k * (N//2))) + 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))) + 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()

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) * (10 ** k)) + 1)%mod
        else:
            x = f(N//2)
            return (x * (10 ** (k * (N//2))) + 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))) + 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()

Submission status: Wrong Answer

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

cd = []
for i in range(18):
    cd.append(max(0, (10 ** (i + 1) - 1 - a) // b + 1))
    if cd[-1] >= l:
        cd[-1] = l
        break

cd_sum = 0
cd_2 = []

for i in cd:
    cd_2.append(i-cd_sum)
    cd_sum += i-cd_sum

X = [0, a, 1]
B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
for i in range(len(cd_2)):
    A = [[(10 ** (i + 1)), 0, 0], [1, 1, 0], [0, b, 1]]
    B = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

    #for j in range(cd_2[i]):
    n = cd_2[i]
    while n > 0:
        print(n)
        if n & 1:

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

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

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

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

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

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

        print(A)
        n = n >> 1

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

print(X[0] % m)

Submission status: Wrong Answer

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

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

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

    A = np.matrix([[10**d, 0, 0], [1, 1, 0], [0, b, 1]])
    B *= A

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

Submission status: Wrong Answer

Solution code:
l, a, b, MOD = [int(item) for item in input().split()]
vec = [a, a+b, 1]

def mat_mat_mul3(m1, m2):
    m3 = [[0] * 3 for _ in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                m3[i][j] += m1[i][k] * m2[k][j]
                m3[i][j] %= MOD
    return m3

def mat_vec_mul3(m1, v1):
    v2 = [0] * 3
    for i in range(3):
        for k in range(3):
            v2[i] += v1[k] * m1[i][k]
            v2[i] %= MOD
    return v2

A_d_pow = [[None] * 65 for _ in range(19)]
for d in range(19):
    for i in range(65):
        if i == 0:
            A = [[1, 0, 0], 
                 [0, 1, 0], 
                 [0, 0, 1]]
            A_d_pow[d][i] = A
            continue
        if i == 1:
            A = [[10**d % MOD, 1, 0], 
                 [0, 1, b % MOD], 
                 [0, 0, 1]]
            A_d_pow[d][i] = A
            continue
        A_d_pow[d][i] = mat_mat_mul3(A_d_pow[d][i-1], A_d_pow[d][i-1])

multimes = [0] * 19
l -= 1
for i in range(1,19):
    if 10**i - 1 - a > 0:
        cnt = (10**i - 1 - a) // b
        if cnt >= l:
            multimes[i] = l
            break
        else:
            multimes[i] = cnt
            l -= cnt
            a += cnt * b

for i, num in enumerate(multimes):
    if num == 0:
        continue
    for j in range(65):
        if num & 1 << j:
            vec = mat_vec_mul3(A_d_pow[i][j+1], vec)

print(vec[0])

Submission status: Wrong Answer

Solution code:
l, a, b, MOD = [int(item) for item in input().split()]
vec = [a, a+b, 1]

def mat_mat_mul3(m1, m2):
    m3 = [[0] * 3 for _ in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                m3[i][j] += m1[i][k] * m2[k][j]
                m3[i][j] %= MOD
    return m3

def mat_vec_mul3(m1, v1):
    v2 = [0] * 3
    for i in range(3):
        for k in range(3):
            v2[i] += v1[k] * m1[i][k]
            v2[i] %= MOD
    return v2

A_d_pow = [[None] * 65 for _ in range(19)]
for d in range(19):
    for i in range(65):
        if i == 0:
            A = [[1, 0, 0], 
                 [0, 1, 0], 
                 [0, 0, 1]]
            A_d_pow[d][i] = A
            continue
        if i == 1:
            A = [[10**d % MOD, 1, 0], 
                 [0, 1, b % MOD], 
                 [0, 0, 1]]
            A_d_pow[d][i] = A
            continue
        A_d_pow[d][i] = mat_mat_mul3(A_d_pow[d][i-1], A_d_pow[d][i-1])

multimes = [0] * 19
l -= 1
for i in range(1,19):
    if 10**i - 1 - a > 0:
        cnt = (10**i - 1 - a) // b
        if cnt > l:
            multimes[i] = l
            break
        else:
            multimes[i] = cnt
            l -= cnt
            a += cnt * b

for i, num in enumerate(multimes):
    if num == 0:
        continue
    for j in range(19):
        if num & 1 << j:
            vec = mat_vec_mul3(A_d_pow[i][j+1], vec)

print(vec[0])

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res       
L, A, B, M = map(int, input().split())

pre = -1
S = []
for k in range(50):
    j = -((A - 10**(k+1))//B) - 1
    j = min(j, L-1)
    j = j - pre
    pre += j
    if j:
        S.append((j, k+1))

Ans = [[0], [A], [1]]
for p, k in S:
    cmat = [[pow(10, k, M), 1, 0], [0, 1, B], [0, 0, 1]]
    
    Ans = mat_dot(mat_pow(cmat, p, M), Ans, M)
print(Ans[0][0])

Submission status: Wrong Answer

Solution code:
def mat_dot(A, B, mod):
    assert len(A[0]) == len(B), 'invalid_size'
    
    L = len(A)
    M = len(A[0])
    N = len(B[0])
    
    res = [[0]*N for _ in range(L)]
    
    for i in range(L):
        for j in range(N):
            a = 0
            for k in range(M):
                a = (a+A[i][k]*B[k][j]) % mod
            res[i][j] = a
            
    return res

def mat_pow(A, x, mod):
    N = len(A)
    res = [[0]*N for _ in range(N)]
    for i in range(N):
        res[i][i] = 1
    for i in range(x.bit_length()):
        if 2**i & x:
            res = mat_dot(res, A, mod)
        A = mat_dot(A, A, mod)
    return res

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

pre = -1
ans = 0
S = []
for k in range(50):
    j = -((A - 10**(k+1))//B) - 1
    j = min(j, L-1)
    j = j - pre
    pre += j
    if j:
        S.append((j, k+1))

Ans = [[ans], [A], [1]]
for p, k in S:
    cmat = [[pow(10, k, M), 1, 0], [0, 1, B], [0, 0, 1]]
    
    Ans = mat_dot(mat_pow(cmat, p, M), Ans, M)
print(Ans[0][0])

Submission status: Wrong Answer

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

ten = 10

def pow_(x, t):
  if t == 0:
    return 1
  if t % 2 == 1:
    return (pow_(x, t - 1) * x) % M
  else:
    y = pow_(x, t // 2)
    return (y * y) % M

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

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

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

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

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

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

print(ans % M)

Submission status: Wrong Answer

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

ten = 0

def pow_(x, t):
  if t == 0:
    return 1
  if t % 2 == 1:
    return (pow_(x, t - 1) * x) % M
  else:
    y = pow_(x, t // 2)
    return (y * y) % M

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

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

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

for _ in range(18):
  l = ten // 10
  r = ten - 1
  if last < l:
    ten *= 10
    continue
  if A > r:
    ten *= 10
    continue
  na = 0
  nl = 0

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

  ans *= pow_(ten, nl) % M
  ans += (f(nl) * na) % M
  ans += g(nl) % M
print(ans % M)

Submission status: Wrong Answer

Solution code:
from math import ceil
def dot(a,b,m):
    c = [[0,0,0],[0,0,0],[0,0,0]]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                c[i][j] += a[i][k]*b[k][j]
                c[i][j] %= m
    return c
    
def powm(a,n,m):
    if n == 0:
        return [[1,0,0],[0,1,0],[0,0,1]]
    for i in range(3):
        for j in range(3):
            a[i][j] %= m
    r = powm(a,n//2,m)
    if n%2==0:
        return dot(r,r,m)
    else:
        return dot(dot(r,r,m),a,m)

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

a = [[1,0,0],[0,1,0],[0,0,1]]
for d in range(1,19):
    C = min(max(0,ceil((10**d-A)/B)),L)-min(max(0,ceil((10**(d-1)-A)/B)),L)
    a = dot(powm([[pow(10,d,M),1,0],[0,1,B%M],[0,0,1]],C,M),a,M)
ans = (a[0][1]*A%M+a[0][2])%M
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil
def dot(a,b,m):
    c = [[0,0,0],[0,0,0],[0,0,0]]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                c[i][j] += a[i][k]*b[k][j]
                c[i][j] %= m
    return c
    
def powm(a,n,m):
    if n == 0:
        return [[1,0,0],[0,1,0],[0,0,1]]
    for i in range(3):
        for j in range(3):
            a[i][j] %= m
    r = powm(a,n//2,m)
    if n%2==0:
        return dot(r,r,m)
    else:
        return dot(dot(r,r,m),a,m)

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

a = [[1,0,0],[0,1,0],[0,0,1]]
for d in range(1,19):
    C = min(max(0,ceil((10**d-A)/B)),L)-min(max(0,ceil((10**(d-1)-A)/B)),L)
    a = dot(powm([[pow(10,d,M),1,0],[0,1,B%M],[0,0,1]],C,M),a,M)
ans = (a[0][1]*A+a[0][2])%M
print(ans)

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b-1) // b - max(0, (10**(i-1) -a+b-1) // b)
dgt[n] = l-sum(dgt)
#print(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
#print(d_sum)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
#print(fr)
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	#print(j)
	#print(len(l), len(j), len(j[0]))
	l = mat_dot(l, j, m)
	#print(l)

print(l[0][0]%m)

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b-1) // b - max(0, (10**(i-1) -a+b-1) // b)
dgt[n] = l-sum(dgt)
print(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
#print(d_sum)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
#print(fr)
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	#print(j)
	#print(len(l), len(j), len(j[0]))
	l = mat_dot(l, j, m)
	#print(l)

print(l[0][0]%m)

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
ans = 0
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b-1) // b - max(0, (10**(i-1) -a+b-1) // b)
dgt[n] = l-sum(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	l = mat_dot(l, j, m)

print(l[0][0])

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
ans = 0
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b-1) // b - (10**(i-1) -a+b-1) // b
dgt[n] = l-sum(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	l = mat_dot(l, j, m)

print(l[0][0])

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
ans = 0
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b) // b - (10**(i-1) -a+b) // b
dgt[n] = l-sum(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	l = mat_dot(l, j, m)

print(l[0][0])

Submission status: Wrong Answer

Solution code:
def mat_dot(one, two, mod):
	return [[sum([(one[i][k]*two[k][j])%mod for k in range(len(two))])%mod for j in range(len(two[0]))] for i in range(len(one))]

def mat_pow(mat, exp, mod):
	size = len(mat)
	res = [[0 for _ in range(size)] for _ in range(size)]
	for i in range(size):
		res[i][i] = 1
	cnt = 0
	while (1<<cnt) <= exp:
		if (exp>>cnt)&1:
			res = mat_dot(res, mat, mod)
		mat = mat_dot(mat, mat, mod)
		cnt += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
ans = 0
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b -1) // b - dgt[i-1]
dgt[n] = l-sum(dgt)
#print(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
#print(d_sum)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
#print(fr)
l = [[0, a%m, 1]]
for d in range(1, n+1):
	k = [[(10**d)%m, 0, 0], [1, 1, 0], [0, b%m, 1]]
	j = mat_pow(k, dgt[d], m)
	#print(l, j)
	#print(len(l), len(j), len(j[0]))
	l = mat_dot(l, j, m)
	#print(l)

print(l[0][0])

Submission status: Wrong Answer

Solution code:
import numpy as np
def mat_pow(mat, exp, mod):
	res = np.eye(len(mat))
	i = 0
	while (1<<i) <= exp:
		if (exp>>i)&1:
			res = np.dot(res, mat)
			res %= mod
		mat = np.dot(mat, mat)
		mat %= mod
		i += 1
	return res

l, a, b, m = map(int, input().split())
c = a + (l-1)*b
ans = 0
n = len(str(c))
dgt = [0 for _ in range(n+1)]
for i in range(1, n):
	dgt[i] = (10**i -a+b -1) // b - dgt[i-1]
dgt[n] = l-sum(dgt)
#print(dgt)
d_sum = [0 for _ in range(n+1)]
for i in range(n-1, 0, -1):
	d_sum[i] = d_sum[i+1] + dgt[i+1]*(i+1)
#print(d_sum)
fr = [0 for _ in range(n+1)]
fr[1] = a
for i in range(2, n+1):
	fr[i] = fr[i-1] + b*dgt[i-1]
#print(fr)
l = np.array([0, a, 1])
for d in range(1, n+1):
	k = np.array([[10**d, 0, 0], [1, 1, 0], [0, b, 1]])
	j = mat_pow(k, dgt[d], m)
	#print(j)
	l = np.dot(l, j)
	#print(l)
print(int(l[0])%m)

Submission status: Wrong Answer

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

T = []
for w in range(0, len(str(A+B*(L-1)))):
  l0 = ((10**w-1)-A)/B+1
  l1 = ((10**(w+1)-1)-A)/B+1
  T += [(min(l1, L)-max(l0, 0), A+B*l0, w+1)]

def f(L, w):
  if L==1:
    return 1
  t = f(L/2, 2*w)*(pow(10, w, M) + 1)%M
  if L%2!=0:
    t = (t*pow(10, w, M) + 1)%M
  return t

def g(L, B, w):
  if L==1:
    return 0
  t = (g(L/2, B*2, w*2)*(pow(10, w, M) + 1) + B*f(L/2, w*2))%M
  if L%2!=0:
    t = (t*pow(10, w, M) + B*(L-1))%M
  return t

ans = 0
w = 0
for t in T[::-1]:
  if t[0]>0:
    ans += (t[1]*f(t[0], t[2]) + g(t[0], B, t[2]))*pow(10, w, M)
    ans %= M
  w += t[2]*t[0]
print ans

Submission status: Wrong Answer

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

T = []
for w in range(0, len(str(A+B*(L-1)))):
  l0 = ((10**w-1)-A)/B+1
  l1 = ((10**(w+1)-1)-A)/B+1
  T += [(min(l1, L)-l0, A+B*l0, w+1)]

def f(L, w):
  if L==1:
    return 1
  t = f(L/2, 2*w)*(pow(10, w, M) + 1)%M
  if L%2!=0:
    t = (t*pow(10, w, M) + 1)%M
  return t

def g(L, B, w):
  if L==1:
    return 0
  t = (g(L/2, B*2, w*2)*(pow(10, w, M) + 1) + B*f(L/2, w*2))%M
  if L%2!=0:
    t = (t*pow(10, w, M) + B*(L-1))%M
  return t

ans = 0
w = 0
for t in T[::-1]:
  if t[0]>0:
    ans += (t[1]*f(t[0], t[2]) + g(t[0], B, t[2]))*pow(10, w, M)
    ans %= M
  w += t[2]*t[0]
print ans

Submission status: Wrong Answer

Solution code:
# -*- encoding: utf-8 -*-
import sys
import re
import random
import numpy as np
from math import sqrt,log
from fractions import gcd  # python<=3.4
from collections import Counter, defaultdict

# st = Counter(raw_input())

MOD = 1000000007

def ADD(x, y):
    return (x + y) % MOD

def SUB(x, y):
    return (x - y + MOD) % MOD

def MUL(x, y):
    return (x * y) % MOD

def POW(x, e):
    v = 1
    while e > 0:
        if e % 2 == 1:
            v = MUL(v, x)
        x = MUL(x, x)
        e /= 2
    return v

def DIV(x, y):
    return MUL(x, POW(y, MOD-2))



def lcm(a,b):
  return a/gcd(a,b)*b


def solve(l,a,b):
    q = []
    k = 1
    mx = 9
    x = a
    rem = l
    while True:
        # print '// k=%d, mx=%d, x=%d, rem=%d' % (k,mx,x,rem)
        if x <= mx:
            lo = x
            cnt = int((mx - lo)/b) + 1
            if cnt > rem:
                cnt = rem
            hi = lo + b*(cnt-1)
            q.append( (k,cnt,hi,lo) )
            rem -= cnt
            if rem == 0:
                break
            x = hi + b
            # print '// x=%d, mx=%d' % (x, mx)
            assert mx < x
        mx = mx*10 + 9
        k += 1
    q.reverse()
#    print q
    # k,cnt,hi,lo
    ans = 0
    right = 0

    def s(x, n):
        y = DIV(SUB(POW(x, n+1), 1), x-1)
        # print 's(%d, %d) = %d' % (x, n, y)
        return y
    def t(x, n):
        y = SUB(SUB(s(x,n),1), MUL(n, POW(d, n+1)))
        z = DIV(y, SUB(1, d))
        # print 't(%d, %d) = %d' % (x, n, z)
        return z

    G = 1
    for k,cnt,hi,lo in q:
        # print "G=%d k=%d cnt=%d ; %d..%d" % (G,k,cnt,lo,hi)
        d = POW(10, k)
        u = MUL(hi, s(d, cnt-1))
        u = SUB(u, MUL(b, t(d, cnt-1)))
        ans = ADD(ans, MUL(G, u))

        right += k * cnt  # 右の桁数
        G = POW(10, right)

    return ans % MOD

l,a,b,m = map(int, raw_input().rstrip().split())
MOD = m
print solve(l,a,b)

Submission status: Wrong Answer

Solution code:
def inv(x, N):
    def invv(a, n):
        if a == 1: return (1, 0)
        if a == 0: return (-1, -1)
        b, m = a, n
        while m != 0: b,m = m,b%m
        if b > 1: return (-1, -1)
        k = n // a
        y, yy = invv(n % a, a)
        if y < 0: return (-1, -1)
        x = yy - k * y
        while x < 0:
            x += n
            y -= a
        return (x, y)
    return invv(x, N)[0]

L, A, B, M = map(int, input().split())
X = [10**i for i in range(20)]
Y = [max((10**i-A+B-1)//B,0) for i in range(20)]
ans = 0
s = 0
for i in range(19)[::-1]:
    a, b, n = A+B*Y[i], B, max(min(L, Y[i+1])-Y[i], 0)
    a, b = a+b*(n-1), -b
    if n == 0:
        continue
    r = 10**(i+1)
    if M % 9:
        tmp = (((1-r)*a+b*(1-r**(n-1))*r-(1-r)*(a+(n-1)*b)*r**n)*10**s)//((1-r)**2)
    else:
        tmp = (((1-r)*a+b*(1-pow(r,n-1,M))*r-(1-r)*(a+(n-1)*b)*pow(r,n,M))*pow(10,s,M))*inv((1-r)**2,M)%M
    ans = (ans+tmp)%M
    s += (i+1)*n
print(ans)

Submission status: Wrong Answer

Solution code:
def c123(l, n, m):
    p = ((10 ** l) * (10 ** (l * n) - 1) - ((10 ** l) * n) + n) // ((10 ** l - 1) ** 2)
    # p = ((pow(10, l, m) * pow(10, (l * n), m) - 1) - (pow(10, l, m) * n) + n) * (pow(pow(10, l, m) - 1, m - 2, m))
    return p % m

def c111(l, n, m):
    # p = (10 ** (l * n) - 1) // (10 ** l - 1)
    p = (pow(10, l * n, m) - 1) * pow(pow(10, l, m) - 1, m - 2, m)
    return p % m

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

fst = a
lst = a + (l - 1) * b

fst_l, lst_l = len(str(fst)), len(str(lst))
# print(fst, lst)
# print(fst_l, lst_l)

res = 0

# 全ての桁数に対して
digit = 0   # それまでの桁数
for keta in range(fst_l, lst_l + 1):
    q = (10 ** (keta - 1) - 1 - a) // b
    p = (10 ** (keta) - 1 - a) // b
    numb = a + (q + 1) * b
    num = a + p * b
    if num > lst:
        num = lst
    kosuu = (num - numb) // b + 1

    digit = kosuu * keta

    if True or keta != lst_l:
        res *= pow(10, digit, m)

    # かけてからたす
    res += (c123(keta, kosuu - 1, m) * b + c111(keta, kosuu, m) * numb)
    res %= m

    # print(numb, num, digit)
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

l, a, b, m = map(int, input().split())
seq = a + np.arange(5, dtype=int) * b


seq_ = seq.astype(dtype=str)
a = ""
for i in seq_:
  a += i
  
b = int(a)

print(b % 10007)

Submission status: Wrong Answer

Solution code:
def inv(x, N):
    def invv(a, n):
        if a == 1: return (1, 0)
        if a == 0: return (-1, -1)
        b, m = a, n
        while m != 0: b,m = m,b%m
        if b > 1: return (-1, -1)
        k = n // a
        y, yy = invv(n % a, a)
        if y < 0: return (-1, -1)
        x = yy - k * y
        while x < 0:
            x += n
            y -= a
        return (x, y)
    return invv(x, N)[0]

L, A, B, M = map(int, input().split())
X = [10**i for i in range(20)]
Y = [max((10**i-A+B-1)//B,0) for i in range(20)]
ans = 0
s = 0
for i in range(19)[::-1]:
    a, b, n = A+B*Y[i], B, max(min(L, Y[i+1])-Y[i], 0)
    a, b = a+b*(n-1), -b
    if n == 0:
        continue
    r = 10**(i+1)
    tmp = (((1-r)*a+b*(1-pow(r,n-1,M))*r-(1-r)*(a+(n-1)*b)*pow(r,n,M))*pow(10,s,M))*inv((1-r)**2,M)%M
    ans = (ans+tmp)%M
    s += (i+1)*n
print(ans)

Submission status: Wrong Answer

Solution code:
inp = input().split()
L = int(inp.pop(0))
A = int(inp.pop(0))
B = int(inp.pop(0))
M = int(inp.pop(0))

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
data = stdin.readline().split()
L, A, B, M = int(data[0]), int(data[1]), int(data[2]), int(data[3])
S = ''
for i in range(L):
    S += str(A + B * i)
print(S)
print(int(S) % M)