【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4802

【题目大意】

  已知N,求phi(N),N<=10^18

【题解】

  我们用Pollard_Rho对N进行质因数分解,然后计算欧拉函数即可。

【代码】

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define C 2730
#define S 3
using namespace std;
typedef long long ll;
ll n,m,cnt,cnf,ans;
vector<ll> v;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mul(ll a,ll b,ll n){return(a*b-(ll)(a/(long double)n*b+1e-3)*n+n)%n;}
ll pow(ll a, ll b, ll n){
ll d=1; a%=n;
while(b){
if(b&1)d=mul(d,a,n);
a=mul(a,a,n);
b>>=1;
}return d;
}
bool check(ll a,ll n){
ll m=n-1,x,y;int i,j=0;
while(!(m&1))m>>=1,j++;
x=pow(a,m,n);
for(i=1;i<=j;x=y,i++){
y=pow(x,2,n);
if((y==1)&&(x!=1)&&(x!=n-1))return 1;
}return y!=1;
}
bool miller_rabin(int times,ll n){
ll a;
if(n==1)return 0;
if(n==2)return 1;
if(!(n&1))return 0;
while(times--)if(check(rand()%(n-1)+1,n))return 0;
return 1;
}
ll pollard_rho(ll n,int c){
ll i=1,k=2,x=rand()%n,y=x,d;
while(1){
i++,x=(mul(x,x,n)+c)%n,d=gcd(y-x,n);
if(d>1&&d<n)return d;
if(y==x)return n;
if(i==k)y=x,k<<=1;
}
}
void findfac(ll n,int c){
if(n==1)return;
if(miller_rabin(S,n)){
v.push_back(n);
return;
}ll m=n;
while(m==n)m=pollard_rho(n,c--);
findfac(m,c),findfac(n/m,c);
}
int main(){
while(~scanf("%lld",&n)){
findfac(n,C);
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
ll phi=n;
for(int i=0;i<v.size();i++){
ll y=v[i];
phi=(phi/y)*(y-1);
}printf("%lld\n",phi);
}return 0;
}

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)

    开始一直T,原来是没有srand- CODE #include<bits/stdc++.h> using namespace std; typedef long long LL; vect ...

  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. 【BZOJ4802】欧拉函数(Pollard_rho)

    [BZOJ4802]欧拉函数(Pollard_rho) 题面 BZOJ 题解 这么大的范围肯定不好杜教筛. 考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了. 直接\(Pollard\_rho\ ...

  9. bzo4802 欧拉函数 miller_rabin pollard_rho

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

随机推荐

  1. 小程序_RSA加密功能

    这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...

  2. 打开Android系统安装APK的页面

    //使用隐式意图开启安装APK的Activity Intent intent = new Intent("android.intent.action.VIEW"); intent. ...

  3. 【JDK】JDK7与JDK8环境共存与切换:先安装jdk7,配置好环境变量后再安装jdk8

    1.先安装JDK7 下载jdk-7u79-windows-i586.exe,安装后配置好环境变量JAVA_HOME,CLASSPATH,PATH java -version javac 指令都正常 2 ...

  4. 【Python学习笔记】Pandas库之DataFrame

    1 简介 DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表. 或许说它可能有点像matlab的矩阵,但是matlab的矩阵只能放数值型值(当然matla ...

  5. python基础===zmail,收发邮件的模块

    项目地址: GitHub:https://github.com/ZYunH/zmail  介绍: https://mp.weixin.qq.com/s?__biz=MzAxMjUyNDQ5OA==&a ...

  6. 项目评审ppt的纲要

    1.prd不能模糊,产品的问题全部明确 2.收益在哪里 3.设计体现业务4.怎样保证数据的前后协作5.异常如何处理6.技术解决的痛点7.对外部依赖8.性能指标预期(响应时间)9.

  7. Nginx-1.6.3反向代理

    源码安装nginx cat /etc/redhat-release uname -rm yum install pcre-devel openssl-devel -y rpm -qa pcre pcr ...

  8. eclipse out of memory

    eclipse 安装目录 修改 eclipse.ini 在eclipse根目录下打开eclipse.ini,默认内容为(这里设置的是运行当前开发工具的JVM内存分配): -vmargs-Xms40m- ...

  9. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  10. C# 中从程序中下载Excel模板

    方法一: #region 下载模板 /// <summary> /// 下载模板 /// </summary> /// <param name="sender& ...