Skip to content

Instantly share code, notes, and snippets.

@uchidama
Created July 12, 2021 09:00
Show Gist options
  • Save uchidama/1481b9c489a379c28b87d74daabf848b to your computer and use it in GitHub Desktop.
Save uchidama/1481b9c489a379c28b87d74daabf848b to your computer and use it in GitHub Desktop.
AtCoder Beginner Contest 174 [ C - Repsept ] https://atcoder.jp/contests/abc174/tasks/abc174_c
'''
[問題]
https://atcoder.jp/contests/abc174/tasks/abc174_c
[解法]
https://youtu.be/h0MGG8rxrYc?t=1654
数列 7, 77, 777, 7777, ....は
漸化式 *10+7 で表現できる。
これはmod(余り)をとった値でも同様。
7%K の次の77%K も *10+7の関係にある。
それを利用して、K回この処理を繰り返せば、とりうる余りは1ループする。
この余りのループの中に0があれば、それがKの倍数
こんなん、説明されなきゃ絶対わからん(^^;
'''
import sys
sys.setrecursionlimit(10 ** 6) # 再帰上限の引き上げ
input = sys.stdin.readline
INF = 2 ** 63 - 1
K = int(input())
x = 7%K
for i in range(1,K + 1):
if x == 0:
print(i)
exit()
x = (x*10+7)%K
print(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment