LG4720 [template] to expand Lucas Theorem

Extended Lucas Theorem

Seeking \ (C_n ^ m \ BMOD {P} \) , where \ (C \) is the number of combinations.

\ (1≤m≤n≤10 18 is {^}, 2≤p≤1000000 \) , do not guarantee \ (P \) is a prime number .

Fading solution to a problem of

Set
\ [p = p_1 ^ {\ alpha_1} p_2 ^ {\ alpha_2} \ cdots p_k ^ {\ alpha_k} \]

Obtained

\[ \left\{\begin{align*} C_n^m & \mod & {p_1^{\alpha_1}} \\ C_n^m & \mod & {p_2^{\alpha_2}}\\ \vdots\\ C_n^m & \mod & {p_k^{\alpha_k}} \end{align*} \right. \]

Finally, the Chinese remainder theorem to merge.

Suppose we now require

\[ C_n^m\bmod{P^k}\\ =\frac {n!}{m!(n-m)!}\bmod{P^k} \]

We were unable to obtain \ (m!, (Nm) ! \) The inverse, the reason is very simple, not necessarily solvable.

How to do it? Found \ (A \) of \ (P \) Sufficient Conditions for inverse \ (GCD (A, P) =. 1 \) . So we change the formula:

\[ \frac {\frac {n!}{P^{x}}}{\frac {m!}{P^{y}}\frac {(n-m)!}{P^{z}}}P^{x-y-z}\bmod{P^k} \]

Wherein \ (X \) is the \ (n-! \) In \ (P \) number of factors (including the number \ (P \) factor). The other is empathy.

So if we for a \ (the n-\) , can be obtained \ (\ the n-FRAC {!} {P} the X-^ \) , it does not get away with it? (So that we can in order to reverse yuan)

Problem is equivalent to finding \ (\ {n-FRAC! P ^ {X}} \ BMOD P ^ {K} \) . We \ (n \!) Deformation:

\[ n!=1\cdot 2\cdot 3\cdots n=(P\cdot 2P\cdot 3P\cdots)(1\cdot 2\cdots) \]

Left \ (\ 1 \ sim n) all of the \ (\ leq n \) a \ (P \) multiple of the right is the other remaining.

Since \ (\ 1 \ sim n) have \ (\ lfloor \ frac nP \ rfloor \) a \ (P \) multiple,

\[ \therefore\ P^{\lfloor \frac nP\rfloor}(1 \cdot 2 \cdot 3\cdots)(1\cdot 2\cdots) =P^{\lfloor \frac nP\rfloor}(\lfloor \frac nP\rfloor)!\prod_{i=1,i\not\equiv 0\pmod {P}}^ni \]

Obviously latter \ (\ bmod \ P \) is circulating section.

\[ =P^{\lfloor \frac nP\rfloor}(\lfloor \frac nP\rfloor)!(\prod_{i=1,i\not\equiv 0\pmod {P}}^{P^k}i)^{\lfloor \frac n{P^k}\rfloor}(\prod_{i=P^k\lfloor \frac n{P^k}\rfloor,i\not\equiv 0\pmod {P}}^ni) \]

Wherein \ ((\ prod_ {i = 1, i \ not \ equiv 0 \ pmod {P}} ^ {P ^ k} i) ^ {\ lfloor \ frac n {P ^ k} \ rfloor} \) is a cyclic section \ (1 \ sim P \) all none the \ (P \) the product of a factor, \ ((\ prod_ {I = P ^ K \ lfloor \ FRAC n-{P ^ K} \ rfloor, I \ not \ equiv 0 \ pmod {P}} ^ ni) \) is the remainder of the product.

such as

\[ 22!\equiv(1\cdot 2\cdot 4\cdot 5\cdot 7\cdot 8)(10\cdot 11\cdot 13\cdot 14\cdot 16\cdot 17)(19\cdot 20\cdot 22)(3\cdot 6\cdot 9\cdot 12\cdot 15\cdot 18\cdot 21)\pmod {3^2}\\ \equiv(1\cdot 2\cdot 4\cdot 5\cdot 7\cdot 8)^2(19\cdot 20\cdot 22)3^7(1\cdot 2\cdot 3\cdot 4\cdot 5\cdot 6\cdot 7)\pmod {3^2}\\ \equiv3^77!(1\cdot 2\cdot 4\cdot 5\cdot 7\cdot 8)^2(19\cdot 20\cdot 22)\pmod {3^2} \]

Exactly the same, $ \ lfloor \ frac {22} {3 ^ 2} \ rfloor = 2 $. Understand it ...

Found in front of the \ (P ^ {\ lfloor \ frac nP \ rfloor} \) to get rid of, but \ ((\ lfloor \ frac nP \ rfloor)! \) There may also contain \ (P \) .

So, we defined functions:

\[ f(n)=\frac {n!}{P^x}\\ =f(\lfloor \frac nP\rfloor)(\prod_{i=1,i\not\equiv 0\pmod {P}}^{P^k}i)^{\lfloor \frac n{P^k}\rfloor}(\prod_{i=P^k\lfloor \frac n{P^k}\rfloor,i\not\equiv 0\pmod {P}}^ni)) \]

That's all. Time complexity is \ (O (\ n-LOG_ {P}) \) . Boundary \ (F (0) =. 1 \) .

Take a look at the original formula

\[ \frac {\frac {n!}{P^{x}}}{\frac {m!}{P^{y}}\frac {(n-m)!}{P^{z}}}P^{x-y-z}\bmod{P^k}\\ =\frac {f(n)}{f(m)f(n-m)}P^{x-y-z}\bmod{P^k} \]

Because of \ (f (m) \) necessarily \ (P ^ k \) coprime, so we can directly \ (exgcd \) inversing it.

Finally, we have a problem, \ (P ^ {xyz} \) ye seek? Actually very good demand.

For instance, we require \ (f (n) = \ frac {n!} {P ^ x} \) of \ (X \) .

Set \ (G (n-) = X \) (the above-described \ (X \) ), look factorial formula

\[ n!=P^{\lfloor \frac nP\rfloor}(\lfloor \frac nP\rfloor)!(\prod_{i=1,i\not\equiv 0\pmod {P}}^{P^k}i)^{\lfloor \frac n{P^k}\rfloor}(\prod_{i=P^k\lfloor \frac n{P^k}\rfloor,i\not\equiv 0\pmod {P}}^ni) \]

Obviously we want is \ (P ^ {\ lfloor \ FRAC nP \ rfloor} \) , and \ ((\ lfloor \ frac nP \ rfloor)! \) And possibly \ (P \) factor, so we can get the following recursive formula

\[g(n)=\lfloor \frac nP\rfloor+g(\lfloor \frac nP\rfloor)\]

Time complexity is \ (O (\ n-LOG_ {P}) \) . Boundary \ (g (n) = 0 (n <P) \)

So the answer is,

\[ \frac {\frac {n!}{P^{x}}}{\frac {m!}{P^{y}}\frac {(n-m)!}{P^{z}}}P^{g(x)-g(y)-g(z)}\bmod{P^k} \]

Finally, Chinese Remainder Theorem merger answer can be obtained by \ (C_n ^ m \) . The total time complexity \ (O (P \ log P) \) .

#include<bits/stdc++.h>
#define co const
#define il inline
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T> il T read(T&x){
    return x=read<T>();
}
using namespace std;
#define int long long

#define gcd __gcd
int pow(int a,int b,int mod){
    a=(a%mod+mod)%mod;
    int ans=1;
    for(;b;b>>=1,a=a*a%mod)
        if(b&1) ans=ans*a%mod;
    return ans;
}
void exgcd(int a,int b,int&x,int&y){
    if(!b) x=1,y=0;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
int inv(int a,int mod){
    if(!a) return 0;
    if(gcd(a,mod)!=1) return -1;
    int x,y;
    exgcd(a,mod,x,y);
    return (x%mod+mod)%mod;
}
int fac(int n,int pi,int pk){ //n! % pi^ki pk=pi^ki
    if(n<=1) return 1;
    int ans=1;
    for(int i=2;i<pk;++i)
        if(i%pi) ans=ans*i%pk;
    ans=pow(ans,n/pk,pk);
    for(int i=2;i<=n%pk;++i)
        if(i%pi) ans=ans*i%pk;
    return ans*fac(n/pi,pi,pk)%pk;
}
int binom(int n,int m,int pi,int pk){
    int up=fac(n,pi,pk),dl=fac(m,pi,pk),dr=fac(n-m,pi,pk);
    int ex=0;
    for(int i=n;i;i/=pi) ex+=i/pi;
    for(int i=m;i;i/=pi) ex-=i/pi;
    for(int i=n-m;i;i/=pi) ex-=i/pi;
    return up*inv(dl,pk)%pk*inv(dr,pk)%pk*pow(pi,ex,pk)%pk;
}
signed main(){
    int n=read<int>(),m=read<int>(),P=read<int>();
    int ans=0,p=P;
    for(int i=2;i<=p;++i)if(p%i==0){
        int pi=i,pk=1;
        while(p%i==0) p/=i,pk*=i;
        ans=(ans+binom(n,m,pi,pk)*(P/pk)%P*inv(P/pk,pk)%P)%P;
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/extended_lucas_theorem.html