【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test
2024-09-25 16:38:02
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.
Sample Input
2
5
10
Sample Output
Prime
2
Source
【分析】
模板题
/*
宋代谢逸
《踏莎行·柳絮风轻》
柳絮风轻,梨花雨细。春阴院落帘垂地。碧溪影里小桥横,青帘市上孤烟起。
镜约关情,琴心破睡。轻寒漠漠侵鸳被。酒醒霞散脸边红,梦回山蹙眉间翠。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n, Ans; //快速乘
long long multi(long long a, long long b, long long c){
if (b == ) return ;
if (b == ) return a % c;
long long tmp = multi(a, b / , c);
if (b % == ) return (tmp + tmp) % c;
else return (((tmp + tmp) % c) + a) % c;
}
ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (multi(tmp, tmp, p));
else return multi(multi(tmp, tmp, p), (a % p), p);
}
//二次探测
bool Sec_Check(ll a, ll p, ll c){
ll tmp = pow(a, p, c);
if (tmp != && tmp != (c - )) return ;//不通过
if (tmp == (c - ) || (p % != )) return ;
return Sec_Check(a, p / , c);
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - , n)) return ;
}
return ;
}
//int f(int ) {return }
long long gcd(long long a, long long b){return b == ? a : gcd(b, a % b);}
long long BIGRAND() {return rand() * RAND_MAX + rand();}
long long pollard_rho(long long n, long long c){
long long x, y, d;
long long i = , k = ;
x = ((double)rand()/RAND_MAX*(n - )+0.5) + ;
y = x;
while(){
i++;
//注意顺序
x = (multi(x, x, n) % n + c) % n;
d = gcd(y - x + n, n);
if( < d && d < n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
}
//
void find(long long n, long long c){
if (n == ) return;
if (miller_rabin(n)) {
if (Ans == -) Ans = n;
else Ans = min(Ans, n);
return ;
}
long long p = n;
while (p >= n) p = pollard_rho(n, c--);
find(p, c);
find(n / p, c);
//return find(p, c) + find(n / p, c);
} int main(){
int T;
srand(time()); scanf("%d", &T);
while (T--){
scanf("%lld", &n);
if (n != && miller_rabin(n)) printf("Prime\n");
else {
Ans = -;
find(n, );
printf("%lld\n", Ans);
}
}
return ;
}
最新文章
- Lua 学习笔记(七)编译、执行外部代码块
- The Number Off of FFF
- maven -- 学习笔记(一)之maven环境搭建
- objective-c可变数组
- 简单使用SQLite 的增删改查
- 拖拽碰撞效果,高级浏览器下全部搞定(ie6-8还没有搞定)
- BLOB存储图片文件二进制数据是非对错
- php的迭代器
- TCP协议总结
- mysql用limit时offset越大时间越长
- Vnpy二次开发应用所需图标
- Nginx禁止IP访问,只允许域名访问
- Qt绘制文本二 弯曲排列和旋转效果 弧形路径 正弦函数路径
- html圈圈
- MySQL -- 全文检索(查询扩展检索)
- python入门-列表
- Unity 几何着色器
- 使用URLConnection发送http请求实现简单爬虫(可以配置代理)
- [ 转载 ] Java基础11--Java总结篇系列:Java泛型
- PHP 判断字符串 是否 包含另一个字符串
热门文章
- NIO的学习
- 向老项目JSP集成JSTL遇到的问题
- JavaScript高级程序设计49.pdf
- Response.Write,Page.RegisterClientScriptBlock和Page.RegisterStartupScript的区别
- 各种jee服务器的比较,tomcat, jboss, glassfish, websphere, weblogic
- 2 weekend110的zookeeper的原理、特性、数据模型、节点、角色、顺序号、读写机制、保证、API接口、ACL、选举、 + 应用场景:统一命名服务、配置管理、集群管理、共享锁、队列管理
- Yii框架常见问题汇总
- ibatis 开发中的经验 (一)ibatis 和hibernate 在开发中的理解
- Memcached笔记——(四)应对高并发攻击【转】
- [TypeScript] Stopping a TypeScript Build When Errors Are Found