Lv.2divide-and-conquermath
Modular Exponentiation
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
For each query [a, b, m] return (with ).
Examples
| queries | result |
|---|---|
[[2, 10, 1000], [3, 0, 7], [10, 18, 7], [5, 3, 1]] | [24, 1, 1, 0] |
[[7, 222, 13]] | [12] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(queries: long[][]) → long[]
Constraints
-
queries.length - , ,
Expected complexity
- Time
- O(Q log B)
- Space
- O(1)
Examples
| queries | Expected |
|---|---|
[[2,10,1000],[3,0,7],[10,18,7],[5,3,1]] | [24,1,1,0] |
[[7,222,13]] | [12] |
Hint
Hint
Square-and-multiply: square the base each step and multiply into the result where b has a 1 bit, reducing modulo m after
every multiplication (products stay below ). Start from 1 % m. Multiplying b times exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 258 ms | estimated |
| C++ (GCC, gnu++23) | 258 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1638 ms | derived |
| Kotlin 2.4 (JVM) | 1516 ms | estimated |
| C# 14 (.NET 10) | 1516 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 5 | verdict only |
| performance | 60 | 4 | verdict only |