http://poj.org/problem?id=1811

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime>
using namespace std;
typedef __int64 LL;
const int times = ;
LL minf, n; LL random(LL n){
return (double)rand() / RAND_MAX * n + 0.5;
} LL multi(LL a, LL b, LL mod){
a %= mod, b %= mod;
LL ans = ;
while(b){
if(b & ) ans += a, ans %= mod;
b >>= ;
a <<= ;
a %= mod;
}
return ans;
} LL power(LL a, LL p, LL mod){
a %= mod;
LL ans = ;
while(p){
if(p & ) ans = multi(ans, a, mod);
p >>= ;
a = multi(a, a, mod);
}
return ans;
} LL gcd(LL a, LL b){
if(!b) return a;
return gcd(b, a % b);
} bool witness(LL a, LL n){
LL u = n - ;
while(!(u & )) u >>= ;
LL t = power(a, u, n);
while(u != n - && t != && t != n - ){
t = multi(t, t, n);
u <<= ;
}
return t == n - || u & ;
} bool miller_rabin(LL n){
if(n == ) return ;
if(n < || !(n & )) return ;
//test for odd numbers larger than 2
for(int i = ; i < times; i++){
LL p = random(n - ) + ;
if(!witness(p, n)) return ;
}
return ;
} LL pollard_rho(LL n, LL t){
LL x = random(n - ) + ;
LL y = x;
LL i = , k = , d;
while(){
++i;
x = (multi(x, x, n) + t) % n;
d = gcd(y - x, n);
if( < d && d < n) return d;
if(x == y) return n;
if(i == k){
y = x;
k <<= ;
}
}
} void fact(LL n, LL t){
if(n == ) return;
if(miller_rabin(n)){
minf = min(minf, n);
return;
}
LL p = n;
while(p >= n) p = pollard_rho(p, t--);
fact(p, t);
fact(n / p, t);
} void solve(){
//if n is prime
if(miller_rabin(n)){
puts("Prime");
return;
}
//try to factorize n
//initialize the minimum non trival factor of n
minf = n;
fact(n, );
printf("%I64d\n", minf);
} int main(){
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--) scanf("%I64d", &n), solve();
return ;
}

poj1811 Prime Test的更多相关文章

  1. POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)

    http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接 ...

  2. [poj1811]Prime Test(Pollard-Rho大整数分解)

    问题描述:素性测试兼质因子分解 解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用 期望复杂度:$O({n^{\frac{1}{4}}})$ #include<cst ...

  3. 【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

  4. 【POJ1811】Prime Test

    [题目大意] 若n是素数,输出“Prime”,否则输出n的最小素因子,(n<=2^54) [题解] 和bzoj3667差不多,知识这道题没那么坑. 直接上Pollord_Rho和Rabin_Mi ...

  5. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  6. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  9. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

随机推荐

  1. c# 基本知识 ref 和 out

    一:首先两者都是引用传递,都是按地址传递的. 二: 区别 (1)ref 默认必须初始化,out 不需要显式初始化.例如: ; int num2; refMethod(ref num1); outMet ...

  2. ubuntu安装jdk遇到的问题:cannot execute binary file

    安装完jdk,配置好环境变量出现如下状况: cannot execute binary file 问题原因: jdk的位数与ubuntu的系统位数不一致 jdk 64位 ubuntu 32位 然后通过 ...

  3. 【皇甫】☀那些事儿......STEP

    写项目之前呢,先来缕缕思路,既然要写学生管理系统,那肯定上不了从数据库中读取信息,然而想要从数据库中拿到你想要的东西,就要先登录,得到他的权限才行,所以我们第一步就要先搞出一个登录页面并且能连接到数据 ...

  4. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

  5. 插入多行数据和类似 select union 方法

    Cite:http://blog.csdn.net/downmoon/article/details/5936706 [ruby] view plaincopyprint? Create table ...

  6. PHP XDEBUG

    PHP调试时,不得不提XDEBUG这个调试利器.学习PHP以来,几乎所有的问题我都利用它来解决. 首先关于如何安装,不在赘述,请自行google之.(需要特别注意的是:PHP5.2 和5.3 ,关于加 ...

  7. 某个系统配置文件 用户层的SQL

    SELECT fpo.user_profile_option_name, fpv.level_id, fpv.level_value, fpv.profile_option_value, fu.use ...

  8. FRM-92101解决办法

    /u02/UAT/inst/apps/UAT_newerp3/logs/ora/10.1.3/opmn/forms_default_group_1/ http://blog.csdn.net/orth ...

  9. 夺命雷公狗---DEDECMS----3快速入门之隐藏

    如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...

  10. VNC & LSF

    VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件, 由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UN ...