Skip to content

Commit

Permalink
abc336
Browse files Browse the repository at this point in the history
  • Loading branch information
reo11 committed Jan 15, 2024
1 parent 29ae5fa commit e39c549
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
7 changes: 7 additions & 0 deletions atcoder/abc/abc301-400/abc336/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
n = int(input())
ans = ["L"]
for _ in range(n):
ans.append("o")
ans.append("n")
ans.append("g")
print("".join(ans))
10 changes: 10 additions & 0 deletions atcoder/abc/abc301-400/abc336/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
n = int(input())

ans = 0
while n > 0:
if n & 1 == 0:
ans += 1
else:
break
n >>= 1
print(ans)
16 changes: 16 additions & 0 deletions atcoder/abc/abc301-400/abc336/c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
n = int(input())

# nの5進数を考える
def base_5(n):
ans = []
if n == 0:
return [0]
while n > 0:
ans.append(n % 5)
n //= 5
return ans[::-1]

mapping = [0, 2, 4, 6, 8]
ans = int("".join(map(str, [mapping[x] for x in base_5(n - 1)])))
print(ans)

14 changes: 14 additions & 0 deletions atcoder/abc/abc301-400/abc336/d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
n = int(input())
a = list(map(int, input().split()))
a = [0] + a + [0]

# 左右から挟む
# 左から
possible = [0 for _ in range(len(a))]
for i in range(1, len(a)):
possible[i] = min(a[i], possible[i - 1] + 1)

# 右から
for i in range(len(a) - 2, 0, -1):
possible[i] = min(possible[i], possible[i + 1] + 1)
print(max(possible))
19 changes: 19 additions & 0 deletions atcoder/abc/abc301-400/abc336/e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
n = int(input())

# その桁をxにしたときのmod pを前計算して後でmod pがゼロになるものを数える
# 下の桁からその桁以下を任意の数にしてmod pになるものの数を下から数える
# 9999...が桁和最大で、9 * 15 = 135

counts = [[0 for _ in range(135)] for _ in range(15)]

# 1桁目
for i in range(1, 135):
for j in range(1, 10):
counts[0][i] =

for i in range(1, 15):
for j in range(135):
counts[i][j] = counts[i - 1][j]
counts[i][j] += counts[i - 1][(j - i * (10 ** (i - 1))) % 135]
counts[i][i * (10 ** (i - 1)) % 135] += 1

78 changes: 78 additions & 0 deletions atcoder/abc/abc301-400/abc336/f.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import sys
import pypyjit
from collections import defaultdict
pypyjit.set_param('max_unroll_recursion=-1')
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)
INF = float('inf')

h, w = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(h)]

# 連続して同じところを操作すると元に戻るので、最初だけ4つの選択肢、そのあとは3つの選択肢
# 左上、右上、左下、右下

def is_state_correct(X):
for i in range(h):
for j in range(w):
if X[i][j] != i * w + j + 1:
return False
return True

def spin(pattern_num, X):
# 左上、右上、左下、右下
tmp_X = [[0 for _ in range(w)] for _ in range(h)]
dxy = [[0, 0], [0, 1], [1, 0], [1, 1]]
x = dxy[pattern_num][0]
y = dxy[pattern_num][1]
for i in range(h - 1):
for j in range(w - 1):
tmp_X[i + x][j + y] = X[h - 2 - i + x][w - 2 - j + y]
if y == 0:
for i in range(h):
tmp_X[i][w - 1] = X[i][w - 1]
else:
for i in range(h):
tmp_X[i][0] = X[i][0]
if x == 0:
for j in range(w):
tmp_X[h - 1][j] = X[h - 1][j]
else:
for j in range(w):
tmp_X[0][j] = X[0][j]
return tmp_X

def state_str(X):
return '_'.join(['_'.join(map(str, x)) for x in X])

def display(X):
return '\n'.join([' '.join(map(str, x)) for x in X])

visited = defaultdict(lambda: INF)
visited[state_str(s)] = 0

ans = INF
def dfs(X, pre_pattern_num = -1, depth = 1):
for i in range(4):
if i == pre_pattern_num:
continue
tmp_X = spin(i, X)
state = state_str(tmp_X)
if visited[state] < depth:
continue
visited[state] = depth
if depth + 1 >= 21:
continue
dfs(tmp_X, i, depth + 1)

if is_state_correct(s):
print(0)
exit()

dfs(s)
state = "_".join([str(x + 1) for x in range(h * w)])
ans = visited[state]
if ans == INF:
print(-1)
else:
print(ans)
Empty file.

0 comments on commit e39c549

Please sign in to comment.