結果

問題 No.1255 ハイレーツ・オブ・ボリビアン
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-23 14:08:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 635 ms / 2,000 ms
コード長 783 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 81,716 KB
実行使用メモリ 127,444 KB
最終ジャッジ日時 2023-10-22 08:18:41
合計ジャッジ時間 4,172 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,708 KB
testcase_01 AC 39 ms
53,708 KB
testcase_02 AC 44 ms
59,696 KB
testcase_03 AC 43 ms
59,696 KB
testcase_04 AC 44 ms
59,696 KB
testcase_05 AC 47 ms
61,872 KB
testcase_06 AC 48 ms
61,872 KB
testcase_07 AC 48 ms
61,872 KB
testcase_08 AC 278 ms
114,676 KB
testcase_09 AC 296 ms
114,232 KB
testcase_10 AC 276 ms
112,336 KB
testcase_11 AC 283 ms
112,268 KB
testcase_12 AC 291 ms
114,280 KB
testcase_13 AC 70 ms
84,836 KB
testcase_14 AC 635 ms
127,444 KB
testcase_15 AC 272 ms
111,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def xgcd(a, b):
  x0,y0,x1,y1=1,0,0,1
  while b!=0:
    q,a,b=a//b,b,a%b
    x0,x1=x1,x0-q*x1
    y0,y1=y1,y0-q*y1
  return x0
memo={}
# mod m におけるaの逆元。gcd(a,m)=1
def modinv(a, m):
  if (a,m) in memo:return memo[(a,m)]
  x=xgcd(a, m)
  memo[(a,m)]=x%m
  return x % m

def main2(n):
  if n==1:return 1
  # 2^i==1 mod 2*n-1 となるiを返す
  mod=2*n-1
  d={0:1}
  now=2
  c=int(n**0.5)
  for i in range(c+3):
    if now==1:
      return i+1
    d[now]=i+1
    now=now*2%mod
  inv2=modinv(2,mod)
  gs=pow(inv2,c,mod)
  now=gs
  cnt=1
  while True:
    if now in d:
      # 2^(-c*cnt)=2^d[now]
      # 1=2^(c*cnt+d[now])
      return d[now]+c*cnt
    now=now*gs%mod
    cnt+=1

t=int(input())
cases=[int(input()) for _ in range(t)]
for n in cases:
  print(main2(n))
0