結果

問題 No.1339 循環小数
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-12 13:24:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 462 ms / 2,000 ms
コード長 1,385 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 131,060 KB
最終ジャッジ日時 2023-09-26 12:49:06
合計ジャッジ時間 10,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,924 KB
testcase_01 AC 81 ms
76,192 KB
testcase_02 AC 83 ms
76,208 KB
testcase_03 AC 81 ms
76,036 KB
testcase_04 AC 81 ms
76,184 KB
testcase_05 AC 82 ms
76,044 KB
testcase_06 AC 80 ms
76,128 KB
testcase_07 AC 79 ms
76,184 KB
testcase_08 AC 79 ms
76,192 KB
testcase_09 AC 78 ms
76,588 KB
testcase_10 AC 79 ms
76,104 KB
testcase_11 AC 82 ms
76,520 KB
testcase_12 AC 82 ms
76,184 KB
testcase_13 AC 83 ms
76,404 KB
testcase_14 AC 82 ms
76,456 KB
testcase_15 AC 84 ms
76,392 KB
testcase_16 AC 85 ms
76,512 KB
testcase_17 AC 84 ms
76,400 KB
testcase_18 AC 83 ms
76,472 KB
testcase_19 AC 83 ms
76,344 KB
testcase_20 AC 83 ms
76,312 KB
testcase_21 AC 400 ms
127,660 KB
testcase_22 AC 401 ms
128,624 KB
testcase_23 AC 403 ms
130,240 KB
testcase_24 AC 405 ms
129,164 KB
testcase_25 AC 410 ms
130,632 KB
testcase_26 AC 406 ms
129,392 KB
testcase_27 AC 404 ms
129,136 KB
testcase_28 AC 409 ms
127,344 KB
testcase_29 AC 389 ms
130,296 KB
testcase_30 AC 397 ms
131,060 KB
testcase_31 AC 413 ms
129,464 KB
testcase_32 AC 417 ms
129,224 KB
testcase_33 AC 399 ms
128,200 KB
testcase_34 AC 221 ms
104,076 KB
testcase_35 AC 462 ms
124,940 KB
testcase_36 AC 390 ms
127,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main1(n):
  # now=1からはじめ、以下を繰り返す。
  # ・now%=n
  # ・now*=10
  # 過去に一度出たnowが再び出たらそこから循環する。nowの取りうる値は0~n-1なので循環は高々n個から成る。
  # なのでn回操作を行った後のnowは必ず循環に入る。n回操作を行った後のnowから探索を開始してよい。
  # このnowをv0とする。
  # baby-step-giant-stepでできそう。
  # 0.c=int(n**0.5)+1とする。
  # 1.v0からc-1回操作を行う(baby-step)。この時v0が出てきたら循環するので操作は終了。
  # 2.v0にc回操作を一回にまとめた操作を行う(giant-step)。1で出た値が出たら循環する。この操作は必ず終わる。
  # 合計O(√n)
  # n未満の整数は、c=int(n**0.5)+1として、ある整数a,b(0<=a,b<c)があり、a*c+bと一意に表すことができる。
  # aがgiant-step、bがbaby-step
  v0=pow(10,n,n)
  c=int(n**0.5)+1
  d={}
  # baby-step
  now=v0
  for i in range(c):
    d[now]=i
    now*=10
    now%=n
    if now==v0:return i+1
  # giant-step
  now=v0
  gs=pow(10,c,n)
  for i in range(c):
    pre=now
    now*=gs
    now%=n
    if now in d:return (i+1)*c-d[now]
  return n

if __name__=='__main__':
  t=int(input())
  cases=[int(input()) for _ in range(t)]
  for n in cases:
    ret1=main1(n)
    print(ret1)
0