GCD & LCM Inverse
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16206   Accepted: 3008

Description

Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.

Input

The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.

Output

For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.

Sample Input

3 60

Sample Output

12 15

Source

题意:
给出gcd和lcm求和最小的两个数a,b
代码:
//a*b=gcd*lcm,x=a/gcd,y=b/gcd,s=lcm/gcd => x*y=s;
//想要a+b小 => x*gcd+y*gcd小 => x+y小,x和y越接近sqrt(S)时x+y越小(对勾函数)
//所以求出s的素因子然后搞一搞就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
typedef long long ll;
const int S=;//随机算法判定次数,S越大判错概率越小
//计算(a*b)%c;a,b,c<2^63
ll mult_mod(ll a,ll b,ll c){
a%=c;
b%=c;
ll ret=;
while(b){
if(b&){
ret+=a;
ret%=c;
}
a<<=;
if(a>=c) a%=c;
b>>=;
}
return ret;
}
//计算(x^n)%c
ll pow_mod(ll x,ll n,ll c){
if(n==) return x%c;
x%=c;
ll tmp=x;
ll ret=;
while(n){
if(n&) ret=mult_mod(ret,tmp,c);
tmp=mult_mod(tmp,tmp,c);
n>>=;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(ll a,ll n,ll x,ll t){
ll ret=pow_mod(a,x,n);
ll last=ret;
for(int i=;i<=t;i++){
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;
last=ret;
}
if(ret!=) return true;
return false;
}
// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(ll n){
if(n<) return false;
if(n==) return true;
if((n&)==) return false;
ll x=n-;
ll t=;
while((x&)==){
x>>=;
t++;
}
for(int i=;i<S;i++){
ll a=rand()%(n-)+;
if(check(a,n,x,t)) return false;
}
return true;
}
ll gcd(ll a,ll b){
if(a==) return ;
if(a<) return gcd(-a,b);
while(b){
ll t=a%b;
a=b;
b=t;
}
return a;
}
//Pollard_rho质因数分解算法
ll factor[];//质因数分解结果(无序的)
int tot;//质因数的个数,数组下标从0开始
ll Pollard_rho(ll x,ll c){
ll i=,k=;
ll x0=rand()%x;
ll y=x0;
while(){
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
ll d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){
y=x0;
k+=k;
}
}
}
//对n进行素因子分解
void findfac(ll n){
if(Miller_Rabin(n)){//素数
factor[tot++]=n;
return;
}
ll p=n;
while(p>=n)
p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
}
ll ans,fa[];
int nu;
void dfs(int st,ll x,ll maxx)
{
if(st>nu){
if(x<=maxx&&x>ans)
ans=x;
return;
}
dfs(st+,x,maxx);
dfs(st+,x*fa[st],maxx);
}
int main()
{
//srand(time(NULL));
ll a,b;
while(scanf("%lld%lld",&a,&b)==){
if(a==b){
printf("%lld %lld\n",a,b);
continue;
}
tot=nu=;
ll s=b/a;
findfac(s);
sort(factor,factor+tot);
fa[]=factor[];
for(int i=;i<tot;i++){//合并相同的素因子
if(factor[i]==factor[i-]) fa[nu]*=factor[i];
else
fa[++nu]=factor[i];
}
ll x=(ll)sqrt(s*1.0);
ans=;
dfs(,,x);
printf("%lld %lld\n",ans*a,s/ans*a);
}
return ;
}

POJ 2429 long long 质因数分解的更多相关文章

  1. poj 3421 X-factor Chains——质因数分解

    题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...

  2. POJ 2167 Irrelevant Elements 质因数分解

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 55 ...

  3. POJ 1845 Sumdiv#质因数分解+二分

    题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...

  4. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  5. poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...

  6. Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)

    一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...

  7. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  8. 求n!质因数分解之后素数a的个数

    n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...

  9. AC日记——质因数分解 1.5 43

    43:质因数分解 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...

随机推荐

  1. python selenium 使用htmlunit 执行测试。非图形界面浏览器。

    其实就是换个浏览器,只是这个浏览器没有图形界面而已. browser = webdriver.Chrome() 换成 browser = webdriver.Remote(desired_capabi ...

  2. Visual Stdio Code编辑Mark Down

    Visual Studio Code可以一边写Markdown一边预览了,而且不需要任何插件. 方法如下: 新建一个文件,以 .md 为后缀: Visual Studio Code 原生就支持高亮Ma ...

  3. 5.azkaban权限管理

    权限简介 user 登录azkaban的用户 注意,如果不给用户roles groups,则用户就是普通用户,只能创建\查看\执行\调度自己的任务,不能看别人的 group group:用户的集合,给 ...

  4. HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)

    其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...

  5. Python中的list

    list的创建 1 字面量 >>>L = [1, 2, 3] [1, 2, 3] 2 通过iterable可迭代对象,比如str对象,range对象,map对象 >>&g ...

  6. 自测之Lesson14:多线程编程

    题目:创建一个线程,并理清主线程结束时会发生什么. 代码: #include <stdio.h> #include <pthread.h> #include <unist ...

  7. 并查集(Union/Find)模板及详解

    概念: 并查集是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题.一些常见的用途有求连通子图.求最小生成树的Kruskal 算法和求最近公共祖先等. 操作: 并查集的基本操作有两个 ...

  8. 算法与数据结构3.3 calculator

    ★实验任务 小 V 发明了一个神奇的整数计算器: 给定一个合法的表达式,这个计算器能求出这个表达式的最终答案. 表达式可能包含: +:运算符,整数加法.如 1+1=2 -:运算符,整数减法.如 1-1 ...

  9. holoeverywhere修改actionbar背景

    <style name="Holo.Theme.Light.MyActionBar" parent="Holo.Base.Theme.Light.DarkActio ...

  10. cacti 安装perl 和XML::Simple

    一.安装perl #tar zxvf perl-5.20.1.tar.gz #cd perl-5.20.1  #./Configure -de  #make  #make test  #make in ...