题目链接: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的更多相关文章

  1. 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 ...

  2. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  3. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  4. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...

  5. 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 ...

  6. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  7. bzo4802 欧拉函数 miller_rabin pollard_rho

    欧拉函数 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 1112  Solved: 418[Submit][Status][Discuss] Descr ...

  8. pollard_rho 学习总结 Miller_Rabbin 复习总结

    吐槽一下名字,泼辣的肉..OwO 我们知道分解出一个整数的所有质因子是O(sqrt(n)/ln(n))的 但是当n=10^18的时候就显得非常无力的 这个算法可以在大概O(n^(1/4))的时间复杂度 ...

  9. POJ 2429

    思路:a/n*b/n=lcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况.套Miller_Rabin和pollard_rho模板 //#pragma comment(linker, &q ...

随机推荐

  1. Android Studio 初探

    前言 上周由于写了一篇关于"Eclipse+ADT+Android SDK 搭建安卓开发环境" 的博文,其他博主们表示相当的不悦,都什么年代了还用Eclipse+ADT开发安卓应用 ...

  2. Hibernate HQL查询的参数绑定

    参数绑定: Hibernate中对动态查询参数绑定提供了丰富的支持,那么什么是查询参数动态绑定呢?其实如果我们熟悉传统JDBC编程的话,我们就不难理解查询参数动态绑定,如下代码传统JDBC的参数绑定: ...

  3. TCP protocol

    he characteristics of TCP protocol TCP (which means Transmission Control Protocol) is one of the mai ...

  4. 完美配置Tomcat的HTTPS

    Tomcat配置HTTPS的文章到处都有,过程也比较简单,随后文中会转一段过来. 但对于启用APR情况下报异常“java.lang.Exception: Connector attribute SSL ...

  5. Space and GridLayout 教程

    Ice Cream Sandwich (ICS) sports two new widgets that have been designed to support the richer user i ...

  6. float label 提示

    很多时候,我们写input 都会添加 placeholder 属性,用于提示用户这里该输入什么,怎么输入,但是当用户一旦输入了字符串,该提示就会消失,相信会有人,输入内容后可能会忘记这里要输入的是什么 ...

  7. Struts2配置

    1.      设定server a)      window– preferences – myeclipse – servers – tomcat – 6.x b)      选择tomcat h ...

  8. hdu 4604 Deque(最长不下降子序列)

    从后向前对已搜点做两遍LIS(最长不下降子序列),分别求出已搜点的最长递增.递减子序列长度.这样一直搜到第一个点,就得到了整个序列的最长递增.递减子序列的长度,即最长递减子序列在前,最长递增子序列在后 ...

  9. 【C#学习笔记】获得本机IP

    using System; using System.Net; namespace ConsoleApplication { class Program { static void Main(stri ...

  10. 使用源代码安装lnmp

    一.安装nginx前,安装pcre. # tar zxvf pcre-8.12.tar.gz# ./configure# make# make install 二.安装nginx # tar zxvf ...