ZhenlongDai/ACPR huggingface dataset:
Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)
else: num += cnt 

S = S + S
now,cnt,num2 = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num2 += cnt
        cnt = 1
        now = i
if S[-1] == now:
    num2 += (cnt+1)
else: num2 += cnt

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Failing test case input:
enomntppfdntedecpfemredn
1360361

Expected output:
1360361


Actual output:
32648663


Fixed submission (verdict: Accepted):
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)
else: num += cnt

S = S + S
now,cnt,num2 = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num2 += cnt
        cnt = 1
        now = i
if S[-1] == now:
    num2 += (cnt+1)
else: num2 += cnt

length = len(S)
if len(set(S)) == 1 and length%2 == 1:
    print(length
    exit()

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())

groups = []
char = ''
i=0
j=0
while i<len(S) and j<len(S)-1:
    j+=1
    if S[i]==S[j]:
        char = S[i]
    else:
        if j==i+1:
            i=j
            continue
        groups.append((i,j-1))
        char=''
        i = j
if j!=i:
    groups.append((i,j))

ans = 0
temp = 0
temp2 = []
for j in range(len(groups)):
    first = groups[j][0]
    end = groups[j][1]
    if first==0 or end==len(S)-1:
        temp += end-first+1
        temp2.append((first,end))
    else:
        ans += (end-first+1)

for j in range(len(temp2)):
    first = temp2[j][0]
    end = temp2[j][1]
    ans += (end-first+1)

ans += temp

print(ans)



Failing test case input:
rr
552

Expected output:
552


Actual output:
4


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

ccc = 0
for i in range(len(S)):
    if S[i]==S[0]:
        ccc+=1

if ccc==len(S):
    print(len(S)*K
    exit()

groups = []
char = ''
i=0
j=0
while i<len(S) and j<len(S)-1:
    j+=1
    if S[i]==S[j]:
        char = S[i]
    else:
        if j==i+1:
            i=j
            continue
        groups.append((i,j-1))
        char=''
        i = j
if j!=i:
    groups.append((i,j))



if i==j and i==0:
    print(K
    exit()

ans = 0
temp = 0
temp2 = []
for j in range(len(groups)):
    first = groups[j][0]
    end = groups[j][1]
    if first==0 or end==len(S)-1:
        temp += end-first+1
        temp2.append((first,end))
    else:
        ans += (end-first+1)

if S[0]==S[-1]:
    for j in range(len(temp2)):
        first = temp2[j][0]
        end = temp2[j][1]
        ans += (end-first+1)

    ans += temp
else:
    for j in range(len(temp2)):
        first = temp2[j][0]
        end = temp2[j][1]
        ans += (end-first+1)
print(ans)
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = list(map(str, input().strip()))
k = int(input())

pre = 0
cnt = 1
ans = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        ans += cnt 
        cnt = 1
    pre = i
pcnt = cnt
dcnt = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        dcnt = cnt - pcnt
        break
if dcnt % 2 and pcnt % 2:
    ans += pcnt
    ans = ans * k + k - 1
else:
    ans += pcnt
    ans *= k

print(ans)


Failing test case input:
enomntppfdntedecpedmredn
4578012

Expected output:
4578012


Actual output:
114450300


Fixed submission (verdict: Accepted):
s = list(map(str, input().strip()))
k = int(input())

pre = 0
cnt = 1
ans = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        ans += cnt 
        cnt = 1
    pre = i

ans += cnt

if cnt == len(s):
    print((cnt*k)
    exit(0)
pcnt = cnt
dcnt = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        dcnt = cnt - pcnt
        break

if dcnt % 2 and pcnt % 2:
    ans = ans * k + k - 1
else:
    ans *= k

print(ans)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = list(input())
k = int(input())
count = 0
import copy
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    elif (count + 2) % 5 == 0:
        count = ((count + 2) / 5) * k - 2
    else:
        count = ((count + 4) / 5) * k - 4
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))


Failing test case input:
hssih
3

Expected output:
5


Actual output:
3


Fixed submission (verdict: Accepted):
s = list(input())
k = int(input())
count = 0
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    elif (count + 1) % 5 == 0:
        count = ((count + 1) / 5) * k - 1
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
k = int(input())

ans = 0
rep = 1
for i in range(0, len(s) - 1):
    current = s[i]
    next = s[i+1]
    if current == next:
        rep += 1
    else:
        if rep > 1:
            ans += rep 
            rep = 1

if rep > 1:
    ans += rep 

start_eq_end = 0
if len(s) > 2 and s[0] == s[-1]:
    if rep % 2 != 0:
        start_eq_end = k - 1


print(ans * k + start_eq_end)

Failing test case input:
enomntppfdntededpedmdern
13688836

Expected output:
13688836


Actual output:
27377672


Fixed submission (verdict: Accepted):
s = input()
k = int(input())

ans = 0
rep = 1
first_rep = 1

tmp = None
for i in s:
    if tmp:
        if tmp == i:
            first_rep += 1
        else:
            break
    else:
        tmp = i

if first_rep == len(s):
    print(len(s) * k 
    exit(0)


for i in range(0, len(s) - 1):
    current = s[i]
    next = s[i+1]
    if current == next:
        rep += 1
    else:
        if rep > 1:
            ans += rep 
            rep = 1

if rep > 1:
    ans += rep 

start_eq_end = 0
if len(s) > 2 and s[0] == s[-1] and first_rep % 2 != 0:
    if rep % 2 != 0:
        start_eq_end = k - 1


print(ans * k + start_eq_end)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

s = input()
k = int(input())

def cnt(s):
    if len(s)%2==1:
        res = 0
        for i in range(1,len(s)-1,2):
            if s[i-1]==s[i] or s[i]==s[i+1]:
                res += 1
        return res
    
    res = 0
    for i in range(0,len(s),2):
        if (i>0 and s[i-1]==s[i]) or (i<len(s)-1 and s[i]==s[i+1]):
            res += 1
    return res

print(cnt(s) + (cnt(s+s)-cnt(s))*(k-1))

Failing test case input:
tsedeeeeeeeetnoooooooooc
999993333

Expected output:
7999946664


Actual output:
8999939997


Fixed submission (verdict: Accepted):

from collections import Counter

def count(s):
    cnt = 1
    res = 0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            cnt += 1
        else:
            res += cnt
            cnt = 1
    res += cnt
    return res

s = input()
k = int(input())

c = Counter(s)

if len(c.keys())==1:
    print(len(s)*k
    exit()

if s[0]!=s[-1]:
    print(count(s)*k)
    exit()

cs = count(s)
cs2 = count(s+s)
print((cs2-cs)*(k-1) + cs)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            count += same 
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1:
            count += same 
            bl = True
        else:
            count += same 

    else:
        count += same 

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()


Failing test case input:
dnofoopondntededpedmdest
12075817

Expected output:
12075817


Actual output:
289819608


Fixed submission (verdict: Accepted):

import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k 
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) 
        else:
            
            ans = len(s) * k 
            
            
        print(ans)
        sys.exit()

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same 
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same 
            bl = True
        else:
            count += same 
    else:
        count += same 

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())

a = S[0]
c = 0
g = []

for i in range(len(S)):
    if a == S[i]:
        c += 1
    else:
        g.append(c)
        a = S[i]
        c = 1

g.append(c)

if len(g) == 1:
    c = len(S) * K / 2
elif S[0] == S[-1]:
    c = int(g[0] / 2)
    c += int(g[-1] / 2)

    for i in range(1, len(g) - 1):
        c += int(g[i] / 2) * K
else:
    c = 0
    for i in g:
        c += int(i / 2) * K


print(int(c))



Failing test case input:
hssih
6

Expected output:
11


Actual output:
6


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

a = S[0]
c = 0
g = []

for i in range(len(S)):
    if a == S[i]:
        c += 1
    else:
        g.append(c)
        a = S[i]
        c = 1

g.append(c)

if len(g) == 1:
    c = len(S) * K / 2
elif S[0] == S[-1]:
    c = int(g[0] / 2)
    c += int(g[-1] / 2)
    c += int((g[0]+g[-1]) / 2) * (K-1)

    for i in range(1, len(g) - 1):
        c += int(g[i] / 2) * K
else:
    c = 0
    for i in g:
        c += int(i / 2) * K


print(int(c))
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
k = int(input())
n = len(s)
l = []

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            x += 1
            l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k

print(ans)

Failing test case input:
enomftppfemtrdecpnemeedn
27716

Expected output:
55432


Actual output:
637468


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
n = len(s)
l = []

t = set(list(s))
if len(t) == 1:
    print(n * k 
    exit()

x = 1
for i in range(n):
    if i == n-1:
        l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math
s = list(input())
ss = s[:]
k = int(input())
cnt = 0
if len(set(s)) == 1:
    cnt = math.floor(len(s) * k / 2)
else:
    cnt = 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
            s[i + 1] = '*'
    cnt *= k
    if s[0] == s[-1]:
        a = 0
        for i in range(len(s) - 1):
            if ss[i] == ss[i + 1]:
                a += 1
        b = 0
        for j in range(len(s), 0, -1):
            if ss[i] == ss[i - 1]:
                b += 1
        cnt -= (k - 1) * (math.floor(a / 2) + math.floor(b / 2) - math.floor((a + b) / 2))
print(cnt)


Failing test case input:
hirsh
6

Expected output:
5


Actual output:
0


Fixed submission (verdict: Accepted):
import math
s = list(input())
ss = s[:]
k = int(input())
cnt = 0

if len(set(s)) == 1:
    cnt = math.floor(len(s) * k / 2)
else:
    cnt = 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
            s[i + 1] = '*'
    cnt *= k
    if ss[0] == ss[-1]:
        a = 1
        for i in range(len(ss) - 1):
            if ss[i] == ss[i + 1]:
                a += 1
            else:
                break
        b = 1
        for j in range(len(ss) - 1, 0, -1):
            if ss[j] == ss[j - 1]:
                b += 1
            else:
                break
        cnt -= (k - 1) * (math.floor(a / 2) + math.floor(b / 2) - math.floor((a + b) / 2))
print(cnt)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = list(input())
K = int(input())



i = 0
sum = 1
total_sum = 0
sum_ketu = 0
j = 0
add_sum = 1
if len(S) == 1:
    print( K -1)
    exit()
    
while(1):
    if i == len(S)-1:
        if S[i] == S[0]:
            while( j <= len(S)-2):
                if S[j] == S[j+1]:
                    add_sum += 1
                    j += 1
                else:
                    break
            if (sum % 2 == 1) and (add_sum % 2 == 1):
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     
     
        sum += 1
     
        i += 1
    else:
        total_sum += sum 
        sum = 1
        i += 1
        
total_sum += sum 



print(total_sum * K + sum_ketu)



Failing test case input:
tsedmeepeedetndoopoofond
30581139

Expected output:
122324556


Actual output:
733947336


Fixed submission (verdict: Accepted):
S = list(input())
K = int(input())

if len(S) == 1:
    print( K 
    exit()

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count
        count = 1
        i += 1
    if i == len(S)-1:
        break

sum += count




j = 0 
a = 1 
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break



m = len(S)-1 
b = 1 
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break




if a == len(S): 
    print((len(S)* K)
elif S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)
else:
    print( sum * K)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = list(map(str, input().strip()))
k = int(input())

pre = 0
cnt = 1
ans = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        ans += cnt 
        cnt = 1
    pre = i

ans += cnt

pcnt = cnt
dcnt = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        dcnt = cnt - pcnt
        break

if len(s) == 1:
    ans = k
elif dcnt % 2 and pcnt % 2:
    ans = ans * k + k - 1
else:
    ans *= k

print(ans)


Failing test case input:
enomntppfdntedecpedmredn
2389896

Expected output:
2389896


Actual output:
59747400


Fixed submission (verdict: Accepted):
s = list(map(str, input().strip()))
k = int(input())

pre = 0
cnt = 1
ans = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        ans += cnt 
        cnt = 1
    pre = i

ans += cnt

if cnt == len(s):
    print((cnt*k)
    exit(0)
pcnt = cnt
dcnt = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        dcnt = cnt - pcnt
        break

if dcnt % 2 and pcnt % 2:
    ans = ans * k + k - 1
else:
    ans *= k

print(ans)
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
S = input()
K = int(input())

sumarr = list()
cnt = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        cnt += 1
    else:
        sumarr.append(cnt)
        cnt = 1
sumarr.append(cnt)

print(sumarr)

if len(sumarr) == 1:
    print(int(len(S) * K / 2))
    exit(0)


def to_cnt(num):
    if num < 2:
        return 0
    else:
        return int(num / 2)


ans = 0
if S[0] == S[len(S) - 1]:
    ans += to_cnt(sumarr[0])
    ans += to_cnt(sumarr[len(sumarr) - 1])
    connection = to_cnt(sumarr[0] + sumarr[len(sumarr) - 1])
    cnt = sum(map(to_cnt, sumarr[1: len(sumarr) - 1]))
    ans += cnt * K
    ans += connection * (K - 1)
else:
    ans += sum(map(to_cnt, sumarr)) * K

print(ans)


Failing test case input:
fnomftppfemurdecpndmeedm
1220

Expected output:
2440


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


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

sumarr = list()
cnt = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        cnt += 1
    else:
        sumarr.append(cnt)
        cnt = 1
sumarr.append(cnt)

if len(sumarr) == 1:
    print(int(len(S) * K / 2))
    exit(0)


def to_cnt(num):
    if num < 2:
        return 0
    else:
        return int(num / 2)


ans = 0
if S[0] == S[len(S) - 1]:
    ans += to_cnt(sumarr[0])
    ans += to_cnt(sumarr[len(sumarr) - 1])
    connection = to_cnt(sumarr[0] + sumarr[len(sumarr) - 1])
    cnt = sum(map(to_cnt, sumarr[1: len(sumarr) - 1]))
    ans += cnt * K
    ans += connection * (K - 1)
else:
    ans += sum(map(to_cnt, sumarr)) * K

print(ans)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S) - 1:
            c = 1
            flag = True
            while S[i] == S[i+1]:
                c += 1
                i += 1
                if i + 1 >= len(S):
                    flag = False
                    break
            i += 1
            if c == 1:
                continue
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                if S[0] == S[-1] and c % 2 == 1:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()

Failing test case input:
hsrih
6

Expected output:
5


Actual output:
0


Fixed submission (verdict: Accepted):
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S):
            c = 1
            flag = False
            if i < len(S) - 1:
                flag = True
                while S[i] == S[i+1]:
                    c += 1
                    i += 1
                    if i + 1 >= len(S):
                        flag = False
                        break
            i += 1
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                u = 0
                a = 0
                while S[u] == S[-1]:
                    u += 1
                    a += 1
                if c % 2 == 0 and a % 2 == 0:
                    continue
                elif c % 2 == 1 and a % 2 == 0:
                    continue
                elif c % 2 == 0 and a % 2 == 1:
                    continue
                else:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from itertools import groupby

def count(s):
    l = []
    c = 0
    for key,value in groupby(s):
        l.append(list(value))
    for i in l:
        c += len(i)
    return c
s = list(input())
k =int(input())
if k <= 10**5:
    print(count(s*k))
else:
    print((count(s*3)-count(s*2))*(k-2) + count(s*2))



Failing test case input:
rq
333

Expected output:
0


Actual output:
666


Fixed submission (verdict: Accepted):
from itertools import groupby

def count(s):
    l = []
    c = 0
    for key,value in groupby(s):
        l.append(list(value))
    for i in l:
        c += len(i)
    return c
s = list(input())
k =int(input())
if k <= 10**5:
    print(count(s*k))
else:
    d2 = count(s*3)-count(s*2)
    d3 = count(s*4)-count(s*3)
    if d2 != d3:
        if k%2 == 1:
            print(d2*((k-2)
        else:
            print(d2*((k-2)
    else:
        print(d2*(k-2) + count(s*2))

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



S=one_str()
K=one_int()

before = S[0]
count=1
ranrength = []
for i in S[1:]:
    if before==i:
        count+=1
    else:
        ranrength.append(count)
        count=1
    before=i

ranrength.append(count)

output_count=0
for r in ranrength:
    if r>1:
        output_count += r
output_count *= K

if S[0]==S[-1]:
    head=ranrength[0]
    tail=ranrength[-1]
    
    if head>1 and head%2==1 and tail>1 and tail%2==1:
        output_count += (K-1)
print(output_count)

Failing test case input:
enomftppfentrdecpnemeedn
854447

Expected output:
1708894


Actual output:
3417788


Fixed submission (verdict: Accepted):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



S=one_str()
K=one_int()

before = S[0]
count=1
ranrength = []
for i in S[1:]:
    if before==i:
        count+=1
    else:
        ranrength.append(count)
        count=1
    before=i

ranrength.append(count)

output_count=0
for r in ranrength:
    if r>1:
        output_count += r
output_count *= K

if S[0]==S[-1]:
    head=ranrength[0]
    tail=ranrength[-1]
    
    if head>1 and head%2==1 and tail>1 and tail%2==1:
        output_count += (K-1)

if len(ranrength)==1:
    print(ranrength[0]*K
else:
    print(output_count)
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
S = input()
N = int(input())

cnt = 0
ans = 0

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
        if i == len(S)-2:
            ans += (cnt + 1) 
    else:
        if cnt != 0:
            ans += (cnt + 1) 
        cnt = 0

if S[0] == S[len(S)-1]:
    fcnt = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            fcnt += 1
        else:
            break
    bcnt = 0
    for i in range(len(S)-1, 0, -1):
        if S[i] == S[i - 1]:
            bcnt += 1
        else:
            break
    if fcnt%2 == 0 and bcnt%2 == 0:
        print((ans+1)*N-1)
    else:
        print(ans*N)
else:
    print(ans*N)

Failing test case input:
tsedmeepeedetndooooofond
19284764

Expected output:
77139056


Actual output:
173562876


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

cnt = 0
ans = 0

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
        if i == len(S)-2:
            ans += (cnt + 1) 
    else:
        if cnt != 0:
            ans += (cnt + 1) 
        cnt = 0

if S[0] == S[len(S)-1]:
    fcnt = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            fcnt += 1
        else:
            break
    bcnt = 0
    for i in range(len(S)-1, 0, -1):
        if S[i] == S[i - 1]:
            bcnt += 1
        else:
            break
    if fcnt%2 == 0 and bcnt%2 == 0:
        if fcnt == len(S)-1:
            print((ans+ans+1)*N
        else:
            print((ans+1)*N-1)
    else:
        print(ans*N)
else:
    print(ans*N)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import string


def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 2:
        print(count_changes(s, k))
    else:
        base_count = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, s[-1])
        print(res)


Failing test case input:
hssih
6

Expected output:
11


Actual output:
10


Fixed submission (verdict: Accepted):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if len(set(s)) == 1:
      print((len(s) * k) 
      exit()
    if k <= 10 ** 8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)
Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = ""
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            cnt = 1
            s_temp = s
    ans += cnt 
    ans *= K
    if S[0] == S[-1] and cnt % 2 == 1:
        ans += K - 1
    elif len(S) == 1:
        ans = K 

    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
dnofoopondntededpedmdest
12075817

Expected output:
12075817


Actual output:
289819608


Fixed submission (verdict: Accepted):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt 
    ans *= K
    if len(S) == fcnt:
        ans = (len(S) * K) 
    elif S[0] == S[-1]:
        ans -= (fcnt 

    print(ans)


if __name__ == "__main__":
    main()

Problem: p02891
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


S = input()
origin = S
K = int(input())
ans = 0
S += S[0]
n = len(S)

S = list(S)
l = 0
r = 1
while l < n:
    while r < n and S[l] == S[r]:
        r += 1
    c = r - l
    for i in range(c):
        if i % 2 == 1: 
            S[l + i] = '.'
    ans += c 
    l = r
    r = l + 1
ans *= K
if S[-2] == '.' and origin[-1] != origin[-2]:
    ans -= 1
print(ans)

Failing test case input:
tredmdepdedetndmopnnfond
14180048

Expected output:
14180048


Actual output:
354501200


Fixed submission (verdict: Accepted):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


S = input()
K = int(input())
ans = 0

S = list(S)
def f(T):
    ret = 0
    l = 0
    r = 1
    n = len(T)
    while l < n:
        while r < n and T[l] == T[r]:
            r += 1
        c = r - l
        ret += c 
        l = r
        r = l + 1
    return ret

first = f(S)
second = f(S + S)
diff = second - first

if len(set(S)) == 1:
    print(len(S) * K 
else:
    print(first + diff * (K - 1))
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = 0
      firstChart = S[i-1]
    flag = True
    ans += math.ceil(count/2)*K
    count = 0

ans += math.ceil(count/2)
    
if flag:
  if firstChart == S[len(S)-1]:
    ans += math.ceil((firstCount + count)/2)*(K-1)
else:
  ans += math.ceil(count/2)*(K-1) 
print(ans)

Failing test case input:
hssih
2

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0

if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) 
    ans += math.ceil(count/2)
    ans += math.ceil(firstCount/2)
  else:
    ans += math.ceil(count/2)*K
    ans += math.ceil(firstCount/2)*K
else:
  ans += (count+1)*K
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K
else:
  print(ans)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
k = int(input())
n = len(s)
l = []

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k
if (l[0] + l[-1]) % 2 == 0:
    ans += k-1

print(ans)

Failing test case input:
fnomftppfemurdecpndmeedm
1185

Expected output:
2370


Actual output:
28439


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
n = len(s)
l = []

t = set(list(s))
if len(t) == 1:
    print(n * k 
    exit()

x = 1
for i in range(n):
    if i == n-1:
        l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product








import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    gr = groupby(S)
    for key, group in gr:
        l.append(list(group))
    for i in range(len(l)):
        cnt += len(l[i]) 

    if l[0][0]==l[-1][0]:
        dev = len(l[0]) 
    else:
        dev=0
    print(cnt * K - dev * (K - 1))


resolve()

Failing test case input:
enomntppfdntedecpedmredn
7140207

Expected output:
7140207


Actual output:
171364968


Fixed submission (verdict: Accepted):
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product








import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    if len(S)==1:
        print(K
    else:
        gr = groupby(S)
        for key, group in gr:
            l.append(list(group))
        if len(l)==1:
            print(len(S)*K
        else:
            for i in range(len(l)):
                cnt += len(l[i]) 
                
            if l[0][0] == l[-1][0]:
                dev = len(l[0]) 
            else:
                dev = 0
            print(cnt * K - dev * (K - 1))


resolve()

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())

pre_c = ''
flg = 0
count = 1

f_c = S[0]
f_e = S[-1]
t = []

for c in S:
  if c == pre_c:
    count += 1
  else:
    t.append(count)
    count = 1
  pre_c = c

t.append(count)

count_1 = 0
for i in t:
  count_1 += i 
    
if f_c == f_e:
  t[0] = t[0] + t[-1]
  t.pop(-1)
  
count_2 = 0
for i in t:
  count_2 += i 
  
print(count_1 + count_2 * (K - 1))

Failing test case input:
tsedmdepdedetndoopoofond
6235068

Expected output:
12470136


Actual output:
155876700


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

pre_c = ''
flg = 0
count = 1

f_c = S[0]
f_e = S[-1]
t = []

for c in S:
  if c == pre_c:
    count += 1
  elif pre_c == '':
    pass
  else:
    t.append(count)
    count = 1
  pre_c = c

t.append(count)
count_1 = 0
for i in t:
  count_1 += i 
  
if f_c == f_e and len(t) != 1:
  t[0] = t[0] + t[-1]
  t.pop(-1)

count_2 = 0
for i in t:
  count_2 += i 
  
if len(S) == 1:
  print(K 
elif len(t) == 1:
  print(K * len(S) 
else:
  print(count_1 + count_2 * (K - 1))
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



S=one_str()
K=one_int()

before = S[0]
count=1
ranrength = []
for i in S[1:]:
    if before==i:
        count+=1
    else:
        ranrength.append(count)
        count=1
    before=i

ranrength.append(count)

output_count=0
for r in ranrength:
    if r>1:
        output_count += r
output_count *= K

if S[0]==S[-1]:
    head=ranrength[0]
    tail=ranrength[-1]
    
    if head>1 and head%2==1 and tail>1 and tail%2==1:
        output_count += 1
print(output_count)

Failing test case input:
tredmdepdedetndmopnnfond
5168106

Expected output:
5168106


Actual output:
10336212


Fixed submission (verdict: Accepted):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



S=one_str()
K=one_int()

before = S[0]
count=1
ranrength = []
for i in S[1:]:
    if before==i:
        count+=1
    else:
        ranrength.append(count)
        count=1
    before=i

ranrength.append(count)

output_count=0
for r in ranrength:
    if r>1:
        output_count += r
output_count *= K

if S[0]==S[-1]:
    head=ranrength[0]
    tail=ranrength[-1]
    
    if head>1 and head%2==1 and tail>1 and tail%2==1:
        output_count += (K-1)

if len(ranrength)==1:
    print(ranrength[0]*K
else:
    print(output_count)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
n = len(s)
K = int(input())
L = 2
t = list(s*L)
s = list(s)
m = len(t)
cnt = 0
c = 0
for i in range(1, m):
    if t[i] == t[i-1]:
        t[i] = 'A'
        cnt+=1
for i in range(1, n):
    if s[i] == s[i-1]:
        s[i] = 'A'
        c+=1
res = c+(cnt-c)
if n == 1:
    res = K
print(res)

Failing test case input:
enomftppfemtrdecpnemeedn
102348

Expected output:
204696


Actual output:
4


Fixed submission (verdict: Accepted):

s = input()
check = False
if len(set(s)) == 1:
    check = True
n = len(s)
K = int(input())
s = s*2
s = list(s)
cnt = 0
for i in range(1, len(s)):
    if s[i] == s[i-1]:
        s[i] = 'A'
        cnt += 1
res = 0
if cnt % 2 == 1:
    res = (cnt
else:
    res = cnt
if check:
    res = n * K 
if n == 1:
    res = K
print(res)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]
    print(int(c*k))
elif d == 1:
    print(int(d*k))
else:
    for i in range(1,d-1):
        c += a[i]
    c += (k-1)*((a[0]+a[d-1])/2) + a[0]/2 + a[d-1]/2
    print(int(c))


Failing test case input:
issii
2

Expected output:
4

Actual output:
5


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k
else:
    for i in range(d):
        c += a[i]
    c = c*k - (k-1)*(a[0]
    print(int(c))

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import string


def count_changes(s, k, pool, prev=None):
    t = s * k
    count = 0
    i = 0
    for cur in t:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        if rest:
            print(0)
        else:
            print(base_count * d)


Failing test case input:
fnomftppfemurdecpndmeedm
1085

Expected output:
2170


Actual output:
0


Fixed submission (verdict: Accepted):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if len(set(s)) == 1:
      print((len(s) * k) 
      exit()
    if k <= 10 ** 8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)
Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt 
    ans *= K
    if S[0] == S[-1] and cnt % 2 == 1 and fcnt % 2 == 1:
        ans += K - 1
    if len(S) == 1:
        ans = (K + 1) 

    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
hirhs
6

Expected output:
0


Actual output:
30


Fixed submission (verdict: Accepted):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt 
    ans *= K
    if len(S) == fcnt:
        ans = (len(S) * K) 
    elif S[0] == S[-1]:
        ans -= (fcnt 

    print(ans)


if __name__ == "__main__":
    main()

Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt 
    ans *= K
    if S[0] == S[-1] and cnt % 2 == 1 and fcnt % 2 == 1:
        ans += K - 1
    if len(S) == 1:
        ans = K 

    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
tredmdepdedetndmopnnfond
14886473

Expected output:
14886473


Actual output:
357275352


Fixed submission (verdict: Accepted):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt 
    ans *= K
    if len(S) == fcnt:
        ans = (len(S) * K) 
    elif S[0] == S[-1]:
        ans -= (fcnt 

    print(ans)


if __name__ == "__main__":
    main()
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
n = int(input())

ans = 0

count = 1
past = ''

for i in s:
    if i == past:
        count += 1
    else:
        ans += count 
        count = 1
    past = i

ans += count 
ans *= n

if s[0] == s[-1]:
    past = s[0]
    idx = 1
    begin = 1

    while idx < len(s) and past == s[idx] :
        begin += 1
        idx += 1

    past = s[-1]
    idx = -1
    end = 1

    while idx > 0 and past == s[idx] :
        end += 1
        idx -= 1

    if begin %2 != 0 and count %2 != 0:
        ans += n - 1

print(ans)


Failing test case input:
mdeemdnpcedrumefpptfmonf
313

Expected output:
626


Actual output:
7825


Fixed submission (verdict: Accepted):
s = input()
n = int(input())

ans = 0

count = 1
past = ''

for i in s:
    if i == past:
        count += 1
    else:
        ans += count 
        count = 1
    past = i

if count == len(s):
    print((len(s)*n)
    exit()

ans += count 
ans *= n

if s[0] == s[-1]:
    past = s[0]
    idx = 1
    begin = 1

    while idx < len(s) and past == s[idx] :
        begin += 1
        idx += 1
    past = s[-1]
    idx = -1
    end = 1

    while idx > 0 and past == s[idx] :
        end += 1
        idx -= 1

    if begin %2 != 0 and count %2 != 0:
        ans += n - 1

print(ans)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
k = int(input())

prev = ""
count1 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count1 += 1
        flag = 1
    prev = i

prev = s[-1]
count2 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count2 += 1
        flag = 1
    prev = i

ret = count1 + (k-1) * count2

print(ret)

Failing test case input:
issii
2

Expected output:
4

Actual output:
5


Fixed submission (verdict: Accepted):
s = input()
k = int(input())

prev = ""
count1 = 0
count2 = 0
count3 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count1 += 1
        flag = 1
    prev = i



for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count2 += 1
        flag = 1
    prev = i

for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count3 += 1
        flag = 1
    prev = i

if k % 2 == 1:
    ret = count1 + (k-1) 
else:
    ret = count1 + ((k-1)

print(ret)
Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
S = input()
K = int(input())

def calc(S):
  ret = 0
  seq = 1
  for i in range(1,len(S)):
    if S[i-1] == S[i]:
      seq += 1
    else:
      ret += seq 
      seq = 1
  ret += seq 
  return ret


if K < 1000:
  print(calc(S*K))
else:
  x1 = calc(S)    
  x2 = calc(S*2)  
  print((x2-x1) * K + 2*x1 - x2)
  


Failing test case input:
tsedeeeeeedetnpoonoonooc
51810827

Expected output:
310864962


Actual output:
1243459848


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

def calc(S):
  ret = 0
  seq = 1
  for i in range(1,len(S)):
    if S[i-1] == S[i]:
      seq += 1
    else:
      ret += seq 
      seq = 1
  ret += seq 
  return ret




if S[0] * len(S) == S:
  
  print((len(S) * K) 
elif K < 1000:
  print(calc(S*K))
else:
  x1 = calc(S)    
  x2 = calc(S*2)  
  print((x2-x1) * K + 2*x1 - x2)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from itertools import groupby
S = list(input())
K = int(input())
newS = list([list(g) for k, g in groupby(S)])
change = 0
for i in range(len(newS)):
    change += len(newS[i])
chain = 0
if len(newS) > 2 and newS[0] == newS[len(newS)-1]:
    chain = 1
print(change*K+chain*(K-1))

Failing test case input:
cooooooooonteeeeeeeedest
1894324309

Expected output:
15154594472


Actual output:
45463783416


Fixed submission (verdict: Accepted):
from itertools import groupby
S = list(input())
K = int(input())
newS = list([list(g) for k, g in groupby(S)])
change = 0
for i in range(len(newS)):
    change += len(newS[i])
chain = 0
if len(newS) == 1:
    print(K*len(S)
else:
    if len(newS) > 2 and newS[0] == newS[len(newS)-1]:
        chain = 1
    print(change*K+chain*(K-1))
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
def get_abc(s1,s2):
    if s1 == 'a':
        if s2 == 'b':
            return 'c'
        else:
            return 'b'
    elif s1 == 'b':
        if s2 == 'a':
            return 'c'
        else:
            return 'a'
    else:
        if s2 == 'a':
            return 'b'
        else:
            return 'a'
        
s_len = len(S)
count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S_list[0])
    pre_character = S_list[i]
if S_list[0] == S_list[-1]:
    print((count+1)*K - 1)
else:
    print(count*K)
print(S_list)

Failing test case input:
enomftppfemtrdecpnemeedn
102348

Expected output:
204696


Actual output:
204696
['e', 'n', 'o', 'm', 'f', 't', 'p', 'a', 'f', 'e', 'm', 't', 'r', 'd', 'e', 'c', 'p', 'n', 'e', 'm', 'e', 'a', 'd', 'n']


Fixed submission (verdict: Accepted):
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def try_count(pre_character):
    S_list=list(S)
    count = 0
    for i in range(s_len):
        if pre_character == S_list[i]:
            count += 1
            if (s_len - 1) != i:
                S_list[i] = '' 
            else:
                S_list[i] = '' 
        pre_character = S_list[i]
    return count,S_list
first_count,fist_list = try_count('')

second_count,second_list = try_count(fist_list[-1])

third_count,third_list = try_count(second_list[-1])


if second_count == third_count:
    print(first_count+ (second_count*(K-1)))
else:
    gusu = K
    kisu = K - gusu
    print((first_count*kisu)+ (second_count*gusu))