poj1811 Prime Test
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的更多相关文章
- POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)
http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接 ...
- [poj1811]Prime Test(Pollard-Rho大整数分解)
问题描述:素性测试兼质因子分解 解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用 期望复杂度:$O({n^{\frac{1}{4}}})$ #include<cst ...
- 【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 ...
- 【POJ1811】Prime Test
[题目大意] 若n是素数,输出“Prime”,否则输出n的最小素因子,(n<=2^54) [题解] 和bzoj3667差不多,知识这道题没那么坑. 直接上Pollord_Rho和Rabin_Mi ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Prime Generator
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
随机推荐
- Servlet加载器的实验
今天,看了张孝祥老师的类加载器的一个高级实验分析的教程,有点受益匪浅. 新建servlet工程,在Servlet类中 package com.sinosoft.servelt; import java ...
- display:flex 多栏多列布局
转自:http://www.360doc.com/content/14/0811/01/2633_400926000.shtml display:flex 多栏多列布局浏览器支持情况:火狐直接支持w3 ...
- 暴力枚举-数长方形(hdu5258)
数长方形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 我与 美国作家 21天精通C++ 作者 Rao的对话:
这就是动力呀!
- [转] java书籍(给Java程序猿们推荐一些值得一看的好书 + 7本免费的Java电子书和教程 )
7本免费的Java电子书和教程 1. Thinking in Java (Third Edition) 本书的作者是Bruce Eckel,它一直都是Java最畅销的免费电子书.这本书可以帮助你系统的 ...
- UML: 状态机图
摘自http://www.umlonline.org/school/viewthread.php?tid=39 活动图将流程分解为一个一个的活动,通过活动的先后顺序来展示流程:而状态机图从某个物品的状 ...
- length() 和 size()
length() 和 size() ==>length() 是针对数组字符串说的 size() 是针对 泛型集合的
- demo04calc
package com.example.demo05simplecalc; import android.os.Bundle; import android.app.Activity; import ...
- Android 播放视频文件
package com.example.myvideo2; import java.io.File; import android.app.Activity; import android.net.U ...
- [php]表单和验证
<?php /* 表单的作用: 通过表单 发布和收集 信息. 对html表单进行编码 只是有效接受用户输入的必要操作的(一部分), 必须由[服务器端]组件来处理 一 标头函数(header()) ...