Under construction

Still being built. Things may change

DivModulo

Time limit3sMemory limit1024 MB

Summary
Compute C(M,N) after removing every factor of D, and report the remainder modulo D, for M up to 4e18 and D up to 1.6e7.
Level

Hard9 of 10

Topics
Math, Number theory, Combinatorics, Implementation
Solved
No attempts yet

Problem

Modulo (mod) is a very common operator on integers. For two integers nn and dd with d>0d > 0, r≡(n mod d)r \equiv (n \bmod d) is defined as the value rr such that 0≤r<d0 \le r < d and there exists an integer qq with n=q×d+rn = q \times d + r. For example, (200 mod 5)≡0(200 \bmod 5) \equiv 0 means the remainder of 200 divided by 5 is 0.

Here is another new operator called DivModulo (dmod) defined as follows. For two integers nn and dd with d>0d > 0, r≡(ndmodd)r \equiv (n \mathbin{\mathrm{dmod}} d) is defined as the value rr such that r≡(m mod d)r \equiv (m \bmod d) where there exist integers mm and hh with n=m×dhn = m \times d^h and dd is not a factor of mm. For example, (200dmod5)≡3(200 \mathbin{\mathrm{dmod}} 5) \equiv 3, since 200=8×52200 = 8 \times 5^2 and (8 mod 5)≡3(8 \bmod 5) \equiv 3.

Consider the factorials and the combination function. For an integer M≥0M \ge 0, the factorial M!M! is defined as M!=M×(M−1)×(M−2)×⋯×3×2×1M! = M \times (M-1) \times (M-2) \times \cdots \times 3 \times 2 \times 1, and 0!=10! = 1 is defined. For integers MM and NN with 0≤N≤M0 \le N \le M, the combination function C(M,N)C(M, N) is defined as C(M,N)=M!/(N!×(M−N)!)C(M, N) = M!/(N! \times (M-N)!). Now given three integers MM, NN, DD with D>0D > 0, compute C(M,N)dmodDC(M, N) \mathbin{\mathrm{dmod}} D. For example, (C(9,2)dmod3)≡(36dmod3)≡(4×32dmod3)≡(4 mod 3)≡1(C(9, 2) \mathbin{\mathrm{dmod}} 3) \equiv (36 \mathbin{\mathrm{dmod}} 3) \equiv (4 \times 3^2 \mathbin{\mathrm{dmod}} 3) \equiv (4 \bmod 3) \equiv 1.

Input

Three integers MM, NN, and DD are given in one line.

Output

Print C(M,N)dmodDC(M, N) \mathbin{\mathrm{dmod}} D in one line.

Constraints

  • 1≤M≤4×10181 \le M \le 4 \times 10^{18}
  • 0≤N≤M0 \le N \le M
  • 2≤D≤1.6×1072 \le D \le 1.6 \times 10^7

Examples4

  1. Example 1

    Input
    9 2 3
    
    Expected output
    1
    
  2. Example 2

    Input
    5 2 5
    
    Expected output
    2
    
  3. Example 3

    Input
    6 3 6
    
    Expected output
    2
    
  4. Example 4

    Input
    7654321 1234567 1050
    
    Expected output
    210