zoukankan      html  css  js  c++  java
  • Lucas定理学习笔记 & 洛谷 P3807 【模板】卢卡斯定理/Lucas 定理

    传送门


    Lucas定理

    OI-Wiki讲的太好了:http://oi-wiki.com/math/number-theory/lucas/#lucas

    做一下总结补充解释:

    1. ((a+b)^pequiv a^p+b^p pmod p)
    2. (f^p(x)equiv f(x^p) pmod p) 其中 (f(x)) 表示关于 (x) 的一个多项式。
    3. [egin{aligned} (a+b)^n& equiv (a+b)^{p imes left lfloor frac{n}{p} ight floor} imes(a+b)^{n mod p}pmod p\ & equiv (a^p+b^p)^{left lfloor frac{n}{p} ight floor} imes(a+b)^{n mod p}pmod p\ end{aligned} ]

    (C_n^m) 时,相当于求 ((a+b)^n) 的第 (m) 项((a) 的次数为 (m) 的项)的系数。
    也就是化简之后的式子 ((a^p+b^p)^{left lfloor frac{n}{p} ight floor}) 的第 (left lfloor frac{m}{p} ight floor) 项和 ((a+b)^{n mod p}) 的第 (m) 项的系数的乘积。

    这样不断递归下去求解即可。

    最终式子为:

    [C_n^m=C_{(n/p)}^{(m/p)} imes C_{(nmod p)}^{(m mod p)}pmod p ]

    还有exLucas定理,省选以上内容,希望有朝一日有必要学到(强烈暗示)

    AC代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iomanip>
    #include<vector>
    #include<ctime>
    #include<set>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    template<class T>inline void read(T &x)
    {
        x=0;register char c=getchar();register bool f=0;
        while(!isdigit(c))f^=c=='-',c=getchar();
        while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
        if(f)x=-x;
    }
    template<class T>inline void print(T x)
    {
        if(x<0)putchar('-'),x=-x;
        if(x>9)print(x/10);
        putchar('0'+x%10);
    }
    const int maxn=2e5+5;
    long long T,n,m,p,d[maxn];
    long long ksm(long long a,long long b){
    	if(b==1) return a%p;
    	if(b==0) return 1;
    	long long res=ksm(a,b/2);
    	if(b&1) return res*res%p*a%p;
    	return res*res%p;
    }
    inline long long inv(long long a){
    	return ksm(a,p-2);
    }
    inline long long c(long long n,long long m){
        if(m>n) return 0;
    	return d[n]*inv(d[m])%p*inv(d[n-m])%p;
    }
    long long dfs(long long n,long long m){
    	if(m==0) return 1;
    	if(m==1) return n%p;
    	return dfs(n/p,m/p)*c(n%p,m%p)%p;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>T;
    	while(T--){
    		cin>>n>>m>>p;
    		d[0]=1;
    		for(int i=1;i<p;i++) d[i]=(d[i-1]*i)%p;
    		cout<<dfs(n+m,n)<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    isequal 和startswith 使用
    UVa10340
    UVa1368
    UVa455
    UVa1225
    UVa1586
    UVa 1585
    UVa10082
    UVa272
    NYOJ1
  • 原文地址:https://www.cnblogs.com/yinyuqin/p/15362496.html
Copyright © 2011-2022 走看看