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;
    }
    
    
  • 相关阅读:
    H公司以及我的目标
    新的起点
    apache服务器配置Net的实践
    会计简要学习
    二、MongoDB的简单增删改查
    一、MongoDB安装与启动
    KnockOutJs初次体验
    DevExpress 全体窗口换肤的功能 winform
    DevExporess 右键菜单的实现
    使用gridControl gridview总结
  • 原文地址:https://www.cnblogs.com/JohnRan/p/13394565.html
Copyright © 2011-2022 走看看