Lv.3combinatoricsmathmodular-inverse
Binomial Coefficient Queries
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
For each query [n, r] return .
Examples
| queries | result |
|---|---|
[[5, 2], [10, 0], [10, 10], [6, 3]] | [10, 1, 1, 20] |
[[1000000, 500000]] | [996692777] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(queries: int[][]) → int[]
Constraints
-
queries.length,
Expected complexity
- Time
- O(MAX_N + Q)
- Space
- O(MAX_N)
Examples
| queries | Expected |
|---|---|
[[5,2],[10,0],[10,10],[6,3]] | [10,1,1,20] |
[[1000000,500000]] | [996692777] |
Hint
Hint
Precompute factorials and inverse factorials modulo the prime (one Fermat inverse, then inv_fact[i-1] = inv_fact[i] · i) and
answer each query as fact[n] · inv_fact[r] · inv_fact[n-r]. Recomputing each coefficient is .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1578 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 5 | verdict only |
| performance | 60 | 4 | verdict only |