题目

求大数的欧拉函数φ\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);
}

最新文章

  1. HTML5的全新语义化元素
  2. SpaceSniffer 硬盘透视软件
  3. Delphi操作Excel大全
  4. swift学习笔记之-自动引用计数
  5. edge.js架起node.js和.net互操作桥梁
  6. request对象多种方法封装表单数据
  7. struct和class 区别
  8. Spring RESTful服务接收和返回JSON最佳实践
  9. pyqt样式表语法笔记(下)--原创
  10. arcgis server 中Web墨卡托投影与WGS-84坐标的转换
  11. 爬虫 BeatifulSoup 模块
  12. 20164301 Exp4 恶意代码分析
  13. 什么是CSR以及CSR的作用和生成
  14. UVA 815 Flooded!
  15. android studio 中去除应用标题栏
  16. 登录tomcat服务器首页直接跳转到项目
  17. sql 一列拼接成一行,再分割成列
  18. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控
  19. spring的普通类中获取session和request对像
  20. __import__ 与动态加载 python module

热门文章

  1. ubuntu下安装amqp扩展
  2. python基础学习(九)
  3. Python循环的基本使用(for in、while)
  4. 了解CAdoSqlserver
  5. windows下捕获本地回环网络中的报文RawCap
  6. Go 互斥锁(sync.Mutex)和 读写锁(sync.RWMutex)
  7. weui中的picker滑动报错
  8. logback配置文件模板
  9. python 将字符串中的unicode字符码转换成字符
  10. PCI总线学习