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
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 1
ans = 0
inf = float("inf")

s = s()
k = k()

if s[0]*len(s) == s:
    print(len(s)*k // 2)
    sys.exit()

res = [1]

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

for i in res:
    ans += (i//2)*k
if s[0] == s[-1]:
    diff = (res[0]+res[-1])//2 - (res[0]//2 + res[-1]//2)
    ans += diff*(k-1)
print(ans)

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

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

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

ans = 0
cnt = 0
now = s[l]
for i in range(l,r):
    if s[i] == now:
        cnt += 1
    else:
        ans += cnt//2
        cnt = 1
        now = s[i]
ans += cnt//2
ans *= k

if s[0] == s[-1]:
    ans += (head+tail)//2*(k-1)
    ans += head//2+tail//2
else:
    ans += (head//2+tail//2)*k
print(ans)

Solution (Python):
s=str(input())
k=int(input())
s=list(s)
s2=s+s
cnt=0
temp1=0
temp2=0
l=0
for i in range(1,len(s)):
  if s[i]==s[i-1]:
    cnt=cnt+1
    l=l+1
  if s[i]!=s[i-1]:
    temp1=temp1+(cnt+1)//2
    cnt=0
temp1=temp1+(cnt+1)//2
cnt=0
for i in range(1,len(s2)):
  if s2[i]==s2[i-1]:
    cnt=cnt+1
  if s2[i]!=s2[i-1]:
    temp2=temp2+(cnt+1)//2
    cnt=0
temp2=temp2+(cnt+1)//2
cnt=0
if l==len(s)-1:
  print(k*len(s)//2)
elif k==1:
  print(temp1)
else:
  print(temp1+(temp2-temp1)*(k-1))

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

if list(s[0]*len(s))==s:
	
	print(len(s)*k//2)
	exit()

res = [1]
for i in range(len(s)-1):
	if s[i]==s[i+1]:
		res[-1] += 1
	else:
		res.append(1)
ans = 0
for i in res:
	ans += (i//2)*k
if s[0]==s[-1]:
	diff = (res[0]+res[-1])//2 -(res[0]//2 + res[-1]//2)
	ans += diff*(k-1)
print(ans)

Solution (Python):
s = input()
k = int(input())
n = len(s)
res = 0
i = 0
if s.count(s[0]) == n:
    print(n*k//2)
    exit()

while i < n-1:
    if s[i] == s[i+1]:
        res += 1
        i += 1
    i += 1

res *= k

if s[0] != s[n-1]: 
    print(res)
else:
    a = 0
    b = 0
    i = 0
    while s[i] == s[0]:
        a += 1
        i += 1
    while s[n-1] == s[0]:
        b += 1
        n -= 1
    res -= ((a//2)+(b//2)-((a+b)//2))*(k-1)
    print(res)

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

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

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

if s[0] != s[-1]:
    ans = [i//2 for i in ans]
    print(sum(ans) * k)
elif s[0] == s[-1]:
    f, m, l = ans[0] // 2, (ans[0] + ans[-1]) // 2, ans[-1] // 2
    ans = [i//2 for i in ans[1:-1]]
    print(f + sum(ans) * k + m * (k - 1) + l)

Solution (Python):
s = list(input())
k = int(input())
n = len(s)
res = 0
i = 1
if s.count(s[0]) == n: 
    print(n*k//2)
    exit()

while i < n:
    if s[i-1] == s[i]:
        res += 1
        i +=1
    i+=1
res *= k

if s[0] != s[n-1]:
    print(res)
else:
    a=0
    b=0
    v=s[0]
    i=0
    while s[i] == v:
        a+=1
        i+=1
    while s[n-1] == v:
        b+=1
        n-=1
    res -= (((a//2)+(b//2))-((a+b)//2))*(k-1)
    print(res)

Solution (Python):
S = input()
K = int(input())
X = 0
i = 1
while i < len(S):
  if S[i] == S[i - 1]:
    X += 1
    i += 1
  i += 1
X *= K
if S == S[0] * len(S) and len(S) % 2 == 1:
  X += K // 2
elif i == len(S) and S[0] == S[-1]:
  x = 1
  i = 2
  while i < len(S):
    if S[i] == S[i - 1]:
      x += 1
      i += 1
    i += 1
  X += (K - 1) * (x - X // K)
print(X)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    S = readline().decode('utf-8').strip()
    K = int(readline())
    L = len(S)

    continuous = deque()

    count = 0
    last = ''
    for i in range(L):
        if i == 0:
            last = S[i]
            count += 1
        else:
            if last==S[i]:
                count += 1
            else:
                continuous.append(count)
                count = 1
                last = S[i]
    continuous.append(count)

    ans = 0
    if len(continuous)==1:
        ans = L*K//2
    else:
        if S[0]==S[-1]:
            a = continuous.popleft()
            b = continuous.pop()
            ans += ((a+b)//2)*(K-1) + a//2 + b//2
        while continuous:
            a = continuous.popleft()
            ans += a//2*K
    print(ans)

if __name__ == '__main__':
    main()

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

s_2=s*2
pre=""
ans=0

if s.count(s[0])==len(s):
  print((len(s)*k)//2)
  sys.exit()

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

pre=""
ans1=0
for i in range(len(s_2)):
  if pre==s_2[i]:
    ans1+=1
    pre=""
  else:
    pre=s_2[i]

if 2*ans<ans1:
  print(ans+(k-1)*(ans1-ans))
else:
  print(ans*k)

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
k = INT()
ans = 0
cnt = 1
s = s + s

for i in range(1, len(s)):
    if s[i] == s[i-1]:
        cnt += 1
    else:
        ans += cnt // 2
        cnt = 1
ans += cnt // 2

if ans % 2 == 0 or ans == len(s)//2:
    print(ans*k//2)
else:
    print(ans//2+(ans+1)//2*(k-1))

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


rle = []
pre = 'A'
chain = 1
for c in S:
    if c == pre:
        chain += 1
    else:
        if pre != 'A':
            rle.append([pre,chain])
        
        pre = c
        chain = 1
rle.append([pre,chain])

ans = 0


if len(rle) == 1:
    ans = (rle[0][1] * K) // 2


else:
    
    if rle[0][0] == rle[-1][0]:
        
        ans += rle[0][1]//2
        ans += rle[-1][1]//2
        rle[0][1] += rle[-1][1]
        rle[-1][1] = 0
        ans += rle[0][1]//2 * (K-1)

        
        mid_chains = 0
        for i in range(1,len(rle)):
            mid_chains += rle[i][1]//2
        ans += mid_chains * K

    
    else:
        mid_chains = 0
        for i in range(len(rle)):
            mid_chains += rle[i][1]//2
        ans += mid_chains * K



print(ans)

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0

sss=S*3

sss=[a for a in sss]
suuji=0
N=len(sss)
for n in range(N-1):
    if sss[n]==sss[n+1]:
        suuji+=1
        sss[n+1]=0

number=0
s=[a for a in S]
N=len(s)
for n in range(N-1):
    if s[n]==s[n+1]:
        number+=1
        s[n+1]=0
if K==1:
    print(number)
else:
    if answer - number == suuji - answer:
        print(number + (answer - number) * (K - 1))
    else:
        if K % 2 == 0:
            print(number + (answer - number) * (K//2)+(suuji-answer)*(K//2-1))
        else:
            print(number + (answer - number) * (K//2)+(suuji-answer)*(K//2))

Solution (Python):
from itertools import groupby
S=input()
K=int(input())
count=0
group=[len(list(value)) for key,value in groupby(S)]
for value in group:
    count+=value//2
count*=K
if S[0]==S[-1] and group[0]%2!=0 and group[-1]%2!=0:
    if len(S)==group[0]:
        count+=K//2
    else:
        count+=K-1
print(count)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

s = input()
K = ir()
ind = len(s)-1
while ind > 0 and s[ind] == s[ind-1]:
    ind-=1
if ind == 0:
    print(len(s)*K//2)
else:
    tmp = ''.join([s[ind:], s[:ind]])
    k = 0
    ans_div = 0
    pre = ''
    for c in tmp:
        if c != pre:
            ans_div+=((k+1)//2)
            k=0
        else:
            k+=1
        pre = c
    ans_div +=((k+1)//2)
    fir = s[:ind]
    firc = 0
    las = s[ind:]
    lasc = 0
    k = 0
    pre = ''
    for c in fir:
        if c != pre:
            firc+=((k+1)//2)
            k=0
        else:
            k+=1
        pre = c
    firc += ((k+1)//2)
    k = 0
    pre = ''
    for c in las:
        if c != pre:
            lasc+=((k+1)//2)
            k=0
        else:
            k+=1
        pre = c
    lasc += ((k+1)//2)
    print(firc+lasc+ans_div*(K-1))

Solution (Python):
S = list(input())
NS = [s for s in S]
K = int(input())
L = len(S)

if "".join(S) == S[0] * L:
  print(L * K // 2)
  exit()

ans = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    S[i+1] = '-'
    ans += 1


if S[0] != S[-1]:
  ans *= K

else:
  a = 0
  for i in range(L):
    if NS[i] == NS[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if NS[i] == NS[-1]:
      b += 1
    else:
      break
      

  ans *= K
  
  
  t = a // 2 + b // 2 - (a+b) // 2
  ans -= t * (K-1)
  
print(ans)

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

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

tmp = list(s * 2)
for i in range(len(tmp)-1):
    if i < len(s) and tmp[i] == tmp[i+1]:
        tmp[i + 1] = "
    elif i >= len(s) and tmp[i] == tmp[i+1]:
        tmp[i + 1] = "

fst = tmp[:len(s)].count("
snd = tmp[len(s):].count("

if k == 1:
    print(fst)
else:
    print(fst + snd * (k-1))

Solution (Python):


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


i = 0
a = 0

while i < len(s):
  if i == len(s) - 1:
    break
  if s[i] == s[i+1]:
    a += 1
    i += 2
  else:
    i += 1
else:
  print(a * k)
  exit()
  
i = -1
b = 0

while i < len(s):
  if i == len(s) - 1:
    print(a + b * (k - 1))
    exit()
  if s[i] == s[i + 1]:
    b += 1
    i += 2
  else:
    i += 1
else:
  print(a * ((k + 1) // 2) + b * ((k - 1) // 2))
  exit()

Solution (Python):
t=input()
s=list(t)
k=int(input())
if s==[s[0]]*len(s):
  print((len(s)*k)//2)
  exit()
b=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:b[-1]+=1
  else:b.append(1)
ans=0
for i in b:ans+=i//2*k
if t[0]==t[-1] and (b[0]+b[-1])%2==0:ans+=k-1
print(ans)

Solution (Python):
s, k = input(), int(input())
 
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()
 
ans, cnt = [], 1
for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        ans.append(cnt)
        cnt = 1
else:
    ans.append(cnt)
 
if s[0] != s[-1]:
    ans = [i//2 for i in ans]
    print(sum(ans) * k)
elif s[0] == s[-1]:
    f, m, l = ans[0] // 2, (ans[0] + ans[-1]) // 2, ans[-1] // 2
    ans = [i//2 for i in ans[1:-1]]
    print(f + sum(ans) * k + m * (k - 1) + l)

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

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

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

    if s[0] == s[-1]:
        ans = 0
        for i in range(m):
            if i == 0 or i == m-1:
                ans += memo[i] // 2
            else:
                ans += (memo[i] // 2) * k
        ans += ((memo[0] + memo[-1]) // 2) * (k-1)

    else:
        ans = sum(memo[i] // 2 for i in range(m)) * k

    print(ans)

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

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

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

if s[0] != s[-1]:
    ans = [i//2 for i in ans]
    print(sum(ans) * k)
elif s[0] == s[-1]:
    f, m, l = ans[0] // 2, (ans[0] + ans[-1]) // 2, ans[-1] // 2
    ans = [i//2 for i in ans[1:-1]]
    print(f + sum(ans) * k + m * (k - 1) + l)

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

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

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

if s[0] != s[-1]:
    ans = [i//2 for i in ans]
    print(sum(ans) * k)
elif s[0] == s[-1]:
    f, m, l = ans[0] // 2, (ans[0] + ans[-1]) // 2, ans[-1] // 2
    ans = [i//2 for i in ans[1:-1]]
    print(f + sum(ans) * k + m * (k - 1) + l)

Solution (Python):
s=input()
k=int(input())
cnt=1
temp=[]
for i in range(len(s)-1):
    if s[i]==s[i+1]: cnt+=1
    else:
        temp.append([s[i],cnt])
        cnt=1
if cnt>=1: temp.append([s[-1],cnt])
if len(temp)==1:
    print(k*len(s)//2)
    exit()
ans=0
if temp[0][0]!=temp[-1][0]:
    for pair in temp:
        if pair[1]!=1: ans+=pair[1]//2
    print(ans*k)
else:
    for pair in temp[1:-1]:
        if pair[1]!=1: ans+=pair[1]//2
    ans*=k
    ans+=(k-1)*((temp[0][1]+temp[-1][1])//2)
    ans+=temp[0][1]//2+temp[-1][1]//2
    print(ans)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
S = input()
k = I()
from itertools import groupby
x = y = 0
g = [len(list(v)) for k,v in groupby(S)]
for c in g:
    x += c//2
if S[-1] == S[0]:
    if g[-1]%2 == 0 or g[0]%2 == 0:
        pass
    elif len(S) == 1:
        y = k//2
    elif len(S) == g[0]:
        y = k//2
    else:
        y = k-1
print(x * k + y)

Solution (Python):
def run_length_compress(S):
    res = [[S[0], 1]]
    for c in S[1:]:
        if c == res[-1][0]:
            res[-1][1] += 1
        else:
            res.append([c, 1])
    return res


def solve():
    S = input()
    N = len(S)
    K = int(input())
    T = run_length_compress(S)
    if T[0][1] == N:
        return N * K // 2
    ans = sum(l // 2 for _, l in T) * K
    if T[0][0] == T[-1][0]:
        head_cnt, tail_cnt = T[0][1], T[-1][1]
        tmp = (head_cnt + tail_cnt) // 2 - head_cnt // 2 - tail_cnt // 2
        ans += tmp * (K - 1)
    return ans


print(solve())

Solution (Python):
def run_length_compress(S):
    res = [[S[0], 1]]
    for c in S[1:]:
        if c == res[-1][0]:
            res[-1][1] += 1
        else:
            res.append([c, 1])
    return res


S = input()
N = len(S)
K = int(input())
T = run_length_compress(S)
if T[0][1] == N:
    print(N * K // 2)
    exit()
ans = sum(l // 2 for _, l in T) * K
if T[0][0] == T[-1][0]:
    head, tail = T[0][1], T[-1][1]
    ans += ((head + tail) // 2 - head // 2 - tail // 2) * (K - 1)
print(ans)

Solution (Python):
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0

def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 * k
if sr[0][0] == sr[-1][0]:
    x = sr[0][1]
    y = sr[-1][1]
    cnt -= (x//2 + y//2 - (x+y)//2) *(k-1)
print(cnt)

Solution (Python):
C = {}
S = input().strip()
N = len(S)
K = int(input())
for i in range(N):
    s = S[i]
    if s not in C:
        C[s]=0
    C[s] += 1
if len(C)>1:
    a = 0
    cnt = 1
    for i in range(1,N):
        if S[i]==S[i-1]:
            cnt += 1
        else:
            a += cnt//2
            cnt = 1
    a += cnt//2
    X = S+S
    b = 0
    cnt = 1
    for i in range(1,2*N):
        if X[i]==X[i-1]:
            cnt += 1
        else:
            b += cnt//2
            cnt = 1
    b += cnt//2
    d = b-a
    print(a+d*(K-1))
else:
    A = list(C.items())
    k = A[0][1]
    if k%2==0:
        print((k//2)*K)
    else:
        if K%2==0:
            a = k
            b = 2*k
            d = k
            print(a+d*((K//2)-1))
        else:
            a = k//2
            b = (k*3)//2
            d = b-a
            print(a+d*(K//2))

Solution (Python):
S = list(input())
N = len(S)
K = int(input())
old = S[0]
a = 0
c = 1
for i in range(1,N):
  if S[i] == old:
    c += 1
  else:
    a += c // 2
    old = S[i]
    c = 1
b = 0
a += c // 2
for i in range(N):
  if S[i] == old:
      b += 1
  else:
    break
ans = a * K + ((b + c) // 2 - b // 2 - c // 2) * (K - 1)
if c == N:
  ans = N * K // 2
print(ans)

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

n = len(s)

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

cur = s[0]
X = []
cnt = 0
for i in range(n):
    if s[i] != cur:
        X.append(cnt)
        cur = s[i]
        cnt = 1
    else:
        cnt += 1
else:
    X.append(cnt)


ans = 0
if s[0] != s[-1]:
    for x in X:
        ans += k*(x//2)
else:
    for i, x in enumerate(X):
        if i == 0 or i == len(X)-1:
            continue
        else:
            ans += k*(x//2)
    ans += X[0]//2
    ans += X[-1]//2
    ans += (k-1)*((X[0]+X[-1])//2)
print(ans)

Solution (Python):
from copy import copy
def solve():
    S = input()
    K = int(input())
    ans = 0
    seqs = []
    seq = 1
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            seq += 1
        else:
            seqs.append(seq)
            seq = 1
    seqs.append(seq)
    first_seq = seqs[0]
    last_seq = seqs[-1]
    seqs.pop(0)
    if len(seqs):
        ans += first_seq//2 + last_seq//2
        seqs.pop(-1)
        if S[0]==S[-1]:
            ans += ((first_seq + last_seq)//2)*(K-1)
        else:
            ans *= K
        ans += sum(map(lambda x: x//2,seqs))*K
    else:
        if S[0]==S[-1]:
            ans += first_seq*K//2
        else:
            ans += first_seq//2*K
    return ans
print(solve())

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(s) + (k-1) * (n_change(2*s) - n_change(s)))

Solution (Python):
import sys
s=input()
k=int(input())
distinct=0
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    distinct=1
    first=i
    break
if distinct==0:
  print((len(s)*k)//2)
  sys.exit()
for i in range(len(s)-1,0,-1):
  if s[i]!=s[i-1]:
    last=i
    break
ans=(first+1)//2
ans+=((len(s)-last)//2)
count=1
for i in range(first+1,last):
  if s[i]!=s[i+1]:
    ans+=k*(count//2)
    count=1
  else:
    count+=1
if s[0]==s[-1]:
  ans+=((len(s)+first+1-last)//2)*(k-1)
else:
  ans+=((len(s)-last)//2+(first+1)//2)*(k-1)
print(ans)

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

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

ss = s + s
cnts = 0
now = s[0]
temp = 1
for i in range(1, n):
    if s[i] == now:
        temp += 1
    else:
        cnts += temp // 2
        temp = 1
        now = s[i]
cnts += temp // 2

cntss = 0
now = ss[0]
temp = 1
for i in range(1, 2*n):
    if ss[i] == now:
        temp += 1
    else:
        cntss += temp // 2
        temp = 1
        now = ss[i]
cntss += temp // 2

kousa = cntss - cnts
print(cnts + kousa*(k-1))

Solution (Python):
t=input()
s=list(t)
k=int(input())
if s==[s[0]]*len(s):
  print((len(s)*k)//2)
  exit()
b=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:b[-1]+=1
  else:b.append(1)
ans=0
for i in b:ans+=i//2*k
if t[0]==t[-1] and (b[0]+b[-1])%2==0:ans+=k-1
print(ans)

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

lst = []
b = ""
tmp = 0
rlt = 0
for s in S:
  if s == b:
    tmp += 1
  else:
    lst.append(tmp)
    tmp = 1
  b = s
    
lst.append(tmp)

if len(lst) == 2:
  rlt = (lst[1]*K)//2

elif S[0] == S[-1]:
  rlt = ((lst[1]+lst[-1])//2)*(K-1) + lst[1]//2 + lst[-1]//2
  for t in lst[2:-1]:
    rlt += (t//2)*K
else:
  for t in lst[1:]:
    rlt += (t//2)*K
    
print(rlt)

Solution (Python):
s = str(input())
k = int(input())
 
n = len(s)
l = [0] * n

for i in range(n-1):
    if s[i] == s[i+1] and l[i] == 0:
        l[i+1] = 1

if s.count(s[0]) == n:
    print(n*k//2)
elif s[0] != s[-1]:
    print(sum(l) * k)
else:
    a = 0
    b = 0
    ac = True
    bc = True
    for i in range(n-1):
        if s[i] == s[0] and ac == True:
            a += 1
        elif s[i] != s[0]:
            ac = False
        if s[n-1-i] == s[n-1] and bc == True:
            b += 1
        else: bc = False  
    sub = a//2 + b//2 - (a+b)//2
    print(sum(l)*k - sub*(k-1))

Solution (Python):
s = input()
k = int(input())
if len(s) == 1:
    print(k // 2)
    exit()

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

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

count = 0
for str, num in press:
    if num > 1:
        count += num // 2

count *= k
if s[0] == s[-1]:
    count -= (press[0][1] // 2 + press[-1][1] // 2 - (press[0][1] + press[-1][1]) // 2) * (k - 1)
print(count)

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

check=0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        check=1

if check==0:
    print(len(S)*K//2)
    exit()
import copy
s=copy.deepcopy(S)

cnt=0
for i in range(1,len(S)):
    if s[i]==s[i-1]:
        s[i]=str(i)
        cnt+=1

ss=S*2
cntss=0

for i in range(1,len(ss)):
    if ss[i]==ss[i-1]:
        ss[i]=str(i)
        cntss+=1
print(cnt*(K)+(cntss-cnt*2)*(K-1))

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

s2 = s + s
s3 = s + s + s

tmp = s[0]
c = 0
for i in range(1,len(s)):
    if s[i] == tmp:
        s[i] = "@"
        c += 1
    tmp = s[i] 

tmp = s2[0]
c2 = 0
for i in range(1,len(s2)):
    if s2[i] == tmp:
        s2[i] ="@"
        c2 += 1
    tmp = s2[i]

tmp = s3[0]
c3 = 0
for i in range(1,len(s3)):
    if s3[i] == tmp:
        s3[i] ="@"
        c3 += 1
    tmp = s3[i]

if c2-c == c3-c2:
    ans = c + (k-1)*(c2-c)
else:
    if k%2 != 0:
        ans = c + ((k-1)//2)*(c3-c)
    else:
        ans = c + ((k-2)//2)*(c3-c2) + c2 - c
print(ans)

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

S1 = list(S)
cnt1 = 0
for i in range(1, len(S1)):
    if S1[i] == S1[i - 1]:
        S1[i] = '*'
        cnt1 += 1

S2 = list(S) * 2
cnt2 = 0
for i in range(1, len(S2)):
    if S2[i] == S2[i - 1]:
        S2[i] = '*'
        cnt2 += 1
        
S3 = list(S) * 3
cnt3 = 0
for i in range(1, len(S3)):
    if S3[i] == S3[i - 1]:
        S3[i] = '*'
        cnt3 += 1



ans = cnt1 + (cnt2 - cnt1) * (K // 2) + (cnt3 - cnt2) * ((K - 1) // 2)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7



S = list(input())
K = INT()

if len(Counter(S).values()) == 1:
	print(len(S)*K//2)
	exit()

S_one = S.copy()
S_two = S*2

inner = 0
outer = 0

tmp = ""
for i, s in enumerate(S_one):
	if tmp == s:
		S_one[i] ="_"
		inner += 1
		tmp = ""
	else:
		tmp = s

tmp = ""
for i, s in enumerate(S_two):
	if tmp == s:
		S_two[i] = "_"
		outer += 1
		tmp = ""
	else:
		tmp = s

outer -= inner
print(inner+outer*(K-1))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7



S = input()
K = INT()

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7



S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

if tmp or S[0] != S[-1]:
	print(cnt*K)
	exit()

cnt2 = 1
tmp = True
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt2 += 1
		tmp = True

if tmp:
	print((cnt+cnt2)*(K//2) + cnt*(K%2))
else:
	print(cnt+cnt2*(K-1))

Solution (Python):



def cnt(st):
    x = st.copy()
    ret = 0
    for i in range(1,len(st)):
        if x[i]==x[i-1]:
            ret += 1
            x[i] = '0'
    return ret

def main():
    S = list(input())
    K = int(input())
    res1 = cnt(S)
    res2 = cnt(S+S)
    res3 = cnt(S+S+S)
    d1=res2-res1
    d2=res3-res2
    x2=(K-1)//2
    x1=K-1 - x2
    ret=res1+x1*d1+x2*d2
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
K = int(input())
oans = 0
head = 0
tail = 0
N = len(S)
count = 1

if S.count(S[0]) == N:
  print(N*K//2)
  exit()
for i in range(N-1):
  if S[i+1] == S[i]:
    count += 1
  else:
    oans += count // 2
    if head == 0:
      head = count
    count = 1
  if i == N-2:
    oans += count // 2
    tail = count
    if head == 0:
      print(N*K//2)
      exit()
plus = 0

if S[0] != S[-1]:
  print(oans * K)
  exit()

d = head//2 + tail //2 -(head + tail)//2

print(oans*K -(K-1)*d)

Solution (Python):


import sys
import numpy as np
from itertools import groupby
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = list(readline().decode().rstrip())
K = int(readline())

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

if len(g) == 1:
    print(len(S) * K // 2)
else:
    acc = sum(l // 2 for l in g) * K
    if S[0] == S[-1] and g[0] % 2 == 1 and g[-1] % 2 == 1:
        acc += K - 1
    print(acc)

Solution (Python):
def mi():
    return map(int, input().split())

def main():
    S = input()
    K = int(input())
    N = len(S)
    if len(set(S)) == 1:
        print(N*K//2)
    else:
        SS = S+S
        t = ''
        tt = ''
        flg = 0
        for i in range(N):
            if i and t[i-1] == S[i]:
                t += '
            else:
                t += S[i]

        for i in range(2*N):
            if i and tt[i-1] == SS[i]:
                tt += '
            else:
                tt += SS[i]

        d = tt.count('
        print(t.count('

if __name__ == '__main__':
    main()

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


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


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

else:
    ans = 0
    
    if s[0] != s[-1]:
        for cnt in counts:
            ans += cnt // 2
        ans *= k

    
    else:
        
        for cnt in counts[1:-1]:
            ans += cnt // 2
        ans *= k
        
        ans += counts[0] // 2
        
        ans += counts[-1] // 2
        length = counts[0] + counts[-1]
        
        ans += (length // 2) * (k - 1)

    print(ans)