【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 ...
随机推荐
- SQL索引及视图常用语法
ALTER TABLE department ADD INDEX dept_name_idx (name); SHOW INDEX FROM department \G ALTER TABLE dep ...
- Tabular Model下的ADOMD.NET
ADOMD.NET是一套对象架构体系,它包含需要向SSAS数据库做访问的一切支持的对象和方法.很多微软官方以及第三方的SSAS客户端应用都是通过这个对象来操作数据. 多维模式的ADOMD.NET在我以 ...
- bbed的使用--安装及初探
bbed是oracle内部一款用来直接查看和修改数据文件数据的工具,可以直接修改Oracle数据文件块的内容,在一些特殊恢复场景下比较有用. 1.bbed 的安装 在9i/10g中连接生成bbed: ...
- Alcatraz安装 不能用解决方案
1.安装 1>Github上下载Alcatraz,下载地址:https://github.com/supermarin/Alcatraz 2>Alcatraz是xcode的插件,这个插件 ...
- xUtils,butterknife...处理findviewbyid
在写android中,经常要出现大量的findviewbyid et_path = (EditText) findViewById(R.id.et_path); tv_info = (TextVi ...
- DOM、Window对象操作
一.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 一.基本语法: 数据类型(字符串,小数,整数,布尔,时间) var, v ...
- JavaScript模拟函数重载
JavaScript是没有函数重载的,但是我们可以通过其他方法来模拟函数重载,如下所示: <!DOCTYPE html> <html> <head> <met ...
- 本人经过测试认为最简单最好的popupwindow样式
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- solid 设置 ...
- Linux使用jstat命令查看jvm的GC情况
Linux使用jstat命令查看jvm的GC情况 http://www.open-open.com/lib/view/open1390916852007.html http://www.aiuxian ...
- LoadRunner常见函数分析
LoadRunner常见函数分析 ?%Ze\N%?~"_J}.t051Testing软件测试网;FQ X%L|GO+R Web用户Action51Testing软件测试网b,Q8iO w1j ...