题目链接:

https://cn.vjudge.net/problem/POJ-2429

题目大意:

给出两个数的gcd和lcm,求原来的这两个数(限定两数之和最小)。

解题思路:

首先,知道gcd和lcm求原来的两个数,需要分解lcm / gcd 。将其分解为互质的两个数。

首先将lcm/gcd质因数分解,要分解出沪互质两个数字,那么这两个数字的gcd=1,也就是没有公共的质因子,所以可以直接枚举这两个数字的质因子,如果一个数要取这个质因子,就把它的指数全部取掉。

质因数分解用大数因式分解来做。

分成两个互质的数字可以用二进制枚举子集,1表示取这个质因数,0表示不取,最终求出最小的和的两个数。

 #include<iostream>
#include<ctime>
#include<algorithm>
#include<map>
#define INF 1000000000000000009
using namespace std;
typedef long long ll;
map<ll, int>m;
const int mod = ;
const int times = ;//测试50次
ll mul(ll a, ll b, ll m)
//求a*b%m
{
ll ans = ;
a %= m;
while(b)
{
if(b & )ans = (ans + a) % m;
b /= ;
a = (a + a) % m;
}
return ans;
}
ll pow(ll a, ll b, ll m)
//a^b % m
{
ll ans = ;
a %= m;
while(b)
{
if(b & )ans = mul(a, ans, m);
b /= ;
a = mul(a, a, m);
}
ans %= m;
return ans;
}
bool Miller_Rabin(ll n, int repeat)//n是测试的大数,repeat是测试重复次数
{
if(n == || n == )return true;//特判
if(n % == || n == )return false;//偶数和1 //将n-1分解成2^s*d
ll d = n - ;
int s = ;
while(!(d & )) ++s, d >>= ;
//srand((unsigned)time(NULL));在最开始调用即可
for(int i = ; i < repeat; i++)//重复repeat次
{
ll a = rand() % (n - ) + ;//取一个随机数,[2,n-1)
ll x = pow(a, d, n);
ll y = ;
for(int j = ; j < s; j++)
{
y = mul(x, x, n);
if(y == && x != && x != (n - ))return false;
x = y;
}
if(y != )return false;//费马小定理
}
return true;
}
ll gcd(ll a, ll b)
{
return b == ? a : gcd(b, a % b);
}
ll pollard_rho(ll n, ll c)//找到n的一个因子
{
ll x = rand() % (n - ) + ;
ll y = x, i = , k = ;
while()
{
i++;
x = (mul(x, x, n) + c) + n;//不断调整x2
ll d = gcd(y - x, n);
if( < d && d < n)
return d;//找到因子
if(y == x)
return n;//找到循环,返回n,重新来
if(i == k)//一个优化
{
y = x;
k <<= ;
}
}
}
void Find(ll n, ll c)
{
if(n == )return;//递归出口 if(Miller_Rabin(n, times))//如果是素数,就加入
{
m[n]++;
return;
} ll p = n;
while(p >= n)
p = pollard_rho(p, c--);//不断找因子,知道找到为止,返回n说明没找到 Find(p, c);
Find(n / p, c);
}
ll pow2(ll a, ll b)
{
ll ans = ;
while(b)
{
if(b & )ans *= a;
b /= ;
a *= a;
}
return ans;
}
int main()
{
ll x, y;
ll a[], b[];
//srand((unsigned)time(NULL));
while(cin >> x >> y)
{
m.clear();
y = y / x;
Find(y, );//这是自己设置的一个数
map<ll, int>::iterator it = m.begin();
for(int i = ; it != m.end(); it++, i++)
{
a[i] = it->first;
b[i] = it->second;
}
ll Max = INF, ansa, ansb;
int t = m.size();
for(int i = ; i < (<<t); i++)
{
ll tot = ;
for(int j = ; j < t; j++)
{
if(i & (<<j))
tot *= pow2(a[j], b[j]);
}
ll cnt = tot + y / tot;
if(cnt < Max)
{
Max = cnt;
ansa = tot;
ansb = y / tot;
}
}
if(ansa > ansb)swap(ansa, ansb);
cout<<ansa*x<<" "<<ansb*x<<endl;
}
return ;
}

POJ-2429 GCD & LCM Inverse---给出gcd和lcm求原来两个数的更多相关文章

  1. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9

    python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...

  2. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

  3. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted:  ...

  4. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  5. 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 ...

  6. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  7. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  8. POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)

    原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...

  9. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

随机推荐

  1. jstl缺包时的报错

    jsp中:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  报错: ...

  2. entity framework 查看自动生成的sql

    public MesDbContext() : base("name=mysql") { Database.Log = new Action<string>(msg = ...

  3. vue 使用Slot 分发内容 学习总结。

    https://cn.vuejs.org/v2/guide/components.html#使用-Slot-分发内容    官方API地址 我对solt的理解是当组件中某一项需要单独定义,那么就应该使 ...

  4. 关于sql优化整理一下

    1.where 子句中可以对字段进行 null 值判断吗?        可以,比如 select id from t where num is null 这样的 sql 也是可以的.但是最好不要给数 ...

  5. python模块之contexlib

    一.上下文管理器 with是python实现上下文管理器的核心关键词.它能够在代码执行前和执行后做一些额外的事情. 最常见的代码恐怕就是文件操作了. with open("file" ...

  6. HttpServletResponse对象(二)之常见应用

    ---恢复内容开始--- 1. 使用HttpServletResponse对象实现文件下载 文件下载功能是web开发中经常使用到的功能,使用HttpServletResponse对象就可以实现文件的下 ...

  7. PHP 八种基本的数据类型

    四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: array (数组) object (对 ...

  8. 学习stylus笔记

    最近在研究v-cli3.0,发现了一种新的预处理器,于是花了一些时间去学习下. 学习网站 基本上这个网站上,讲的已经很详情.我下面把我在学习之中的笔记和觉得自己用的多方法贴出来. 1.缩排 使用缩排和 ...

  9. org.springframework.beans.factory.NoSuchBeanDefinitionException

    1. 问题描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxx ...

  10. [TJOI2007]小朋友

    题面 Luogu Sol 弦图最大独立集 做法见上篇博客 # include <bits/stdc++.h> # define RG register # define IL inline ...