【BZOJ4802】欧拉函数(Pollard_rho)

题面

BZOJ

题解

这么大的范围肯定不好杜教筛。

考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了。

直接\(Pollard\_rho\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
ll N;
ll Multi(ll a,ll b,ll MOD)
{
ll s=0;
while(b){if(b&1)s=(s+a)%MOD;a=(a+a)%MOD;b>>=1;}
return s;
}
ll fpow(ll a,ll b,ll MOD)
{
ll s=1;
while(b){if(b&1)s=Multi(s,a,MOD);a=Multi(a,a,MOD);b>>=1;}
return s;
}
bool Miller_Rabin(ll n)
{
if(n==2)return true;
for(int tim=10;tim;--tim)
{
ll a=rand()%(n-2)+2,p=n-1;
if(fpow(a,n-1,n)!=1)return false;
while(!(p&1))
{
p>>=1;ll nw=fpow(a,p,n);
if(Multi(nw,nw,n)==1&&nw!=1&&nw!=n-1)return false;
}
}
return true;
}
vector<ll> fac;
ll Pollard_rho(ll n,int c)
{
ll i=0,k=2,x=rand()%(n-1)+1,y=x;
while(233)
{
++i;x=(Multi(x,x,n)+c)%n;
ll d=__gcd((y-x+n)%n,n);
if(d!=1&&d!=n)return d;
if(x==y)return n;
if(i==k)y=x,k<<=1;
}
}
void Fact(ll n,int c)
{
if(n==1)return;
if(Miller_Rabin(n)){fac.push_back(n);return;}
ll p=n;while(p>=n)p=Pollard_rho(p,c--);
Fact(p,c);Fact(n/p,c);
}
int main()
{
cin>>N;Fact(N,233);
sort(fac.begin(),fac.end());
int l=unique(fac.begin(),fac.end())-fac.begin();
for(int i=0;i<l;++i)N=N*(1.0*(fac[i]-1)/fac[i]);
cout<<N<<endl;
return 0;
}

【BZOJ4802】欧拉函数(Pollard_rho)的更多相关文章

  1. BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)

    Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...

  2. BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)

    题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...

  3. bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...

  4. [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法

    啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...

  5. BZOJ4802 欧拉函数 数论

    原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...

  6. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  7. [BZOJ4802]欧拉函数

    bzoj description 给出\(n\),求\(\varphi(n)\).\(n\le10^{18}\) sol \(Pollard\ Rho\),存个代码. code #include< ...

  8. bzo4802 欧拉函数 miller_rabin pollard_rho

    欧拉函数 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 1112  Solved: 418[Submit][Status][Discuss] Descr ...

  9. BZOJ 4802 欧拉函数(Pollard_Rho)

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

随机推荐

  1. Matplotlib API汉化

    Pyplot API 示例汇总:https://matplotlib.org/gallery/index.html#api-examples 该matplotlib.pyplot模块包含的功能允许您快 ...

  2. My status

    I haven‘t any one who is strong relationship with me. My skill is normal. I'm not interesting in neg ...

  3. linux-ubuntu常用命令(深圳文鹏)

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  4. 用树莓派Raspberry Pi和Micro:bit做一个自拍器

    在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...

  5. Lua学习笔记(2): 流程控制与循环以及初涉迭代器

    条件判断语句 --if...语句 if (表达式) then --表达式为1时执行的语句 end --if...else语句 if (表达式) then --表达式为1时执行的语句 else --表达 ...

  6. 【转】PHPCMS+PHPExcel实现后台数据导入导出功能

    首先,上图之中的红色框框是没有的,我们想要给他加上,当然是要改HTML页面啦,废话,我们跟ECSHOP一样由PHP路径找模板: 看看路由原理: 首先,上图之中的红色框框是没有的,我们想要给他加上,当然 ...

  7. IO模型浅析-阻塞、非阻塞、IO复用、信号驱动、异步IO、同步IO

    最近看到OVS用户态的代码,在接收内核态信息的时候,使用了Epoll多路复用机制,对其十分不解,于是从网上找了一些资料,学习了一下<UNIX网络变成卷1:套接字联网API>这本书对应的章节 ...

  8. Python3实现机器学习经典算法(一)KNN

    一.KNN概述 K-(最)近邻算法KNN(k-Nearest Neighbor)是数据挖掘分类技术中最简单的方法之一.它具有精度高.对异常值不敏感的优点,适合用来处理离散的数值型数据,但是它具有 非常 ...

  9. 微软职位内部推荐-Senior Software Engineer II-Sharepoint

    微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...

  10. [笔记] postgresql 流复制(streaming replication)

    基本环境说明: os:FreeBSD 9.3 postgresql version: master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 ...