题目

求大数的欧拉函数φ\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)的更多相关文章

  1. bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...

  2. BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)

    Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...

  3. [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法

    啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...

  4. [BZOJ4802]欧拉函数

    bzoj description 给出\(n\),求\(\varphi(n)\).\(n\le10^{18}\) sol \(Pollard\ Rho\),存个代码. code #include< ...

  5. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  6. BZOJ4802 欧拉函数 数论

    原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...

  7. 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式

    找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...

  8. BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数

    BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sa ...

  9. 【BZOJ4802】欧拉函数(Pollard_rho)

    [BZOJ4802]欧拉函数(Pollard_rho) 题面 BZOJ 题解 这么大的范围肯定不好杜教筛. 考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了. 直接\(Pollard\_rho\ ...

随机推荐

  1. Windows Subsystem for Linux(wsl)使用

    由于项目有一些环境需要在linux环境运行.可用微软win10的WSL来搭配使用 安装wsl 控制面板--程序和功能--启用或关闭windows功能,适用于linux的windows系统 应用商城下载 ...

  2. python 之 前端开发(CSS三大特性、字体属性、文本属性、背景属性)

    11.38 css三大特性 11.381 继承性 1.定义:给某一个元素设置一些属性,该元素的后代也可以使用,这个我们就称之为继承性​2.注意:    1.只有以color.font-.text-.l ...

  3. python基础 — CSV 数据处理

    什么是csv 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本) 编码: encode  ...

  4. PAT(B) 1089 狼人杀-简单版(Java)逻辑推理

    题目链接:1089 狼人杀-简单版 (20 point(s)) 题目描述 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局" ...

  5. PB之取下来列修改后的值(AcceptText)

    AcceptText()功能 将“漂浮”在数据窗口控件上编辑框的内容放入到数据窗口控件的当前项中(主缓区中).在将数据放入到当前项之前,编辑框中的数据必须通过有效性规则检查语法  dwcontrol. ...

  6. python3 内置方法 字符串转换为字典

    内置方法:eval()将字符串转换为字典代码: str = '''{'backend':'www.oldboy.org', 'record':{ 'server':'122.111.2.23', 'w ...

  7. (转)高效线程池之无锁化实现(Linux C)

    本文链接:https://blog.csdn.net/xhjcehust/article/details/45844901 笔者之前照着通用写法练手写过一个小的线程池版本,最近几天复习了一下,发现大多 ...

  8. Yii2 redis 使用

    首先要安装一下redis的扩展 composer require yiisoft/yii2-redis 在配置文件中添加redis配置 'components' => [ .... 'redis ...

  9. 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)

    原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  10. Xcode如何快速定位crash的位置?

    最近发现经常有人程序崩掉后不知道怎么定位crash的位置 如何快速定位crash的位置? 选择右箭头 选择Add Exception Breakpoint 这样如果你的app再crash就会自动定位到 ...