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 STDOUTfromfractionsimportgcd# Sieve primeslim=111111primes=[0]*2+[1]*(lim-2)pc=0foriinrange(2,lim):ifprimes[i]:primes[pc]=ipc+=1forjinrange(i*i,lim,i):primes[j]=0defprime_fac(n):'''yieldsallprime-exponentpairsofthefactorizationofn'''ifn==1:returnforpiinrange(pc):p=primes[pi]ifp*p>n:p=ne=0whilen%p==0:n//= p e+=1ife:yieldp,eifn==1:breakdefphi(n):'''Euler'stotientofn'''forp,einprime_fac(n):n-=n// preturnndivs=[1]*limdefdivisors(n):'''yieldsalldivisorsofn'''yield1,1dc=1forp,einprime_fac(n):odc=dcfortinrange(odc*e):old=divs[dc-odc]new=divs[dc]=old*pdc+=1yieldnew,oldz=int(input())assert1<=z<=1000forcasinrange(z):n,a=map(int,input().strip().split())assert1<=n<=10**9and1<=abs(a)<=nm=2*n+1m//= gcd(a, m)dp={}fork,koldinsorted(divisors(phi(m))):dp[k]=pow(dp[kold],k// kold, m) if k > 1 else 2 % mifdp[k]==1:print(2*n-k)break
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ichigo and Rooms
You are viewing a single comment's thread. Return to all comments →
Working Python3 code