poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小。
这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜索的层数也变的非常少了。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define maxn 10000
LL factor[maxn];
int tot;
const int S=10; //測试次数
LL muti_mod(LL a,LL b,LL c)
{
a%=c;b%=c;
LL ret=0;
while (b){
if (b&1){
ret+=a;
if (ret>=c) ret-=c;
}
a<<=1;
if (a>=c) a-=c;
b>>=1;
}
return ret;
}
LL pow_mod(LL x,LL n,LL mod)
{
if (n==1) return x%mod;
int bit[90],k=0;
while (n){
bit[k++]=n&1;
n>>=1;
}
LL ret=1;
for (k=k-1;k>=0;k--){
ret=muti_mod(ret,ret,mod);
if (bit[k]==1) ret=muti_mod(ret,x,mod);
}
return ret;
}
bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数
LL ret=pow_mod(a,x,n),last=ret;
for (int i=1;i<=t;i++){
ret=muti_mod(ret,ret,n);
if (ret==1 && last!=1 && last!=n-1) return 1;
last=ret;
}
if (ret!=1) return 1;
return 0;
}
bool Miller_Rabin(LL n){ //是素数返回0,合数返回1
LL x=n-1,t=0;
while ((x&1)==0) x>>=1,t++;
bool flag=1;
if (t>=1 && (x&1)==1){
for (int k=0;k<S;k++){
LL a=rand()%(n-1)+1;
if (check(a,n,x,t)) {flag=1;break;}
flag=0;
}
}
if (!flag || n==2) return 0;
return 1;
} LL gcd(LL a,LL b){
if (a==0) return 1;
if (a<0) return gcd(-a,b);
while (b){
LL t=a%b; a=b; b=t;
}
return a;
}
LL Pollard_rho(LL x,LL c){
LL i=1,x0=rand()%x,y=x0,k=2;
while (1){
i++;
x0=(muti_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if (d!=1 && d!=x){
return d;
}
if (y==x0) return x;
if (i==k){
y=x0;
k+=k;
}
}
}
void findfac(LL n)//质因数分解,存在factor里
{
if (!Miller_Rabin(n)){
factor[tot++] = n;
return;
}
LL p=n;
while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);
findfac(p);
findfac(n/p);
}
LL mins,aa,bb;
int top;
void dfs(LL a,LL b,int p)
{
if(a+b>=mins) return;
if(p==top)
{
if(a+b<mins)
{
mins=a+b;
aa=a;
bb=b;
}
return;
}
dfs(a*factor[p],b,p+1);
dfs(a,b*factor[p],p+1);
}
int main()
{
LL a,b,c;
while(~scanf("%lld%lld",&a,&b))
{
if(a==b) {printf("%lld %lld\n",a,b);continue;}
mins=~0ull>>1;
c=b/a;
tot=0;
findfac(c);
sort(factor,factor+tot);
top=0;
for(int i=0;i<tot;i++)
{
if(i==0) factor[top++]=factor[i];
else if(factor[i]==factor[i-1]) factor[top-1]*=factor[i];
else factor[top++]=factor[i];
}
dfs(a,a,0);
if(aa>bb) swap(aa,bb);
printf("%lld %lld\n",aa,bb);
}
return 0;
}
poj 2429 Pollard_rho大数分解的更多相关文章
- Pollard_Rho大数分解模板题 pku-2191
题意:给你一个数n, 定义m=2k-1, {k|1<=k<=n},并且 k为素数; 当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...
- 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811
题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
- HDU4344(大数分解)
题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...
- Miller-Rabbin 素性测试 和 Pollard_rho整数分解
今天学习一下Miller-Rabbin 素性测试 和 Pollard_rho整数分解. 两者都是概率算法. Miller_Rabbin素性测试是对简单伪素数pseudoprime测试的改进. (ps ...
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...
随机推荐
- css基本属性
CSS属性分类(最最常用的属性):颜色和长度文字盒模型布局定位背景文本和其他 常用CSS属性: <style>div{ color:#23729e; /*前景色*/ background: ...
- 编写简单的 NT 式驱动程序的加载与卸载工具
写驱动的加载需要用到五个函数: OpenSCManager() CreateService() OpenService() StartService() CloseServiceHandle() 这五 ...
- (原)使用mkl中函数LAPACKE_sgesv计算矩阵的逆矩阵
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5578027.html 参考文档:mkl的说明文档 lapack_int LAPACKE_sgesv(i ...
- vb.net中存储过程的使用
在机房收费系统过程中,试着使用了存储过程,离之前数据库的学习已经有些日子了.之前对于存储过程的了解也是听过而已,非常不清楚.因此,写这篇博客! 专业概念:存储过程是一个SQL语句和控制结构的集合,创建 ...
- jira汉化
https://translations.atlassian.com/dashboard/dashboard 下载汉化jar文件,在jira中上传插件,系统设置中文即可 LOFTER:我们的故事 ...
- SQL Server 2000/2005 分页SQL — 单条SQL语句
有关分页 SQL 的资料很多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是经过预编译的,执行效率高,也更灵活.先看看单条 SQL ...
- 通过meteor实现的一个照片墙
always love tech 初次使用meteor所遇到的一个问题: insert failed: Method '/pictures/insert' not found 提示没有这个方法,然后可 ...
- 存储过程使用表变量或临时表代替游标Fetch实例,访问远程数据库
定义表变量是可以直接操作在内存中的数据,比较快.临时表在大数据量时会比游标使用的资源少.还是要看具体情况了.也有可能在实际优化过程中相互替换呢. 留作记忆的代码如下: if object_id('te ...
- [Head First Python]6. summary
1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...
- Ubunu下安装mongoDB
mongoDB有两种安装模式: 1. 源码安装 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.2.0.tgz tar zxvf ...