ZhenlongDai/ACPR huggingface dataset:
Problem: p02954
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S=input()

num={}
for i in range(len(S)):
    num[i]=0

for i in range(len(S)):
    if S[i]=='L' and S[i-1]=='R':
        j=1
        count=1
        while(1):
            if i-j<0:
                break
            if S[i-j]=='R':
                if j%2==0:
                    count+=1
            else:
                break
            j+=1
        num[i]=count

for i in range(len(S)):
    if S[i]=='R' and S[i+1]=='L':
        j=1
        count=1
        while(1):
            if i+j>len(S)-1:
                break
            if S[i+j]=='L':
                if j%2==0:
                    count+=1
            else:
                break
            j+=1
        num[i]=count


for i in range(len(S)):
    print(num[i],end=' ')

print('')

Failing test case input:
RRRLLRLLLRRLLRLL

Expected output:
0 0 3 2 0 2 2 0 0 0 2 2 0 2 1 0


Actual output:
0 0 2 2 0 2 1 0 0 0 2 2 0 2 1 0 


Fixed submission (verdict: Accepted):
S=input()

num={}
for i in range(len(S)):
    num[i]=0

for i in range(1,len(S)):
    even=1
    odd=1
    if S[i]=='L' and S[i-1]=='R':

        j=1
        while(1):
            if i+j>(len(S)-1):
                break
            if S[i+j]=='L':
                if j%2==1:
                    odd+=1
                else:
                    even+=1
            else:
                break
            j+=1
        
        j=1
        while(1):
            if i-1-j<0:
                break
            if S[i-1-j]=='R':
                if j%2==1:
                    even+=1
                else:
                    odd+=1
            else:
                break
            j+=1

        num[i]=even
        num[i-1]=odd

for i in range(len(S)):
    print(num[i],end=' ')
print('')

Problem: p02954
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import math

S = input()

l = [0]
ans = [0]*len(S)

tmp = "R"
for i in range(len(S)):
  if tmp != S[i]:
    l.append(i)
  tmp = S[i]

l.append(len(S))

for i in range(1, len(l), 2):
  ans[l[i]-1] = math.ceil((l[i]-l[i-1])/2) + math.floor((l[i+1]-l[i])/2)
  ans[l[i]] = math.floor((l[i]-l[i-1])/2) + math.ceil((l[i+1]-l[i])/2)

print(ans)

Failing test case input:
RRLLRLLLRRLLRLRL

Expected output:
0 2 2 0 2 2 0 0 0 2 2 0 1 1 1 1


Actual output:
[0, 2, 2, 0, 2, 2, 0, 0, 0, 2, 2, 0, 1, 1, 1, 1]


Fixed submission (verdict: Accepted):
import math

S = input()

l = [0]
ans = [0]*len(S)

tmp = "R"
for i in range(len(S)):
  if tmp != S[i]:
    l.append(i)
  tmp = S[i]

l.append(len(S))

for i in range(1, len(l), 2):
  ans[l[i]-1] = math.ceil((l[i]-l[i-1])/2) + math.floor((l[i+1]-l[i])/2)
  ans[l[i]] = math.floor((l[i]-l[i-1])/2) + math.ceil((l[i+1]-l[i])/2)

print(' '.join(map(str, ans)))
Problem: p02954
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
n = len(s)
rl = 0
lr = 0
count = [0, 0]
ans = [0] * n
prev = s[0]
for i in range(n):
    if s[i] != prev:
        if s[i] == 'L':
            rl = i
            ans[rl - 1] += count[0]
            ans[rl] += count[1]
            count = [0, 0]
            print(ans)
        if s[i] == 'R':
            lr = i
            ans[rl] += count[0]
            ans[rl - 1] += count[1]
            count = [0, 0]
            print(ans)
    if s[i] == 'L':
        count[(i - rl) % 2] += 1
    if s[i] == 'R':
        count[(i - lr) % 2] += 1
    prev = s[i]
    print(count)
ans[rl] += count[0]
ans[rl - 1] += count[1]

print(ans)
print(' '.join(map(lambda x: str(x), ans)))








Failing test case input:
RRRLLLRLLRRLRLLL

Expected output:
0 0 3 3 0 0 2 1 0 0 1 2 2 2 0 0


Actual output:
[1, 0]
[1, 1]
[2, 1]
[0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0]
[1, 1]
[2, 1]
[0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0]
[0, 0, 3, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0]
[1, 1]
[0, 0, 3, 3, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0]
[1, 1]
[0, 0, 3, 3, 0, 0, 2, 1, 0, 0, 1, 1, 0, 0, 0, 0]
[1, 0]
[0, 0, 3, 3, 0, 0, 2, 1, 0, 0, 1, 2, 0, 0, 0, 0]
[1, 0]
[0, 0, 3, 3, 0, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0]
[1, 0]
[1, 1]
[2, 1]
[0, 0, 3, 3, 0, 0, 2, 1, 0, 0, 1, 2, 2, 2, 0, 0]
0 0 3 3 0 0 2 1 0 0 1 2 2 2 0 0


Fixed submission (verdict: Accepted):
s = input()
n = len(s)
rl = 0
lr = 0
count = [0, 0]
ans = [0] * n
prev = s[0]
for i in range(n):
    if s[i] != prev:
        if s[i] == 'L':
            rl = i
            ans[rl - 1] += count[0]
            ans[rl] += count[1]
            count = [0, 0]
            
        if s[i] == 'R':
            lr = i
            ans[rl] += count[0]
            ans[rl - 1] += count[1]
            count = [0, 0]
            
    if s[i] == 'L':
        count[(i - rl) % 2] += 1
    if s[i] == 'R':
        count[(i - lr) % 2] += 1
    prev = s[i]
    
ans[rl] += count[0]
ans[rl - 1] += count[1]

print(' '.join(map(lambda x: str(x), ans)))
Problem: p02954
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
a = []
r_ = 0 
l_ = 0
flag = 0
for i in range(len(S)):
    if flag == 0:
        if S[i]=="R":
            r_ = r_ +1
            continue
        else:
            flag = 1
            l_ = l_ +1
            continue
    if flag == 1:
        if S[i] == "L"   :
            l_ = l_ +1
        else:
            al = l_ + r_
            a.append([al,r_])
            r_ = 1
            l_ = 0 
            flag = 0
        if i == len(S) -1:
            al = l_ + r_
            a.append([al,r_])
 

ans = []
for i in range(len(a)):
    n = a[i][0]
    r = a[i][1]
    odd = n 
    eve = n - odd
    for j in range(r -1):
        ans.append(0)
    ans.append(eve)
    ans.append(odd)
    for j in range(n -r -1):
        ans.append(0)
print(*ans)
    
            

Failing test case input:
RRLLLRLRRLRLLLRL

Expected output:
0 2 3 0 0 1 1 0 1 2 2 2 0 0 1 1


Actual output:
0 0 5 0 0 0 2 0 0 3 0 4 0 0


Fixed submission (verdict: Accepted):
S = input()
a = []
r_ = 0 
l_ = 0
flag = 0
for i in range(len(S)):
    if flag == 0:
        if S[i]=="R":
            r_ = r_ +1
            continue
        elif i != len(S) -1:
            flag = 1
            l_ = l_ +1
            continue
        else:
            l_ = l_ +1
            al = l_ + r_
            a.append([al,r_])
            continue
            
    if flag == 1:
        if S[i] == "L"   :
            l_ = l_ +1
        else:
            al = l_ + r_
            a.append([al,r_])
            r_ = 1
            l_ = 0 
            flag = 0
        if i == len(S) -1:
            al = l_ + r_
            a.append([al,r_])
 

ans = []
for i in range(len(a)):
    n = a[i][0]
    r = a[i][1]
    odd = n 
    eve = n - odd
    for j in range(r -1):
        ans.append(0)
    if r 
        ans.append(eve)
        ans.append(odd)
    else:
        ans.append(odd)
        ans.append(eve)
    for j in range(n -r -1):
        ans.append(0)
print(*ans)

Problem: p02954
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S=input()
L=len(S)
x=[1]*len(S)
for i in range(L-2):
    if S[i] == S[i+1] == "R":
        x[i+2] += x[i]
        x[i]=0
for i in range(L-1,-1,-1):
    if S[i] == S[i-1] =="L":
        x[i-2] += x[i]
        x[i]=0

print(x)

Failing test case input:
RRLLLLLLRRRL

Expected output:
0 4 4 0 0 0 0 0 0 0 2 2


Actual output:
[0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 2, 2]


Fixed submission (verdict: Accepted):
S=input()
L=len(S)
x=[1]*len(S)
for i in range(L-2):
    if S[i] == S[i+1] == "R":
        x[i+2] += x[i]
        x[i]=0
for i in range(L-1,-1,-1):
    if S[i] == S[i-1] =="L":
        x[i-2] += x[i]
        x[i]=0


print(' '.join(map(str, x)))
Problem: p02954
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

import sys

S =  input()
l = len(S)
ans = [0] * l
cot = [0,0]
for i  in range(l):
    if S[i] == "R":
        cot[i%2] += 1
    else:
        ans[i] += cot[i %2 ]
        ans[i - 1 ] += cot[(i + 1)%2]
        cot[0] = cot[1] = 0

cot[0] = cot[1] = 0
for i  in range(l-1,0,-1):
    if S[i] == "L":
        cot[i%2] += 1
    else:
        ans[i] += cot[i %2 ]
        ans[i + 1 ]  += cot[(i + 1)%2]
        cot[0] = cot[1] = 0
print(" ".join(list(map(str,ans))))




Failing test case input:
RLRLLLRRRRLLLRLL

Expected output:
1 1 2 2 0 0 0 0 0 3 4 0 0 2 1 0


Actual output:
1 0 2 2 0 0 0 0 0 3 4 0 0 2 1 0


Fixed submission (verdict: Accepted):

import sys

S =  input()
l = len(S)
ans = [0] * l
cot = [0,0]
for i  in range(l):
    if S[i] == "R":
        cot[i%2] += 1
    else:
        ans[i] += cot[i %2 ]
        ans[i - 1 ] += cot[(i + 1)%2]
        cot[0] = cot[1] = 0

cot[0] = cot[1] = 0
for i  in range(l-1,-1,-1):
    if S[i] == "L":
        cot[i%2] += 1
    else:
        ans[i] += cot[i %2 ]
        ans[i + 1 ]  += cot[(i + 1)%2]
        cot[0] = cot[1] = 0
print(" ".join(list(map(str,ans))))
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))

d=[a]
a.sort()
ans = a[Q-1] - a[0]


while True:
  d_temp = list()
  sub_temp = list()
  for sub in d:
    for c in sub:
      if c != a[0]:
        sub_temp.append(c)
      else:
        d_temp.append(sub_temp)
        sub_temp = list()
    d_temp.append(sub_temp)
    sub_temp = list()
  d = list()
  a = list()

  for sub in d_temp:
    if len(sub) < K:
      continue
    else:
      d.append(sub)
      sub.sort()
      a.extend(sub[0:len(sub)-K+1])
  a.sort()
  if len(a) < Q:
    break
  else:
    ans = min(ans, a[Q-1] - a[0])

print(ans)


Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 291002410 971407775 628894325 210342416 1604024289

Expected output:
246841358


Actual output:
159013528


Fixed submission (verdict: Accepted):
n, k, q = map(int, input().split())
a = list(map(int, input().split()))
 
d = [a]
a = sorted(a)
ans = a[q - 1] - a[0]
 
while True:
    newd = list()
    li = list()
    for sub in d:
        for c in sub:
            if c != a[0]:
                li.append(c)
            else:
                newd.append(li)
                li = list()
        newd.append(li)
        li = list()
 
    d = list()
    a = list()
    for li in newd:
        if len(li) < k:
            continue
        else:
            d.append(li)
            li = sorted(li)
            a.extend(li[0:len(li) - k + 1])
 
    a.sort()
 
    if len(a) >= q:
        ans = min(a[q - 1] - a[0], ans)
    else:
        break
print(ans)
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k, q = map(int, input().split())
a = list(map(int, input().split()))

d = [a]
a = sorted(a)
ans = a[q - 1] - a[0]

while True:
    newd = list()
    li = list()
    for sub in d:
        for c in sub:
            if c != a[0]:
                li.append(c)
            else:
                newd.append(li)
                li = list()
    newd.append(li)

    d = list()
    mi = list()
    for li in newd:
        if len(li) < k:
            continue
        else:
            d.append(li)
            li = sorted(li)
            mi.extend(li[0:len(li) - k + 1])
 
    mi.sort()

    if len(mi) >= q:
        a.pop(0)
        ans = min(mi[q - 1] - mi[0], ans)
    else:
        break
print(ans)

Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 38897442 971407775 628894325 210342416 1604024289

Expected output:
418551909


Actual output:
360986584


Fixed submission (verdict: Accepted):
n, k, q = map(int, input().split())
a = list(map(int, input().split()))

d = [a]
a = sorted(a)
ans = a[q - 1] - a[0]

while True:
    newd = list()
    li = list()
    for sub in d:
        for c in sub:
            if c != a[0]:
                li.append(c)
            else:
                newd.append(li)
                li = list()
        newd.append(li)
        li = list()

    d = list()
    a = list()
    for li in newd:
        if len(li) < k:
            continue
        else:
            d.append(li)
            li = sorted(li)
            a.extend(li[0:len(li) - k + 1])
 
    a.sort()

    if len(a) >= q:
        ans = min(a[q - 1] - a[0], ans)
    else:
        break
print(ans)

Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k,q = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
ans = 10**9
for i in a:
	p,s,f = [],0,1
	for j in range(n):
		if a[j]>=i and f:
			s,f = j,0
		elif a[j]<i and f-1:
			if j-s>=k:
				for j2 in sorted(a[s:j])[:j-s-k+1]: p.append(j2)
			f = 1
	if n-s>=k:
		for j2 in sorted(a[s:n])[:n-s-k+1]: p.append(j2)
	if len(p)>=q: ans = min(ans,sorted(p)[q-1]-i)
print(ans)

Failing test case input:
11 7 5
10227698 831715734 623690081 802524328 190349718 262703497 147009374 971407775 628894325 146780215 88196393

Expected output:
180122020


Actual output:
229159


Fixed submission (verdict: Accepted):
n,k,q = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
ans = 10**9
for i in a:
	p,s,f = [],0,1
	for j in range(n):
		if a[j]>=i and f:
			s,f = j,0
		elif a[j]<i and f-1:
			if j-s>=k:
				for j2 in sorted(a[s:j])[:j-s-k+1]: p.append(j2)
			f = 1
	if f-1 and n-s>=k:
		for j2 in sorted(a[s:n])[:n-s-k+1]: p.append(j2)
	if len(p)>=q: ans = min(ans,sorted(p)[q-1]-i)
print(ans)
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def find_maximum(y):
  C = [[]]
  for a in A:
    if a < y and len(C[-1]) > 0:
      C.append([])
    else:
      C[-1].append(a)
  cand_x = []
  for Ci in C:
    m = len(Ci)
    if m >= K:
      Ci.sort()
      cand_x += Ci[:m-K+1]
  if len(cand_x) >= Q:
    cand_x.sort()
    return cand_x[Q-1], cand_x[0]
  else:
    return float('inf'), 0

ans = float('inf')
for a in A:
  x, y = find_maximum(a)
  ans = min(ans, x-y)

print(ans)

Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 291002410 971407775 628894325 210342416 1604024289

Expected output:
246841358


Actual output:
444901352


Fixed submission (verdict: Accepted):
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def find_maximum(y):
  C = [[]]
  for a in A:
    if a < y:
      if len(C[-1]) > 0:
        C.append([])
    else:
      C[-1].append(a)
  cand_x = []
  for Ci in C:
    m = len(Ci)
    if m >= K:
      Ci.sort()
      cand_x += Ci[:m-K+1]
  if len(cand_x) >= Q:
    cand_x.sort()
    return cand_x[Q-1], cand_x[0]
  else:
    return 10**18, 0

ans = 10**18
for a in A:
  x, y = find_maximum(a)
  ans = min(ans, x-y)

print(ans)

Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n,k,q=list(map(int,input().split()))
a=list(map(int,input().split()))
sorta=sorted(a)
ans=10**9
for i in sorta:
    A=[]
    l=[]
    flg=1
    for j in a:
      if i>j:
        flg*=-1
      if flg==1:
        l.append(j)
      if flg==-1:
        flg=1
        if len(l)<k:
          l=[]
          continue
        else:
          l.sort()
          A=A+l[:len(l)-k+1]
    if l:
      l.sort()
      A=A+l[:len(l)-k+1]
    if len(A)<q:
      continue
    else:
      A.sort()
      ans=min(ans,A[q-1]-A[0])
print(ans)

Failing test case input:
9 1 4
1 1 2 5 5 10 1 21 34 55

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n,k,q=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=10**9
for i in a:
    A=[]
    l=[]
    ln=0
    for j in a+[-1]:
      if i>j:
        if ln-k+1>=0:
          l.sort()
          A+=l[:ln-k+1]
        l=[]
        ln=0
      else:
        l.append(j)
        ln+=1
    if len(A)<q:
      continue
    else:
      A.sort()
      ans=min(ans,A[q-1]-i)
print(ans)
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k,q=map(int,input().split())
arr=list(map(int,input().split()))
arr2=sorted(arr)
ans=10**18
m=arr2[0]
for val in arr:
  tmp=[]
  vals=[]
  for i in range(n):
    if arr[i]>=val:
      tmp.append(arr[i])
    else:
      if len(tmp)>=k:
        tmp=sorted(tmp)
        vals.extend(tmp[:len(tmp)-k+1])
        tmp=[]
  if len(tmp)>=k:
    tmp=sorted(tmp)
    vals.extend(tmp[:len(tmp)-k+1])
  if len(vals)<q:
    continue
  else:
    vals=sorted(vals)
    ans=min(ans,vals[q-1]-vals[0])
print(ans)

Failing test case input:
11 7 2
22030887 861648772 623690081 531780763 476190629 138767190 327456372 934647508 1102097917 269743313 822804784

Expected output:
116736303


Actual output:
57713059


Fixed submission (verdict: Accepted):
n,k,q=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18
for val in arr:
  tmp=[]
  vals=[]
  for i in range(n):
    if arr[i]>=val:
      tmp.append(arr[i])
    else:
      if len(tmp)>=k:
        tmp=sorted(tmp)
        vals.extend(tmp[:len(tmp)-k+1])
      tmp=[]
  if len(tmp)>=k:
    tmp=sorted(tmp)
    vals.extend(tmp[:len(tmp)-k+1])
  if len(vals)<q:
    continue
  else:
    vals=sorted(vals)
    ans=min(ans,vals[q-1]-vals[0])
print(ans)

Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, K, Q = map(int, input().split())
X = list(map(int, input().split()))
Y = sorted(X)





r = Y[Q-1] - Y[0]
for y in Y[1:]:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if tn-K+1 > 0:
                tmp2 += tmp[:tn-K+1]
            tmp = []
            continue
        tmp.append(x)
    tn = len(tmp)
    if tn-K+1 > 0:
        tmp2 += tmp[:tn-K+1]
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)


Failing test case input:
11 7 2
24979445 861648772 623690081 531780763 476190629 138767190 211047202 934647508 1102097917 269743313 822804784

Expected output:
72280012


Actual output:
113787745


Fixed submission (verdict: Accepted):
N, K, Q = map(int, input().split())
X = list(map(int, input().split()))

r = 10**18
for y in X:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if len(tmp) > K-1:
                tmp2 += tmp[:tn-K+1]
            tmp = []
            continue
        tmp.append(x)
    tmp.sort()
    tn = len(tmp)
    if tn-K+1 > 0:
        tmp2 += tmp[:tn-K+1]
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)
Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from heapq import *

n,k,q = map(int, input().split())
a = list(map(int, input().split()))
num_set = set(a)

def parse(b):
    ret = []
    cnt = 0
    tmp = []
    for ai in a:
        if ai >= b:
            tmp.append(ai)
        else:
            if len(tmp) >= k:
                tmp.sort(reverse=True)
                tmp = tmp[k-1:]
                heapify(tmp)
                ret.append(tmp)
                tmp = []
    else:
        if len(tmp) >= k:
            tmp.sort(reverse=True)
            tmp = tmp[k-1:]
            heapify(tmp)
            ret.append(tmp)
    return ret

ans = 10**10

for b in num_set:
    heap = parse(b)
    heapify(heap)
    tmp = []
    i = 0
    while i<q and heap:
        lis = heappop(heap)
        tmp.append(heappop(lis))
        if lis:
            heappush(heap, lis)
        i += 1
    if i == q:
        ans = min(ans, max(tmp)-min(tmp))
print(ans)


Failing test case input:
11 7 2
24979445 1631672198 123301419 1102367299 65734218 58272250 43247513 85567703 628894325 210342416 1039210630

Expected output:
15024737


Actual output:
7461968


Fixed submission (verdict: Accepted):
n,k,q = map(int, input().split())
a = list(map(int, input().split()))
num_set = set(a)

def parse(b):
    ret = []
    tmp = []
    for ai in a:
        if ai >= b:
            tmp.append(ai)
        else:
            if len(tmp) >= k:
                tmp.sort(reverse=True)
                for i in range(k-1,len(tmp)):
                    ret.append(tmp[i])
            tmp = []
    else:
        if len(tmp) >= k:
            tmp.sort(reverse=True)
            for i in range(k-1,len(tmp)):
                ret.append(tmp[i])
            tmp = []
    return ret

ans = 10**10

for b in num_set:
    lis = parse(b)
    if len(lis) < q:
        continue
    ans_tmp = sorted(lis)[q-1] - min(lis)
    ans = min(ans, ans_tmp)

print(ans)
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k, q = map(int, input().split())
a = [int(x) for x in input().split()]
m = None
for x in a:
    b = []
    c = []
    for y in a:
        if y < x:
            if len(c) > k:
                c.sort()
                b.extend(c[:len(c) - k + 1])
            c = []
        else:
            c.append(y)
    if len(c) > k:
        c.sort()
        b.extend(c[:len(c) - k + 1])
    if len(b) >= q:
        b.sort()
        if m is None or m > b[q - 1]: m = b[q - 1] - x
print(m)


Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 291002410 971407775 628894325 210342416 1604024289

Expected output:
246841358


Actual output:
444901352


Fixed submission (verdict: Accepted):
n, k, q = map(int, input().split())
a = [int(x) for x in input().split()]
m = None
for x in a:
    b = []
    c = []
    for y in a:
        if y < x:
            if len(c) >= k:
                c.sort()
                b.extend(c[:len(c) - k + 1])
            c = []
        else:
            c.append(y)
    if len(c) >= k:
        c.sort()
        b.extend(c[:len(c) - k + 1])
    if len(b) >= q:
        b.sort()
        if m is None or m > b[q - 1] - x: m = b[q - 1] - x
print(m)

Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def find_maximum(y):
  C = [[]]
  for a in A:
    if a < y and len(C[-1]) > 0:
      C.append([])
    else:
      C[-1].append(a)
  cand_x = []
  for Ci in C:
    m = len(Ci)
    if m >= K:
      Ci.sort()
      cand_x += Ci[:m-K+1]
  cand_x.sort()
  if len(cand_x) >= Q and cand_x[0] == y:
    return cand_x[Q-1]
  else:
    return float('inf')

ans = float('inf')
for a in A:
  x = find_maximum(a)
  ans = min(ans, x-a)

print(ans)

Failing test case input:
11 4 5
10227698 1631672198 623690081 802524328 575779469 262703497 147009374 971407775 628894325 210342416 520756664

Expected output:
428770095


Actual output:
510528966


Fixed submission (verdict: Accepted):
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def find_maximum(y):
  C = [[]]
  for a in A:
    if a < y:
      if len(C[-1]) > 0:
        C.append([])
    else:
      C[-1].append(a)
  cand_x = []
  for Ci in C:
    m = len(Ci)
    if m >= K:
      Ci.sort()
      cand_x += Ci[:m-K+1]
  if len(cand_x) >= Q:
    cand_x.sort()
    return cand_x[Q-1], cand_x[0]
  else:
    return 10**18, 0

ans = 10**18
for a in A:
  x, y = find_maximum(a)
  ans = min(ans, x-y)

print(ans)
Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
ta = a + []
res = pow(10, 10)
for x in sorted(a):
  
  t = sorted(ta)
  if t[Q - 1] == pow(10, 10): break
  res = min(res, t[Q - 1] - t[0])
  for i in range(N):
    if a[i] == x:
      for j in range(i - K + 1, i + K):
        if j in range(N):
          ta[j] = pow(10, 10)
print(res)

Failing test case input:
11 7 2
24979445 1631672198 623690081 70942633 476190629 262703497 147009374 59168370 628894325 210342416 790610066

Expected output:
11774263


Actual output:
34188925


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
ta = a + []
res = pow(10, 10)
bs = pow(10, 10)
for x in sorted(set(a)):
  t = [[]]
  for i in range(N):
    if ta[i] == bs:
      if t[-1] != []:
        t.append([])
    else: t[-1].append(a[i])
  tt = []
  for i in range(len(t)):
    t[i].sort()
    for j in range(len(t[i]) - K + 1):
      tt.append(t[i][j])
  
  if len(tt) < Q: break
  tt.sort()
  res = min(res, tt[Q - 1] - tt[0])
  
  for i in range(N):
    if a[i] == x:
      ta[i] = bs
  
print(res)

Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N,K,Q = a = map(int,input().split())
a = list(map(int,input().split()))
inf = 10**10
ans = inf
for i in range(N):
    small = a[i]
    seq = []
    seqq = []
    for j in range(N):
        if a[j] < small:
            seq.append(seqq)
            seqq = []
        else:
            seqq.append(a[j])
    seq.append(seqq)
    aaa = []
    for s in seq:
        if len(s)>=K:
            s.sort()
            aaa += s[:len(s)-K+1]
    if len(aaa)>=Q:
        aaa.sort()
        ans = min(ans,aaa[Q-1]-aaa[0])
        print(i,a[i],aaa,ans)
print(ans)

Failing test case input:
11 7 5
24979445 861648772 623690081 433933447 476190629 138767190 211047202 971407775 628894325 731963982 822804784

Expected output:
451211184


Actual output:
0 24979445 [24979445, 138767190, 211047202, 433933447, 476190629] 451211184
451211184


Fixed submission (verdict: Accepted):
N,K,Q = a = map(int,input().split())
a = list(map(int,input().split()))
inf = 10**10
ans = inf
for i in range(N):
    small = a[i]
    seq = []
    seqq = []
    for j in range(N):
        if a[j] < small:
            seq.append(seqq)
            seqq = []
        else:
            seqq.append(a[j])
    seq.append(seqq)
    aaa = []
    for s in seq:
        if len(s)>=K:
            s.sort()
            aaa += s[:len(s)-K+1]
    if len(aaa)>=Q:
        aaa.sort()
        ans = min(ans,aaa[Q-1]-aaa[0])
print(ans)
Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, K, Q = map(int, input().split())
X = list(map(int, input().split()))
Y = sorted(X)





r = Y[Q-1] - Y[0]
for y in Y[1:]:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if tn-K+1 > 0:
                tmp2 += tmp[:tn-K+1]
            tmp = []
            continue
        tmp.append(x)
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)


Failing test case input:
11 7 2
24979445 1631672198 623690081 70942633 476190629 262703497 147009374 85567703 628894325 210342416 520756664

Expected output:
14625070


Actual output:
45963188


Fixed submission (verdict: Accepted):
N, K, Q = map(int, input().split())
X = list(map(int, input().split()))

r = 10**18
for y in X:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if len(tmp) > K-1:
                tmp2 += tmp[:tn-K+1]
            tmp = []
            continue
        tmp.append(x)
    tmp.sort()
    tn = len(tmp)
    if tn-K+1 > 0:
        tmp2 += tmp[:tn-K+1]
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)

Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
res = pow(10, 10)
for x in sorted(a):
  t = sorted(a)
  if t[Q - 1] == pow(10, 10): break
  res = min(res, t[Q - 1] - t[0])
  for i in range(N):
    if a[i] == x:
      for j in range(i - K + 1, i + K):
        if j in range(N):
          a[j] = pow(10, 10)
      break
print(res)

Failing test case input:
11 3 5
24979445 469880797 623690081 433933447 476190629 262703497 147009374 971407775 628894325 210342416 822804784

Expected output:
322871423


Actual output:
329181255


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
ta = a + []
res = pow(10, 10)
bs = pow(10, 10)
for x in sorted(set(a)):
  t = [[]]
  for i in range(N):
    if ta[i] == bs:
      if t[-1] != []:
        t.append([])
    else: t[-1].append(a[i])
  tt = []
  for i in range(len(t)):
    t[i].sort()
    for j in range(len(t[i]) - K + 1):
      tt.append(t[i][j])
  
  if len(tt) < Q: break
  tt.sort()
  res = min(res, tt[Q - 1] - tt[0])
  
  for i in range(N):
    if a[i] == x:
      ta[i] = bs
  
print(res)
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K, Q = [int(i) for i in input().split()]
A = [(int(v),i) for i,v in enumerate(input().split())]
A.sort()
B = [0]*(N)
for i in range(N):
    B[A[i][1]] = i
C = [A[B[i]][0] for i in range(N)]
Candi = set(range(N))
ctr = 0
ans = []
while len(Candi) >= Q:
    ans.append((list(Candi)[0],list(Candi)[Q-1]))
    x = B.index(ctr)
    Candi = Candi - set(B[x-K+1:x+K])
    ctr += 1
print(min([A[j][0]-A[i][0] for i,j in ans]))


Failing test case input:
10 1 4
1 1 2 3 5 10 4 21 34 55

Expected output:
2


Actual output:
-13


Fixed submission (verdict: Accepted):
import bisect
N, K, Q = [int(i) for i in input().split()]
A = [(int(v),i) for i,v in enumerate(input().split())]
A.sort()
B = [0]*(N)
for i in range(N):
    B[A[i][1]] = i
C = [A[B[i]][0] for i in range(N)]
Candi = set(range(N))
ctr = 0
ans = []
rng = [-1,N]
while len(Candi) >= Q:
    T = sorted(Candi)
    ans.append((list(T)[0],list(T)[Q-1]))
    x = B.index(ctr)
    st = bisect.bisect_left(rng, x)
    Candi = Candi - set(sorted(B[rng[st-1]+1:x]+[2002],reverse = 1)[:K])\
    - set(sorted(B[x:rng[st]]+[2002],reverse = 1)[:K])- set([B[x]])
    bisect.insort_left(rng, x)
    ctr += 1
print(min([A[j][0]-A[i][0] for i,j in ans]))

Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
ta = a + []
res = pow(10, 10)
bs = pow(10, 10)
for x in sorted(set(a)):
  t = [[]]
  for i in range(N):
    if ta[i] == bs:
      if t[-1] != []:
        t.append([])
    else: t[-1].append(a[i])
  tt = []
  for i in range(len(t)):
    t[i].sort()
    for j in range(len(t[i]) - K + 1):
      if t[i][j] != bs:
        tt.append(t[i][j])
  if len(tt) < Q: break
  tt.sort()
  res = min(res, tt[Q - 1] - tt[0])
  
  for i in range(N):
    if a[i] == x:
      for j in range(i - K + 1, i + K):
        if j in range(N):
          ta[j] = bs
  
print(res)


Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 147009374 971407775 628894325 210342416 1604024289

Expected output:
329181255


Actual output:
362535481


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
N, K, Q = map(int, input().split())
a = list(map(int, input().split()))
ta = a + []
res = pow(10, 10)
bs = pow(10, 10)
for x in sorted(set(a)):
  t = [[]]
  for i in range(N):
    if ta[i] == bs:
      if t[-1] != []:
        t.append([])
    else: t[-1].append(a[i])
  tt = []
  for i in range(len(t)):
    t[i].sort()
    for j in range(len(t[i]) - K + 1):
      tt.append(t[i][j])
  
  if len(tt) < Q: break
  tt.sort()
  res = min(res, tt[Q - 1] - tt[0])
  
  for i in range(N):
    if a[i] == x:
      ta[i] = bs
  
print(res)
Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import bisect
n,k,q = map(int,input().split())
a = list(map(int,input().split()))
if q == 1:
  print(0)
  exit()
ls = [-1,n]
b = sorted(((a[i],i) for i in range(n)))
ansls = [b[q-1][0]-b[0][0]]
for i,x in enumerate(b):
  ins = bisect.bisect_left(ls,i)
  ls.insert(ins,i)
  space = []
  qc = q-1
  for j in range(1,i+3):
    space.append(ls[j]-ls[j-1]-1)
  for j in range(i+1,n):
    idx = bisect.bisect_left(ls,b[j][1])
    if space[idx-1] < k:
      continue
    qc -= 1
    space[idx-1] -= 1
    if qc == 0:
      ansls.append(b[j][0]-x[0])
      break
print(min(ansls))

Failing test case input:
11 7 2
22030887 861648772 623690081 531780763 476190629 138767190 327456372 934647508 1102097917 269743313 822804784

Expected output:
116736303


Actual output:
57713059


Fixed submission (verdict: Accepted):
import bisect
n,k,q = map(int,input().split())
a = list(map(int,input().split()))
if q == 1:
  print(0)
  exit()
ls = [-1,n]
b = sorted(((a[i],i) for i in range(n)))
ansls = [b[q-1][0]-b[0][0]]
space = [n]
qc = q
for i,x in enumerate(b):
  for j in range(i,n):
    idx = bisect.bisect_left(ls,b[j][1])
    if space[idx-1] < k:
      continue
    qc -= 1
    space[idx-1] -= 1
    if qc == 0:
      ansls.append(b[j][0]-x[0])
      break
  ins = bisect.bisect_left(ls,x[1])
  ls.insert(ins,x[1])
  space = []
  qc = q
  for j in range(1,i+3):
    space.append(ls[j]-ls[j-1]-1)
print(min(ansls))

Problem: p03343
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import bisect
n,k,q = map(int,input().split())
a = list(map(int,input().split()))
if q == 1:
  print(0)
  exit()
ls = [-1,n]
b = sorted(((a[i],i) for i in range(n)))
ansls = [b[q-1][0]-b[0][0]]
space = [n]
qc = q
for i,x in enumerate(b):
  if n-i < k+q-1:
    break
  for j in range(i,n):
    idx = bisect.bisect_left(ls,b[j][1])
    if space[idx-1] < k:
      continue
    qc -= 1
    space[idx-1] -= 1
    if qc == 0:
      ansls.append(b[j][0]-x[0])
      break
  ins = bisect.bisect_left(ls,i)
  ls.insert(ins,x[1])
  space = []
  qc = q
  for j in range(1,i+3):
    space.append(ls[j]-ls[j-1]-1)
print(min(ansls))

Failing test case input:
11 4 5
10227698 1631672198 623690081 802524328 575779469 262703497 147009374 971407775 628894325 210342416 520756664

Expected output:
428770095


Actual output:
366190828


Fixed submission (verdict: Accepted):
import bisect
n,k,q = map(int,input().split())
a = list(map(int,input().split()))
if q == 1:
  print(0)
  exit()
ls = [-1,n]
b = sorted(((a[i],i) for i in range(n)))
ansls = [b[q-1][0]-b[0][0]]
space = [n]
qc = q
for i,x in enumerate(b):
  for j in range(i,n):
    idx = bisect.bisect_left(ls,b[j][1])
    if space[idx-1] < k:
      continue
    qc -= 1
    space[idx-1] -= 1
    if qc == 0:
      ansls.append(b[j][0]-x[0])
      break
  ins = bisect.bisect_left(ls,x[1])
  ls.insert(ins,x[1])
  space = []
  qc = q
  for j in range(1,i+3):
    space.append(ls[j]-ls[j-1]-1)
print(min(ansls))
Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k,q=map(int,input().split())
a=list(map(int,input().split()))
ans=10**10
for x in a:
  b=[[]]
  can=0
  for i in a:
    if i<x:
      if len(b[-1]):b.append([])
    else:b[-1].append(i)
  if len(b[-1])==0:del b[-1]
  for i in range(len(b)):b[i].sort()
  c=[]
  for i in b:
    i.sort()
    if len(i)>=k:c+=i[:len(i)-k+1]
  if len(c)<q:continue
  ans=min(ans,c[q-1]-c[0])
print(ans)

Failing test case input:
11 3 5
24979445 469880797 623690081 509544855 476190629 262703497 38897442 971407775 628894325 210342416 1604024289

Expected output:
418551909


Actual output:
366190828


Fixed submission (verdict: Accepted):
n,k,q=map(int,input().split())
a=list(map(int,input().split()))
ans=10**10
for x in a:
  b=[[]]
  can=0
  for i in a:
    if i<x:
      if len(b[-1]):b.append([])
    else:b[-1].append(i)
  if len(b[-1])==0:del b[-1]
  for i in range(len(b)):b[i].sort()
  c=[]
  for i in b:
    i.sort()
    if len(i)>=k:c+=i[:len(i)-k+1]
  if len(c)<q:continue
  c.sort()
  ans=min(ans,c[q-1]-c[0])
print(ans)

Problem: p03343
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K, Q = [int(i) for i in input().split()]
A = [(int(v),i) for i,v in enumerate(input().split())]
A.sort()
B = [0]*(N)
for i in range(N):
    B[A[i][1]] = i
C = [A[B[i]][0] for i in range(N)]
Candi = set(range(N))
ctr = 0
ans = []
while len(Candi) >= Q:
    T = sorted(Candi)
    ans.append((list(T)[0],list(T)[Q-1]))
    x = B.index(ctr)
    Candi = Candi - set(B[x-K+1:x+K])
    ctr += 1
print(min([A[j][0]-A[i][0] for i,j in ans]))

Failing test case input:
11 4 5
10227698 1631672198 623690081 802524328 575779469 262703497 101880757 971407775 628894325 210342416 520756664

Expected output:
473898712


Actual output:
510528966


Fixed submission (verdict: Accepted):
import bisect
N, K, Q = [int(i) for i in input().split()]
A = [(int(v),i) for i,v in enumerate(input().split())]
A.sort()
B = [0]*(N)
for i in range(N):
    B[A[i][1]] = i
C = [A[B[i]][0] for i in range(N)]
Candi = set(range(N))
ctr = 0
ans = []
rng = [-1,N]
while len(Candi) >= Q:
    T = sorted(Candi)
    ans.append((list(T)[0],list(T)[Q-1]))
    x = B.index(ctr)
    st = bisect.bisect_left(rng, x)
    Candi = Candi - set(sorted(B[rng[st-1]+1:x]+[2002],reverse = 1)[:K])\
    - set(sorted(B[x:rng[st]]+[2002],reverse = 1)[:K])- set([B[x]])
    bisect.insort_left(rng, x)
    ctr += 1
print(min([A[j][0]-A[i][0] for i,j in ans]))
Problem: p03016
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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) 
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)

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

Failing test case input:
3 57716816346744 7711916121742 23520665

Expected output:
22294153


Actual output:
[0, -50004900224993, -50004900224903, -50004900224003, -50004900215003, -50004900125003, -50004899225003, -50004890225003, -50004800225003, -50003900225003, -49994900225003, -49904900225003, -49004900225003, -40004900225003, 638952591813932]
19930756


Fixed submission (verdict: Accepted):
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] = max(0, (10**i -a+b-1) 
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]%m)
Problem: p03016
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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) 
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])

Failing test case input:
4 42030388707156 321306038787033 9413723

Expected output:
6739839


Actual output:
3778830


Fixed submission (verdict: Accepted):
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] = max(0, (10**i -a+b-1) 
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]%m)
Problem: p03016
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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) 
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])

Failing test case input:
15 65472025464171 34316611981321 23143760

Expected output:
15163385


Actual output:
5079378


Fixed submission (verdict: Accepted):
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] = max(0, (10**i -a+b-1) 
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]%m)
Problem: p03016
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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) 
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]%m)

Failing test case input:
1 6082488590097 1634888304420825 26385493

Expected output:
25587258


Actual output:
16971997


Fixed submission (verdict: Accepted):
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] = max(0, (10**i -a+b-1) 
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]%m)
Problem: p03016
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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) 
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])

Failing test case input:
15 21284366351213 247667443425780 8854374

Expected output:
3545109


Actual output:
8604129


Fixed submission (verdict: Accepted):
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] = max(0, (10**i -a+b-1) 
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]%m)