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

Solution (Python):

import sys

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

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            if k % 2 == 0:
                ans = len(s) * (k // 2)
            else:
                ans = len(s) * (k // 2) + len(s) // 2
        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 // 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()

Solution (Python):

import sys

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

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            
            ans = len(s) * k // 2
            
            
        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 // 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()

Solution (Python):
S_input = list(input())
K = int(input())
 

def change_count(str_list):
    answer = 0
    for i in range(1,len(str_list)):
        if str_list[i] == str_list[i-1]:
            answer += 1
            str_list[i] = i
    return answer
 
count = 0
 

if K == 1:
    count += change_count(S_input)
    print(count)
    exit()
 

if len(set(S_input)) == 1:
    count += (len(S_input)*K) // 2
    print(count)
    exit()
 

SS = S_input + S_input
count += change_count(SS)

if K == 2:
    print(count)
    exit()
 

SSS = S_input + S_input + S_input
change = change_count(SSS) - count
 

count += change * (K-2)    
 
print(count)

Solution (Python):
s=str(input())
k=int(input())
n=len(s)
if s[0]!=s[-1] or k==1:
    ans=0
    a=0
    c='0'
    for i in range(n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ans+=-(-a//2)
            a=0
    ans+=-(-a//2)
    print(ans*k)
elif n==1:
    print(k//2)
elif len(set(s))==1:
    print(k*len(s)//2)
else:
    ans=0
    a=0
    c='0'
    for i in range(n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ans+=-(-a//2)
            a=0
    ans+=-(-a//2)
    ANS=0
    a=0
    c='0'
    s+=s
    for i in range(2*n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ANS+=-(-a//2)
            a=0
    ANS+=-(-a//2)
    print(ans+(ANS-ans)*(k-1))

Solution (Python):
s = str(input())
k = int(input())

def main(s, k):
  separation = []
  tmp = ""
  for x in range(len(s)-1):
    tmp += s[x]
    if s[x] == s[x+1]:
      if tmp == "":
        tmp += s[x]
    else:
      separation.append(tmp)
      tmp = ""
  tmp += s[-1]
  separation.append(tmp)

  total = 0

  if len(separation) == 1:
    if (len(separation[0]) == 1):
      return k//2
    else:
      return (len(separation[0])*k)//2

  if (s[0] == s[-1]) and k>1 and len(separation)>1:
    a = len(separation[0])//2
    b = len(separation[-1])//2
    total += a
    total += b
    separation[0] += separation[-1]
    a = separation[0]
    c = (len(a)//2)*(k-1)
    total += c
    separation = separation[1:-1]

  

  for x in separation:
    total += (len(x)//2)*k

  return total

print(main(s,k))

Solution (Python):
s = list(input())
k = int(input())
first = s[0]
last = s[-1]
first_num = 0
last_num = 0
now_num = 0
now = s[0]
ans = 0
for i in s:
  if i == first:
    first_num += 1
  else:
    break
for i in reversed(s):
  if i != last:
    break
  last_num += 1
for i in s:
  if i == now:
    now_num += 1
  else:
    ans += now_num // 2
    now_num = 1
    now = i
ans += now_num // 2
ans *= k
if first == last:
  if first_num % 2 and last_num % 2:
    ans += k - 1
  if len(set(s)) == 1:
    ans = (first_num * k) // 2 
print(ans)

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

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

a=0
n=len(S)
while a<n-1 and S[0]==S[a+1]:
  a+=1
  
b=-1
while a<n-1 and S[0]==S[b]:
  b-=1
  
if a==n-1:
  ans=n*K//2
elif b==-1:
  ans=f(S)*K
elif a==0 and b!=-1:
  ans=(K-1)*((-b-1)//2)+K*f(S[1:b+1])+(-b-1)//2
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)

Solution (Python):
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)

Solution (Python):
s=input()
t=int(input())
a=len(s)
b=0
i=0
while i<a-1:
  if s[i]==s[i+1]:
    b=b+1
    i+=2
  else:
    i+=1
if s[0]!=s[a-1]:
  print(b*t)
else:
  d=1
  i=0
  while i<a-1:
    if s[i]==s[i+1]:
      d+=1 
      i=i+1
    else:
      break
  e=1
  i=0
  while i<a-1:
    if s[a-1-i]==s[a-1-i-1]:
      e+=1
      i=i+1
    else:
      break
  if d+e==a*2:
    print(a*t//2)
  else:
    b-=d//2
    b-=e//2
    print(b*t+d//2+e//2+(d+e)//2*(t-1))

Solution (Python):
from sys import stdin
from itertools import groupby
S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
l = len(S)

rle = [(k, sum(1 for _ in g)) for k, g in groupby(S)]

if len(set(S)) == 1:
    print(len(S) * K // 2)
else:
    tmp_ans = sum([t[1] // 2 for t in rle])*K
    if S[0] != S[-1]:
        print(tmp_ans)
    else:
        print(tmp_ans - (rle[0][1] // 2 + rle[-1][1] // 2 - (rle[0][1] + rle[-1][1]) // 2) * (K-1))

Solution (Python):
from sys import stdin
from itertools import groupby
S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
l = len(S)

rle = [len(list(g)) for k, g in groupby(S)]

if len(set(S)) == 1:
    print(len(S) * K //2)
else:
    tmp_ans = sum([l // 2 for l in rle])*K
    if S[0] != S[-1]:
        print(tmp_ans)
    else:
        print(tmp_ans - (rle[0] // 2 + rle[-1] // 2 - (rle[0] + rle[-1]) // 2) * (K-1))

Solution (Python):
import sys
readline=sys.stdin.buffer.readline
 
S=readline().rstrip().decode("utf-8")
K=int(readline())
N=len(S)

 
if len(set(S))==1:
  answer=N*K//2
else:
  P=[0] 
  C=[] 
  S=S+"X"
  
  for i in range(N):
    if S[i]!=S[i+1]:
      P.append(i+1)
  for i in range(len(P)-1):
    C.append(P[i+1]-P[i])
  
    
  S=S[0:-1]
  answer=0
  if S[0]==S[-1]:
    for i in range(1,len(C)-1):
      answer+=C[i]//2
    answer*=K
    answer+=C[0]//2+C[-1]//2+((C[0]+C[-1])//2)*(K-1)
  else:
    for c in C:
      answer+=c//2
    answer*=K
print(answer)

Solution (Python):

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//2
            cnt = 1
    res += cnt//2
    return res

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

c = Counter(s)

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

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

cs = count(s)
cs2 = count(s+s)
print((cs2-cs)*(k-1) + cs)

Solution (Python):
s = input()
k = int(input())
l = []
cur = s[0]
c = 1
for i in s[1:]:
  if i != cur:
    l.append([cur,c])
    cur = i
    c = 1
  else:
    c += 1
l.append([cur,c])
c = 0
if len(l) == 1:
  print((l[0][1]*k)//2)
  exit()
elif len(l) == 2:
  print((l[0][1]//2+l[1][1]//2)*k)
  exit()

for i,j in l[1:-1]:
  c += j//2
if l[0][0] != l[-1][0]:
  c += l[0][1]//2 + l[-1][1] //2
  print(c*k)
else:
  re = c*k
  le = l[0][1] // 2
  r = l[-1][1] // 2
  ir = (l[0][1] + l[-1][1]) // 2
  print(re + le + r + ir * (k-1))

Solution (Python):
A = input()
n = len(A)
S = []
R = [A[n-1]]
for i in range(n):
    S.append(A[i])
    R.append(A[i])
K = int(input())
s = 0
p = S[0]
q = S[n-1]
for i in range(n-1):
    x = S[i]
    y = S[i+1]
    if x == y:
        S[i+1] = 1
        s += 1
t = 0
for j in range(n):
    x = R[j]
    y = R[j+1]
    if x == y:
        R[j+1] = 1
        t += 1
if p != q:
    ans = s*K
else:
    if S[n-1] == 1:
        ans = s*K
    else:
        if R[n] == 1:
            k = K//2
            ans = (K-k)*s + k*t
        else:
            ans = s + (K-1)*t
print(ans)

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

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

t1 = s + "
t2 = s * 2 +"



def f(text):
    start, end = 0, 0
    rc = []

    while start != len(text)-1:
        if text[start] == text[end]:
            end += 1
        else:
            rc.append(end-start)
            start = end

    return(rc)

first = sum([i//2 for i in f(t1)])
dif = sum([i//2 for i in f(t2)]) - sum([i//2 for i in f(t1)])

print(first + dif*(k-1))

Solution (Python):
import sys
input = sys.stdin.readline

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


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

    s3 = [0 if item != 1 else item for item in s3]

    ls = len(S)
    cnt_0 = sum(s3[:ls])
    cnt_1 = sum(s3[ls:2 * ls])
    cnt_2 = sum(s3[2 * ls:3 * ls])

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


chker(_s, _k)

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

t = bytearray(S * 3, 'us-ascii')
for i in range(1, len(t)):
    if t[i - 1] == t[i]:
        t[i] = 0
b0 = t[:len(S)].count(0)
b1 = t[len(S):len(S)*2].count(0)
b2 = t[len(S)*2:len(S)*3].count(0)
print(b0 + b1 * (K // 2) + b2 * ((K - 1) // 2))

Solution (Python):
from sys import exit

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

if K < 4:
    t = bytearray(S * K, 'us-ascii')
    for i in range(1, len(t)):
        if t[i - 1] == t[i]:
            t[i] = 0
    print(t.count(0))
    exit()

t = bytearray(S * 3, 'us-ascii')
for i in range(1, len(t)):
    if t[i - 1] == t[i]:
        t[i] = 0
b0 = t[:len(S)].count(0)
b1 = t[len(S):len(S)*2].count(0)
b2 = t[len(S)*2:len(S)*3].count(0)
print(b0 + b1 * (K // 2) + b2 * ((K - 1) // 2))

Solution (Python):
from itertools import groupby

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

def count(nums):
    return sum(num//2 for num in nums)

l = [len(list(g)) for k, g in groupby(S)]

if len(l) == 1:
    print(l[0]*K//2)
elif S[0] == S[-1]:
    print(count(l)*K - (count([l[0]]) + count([l[-1]]) - count([l[0]+l[-1]]))*(K-1))
else:
    print(count(l)*K)

Solution (Python):
s = input()
k = int(input())
same = len([i for i in s if i!=s[0]])
s = s+s
count = 1
a1 = 0
a2 = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    count += 1
  else:
    a2 += count//2
    count = 1
  if i == len(s)//2-2:
    a1 = a2
    if count > 1:
      a1 += count//2
if count > 1:
  a2 += count//2
ans = a1 + (a2-a1)*(k-1)
if same != 0:
  print(ans)
else:
  print((len(s)//2)*k//2)

Solution (Python):
s = input()
k = int(input())
ans = 0
cnt = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        ans += (cnt//2)*k
        cnt = 1
ans += ((cnt)//2)*k
if cnt == len(s):
    ans = len(s)*k//2
elif s[0] == s[-1]:
    l = 1
    r = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            l += 1
        else:
            break
    for i in range(len(s)-1,0,-1):
        if s[i] == s[i-1]:
            r += 1
        else:
            break
    ans -= (l//2 + r//2 - ((l+r)//2))*(k-1)
print(ans)

Solution (Python):
strings = list(input())
k = int(input())
counter = 0
same_starts = 0
for  i in range(len(strings)-1):
    if strings[i] != strings[i+1]:
        break
    same_starts += 1
for  i in range(len(strings)-1):
    if strings[i] == strings[i+1]:
        strings[i+1] = str(counter)
        counter += 1

if strings[0] != strings[-1]:
    print(counter * k)
else:
    
    if (same_starts + 1) == len(strings):
        
        if len(strings) % 2 == 0:
            print(k * len(strings)//2)
        else:
        
            
            if k % 2 == 0:
                print(counter * k // 2 + (counter + 1) * k // 2)
            else:
            
                print(counter * ((k - 1) // 2 + 1)+ (counter + 1) * (k - 1)//2)
    else:
        if (same_starts+1) % 2 == 0:
            print(counter * k)
        else:
            print((counter+1) * (k-1) + counter)

Solution (Python):
S = input()
K = int(input())
ListS = list(S)

def OneString(a):
    seq = 0
    seqit = 0
    for i in range(len(a)):
        if i == len(a) - 1:
            seq += (seqit+1)//2
        elif a[i]!=a[i+1]:
            seq += (seqit+1)//2
            seqit = 0
        else:
            seqit += 1
    return seq

def TBodd(a):
    seqnumT = 1
    seqnumB = 1
    if a[0] != a[-1]:
        return False
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            seqnumT += 1
        else: break
    for i in range(len(a)-2):
        if a[-(i+1)]==a[-(i+2)]:
            seqnumB += 1
        else: break
    if seqnumT%2 != 0 and seqnumB%2 != 0:
        return True
    else: return False
            

if len(set(ListS)) == 1:
    ans = (len(S)*K)//2
elif TBodd(S):
    ans = (OneString(S) + 1)*K - 1
else: ans = OneString(S)*K
    
print(ans)

Solution (Python):
s = str(input())
n = int(input())
num = []
cnt = 1
for i in range(len(s)-1):
    if s[i+1] != s[i]:
        num.append(cnt)
        cnt = 1
    else:
        cnt += 1
num.append(cnt)
if len(num) == 1:
    print((num[0] * n)//2)
    exit()
ans = 0
for nu in num:
    ans += nu//2
if s[0] != s[-1]:
    ans *= n
else:
    ans -= num[0]//2
    ans -= num[-1]//2
    ans *= n
    ans += num[0]//2
    ans += num[-1]//2
    ans += ((num[0]+num[-1])//2)*(n-1)
print(ans)

Solution (Python):
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//2
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//2)
else:
    print(output_count)

Solution (Python):
from itertools import groupby

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

l=[(k, list(v)) for k, v in groupby(S)]
if len(l) == 1:
    n = len(S)*K // 2
else:
    n=sum([len(v)//2 for k, v in l if len(v) > 1])*K
    if l[0][0]==l[-1][0] and len(l[0][1])%2 !=0 and len(l[-1][1])%2 !=0:
        n = n + K - 1
print(n)

Solution (Python):
S=list(input())
K=int(input())
SS=S*2
F=S[0]
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)

Solution (Python):
from itertools import groupby

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

l=[(k, list(v)) for k, v in groupby(S)]
if len(l) == 1:
    len_S = len(S)
    if len_S%2:
        n = len_S * (K//2) + (len_S//2) * (K%2)
    else:
        n = (len_S//2) * K
else:
    n=sum([len(v)//2 for k, v in l if len(v) > 1])*K
    if l[0][0]==l[-1][0] and len(l[0][1])%2 !=0 and len(l[-1][1])%2 !=0:
        n = n + K - 1
print(n)

Solution (Python):
import numpy as np

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

S1 = S + ['0']
S2 = S + S + ['0']

cur=S1[0]
cnt=0
Sl1 = list()
for s in S1:
    if cur==s:
        cnt+=1
    else:
        Sl1 += [cnt]
        cur = s
        cnt = 1

cur=S2[0]
cnt=0
Sl2 = list()
for s in S2:
    if cur==s:
        cnt+=1
    else:
        Sl2 += [cnt]
        cur = s
        cnt = 1

Sl1 = np.array(Sl1,dtype='int64')
Sl2 = np.array(Sl2,dtype='int64')

if 2*len(Sl1)==len(Sl2):
    N = int(K*np.sum(Sl1[::]//2))
else:
    if len(Sl1)>1 :
        N = int(Sl1[0]//2 + Sl1[-1]//2 + K*np.sum(Sl1[1:-1]//2) + (K-1)*((Sl1[0]+Sl1[-1])//2))
    else:
        N = int((K*Sl1[0])//2)


print(N)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
k = inp()
cnt = 1
res = 0
if s[0] == s[-1]:
    l = 1
    r = 1
    for i in range(n-1):
        if s[i] == s[i+1]:
            l += 1
        else:
            break
    for i in range(n-1)[::-1]:
        if s[i] == s[i+1]:
            r += 1
        else:
            break
    if l == n and r == n:
        res = (n*k)//2
    else:
        
        if r > 1:
            res += r//2
        if l > 1:
            res += l//2
        res += ((r+l)//2)*(k-1)
        
        for i in range(l,n-r-1):
            if s[i] == s[i+1]:
                cnt += 1
            elif cnt > 1:
                res += (cnt//2)*k
                cnt = 1
        if cnt > 1:
            res += (cnt//2)*k
else:
    for i in range(n-1):
        if s[i] == s[i+1]:
            cnt += 1
        elif cnt > 1:
            res += (cnt//2)*k
            cnt = 1
    if cnt > 1:
        res += cnt//2*k
print(res)

Solution (Python):
def main():
    s=list(input())
    k=int(input())
    n=len(s)

    ans=cnt=key=0
    for i in range(n-1):
        if key==0 and s[i]==s[i+1]:
            cnt+=1
            key=1
        elif key==1 and s[i]==s[i+1]:
            key=0
        else:
            key=0
    ans=cnt*k
    a=b=1
    for i in range(n-1):
        if s[i]==s[i+1]:
            a+=1
        else:
            break
    for i in range(n-1):
        if s[n-1-i]==s[n-2-i]:
            b+=1
        else:
            break
    if a!=n and s[0]==s[n-1]:
        ans+=(((a+b)//2)-(a//2)-(b//2))*(k-1)
    if a==n:
        if n%2==0:
            print(cnt*k)
            return
        else:
            print((n*k)//2)
            return
    print(ans)
main()

Solution (Python):
import sys

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

def count_min_operation(s):
	i = 0
	j = 0
	count = 0
	length = len(s)
	while j < length-1:
		c = 1
		for j in range(i+1,length):
			if s[i] == s[j]:
				c += 1
			else:
				count += c // 2
				i = j
				c = 1
				break

		if j == length-1:
			count += c // 2

	return count

for i in range(len(s)-1):
	if s[i] != s[i+1]:
		s1 = count_min_operation(s)
		s2 = count_min_operation(s+s)
		update = s2 - s1
		print(s1 + update * (k-1))
		sys.exit()

print(k * len(s) // 2)

Solution (Python):
import sys
S = input() 
K = int(input()) 
l = len(S)
ren = [0]*l
cnt = 0
 
for i in range(1,l):
  if S[i] == S[i-1]:
    cnt += 1
  elif S[i] != S[i-1]:
    ren[cnt] +=1
    cnt = 0
  if i == l-1:
    ren[cnt] +=1
    
ren = [K*i for i in ren]
   
if ren[l-1] or l ==1:
  print((l*K)//2)
  sys.exit()
  
if S[0]==S[-1]:
  start = 0
  while S[start]==S[start+1]:
    start += 1
  end = 0
  while S[-1-end]==S[-2-end]:
    end += 1
  ren[start+end+1] += K-1
  ren[start] -= K-1
  ren[end] -= K-1
 
 
ans = 0
 
for i in range(1,l):
  if i%2 ==0:
    ans += (i//2)*ren[i]
  else:
    ans += ((i+1)//2)*ren[i]
      
print(ans)

Solution (Python):
import sys
s_ = input()
k = int(input())

s = s_+"1"

RLE = []

cha = s[0]
cnt = 1

for i in range(1,len(s)):
    if s[i-1] == s[i]:
        cnt += 1
    else:
        RLE.append(cnt)
        cnt = 1
        cha = s[i]

ans = 0
if len(RLE)==1:
    print(RLE[0]*k//2)
    sys.exit()
if s[0]!= s[-2]:
    for c in RLE:
        ans += c//2
    
    ans *= k
    print(ans)
else:
    RLE[-1]+= RLE[0]
    for c in RLE[1:]:
        ans += c//2
    
    ans *= k
    ans -= RLE[-1]//2
    ans += RLE[0]//2
    ans += (RLE[-1]-RLE[0])//2
    print(ans)

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

ls = len(s)
l = []
tmp = 1
for i in range(ls-1):
    if (s[i] == s[i+1]):
        tmp += 1
    else:
        l.append(tmp)
        tmp = 1
l.append(tmp)

ans = 0
ss = 0
ll = len(l)

if (ll != 1) :
    
    if (s[0] == s[ls-1]):
        ans -= (l[0] // 2 + l[ll-1] // 2 - (l[0] + l[ll-1]) // 2) * (k-1)

    for i in range(ll):
        ss += l[i] // 2
    print(ans+ss*k)
else :
    print(ls * k // 2)

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

A = [0]
a = 0
for i in range(1, len(S)):
  if S[i-1] != S[i]:
    a += 1
    A.append(i)
A.append(len(S))
if a == 0:
  print((K * len(S)) // 2)
u = 0
if a != 0:
  B = [A[i] - A[i-1] for i in range(1, len(A))]
  for i in range(1, len(B) - 1):
    u += B[i] // 2
  u *= K
  if S[0] == S[len(S)-1]:
    u += B[0] // 2 + B[len(B)-1] // 2 + (K-1) * ((B[0] + B[len(B)-1]) // 2)
  else:
    u += K * (B[0] // 2 + B[len(B)-1] // 2)
  print(u)

Solution (Python):
import itertools as it 

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

groups = [(k, len(list(g))) for k, g in it.groupby(s)]



if s[0] != s[-1]:
    answer = sum([x[1] // 2 for x in groups]) * k

elif len(set(s)) == 1:
    answer = (len(s) * k) // 2

elif s[0] == s[-1]:
    answer = sum([x[1] // 2 for x in groups[1:-1]]) * k + groups[0][1] // 2 + groups[-1][1] // 2
    answer += (k - 1) * ((groups[0][1] + groups[-1][1]) // 2)

print(answer)

Solution (Python):
from itertools import groupby

S = input()
s = len(S)
K = int(input())

gr = []
for k, g in groupby(S):
    gr.append(len(list(g)))

if len(gr) == 1:
    print(len(S) * K // 2)
elif S[0] != S[-1]:
    print(sum([i // 2 for i in gr]) * K)
else:
    print(sum([i // 2 for i in gr]) * K - (gr[0] // 2 + gr[-1] // 2 - (gr[0] + gr[-1]) // 2) * (K - 1))

Solution (Python):
S = input()
s = len(S)
K = int(input())

if len(set(list(S))) == 1:
    print(len(S) * K // 2)
else:
    l = []
    left = 0
    right = 1
    while right < s:
        if S[left] == S[right]:
            right += 1
            if right == s:
                l.append(right - left)
        else:
            if right - left > 1:
                l.append(right - left)
                left = right
                right += 1
            else:
                left = right
                right += 1
    l_o = l[:]
    l = [i // 2 for i in l]
    if S[0] == S[-1]:
        if S[0] != S[1] and S[-1] != S[-2]:
            print(sum(l) * K + K - 1)
        elif S[0] == S[1] and S[-1] == S[-2]:
            print(sum(l) * K - (l[0] + l[-1] - (l_o[0] + l_o[-1]) // 2) * (K - 1))
        elif S[0] == S[1]:
            print(sum(l) * K - (l[0] - (l_o[0] + 1) // 2) * (K - 1))
        else:
            print(sum(l) * K - (l[-1] - (l_o[-1] + 1) // 2) * (K - 1))
    else:
        print(sum(l) * K)

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

ss = s + s

if len(set(s))==1:
    print(int((len(s)*k)/2))
else:
    a=0
    prev = "?"
    for v in s:
        if v==prev:
            a += 1
            prev = "?"
        else:
            prev = v

    b=0
    prev = "?"
    for v in ss:
        if v==prev:
            b += 1
            prev = "?"
        else:
            prev = v

    print(a+(b-a)*(k-1))

Solution (Python):
s=list(input())
n=len(s)
k=int(input())
S=s*3
for i in range(n*3-1):
    if S[i]==S[i+1]:S[i+1]="A"
    
A=S[:n].count("A")
B=S[n:n*2].count("A")
C=S[n*2:n*3].count("A")

if s.count(s[0])==n:print(n*k//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Solution (Python):
s=list(input())
k=int(input())
test=s*3
for i in range(len(s)*3-1):
    if test[i]==test[i+1]:
        test[i+1]="A"
A=test[:len(s)].count("A")
B=test[len(s):len(s)*2].count("A")
C=test[len(s)*2:len(s)*3].count("A")

if s.count(s[0])==len(s):
    print((len(s)*k)//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Solution (Python):
import math
s = input()
k = int(input())

def connection_list(s):
  res = [0 for i in range(len(s))]
  prev = ''
  cnt = 0
  start = 0
  connected = 0
  for i in range(len(s)):
    if s[i] == prev:
      cnt += 1
    else:
      res[start] = cnt
      cnt = 0
      start = i
      prev = s[i]
  if cnt > 0:
    res[-1] = cnt
  if s[0] == s[-1]:
    connected = res[-1] + res[0] + 1
  return res, connected

def calc_ceil(n):
  return math.ceil(n/2)
cl, cn = connection_list(s)
if cl[-1] == len(cl) - 1:
  res = calc_ceil(len(cl) * k - 1)
else:
  cl = list(map(calc_ceil, cl))
  if cn > 0:
    res = sum(cl[1:-1]) * k + cl[0] + cl[-1] + calc_ceil(cn) * (k - 1)
  else:
    res = sum(cl) * k
print(res)

Solution (Python):
s=list(input())
k=int(input())
test=s*3
for i in range(len(s)*3-1):
    if test[i]==test[i+1]:
        test[i+1]="A"
A=test[:len(s)].count("A")
B=test[len(s):len(s)*2].count("A")
C=test[len(s)*2:len(s)*3].count("A")

if s.count(s[0])==len(s):
    print((len(s)*k)//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Solution (Python):
s=list(input())
k=int(input())
A,B,C=0,0,0
test=s*3
for i in range(len(s)*3-1):
    if test[i]==test[i+1]:
        test[i+1]="A"
A=test[:len(s)].count("A")
B=test[len(s):len(s)*2].count("A")
C=test[len(s)*2:len(s)*3].count("A")

if s.count(s[0])==len(s):
    print((len(s)*k)//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Solution (Python):
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 // 2
        cnt = 1
    pre = i

ans += cnt//2

if cnt == len(s):
    print((cnt*k)//2)
    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)

Solution (Python):
s = input()
K = int(input())
a = 1
ans = 0
e = 0
l_s = []
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        a += 1
    else:
        l_s.append(a)
        a = 1
l_s.append(a)
if len(l_s) == 1:
    print((l_s[0]*K)//2)
    exit()
if s[0] == s[-1]:
    f = l_s[0]
    l = l_s [-1]
    l_s[-1] += l_s[0]
    del l_s[0]
    e = 1
for i in l_s:
    ans += i//2
if e == 1:
    M = ans*K - l_s[-1]//2
    M += f//2
    M += l//2
else:
    M = ans*K
print(M)

Solution (Python):
from itertools import groupby as gb
s=input()
k=int(input())
if len(set(s))==1:print((len(s)*k)//2);exit()
elif len(s)==2 and len(set(list(s)))==1:print(k);exit()
ji=[len(list(b))//2 for a,b in gb(s)]
l=k*sum(ji)
if s[0]==s[-1]:
  ji=[len(list(b)) for a,b in gb(s)]
  if ji[0]%2 and ji[-1]%2:l+=k-1
print(l)

Solution (Python):
s = input()
k = int(input())
def counter(str):
    str_list = list(str)
    count = 0
    for i in range(1, len(str_list)):
        if str_list[i] == str_list[i-1]:
            str_list[i] = "&"
            count += 1
    return count
if k <= 3:
    print(counter(s*k))
else:
    moto = counter(s)
    cost1 = counter(s*2) - 2*moto
    cost2 = counter(s*3) - counter(s*2) - moto
    if k%2 == 0:
        print(moto*k + ((k-1)//2 + 1)*cost1 + ((k-1)//2)*cost2)
    else:
        print(moto*k + ((k-1)//2)*(cost1+cost2))

Solution (Python):
s = input()
k = int(input())
if len(set(list(s))) == 1:
  print(len(s)*k//2)
  exit()
l = list()
t = 0
c = s[0]
for x in s:
  if x == c:
    t += 1
  else:
    c = x
    l.append(t)
    t = 1
l.append(t)
ans = 0
if s[0]==s[-1]:
  ans += l[0]//2 + l[-1]//2 - (l[0]+l[-1])//2
  l[0] += l[-1]
  l.pop()
for x in l:
  ans += (x//2)*k
print(ans)

Solution (Python):
s = input()
k = int(input())
key = s[0]
count = 1
sum_count = 0
list1=[]
ans = 0
ans2 =0
for i in range(1,len(s)):
    if key == s[i]:
        count += 1
    else:
        list1.append(count)
        count = 1
        key = s[i]
list1.append(count)
if len(list1) == 1:
    print(list1[0]*k//2)
elif s[0] == s[len(s)-1]:
    ans += (list1[0]+list1[len(list1)-1])//2*(k-1)
    for i in range(1,len(list1)-1):
        ans2 += list1[i]//2
    print(ans2*k+ans+list1[0]//2+list1[len(list1)-1]//2)
else:
    for i in range(len(list1)):
        ans += list1[i]//2
    print(ans*k)

Solution (Python):
s=input()
n=int(input())
l=len(s)
ct=0
for i in range(l-1):
 if s[i]==s[i+1]:
  ct=ct+0.5
 else:
  ct=int(ct+0.5)
ct=int(ct+0.5)
s1=s*3
l1=l*3
ct1=0
for i in range(l1-1):
 if s1[i]==s1[i+1]:
  ct1+=0.5
 else:
  ct1=int(ct1+0.5)
ct1=int(ct1+0.5)
ans=int((ct1-ct*3)/2*(n-1)+0.5)+ct*n
print(ans)

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

a = [1]
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        a[-1]+=1
    else:
        a.append(1)

ans = 0
if len(a)==1:
    ans = len(s)*k//2
elif s[0]!=s[-1]:
    for i in range(len(a)):
        ans+=(a[i]//2)*k
else:
    for i in range(1,len(a)-1):
        ans+=(a[i]//2)*k
    ans+=a[0]//2
    ans+=a[-1]//2
    ans+=((a[0]+a[-1])//2)*(k-1)
print(ans)

Solution (Python):
import sys
string = input()
K = int(input())

if len(set(string)) == 1:
    print((K * len(string)) // 2)
    sys.exit()


f = string[0]
l = string[-1]
if f == l:
    first = 1
    for i in range(1, len(string)):
        if string[i] == f:
            first += 1
        else:
            break
    last = 1
    for i in range(len(string) - 2, 0, -1):
        if string[i] == l:
            last += 1
        else:
            break
else:
    first = 0
    last = 0

ans = 0
final = 0
if (first + last) % 2 == 0 and first % 2 and last % 2:
    final = K - 1

string = string + '_'

count = 1
for n in range(1, len(string)):
    if string[n - 1] == string[n]:
        count += 1
    elif string[n - 1] != string[n] or n == len(string) - 1:
        if count >= 2:
            ans += (count // 2)
            
            
            
        count = 1
print(ans * K + final)