ABC152 D - Handstand 2
https://atcoder.jp/contests/abc152/tasks/abc152_d
提出
TLE
code: python
n = int(input())
pair = {}
for i in range(1, n+1):
pairi = set()
for i in range(1, n+1):
for j in range(1, n+1):
if (int(str(i)-1) == int(str(j)0) and int(str(i)0) == int(str(j)-1)):
pairi.add(j)
ans = 0
for v in pair.values():
ans += len(v)
print(ans)
解答
code: python
n = int(input())
cnt = dict()
for top in range(10):
for bottom in range(10):
cnt(top, bottom) = 0
for i in range(1, n + 1):
top = int(str(i)0)
bottom = int(str(i)-1)
cnt(top, bottom) += 1
result = 0
# 先頭が top、末尾が bottom の数が、N 以下にいくつあるのかを数える
for top in range(10):
for bottom in range(10):
result += cnt(top, bottom) * cnt(bottom, top)
print(result)
メモ
AtCoder Beginner Contest 152 D – Handstand 2 をPython3で解く
提出
code: python
n = int(input())
# (2, 2*2)
# (123, 3*1)
# (123456, 1*6)