设为首页 加入收藏

TOP

poj 2429 Pollard_rho大数分解
2015-07-20 17:57:24 来源: 作者: 【 】 浏览:2
Tags:poj 2429 Pollard_rho 大数分解

先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小。

这里可以直接搜索,注意一个问题,由于相同因子不能分配给两边(会改变gcd)所以可以将相同因子合并,这样的话,搜索的层数也变的很少了。

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          using namespace std; typedef long long LL; #define maxn 10000 LL factor[maxn]; int tot; const int S=10; //测试次数 LL muti_mod(LL a,LL b,LL c) { 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) { if (n==1) return x%mod; int bit[90],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){ //是素数返回0,合数返回1 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
         
          =n) p=Pollard_rho(p,rand() % (n-1) +1); findfac(p); findfac(n/p); } LL mins,aa,bb; int top; void dfs(LL a,LL b,int p) { if(a+b>=mins) return; if(p==top) { if(a+b
          
           >1; c=b/a; tot=0; findfac(c); sort(factor,factor+tot); top=0; for(int i=0;i
           
            bb) swap(aa,bb); printf("%lld %lld\n",aa,bb); } return 0; } 
           
          
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU1028Ignatius and the Princes.. 下一篇poj 3258(二分)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: