Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Runtime Error

Solution code:
# Author: cr4zjh0bp
# Created: Sat Mar 14 19:55:57 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

s = ns()
k = ni()

c = []
cnt = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    elif cnt > 1:
        c.append(cnt)
        cnt = 1
if cnt > 1:
    c.append(cnt)

itv = 0
if len(s) > 1 and len(c) > 0:
    if s[0] == s[1] and s[-1] == s[-2]:
        itv = sum([x // 2 for x in c[1:-1]])
    elif s[0] == s[1] and s[-1] != s[-2]:
        itv = sum([x // 2 for x in c[1:]])
    elif s[0] != s[1] and s[-1] == s[-2]:
        itv = sum([x // 2 for x in c[:-1]])
    else:
        itv = sum([x // 2 for x in c[:]])

if len(c) == 1 and c[0] == len(s):
    # 全ての要素が連続している
    print(len(s) * k // 2)
elif s[0] != s[-1]:
    # 両端が連続していない
    print(itv * k)
else:
    ans = 0
    # 左端を操作する必要がある
    if s[0] == s[1]:
        ans += c[0] // 2
    # 右端を操作する必要がある
    if s[-1] == s[-2]:
        ans += c[-1] // 2

    # 左端、右端以外
    ans += itv * k

    if len(c) == 0:
        # 左端と右端がどちらも連続していない
        ans += k - 1
    else:
        if s[0] == s[1] and s[-1] == s[-2]:
            # 両端に連続した要素がある
            ans += (c[-1] + c[0]) // 2 * (k - 1)
        elif s[0] == s[1] and s[-1] != s[-2]:
            # 右端に連続した要素がない
            ans += (c[1] + 1) // 2 * (k - 1)
        else:
            # 左端に連続した要素がない
            ans += (c[-1] + 1) // 2 * (k - 1)

    print(ans)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
cnt=0
tmp=1
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        tmp+=1
    else:
        cnt+=tmp//2
        tmp=1
cnt+=tmp//2
if s[0]!=s[1] and s[-1]!=s[-2] and s[0]==s[-1]:
    cnt+=1

print(cnt*k)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

if len(set(s)) == 1:
    ans = len(s) * k //2
else:
    if s[0] != s[-1]:
        i = 0
        cnt = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt += 1
                i += 2
            else:
                i += 1
        ans = cnt * k
    else:
        cnt = 0
        for i in range(len(s) - 1):
            if s[i] == s[i+1]:
                cnt += 1
        tmp = cnt * k

        # # s_head == s_tail
        # s_head = s[0]
        # s_tail = s[-1]
        # a, b = 0, 0
        # for val in s:
        #     if val == s_head:
        #         a += 1
        #     else:
        #         break
        # for val in s[::-1]:
        #     if val == s_tail:
        #         b += 1
        #     else:
        #         break
        
        cnt_s = 0
        i = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt_2 += 1
                i += 2
            else:
                i += 1
        
        s_2 = s * 2
        cnt_s_2 = 0
        i = 0
        while i < 2 * len(s) - 1:
            if s[i] == s[i+1]:
                cnt_s_2 += 1
                i += 2
            else:
                i += 1

        diff = cnt_s_2 - cnt_s
        ans = tmp - diff * (k-1)

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

ans = 0

def f(text):
    ret = 0
    if len(set(text))==1:
        return len(text)//2
    else:
        for i in range(len(text)):
            if i==0:
                n=1
                tmp = text[i]
            elif text[i] == tmp:
                n += 1
            else:
                if n!=1:
                    ret += n//2
                tmp = text[i]
                n = 1
        return ret

if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = f(s)
        print(ans*k)
    else:
        c = s[0]
        a, b = 0,0
        ite = 0
        if len(set(s))!=0:
            while s[a] == c:
                a += 1
            while s[len(s)-b-1] == c:
                b += 1
        s1 = s[:a]
        s2 = s[a:len(s)-b]
        s3 = s[len(s)-b:]
        
        A = f(s1)
        B = f(s3)
        C = f(s1+s3)
        X = f(s2)
        
        print(A+B+X*k+C*(k-1))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += n//2
                tmp = s[i]
                n = 1
        print(ans*k)
        exit()
    
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))>=1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += n//2
                tmp = s[i]
                n = 1
        print(ans*k)
        #exit()
    
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))>=1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += 1
                tmp = s[i]
                n = 1
        print(ans*k)
        exit()
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))==1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    a = s[0]
    b = s[-1]
    
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))==1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    if a!=b:
        print((A+B+X)*k)
    else:
        C = (len(s1+s3))//2
        print(A+B+X*k+C*(k-1))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

count = 0
change = False

for i in range(1, len(s)):
    if s[i-1] == s[i] and not change
        count += 1
        change = True
    else:
        change = False

count2 = 0
change2 = False
for i in range(2, len(s)):
    if s[i-1] == s[i] and not change2:
        count2 += 1
        change2 = True
    else:
        change2 = False


if len(s) == 1:
    print(k//2)
elif s[0] == s[-1] and not change:
    if change2:
        print((count+count2+1)*(k//2)+count*(k%2))
    else:
        print(count+(count2+1)*(k-1))
else:
    print(count*k)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode("utf-8").rstrip()
K = int(readline())

last = ""
cnt = 0
for s in S:
    if last == s:
         += 1
        last = ""
    else:
        last = s
        
print(cnt * K)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = int(readline().decode("utf-8").rstip())
K = int(readline())

last = ""
cnt = 0
for s in S:
    if last == s:
        cnt += 1
        last = ""
    else:
        last = s
        
print(cnt * K)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
S=S*K
cnt=1
s=0
e=s+1
sum=0
while True:
  if e>len(S)-1:
    break    
  if S[s]==S[e]:
    cnt=cnt+1
    e=e+1
  else:
    s=e
    sum+=cnt//2
    e=s+1
    cnt=1
'''
sum=sum*K
if S[0]==S[-1]:
  sum+=K
'''
print(sum)

Submission status: Runtime Error

Solution code:
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)//2
    return c
s = list(input())
k =int(input())
print(count(s*k))

Submission status: Runtime Error

Solution code:
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)//2
    return c


if __name__=="__main__":
    s = list(input())
    k = int(input())
    c = 0
    l = []
    c += count(s*k)
    print(c)

Submission status: Runtime Error

Solution code:
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)//2
    return c


if __name__=="__main__":
    s = list(input())
    k = int(input())
    c = 0
  	c += count(s*k)
    print(c)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = 1
            count += 1
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[0] == S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] == S[-2]:
            S[-1] = 1
            count += 1
        elif S[0] == S[1] and S[-1] == S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        
        
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = 1
                count += 1
        print(K*count)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = 1
            count += 1
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[0] == S[1]:
            S[0] = 1
            count += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = 1
                count += 1
        print(K*count)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if not flag and S[i] == S[i+1]:
            count += 1
            flag = True
        if flag:
            flag = False
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[-1] == S[-2]:
            S = S[:-1]+'X'
            count = 1
        flag = False
        for i in range(len(S)-1):
            if not flag and S[i] == S[i+1]:
                count += 1
                flag = True
            if flag:
                flag = False
        print(K*count)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
cnt=0
same_length=1
for i in range(len(s)-1):
	if s[i]==s[i+1]:
		same_length +=1
	else:
		cnt+=int(same_length//2)
		same_length=1
cnt+=int(same_length//2)
if s[0]==s[-1] and s[-1]!=s[-2] and s[0]!=s[1]:
	cnt+=1
print(cnt*k)

Submission status: Runtime Error

Solution code:
import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

S = input()
S = S+S
K = int(input())
T = list(nchars(S, 2))
ans = 0
if all(s == S[0] for s in S):
  ans = S*K//2
else:
  T = T[:len(T)//2]
  for Tp in T:
    ans += len(Tp)//2
  ans *= K
print(ans)

Submission status: Runtime Error

Solution code:
#AGC-041
S = input()
K = int(input())
ans = 0

check_char = S[0]
repeat = 1

#まずSだけで数える。
for i in range(1, len(S)):
    if S[i] == check_char:
        repeat += 1
    else:
        ans += (repeat//2)
        check_char = S[i]
        repeat = 1
    
ans += (repeat//2)

#K倍を答えとする
if len(S) > 1 and S[0] == S[-1]:
    a, b, i, j = 1, 1, 1, -2
    char = S[0]
    while True:
        if S[i] == char:
            a += 1
            i += 1
        else:
            break
    char = S[-1]
    while True:
        if S[j] == char:
            b += 1
            j -= 1
        else:
            break
    minus = (K-1) * (a//2 + b//2 - (a+b)//2)
    print(ans * K - minus)
else:
    print(ans * K)

Submission status: Runtime Error

Solution code:
#AGC-041
S = input()
K = int(input())
ans = 0

check_char = S[0]
repeat = 1

#まずSだけで数える。
for i in range(1, len(S)):
    if S[i] == check_char:
        repeat += 1
    else:
        ans += (repeat//2)
        check_char = S[i]
        repeat = 1
    
ans += (repeat//2)

#K倍を答えとする
if S[0] == S[-1]:
    a, b, i, j = 1, 1, 1, -2
    char = S[0]
    while True:
        if S[i] == char:
            a += 1
            i += 1
        else:
            break
    char = S[-1]
    while True:
        if S[j] == char:
            b += 1
            j -= 1
        else:
            break
    minus = (K-1) * (a//2 + b//2 - (a+b)//2)
    print(ans * K - minus)
else:
    print(ans * K)

Submission status: Runtime Error

Solution code:
#AGC-041
S = input()
K = int(input())
T = S * K
ans = 0

check_char = T[0]
repeat = 1
for i in range(1, len(T)):
    if T[i] == check_char:
        repeat += 1
    else:
        ans += (repeat//2)
        check_char = T[i]
        repeat = 1

ans += (repeat//2)
print(ans)

Submission status: Runtime Error

Solution code:
#AGC-041
S = input()
K = int(input())
T = S * K
ans = 0

check_char = T[0]
repeat = 1
for i in range(1, len(T)):
    if T[i] == check_char:
        repeat += 1
    else:
        ans += (repeat//2)
        check_char = T[i]
        repeat = 1
#     print("repeat",repeat,"i", i, "ans", ans)

ans += (repeat//2)
print(ans)

Submission status: Runtime Error

Solution code:
#coding:utf-8
s = input()
k = int(input())


if len(set(s))==1:
    print ((len(s)*k)//2)
    exit()

old = ""
ans = 0
for i in range(len(s)):
    if(s[i]==old):
        ans+=1
        old = ""
    else:
        old = s[i]



head = 0
for i in range(len(s)):
    if head==0 or s[i]==s[i-1]:
        head+=1
    else:
        break

tail = 0
t = s[::-1]
for i in range(len(t)):
    if tail==0 or t[i]==t[i-1]:
        tail+=1
    else:
        break

print (ans*k-(k-1)*(head//2)+tail//2)-((head+tail)//2)))

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())

if list(S).count(S[0])==len(S):
    print(len(S)*K//2)

else:
    S_count=0
    i=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            S_count+=1
            i+=1
        i+=1

    if S[0]==S[-1]:
        a=0
        i=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                a+=1
                i+=1
            else:
                break
        b=0
        i=len(S)-1
        while i>0:
            if S[i]==S[i-1]:
                b+=1
                i+=-1
            else:
                break

        S_count*=K
        S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)
        print(unko)
    else:
        S_count*=K
    print(S_count)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())

t = s * k

count = 0

for i in range(len(t)-1):
  if t[i] == t[i+1]:
    count += 1
    
for i in range(len(t)-2):
  if t[i] == t[i+1] == t[i+2]:
    count -= 1

print(count)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
my_bool=True
cnt=0
for i in range(1,len(s)):
    if my_bool:
        if s[i-1]==s[i]:
            cnt+=1
            my_bool=False
    else:
        my_bool=True
        continue
if my_bool:
    if not s[-1]==s[-2] and s[0]==s[-1]:
        cnt+=1
print(cnt*n)

Submission status: Runtime Error

Solution code:
# 2019-11-25 06:26:24(JST)
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:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
str = str(input())
n = int(input())

seperation = []
tmp = ""
for x in range(len(str)-1):
  tmp += str[x]
  if str[x] == str[x+1]:
    if tmp == "":
      tmp += str[x]
  else:
    seperation.append(tmp)
    tmp = ""
tmp += str[-1]
seperation.append(tmp)

total = 0

# print(seperation)

if len(seperation) == 1:
  if (len(seperation[0]) == 1):
    return n//2

if (str[0] == str[-1]) and n>1 and len(seperation)>1:
  a = len(seperation[0])//2
  b = len(seperation[-1])//2
  # print("a,b:",a,b)
  total += a
  total += b
  seperation[0] += seperation[-1]
  a = seperation[0]
  print(a,len(a),len(a)//2, n-1)
  c = (len(a)//2)*(n-1)
  # print("c:",c)
  total += c
  seperation = seperation[1:-1]

# print(total)
# print(seperation)

for x in seperation:
  total += (len(x)//2)*n

print(total)

Submission status: Runtime Error

Solution code:
s=input()
t=int(input())
s=s*t
a=0
s=list(s)
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        s[i]='*'
        #print(i,s[i],s)
        a+=1
print(a)

Submission status: Runtime Error

Solution code:
s = (input())
k = int(input())

s *= k 
ans = 0
#print(s,k)
s_m = s[0]  
for i in s[1:]:
#  print(i,s_m)
  if i == s_m:
    ans += 1
    s_m = '0'
  else:
    s_m = i  

print(ans)

Submission status: Runtime Error

Solution code:
s = (input())
k = int(input())

s *= k 
ans = 0
#print(s,k)
s_m = s[0]  
for i in s[1:]:
#  print(i,s_m)
  if i == s_m:
    ans += 1
    s_m = 0
  else:
    s_m = i  

print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())
n = int(input())

result =0
i =0

while i<len(s)-1:
  if s[i]==s[i+1]:
    result += 1
    i+=1
  i+=1

result=result*n
    
if s[0]==s[len(s)-1] and s[0] != s[1] and s[len(s)-1] != s[len(s)-2] :
  result = result-(n-1)
  
print(result)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())

def f(s):
  i=0
  c=0
  n=len(s)
  if n==1:
    return 0
  else:
    while i<n:
      if s[i]==s[i+1]:
        c+=1
        i+=2
      else:
        i+=1
    return c

a=0
while S[0]==S[a+1]:
  a+=1
  
b=-1
while S[0]==S[b]:
  b-=1
  
if b==-1:
  ans=f(S)*K
elif a==0 and b!=-1:
  ans=(K-1)*(-b-1)//2+K*f(S[1:b+1])
elif a!=0 and b!=-1:
  ans=(a+1)//2+f(S[a+1:b+1])*K+((a-b)//2)*(K-1)+(-b-1)//2
  
print(ans)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())

i=0
c=0
while i<len(S)-1:
  if S[i]==S[i+1]:
    c+=1
    i+=2
  else:
    i+=1
    
if S[0]==S[-1] and S[-2]!=S[-1]:
  print((c+1)*K-1)
else:
  print(c*K)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=set(s)
if len(a)==1:print(k*len(s)//2)
else:
  V=[]
  unit=0
  for i in s:
	if len(V)==0 or V[-1][0]!=i:
		V.append([i,1])
	else:
		V[-1][1] += 1
  for i in V:
    unit+=i[1]//2
  ans=unit*k
  if s[0]==s[-1]:
    ans=ans-(V[0][1]//2+V[-1][1]//2-(V[0][1]+V[-1][1])//2)*(k-1)
  print(ans)

Submission status: Runtime Error

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W, K = map(int, input().split())
Upper = H//2 if H % 2 == 0 else H//2+1
for h in range(Upper):
    w = (K-W*h)/(H-2*h)
    if int(w) == w and 0 <= w <= W:
        print("Yes")
        exit()
print("No")

Submission status: Runtime Error

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[0].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[1].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=input()
K=int(input())

T=list(S)
cnt=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]=5
        cnt+=1

T2=list(S)
cnt2=2
if T2[0]==T2[-1]:
    i=1
    while T2[i]==T2[0]:
        cnt2+=1
        i+=1
    i=-2
    T2[-2]
    while T2[i]==T2[0]:
        cnt2+=1
        i-=1
    
if cnt2%2==0:
    print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Runtime Error

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[0].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[1].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=input()
K=int(input())

T=list(S)
cnt=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]=5
        cnt+=1

T2=list(S)
if T2[0]==T2[-1]:
    cnt2=2
    i=1
    while T2[i]==T2[0]:
        cnt2+=1
        i+=1
    i=-2
    T2[-2]
    while T2[i]==T2[0]:
        cnt2+=1
        i-=1
    
if cnt2%2==0:
    print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Runtime Error

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[0].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[1].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=input()
K=int(input())

T=list(S)

cnt=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]=5
        cnt+=1

if T[0]==T[-1]:
    if T[0]!=T[1] and T[-1]!=T[-2]:
        cnt+=1
        print(cnt*K-1)
else:
    print(cnt*K)

Submission status: Runtime Error

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[0].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[1].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=input()
K=int(input())

T=list(S)

cnt=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]=5
        cnt+=1

if T[0]==T[-1]:
    if T[0]!=T[1] and T[-1]!=T[-2]:
        cnt+=1
print(cnt*K)

Submission status: Runtime Error

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[0].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[1].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=input()
K=int(input())

T=list(S)

cnt=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]=5
        cnt+=1

if T[0]==T[-1]:
    if T[0]!=T[1] and T[-1]!=T[-2]:
        cnt+=1
print(cnt*K)

Submission status: Runtime Error

Solution code:
S = input()
S2 = S + S
K = int(input())
n = len(S2)
i = 0
s = 0
while i < n-1 :
    i += 1
    x = S2[i]
    y = S2[i+1]
    if x == y:
        s += 1
        i += 1
p = s//2
q = s-p
ans = p + q*(K-1)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

_s = input()[:-1]
_k = int(input())


def chker(S, K):
    s3 = S * 3
    for i in range(1, len(s3)):
        if s3[i - 1] == s3[i]:
            s3[i] = '0'

    ls = len(S)
    cnt_0 = s3[:ls].count('0')
    cnt_1 = s3[ls:2 * ls].count('0')
    cnt_2 = s3[2 * ls:3 * ls].count('0')

    print(cnt_0 + cnt_1 * (K // 2) * cnt_2 * ((K - 1) // 2))


chker(_s, _k)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=S*K
c=''
t=0
for s in T:
    if s==c:
        t+=1
    c=s
print(t)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 0
if len(s) == 1:
    ans = k//2
else:
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt += 1
        else:
            ans += ((cnt+1)//2)*k
            cnt = 0
    ans += ((cnt+1)//2)*k
    if s[0] == s[-1]:
        if s[0] != s[1] and s[-1] != s[-2]:
            ans += k - 1
        if len(s) > 2 and s[0] == s[1] == s[2] or s[-1] == s[-2] == s[-3]:
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define downque(que)  priority_queue<ll> que;
#define upque(que) priority_queue<ll, vector<int>, greater<int>> que;
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;


int main(){
  string s;  cin>>s;
  ll k; cin>>k;
  int n=s.size();
  string t=s+s+s+s;
  ll c1=0,c2=0,c3=0,c4=0;
  
  
  FOR(i,1,t.size()){
      if(t[i]==t[i-1]){
        t[i]='A';
        if(i<n)c1++;
        else if(i<n*2)c2++;
        else if(i<n*3)c3++;
        else c4++;
      }
  }

  ll res;
  if(k==1)res=c1;
  else if(k==2)res=c1+c2;
  else if(k%2==0)res=c1+(c2+c3)*((k-2)/2)+c4;
  else if(k%2==1)res=c1+(c2+c3)*((k-1)/2);
  
  cout<<res<<endl;


}

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
flag=True
co=1
while flag==True:
    S+=S
    co+=1
    if co==K:
        flag=False
S=list(S)
t=[S[0]]
for i in range(1,len(S)):
    if S[i]!=t[-1]:
        t.append(S[i])
    else:
        t.append(":")
print(t.count(":"

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
flag=True
co=1
while flag==True:
    S+=S
    co+=1
    if co==K:
        flag=False
S=list(S)
t=["i"]
for i in range(1,len(S)):
    if S[i]!=t[-1]:
        t.append(S[i])
    else:
        t.append(":")
print(t.count(":"))

Submission status: Runtime Error

Solution code:
S=list(input())
K=int(input())
SS=S*2
F=S[1]

if len(S)==1:
	P=int(K//2)
elif S==[F]*len(S):
	P=int((len(S))*K//2)
else:
	for G in range(1,len(S)):
		if S[G-1]==S[G]:
			S[G]="O"
		else:
			continue
	A=S.count("O")
	for J in range(1,len(SS)):
		if SS[J-1]==SS[J]:
			SS[J]="O"
		else:
			continue
	B=SS.count("O")
	P=A+(B-A)*(K-1)
print(P)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
for i in range(len(S)-1):
  if S[i] != S[i+1]:
    J = False
S.append('end')
if S[0] != S[len(S)-2]:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif J:
  print((len(S)-1)*K//2)
else:
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)-1])*(K-1)//2) + length[0]//2 + length[len(length)-1]//2
  length[0] = 0
  length[len(length)-1] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)-2] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)