We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTdefeuclid(a,b):ifb==0:return(a,1,0)q=a// bg,u,v=euclid(b,a-q*b)return(g,v,u-q*v)defmodInverse(a,m):(g,u,v)=euclid(a,m)assert(g==1)returnu%mdefdpow(x,p,one,mul):ret=onewhilep>0:ifp%2==1:ret=mul(ret,x)p//= 2x=mul(x,x)returnret# find min t <=limit: x^t==ydefdLog(x,y,one,mul,inv,limit):step=1whilestep*step<limit:step+=1d=dict()invx=inv(x)cur=mul(invx,y)foriinrange(1,step+1):ifcurnotind:d[cur]=icur=mul(cur,invx)ret=limit+1cur=onexstep=dpow(x,step,one,mul)foriinrange(step):ifcurind:ret=min(ret,step*i+d[cur])cur=mul(cur,xstep)return-1ifret==limit+1elseretdefmul(p1,p2):a1,b1=p1a2,b2=p2return((a1*a2+b1*b2*5)%m,(a1*b2+a2*b1)%m)defnorm(p):a,b=preturn(a*a-b*b*5)%mdefinv(p):assert(norm(p)==1)a,b=preturn(a,-b)tests=int(input())fortestinrange(tests):a,b,m=map(int,input().split())x=(a,b)xlen=norm(x)ifeuclid(xlen,m)[0]!=1:print(-1)continueplen=dLog(xlen,1,1,lambdaa,b:(a*b)%m,lambdaa:modInverse(a,m),m)x=dpow(x,plen,(1,0),mul)pdir=dLog(x,(1,0),(1,0),mul,inv,10*m)assert(plen!=-1)print(plen*pdir)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Period
You are viewing a single comment's thread. Return to all comments →
Python3 solution