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.
Use mod to keep the value inside the default int range for best performance
Python code:
defsolve(a,b,k,m):defmul(r1,i1,r2,i2):return(r1*r2-i1*i2)%m,(r1*i2+r2*i1)%mk2=bin(k)[2:][::-1]# dp_i := (a + bj) ^ (2*i), i is index, j is image unitdp=(a,b)dpi=0real,imag=1,0forninrange(len(k2)):ifk2[n]=='1':whilen!=dpi:dpi+=1dp=mul(*dp,*dp)real,imag=mul(real,imag,*dp)returnreal,imag
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Russian Peasant Exponentiation
You are viewing a single comment's thread. Return to all comments →
Python code: