Problems > Binomial Modulo Small Prime

Binomial Modulo Small Prime

Difficulty: 2100 4000 ms 128 MB

combinatorics number-theory

Compute C(N, K) modulo a prime P, where N and K are enormous (up to 1018) but P is small.

Input: one line with three integers N, K, P.

Output: C(N, K) mod P.

Constraints:

  • 0 ≤ K ≤ N ≤ 1018
  • 2 ≤ P ≤ 5000, P prime

Sample Tests

Input 1
10 3 7
Output 1
1
Input 2
5 0 11
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.