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

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

 #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. hdu4618 Palindrome Sub-Array dp+记忆化搜索 或者直接暴力

    题意就是找一个 左右上下对称的正方形矩阵. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 没想到记忆+dp和暴力就能水过... //记忆话搜索+d ...

  2. (转)ios获取设备系统信息

    UIDevice *device_=[[UIDevice alloc] init]; NSLog(@"设备所有者的名称--%@",device_.name); NSLog(@&qu ...

  3. (转)iOS动画Core Animation

    文章转载:http://blog.sina.com.cn/s/blog_7b9d64af0101b8nh.html 在iOS中动画实现技术主要是:Core Animation. Core Animat ...

  4. App开放接口api安全性—Token签名sign的设计与实现

    前言 在app开放接口api的设计中,避免不了的就是安全性问题,因为大多数接口涉及到用户的个人信息以及一些敏感的数据,所以对这些接口需要进行身份的认证,那么这就需要用户提供一些信息,比如用户名密码等, ...

  5. Servlet生命周期以及获取参数

    1. 创建Servlet几种方式  1) 实现Servlet接口     控制Servlet的生命周期       构造器       init()       service()       des ...

  6. 【英文】Bingo口语笔记(18) - Cover系列

    cover charge 服务费 cover version 翻唱版本 cover the news 头条新闻

  7. 使用 Linux 终端 SSH 登录 VPS

    Windows 中远程 SSH 登录 VPS 进行管理的利器是 PuTTY,但是 Linux 中就没必要用它了.Linux.Unix(包括 Mac iOS)都必然有内置的命令行终端,内建了 OpenS ...

  8. mkdir -p命令

    如果要创建目录A并创建目录A的子目录B,没有用-p的情况下是mkdir 2次如果用-p 可以直接创建2个目录 mkdir -p 目录A/子目录B就可以

  9. Arduino 软重启 软件reset

    将12脚连接一个1K电阻,然后用电阻另一端连接RESET脚.注意不是12脚直接连接RESET!! 代码如下(要注意RESET脚为LOW时自动重启) #define PIN 12 void setup( ...

  10. ubuntu eclipse 安装svn

    1.helper->install new software 在弹出的窗口中work with 输入http://subclipse.tigris.org/update_1.6.x 2.下面窗口 ...