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][ ...
随机推荐
- layui 各种弹出框
第一步:引用文件 第二步:写脚本 <script> //iframe窗 //layer.open({ // type: 2, // title: false, // closeBtn: 0 ...
- nginx负载均衡后端tomcat无法加载js资源
JS或css无法完全加载 nginx的代理缓存区,默认较小导致部分文件出现加载不全的问题,比较典型的如jQuery框架,可以通过配置调整nginx的缓存区即可.主要参考proxy参数 最终完整配置如下 ...
- 驳2B文 "我为什么放弃Go语言"
此篇文章流传甚广, 其实里面没啥干货, 而且里面很多观点是有问题的. 这个文章在 golang-china 很早就讨论过了. 最近因为 Rust 1.0 和 1.1 的发布, 导致这个文章又出来毒 ...
- C#操作excel(多种方法比较)
1.用查询表的方式查询并show在数据集控件上. public static string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; D ...
- swift 学习- 19 -- 可选链式调用
// 可选链式调用 是一种在当前值可能为 nil 的可选值上请求 和 调用属性, 方法以及下标, 如果 可选值有值, 那么调用就会成功, 如果可选值是 nil, 那么就会将返回 nil , // 多个 ...
- Confluence 6 安装一个语言组件
Confluence 捆绑了一系列的语言包.这些语言包在 'Language Configuration' 界面中的语言选项中.在 Confluence 的管理员控制台,你可以选择 Choosing ...
- Confluence 6 编辑站点欢迎消息
通过编辑欢迎信息能够为你站点的主页面添加一些个人信息. 站点的欢迎信息显示在站点主面板的右侧,这是你对站点添加声明,连接,有关你项目组美好回忆照片的完美位置. 你需要 Confluence 管理员权限 ...
- django rest framework(3)
目录 一.版本 二.解析器 三.序列化 四.请求数据验证 一.版本 程序也来越大时,可能通过版本不同做不同的处理 没用rest_framework之前,我们可以通过以下这样的方式去获取. class ...
- django----基于Form组件实现的增删改和基于ModelForm实现的增删改
一.ModelForm的介绍 ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 lab ...
- python+selenium十五:CSS与Jquery
在css中,id用#表示,class用.表示,要定位标签直接写标签名,其他属性就用[xxx='xxx'] 一.css定位 1.属性定位:可以通过任意属性定位,不局限于id.class.name.tag ...