【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 ...
随机推荐
- VS对路径的访问被拒绝
问题:权限问题. 1.检查自己电脑账户.是否是Administrator.如果没有启用.在[我的电脑]——[管理]——[本地用户和组]——[用户]——[Administrator]——[属性启用] 2 ...
- linux中创建gpio节点
转自:http://blog.chinaunix.net/uid-29165999-id-4296162.html #define GPIO_MAJOR 230 // major device NO. ...
- ***电商数据库设计参考:ecshop数据库+订单表结构等
ecshop订单表结构ecs_order_info说明 -- 表的结构 `ecs_order_info` CREATE TABLE IF NOT EXISTS `ecs_order_info` ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- 扩展LV
LVM最大的特性就是可以弹性调整磁盘容量下面扩展一个已经存在的LV [root@ol6--rac1 mnt]# lvdisplay --- Logical volume --- LV Path /de ...
- 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)
原文 http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...
- 7-13IN和NOT IN 子查询
IN后面的子查询可以返回多条记录. SELECT ...FROM WHERE 查询表达式 IN(子查询) 常用IN替换等于(=)的比较子查询. 用法: (1)使用 :IN关键字可以使父查询匹配子查询 ...
- AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(三):配置ActiveXForm运行环境
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- [Eclipse][SVN] 在eclipse上安装SVN
以前装过好多次SVN,始终没有一次把安装过程记录下来,这次新装机器,安装SVN插件时一波三折,记录下来免得以后又忘记了. 方法一: 1. 直接通过后台添加URL通过互联网进行安装,直接上图: 2. ...
- WebRTC代码走读(十):rtp_rtcp模块分析,webrtcrtp_rtcp
转自:http://www.bkjia.com/Androidjc/1020017.html 1. 对外提供的主要流程接口 收包的调用接口RtpReceiverImpl::Incoming ...