BZOJ 4802: 欧拉函数 (Pollard-Rho)
开始一直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)的更多相关文章
- BZOJ 4802 欧拉函数
4802: 欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Outp ...
- BZOJ 4802 欧拉函数(Pollard_Rho)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题目大意] 已知N,求phi(N),N<=10^18 [题解] 我们用P ...
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- [bzoj 2818]欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 枚举最大公约数,对于每一个质数p,只需要求出1<=x,y<=(n/p)范 ...
- BZOJ 2190 欧拉函数
思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...
- BZOJ 4805: 欧拉函数求和 杜教筛
https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...
- 【刷题】BZOJ 4805 欧拉函数求和
Description 给出一个数字N,求sigma(phi(i)),1<=i<=N Input 正整数N.N<=2*10^9 Output 输出答案. Sample Input 1 ...
- 数学基础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 ...
随机推荐
- PTA(Basic Level)1039.到底买不买
小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子 ...
- JS写斐波那契数列的几种方法
斐波那契数,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波那契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=Fn-1+Fn-2(n>=2,n∈N*),用文字 ...
- 【opencv源码解析】 三、resize
resize.cpp void cv::resize( InputArray _src, OutputArray _dst, Size dsize, double inv_scale_x, doubl ...
- LeetCode:178.分数排名
题目链接:https://leetcode-cn.com/problems/rank-scores/ 题目 编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注 ...
- GC原理图
GC原理图,Jdk1.6及以下版本 永久代 永久代是Hotspot虚拟机特有的概念,是方法区的一种实现,别的JVM都没有这个东西.在Java 8中,永久代被彻底移除,取而代之的是另一块与堆不相连的本地 ...
- struts-2.5.14.1中jar包引入
- NET如何使用ELinq-实现增删改查
1 通过对ELinq主页的参考和学习,以及在项目中(wpf项目中用到的)中应用,ORM框架中的ELinq确实非常的强大,特此以建立wpf项目为例子来总结下如何在项目中应用ELinq,要想使用这个框架首 ...
- span元素
<span>标签属于行内元素(inline),所以无法设置高度和宽度: 如果需要改变其宽高,就需要将其转变为块体元素(block)或行内块体元素(inle-block)
- mysql 利用 case 批量更新
- Oracle权限管理详解(1)
详见:https://www.cnblogs.com/yw0219/p/5855210.html Oracle 权限 权限允许用户访问属于其它用户的对象或执行程序,ORACLE系统提供三种权限:Obj ...