开始一直T,原来是没有srand…

CODE

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<LL>arr;
inline LL multi(LL a, LL b, LL p) {
LL re = a * b - (LL)((long double) a / p * b + 1e-8) * p;
return re < 0 ? re + p : re;
}
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
inline LL qpow(LL a, LL b, LL p) {
LL re = 1;
while(b) {
if(b&1) re = multi(re, a, p);
a = multi(a, a, p); b >>= 1;
}
return re;
}
inline LL Pollard_Rho(LL n, int sed) {
LL i = 1, k = 2, x = rand()%(n-1)+1, y = x;
while(true) {
x = (multi(x, x, n) + sed) % n;
LL p = gcd(n, (y-x+n)%n);
if(p != 1 && p != n) return p;
if(y == x) return n;
if(++i == k) y = x, k <<= 1;
}
}
LL x[100];
inline bool MR(LL n) {
if(n == 2) return 1;
int s = 20, t = 0; LL u = n-1;
while(!(u&1)) ++t, u>>=1;
while(s--) {
LL a = rand()%(n-2) + 2;
x[0] = qpow(a, u, n);
for(int i = 1; i <= t; ++i) {
x[i] = multi(x[i-1], x[i-1], n);
if(x[i] == 1 && x[i-1] != 1 && x[i-1] != n-1) return 0;
}
if(x[t] != 1) return 0;
}
return 1;
}
void find(LL n, int sed) {
if(n == 1) return;
if(MR(n)) { arr.push_back(n); return; }
LL p = n; int k = sed;
while(p == n) p = Pollard_Rho(p, sed--);
find(p, k);
find(n/p, k);
}
LL N;
int main()
{
srand(19260817);
scanf("%lld", &N);
find(N, 107);
sort(arr.begin(), arr.end());
int siz = unique(arr.begin(), arr.end()) - arr.begin();
LL ans = N;
while(siz--)
ans /= arr[siz] , ans *= arr[siz]-1;
printf("%lld\n", ans);
}

BZOJ 4802: 欧拉函数 (Pollard-Rho)的更多相关文章

  1. BZOJ 4802 欧拉函数

    4802: 欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Outp ...

  2. BZOJ 4802 欧拉函数(Pollard_Rho)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题目大意] 已知N,求phi(N),N<=10^18 [题解] 我们用P ...

  3. [BZOJ]4805: 欧拉函数求和

    解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i)  ...

  4. [bzoj 2818]欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 枚举最大公约数,对于每一个质数p,只需要求出1<=x,y<=(n/p)范 ...

  5. BZOJ 2190 欧拉函数

    思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...

  6. BZOJ 4805: 欧拉函数求和 杜教筛

    https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...

  7. 【刷题】BZOJ 4805 欧拉函数求和

    Description 给出一个数字N,求sigma(phi(i)),1<=i<=N Input 正整数N.N<=2*10^9 Output 输出答案. Sample Input 1 ...

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

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

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

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

随机推荐

  1. Oracle定时调用存储过程

    #1Demo: 1.创建表 create table job_table(run_time date); 2.创建存储过程 create or replace procedure job_proc i ...

  2. LeetCode 第 164 场周赛

    访问所有点的最小时间 不难看出,从点(x1,y1) 到 (x2,y2) 的步数需要 min(dx,dy),其中 dx = abs(x1-x2),dy = abs(y1-y2) class Soluti ...

  3. fastdfs 集群搭建

    1.部署FastDFS及Nginx (本套FastDFS为简化版安装部署,只需解压至普通用户家目录下或者任意目录,解压后修改脚本,执行脚本后即可使用.) 说明:FastDFS分为tracker(默认端 ...

  4. docker服务端与客户端通信方式

    docker的服务端与客户端间可以通过unix.tcp方式进行通信.但默认情况下,服务端只监听本地unix接口/var/run/docker.sock,所以客户端只能在服务端所在的机器上使用该unix ...

  5. Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a thin pool

    Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a th ...

  6. Spring、SpringMVC版本及配置

    一.Spring版本 Spring的最新版本是Spring 5.x,Spring 4.x的最后版本是Spring 4.3.x,会维护到2020年(Spring的GitHub主页对此有说明). 二.Sp ...

  7. vue中的键盘事件

    @keydown(键盘按下时触发),@keypress(键盘按住时触发),@keyup(键盘弹起) 获取按键的键码 e.keyCode @keyup.13     按回车键 @keyup.enter ...

  8. linux - 卸载python

    2019年10月15日12:05:42 [root@spider1 bin]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制 ...

  9. 客户端相关知识学习(九)之h5给app传递数据

    方法一: 情况一: if (window.JdAndroid){          window.JdAndroid.setPayCompleted();          window.JdAndr ...

  10. arcgis 服务网页打开需要输入用户名和密码问题解决

    解决方法: 在站点manager中,检查服务的安全性,确认是否是公共.如果不是,设置为公共,面向任何人:如果服务已经被设置为面向公共,那么先设置为私有,面向所选用户,然后再设置为公共,面向任何人 如果 ...