zoukankan      html  css  js  c++  java
  • E.Multiply Pollard_rho质因数分解

    2019 icpc xuzhou
    思路很简单, 但是这个Pollard_rho的模板要选好, 不然不是wa 就是 tle ,我太难了

    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    const int S=20;
    using namespace std;
    
    typedef long long ll;
    #define maxn 1000000
    
    ll factor[maxn];
    int tot;
    
    ll muti_mod(ll a,ll b,ll c){    //返回(a*b) mod c,a,b,c<2^63
        a%=c;
        b%=c;
        ll ret=0;
        while (b){
            if (b&1){
                ret+=a;
                if (ret>=c) ret-=c;
            }
            a<<=1;
            if (a>=c) a-=c;
            b>>=1;
        }
        return ret;
    }
    
    ll pow_mod(ll x,ll n,ll mod){  //返回x^n mod c ,非递归版
        if (n==1) return x%mod;
        int bit[64],k=0;
        while (n){
            bit[k++]=n&1;
            n>>=1;
        }
        ll ret=1;
        for (k=k-1;k>=0;k--){
            ret=muti_mod(ret,ret,mod);
            if (bit[k]==1) ret=muti_mod(ret,x,mod);
        }
        return ret;
    }
    
    bool check(ll a,ll n,ll x,ll t){   //以a为基,n-1=x*2^t,检验n是不是合数
        ll ret=pow_mod(a,x,n),last=ret;
        for (int i=1;i<=t;i++){
            ret=muti_mod(ret,ret,n);
            if (ret==1 && last!=1 && last!=n-1) return 1;
            last=ret;
        }
        if (ret!=1) return 1;
        return 0;
    }
    
    bool Miller_Rabin(ll n){
        ll x=n-1,t=0;
        while ((x&1)==0) x>>=1,t++;
        bool flag=1;
        if (t>=1 && (x&1)==1){
            for (int k=0;k<S;k++){
                ll a=rand()%(n-1)+1;
                if (check(a,n,x,t)) {flag=1;break;}
                flag=0;
            }
        }
        if (!flag || n==2) return 0;
        return 1;
    }
    
    ll gcd(ll a,ll b){
        if (a==0) return 1;
        if (a<0) return gcd(-a,b);
        while (b){
            ll t=a%b; a=b; b=t;
        }
        return a;
    }
    
    ll Pollard_rho(ll x,ll c){
        ll i=1,x0=rand()%x,y=x0,k=2;
        while (1){
            i++;
            x0=(muti_mod(x0,x0,x)+c)%x;
            ll d=gcd(y-x0,x);
            if (d!=1 && d!=x){
                return d;
            }
            if (y==x0) return x;
            if (i==k){
                y=x0;
                k+=k;
            }
        }
    }
    
    void findfac(ll n){           //递归进行质因数分解N
        if (!Miller_Rabin(n)){
            factor[tot++] = n;
            return;
        }
        ll p=n;
        while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);
        findfac(p);
        findfac(n/p);
    }
    int idx = 0 ;
    struct node
    {
    	ll val , num ;
    	node() {}
    	node(ll val , ll num) : val(val) , num(num) {} 
    	bool operator<(const node &a) const 
    	{
    		val < a.val ;
    	}
    }fac[maxn];
    ll b[maxn] ;
    bool solve(ll n)
    {
    	if (!Miller_Rabin(n)) 
    	 {
    	 	fac[++ idx] = {n , 1} ;
    	 }
    	else{
                tot = 0;
                findfac(n);
                sort(factor , factor + tot) ;
                idx = 0 ;
                for(int i = 0 ; i < tot ;i ++)
                 {
                 	if(factor[i] != fac[idx].val) 
                 	 fac[++ idx] = {factor[i] , 1};
                 	else 
                 	 fac[idx].num ++ ;
    			 }
            }
    }
    ll get(ll n , ll p)
    {
    	ll res = 0 ;
    	while(n)
    	{
    		res += n / p ;
    		n /= p ;
    	}
    	return res ;
    }
    int main(){
        srand(time(NULL));
        int t;
        scanf("%d",&t);
        while (t--){
            ll n , x , y ;
            scanf("%lld%lld%lld",&n , &x , &y);
            idx = 0 ;
    		solve(x) ;
    		for(int j = 1; j <= idx ;j ++) b[j] = 0 ;
            for(int i = 1; i <= n ;i ++)
             {
             	ll p ;
             	scanf("%lld" , &p) ;
             	for(int j = 1 ;j <= idx ;j ++)
             	 	b[j] += get(p , fac[j].val)  ;
    		 }
    		ll minx = 0 ;
    		for(int j = 1; j <= idx ;j ++)
    		  b[j] = get(y , fac[j].val) - b[j] , minx = max(minx , b[j]);
    		for(int j = 1; j <= idx ;j ++)
    		 	if(b[j] > 0)
    		 	 	minx = min(minx , b[j] / fac[j].num) ; 
    		printf("%lld
    " , minx) ;
        }
    }
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    求Computational problems associated with Racah algebra
    病理情形
    扫描算法求最大子序列的一次简单非严格证明
    分治法求最大子序列,关于复杂度的一次弱推导
    VFP_等待 rar 命令结束
    Windows_解决win7开机画面变成vista画面的方法
    C#_WinForm的等待窗口
    C#_控件的缩写大全
    SQL Server 2008_基本安装说明
    C#_获取 SQL服务器列表
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870862.html
Copyright © 2011-2022 走看看