原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html


题目传送门 - BZOJ4802


题意概括

Description

已知N,求phi(N)

Input

正整数N。N<=10^18

题解

  Miller_Rabin+Pollard_Rho

  至于Pollard_Rho,我感到很奇怪。判定的时候为何不能丢第一个值!!

  请看下面两个代码,第一个对的,第二个错的……


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
LL Mul(LL a,LL b,LL mod){
LL ans=0;
a%=mod;
while (b){
if (b&1)
ans=(ans+a)%mod;
b>>=1,a=(a<<1)%mod;
}
return ans;
}
LL Pow(LL a,LL b,LL mod){
LL ans=1;
a%=mod;
while (b){
if (b&1)
ans=Mul(ans,a,mod);
b>>=1,a=Mul(a,a,mod);
}
return ans;
}
bool Miller_Rabin(LL n){
if (n==2)
return 1;
if (n<2||n%2==0)
return 0;
LL m=n-1,k=0;
while (!(m&1))
m>>=1,k++;
for (int i=0;i<10;i++){
LL a=rand()%(n-1)+1,x=Pow(a,m,n),y;
for (int j=0;j<k;j++){
y=Mul(x,x,n);
if (y==1&&x!=1&&x!=n-1)
return 0;
x=y;
}
if (x!=1)
return 0;
}
return 1;
}
LL rnd(LL x,LL n,LL c){
return (Mul(x,x,n)+c)%n;
}
LL Pollard_Rho(LL n,LL c){
LL x,y;
while (1){
x=rand()%(n-1)+1,y=rnd(x,n,c);
while (1){
if (x==y)
break;
LL d=gcd(llabs(y-x)%n,n);
if (1<d&&d<n)
return d;
x=rnd(x,n,c);
y=rnd(rnd(y,n,c),n,c);
}
c=rand()%n;
}
}
LL n,x[66],pcnt;
void find(LL n){
if (n==1)
return;
if (Miller_Rabin(n)){
x[++pcnt]=n;
return;
}
LL p=Pollard_Rho(n,rand()%n);
find(p);
find(n/p);
}
int main(){
srand(19260817);
scanf("%lld",&n);
pcnt=0;
find(n);
sort(x+1,x+pcnt+1);
LL yz=pcnt?x[1]-1:1;
for (int i=2;i<=pcnt;i++)
yz=yz*(x[i]-(bool)(x[i]!=x[i-1]));
printf("%lld\n",yz);
return 0;
}

  

BZOJ4802 欧拉函数 数论的更多相关文章

  1. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

  2. 中国剩余定理(CRT)与欧拉函数[数论]

    中国剩余定理 ——!x^n+y^n=z^n 想必大家都听过同余方程这种玩意,但是可能对于中国剩余定理有诸多不解,作为一个MOer&OIer,在此具体说明. 对于同余方程: x≡c1(mod m ...

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

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

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

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

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

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

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

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

  7. Bzoj 2186: [Sdoi2008]沙拉公主的困惑 乘法逆元,线性筛,欧拉函数,数论

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2560  Solved: 857[Submit][St ...

  8. 紫书 例题 10-26 UVa 11440(欧拉函数+数论)

    这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...

  9. Bzoj 2749: [HAOI2012]外星人 欧拉函数,数论,线性筛

    2749: [HAOI2012]外星人 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 568  Solved: 302[Submit][Status][ ...

随机推荐

  1. mysql优化——show processlist命令详解

    SHOW PROCESSLIST显示哪些线程正在运行 不在mysql提示符下使用时用mysql -uroot  -e 'Show  processlist'   或者   mysqladmin pro ...

  2. 学了这么久,vue和微信小程序到底有什么样的区别?

    前言 写了vue项目和小程序,发现二者有许多相同之处,在此想总结一下二者的共同点和区别.相比之下,小程序的钩子函数要简单得多. 一.生命周期 先贴两张图: vue生命周期 小程序生命周期   相比之下 ...

  3. 用VS制作的windows服务安装包 安装完后如何让服务自动启动

    vs 服务做成安装包,如何安装以后启动服务,只要在类名为projectinstaller的类中重写commit事件即可         public override void Commit(IDic ...

  4. Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较以及问题

    Net 4.5 WebSocket在Windows 8, Windows 10, Windows Server 2012可以,但是在Windows 7, 就会报错. 错误1.“一个文件正在被访问,当前 ...

  5. 解决在编程方式下无法访问Spark Master问题

    我们可以选择使用spark-shell,spark-submit或者编写代码的方式运行Spark.在产品环境下,利用spark-submit将jar提交到spark,是较为常见的做法.但是在开发期间, ...

  6. Eclipse搭建C++\C开发环境

    1.最近使用visualStudio IDE开发Unity 3D使用的编程语言是C#但是发现visualStudio12 版本在自己主机上运行速度比够快,怀疑是不是处理器或者是版本问题,所以该卸载了, ...

  7. C语言将字符串转json

    示例代码: #include <stdio.h> #include <string.h> #include <stdlib.h> char *strrpc(char ...

  8. ajax请求数据时什么时候用GET,什么时候用POST

    GET的目的就如同其名字一样是用于获取信息的.它旨在显示出页面上你要阅读的信息.浏览器会缓冲GET请求的执行结果,如果同样的GET请求再次发出,浏览器就会显示缓冲的结果而不是重新运行整个请求.重新请求 ...

  9. 学习Maven POM

    什么是POM POM stands for "Project Object Model".It is an XML representation of a Maven projec ...

  10. java运行环境增加字体

    背景 今天在使用jfreeChart时,显示中文乱码,如下图: 环境:Ubuntu 13.04 64bit  java7  tomcat 7.0.42 解决方法--增加系统字体 0. 安装环境包 su ...