【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 ...
随机推荐
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- maven 依赖查询
该文章源地址:http://xiejianglei163.blog.163.com/blog/static/1247276201362733217604/ 为方便个人使用,转载于此处. http:// ...
- C语言中结构体的位域(bit-fields)
转自:http://blog.sina.com.cn/s/blog_6240b5980100tcba.html 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一 ...
- 11g添加asm
1.创建组 2.创建grid用户 3.用grid安装Gride Infrastructure软件 4.执行root.sh[root@ora11g softdb]# /u01/app/11.2.0/gr ...
- WPF线程(Step2)——BackgroundWorker
在WPF中第二个常用的线程处理方式就是BackgroundWorker. 以下是BackgroundWorker一个简单的例子. public partial class MainWindow : W ...
- Android 第3方控件一览表
1 UnSlideListView 解决在ScrollView的无法正常显示的问题 例子在“真好项目”中“NGDetailActivity”.“HKcfqjActivity”.
- 【rqnoj39】 饮食问题
题目描述 Bessie 正在减肥,所以她规定每天不能吃超过 C (10 <= C <= 35,000)卡路里的食物.农民 John 在戏弄她,在她面前放了B (1 <= B < ...
- [转载]explicit关键字
本文转自http://www.programlife.net/cpp-explicit-keyword.html. 其实explicit主要用于防止隐式转换,用于修饰构造函数.复制构造函数[注意:一般 ...
- SPOJ DISUBSTR 后缀数组
题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...
- Scau 8633 回文划分 mancher + dp
时间限制:1000MS 内存限制:1000K 提交次数: 通过次数: 题型: 编程题 语言: G++;GCC Description 我们说一个字符串是回文串,那么意味着这个串从两边读起来的字母都是一 ...