【POJ】1811 Prime Test
http://poj.org/problem?id=1811
题意:求n最小素因子。(n<=2^54)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
const ll lim=1e9;
inline void C(ll &a, ll c) { if(a>=c || a<=-c) a%=c; }
inline ll mul(ll a, ll b, ll c) { if(a<=lim && b<=lim) return a*b%c; ll x=0; for(; b; b>>=1, C(a+=a, c)) if(b&1) C(x+=a, c); return x; }
inline ll mpow(ll a, ll b, ll c) { ll x=1; for(; b; b>>=1, a=mul(a, a, c)) if(b&1) x=mul(a, x, c); return x; }
inline ll rand(ll a, ll b) {
static const ll M=1e9+7, g=220703118;
static ll now=1998;
C(now*=g, M);
return a+(now*now)%(b-a+1);
}
ll gcd(ll a, ll b) { return b?gcd(b, a%b):a; }
inline ll iabs(ll a) { return a<0?-a:a; }
inline ll PR(ll n, ll c) {
ll x=rand(0, n-1), y=x, k=2, t;
for(int i=2; ; ++i) {
x=mul(x, x, n); x+=c; C(x, n);
t=gcd(iabs(y-x), n);
if(t!=1 && t!=n) return t;
if(y==x) return n;
if(i==k) y=x, k<<=1;
}
}
bool check(ll n) {
if(n==2 || n==3 || n==5 || n==7 || n==11 || n==13) return 1;
if(n<2 || (n&1)==0 || n%3==0 || n%5==0 || n%7==0 || n%11==0 || n%13==0) return 0;
ll d=n-1;
int cnt=0;
while((d&1)==0) d>>=1, ++cnt;
for(int i=0; i<20; ++i) {
ll a=mpow(rand(2, n-1), d, n);
for(int i=0; i<cnt; ++i) { ll t=a; a=mul(a, a, n); if(a==1 && t!=1 && t!=n-1) return 0; }
if(a!=1) return 0;
}
return 1;
}
ll f[100], ans;
int cnt;
void find(ll n) { //printf("%lld\n", n);
if(check(n)) {
f[++cnt]=n; ans=min(ans, n);
return;
}
ll p=n;
while(p==n) p=PR(n, rand(1, n-1));
find(p); find(n/p);
} int main() {
int T; scanf("%d", &T);
while(T--) {
ll n;
scanf("%lld", &n);
cnt=0; ans=n;
find(n);
if(cnt==1) puts("Prime");
else printf("%lld\n", ans);
} return 0;
}
学习了下Pollard-Rho算法= =复杂度期望为$O(n^{1/4})$
具体不说看算导= =
大概就是用$x=x^2+c \pmod{n}$然后判$(y-x, n)$是否=1。其中$y$是第$2^k$个$x$。然后找到一个约数后递归这个约数和n/约数。($c$和初始的$x$随机= =
然后如果碰到环退出就行了= =(一开始不知道为啥被卡了= =原来乘法爆掉了QAQ写个快速乘啊 !!!
【POJ】1811 Prime Test的更多相关文章
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- 【题解】CF45G Prime Problem
[题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- 【数学】Prime-Factor Prime
Prime-Factor Prime 题目描述 A positive integer is called a "prime-factor prime" when the numbe ...
随机推荐
- 攻城狮在路上(叁)Linux(零)--- 软件环境、参考书目等一览表
1.参考书目:鸟哥的Linux私房菜. 2.环境: Cent_os.
- golang基础知识之文件操作
读取文件所有内容以及获得文件操作对象 package mainimport ( "bufio" "fmt" "io" "io/io ...
- android 入门-android Studio 解决方案
一.当提示 解决方案: 1. 2. 二.从这步到这步 的时候,可能遇见下面的问题. 解决方案: 更新一下build-tools 19.1.0版本 放到你的sdk里并重启as. 三. 当遇见这样的情况 ...
- 命令模式/command模式/行为型模式
举个栗子 指挥官向士兵下达命令,士兵执行 实现代码如下: class Soldier { public void exe() { System.out.println("执行命令" ...
- git warning: LF will be replaced by CRLF in...
如果你有git项目,在提交代码的过程中可能会碰到上面的警告,特别是的项目中包含序列化对象的时候,你可能要小心!! 警告的含义是说换行符的,不同的操作系统的换行符是不一致的,如果你不清楚,真得看看这个 ...
- 通信原理实践(四)——模拟通信系统性能分析
一.模拟通信系统性能分析 1.系统框图 2.信噪比定义 (1)输入信噪比: (2)输出信噪比: (3)调制制度增益: 3.模拟通信系统分析等价模型 即自己产生一个高斯白噪声,加入到调制信号,然后在送入 ...
- 【T_SQL】 基础 事务
1.使用 T-SQL 语句来管理事务 开始事务:BEGIN TRANSACTION 提交事务:COMMIT TRANSACTION 回滚(撤销)事务:ROLLBAC ...
- JQuery 定时器 (Jquery Timer 插件)
jQuery Timers插件地址: http://plugins.jquery.com/project/timers JQuery Timers应用知识提供了三个函式1. everyTime(时 ...
- JAVA Day3
分支与循环 char sex = in.next().charAt(0); java中 ...
- 常用meta标签举例说明
本文是作者从百度百科和其他从网页中搜索到的资料,经综合整理,把常用meta标签列举并示例说明,仅供参考. 1.<meta charset="UTF-8"> --- ch ...