Problem:


Score : 900 points

Problem Statement

Takahashi has a lot of peculiar devices.
These cylindrical devices receive balls from left and right.
Each device is in one of the two states A and B, and for each state, the device operates as follows:

When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.

When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.

The transition of the state of a device happens momentarily and always completes before it receives another ball.

Takahashi built a contraption by concatenating N of these devices. In this contraption,

A ball that was thrown out from the right side of the i-th device from the left (1 \leq i \leq N-1) immediately enters the (i+1)-th device from the left side.

A ball that was thrown out from the left side of the i-th device from the left (2 \leq i \leq N) immediately enters the (i-1)-th device from the right side.

The initial state of the i-th device from the left is represented by the i-th character in a string S.
From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end.
Here, it can be proved that the ball always comes out of the contraption after a finite time.
Find the state of each device after K balls are processed.

Constraints

1 \leq N \leq 200,000

1 \leq K \leq 10^9

|S|=N

Each character in S is either A or B.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print a string that represents the state of each device after K balls are processed.
The string must be N characters long, and the i-th character must correspond to the state of the i-th device from the left.

Sample Input 1

5 1
ABAAA

Sample Output 1

BBAAA

In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.

Sample Input 2

5 2
ABAAA

Sample Output 2

ABBBA

Sample Input 3

4 123456789
AABB

Sample Output 3

BABA



Sample input:
5 1
ABAAA


Sample output:
BBAAA

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque

# f(AX) = XB
# f(BX) = flip(X) + A = flip(XB)

N,K = map(int,readline().split())
S = read().rstrip()
S = deque(0 if x == b'A'[0] else 1 for x in S)

# Kが大きいときは、周期で減らせる
U = 10**6
if K > U:
    K = U + (K&1)

flip = 0
for _ in range(K):
    top = S[0]^flip
    if top == 0:
        # A
        S[0] ^= 1
    else:
        # B 
        flip ^= 1
        S.popleft()
        S.append(flip)

if flip == 0:
    A = 'A'; B = 'B'
else:
    A = 'B'; B = 'A'
answer = ''.join(A if x == 0 else B for x in S)
print(answer)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=list(input())*3
p=['A' if s[i]=='B' else 'B' for i in range(n)]*3
head,tail=0,n
if k>2*n:
	k=2*n+k%2
for i in range(k):
	if s[head]=='A':
		s[head],p[head]='B','A'
	else:
		s,p=p,s
		head+=1
		s[tail],p[tail]='A','B'		
		tail+=1
print(''.join(s[head:tail]))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=list(input())*3
p=['A' if s[i]=='B' else 'B' for i in range(n)]*3
head,tail=0,n
for i in range(2*n):
	if s[head]=='A':
		s[head],p[head]='B','A'
	else:
		s,p=p,s
		head+=1
		s[tail],p[tail]='A','B'		
		tail+=1
	if i==k-1:
		print(''.join(s[head:tail]))
		break
else:
	if n%2==1 and k%2==1:
		if s[head]=='A':
			s[head],p[head]='B','A'
		else:
			s,p=p,s
			head+=1
			s[tail],p[tail]='A','B'		
			tail+=1
	print(''.join(s[head:tail]))

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
from collections import deque

def solve():
    N, K = map(int, input().split())
    S = input()

    '''
    if N == 1:
        if K % 2 == 0:
            print(S)
        else:
            print('A' if S == 'B' else 'B')

        return
    '''

    q = deque([1 if c == 'A' else 0 for c in S])
    cb = 0

    for i in range(min(K, 2 * N + K % 2)):
        if q[0] == (cb ^ 1):
            q[0] ^= 1
        else:
            q.popleft()
            q.append(cb)
            cb ^= 1

    # print(q)

    ans = ['A' if (c ^ cb) else 'B' for c in q]
    print(''.join(ans))


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(400020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 400020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if n % 2 and k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())*2
p=['A' if s[i]=='B' else 'B' for i in range(n)]*2
head,tail=0,n
for i in range(2*n):
	if s[0]=='A':
		s[0],p[0]='B','A'
	else:
		s,p=p,s
		head+=1
		s[tail],p[tail]='A','B'
		tail+=1
	if i==k-1:
		print(''.join(s[head:tail]))
		break
else:
	if n%2==1 and k%2==1:
		if s[0]=='A':
			s[0],p[0]='B','A'
		else:
			s,p=p,s
			head+=1
			s[tail],t[tail]='A','B'
			tail+=1
	print(''.join(s[head:tail]))

Submission status: Runtime Error

Solution code:
import sys
import math

def ok(n,k):
	if k<0:
		return 0
	
	v = 9*(n+k)
	d = 0
	while v > 0:
		d += v%10
		v /= 10
	return 9*k>=d

N=input()
K=1<<20
for i in range(19,-1,-1):
	if ok(N,K-(1<<i)):
		K -= 1<<i

print K

Submission status: Runtime Error

Solution code:
import sys

INF = 10**9+7

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def nal(): return list(map(int, stdin.readline().split()))
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())

n, k = nal()
s = nsl()

st = s[0]
lst = []
c=1
for i in range(1, n):
    if(st == s[i]):
        c += 1
    else:
        lst.append(c)
        c = 1
        st = s[i]
lst.append(c)
st = s[0]

for i in range(k):
    if(st == 'A'):
        if(lst[0] == 1):
            lst.pop(0)
            lst[0] += 1
        else:
            lst.insert(0, 1)
            lst[1] -= 1
        st = 'B'

    else:
        lst.insert(0, 1)
        for j in range(len(lst)//2):
            t = j*2+1
            lst[t] = lst[t] + lst[t+1] - 2
            lst[t+1] = 2

        if(len(lst) % 2 == 1):
            lst[-1] -= 1
        else:
            lst[-2] = lst[-1] + 1
            lst.pop(-1)

        st = 'A'

st = s[0]
ans = []
for i in range(len(lst)):
    for j in range(lst[i]):
        ans.append(st)
    if(st == 'A'):
        st = 'B'
    else:
        st = 'A'
print("".join(ans))

Submission status: Runtime Error

Solution code:
import sys

INF = 10**9+7

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def nal(): return list(map(int, stdin.readline().split()))
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())

def func(n, k, s):
    st = s[0]
    lst = []
    c=1
    for i in range(1, n):
        if(st == s[i]):
            c += 1
        else:
            lst.append(c)
            c = 1
            st = s[i]
    lst.append(c)
    st = s[0]

    for i in range(k):
        if(st == 'A'):
            if(lst[0] == 1):
                lst.pop(0)
                lst[0] += 1
            else:
                lst.insert(0, 1)
                lst[1] -= 1
            st = 'B'

        else:
            lst.insert(0, 1)
            for j in range(len(lst)//2):
                t = j*2+1
                lst[t] = lst[t] + lst[t+1] - 2
                lst[t+1] = 2

            if(len(lst) % 2 == 1):
                lst[-1] -= 1
            else:
                lst[-2] = lst[-1] + 1
                lst.pop(-1)

            st = 'A'

    st = s[0]
    ans = []
    for i in range(len(lst)):
        for j in range(lst[i]):
            ans.append(st)
        if(st == 'A'):
            st = 'B'
        else:
            st = 'A'
    print("".join(ans))




n, k = nal()
s = nsl()

func(n, k, s)

Submission status: Runtime Error

Solution code:
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(400020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 400020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if not k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(400020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 400020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if not k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()

Submission status: Runtime Error

Solution code:
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(800020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 800020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(800020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 800020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()