POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)
原题链接:http://poj.org/problem?id=2429
GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17639 Accepted: 3237
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
解题心得:
- 题意就是给你LCM和GCD,要求你输出两个数a和b,a和b要满足题目所给的LCM和GCD同时要使a+b最小。
- 之前做了给你a和b叫你输出LCM和GCD,所以在看到这个题的时候以为难度不大,但是由于数据量真的太大,所以去网上看了一下大神们的代码,结果就花费了两天的时间去学习前备知识Millar-Rabin强伪素数判断和Pollard-rho素数分解,然后再慢慢码代码。
- 关于Millar-Rabin是个什么,可以去看一下这篇文章《素数和素性判断》,上面说得十分清楚,然后看完这个前备知识之后剩下的就很好解决了,还有就是感觉有一点点玄学的Pollard-rho素数分解。
- 然后就可以看这个题的思路了,给你的是LCM和GCD,可以设得到的答案为a和b,可以肯定的是(a/gcd)*(b/gcd)= lcm/gcd。那么(lcm/gcd)就可以看作两个互素数的乘积(a/gcd和b/gcd一定是两个互素的数),这个时候lcm和gcd都是知道的,就可以用lcm/gcd,假设c=lcm/gcd,那么就可以将c分解成多个素数的乘积,既然素数的乘积,那么在分解的时候必须要判断当前数是不是素数,使用素筛范围不可能太大,就要用到millar-rabin判断素数,如果是素数就停止分解,如果不是,首先可以筛选的素数范围内得到就直接得到一些素数因子,否则使用Pollard-rho算法分解出因子,然后递归,最后找到所有的素数因子之后再选择,这就只能枚举,可以使用状压。
- 大概思路就是这样的,实在不是很明白的可以先看看代码,了解整体结构。但是还有需要注意的地方就是数据范围太大,不可以两个数直接相乘,需要用快数乘法,化乘法为加法,原理和快速幂一样,这样一边加一边mod才不会超过long long范围。
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <cstring>
#include <map>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 2e5;
bool prim[maxn];
vector <ll> prim_num;
ll __gcd(ll n,ll m) {
if(m == 0)
return n;
return __gcd(m,n%m);
}
ll mod_mult(ll x,ll q,ll mod) {//快速乘法
ll res = 0;
while(q) {
if(q&1) {
res = res + x;
if(res > mod) res -= mod;
}
x <<= 1;
if(x > mod) x -= mod;
q >>= 1;
}
return res;
}
ll mod_exp(ll x,ll q,ll mod) {//快速幂
ll res = 1;
while(q) {
if(q&1)
res = mod_mult(res, x, mod);//注意不能直接相乘
x = mod_mult(x,x,mod);
q >>= 1;
}
return res;
}
bool millar_rabin(ll n) {
ll q = n-1;
if(n < 2 || !(n&1))
return false;
if(n == 2)
return true;
ll k = 0;
while(q%2 == 0) {
k++;
q >>= 1;
}
for(int i=0;i<20;i++) {//这里重复判断20次,将错误的机率减小
ll temp = rand()%(n-1) + 1;
ll x = mod_exp(temp,q,n);
if(x == 1)
continue;
bool found = false;
for(int j=0;j<k;j++){
if(x == n-1) {
found = true;
break;
}
x = mod_mult(x,x,n);
}
if(found)
continue;
return false;
}
return true;
}
void get_prim() {//素数筛选
prim[1] = prim[0] = true;
for(int i=2;i<maxn;i++) {
if(prim[i])
continue;
prim_num.push_back(i);
for(int j=i*2;j<maxn;j+=i)
prim[j] = true;
}
}
bool is_prim(ll x) {//判断是不是素数
if(x < maxn)
return !prim[x];
return millar_rabin(x);
}
ll pollard_rho(ll n,ll c) {
ll x = 2;
ll y = 2;
ll d = 1;
while(d == 1 || d == n) {//分解到为1和n的因子则继续分解
x = mod_mult(x,x,n) + c;
y = mod_mult(y,y,n) + c;
y = mod_mult(y,y,n) + c;
d = __gcd(n,x>y?x-y:y-x);
}
return d;
}
void factorize(map<ll,ll>& maps,ll n) {
if(is_prim(n))//如果n已经是素数了,那么就不用分解了
maps[n]++;
else {
for(int i=0;i<prim_num.size();i++) {//可以在筛选出来的素数中找到的直接找
while(n%prim_num[i] == 0) {
maps[prim_num[i]]++;
n /= prim_num[i];
}
}
if(n != 1) {
if(is_prim(n))//如果在前面已经分解完了那就结束,不然继续分解超过筛选出素数范围
maps[n]++;
else {
ll d = pollard_rho(n,1);//如果分解出一个因子是d,那么另一个因子一定是n/d
factorize(maps, d);//递归下去
factorize(maps,n/d);
}
}
}
}
pair <ll,ll> Solve(ll n,ll m) {
ll c = m/n;
map <ll,ll> maps;//用于储存分解出来的素数因子
factorize(maps,c);//分解c
vector <ll> mult_factors;
map <ll,ll> :: iterator iter1;
for(iter1=maps.begin();iter1!=maps.end();iter1++) {
ll mul = 1;
while(iter1->second){
mul *= iter1->first;
iter1->second--;
}
mult_factors.push_back(mul);
}
ll Max = 1e18,ans_x = 1,ans_y = c;
for(ll i=0;i<(1 << mult_factors.size());i++) {//枚举所有可能的因子,找到最小的那一个,总共有2的n次方种可能性,n是分解出来的因子数,然后用状压,枚举
ll x = 1;
for(ll j=0;j<mult_factors.size();j++) {
if(i & (1 << j))
x *= mult_factors[j];
}
ll y = c/x;//如果一个因子为x,则另一个必定为c/x
if(x < y && x + y < Max) {
Max = x + y;
ans_x = x;
ans_y = y;
}
}
pair<ll,ll> p;
p.first = ans_x*n;//记得要乘上之前除去的GCD
p.second = ans_y*n;
return p;
}
int main() {
ll gcd,lcm;
get_prim();
while(scanf("%lld%lld",&gcd,&lcm) != EOF) {
pair<ll,ll> ans = Solve(gcd,lcm);
printf("%lld %lld\n",ans.first,ans.second);
}
return 0;
}
POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)的更多相关文章
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...
- 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 ...
- POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)
x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小. x的范围是int64的,所以要用Pollard_rho ...
- poj 2429 GCD & LCM Inverse 【java】+【数学】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9928 Accepted: ...
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- 【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)
本题涉及的算法个人无法完全理解,在此提供两个比较好的参考. 原理 (后来又看了一下,其实这篇文章问题还是有的……有时间再搜集一下资料) 代码实现 #include <algorithm> ...
- poj2429 GCD & LCM Inverse
用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...
随机推荐
- Python元组类型、字典类型及常用操作
一.元组类型 1.用途 记录多个值,当多个值没有改的需求,此时用元组更合适,Python的元组与列表类似,不同之处在于元组的元素不能修改. 2.定义方式 在()内用逗号分隔开多个任意类型的值 t=(1 ...
- .net 使用Memcached
1.创建个人MemcachedHelper类 /// <summary> /// 页 面 名:缓存管理类<br/> /// 说 明:设置.获取.移除Cache<br/&g ...
- python列表生成式、列表推导式
运用列表生成式,可以快速生成list,可以通过一个list推导出另一个list,而代码却十分简洁. 格式 [x for x in 内容] [x for x in 内容 if 条件] 1:要把生成的元素 ...
- ASP.NET设置母版页
母版页允许开发人员创建具有指定的可编辑区域的站点级模板.随后,此模板可应用到网站中的 ASP.NET 页面上.这些 ASP.NET 页面只需为母版页中指定的可编辑区域提供相应内容 – 在使用母版页的所 ...
- word禁止自动编号
在回车.换行时使用 shift + enter
- C++ new new[]详解
精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...
- java ——String , StringBuffer, StringBuilder类
一.String类概述 1.String对象一旦创建就不能改变. 2.字符串常量池. 字符串常量池的特点:池中有则直接使用,池中没有则创建新的字符串常量. 例1: “==” 比较两个对象是否引用同一 ...
- 使用函数BAPISDORDER_GETDETAILEDLIST读取S/4HANA中Sales Order行项目数据
事务码MM03查看物料主数据,如下图所示的行项目数据,包含物料ID,描述信息,数量,单价等等: 使用如下代码进行行项目读取: DATA: ls_read TYPE order_view, lt_ite ...
- *205. Isomorphic Strings (string & map idea)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- chromedp下载文件的方法,备忘一下。
sect := `//a[@href="v/443.json"]` wd,_ := os.Getwd() fmt.Println(wd) return chromedp.Tasks ...