Huge Power Mod (Exponent as String)
Python
Hard
4 views
Problem Description
Integers a and m are provided, and exponent b is provided as a very large decimal string. Output (a^b) % m.
Input Format
Line1: a m. Line2: b (string).
Output Format
One integer ans.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
a,m=map(int,lines[0].split())
b=lines[1].strip()
res=1% m
base=a % m
for ch in b:
d=ord(ch)-48
res=pow(res,10,m)
res=(res*pow(base,d,m))%m
sys.stdout.write(str(res))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!