HDU-3864 D_num Miller_Rabin和Pollard_rho
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864
题意:给定一个数n,求n的因子只有四个的情况。
Miller_Rabin和Pollard_rho模板题,复杂度O(n^(1/4)),注意m^3=n的情况。
- //STATUS:C++_AC_62MS_232KB
- #include <functional>
- #include <algorithm>
- #include <iostream>
- //#include <ext/rope>
- #include <fstream>
- #include <sstream>
- #include <iomanip>
- #include <numeric>
- #include <cstring>
- #include <cassert>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <bitset>
- #include <queue>
- #include <stack>
- #include <cmath>
- #include <ctime>
- #include <list>
- #include <set>
- #include <map>
- using namespace std;
- //#pragma comment(linker,"/STACK:102400000,102400000")
- //using namespace __gnu_cxx;
- //define
- #define pii pair<int,int>
- #define mem(a,b) memset(a,b,sizeof(a))
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define PI acos(-1.0)
- //typedef
- typedef long long LL;
- typedef unsigned long long ULL;
- //const
- const int N=;
- const int INF=0x3f3f3f3f;
- const int MOD=,STA=;
- const LL LNF=1LL<<;
- const double EPS=1e-;
- const double OO=1e15;
- const int dx[]={-,,,};
- const int dy[]={,,,-};
- const int day[]={,,,,,,,,,,,,};
- //Daily Use ...
- inline int sign(double x){return (x>EPS)-(x<-EPS);}
- template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
- template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
- template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
- template<class T> inline T Min(T a,T b){return a<b?a:b;}
- template<class T> inline T Max(T a,T b){return a>b?a:b;}
- template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
- template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
- template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
- template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
- //End
- LL factor[]; //质因数分解结果(刚返回时是无序的)
- int tol; //质因数的个数。数组小标从0开始
- const int S=;
- LL gcd(LL a,LL b)
- {
- if(a==)return ;
- if(a<) return gcd(-a,b);
- while(b)
- {
- LL t=a%b;
- a=b;
- b=t;
- }
- return a;
- }
- LL mult_mod(LL a,LL b,LL c)
- {
- a%=c;
- b%=c;
- LL ret=;
- while(b)
- {
- if(b&){ret+=a;ret%=c;}
- a<<=;
- if(a>=c)a%=c;
- b>>=;
- }
- return ret;
- }
- //计算 x^n %c
- LL pow_mod(LL x,LL n,LL mod)//x^n%c
- {
- if(n==)return x%mod;
- x%=mod;
- LL tmp=x;
- LL ret=;
- while(n)
- {
- if(n&) ret=mult_mod(ret,tmp,mod);
- tmp=mult_mod(tmp,tmp,mod);
- n>>=;
- }
- return ret;
- }
- //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
- //一定是合数返回true,不一定返回false
- bool check(LL a,LL n,LL x,LL t)
- {
- LL ret=pow_mod(a,x,n);
- LL last=ret;
- for(int i=;i<=t;i++)
- {
- ret=mult_mod(ret,ret,n);
- if(ret==&&last!=&&last!=n-) return true;//合数
- last=ret;
- }
- if(ret!=) return true;
- return false;
- }
- // Miller_Rabin()算法素数判定
- //是素数返回true.(可能是伪素数,但概率极小)
- //合数返回false;
- bool Miller_Rabin(LL n)
- {
- if(n<)return false;
- if(n==)return true;
- if((n&)==) return false;//偶数
- LL x=n-;
- LL t=;
- while((x&)==){x>>=;t++;}
- for(int i=;i<S;i++)
- {
- LL a=rand()%(n-)+;//rand()需要stdlib.h头文件
- if(check(a,n,x,t))
- return false;//合数
- }
- return true;
- }
- LL Pollard_rho(LL x,LL c)
- {
- LL i=,k=;
- LL x0=rand()%x;
- LL y=x0;
- while()
- {
- i++;
- x0=(mult_mod(x0,x0,x)+c)%x;
- LL d=gcd(y-x0,x);
- if(d!=&&d!=x) return d;
- if(y==x0) return x;
- if(i==k){y=x0;k+=k;}
- }
- }
- //对n进行素因子分解
- void findfac(LL n)
- {
- if(Miller_Rabin(n))//素数
- {
- factor[tol++]=n;
- return;
- }
- LL p=n;
- while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
- findfac(p);
- findfac(n/p);
- }
- LL n;
- int main(){
- // freopen("in.txt","r",stdin);
- srand(time(NULL));
- int i,j;
- LL a,b;
- while(~scanf("%I64d",&n))
- {
- if(n==){
- printf("is not a D_num\n");
- continue;
- }
- tol=;
- findfac(n);
- if(tol!= && tol!=){
- printf("is not a D_num\n");
- continue;
- }
- sort(factor,factor+tol);
- if(tol== && factor[]!=factor[]){
- printf("%I64d %I64d %I64d\n",factor[],factor[],n);
- }
- else if(tol== && factor[]==factor[] && factor[]==factor[]){
- printf("%I64d %I64d %I64d\n",factor[],factor[]*factor[],n);
- }
- else printf("is not a D_num\n");
- }
- return ;
- }
HDU-3864 D_num Miller_Rabin和Pollard_rho的更多相关文章
- hdu 3864 D_num Pollard_rho算法和Miller_Rabin算法
D_num Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- hdu 3864 D_num
思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...
- hdu 3864 素数分解
题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...
- Miller_Rabin、 Pollard_rho Template
Multiply and pow Function: //计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的 // a,b,c <2^63 ll mult_modq(ll a,ll ...
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- bzo4802 欧拉函数 miller_rabin pollard_rho
欧拉函数 Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 1112 Solved: 418[Submit][Status][Discuss] Descr ...
- pollard_rho 学习总结 Miller_Rabbin 复习总结
吐槽一下名字,泼辣的肉..OwO 我们知道分解出一个整数的所有质因子是O(sqrt(n)/ln(n))的 但是当n=10^18的时候就显得非常无力的 这个算法可以在大概O(n^(1/4))的时间复杂度 ...
- POJ 2429
思路:a/n*b/n=lcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况.套Miller_Rabin和pollard_rho模板 //#pragma comment(linker, &q ...
随机推荐
- 通过AOP 实现异常统一管理
package com.zhang.shine.cache; import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJo ...
- 转TerreyLee AJAX入门系列2——ScriptManager的理解总结
ScriptManager的功能之一就是处理页面上局部更新,对于这点,我想大家都知道.但是他工作的原理到底是什么呢,这个暂且不从正面来回答. 我们这样想一下,目前能够真正实现局部刷新的就是js+xml ...
- ios 使用GCD 多线程 教程
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
- axis : java.lang.NoSuchMethodError
Hi friends,Iam getting the following error when deploying my app in jboss error.Iam new to axis .can ...
- iOS开发:应用生命周期
iOS应用通过委托对象AppDelegate类在应用周期的不同阶段会回调不同的方法,应用周期分为以下五种状态: Not Running(非运行状态).应用没有运行或被系统终止. Inactive ...
- 为初学者写ORM,ORM的原理及测试案例
提纲 一.什么是ORM.二.反射以及Attribute在ORM中的应用.三.创建一个数据库表和表对应的实体model.四.实体model如何映射出数据库表.五.组合ORM映射生成insert语句.六. ...
- Asp.net正则获取html内容
1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...
- sql server删除数据后空间无变化处理方法
删除数据库表 第一步: 执行 delete from doc.115sou.com #删除数据,执行效率低 drop table doc.115sou.com #删除表 ...
- 统计nginx日志里流量
用awk可以,比如,我想统计nginx日志里,今天下午3点0分,这一分钟内,访问的流量(文件的大小) grep "07/Nov/2013:15:00:" *.log|awk '{ ...
- svn强制提交备注信息
当我们用tortoisesvn,提交代码时,有很多人不喜欢写注释的,代码版本多了,根本搞不清,哪个版本改了什么东西?所以如果加一些注释的话,我们看起来,也方便很多.所以在提交的时候,我会强制要求,写注 ...