The intervoid method is used to obtain the remainder of a large group.
# Include <cstdio> # include <cstring> using namespace STD; typedef long ll; // evaluate the integer x and y, so that ax + by = D, and | x | + | Y | minimum. Where d = gcd (a, B) void gcd (ll a, LL B, ll & D, ll & X, ll & Y) {If (! B) {d = A; X = 1; y = 0;} else {gcd (B, A % B, D, Y, X ); y-= x * (a/B);} // calculate the inverse of a under the modulo n. If there is no inverse, return-1 LL inv (ll a, ll N) {ll D, X, Y; gcd (A, N, D, x, y ); return d = 1? (X + n) % N:-1;} ll cm (ll n, ll M, ll p) {ll ans1 = 1, ans2 = 1; while (m) {ans1 = ans1 * n % P; ans2 = ans2 * m % P; n --; m --;} return ans1 * inv (ans2, p) % P ;} ll Lucas (ll n, ll M, ll p) {If (M = 0) return 1; return Lucas (N/P, M/P, P) * cm (N % P, M % P, P) % P;} int main () {ll n, m, p; while (scanf ("% LLD", & N, & M, & P )! = EOF) {printf ("% LLD \ n", Lucas (n-2 * m + 1 + M, m, p);} return 0 ;}
Zoj 3557 how many sets II Lucas Theorem