题目链接:

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. 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题

    题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...

  2. UOJ #218. 【UNR #1】火车管理

    Description Solution 实际上添加问题就是一个线段树区间覆盖问题,打标记就好 对于弹栈操作比较难搞,实际上也就是一个历史查询,我们不需要保存栈中的每一个元素,我们通过查找历史状态就可 ...

  3. cs端调用webApi

    public class Httphelper { public static string Post1(string url, string postString) { using (WebClie ...

  4. centos下mongodb安装

    安装说明: 系统环境:Centos-6.5 安装软件:mongodb-linux-x86_64-2.4.9.tgz 下载地址:http://www.mongodb.org/downloads 上传位置 ...

  5. Spring Cloud实战之初级入门(六)— 服务网关zuul

    目录 1.环境介绍 2.api网关服务 2.1 创建工程 2.3 api网关中使用token机制 2.4 测试 2.5 小结 3.一点点重要的事情 1.环境介绍 好了,不知不觉中我们已经来到了最后一篇 ...

  6. App Not Responsing

    参见原文:http://rayleeya.iteye.com/blog/1955657 inputDispatchingTimedOut contentProviderNotResponsing se ...

  7. 前端学习之路之CSS (三)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 创建CSS有三种方法:外部样式表.内部样式表.内联样式.优先级:内联样式>内部样式>外部样式表> ...

  8. jQuery filter() , end()

    1. jquery filter(condition) :  过滤指定对象中符合条件的元素: 2.  jquery  end() : 回到原来的操作对象 3.  example : <body& ...

  9. Python入门-类的成员

    昨天我们简单的认识了一下面向对象,以及和面向过程之间的区别,从而我们知道了类这个东西,今天我们就来详细的了解一下关于类的一些东西. 一.类的成员 首先, 什么是类的成员,很简单, 你能在类中写什么? ...

  10. 移动端h5开发相关内容总结css篇--笔记

    原文参考http://mp.weixin.qq.com/s/Nho2DHj-Y59j2F62vpN9jQ 1.开发移动端,头部必要的配置<meta name="viewport&quo ...