Description

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

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 254).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.
 
题目大意:给一个数n,问是不是素数,若是输出Prime,若不是输出其最小的非1因子。
思路:http://www.2cto.com/kf/201310/249381.html
模板盗自:http://vfleaking.blog.163.com/blog/static/1748076342013231104455989/
 
代码(375MS):
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iterator>
#include <vector>
using namespace std; typedef long long LL; LL modplus(LL a, LL b, LL mod) {
LL res = a + b;
return res < mod ? res : res - mod;
} LL modminus(LL a, LL b, LL mod) {
LL res = a - b;
return res >= ? res : res + mod;
} LL mult(LL x, LL p, LL mod) {
LL res = ;
while(p) {
if(p & ) res = modplus(res, x, mod);
x = modplus(x, x, mod);
p >>= ;
}
return res;
} LL power(LL x, LL p, LL mod) {
LL res = ;
while(p) {
if(p & ) res = mult(res, x, mod);
x = mult(x, x, mod);
p >>= ;
}
return res;
} bool witness(LL n, LL p) {
int t = __builtin_ctz(n - );
LL x = power(p % n, (n - ) >> t, n), last;
while(t--) {
last = x, x = mult(x, x, n);
if(x == && last != && last != n - ) return false;
}
return x == ;
} const int prime_n = ;
int prime[prime_n] = {, , , , }; bool isPrime(LL n) {
if(n == ) return false;
if(find(prime, prime + prime_n, n) != prime + prime_n) return true;
if(n % == ) return false;
for(int i = ; i < prime_n; i++)
if(!witness(n, prime[i])) return false;
return true;
} LL getDivisor(LL n) {
int c = ;
while (true) {
int i = , k = ;
LL x1 = , x2 = ;
while(true) {
x1 = modplus(mult(x1, x1, n), c, n);
LL d = __gcd(modminus(x1, x2, n), n);
if(d != && d != n) return d;
if(x1 == x2) break;
i++;
if(i == k) x2 = x1, k <<= ;
}
c++;
}
} void getFactor(LL n, vector<LL> &ans) {
if(isPrime(n)) return ans.push_back(n);
LL d = getDivisor(n);
getFactor(d, ans);
getFactor(n / d, ans);
} int main() {
int T; LL n;
scanf("%d", &T);
while(scanf("%I64d", &n) != EOF) {
if(isPrime(n)) puts("Prime");
else {
vector<LL> ans;
getFactor(n, ans);
printf("%I64d\n", *min_element(ans.begin(), ans.end()));
}
}
}

POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)的更多相关文章

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

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

  2. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  3. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  4. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  5. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  6. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  7. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  8. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  9. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

  10. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

随机推荐

  1. Xcode Shortcuts

    Description:⌘: Command     ⌥: Option     ⌃: Control    ←↑↓→: Left, Up, Down, Right                  ...

  2. IIS8中部署WCF服务出错:HTTP 错误 404.3 - Not Found

    解决方法,以管理员身份进入命令行模式,运行: "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ ...

  3. 常用jQuery代码02

    一.each函数拿到每个元素的宽度 setTimeout(function () { $(".sticker_list img").each(function () { var W ...

  4. ArcGIS API for Silverlight实现地图测距功能

    原文:ArcGIS API for Silverlight实现地图测距功能 问题:如何实现地图测距功能? 地图工具栏 <Grid x:Name="gToolMenu" Hei ...

  5. Magento SSH 下载安装

    http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/installing_magento_via_shell_ ...

  6. Prestashop 页面空白

    Advanced Parameters > Performance页面空白,无任何提示错误,解决方法: 更改文件/cache/class_index.php 权限为666

  7. C# Socket编程 同步以及异步通信

    套接字简介:套接字最早是Unix的,window是借鉴过来的.TCP/IP协议族提供三种套接字:流式.数据报式.原始套接字.其中原始套接字允许对底层协议直接访问,一般用于检验新协议或者新设备问题,很少 ...

  8. day01-基础内容

    day01-基础内容 1.Linux:  1)开源的操作系统.免费的    主要用于服务器端,而Java主要是服务器端开发  2)Linux与Windows目录结构的区别:    2.1)文件系统不同 ...

  9. 常用jq选择器和遍历的使用

    1.jq的选择器,常用有哪些? class id > ~ ul li a 2.遍历的使用(在使用用遍历节点时,我们的注意遍历在不传递参数(也就是传参),代表的是传递局部全局,也就是"* ...

  10. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...