Modular Exponentiation of a Polynomial at a Point
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests the ability to evaluate a polynomial modulo a prime, raise the result to a large power modulo the same prime, and apply Fermat's little theorem for efficiency.
Modular Exponentiation of a Polynomial at a Point
You are given:
- A list of integer coefficients
coeffsrepresenting a polynomial P wherecoeffs[i]is the coefficient of x^i (index 0 is the constant term). - An integer
x— the point at which to evaluate the polynomial. - A non-negative integer
exp— the exponent to raise the evaluated result to. - A prime integer
p— the modulus.
Your task is to compute:
(P(x))^exp mod p
where P(x) = coeffs[0] + coeffs[1]·x + coeffs[2]·x² + … + coeffs[n-1]·x^(n-1).
To keep intermediate values manageable, reduce every intermediate computation modulo p.
Key requirement: large exponents
The exponent exp can be astronomically large (up to 10^18). You must use Fermat's Little Theorem to reduce the effective exponent before performing fast modular exponentiation:
Since p is prime and the base may be non-zero mod p, use the fact that a^(p−1) ≡ 1 (mod p) for a not divisible by p.
Specifically:
- If
P(x) mod p == 0, the answer is 0 (for anyexp > 0), or 1 ifexp == 0. - Otherwise, reduce
expmodulo(p − 1)before computing the power, then apply fast modular exponentiation.
Constraints
1 ≤ len(coeffs) ≤ 500- Each coefficient satisfies
-10^9 ≤ coeffs[i] ≤ 10^9 -10^9 ≤ x ≤ 10^90 ≤ exp ≤ 10^18pis a prime,3 ≤ p ≤ 10^9 + 7- The answer is always a single integer in
[0, p − 1]
Examples
Example 1
Input: poly_power_mod([1, 2, 1], 3, 4, 7)
Output: 2
Example 2
Input: poly_power_mod([0, 1], 5, 1000000000000000000, 1000000007)
Output: 921116510
Example 3
Input: poly_power_mod([3], 0, 0, 13)
Output: 1
Hints
Hint 1
First evaluate P(x) mod p using Horner's method or direct summation, reducing mod p at each step to avoid overflow.
Hint 2
Apply Fermat's Little Theorem: if the base b = P(x) mod p is non-zero, then b^exp mod p == b^(exp mod (p-1)) mod p — this shrinks the exponent before calling pow().
Hint 3
Handle the two special cases separately: exp == 0 always gives 1, and base == 0 with exp > 0 always gives 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Step 1 — Evaluate P(x) mod p
Use Horner's method to evaluate the polynomial efficiently in O(n) time, reducing modulo p at each step:
P(x) = a0 + x*(a1 + x*(a2 + ... x*(a_{n-1})...))
We iterate from the highest-degree coefficient down, computing val = val * x + coeff at each step, always taken mod p. This avoids computing large powers of x explicitly.
Step 2 — Handle special cases
- If
exp == 0: return 1 unconditionally (any value to the 0th power is 1). - If
val == 0(andexp > 0): return 0 immediately.
Step 3 — Apply Fermat's Little Theorem
Since p is prime and val ≢ 0 (mod p), by Fermat's Little Theorem:
val^(p−1) ≡ 1 (mod p)
Therefore:
val^exp ≡ val^(exp mod (p−1)) (mod p)
We reduce exp to exp % (p − 1) (a number in [0, p−2]), then use Python's built-in pow(val, reduced_exp, p) which internally uses fast modular exponentiation (binary exponentiation) in O(log exp) time.
Why this is correct
Without Fermat's reduction, exp can be up to 10^18. Binary exponentiation alone handles this in ~60 multiplications, so even without FLT the answer would be correct — but the problem specifically exercises understanding of FLT as a number-theoretic optimization and conceptual tool. The reduced exponent is mathematically equivalent by the theorem.
Note: when reduced_exp == 0 (meaning p−1 divides exp), pow(val, 0, p) returns 1, which is correct since val ≠ 0.
Complexity
- Time: O(n + log p) — O(n) for polynomial evaluation via Horner, O(log(p−1)) for modular exponentiation (the reduced exponent is at most p−2 < p).
- Space: O(1) extra.
Reference solution
from typing import List
def poly_power_mod(coeffs: List[int], x: int, exp: int, p: int) -> int:
# Step 1: Evaluate P(x) mod p using Horner's method
val = 0
x_mod = x % p
for coeff in reversed(coeffs):
val = (val * x_mod + coeff % p) % p
# Step 2: Handle edge cases
if exp == 0:
return 1 # anything^0 = 1
if val == 0:
return 0 # 0^exp = 0 for exp > 0
# Step 3: Apply Fermat's Little Theorem to reduce the exponent
# Since p is prime and val != 0 mod p: val^(p-1) ≡ 1 (mod p)
reduced_exp = exp % (p - 1)
return pow(val, reduced_exp, p)