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的更多相关文章

  1. 【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, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【题解】UVA10140 [Prime Distance]

    [题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...

  4. 【题解】CF45G Prime Problem

    [题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...

  5. 【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$都是偶数啦~ ...

  6. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  7. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  8. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  9. 【数学】Prime-Factor Prime

    Prime-Factor Prime 题目描述 A positive integer is called a "prime-factor prime" when the numbe ...

随机推荐

  1. ***PHP Notice: Undefined index: ..问题的解决方法

    首先,这个不是错误,是warning.所以如果服务器不能改,每个变量使用前应当先定义. 方法1:服务器配置修改     修改php.ini配置文件,error_reporting = E_ALL &a ...

  2. 那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传

    一.上传图片并在前端展示 为了避免前端整体刷新,我们采用ajax+iframe(兼容所有浏览器)上传,这样用户上传之后就可以立即看到图片: 上传前: 上传后: 前端部分html: <form s ...

  3. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. Solr auto commit 配置

    为了解决写索引时频繁提交带来的效率问题,考虑使用自动提交. 在solrconfig.xml中增加以下代码: <updateHandler class="solr.DirectUpdat ...

  5. Jenkins搭建

    1.添加仓库: sudo wget -O /etc/yum.repos.d/jenkins.repo \ http://jenkins-ci.org/redhat/jenkins.repo sudo ...

  6. RTP 与RTCP 解释. 含同步时间戳

    转自:http://blog.csdn.net/wudebao5220150/article/details/13816225 RTP协议是real-time transport protocol的缩 ...

  7. webrtc中的带宽自适应算法

    转自:http://www.xuebuyuan.com/1248366.html webrtc中的带宽自适应算法分为两种: 1, 发端带宽控制, 原理是由rtcp中的丢包统计来动态的增加或减少带宽,在 ...

  8. 单例模式/singleton模式/创建型模式

    Java实现要点: 私有构造方法 线程安全(并发的考虑) 延迟加载(效率的考虑,对于较大的类在使用时在加载) 公有方法访问单一实例 常见单例模式代码及问题 //无延迟加载,常驻内存(即使不使用) cl ...

  9. javascript settimeout and setinterval

    setTimeout只运行一次,也就是说设定的时间到后就触发运行指定代码,运行完后即结束.如果运行的代码中再次运行同样的setTimeout命令,则可循环运行.setinterval是循环运行的,即每 ...

  10. sql 根据指定条件获取一个字段批量获取数据插入另外一张表字段中+MD5加密

    /****** Object: StoredProcedure [dbo].[getSplitValue] Script Date: 03/13/2014 13:58:12 ******/ SET A ...