Problem: 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.

Submission attempts (in order):
print(1)

from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
    if S[0] == S[-1]:
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+1)//2)
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
    if S[0] == S[-1]:
        ans+=((K-1)*((rev_eq+for_eq)//2))
        """
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+2)//2)
        """
        """
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        """
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if S[0] == S[-1]:
    ans+=((K-1)*((rev_eq+for_eq)//2))
    ans-=((rev_eq//2)*(K-1))
    ans-=((for_eq//2)*(K-1))
    """
    if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
        ans += ((K+1)//2)
    """
if len(set(S))==1:
    ans = (len(S)*K)//2
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted