中国剩余定理与扩展 Lucas定理与扩展 学习笔记

中国剩余定理

问题

求同余方程组

xc1(modm1)xc2(modm2)xc3(modm3)...xck(modmk) { x ≡ c 1 ( mod m 1 ) x ≡ c 2 ( mod m 2 ) x ≡ c 3 ( mod m 3 ) . . . x ≡ c k ( mod m k )
<script id="MathJax-Element-1" type="math/tex; mode=display"> \left\{ \begin{array}{c} x\equiv c_1\pmod {m_1}\\ x\equiv c_2\pmod {m_2} \\ x\equiv c_3\pmod {m_3}\\ ...\\ x\equiv c_k\pmod {m_k} \end{array} \right. </script>
其中满足 (mi,mj)=1,1<=ij<=k ( m i , m j ) = 1 , 1 <= i ≠ j <= k <script id="MathJax-Element-2" type="math/tex">(m_i,m_j)=1,1<=i\neq j<=k</script>
x的最小正(非负)整数解

结论

M=m1m2m3...mk M = m 1 ∗ m 2 ∗ m 3 ∗ . . . ∗ m k <script id="MathJax-Element-3" type="math/tex">M=m_1*m_2*m_3*...*m_k</script>
x=(i=1kciMmiinv(Mmi,mi))%M x = ( ∑ i = 1 k c i ∗ M m i ∗ i n v ( M m i , m i ) ) % M <script id="MathJax-Element-4" type="math/tex">x=(\sum\limits_{i=1}^k c_i*{M\over m_i}*inv({M\over m_i},mi))\%M</script>

证明

a.在模M意义下x只有唯一解 (有多解那还了得)
b.令 ni n i <script id="MathJax-Element-5" type="math/tex">n_i</script>满足 ni%Mmi=0 n i % M m i = 0 <script id="MathJax-Element-6" type="math/tex">n_i\% {M\over m_i}=0</script>且 ni%mi=1 n i % m i = 1 <script id="MathJax-Element-7" type="math/tex">n_i\% m_i=1</script>,则 N=i=1kcini N = ∑ i = 1 k c i ∗ n i <script id="MathJax-Element-8" type="math/tex">N=\sum\limits_{i=1}^k c_i*n_i</script>为原问题的一个解
c.根据上面的式子容易得出 ni=Mmix,ni=miy+1 n i = M m i x , n i = m i y + 1 <script id="MathJax-Element-9" type="math/tex">n_i={M\over m_i}x,n_i=m_iy+1</script>,则 Mmix=miy+1 M m i x = m i y + 1 <script id="MathJax-Element-10" type="math/tex">{M\over m_i}x=m_iy+1</script>,即 Mmixmiy=1 M m i x − m i y = 1 <script id="MathJax-Element-11" type="math/tex">{M\over m_i}x-m_iy=1</script>
d.由于 mi m i <script id="MathJax-Element-12" type="math/tex">m_i</script>两两互质,所以 Mmi M m i <script id="MathJax-Element-13" type="math/tex">{M\over m_i}</script>与 mi m i <script id="MathJax-Element-14" type="math/tex">m_i</script>也互质,令 a=Mmi,b=mi a = M m i , b = m i <script id="MathJax-Element-15" type="math/tex">a={M\over m_i},b=m_i</script>,则 axby=1 a x − b y = 1 <script id="MathJax-Element-16" type="math/tex">ax-by=1</script>
e.可以发现我们已经将其化简成扩展欧几里得的基本形式 ax+by=(a,b) a x + b y = ( a , b ) <script id="MathJax-Element-17" type="math/tex">ax+by=(a,b)</script>,其中也可以认为x为a在模b意义下的逆元

代码

codevs 3990

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long

LL k,l,r,n,M,x,y,Min,ans,m[15],c[15];

void exgcd(LL a,LL b,LL &x,LL &y)
{
    if (!b) x=1,y=0;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
int main()
{
    scanf("%d%lld%lld",&k,&l,&r);
    M=1LL;
    for (int i=1;i<=k;++i)
    {
        scanf("%lld%lld",&m[i],&c[i]);
        M*=m[i];
    }
    for (int i=1;i<=k;++i)
    {
        LL a=M/m[i],b=m[i];
        exgcd(a,b,x,y);
        x=(x%b+b)%b;
        if (!x) x+=b;
        n+=c[i]*a*x;
    }
    n%=M;
    if (!n) n+=M;

    if (r>=n)
        ans=(r-n)/M+1;
    if (l>=n) ans=ans-((l-n)/M+1);
    if ((l-n)%M==0) ++ans;
    if (ans)
    {
        if (l<=n) Min=n;
        else Min=n+((l-n)/M+1)*M;
    }
    printf("%lld\n%lld\n",ans,Min);
}

扩展中国剩余定理

问题

求同余方程组

xc1(modm1)xc2(modm2)xc3(modm3)...xck(modmk) { x ≡ c 1 ( mod m 1 ) x ≡ c 2 ( mod m 2 ) x ≡ c 3 ( mod m 3 ) . . . x ≡ c k ( mod m k )
<script id="MathJax-Element-18" type="math/tex; mode=display"> \left\{ \begin{array}{c} x\equiv c_1\pmod {m_1}\\ x\equiv c_2\pmod {m_2} \\ x\equiv c_3\pmod {m_3}\\ ...\\ x\equiv c_k\pmod {m_k} \end{array} \right. </script>
x的最小正(非负)整数解

结论

对于两个方程
xc1(modm1) x ≡ c 1 ( mod m 1 ) <script id="MathJax-Element-129" type="math/tex">x\equiv c_1\pmod {m_1}</script>
xc2(modm2) x ≡ c 2 ( mod m 2 ) <script id="MathJax-Element-130" type="math/tex">x\equiv c_2\pmod {m_2}</script>
将其合并成一个方程,有解条件为 (m1,m2)|(c2c1) ( m 1 , m 2 ) | ( c 2 − c 1 ) <script id="MathJax-Element-131" type="math/tex">(m_1,m_2)|(c_2-c_1)</script>
m=m1m2(m1,m2) m = m 1 m 2 ( m 1 , m 2 ) <script id="MathJax-Element-132" type="math/tex">m={m_1m_2\over (m_1,m_2)}</script>
c=(inv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2))%m2(m1,m2)m1+c1 c = ( i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) ) % m 2 ( m 1 , m 2 ) ∗ m 1 + c 1 <script id="MathJax-Element-133" type="math/tex">c=(inv({m_1\over (m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)})\%{m_2\over (m_1,m_2)}*m_1+c_1</script>
最终得出一个式子 xc(modm) x ≡ c ( mod m ) <script id="MathJax-Element-134" type="math/tex">x\equiv c\pmod {m}</script>
x=c%m x = c % m <script id="MathJax-Element-135" type="math/tex">x=c\%m</script>即为原问题的一个解

证明

a.将两个方程写成 x=m1k1+c1,x=m2k2+c2 x = m 1 k 1 + c 1 , x = m 2 k 2 + c 2 <script id="MathJax-Element-26" type="math/tex">x=m_1k_1+c_1,x=m_2k_2+c_2</script>,两式联立得 m1k1+c1=m2k2+c2 m 1 k 1 + c 1 = m 2 k 2 + c 2 <script id="MathJax-Element-27" type="math/tex">m_1k_1+c_1=m_2k_2+c_2</script>,移项 m1k1=c2c1+m2k2 m 1 k 1 = c 2 − c 1 + m 2 k 2 <script id="MathJax-Element-28" type="math/tex">m_1k_1=c_2-c_1+m_2k_2</script>,
b.根据贝祖定理,以上等式有解充要条件为 (m1,m2)|(c2c1) ( m 1 , m 2 ) | ( c 2 − c 1 ) <script id="MathJax-Element-29" type="math/tex">(m_1,m_2)|(c_2-c_1)</script>
c.将等式两边同除 (m1,m2) ( m 1 , m 2 ) <script id="MathJax-Element-30" type="math/tex">(m_1,m_2)</script>得 m1(m1,m2)k1=(c2c1)(m1,m2)+m2(m1,m2)k2 m 1 ( m 1 , m 2 ) ∗ k 1 = ( c 2 − c 1 ) ( m 1 , m 2 ) + m 2 ( m 1 , m 2 ) ∗ k 2 <script id="MathJax-Element-31" type="math/tex">{m_1\over(m_1,m_2)}*k_1={(c_2-c_1)\over (m_1,m_2)}+{m_2\over (m_1,m_2)}*k_2</script>,即 m1(m1,m2)k1(c2c1)(m1,m2)(modm2(m1,m2)) m 1 ( m 1 , m 2 ) k 1 ≡ ( c 2 − c 1 ) ( m 1 , m 2 ) ( mod m 2 ( m 1 , m 2 ) ) <script id="MathJax-Element-32" type="math/tex">{m_1\over(m_1,m_2)}k_1\equiv {(c_2-c_1)\over (m_1,m_2)}\pmod {{m_2\over(m_1,m_2)}}</script>,进一步化简 k1inv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2)(modm2(m1,m2)) k 1 ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) ( mod m 2 ( m 1 , m 2 ) ) <script id="MathJax-Element-33" type="math/tex">k_1\equiv inv({m_1\over(m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)}\pmod {{m_2\over(m_1,m_2)}}</script>, k1=inv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2)+ym2(m1,m2) k 1 = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) + y m 2 ( m 1 , m 2 ) <script id="MathJax-Element-34" type="math/tex">k_1=inv({m_1\over(m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)}+y{{m_2\over (m_1,m_2)}}</script>
d.将其回代 x=m1k1+c1 x = m 1 k 1 + c 1 <script id="MathJax-Element-35" type="math/tex">x=m_1k_1+c_1</script>,得 x=inv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2)m1+ym1m2(m1,m2)+c1 x = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) ∗ m 1 + y m 1 m 2 ( m 1 , m 2 ) + c 1 <script id="MathJax-Element-36" type="math/tex">x=inv({m_1\over(m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)}*m_1+y{{m_1m_2\over (m_1,m_2)}}+c_1</script>,即 xinv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2)m1+c1(modm1m2(m1,m2)) x ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) ∗ m 1 + c 1 ( mod m 1 m 2 ( m 1 , m 2 ) ) <script id="MathJax-Element-37" type="math/tex">x\equiv inv({m_1\over(m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)}*m_1+c_1\pmod {{m_1m_2\over (m_1,m_2)}}</script>
e.至此我们又将其化简成了 xc(modm) x ≡ c ( mod m ) <script id="MathJax-Element-38" type="math/tex">x\equiv c\pmod m</script>的形式,其中 m=m1m2(m1,m2) m = m 1 m 2 ( m 1 , m 2 ) <script id="MathJax-Element-39" type="math/tex">m={m_1m_2\over (m_1,m_2)}</script>, c=(inv(m1(m1,m2),m2(m1,m2))(c2c1)(m1,m2))%m2(m1,m2)m1+c1 c = ( i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ∗ ( c 2 − c 1 ) ( m 1 , m 2 ) ) % m 2 ( m 1 , m 2 ) ∗ m 1 + c 1 <script id="MathJax-Element-40" type="math/tex">c=(inv({m_1\over (m_1,m_2)},{m_2\over (m_1,m_2)})*{(c_2-c_1)\over (m_1,m_2)})\%{m_2\over (m_1,m_2)}*m_1+c_1</script>

代码

pku 2891

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
#define N 1005

int k;
LL c[N],m[N],c1,c2,m1,m2,t;
bool flag;

LL gcd(LL a,LL b)
{
    if (!b) return a;
    else return gcd(b,a%b);
}
void exgcd(LL a,LL b,LL &x,LL &y)
{
    if (!b) x=1LL,y=0LL;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
LL inv(LL a,LL b)
{
    LL x=0LL,y=0LL;
    exgcd(a,b,x,y);
    x=(x%b+b)%b;
    if (!x) x+=b;
    return x;
}
int main()
{
    while (~scanf("%d",&k))
    {
        flag=true;
        for (int i=1;i<=k;++i)
            scanf("%I64d%I64d",&m[i],&c[i]);
        for (int i=2;i<=k;++i)
        {
            m1=m[i-1],m2=m[i],c1=c[i-1],c2=c[i];
            t=gcd(m1,m2);
            if ((c2-c1)%t!=0) {flag=false;break;}
            m[i]=m1*m2/t;
            c[i]=inv(m1/t,m2/t)*((c2-c1)/t)%(m2/t)*m1+c1;
            c[i]=(c[i]%m[i]+m[i])%m[i];
        }
        if (!flag) puts("-1");
        else printf("%I64d\n",c[k]);
    }
}

Lucas定理

问题

Cmn%p C n m % p <script id="MathJax-Element-41" type="math/tex">C_n^m\%p</script>,其中p为质数

结论


n=nkpk+nk1pk1+...+n2p2+n1p+n0 n = n k ∗ p k + n k − 1 ∗ p k − 1 + . . . + n 2 ∗ p 2 + n 1 ∗ p + n 0 <script id="MathJax-Element-42" type="math/tex">n=n_k*p^k+n_{k-1}*p^{k-1}+...+n_2*p^2+n_1*p+n_0</script>
m=mkpk+mk1pk1+...+m2p2+m1p+m0 m = m k ∗ p k + m k − 1 ∗ p k − 1 + . . . + m 2 ∗ p 2 + m 1 ∗ p + m 0 <script id="MathJax-Element-43" type="math/tex">m=m_k*p^k+m_{k-1}*p^{k-1}+...+m_2*p^2+m_1*p+m_0</script>
Cmn=i=0kCmini C n m = ∏ i = 0 k C n i m i <script id="MathJax-Element-44" type="math/tex">C_n^m=\prod\limits_{i=0}^k C_{n_i}^{m_i}</script>

证明

去问卢卡斯
听说要用二项式定理什么的
我怎么可能会这种东西
记住就行了

代码

zoj 3557

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long

LL n,m,Mod;

LL fast_pow(LL a,LL p)
{
    LL ans=1LL;
    for (;p;p>>=1,a=a*a%Mod)
        if (p&1)
            ans=ans*a%Mod;
    return ans;
}
LL inv(LL x)
{
    return fast_pow(x,Mod-2);
}
LL C(LL n,LL m)
{
    if (m>n) return 0LL;
    LL up=1LL,down=1LL;
    for (LL i=n-m+1;i<=n;++i) up=up*i%Mod;
    for (LL i=1;i<=m;++i) down=down*i%Mod;
    return up*inv(down)%Mod;
}
LL lucas(LL n,LL m)
{
    if (m>n) return 0LL;
    LL ans=1;
    for (;m;n/=Mod,m/=Mod)
        ans=ans*C(n%Mod,m%Mod)%Mod;
    return ans;
}
int main()
{
    while (~scanf("%lld%lld%lld",&n,&m,&Mod))
        printf("%lld\n",lucas(n-m+1,m));
}

扩展Lucas定理

问题

Cmn%p C n m % p <script id="MathJax-Element-45" type="math/tex">C_n^m\%p</script>

结论


p=p1k1p2k2...pqkq p = p 1 k 1 ∗ p 2 k 2 ∗ . . . ∗ p q k q <script id="MathJax-Element-46" type="math/tex">p={p_1}^{k_1}*{p_2}^{k_2}*...*{p_q}^{k_q}</script>
列出同余方程组

ansc1(modp1k1)ansc2(modp2k2)...anscq(modpqkq) { a n s ≡ c 1 ( mod p 1 k 1 ) a n s ≡ c 2 ( mod p 2 k 2 ) . . . a n s ≡ c q ( mod p q k q )
<script id="MathJax-Element-47" type="math/tex; mode=display"> \left\{ \begin{array}{c} ans\equiv c_1\pmod {{p_1}^{k_1}}\\ ans\equiv c_2\pmod {{p_2}^{k_2}}\\ ...\\ ans\equiv c_q\pmod {{p_q}^{k_q}}\\ \end{array} \right. </script>
其中 c1...cq c 1 . . . c q <script id="MathJax-Element-48" type="math/tex">c_1...c_q</script>是对于每一个 Cmn%piki C n m % p i k i <script id="MathJax-Element-49" type="math/tex">C_n^m\%{p_i}^{k_i}</script>求出的答案
然后根据中国剩余定理合并
可见 piki p i k i <script id="MathJax-Element-50" type="math/tex">{p_i}^{k_i}</script>并不是质数,而是一个质数的幂的形式
对于如何求 Cmn%piki C n m % p i k i <script id="MathJax-Element-51" type="math/tex">C_n^m\% {p_i}^{k_i}</script>将在证明中给出

证明

a.由于同于方程组在模 p=p1k1p2k2...pqkq p = p 1 k 1 ∗ p 2 k 2 ∗ . . . ∗ p q k q <script id="MathJax-Element-52" type="math/tex">p={p_1}^{k_1}*{p_2}^{k_2}*...*{p_q}^{k_q}</script>意义下有唯一解,可以证明上面做法的正确性
b.由于 p=p1k1p2k2...pqkq p = p 1 k 1 ∗ p 2 k 2 ∗ . . . ∗ p q k q <script id="MathJax-Element-53" type="math/tex">p={p_1}^{k_1}*{p_2}^{k_2}*...*{p_q}^{k_q}</script>实质上是将p进行质因数分解,所以 piki p i k i <script id="MathJax-Element-54" type="math/tex">{p_i}^{k_i}</script>满足两两互质,可以直接用中国剩余定理合并
c.对于如何求 Cmn%piki C n m % p i k i <script id="MathJax-Element-55" type="math/tex">C_n^m\%{p_i}^{k_i}</script>
根据 Cmn=n!m!(nm)! C n m = n ! m ! ( n − m ) ! <script id="MathJax-Element-56" type="math/tex">C_n^m={n!\over m!(n-m)!}</script>,只要分别求出 n!%piki,m!%piki,(nm)!%piki n ! % p i k i , m ! % p i k i , ( n − m ) ! % p i k i <script id="MathJax-Element-57" type="math/tex">n!\% {p_i}^{k_i},m!\% {p_i}^{k_i},(n-m)!\% {p_i}^{k_i}</script>就可以通过逆元求出 Cmn%piki C n m % p i k i <script id="MathJax-Element-58" type="math/tex">C_n^m\%{p_i}^{k_i}</script>
d.对于如何求 n!%piki n ! % p i k i <script id="MathJax-Element-59" type="math/tex">n!\%{p_i}^{k_i}</script>
我们以 n=19,pi=3,ki=2 n = 19 , p i = 3 , k i = 2 <script id="MathJax-Element-60" type="math/tex">n=19,p_i=3,k_i=2</script>为例
n!=12345678910111213141516171819 n ! = 1 ∗ 2 ∗ 3 ∗ 4 ∗ 5 ∗ 6 ∗ 7 ∗ 8 ∗ 9 ∗ 10 ∗ 11 ∗ 12 ∗ 13 ∗ 14 ∗ 15 ∗ 16 ∗ 17 ∗ 18 ∗ 19 <script id="MathJax-Element-61" type="math/tex">n!=1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19</script>
=(12457810111314161719)36(123456) = ( 1 ∗ 2 ∗ 4 ∗ 5 ∗ 7 ∗ 8 ∗ 10 ∗ 11 ∗ 13 ∗ 14 ∗ 16 ∗ 17 ∗ 19 ) ∗ 3 6 ∗ ( 1 ∗ 2 ∗ 3 ∗ 4 ∗ 5 ∗ 6 ) <script id="MathJax-Element-62" type="math/tex">=(1*2*4*5*7*8*10*11*13*14*16*17*19)*3^6*(1*2*3*4*5*6)</script>
根据这个例子发现,求解n!可以分为3部分:第一部分是 pi p i <script id="MathJax-Element-63" type="math/tex">p_i</script>的幂的部分,也就是 36 3 6 <script id="MathJax-Element-64" type="math/tex">3^6</script>即 pinpi p i ⌊ n p i ⌋ <script id="MathJax-Element-65" type="math/tex">{p_i}^{\lfloor{n\over p_i}\rfloor}</script>,可以直接求解;第二部分是一个新的阶乘,也就是6!即 npi! ⌊ n p i ⌋ ! <script id="MathJax-Element-66" type="math/tex">{\lfloor{n\over p_i}\rfloor}!</script>,可以递归下去求解;第三部分是除前两部分之外剩下的数
考虑第三部分如何求解
发现第三部分在模 piki p i k i <script id="MathJax-Element-67" type="math/tex">{p_i}^{k_i}</script>意义下是以 piki p i k i <script id="MathJax-Element-68" type="math/tex">{p_i}^{k_i}</script>为周期的,即 (124578)(101113141617)(modpiki) ( 1 ∗ 2 ∗ 4 ∗ 5 ∗ 7 ∗ 8 ) ≡ ( 10 ∗ 11 ∗ 13 ∗ 14 ∗ 16 ∗ 17 ) ( mod p i k i ) <script id="MathJax-Element-69" type="math/tex">(1*2*4*5*7*8)\equiv(10*11*13*14*16*17)\pmod {{p_i}^{k_i}}</script>,所以只求 piki p i k i <script id="MathJax-Element-70" type="math/tex">{p_i}^{k_i}</script>长度的即可;但是还剩下一个孤立的19,可以发现剩下孤立的数长度不会超过 piki p i k i <script id="MathJax-Element-71" type="math/tex">{p_i}^{k_i}</script>,只需要暴力求解即可
e.最后一个问题是对于求出的 m!%piki m ! % p i k i <script id="MathJax-Element-72" type="math/tex">m!\%{p_i}^{k_i}</script>和 (nm)!%piki ( n − m ) ! % p i k i <script id="MathJax-Element-73" type="math/tex">(n-m)!\%{p_i}^{k_i}</script>有可能与 piki p i k i <script id="MathJax-Element-74" type="math/tex">{p_i}^{k_i}</script>不互质,无法求逆元
所以要将 m!%piki m ! % p i k i <script id="MathJax-Element-75" type="math/tex">m!\%{p_i}^{k_i}</script>和 (nm)!%piki ( n − m ) ! % p i k i <script id="MathJax-Element-76" type="math/tex">(n-m)!\%{p_i}^{k_i}</script>中质因子 pi p i <script id="MathJax-Element-77" type="math/tex">p_i</script>先全部除去,求出逆元后再全部乘回去
计算n!中质因子p的个数x的公式为 x=np+np2+np3+... x = ⌊ n p ⌋ + ⌊ n p 2 ⌋ + ⌊ n p 3 ⌋ + . . . <script id="MathJax-Element-78" type="math/tex">x=\lfloor{n\over p}\rfloor+\lfloor{n\over p^2}\rfloor+\lfloor{n\over p^3}\rfloor+...</script>
递推式也可以写为 f(n)=f(np)+np f ( n ) = f ( ⌊ n p ⌋ ) + ⌊ n p ⌋ <script id="MathJax-Element-79" type="math/tex">f(n)=f(\lfloor{n\over p}\rfloor)+\lfloor{n\over p}\rfloor</script>

代码

codeforces2015ICL,Finals,Div.1#J

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long

LL n,m,MOD,ans;

LL fast_pow(LL a,LL p,LL Mod)
{
    LL ans=1LL;
    for (;p;p>>=1,a=a*a%Mod)
        if (p&1)
            ans=ans*a%Mod;
    return ans;
}
void exgcd(LL a,LL b,LL &x,LL &y)
{
    if (!b) x=1LL,y=0LL;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
LL inv(LL A,LL Mod)
{
    if (!A) return 0LL;
    LL a=A,b=Mod,x=0LL,y=0LL;
    exgcd(a,b,x,y);
    x=((x%b)+b)%b;
    if (!x) x+=b;
    return x;
}
LL Mul(LL n,LL pi,LL pk)
{
    if (!n) return 1LL;
    LL ans=1LL;
    if (n/pk)
    {
        for (LL i=2;i<=pk;++i)
            if (i%pi) ans=ans*i%pk;
        ans=fast_pow(ans,n/pk,pk);
    }
    for (LL i=2;i<=n%pk;++i)
        if (i%pi) ans=ans*i%pk;
    return ans*Mul(n/pi,pi,pk)%pk;
}
LL C(LL n,LL m,LL Mod,LL pi,LL pk)
{
    if (m>n) return 0LL;
    LL a=Mul(n,pi,pk),b=Mul(m,pi,pk),c=Mul(n-m,pi,pk);
    LL k=0LL,ans;
    for (LL i=n;i;i/=pi) k+=i/pi;
    for (LL i=m;i;i/=pi) k-=i/pi;
    for (LL i=n-m;i;i/=pi) k-=i/pi;
    ans=a*inv(b,pk)%pk*inv(c,pk)%pk*fast_pow(pi,k,pk)%pk;
    return ans*(Mod/pk)%Mod*inv(Mod/pk,pk)%Mod;
}
int main()
{
    scanf("%I64d%I64d%I64d",&n,&m,&MOD);
    for (LL x=MOD,i=2;i<=MOD;++i)
        if (x%i==0)
        {
            LL pk=1LL;
            while (x%i==0) pk*=i,x/=i;
            ans=(ans+C(n,m,MOD,i,pk))%MOD;
        }
    printf("%I64d\n",ans);
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页