当前位置:网站首页>Informatics Olympiad 1915: [01NOIP Popularization Group] Greatest Common Divisor and Least Common Multiple | Luogu P1029 [NOIP2001 Popularization Group] The problem of the greatest common divisor and
Informatics Olympiad 1915: [01NOIP Popularization Group] Greatest Common Divisor and Least Common Multiple | Luogu P1029 [NOIP2001 Popularization Group] The problem of the greatest common divisor and
2022-07-30 17:42:00 【Your righteousness _noip】
【题目链接】
ybt 1915:【01NOIP普及组】最大公约数与最小公倍数
洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
【题目考点】
1. 最大公约数与最小公倍数
- 求最大公约数的方法:见信息学奥赛一本通 1207:求最大公约数问题 | OpenJudge 2.2 7592:求最大公约数问题
- 最大公约数与最小公倍数的关系:The product of two numbers is the product of the greatest common divisor and the least common multiple of the two numbers
【解题思路】
已知两个正整数x,y.x是p,q两个数的最大公约数,y是p,q两个数的最小公倍数.
Because the product of two numbers is the product of the greatest common divisor and the least common multiple,所以有: x y = p q xy = pq xy=pq
p,q这两个数字,must be greater than or equal to the greatest common divisorx,Less than or equal to the least common multipley.
从x到y枚举p,通过 q = x y / p q = xy/p q=xy/p得到q.
求出p,q的最大公约数,See if the value of the greatest common divisor is equalx.如果是,那么这一组p, q是满足条件的,做计数.否则不满足条件.
The final output that satisfies the conditionsp, q的个数.
【题解代码】
解法1:Use the iterative method to find the greatest common divisor
#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:Use recursive methods to find the greatest common divisor
#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;
}
边栏推荐
猜你喜欢
随机推荐
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
首发!阿里技术大牛最新耗时半个月整理出最全MySQL性能优化和高可用架构技术宝典,直接封神!
un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
Express framework connects MySQL and ORM framework
Promise入门到精通(1.5w字详解)
[HarekazeCTF2019] Avatar Uploader 1
数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
C陷阱与缺陷 第7章 可移植性缺陷 7.4 字符是有符号数还是无符号数
升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
论文阅读之《Quasi-Unsupervised Color Constancy 》
基于stm32的shell实现
windwons 下GPU环境和pytorch安装
基于MATLAB的电力系统短路故障分析与仿真
基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
向量检索基础方法总结
Servo System of Hydraulic Steering Gear Based on Fuzzy PID
浅谈在线编辑器中增量编译技术的应用
Microsoft Office 2019 软件下载安装详细教程!
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
JVM诊断命令jcmd介绍







![有效的括号字符串[贪心练习]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)

