当前位置:网站首页>信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
2022-07-30 17:33:00 【君义_noip】
【题目链接】
ybt 1915:【01NOIP普及组】最大公约数与最小公倍数
洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
【题目考点】
1. 最大公约数与最小公倍数
- 求最大公约数的方法:见信息学奥赛一本通 1207:求最大公约数问题 | OpenJudge 2.2 7592:求最大公约数问题
- 最大公约数与最小公倍数的关系:两数乘积为这两数最大公约数与最小公倍数的乘积
【解题思路】
已知两个正整数x,y。x是p,q两个数的最大公约数,y是p,q两个数的最小公倍数。
因为两数乘积是最大公约数与最小公倍数的乘积,所以有: x y = p q xy = pq xy=pq
p,q这两个数字,一定都大于等于最大公约数x,小于等于最小公倍数y。
从x到y枚举p,通过 q = x y / p q = xy/p q=xy/p得到q。
求出p,q的最大公约数,看最大公约数的值是否等于x。如果是,那么这一组p, q是满足条件的,做计数。否则不满足条件。
最后输出满足条件的p, q的个数。
【题解代码】
解法1:使用迭代方法求最大公约数
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)//求a, b的最大公约数。注意必须满足a >= b
{
int r;
while(b > 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
int main()
{
int x, y, p, q, ct = 0, x1;
cin >> x >> y;
for(p = x; p <= y; ++p)
{
if(x*y%p == 0)
{
q = x*y/p;
x1 = gcd(p, q);//求出p, q的最大公约数x1
if(x1 == x)//如果与x相同,那么这一组p, q满足条件
ct++;
}
}
cout << ct;
return 0;
}
解法2:使用递归方法求最大公约数
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)//求a, b的最大公约数。注意必须满足a >= b
{
if(b == 0)
return a;
return gcd(b, a%b);
}
int main()
{
int x, y, p, q, ct = 0;
cin >> x >> y;
for(p = x; p <= y; ++p)
if(x*y%p == 0 && gcd(p, x*y/p) == x)
ct++;
cout << ct;
return 0;
}
边栏推荐
- 17.机器学习系统的设计
- Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
- 一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
- FastJson反序列化漏洞(复现)
- PyTorch 猫狗分类源代码及数据集
- Mongoose module
- 优酷视频元素内容召回系统:多级多模态引擎探索
- Metaverse Web 3.0 和 DeFi大师班
- SLIM: Sparse Linear Methods (TopN推荐)
- LeetCode318: Maximum product of word lengths
猜你喜欢
论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
测试.net文字转语音模块System.Speech
un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
论文阅读之《Quasi-Unsupervised Color Constancy 》
浅谈在线编辑器中增量编译技术的应用
WeChat applet picker scroll selector use detailed explanation
FastJson反序列化漏洞(复现)
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
Mongoose模块
SQLServer下载与安装
随机推荐
论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》
Tensorflow中实现正则化
Promise入门到精通(1.5w字详解)
浅谈在线编辑器中增量编译技术的应用
中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
C陷阱与缺陷 第6章 预处理器 6.1 不能忽视宏定义中的空格
JMeter笔记4 | JMeter界面介绍
强烈推荐APP破解常用工具集合!
C# 连接SQL Sever 数据库与数据查询实例 数据仓库
知识蒸馏1:基础原理讲解及yolov5项目实战介绍
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
字符串复制、拼接、比较以及分割函数总结(一)
从零开始的Multi-armed Bandit
How Google earth engine realizes the arrangement and selection of our time list
知识蒸馏4:准备数据集并修改网络配置
每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
LeetCode167:有序数组两数之和
数据库系统原理与应用教程(069)—— MySQL 练习题:操作题 95-100(十三):分组查询与聚合函数的使用
阿里巴巴CAN:Embedding前置的特征交互新思路
测试行业干了5年,从只会点点点到了现在的测试开发,总算是证明了自己