結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,660 KB
testcase_01 AC 39 ms
53,660 KB
testcase_02 AC 44 ms
59,644 KB
testcase_03 AC 44 ms
59,644 KB
testcase_04 AC 44 ms
59,644 KB
testcase_05 AC 48 ms
63,868 KB
testcase_06 AC 48 ms
63,868 KB
testcase_07 AC 48 ms
63,868 KB
testcase_08 AC 378 ms
132,900 KB
testcase_09 AC 394 ms
138,704 KB
testcase_10 AC 384 ms
134,304 KB
testcase_11 AC 388 ms
134,328 KB
testcase_12 AC 398 ms
134,484 KB
testcase_13 AC 87 ms
99,404 KB
testcase_14 AC 697 ms
170,848 KB
testcase_15 AC 371 ms
135,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# baby-step-giant-step
# 離散対数問題(Discrete Logarithm Problem)を解くアルゴリズム。
# あるX,Y,M について X^K≡Y (mod M) となる K を求める。
# X,Mは互いに素

def bsgs(x,y,m):
  c=int(m**0.5)+1
  d={}
  # baby-step
  now=1
  for i in range(c):
    d[now]=i
    now*=x
    now%=m
    if now==y:return i+1
  # giant-step
  gs=modinv(now,m)
  now=y
  for i in range(c):
    now*=gs
    now%=m
    if now in d:return (i+1)*c+d[now]
  return m

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
  return bsgs(2,1,mod)
  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