POJ2429 GCD & LCM Inverse pollard_rho大整数分解-好题

保姆级教程:在树莓派4B上用V4L2驱动USB摄像头(附完整代码) 本文提供树莓派4B上使用V4L2驱动USB摄像头的完整教程,涵盖环境配置、V4L2架构解析、代码实现及问题排查。通过详细步骤和完整代码示例,帮助开发者快速掌握嵌入式Linux下的摄像头驱动开发,解决实际应用中的兼容性和性能问题。 阅读详情

题意:
给出两个数的最大公约数和最小公倍数,找出这两个数,并使它们的和最小。
思路:
设gcd = a1a2…an,lcm = a1a2*…ank;则lcm/gcd = k;现在题目转化为:求两个互质的数a,b满足a*b =k,且a+b最小。
这里首先想到质因数分解,但是平时的埃式筛选法还有欧拉筛选法都不行,空间太大。
这里要用到一种很巧妙的随机算法-Pollard_rho(大整数质因数分解)还有Miller_rabin随机算法(判断一个大整数是否是质数)。
两者算法的具体解析请看传送门。
大整数质因数分解Pollard_rho算法传送门
大整数素数判定Miller_rabin算法传送门

AC code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
//#define INF 0x3f3f3f//使用这个INF会WA 
using namespace std;
typedef long long LL;
const LL INF=(LL)1<<61;
const int maxn = 2e5;
const int times = 100;
map<LL,int> mp;
int cnt = 0;
LL gc = 0,lc = 0,c = 234237,k = 0;
LL minv = INF;
LL resa = 0,resb = 0;
LL gcd(LL a,LL b){
	return b == 0 ? a : gcd(b,a%b);
}

LL q_mul(LL a,LL b,LL mod){
	LL res = 0;
	while(b > 0){
		if(b & 1){
			res = (res + a) % mod;
		}
		a = (a + a) % mod;
		b >>= 1;
	}
	return res;
}

LL q_pow(LL a,LL b,LL mod){
	LL res = 1;
	while(b > 0){
		if(b & 1){
			res = q_mul(res,a,mod);
		}
		a = q_mul(a,a,mod);
		b >>= 1;
	}
	return res;
}

bool witness(LL a,LL n){
	LL temp = n - 1;
	int j = 0;
	while(temp % 2 == 0){
		temp /= 2;
		j++;
	}
	LL x = q_pow(a,temp,n);
	if(x == n-1 || x == 1){
		return true;
	}
	while(j--){
		x = q_mul(x,x,n);
		if(x == n-1){
			return true;
		}
	}
	return false;
}
bool Miller_rabin(LL n){
	if(n == 2){
		return true;
	}
	if(n < 2 || (n % 2 == 0  && n != 2)){
		return false;
	}
	for(int i = 0;i < times;++i){
		LL r = rand() % (n - 2) + 1;//n-1和n-2都可以 
		if(witness(r,n) == false){
			return false;
		}
 	}
	return true;
}

LL pollard_rho(LL n,LL c){
	LL i = 1,k = 2;
	LL x = rand() % (n-1) + 1;
	LL y = x;
	while(true){
		i++;
		x = (q_mul(x,x,n) + c) % n;
		LL d = gcd(y - x,n);
		if(1 < d && d < n){
			return d;
		}
		if(y == x){
			return n;
		}
		if(i == k){
			y = x;
			k <<= 1;
		}
	}
}
void find(LL n,LL c){
	if(n == 1){
		return ;
	}
	if(Miller_rabin(n) == true){
		mp[n]++;
		return ;
	}
	LL p = n;
	while(p >= n){
		p = pollard_rho(p,c--);
	}
	find(p,c);find(n/p,c);
}
void dfs(int depth,LL a){
	if(depth == cnt){
		LL b = k / a;
		if(gcd(a,b) == 1){
			if(a + b < minv){
				minv = a + b;
				resa = a;resb = b;
			}
		}
		return ;
	}
	map<LL,int>::iterator it = mp.begin();
	for(int i = 0;i < depth;++i){
		it++;
	}
	//cout << "hah " << it->first << " " << it->second << endl;
	LL temp = q_pow(it->first,it->second,INF);
	//cout << temp << endl;
	if(a * temp < minv){
		dfs(depth + 1,a * temp);
	}
	if(a < minv){
		dfs(depth + 1,a);
	}
}
void solve(LL n,LL c){
	find(k,c);
	cnt = mp.size();
	/*
	map<LL,int>::iterator it = mp.begin();
	for(it = mp.begin();it != mp.end();it++){
		cout << "em " << it->first << " " << it->second << endl;
	}
	*/
	dfs(0,1); 
	if(resa > resb){
		swap(resa,resb);
	}
	printf("%I64d %I64d\n",resa * gc,resb * gc);
}
int main(){

	while(~scanf("%I64d %I64d",&gc,&lc)){
		minv = INF;
		c = 2137342; 
		mp.clear();
		k = lc / gc;
		solve(k,c);
	}
	return 0;
}
【100分】【等和子数组最小和】 逆序排序,这样能够使得在分组时优先选取较大的元素,以便将元素分配到较小的子集中,从而使得每个子集的和相对均衡。给定一个数组nums,将元素分为若干个组,使得每组和相等,求出满足条件的所有分组中,组内元素和的最小值。分成若干个组,使得每组的和相等,并求出满足条件的所有分组中,组内元素和的最小值。在递归的过程中,我们需要注意一个优化点,即避免将相同和的桶重复计算。4 个子集(5),(1,4),(2,3),(2,3)2 个子集(5, 1, 4),(2,3, 2,3)个子集,则返回子集和的最小值,如果不能,则将。 阅读详情

相关推荐

工业金属表面缺陷检测:4种光学成像方案(明/暗/漫/背光)选型与实测对比

本文深入探讨工业金属表面缺陷检测中的4种光学成像方案(明/暗/漫/背光),详细解析其物理原理、适用场景及实测效果。通过对比不同照明方案在铝合金轮毂、不锈钢餐具等实际工业场景中的表现,提供科学的选型决策树,帮助提升缺陷检出率至99%以上,为计算机视觉在工业质检领域的应用提供实用指南。

weixin_33624741的博客 394

【POJ 2429】大数分解+二进制枚举

1.题目链接。题目大意:给出两个数的gcd和lcm,求这两个数和最小是多少? 2.分析:。那么我们可以简单的推导一下: 这里n是一个整数,并且: 说明n一定是由两个互质的数乘起来的。那么我们枚举n的因子,然后找一下最小...

YG爱木木 471

MATLAB 设置legend加标题

x1 = linspace(0,5); y1 = sin(x1/2); plot(x1,y1) hold on x2 = [0 1 2 3 4 5]; y2 = [0.2 0.3 0.6 1 0.7 0.6]; scatter(x2,y2,'filled') hold off legend({'sin(x/2)','2016'},'Location','northwest','Orientation','horizontal')%水平 legend添加标题 lgd = legend; lgd.Fon

早起CaiCai的学习空间 4724

POJ——2429(数论之大整数分解)

题目地址:http://poj.org/problem?id=2429 解析:题目的关键就是把lcm/gcd的大数分解成质因子之积。(可以用容斥原理求出所有的情况,然后选出来a+b最小的情况。) #include #include #include #include #include #include #include #include using namespace std;

luoshenzhisi 1130

STM32CubeMX+FreeRTOS+CAN总线+传感器实战:多任务系统与通信融合开发详解

本文详细介绍了基于STM32CubeMX和FreeRTOS的CAN总线与光电传感器融合开发实战。通过多任务系统设计,实现传感器数据采集与CAN通信的协同工作,提供完整的配置例程和调试技巧,适用于工业控制与机器人感知等应用场景。

n2o3p4的博客 739

【无标题】EXCEL实现刷题

运用Excel开发的刷题工具,具有普遍实用性,可将WORD的试题自动导入并实现刷题。

weixin_44081794的博客 2982

POJ 2429 Miller-rabin素数判定 + pollard-rho质因子分解 + 埃氏筛法

题目不能说是很难,只是用到了许多数学上的知识(费马小定理,miller-radin,pollard-rho),还有一些算法上的知识DFS,辗转相除。我也很菜,一个周末的时间都用在这个题目上了,但写了很多很多的注释,花费了大量的篇幅,浅谈了我对这些算法的拙见,希望能够帮助大家!

qq_53038151的博客 257

PCI-Express、MiniPCIe、ExpressCard接口转换说明/引脚互换关系

通过引脚PIN互换可以在PCI-Express、MiniPCIe、ExpressCard之间切换不同的接口;提供接口引脚表和标准尺寸图。

tech34的博客 7134

poj 2429

因子分解很简单 直接套用模版即可。。然后之后的处理,这里一个讲解不错。。。。http://blog.sina.com.cn/s/blog_69c3f0410100uac0.html  可以参考一下的。。   Problem Description Given two positive integers a and b, we can easily calculate the greatest...

qq_40859951的博客 614

【WorkBuddy专栏81】自己搭个智能客服,打通微信客服和视频号私信——WorkBuddy 企业 Agent 实战

【WorkBuddy专栏81】自己搭个智能客服,打通微信客服和视频号私信——WorkBuddy 企业 Agent 实战

AI人人懂 288

【文献笔记】CVPR 2023|Attention-Based Point Cloud Edge Sampling

优点能够有效采出点云数据的边缘点分类任务性能优越不足没有设计出一个更好的上采样方法,导致分割任务的性能略低只考虑了边缘特征,没有利用其他几何特征。

m0_55908255的博客 324

POJ2429 GCD & LCM Inverse pollard_rho大整数分解

题目链接:http://poj.org/problem?id=2429 题目大意:给出两个数的最大公约数和最小公倍数,让你找出满足条件的两个数,使他们的和最小。 分析:我们知道,对于两个数a,b和他们的最大公约数gcd以及最小公倍数lcm,有lcm=a*b/gcd,进一步变形可以得到:(a/gcd * b/gcd)*gcd=lcm,即(a/gcd * b/gcd)=lc

AC_Gibson的专栏 1284

【教程4>第2章>第19节】 GMSK解调系统的FPGA开发与matlab对比验证

MSK(Minimum Shift Keying,最小频移键控)和GMSK(Gaussian Minimum Shift Keying,高斯最小频移键控)都是数字调制技术,主要用于无线通信系统中将数字信号转换为模拟信号进行传输。它们都是连续相位移键控(Continuous Phase Shift Keying, CPK)的一种形式,意味着相邻码元之间没有相位跳变的突变,而是连续过渡,这有助于减少信号带宽和降低邻道干扰。GMSK的解调过程与MSK类似,我们这里还是采用和MSK解调一样的1bit解调方法。

FPGA/MATLAB学习教程/源码/项目合作开发 1227

POJ 2429 GCD & LCM Inverse (大整数素性测试与因式分解)

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10429   Accepted: 1917 Description Given two positive integers a and b, we can easily cal

TaoSama's Microcosm 956

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

GCD & LCM Inverse poj2429 大整数分解(java实现)

问题描述 给两个数的gcd和lcm,反求最小的两个数。 问题分析 假设a、b的gcd为g,lcm为l,有a = m*g,b = n*g, n*m = l/g,于是问题变为求n和m。由整数分解的唯一性可以得到l/g可以分解为若干个素数的幂方积,即p1^a1 * p2^a2...* pn^an。由于n和m互质所以,就是将p1、p2...pn成两部分使其和最小。 注意点:题目中是多case,需要...

Xp545945的博客 321

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

GCD and LCM Aizu - 0005(辗转相除)+GCD & LCM Inverse POJ - 2429(java或【Miller Rabin素数測试】+【Pollar Rho整数分解】)

题目:GCD and LCM Aizu - 0005 Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b. Input Input consists of several data sets. Each data set contains a and b separated by a single space in a lin

zeng_jun_yv的博客 289

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

poj2429 GCD & LCM Inverse(pollard_rho大整数分解)

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

qq_41648012的博客 267

【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)

本题涉及的算法个人无法完全理解,在此提供两个比较好的参考。 原理 代码实现 个人改编的AC代码: #include &lt;algorithm&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #in...

weixin_30420305的博客 147

poj 2429 GCD & LCM Inverse(Miller_rabin 测试+pollard_rho大数分解)

题目链接GCD & LCM Inverse 分析题目本身是不难的,直接对 lcmgcd\frac{lcm}{gcd}进行素因子分解,找到,lcmgcd\frac{lcm}{gcd}的两个因子 x,yx,y使得 x+yx+y最小,但是由于范围是2632^{63}次方,有点大,一般的因子分解法会超时,需用Miller_Rabin 强伪素数测试,与Pollard_rho方法分解因子,具体做法就是,将lc

taotao 的大学墓志 645

POJ 2429 GCD & LCM Inverse(Pollard-rho 大整数分解+DFS)

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16795   Accepted: 3107 Description Given two positive integers a and b, we can easily calcu

信仰.的博客 650

POJ 2429 GCD & LCM Inverse(大整数素因子分解+二进制枚举)

GCD & LCM InverseTime Limit: 2000MSMemory Limit: 65536KTotal Submissions: 16540Accepted: 3061DescriptionGiven two positive integers a and b, we can easily calculate the greatest common divisor (GCD) an

代码改变世界 1518

POJ 2429 GCD & LCM Inverse

题目链接:GCD & LCM Inverse 解题思路:这个给了

CrazySona的专栏 588

GCD & LCM Inverse POJ - 2429(Pollard rho整数分解+dfs)

pollard rho整数分解

Grady_Ne的博客 525

POJ 2429 GCD & LCM Inverse ★(pollard-ρ && DFS枚举)

题目链接:http://poj.org/problem?id=2429 题目大意:给定gcd(a,b)和lcm(a,b)(&lt;2^63),求a和b,如果有多种情况,输出和最小的情况. 首先gcd(a,b) * lcm(a,b) = a*b,但是如果我们直接从a*b中分解因子的话,a*b是可能超过long long的,这样就不好处理了. 我们可以先把gcd(a,b)都分给a,b,因...

weixin_30650039的博客 71

poj 2429 GCD & LCM Inverse

一个对Miller_rabin与pallord的一个运用; View Code #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstdlib&gt; #include&lt;algorithm&gt; #include&lt;cmath&gt; #include&lt;queue&gt; #includ...

weixin_34331102的博客 112
上一篇: Pollard_rho随机算法+Miller_rabin算法-大整数质因数分解
下一篇: Dead Fraction-POJ1930-辗转相除法-小数化为分数的技巧
Mavis code
博客等级 码龄9年 13粉丝 147原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值