Number theory · Medium
A deterministic numeric simulator applies the same multiplicative transformation many times and keeps only its remainder. The exponent can be too large to repeat the multiplication once per step. This is arithmetic practice, not a cryptographic implementation.
Return base raised to exponent modulo modulus as the canonical non-negative remainder.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ base: integer, exponent: integer, modulus: integer }
Fields
- base is a signed integer multiplier; exponent is a non-negative integer number of repetitions.
- modulus is a positive integer.
Output
Return value
- Return an integer from 0 through modulus - 1. Negative bases must use a non-negative remainder.
- Exponent zero returns 1 modulo modulus, including base zero. Modulus one always returns zero.
Constraints
- -1000000000 <= base <= 1000000000; 0 <= exponent <= 1000000000; 1 <= modulus <= 1000000000.
- Multiplication can exceed safe Number precision. Use exact intermediate integer arithmetic.
Examples
Example 1
{
"base": 3,
"exponent": 4,
"modulus": 10
}Returns
1Example 2
{
"base": -2,
"exponent": 3,
"modulus": 5
}Returns
2Required complexity
O(log(exponent + 1)) exact-integer multiplications and O(1) accumulators