zoukankan      html  css  js  c++  java
  • HDU 6608

    HDU 6608

    题意:给以你一个质数,求小于它的最大质数的阶乘。

    分析:Miller-Rabin快速判断素性,找到这个最大素数。此外,加上威尔逊定理推式子就好了。威尔逊定理描述的内容是对于一个正素数p:

    ((k-1)!modk)(= k-1)

    有了这个定理,我们就可以很容易得到小于k最大素数阶乘结果了:

    (q!=frac{1}{(k-2)(k-3)...(q+1)} mod k)

    #include<cstdio>
    
    using namespace std;
    
    typedef long long LL;
    
    LL mul(LL x, LL y, LL mod){
        LL res=0;
        while(y){
            if(y&1)
                res=(res+x)%mod;
            x=(x+x)%mod;
            y>>=1;
        }
        return res;
    }
    
    LL powmod(LL x, LL y, LL mod){
        LL res=1;
        while(y){
            if(y&1)
                res=mul(res,x,mod);
            x=mul(x,x,mod);
            y>>=1;
        }
        return res;
    }
    
    int ft[12]={2,3,5,7,11,13,17,19,23,29,31,37};
    
    bool check(LL a, LL n){
        LL x=n-1;
    
        int t=0;
        while((x&1)==0){
            x>>=1;
            ++t;
        }
    
        x=powmod(a,x,n);
        LL y;
        for(int i=1;i<=t;++i){
            y=mul(x,x,n);
            if(y==1&&x!=1&&x!=n-1)return true;
            x=y;
        }
    
        if(y!=1)return true;
        return false;
    }
    
    bool Miller_Rabin(LL x){
        if(x==2)return true;
        if(!(x&1)||x==1)return false;
    
        for(int i=0;i<12;++i){
            if(x<=ft[i])break;
            if(check(ft[i],x)) return false;
        }
    
        return true;
    }
    
    int main(){
        int T;scanf("%d", &T);
        while(T--){
            LL n;scanf("%lld",&n);
            LL q=n-1;
            while(!Miller_Rabin(q))--q;
            LL ans=1;
            for(LL i=q+1;i<n-1;++i)ans=mul(ans,powmod(i,n-2,n),n);
            printf("%lld
    ",ans);
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    从yield关键字看IEnumerable和Collection的区别
    弹出框Fancybox使用详解
    几个不错的JQuery UI框架
    解决 IE6 position:fixed 固定定位问题
    autocomplete 应用(搜索提示框)
    HTML5 基础
    Cookie原理
    escape()、encodeURI()、encodeURIComponent()区别详解
    顶回Top
    移植mavlink协议到STM32详细教程
  • 原文地址:https://www.cnblogs.com/JohnRan/p/13394565.html
Copyright © 2011-2022 走看看