当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied

论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》

Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)

Servo System of Hydraulic Steering Gear Based on Fuzzy PID

esp32系列(5):esp32 蓝牙架构学习

顺通海关查验预约综合管理系统

Summary of String Copy, Concatenation, Comparison and Split Functions (1)

Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法

Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法

分账系统二清解决方案如何助力电商平台合规经营?
随机推荐
游戏化产品搭建思路的拆解与探究
分账系统二清解决方案如何助力电商平台合规经营?
Microsoft Office 2019 software download and installation detailed tutorial!
【网络工程】A、B、C、D、E类IP地址划分依据和特殊的IP地址
C陷阱与缺陷 第6章 预处理器
Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied
MySQL中的存储过程(详细篇)
X射线的应用是什么?
华为无线设备配置Mesh业务
js中的基础知识点 —— BOM
首发!阿里技术大牛最新耗时半个月整理出最全MySQL性能优化和高可用架构技术宝典,直接封神!
什么是工业射线照相设备?
强烈推荐APP破解常用工具集合!
从零开始的Multi-armed Bandit
esp32系列(5):esp32 蓝牙架构学习
基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
JMeter Notes 3 | JMeter Installation and Environment Instructions
全球架构师峰会
Error occurred while trying to proxy request The project suddenly can't get up
Research on intelligent charging strategy of matlab simulink lithium-ion battery