Fast Power Mod Function
Python
Hard
3 views
Problem Description
Read a b m, write function modpow(a,b,m) using binary exponent and output a^b mod m.
Input Format
One line: a b m.
Output Format
One integer ans.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<3: sys.exit(0)
a=int(p[0]); b=int(p[1]); m=int(p[2])
def modpow(x,y,mod):
x%=mod
res=1%mod
while y>0:
if y&1:
res=(res*x)%mod
x=(x*x)%mod
y//=2
return res
sys.stdout.write(str(modpow(a,b,m)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!