miller_rabin + pollard_rho模版
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#define MAXN 100000
using namespace std;
typedef unsigned long long LL;
LL fac[MAXN],cnt,G,L,m,p;
LL min(LL a,LL b)
{
return a<b?a:b;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL mult_mod(LL a,LL b,LL mod)
{
LL ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%mod;
a=(a<<1)%mod;
b>>=1;
}
return ans;
}
LL pow_mod(LL a,LL b,LL mod)
{
LL d=1;
a%=mod;
while(b)
{
if(b&1)
d=mult_mod(d,a,mod);
a=mult_mod(a,a,mod);
b>>=1;
}
return d%mod;
}
bool witness(LL a,LL n)
{
LL u=n-1,t=0;
while((u&1)==0)
{
u>>=1;
t++;
}
LL x,x0=pow_mod(a,u,n);
for(LL i=1; i<=t; i++)
{
x=mult_mod(x0,x0,n);
if(x==1&&x0!=1&&x0!=(n-1))
return true;
x0=x;
}
if(x!=1)
return true;
return false;
}
bool miller_rabin(LL n)
{
if(n==2) return true;
if(n<2||!(n&1)) return false;
for(int j=1; j<=8; j++)
{
LL a=rand()%(n-1)+1;
if(witness(a,n))
return false;
}
return true;
}
LL pollard_rho(LL n,LL c)
{
LL i=1,k=2,d,x=2,y=2;
while(true)
{
i++;
x=(mult_mod(x,x,n)+c)%n;
d=gcd(y-x,n);
if(d!=1&&d!=n)
return d;
if(x==y) return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find_fac(LL n,LL c)
{
if(miller_rabin(n)||n<=1)
{
fac[cnt++]=n;
return;
}
LL p=pollard_rho(n,c);
while(p>=n) p=pollard_rho(p,c--);
find_fac(p,c);
find_fac(n/p,c);
}
void dfs( LL step,LL num)
{
if(step==cnt||num>p)
{
if(num<=p&&num>m)
m=num;
return;
}
dfs(step+1,num*fac[step]);
dfs(step+1,num);
}
int main()
{
srand(time(NULL));
while(scanf("%I64u%I64u",&G,&L)!=EOF)
{
cnt=0;
find_fac(L/G,181);
sort(fac,fac+cnt);
LL i=0,t=0;
while(i<cnt)
{
LL ans=1,j=i;
while(j<cnt&&fac[i]==fac[j])
{
ans*=fac[i];
j++;
}
fac[t++]=ans;
i=j;
}
cnt=t,m=1,p=sqrt(0.0+(L/G));
dfs(0,1);
printf("%I64u %I64u\n",m*G,L/m);
}
return 0;
}
miller_rabin + pollard_rho模版的更多相关文章
- 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)
注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...
- bzo4802 欧拉函数 miller_rabin pollard_rho
欧拉函数 Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 1112 Solved: 418[Submit][Status][Discuss] Descr ...
- 随机算法 - Miller_Rabin pollard_rho
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 计蒜客 18487.Divisions-大数的所有因子个数-Miller_Rabin+Pollard_rho-超快的(大数质因解+因子个数求解公式) (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 F)
这一场两个和大数有关的题目,都用到了米勒拉宾算法,有点东西,备忘一下. 题目传送门 F. Divisions 传送门 这个题是求一个数的所有因子个数,但是数据比较大,1e18,所以是大数的题目,正常的 ...
- BZOJ4802 欧拉函数 数论
原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...
- PLAN OF HEOI(unfinished)
Au:整体二分/计算几何/多项式/fwtAg:可持久化重量平衡树/线段树分治/线段树合并/最短路树/最短路DAGCu:三分Up:博弈论/置换群/杜教筛/矩阵树定理/BSGS/动态树分治/网络流(线性规 ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- HDU-3864 D_num Miller_Rabin和Pollard_rho
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...
- pollard_rho和Miller_Rabin
Miller_Rabin就是以概论大小来判断素数 可以判断2^63范围的数 pollard_rho推荐两个很好的博客来理解:整数分解费马方法以及Pollard rho和[ZZ]Pollard Rho算 ...
随机推荐
- (WWWWWWWWWW)codevs 3305 水果姐逛水果街Ⅱ
写这么长了不A有点舍不得.. 想A又调不出来.. 于是乎就存一下.. 屠龙宝刀点击就送 #include <cstdio> #include <vector> #define ...
- 在SAP CRM WebClient UI中用javascript触发ABAP event
环境:SAP CRM WebClient UI 需求:在WebClient UI里不通过用户手动点击,而是使用JavaScript代码自动触发ABAP后台的代码. 解决方案: 1. 定义一个hidde ...
- 团队作业-Beta冲刺第二天
这个作业属于哪个课程 <https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1> 这个作业要求在哪里 <https ...
- softmax_loss
softmax_loss中的ignore_label是来自于loss layer,而不是softmax_loss的参数
- request :fail url not in domain list
1.可点击开发者工具右上角 详情-域名信息,看看是否配置了域名: 2. 手机预览小程序的时候,打开调试
- bootstrap下拉菜单(Dropdowns)
本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...
- JS与IOS、安卓的交互
最近做的项目中涉及到了与安卓和ios的交互问题,对于一个新手来说,多多少少会有点迷糊.在调用安卓和ios的callback回调时,很轻松的就调用成功了,而且,步骤也不那么繁琐.刚开始,只知道那样使用可 ...
- Vnc自动登录器(VncManager)v1.3-多国语言绿色版-Release1-20190215
Vnc自动登录器 v1.3 (20190215) By: ybmj@vip.163.com , https://www.cnblogs.com/ybmj/ 下载地址:http://bbs.wuyou. ...
- vim中使用pydiction对python代码进行补全
在配置完vim插件YouCompleteMe之后,在vim编写python的时候按tab键会报错,E121: Undefined variable: g:pydiction_location Pydi ...
- 将find过滤添加到数组
array=($(find . -name "*.txt"))for i in "${array[@]}"; do echo $i; done