直接套用模板,以后接着用

这里还有一个素因子分解的模板

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
//#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf ((LL)1<<40)
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T ABS ( T a) { return a >= ? a : -a; }
template<class T> T CMP_MIN ( T a, T b ) { return a < b; }
template<class T> T CMP_MAX ( T a, T b ) { return a > b; }
template<class T> T MAX ( T a, T b ) { return a > b ? a : b; }
template<class T> T MIN ( T a, T b ) { return a < b ? a : b; }
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> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const double PI = 4.0 * atan(1.0); LL n, ans;
int t; #define Times 10 //产生[0, n-1]的一个随机数
LL random(LL n)
{
return ((double)rand() / RAND_MAX * n + 0.5);
}
//乘法,采用加法模拟,避免中间结果超出LL
LL multi(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b & )
{
b--;
ans=(ans+a) % mod;
}
else
{
b/=;
a=(*a) % mod;
}
}
return ans;
} //快速幂,同样避免超出LL的做法
LL Pow(LL a, LL b, LL mod)
{
LL ans=;
while(b)
{
if(b&)
{
b--;
ans=multi(ans,a,mod);
}
else
{
b/=;
a=multi(a,a,mod);
}
}
return ans;
} //miller_rabin的一遍探测,返回false表示是合数
bool witness(LL a,LL n)
{
LL d=n-;
while( !(d&) )
d >>= ;
LL t=Pow(a, d, n);
while(d!=n- && t!= && t!=n-)
{
t=multi(t,t,n);
d<<=;
}
return t==n- || d&;
} //miller_rabin算法,返回false表示是合数,否则是素数
//返回素数出错的概率(最高)为 1 / (4 ^ times)
bool miller_rabin(LL n)
{
if(n == )
return true;
if( n< || !(n&) )
return false;
for(int i = ; i <= Times ; i++ )
{
LL a = random(n-) + ;
if( !witness(a, n) )
return false;
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************
LL factor[];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始 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 Pollard_rho(LL x, LL c)
{
LL i=,k=;
LL x0=rand()%x;
LL y=x0;
while()
{
i++;
x0=(multi(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;
ans = MIN(ans, n);
return;
}
LL p=n;
while(p>=n)
p=Pollard_rho(p, rand()%(n-)+);
findfac(p);
findfac(n/p);
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d", &t))while(t--)
{
ans = inf;
scanf("%I64d", &n);
findfac(n);
if(miller_rabin(n))
printf("Prime\n");
else printf("%I64d\n", ans);
}
return ;
}

POJ 1811Prime Test(米勒拉宾素数测试)的更多相关文章

  1. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  2. Miller_Rabin(米勒拉宾)素数测试

    2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...

  3. Miller_Rabin(米勒拉宾)素数测试算法

    首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...

  4. Miller_Rabin (米勒-拉宾) 素性测试

    之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...

  5. GCDLCM 【米勒_拉宾素数检验 (判断大素数)】

    GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...

  6. 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)

    若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...

  7. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  8. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

  9. HDU2138 & 米勒拉宾模板

    题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...

随机推荐

  1. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  2. postgresql大批量数据导入方法

    一直没有好好关注这个功能,昨天看了一下,数据库插入有瓶颈,今天研究了一下: 主要有以下方案: 1.使用copy从文件导入: copy table_001(a, b, "f", d, ...

  3. 一些纯css3写的公司logo

      随着对css3了解得越深入,越来越发现了css3的强大.css3不但能完成一些基本的特效如圆角阴影等,还能借助动画技术实现一些复杂的动画,能替代很多以前js才能完成的工作,css3的作用还不止于此 ...

  4. HDU 5335 Walk Out (BFS,技巧)

    题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...

  5. hihoCoder #1176 : 欧拉路·一 (简单)

    题意:给出n个岛,每个岛都有桥到达其他岛,且桥数可多可少(即使两岛有多桥),判断是否是欧拉路(即每条桥只能走一次,所有桥都能遍历1遍). 思路: 满足如下条件之一者即为欧拉路: 1.连通图,每个岛的度 ...

  6. ecshop 分类树全部显示

    1.在page_header.lbi文件中加入 $GLOBALS['smarty']->assign('categories',get_categories_tree()); // 保证首页页面 ...

  7. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? W ...

  8. 在centOS中加入本地ISO yum源

    注:本文转载自<liujun_live的博客>,感谢原博主的辛勤写作:原文地址:http://blog.sina.com.cn/s/blog_8ea8e9d50101em6f.html 在 ...

  9. windows 自动关机命令

    Windows 的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中.如果想让Windows 2000也实现同样的效果,可以把Shutdown.exe复制到系统 ...

  10. DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现

    在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中,笔者阐述了一个防御状态机,它可用来抵御来自应用层的DDoS攻击,但是该状态机依赖一个能应对大量条目快速增删的ip黑白 ...