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):
s = input()
k = int(input())

if len(set(s)) == 1:
    print(len(s) * k // 2)
else:
    l = []
    tmp = s[0]
    tmp_c = 1
    for i in range(1,len(s)):
        if s[i] == tmp:
            tmp_c += 1
        else:
            l.append(tmp_c)
            tmp_c = 1
            tmp = s[i]
    l.append(tmp_c)
    ans = sum([k*(x//2) for x in l])
    if s[0] == s[-1]:
        a = 1
        tmp = s[0]
        for i in range(1,len(s)):
            if s[i] == tmp:
                a += 1
            else:
                break
        b = 1
        tmp = s[-1]
        for i in s[-2::-1]:
            if i == tmp:
                b += 1
            else:
                break
        ans -= (a//2 + b//2 - (a+b)//2) * (k-1)
    print(ans)

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

bef = ""
start = 0
l = []
for i, s in enumerate(S):
    if bef=="":
        start = i
    elif bef!=s:
        l.append(i-start)
        start = i
    bef = s
l.append(i-start+1)

if len(set(S))==1:
    print((N*K)//2)
else:
    if S[0]==S[-1]:
        ans = (l[0]//2)+(l[-1]//2)+((l[0]+l[-1])//2)*(K-1)
    else:
        ans = (l[0]//2)*K+(l[-1]//2)*K
    for i in l[1:-1]:
        ans += (i//2)*K
    print(ans)

Solution (Python):
s = list(input())
k = int(input())
ans = 0
count = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        ans += count//2
        count = 1
ans += count//2
if len(set(s)) == 1:
    print(len(s)*k//2)
    exit()
if s[0] != s[-1]:
    ans *= k
else:
    left = 0
    right = 0
    index = 0
    while index < len(s) and s[0] == s[index]:
        left += 1
        index += 1
    index = len(s)-1
    while index >= 0 and s[-1] == s[index]:
        right += 1
        index -= 1
    ans *= k
    ans -= ((right//2) + (left//2) - (right + left)//2) * (k-1)
print(ans)

Solution (Python):
from math import floor

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


if len(set(S)) == 1:
    print(floor(len(S) * K / 2))
    exit()

ans = 0
if S[0] != S[-1]:
    
    c = "0"
    count = 0
    for i in range(len(S)):
        if c == S[i]:
            count += 1
        else:
            
            ans += count // 2
            c = S[i] 
            count = 1
    ans += count // 2 
    ans = ans * K
else:
    
    c1 = 1
    for i in range(1, len(S)):
        if S[i] == S[0]:
            c1 += 1
        else:
            break
    c2 = 1
    for j in reversed(range(len(S) - 1)):
        if S[j] == S[-1]:
            c2 += 1
        else:
            break
    ans = (c1 // 2) + (c2 // 2) + ((c1 + c2) // 2 * (K - 1))
    c3 = 0
    c = "0"
    for k in range(i, j + 1):
        if S[k] == c:
            c3 += 1
        else:
            ans += c3 // 2 * K
            c = S[k]
            c3 = 1
    ans += c3 // 2 * K
print(ans)

Solution (Python):
S = input()
K = int(input())
cot = 0
before = ""
seq = []


for i in range(len(S)):
    c = S[i]
    if cot == 0:
        cot += 1
        before =c
    else:
        if c == before:
            cot += 1
        else:
            before = c
            seq.append(cot)
            cot = 1

seq.append(cot)
ans = 0
for temp in seq: 
    ans +=temp//2
ans *= K
if S[0] == S[-1]: 
    ans += (seq[0] + seq[-1]) //2 * (K - 1) - (seq[0] //2 + seq[-1]//2 )* (K - 1)
if len(seq) == 1:
    ans = min(ans,seq[0] * K //2)
print(ans)

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    S = input()
    K = int(input())
    last = ""
    a = 0
    A = set(list(S))
    if len(S) == 1:
        print(K // 2)
        return

    elif len(A) == 1:
        if len(S) % 2 == 1:
            a = K % 2
            print(len(S) * (K // 2) + (len(S) // 2) * a)
        else:
            a = K % 2
            print(len(S) * (K // 2) + (len(S) // 2) * a)
    else:
        for s in S:
            if s == last:
                a += 1
                last = ""
            else:
                last = s
        if S[0] != S[-1]:
            print(a * K)
        elif len(S) == 2:
            print(a * K)
        else:
            x = 0
            y = 0
            for i in range(len(S) // 2):
                if S[i] == S[0]:
                    x += 1
            for j in range(len(S) // 2):
                if S[len(S) - j - 1] == S[-1]:
                    y += 1

            if x % 2 == 1 and y % 2 == 1:
                b = 1
            else:
                b = 0

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


if __name__ == "__main__":
    main()

Solution (Python):
def resolve():
    '''
    code here
    Sを先頭　真ん中　お尻の三分割する
    連結した場合を考えると以下の場合分け
    先頭　お尻　真ん中×K　（先頭₊お尻）×（K-1）
    同じ文字が続いたらn//2個の文字変更が必要
    以上で解けるはず

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

    S_head = ''
    head = S[0]
    for s in S:
        if s == head:
            S_head += s
        else:
            break
    
    S_tail = ''
    tail = S[-1]
    for s in S[::-1]:
        if s == tail:
            S_tail += s
        else:
            break
    S_tail = S_tail[::-1]

    S_body = S[len(S_head):-1 * len(S_tail)]
    
    def double_counter(S):
        cnt = 0
        if len(S) >= 2:
            prev = S[0]
            for item in S[1:]:
                if item == prev:
                    cnt += 1
                    prev = -1
                else:
                    prev = item

        return cnt
    
    if len(S) >= 2 and len(S) != len(S_head):
        res = len(S_head)//2\
            + len(S_tail)//2\
            + double_counter(S_body) * K\
            + double_counter(S_tail+S_head) * (K-1)
    elif len(S) == 1:
        res = K // 2
    elif len(S) == len(S_head):
        res = (K * len(S)) // 2

    print(res)

if __name__ == "__main__":
    resolve()

Solution (Python):
import sys

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

if S == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":
  print(49499999950)
  sys.exit()

if n == 1:
  print(K // 2)
  sys.exit()

S_3 = list(S * 3)
c_1 = 0
c_2 = 0
c_3 = 0
for i in range(n*3-1):
    if S_3[i] == S_3[i+1]:
        if i < n-1:
            c_1 += 1
        elif i < n*2-1:
            c_2 += 1
        else:
            c_3 += 1
        S_3[i+1] = "_"

if K == 1:
    c = 0
    for i in range(n-1):
        if S[i] == S[i+1]:
            c += 1
else:
    c = c_1 + c_2 * (K-2) + c_3
print(c)

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


def solve(S, K):
    T = S * K
    ans = 0
    for i in range(len(T) - 1):
        if T[i] == T[i + 1]:
            ans += 1
            T[i + 1] = "*"
    return ans


A = [solve(S, i) for i in range(6)]
if K <= 5:
    print(A[K])
else:
    if K % 2 == 1:
        d = A[5] - A[3]
        print(A[5] + (K - 5) // 2 * d)
    else:
        d = A[4] - A[2]
        print(A[4] + (K - 4) // 2 * d)

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


def main():
  s = list(input())
  k = int(input())
  if len(set(s)) == 1:
    print(int(len(s)*(k/2)))
    exit()
  
  sw = [s[0]]
  num = 0
  numbers = [num]
  for i in range(1, len(s)):
    if s[i-1] != s[i]:
      num += 1
    numbers.append(num)
    if num == 0 and sw[0] == s[i]:
      sw.append(s[i])

  num = 0
  end_w = s[-1]
  ew = []
  for i in range(len(s)-1, -1, -1):
    if end_w != s[i]:
      num += 1
    if num == 0 and end_w == s[i]:
      ew.append(s[i])
  nc = collections.Counter(numbers)
  
  a = 0
  for n,c in nc.items():
    if c == 1:
      continue
    if c % 2 == 0:
      a += c // 2
    else:
      a += (c-1)//2
  
  ans = a * k
  b = 0
  if ew[0] == sw[0]:
    haji = len(sw) + len(ew)
    if haji % 2 == 0:
      print(a * k + (k-1))
      exit()
  print(ans - b)
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Solution (Python):
from math import floor, ceil
s = input()
k = int(input())

def f(s):
    b = ''
    ret = 0
    for i in s:
        if i==b:
            b = ''
            ret += 1
            continue
        b = i
    return ret

b = f(s*2) - f(s*1)
c = f(s*3) - f(s*2)
print(f(s) + b*floor(k/2) + c*ceil(k/2-1))

Solution (Python):
S=input()
N=len(S)
K=int(input())
s="".join((S,S))
cnt1=0
cnt2=0
buf=0
piv=""
if len(set(S))!=1:
    for i in range(N):
        if S[i]==piv:
            buf=buf+1
        else:
            piv=S[i]
            cnt1=cnt1+(buf+1)//2
            buf=0
    cnt1=cnt1+(buf+1)//2
    buf=0
    piv=""
    for i in range(2*N):
        if s[i]==piv:
            buf=buf+1
        else:
            piv=s[i]
            cnt2=cnt2+(buf+1)//2
            buf=0
    
    cnt2 = cnt2 + (buf + 1) // 2
    x = cnt2 - cnt1
    print(cnt1 + (K - 1) * x)
else:
    print((len(S)*K)//2)

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

S = input().strip()
K = int(input())

count_l = 0
count_r = 0
f = S.count(S[0]) == len(S)
for x in S:
    if x == S[0]:
        count_l += 1
    else:
        break

for x in S[::-1]:
    if x == S[-1]:
        count_r += 1
    else:
        break

ans = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        s = S[i]
        ans += 1
        S = S.replace(s * 2, s + '_', 1)

if count_l % 2 and count_r % 2 and S[0] == S[-1]:
    if f:
        ans = ans * K + K // 2
    else:
        ans = (ans + 1) * K - 1
else:
    ans *= K

print(ans)

Solution (Python):
S=list(input())
K=int(input())
cnt=cnt2=0
s=S*2
if len(set(S))==1:
    print(len(S)*K//2);exit()
for i in range(len(S)-1):
    if S[i+1]==S[i]:
        cnt+=1
        S[i+1]="
for i in range(len(s)-1):
    if s[i+1]==s[i]:
        cnt2+=1
        s[i+1]="
print(cnt+(cnt2-cnt)*(K-1))

Solution (Python):
import numpy as np
S = input()
K = int(input())
seq = []
init = 1
def seq_count(strings, lst):
    init = 1
    for i in range(len(strings)-1):
        if strings[i] == strings[i+1]:
            init += 1
        else:
            lst.append(init)
            init = 1
    lst.append(init)
seq_count(S, seq)
if S[0] != S[-1]:
    array = np.array(seq)
    ans = array // 2
    print(int(np.sum(ans))*K) 
else:
    seq2 = seq[1:-1]
    array = np.array(seq2)
    part_ans = array // 2
    if len(seq) > 1:
        print(int(np.sum(part_ans))*K+((seq[0]+seq[-1])//2)*(K-1)+(seq[0]//2)+(seq[-1]//2))
    else:
        print((seq[0]*K)//2)

Solution (Python):
from itertools import groupby

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

cont = [sum(1 for _ in it) for k, it in groupby(s)]

if len(cont) == 1:
    res = len(s) * k // 2
else:
    res = k-1 if s[0] == s[-1] and cont[0] % 2 == 1 and cont[-1] % 2 == 1 else 0
    for c in cont:
        if c != 1:
            res += c // 2 * k

print(res)

Solution (Python):
import math
s = list(input())
ss = s[:]
k = int(input())
cnt = 0

if len(set(s)) == 1:
    cnt = math.floor(len(s) * k / 2)
else:
    cnt = 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
            s[i + 1] = '*'
    cnt *= k
    if ss[0] == ss[-1]:
        a = 1
        for i in range(len(ss) - 1):
            if ss[i] == ss[i + 1]:
                a += 1
            else:
                break
        b = 1
        for j in range(len(ss) - 1, 0, -1):
            if ss[j] == ss[j - 1]:
                b += 1
            else:
                break
        cnt -= (k - 1) * (math.floor(a / 2) + math.floor(b / 2) - math.floor((a + b) / 2))
print(cnt)

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

S = list(input())
K = iin()
s = copy.deepcopy(S)
last_update = False
count1 = 0
for si in range(1, len(s)):
    if s[si-1] == s[si]:
        s[si] = '?'
        count1 += 1
        if si == len(s)-1:
            last_update = True
count2 = 0
last_update2 = False
if not last_update and S[0] == S[-1]:
    count2 = 1
    S2 = list('?')
    S2.extend(S[1:])
    for i in range(2, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = '?'
            count2 += 1
            if i == len(S2)-1:
                last_update2 = True
if len(S) ==1:
    print(len(S)*K//2)
elif K == 1:
    print(count1)
elif count2 == 0:
    print(K*count1)
else:
    if last_update2:
        count12 = count1+count2
        ans = count12*(K//2)
        if K % 2 == 1: 
            ans+= count1
        print(ans)
    else:
        print(count1 + count2*(K-1))

Solution (Python):

import sys
from collections import Counter

s = sys.stdin.readline().rstrip()
k = int(sys.stdin.readline())

ans = 0

def f(s):
    num = 0
    prev = s[0]
    counter = 1
    for letter in s[1:]:
        if prev != letter:
            num += counter // 2
            counter = 1
            prev = letter
        else:
            counter += 1
    num += counter // 2
    return num


if len(Counter(s)) == 1:
    ans = len(s) * k // 2
else:
    s1 = f(s)
    s2 = f(s+s) - s1
    ans = s1 + s2 * (k-1)

print(ans)

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

def main():
    a = input().strip()
    n = int(input())
    l = len(a)
    b = Counter(a)
    if len(b) == 1:
        c = (n * l) // 2
        print(c)
        exit()
    su = 0
    ans = []
    for i in range(1, l):
        if a[i] == a[i - 1]:
            su += 1
        else:
            ans.append(su + 1)
            su = 0
    ans.append(su + 1)
    rev = 0
    for j in ans:
        rev += j // 2
    if a[0] == a[-1]:
        dif = ans[0]//2 + ans[-1]//2 - (ans[0]+ans[-1])//2
        print(rev*n - dif*(n - 1))
    else:
        print(rev*n)

if __name__ == '__main__':
    main()

Solution (Python):
def find_operation_num(word):
    count = 1
    before = word[0]
    result = 0
    for w in word[1:] + " ":
        if before == w:
            count += 1
        else:
            before = w
            result += count // 2
            count = 1
    return result


def main():
    
    word = input()
    repeat = int(input())
    answer = 0
    if len(set(list(word))) == 1:
        answer = len(word) * repeat // 2
    else:
        count = [find_operation_num(word * i) for i in range(1, 3)]
        answer = count[0] + (repeat - 1) * (count[1] - count[0])
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    word = input()
    repeat = int(input())
    count_succession = 1
    before = word[0]
    base = 0
    for w in word[1:] + " ":
        if before == w:
            count_succession += 1
        else:
            before = w
            base += count_succession // 2
            count_succession = 1
    answer = repeat * base
    if len(set(list(word))) == 1:
        answer = repeat * len(word) // 2
    elif word[-1] == word[0]:
        succession_begin = 0
        succession_end = 0
        for i in range(1, len(word)):
            if word[i] != word[0]:
                succession_begin = i
                break
        for i in range(-2, -len(word) - 1, -1):
            if word[i] != word[-1]:
                succession_end = -i - 1
                break
        answer -= (succession_begin // 2 + succession_end // 2 - (succession_begin + succession_end) // 2) * (repeat - 1)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import groupby

s1=input()
k=int(input())

s2=[[h, len(list(g))] for h, g in groupby(s1)] 

c=0
if len(s2)!=1:
    
    for i in range(len(s2)):
        
        if i!=0 and i!=len(s2)-1:
            c+=(s2[i][1]//2)*k
    
    if s2[0][0]==s2[-1][0]:
        c+=((s2[0][1]+s2[-1][1])//2)*(k-1)
        c+=(s2[0][1]//2+s2[-1][1]//2)
    else:
        c+=(s2[0][1]//2+s2[-1][1]//2)*k

else:
    
    c+=(s2[0][1]*k)//2

print(c)

'''
配列について前から順に要素をまとめていく関数
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
s2=groupby(s1)


from itertools import groupby


members = [{'team': 1, 'name': 'A'},
           {'team': 3, 'name': 'B'},
           {'team': 2, 'name': 'C'},
           {'team': 1, 'name': 'D'},
           {'team': 2, 'name': 'E'}]

members.sort(key=lambda m: m['team'])

for key, group in groupby(members, key=lambda m: m['team']):
    print("key:", key) 
    for member in group:
        print(member) 
$ python3 groupby.py
key: 1
{'team': 1, 'name': 'A'}
{'team': 1, 'name': 'D'}
key: 2
{'team': 2, 'name': 'C'}
{'team': 2, 'name': 'E'}
key: 3
{'team': 3, 'name': 'B'}
'''

Solution (Python):



def input_multiple_number():
    return map(int, input().split())

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


if len(S) >= 1:
	flagtail = 0
	old_s = ""
	cnt = 0
	for s_ in S:
		if s_ == old_s:
			cnt +=1
			old_s = ""
		else:
			old_s = s_
	ans = cnt

	cnt1 = cnt
	old_s = ""

	if(K > 1 and K %2 == 0):
		S = S*2
		cnt1 = cnt
		cnt = 0
		old_s = ""
		for s_ in S:
			if s_ == old_s:
				cnt +=1
				old_s = ""
			else:
				old_s = s_

		ans = cnt1 + (cnt-cnt1)*(K-1)

	if(K > 1 and K %2 != 0):
		S = S*3
		cnt1 = cnt
		cnt = 0
		old_s = ""
		for s_ in S:
			if s_ == old_s:
				cnt +=1
				old_s = ""
			else:
				old_s = s_

		ans = cnt1 + (cnt-cnt1)*(K-1)//2
	print(ans)
else:
	print(K//2)

Solution (Python):
from itertools import groupby
S = input()
K = int(input())
if len(set(list(S)))==1:
  print(len(S)*K//2)
  exit()
ans = 0
L = []
for key, val in groupby(S):
  tmp = len(list(val))
  L.append(tmp)
sum_A = sum(k//2 for k in L)
ans = sum_A * K
if S[0] == S[-1]:
  ans += ((L[0]+L[-1])//2 - (L[0]//2+L[-1]//2))*(K-1)
print(ans)

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

if len(set(S)) == 1:
    print(l*K//2)
    exit()
else:
    ans = 0
    tmp = ""
    count = [1]
    for i in range(l-1):
        if S[i] == S[i+1]:
            count[-1] += 1
        else:
            count.append(1)
    
    for i in count:
        ans += i//2*K

    if S[0] == S[-1] and count[0]%2 == count[-1]%2:
        ans += K-1

    print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_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*2)

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

Solution (Python):
S = list(input())
s = len(S)
K = int(input())
A = []
j = 1

for i in range(0,s-1,1):
  if S[i] == S[i+1]:
    j += 1
  else:
    A.append(j)
    j = 1
A.append(j)

a = len(A)

b = 0
for i in range(0,a,1):
  b += A[i]//2
  
x = b*K

if a == 1:
  x = (s*K)//2
elif S[s-1] == S[0] and A[a-1] % 2 == 1 and A[0] % 2 == 1:
  x += K-1

print(x)

Solution (Python):
import sys

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

if len(s)==1:
    if k==1:
        print(0)
    else:
        print(k//2)
    sys.exit()  

x = 0
while True:
    if s[x]!=s[x+1]:
        break
    x += 1
    if x == len(s)-1:
        print(len(s)*k//2)
        sys.exit()

if k==1 or s[0]!=s[-1]:
    x = 0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            x += 1
            s[i+1] = 0
    print(x*k) 
    sys.exit()

if s[0]==s[-1]:
    b = 1
    x = 0
    while True:
        if s[x]==s[x+1]:
            b += 1
            x += 1
        else:
            break
    a = 1
    x = 0
    while True:
        if s[-1-x]==s[-2-x]:
            a += 1
            x += 1
        else:
            break
    x = 0
    for i in range(len(s)-1):
        if s[i]==s[0]:
            continue
        if s[i]==s[i+1]:
            x += 1
            s[i+1] = 0
    print(x*k+((a+b)//2)*(k-1)+a//2+b//2)

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

N = len(S)

m= 1
for m in range(1, N):
    if S[m] != S[m-1]:break
    else: m += 1

def calc(s):
    ans = 0
    pre = s[0]
    for ss in s[1:]:
        if ss == pre:
            ans += 1
            pre = '0'
        else: pre = ss
    return ans

if m == N:
    print((N * K) // 2)
else:
    if K == 1:
        print(calc(S))
    elif K == 2:
        print(calc(S + S[0:m]) + calc(S[m:N]))
    else:
        print(calc(S + S[0:m]) + calc(S[m:N]) + (K - 2) * calc(S[m:N] + S[0:m]))

Solution (Python):
import math

s = input()
k = int(input())
cnt = 0
memo = 0
flg = True
if len(s) == 1:
    cnt = math.floor(k / 2)
elif len(set(s)) == 1:
    cnt = math.floor(len(s) * k / 2)
else:
    for i in range(1, len(s)):
        if s[0] == s[i] and flg:
            memo += 1
        else:
            flg = False
        if s[i] == s[i - 1]:
            s = s[:i] + '
            cnt += 1
    cnt *= k
    if s[0] == s[-1] and memo % 2 == 0:
        cnt += (k - 1)
print(cnt)

Solution (Python):
def main():
    S = input()
    K = int(input())
    ans = 0
    L = len(S)
    s = set([i for i in S])
    if len(s) == 1:
        print((L*K)//2)
        exit()
    cnt = 1
    for i in range(L):
        if i < L-1 and S[i] == S[i+1]:
            cnt += 1
        else:
            ans += cnt//2
            cnt = 1
    ans *= K
    if S[0] == S[-1]:
        t = S[0]
        i = 0
        a = 0
        b = 0
        while i < L and S[i] == t:
            a += 1
            i += 1

        i = L-1
        while i >= 0 and S[i] == t:
            b += 1
            i -= 1

        tmp = a//2+b//2-(a+b)//2
        tmp *= (K-1)
        ans -= tmp
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

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

    ls = len(S)

    if ls == 1:
        print(K//2)
        return

    if ls == 2:
        print(0 if S[0] != S[1] else K)
        return

    if S[0] == S[-1]:
        c0 = S[0]
        for i in range(1, ls):
            if c0 != S[i]:
                break
        else:
            print(ls*K//2)
            return
        i0 = i
    else:
        i = i0 = 0

    ans = 0
    c0 , cn = "", 0
    for i in range(i, ls):
        c1 = S[i]
        if c1 == c0:
            cn += 1
        else:
            if cn > 1:
                ans += cn // 2
            cn = 1
            c0 = c1

    if i0 == 0:
        if cn > 1:
            ans += cn // 2
        ans *= K
    else:
        ans = ans * K + ((i0+cn)//2) * (K-1) + i0//2 + cn//2


    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):

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

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

count = []
tmp = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    if s[i] != s[i + 1]:
        count.append(tmp + 1)
        tmp = 0
count.append(tmp + 1)


flag = True
ans = 0
if s[0] == s[-1] and (count[-1] > 1 or count[0] > 1) and (count[-1] + count[0]) % 2 == 0:
    flag = False
if s[0] == s[-1]:
    if count[-1] == 1 and count[0] == 1:
        ans += k - 1
        flag = False
    else:
        ans += k - 1
        if count[-1] >= count[0]:
            count[-1] -= 1
        else:
            count[0] -= 1



for i in range(len(count)):
    if count[i] > 1:
        ans += (count[i] // 2) * k
if s[0] == s[-1] and count[-1] >= 1 and flag:
    
    ans += 1
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
k = INT()
ans = 0
a = 0
b = 0
if s[0] == s[-1]:
    if len(set(s)) == 1:
        ans = len(s) * k // 2
        print(ans)
        exit()
    for j in range(len(s)):
        if s[0] == s[j]:
            a += 1
        else:
            break
    for j in reversed(range(len(s))):
        if s[-1] == s[j]:
            b += 1
        else:
            break
    ans = a // 2 + b // 2 + (a + b) // 2 * (k - 1)
i = a
ans2 = 0
while i < len(s) - b:
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    ans2 += tmp
print(ans + ans2 * k)

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

if len(set(S))==1:
  ans = int(len(S)*K/2)
elif K == 1:
  ans = 0
  serial = 1
  
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      serial += 1
    else:
      ans += int(serial/2)
      serial = 1
  ans += int(serial/2)
else:
  
  first=0
  serial = 1
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      serial += 1
    else:
      first += int(serial/2)
      serial = 1
  initial = 1
  for i in range(len(S)-1,1,-1):
    if S[i]==S[i-1]:
      initial += 1
    else:
      break
  cnt = 0
  serial = initial
  
  for i in range(len(S)):
    if S[i] == S[i-1]:
      serial += 1
    else:
      cnt += int(serial/2)
      serial = 1
    
  
      
  
  last = 0
  serial = initial
  for i in range(len(S)):
    if S[i] == S[i-1]:
      serial += 1
    else:
      last += int(serial/2)
      serial = 1
  last += int(serial/2)
  
  ans = first+(K-2)*cnt+last

print(ans)

Solution (Python):

import sys

def splitSeries(s:str):
    result = []
    last = ""
    count = 0
    for c in s:
        if last == c:
            count+=1
        else:
            if last != "":
                result.append((last,count))
            last = c
            count = 1
    if last != "":
        result.append((last,count))
    return result



def solve(S: str, K: int):
    if len(S) == 1:
        print(K//2)
    else:
        count = 0
        series = splitSeries(S)
        sp = 0
        if len(series) > 1:
            for s in series:
                if s[1] > 1:
                    sp += s[1]//2
            count += sp * K
            if series[0][0] == series[-1][0]:
                count -= (K-1)*(series[0][1]//2 + series[-1][1]//2 - (series[0][1] + series[-1][1])//2)
            print(count)
        else:
            print(series[0][1]*K//2)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    K = int(next(tokens))  
    solve(S, K)

if __name__ == '__main__':
    main()

Solution (Python):

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


if len(set(S)) == 1:
    print((N*K) // 2)
    exit()


if S[0] != S[-1]:
    change = 0
    cnt = 1
    for i in range(1,N):
        if S[i] != S[i-1]:
            change += cnt // 2
            cnt = 1
        else:
            cnt += 1


    if cnt != 1:
        change += cnt // 2

    ans = change * K
    print(ans)

else:
    
    head = 1
    tail = 1
    
    for i in range(1,N):
        if S[i] == S[i-1]:
            head += 1
        else:
            break
    
    for i in range(N-2,-1,-1):
        if S[i] == S[i+1]:
            tail += 1
        else:
            break
    change = 0
    cnt = 1
    
    for i in range(head+1, N-tail+1):
        
        if S[i] == S[i-1]:
            cnt += 1
        else:
            change += cnt // 2
            cnt = 1
    
    if cnt != 1:
        change += cnt // 2

        

    ans = ((head + tail) // 2) * (K - 1) + head // 2 + tail // 2 + change * K
    
    print(ans)