• HDU3037 Saving Beans


    题目链接:HDU3037

    正解:$Lucas$定理

    解题报告:

      Lucas定理裸题:

      求$C_{n+m}^m$ $mod$ $p$,$p<=10^5$.

      边界注意一下。

    //It is made by ljh2000
    //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <ctime>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <string>
    #include <complex>
    #include <bitset>
    using namespace std;
    typedef long long LL;
    const int MAXN = 100011;
    int n,m,p,lim;
    LL jie[MAXN],nj[MAXN];
    inline LL C(LL n,LL m){ if(n<m) return 0;/*!!!*/ return jie[n]*nj[m]%p*nj[n-m]%p; }
    inline LL fast_pow(LL x,LL y){ LL r=1; while(y>0) { if(y&1) r*=x,r%=p; x*=x; x%=p; y>>=1; } return r; }
    inline int getint(){
        int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
        if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    }
    
    inline void init(){
    	lim=p-1; jie[0]=nj[0]=1;
    	for(int i=1;i<=lim;i++) jie[i]=jie[i-1]*i,jie[i]%=p;
    	nj[lim]=fast_pow(jie[lim],p-2);
    	for(int i=lim-1;i>=1;i--) nj[i]=nj[i+1]*(i+1)%p;
    }
    
    inline LL Lucas(LL n,LL m){
    	if(n<m) return 0; if(m==0) return 1;
    	if(n<=lim && m<=lim) return C(n,m);
    	return Lucas(n/p,m/p)*C(n%p,m%p)%p;
    }
    
    inline void work(){
    	int T=getint();
    	while(T--) {
    		n=getint(); m=getint(); p=getint();
    		init();	printf("%lld
    ",Lucas(n+m,m));
    	}
    }
    
    int main()
    {
        work();
        return 0;
    }
    //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
    

      

  • 相关阅读:
    MYSQL 优化指南
    设计模式——依赖倒置原则实例(PHP实现)
    PHP开发笔记
    反射应用
    HMAC-SHA1算法签名及Authorization头认证
    PHP接口和抽象类的区别
    PHP 模板方法模式使用
    RSA JS 加密解密DEMO
    RSA加密解密(PHP Demo)
    【Spark调优】提交job资源参数调优
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6556305.html
Copyright © 2020-2023  润新知