题目

求大数的欧拉函数φ\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. springboot:bootstrap和application有什么区别?

    bootstrap和application区别: Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap,另外一种是 a ...

  2. Python10之列表1(创建列表和列表添加元素的方法)

    一.创建列表 列表名 = [’元素一‘,’元素二‘,’元素三‘,’元素四‘] 列表中的元素可以是任何类型,甚至可以是一个列表. list1 = ['詹姆斯','戴维斯','保罗乔治','字母哥',in ...

  3. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  4. vue防止闪屏小技巧:[v-cloak]

    css 内添加此属性[v-cloak] { display: none; } html中引入即可 <div v-cloak> {{ message }} </div> 如果觉得 ...

  5. Luogu5400 CTS2019随机立方体(容斥原理)

    考虑容斥,计算至少有k个极大数的概率.不妨设这k个数对应的格子依次为(k,k,k)……(1,1,1).那么某一维坐标<=k的格子会对这些格子是否会成为极大数产生影响.先将这样的所有格子和一个数集 ...

  6. VMware Workstation Pro 15 序列号

    VMware Workstation Pro 15 序列号: GA70H-8TYE2-H886P-04YZC-YVA84 YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8 UG5J2-0ME ...

  7. 使用postman mock server

    需要写一个小的Java程序,用来调用云平台的接口 由于云平台的接口程序还没有写好,只能用模拟的方式先行开发代码, 用了post来模拟接口程序. 需要模拟的接口如下: ■请求地址 /openapi/ip ...

  8. IDEA 导入 NodeJS 项目部署启动

    1.导入项目 2.添加模块 3.配置启动项 4.启动 5.备注 如果不明白,新建一个项目查看配置详情 原文地址:https://blog.csdn.net/tiankongzhichenglyf/ar ...

  9. vmware vSphere Data Protection 6.1--------2-初始化

    一.简介 安装完vdp接下来就是部署初始化了 安装篇请参考:vmware vSphere Data Protection 6.1部署 二.开始初始化 登陆https://192.168.216.200 ...

  10. wannafly 挑战赛10 小H和游戏

    题解: 先利用dfs找出各个节点之间的关系.然后利用一个sum[i][j] 数组  sum[i][0] 表示i这个节点收到影响的次数 sum[i][1]表示i这个节点的儿子们收到影响的次数 sum[i ...