BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)
题目
求大数的欧拉函数φ\varphiφ
题解
Pollard-Rho 板子
CODE
#pragma GCC optimize (3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL mul(LL a, LL b, LL p) {
a=(a%p+p)%p, b=(b%p+p)%p;
return (((a*b)-(LL)((long double)a*b/p)*p)%p+p)%p;
}
inline LL qpow(LL a, LL b, LL p) {
LL re=1;
for(; b; b>>=1, a=mul(a,a,p))
if(b&1) re=mul(re,a,p);
return re;
}
namespace Pollard_Rho {
int bs[5] = { 2, 3, 7, 31, 61 };
bool Miller_Robin(LL x) {
for(int i = 0; i < 5; ++i) if(x == bs[i]) return 1;
LL res = x-1, k = 0;
for(; !(res&1); res>>=1, ++k);
for(int i = 0; i < 5; ++i) {
LL pre = qpow(bs[i], res, x), now;
for(int t = k; t--; swap(now, pre))
if((now=mul(pre, pre, x)) == 1 && pre != 1 && pre != x-1)
return 0;
if(pre != 1) return 0;
}
return 1;
}
LL Rho(LL x, LL c) {
LL i = 1, j = 0, sum = 1, a = rand()%(x-1) + 1, b = a, d = 1;
while(d == 1) {
sum = mul(sum, abs((a=(mul(a,a,x)+c)%x)-b), x);
if(++j == i) i<<=1, b = a, d = __gcd(sum, x);
if(!(j&1023)) d = __gcd(sum, x);
}
return d == x ? Rho(x, c+1) : d;
}
map<LL, int>mp;
map<LL, int>::iterator it;
void Pollard(LL x) {
if(x == 1) return;
if(Miller_Robin(x)) { ++mp[x]; return; }
LL tmp = Rho(x, 3);
Pollard(tmp), Pollard(x/tmp);
}
vector<pair<LL,int> > Solve(LL x) {
mp.clear(); Pollard(x);
vector<pair<LL,int> > re;
for(it = mp.begin(); it != mp.end(); ++it)
re.push_back(make_pair(it->first, it->second));
return re;
}
}
LL n;
vector<pair<LL,int> >vec;
int main() {
srand(19260817);
scanf("%lld", &n);
vec = Pollard_Rho::Solve(n);
for(int i = vec.size()-1; i >= 0; --i)
n = n / vec[i].first * (vec[i].first-1);
printf("%lld\n", n);
}
BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)的更多相关文章
- bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...
- BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)
Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...
- [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法
啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...
- [BZOJ4802]欧拉函数
bzoj description 给出\(n\),求\(\varphi(n)\).\(n\le10^{18}\) sol \(Pollard\ Rho\),存个代码. code #include< ...
- 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)
传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...
- BZOJ4802 欧拉函数 数论
原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...
- 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...
- BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数
BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sa ...
- 【BZOJ4802】欧拉函数(Pollard_rho)
[BZOJ4802]欧拉函数(Pollard_rho) 题面 BZOJ 题解 这么大的范围肯定不好杜教筛. 考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了. 直接\(Pollard\_rho\ ...
随机推荐
- Djang简单使用
用户访问内容 用户能够访问的所有的资源,都是程序猿提前暴露的,如果没有暴露,用户是不能进行访问的. diango重启的问题 当我们更改django中的代码的时候,django内部会检测到我们更 ...
- oracle 常用sql 经典sql函数使用 sql语法
各种树操作, 用来查询表中带有子父节点的信息 Oracle 树操作(select-start with-connect by-prior) select m.org_id from sm_organ ...
- 2019牛客暑期多校训练营(第四场)A meeting(dfs或dp,dp待更新)
示例1: 输入: 4 21 23 13 42 4 输出:2 说明: They can meet at place 1 or 3. 题意:从K个点到达不联通图某个点需要的最短时间,这个最短时间是这K个人 ...
- 【Linux】一步一步学Linux——虚拟机安装和卸载(05)
目录 00. 目录 01. Workstation Pro 15.0安装简介 02. Windows 主机上安装 Workstation Pro 15.0 03. Linux 主机上安装 Workst ...
- linux服务器安装oracle
Linux安装Oracle 11g服务器(图文) 应该是最完整的Oracle安装教程了,全程在测试服务器上完成,软件环境:Red Hat Enterprise Linux 6:Oracle 11g ( ...
- mysql 查询表的字段名称,字段类型
select column_name,column_comment,data_type from information_schema.columns where table_name='查询表名称' ...
- Modelsim问题集锦
前言 收集工程调试中遇到的modelsim问题. 问题 (1)仿真发现时钟信号和理论上的数据信号没有边沿对齐. 解决:一般是时钟精度不匹配的问题. 如果想要1ns的精度则代码中的精度需设置为: v语法 ...
- 低功耗蓝牙UUID三种格式转换
熟悉BLE技术同学应该对UUID不陌生,服务.特征值.描述都是有UUID格式定义. 蓝牙广播中对服务UUID格式定义都有三种16 bit UUID.32 bit UUID.128 bit UUID. ...
- 一个让Java事半功倍的反射库
在Java和Android中,我们常常会使用反射来达到一些兼容的目的.Java原生提供的反射很是麻烦,使用起来很是不方便.比如我们想要调UserManager的静态方法get,使用原生的实现如下 tr ...
- python网络爬虫之爬取图片
今天使用requests和BeautifulSoup爬取了一些图片,还是很有成就感的,注释可能有误,希望大家多提意见: 方法一:requests import requests from bs4 im ...