Title Address: Fzu 2020
Test instructions: The value of C (n,m)%p (1 <= m <= n <= 10^9, M <= 10^4, M < p < 10^9, p is prime).
Ideas:
For and and P is prime, we generally use the Lucas theorem to solve.
1). Lucas theorem is used to find the value of C (n,m) mod p, p is the prime number. It is described as:
If
So get
That
Lucas (N,M,P) =c (n%p,m%p) * Lucas (N/P,M/P,P)
Lucas (n,0,p) = 1;
2). For the large combined number of modulo C (n,m)%p=n! /(m! * (n-m)!)% MoD
= (n-m+i)! /m!* (I>=1&&I<=M). Then divide the division into multiplication according to the multiplication inverse.
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef__int64 LL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;using namespace STD; LL N,m,mod; ll Modxp (ll A,ll b) {ll res=1; while(b>0) {if(b&1) Res=res*a%mod; B=b>>1; A=a*a%mod; }returnRes;} ll C (ll N, ll m) {if(m>n)return 0; LL ans=1; for(intI=1; i<=m; i++) {LL a= (n+i-m)%mod; LL B=i%mod; Ans=ans* (A*MODXP (b, mod-2)%mod)%mod; }returnAns;} ll Lucas (ll n,ll m) {if(m==0)return 1;returnC (N%mod,m%mod) *lucas (N/mod,m/mod)%mod;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%lld%lld%lld", &n,&m,&mod);printf("%lld\n", Lucas (n,m)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fzu 2020-Combination (Lucas theorem + inverse solution to solve large combinatorial number modulo)