Double Modular Exponentiation - Problem
You are given a 0-indexed 2D array variables where variables[i] = [ai, bi, ci, mi], and an integer target.
An index i is good if the following formula holds:
((aibi % 10)ci) % mi == target
Return an array consisting of good indices in any order.
Input & Output
Example 1 — Basic Case
$
Input:
variables = [[2,3,3,10],[3,1,4,12],[7,3,1,17],[4,2,5,5]], target = 3
›
Output:
[2]
💡 Note:
For index 2: ((7³ % 10)¹) % 17 = (343 % 10)¹ % 17 = 3¹ % 17 = 3. Only index 2 matches target 3.
Example 2 — Multiple Matches
$
Input:
variables = [[39,3,1000,1000]], target = 49
›
Output:
[0]
💡 Note:
For index 0: ((39³ % 10)¹⁰⁰⁰) % 1000 = (59319 % 10)¹⁰⁰⁰ % 1000 = 9¹⁰⁰⁰ % 1000 = 49.
Example 3 — No Matches
$
Input:
variables = [[2,2,2,2]], target = 5
›
Output:
[]
💡 Note:
For index 0: ((2² % 10)²) % 2 = (4²) % 2 = 16 % 2 = 0 ≠ 5. No indices match.
Constraints
- 1 ≤ variables.length ≤ 1000
- variables[i] = [ai, bi, ci, mi]
- 1 ≤ ai, bi, ci, mi ≤ 106
- 0 ≤ target ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
2D array of variables and target value
2
Process
Apply double modular exponentiation formula to each row
3
Output
Array of indices where formula equals target
Key Takeaway
🎯 Key Insight: Use modular exponentiation to efficiently handle large powers while preventing integer overflow
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code