Skip to content

Instantly share code, notes, and snippets.

@maehrm
Last active February 13, 2026 13:46
Show Gist options
  • Select an option

  • Save maehrm/e72398bee8b4b4ddf95d491ff3ddd26e to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/e72398bee8b4b4ddf95d491ff3ddd26e to your computer and use it in GitHub Desktop.
def solv(S):
if len(S) <= 2:
return int(S) % 8 == 0 or int(S[::-1]) % 8 == 0
else: # 3桁以上の場合:3桁の8の倍数が作れるかを判定
# 各数字の出現回数
digit_count = [0] * 10
for ch in S:
digit_count[int(ch)] += 1
# 3桁の8の倍数をチェック
for multiple in range(104, 1000, 8):
need_count = [0] * 10
for ch in str(multiple):
need_count[int(ch)] += 1
# 作れるか判定
possible = True
for d in range(10):
if need_count[d] > digit_count[d]:
possible = False
break
if possible:
return True
return False
S = input()
print("Yes") if solv(S) else print("No")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment