mod shiftについて
- listの回転について右と左に回転する方法
左回転
L = list(range(10))
size = len(L)
print(L)
shift = 0
# 左に回転
for k in range(20):
shift += 1
print([L[(i+shift)%size] for i in range(10)])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
[3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
[7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
[9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
右回転
L = list(range(10))
size = len(L)
print(L)
shift = 0
# 右に回転
for k in range(20):
shift = (shift + size - 1) % size
print([L[(i+shift)%size] for i in range(10)])
[9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
[7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
[6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
[3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
例; 典型問題
問題
解答
N, Q = map(int, input().split())
(*A,) = map(int, input().split())
shifts = 0
for _ in range(Q):
T, x, y = map(int, input().split())
if T == 1:
x -= 1
y -= 1
A[(x + shifts) % N], A[(y + shifts) % N] = A[(y + shifts) % N], A[(x + shifts) % N]
if T == 2:
shifts = (shifts + N - 1) % N
if T == 3:
x -= 1
print(A[(x + shifts) % N])