开始一直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. 为什么fastjson字段为null时不输出空字符串?

    为什么fastjson字段为null时不输出空字符串? Map < String , Object > jsonMap = new HashMap< String , Object& ...

  2. DotNet跨平台 - .net core项目部署到centos7

    环境说明 系统:CentOS Linux release 7.2.1511 (Core) 相关工具:VS2017  xftp 服务器软件:.net core2.0,nginx 准备.net core应 ...

  3. 关于windows下编写的shell脚本在linux下无法运行报错问题

    首先,你写的shell脚本必须是正确的, 其次,无法运行的原因:因为windows下的换行是两个字节,而你上传到linux,linux下换行是两个字节,所以编译的酒不正确的,导致无法 运行脚本, 这种 ...

  4. 搞懂String、StringBuffer、StringBuilder的区别

    String.StringBuffer.StringBuilder有什么区别呢? 1.String: 首先String是不可变的这是家喻户晓的,它的底层是用一个final修饰的char数组来保存数据的 ...

  5. WeakReference 与 SoftReference 区别

    装载自:http://flyneil.iteye.com/blog/1345177 WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. Wea ...

  6. python3 中的cls和self的区别 静态方法和类方法的区别

      一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某 ...

  7. 【jekins】tomcat+jenkins

    启动jiekins:进入tomcat安装路径下的bin文件,运行startup.bat文件 打开http://127.0.0.1:8080/jenkins 地址 一.构建github项目 原理:构建项 ...

  8. 转载:Java:字节流和字符流(输入流和输出流)

    本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...

  9. Prometheus 监控 Redis 集群的正确姿势

    Prometheus 监控Redis的正确姿势(redis集群) Prometheus 监控 Redis cluster,其实套路都是一样的,使用 exporter. exporter 负责采集指标, ...

  10. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...