BZOJ4802 欧拉函数 数论
原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html
题目传送门 - BZOJ4802
题意概括
Description
Input
题解
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 欧拉函数 数论的更多相关文章
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
- 中国剩余定理(CRT)与欧拉函数[数论]
中国剩余定理 ——!x^n+y^n=z^n 想必大家都听过同余方程这种玩意,但是可能对于中国剩余定理有诸多不解,作为一个MOer&OIer,在此具体说明. 对于同余方程: x≡c1(mod m ...
- bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...
- BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)
Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...
- BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)
题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...
- [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法
啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...
- Bzoj 2186: [Sdoi2008]沙拉公主的困惑 乘法逆元,线性筛,欧拉函数,数论
2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2560 Solved: 857[Submit][St ...
- 紫书 例题 10-26 UVa 11440(欧拉函数+数论)
这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...
- Bzoj 2749: [HAOI2012]外星人 欧拉函数,数论,线性筛
2749: [HAOI2012]外星人 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 568 Solved: 302[Submit][Status][ ...
随机推荐
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- 【原创】大叔经验分享(34)hive中文注释乱码
在hive中查看表结构时中文注释乱码,分为两种情况,一种是desc $table,一种是show create table $table 1 数据库字符集 检查 mysql> show vari ...
- Go斐波拉契数列(Fibonacci)(多种写法)
1 前言 斐波拉契数列有递归写法和尾递归和迭代写法. 2 代码 //recursion func fib(n int) int{ if n < 2{ return n }else{ return ...
- c#获取鼠标坐标
用Control.MousePosition获得当前鼠标的坐标CurrentPoint,使用Control.PointToClient方法,前面获得的CurrentPoint作为其参数,返回的Poin ...
- js调用ajax案例
js调用ajax案例 测试地址:http://www.w3school.com.cn/tiy/t.asp?f=ajax_get 嵌入下面代码,点击提交,再点击请求数据.就可以看到结果了. <ht ...
- Confluence 6 通过 SSL 或 HTTPS 运行 - 确定你的证书路径
在默认的情况下,Tomcat 希望 keystore 文件被命名为 .keystore 文件,同时这个文件应该放置在 Tomcat 运行的 home 目录中(这个目录可能与你自己的 Home 目录的路 ...
- Confluence 6 配置 MySQL 服务器
在这一步,你将要配置你的 MySQL 数据库服务器. 注意: 如果你尝试连接你的 Confluence 到一个已经存在的 MySQL 数据库服务器.我们强烈建议你按照下面描述的安装步骤在 MySQL ...
- 一个简单的 vue.js 实践教程
https://segmentfault.com/a/1190000006776243?utm_source=tuicool&utm_medium=referral 感觉需要改善的地方有: ( ...
- VMware安装windows10系统
- 【supervisor】监控服务
写了一个ftp服务,用supervisor监控一下 1.先写一个配置文件,路径和名称为/etc/supervisord.conf.d/ftp-server.ini [program:ftp-serve ...