当前位置:网站首页>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;
}
边栏推荐
- JMeter笔记4 | JMeter界面介绍
- 宝塔搭建PHP自适应懒人网址导航源码实测
- 基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
- Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
- SQLServer下载与安装
- 数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
- 关于内和调试无法查看ntdll内存的问题
- Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
- 主流的深度学习推理架构有哪些呢?
- fast shell porting
猜你喜欢
随机推荐
编曲软件FL Studio中文版安装教程及切换语言教程
Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
C陷阱与缺陷 第7章 可移植性缺陷 7.4 字符是有符号数还是无符号数
Google earth engine如何实现我们时间列表的排列和选取
592. Fraction Addition and Subtraction
主流的深度学习推理架构有哪些呢?
阿里巴巴CAN:Embedding前置的特征交互新思路
[HarekazeCTF2019] Avatar Uploader 1
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
高级语言垃圾回收思路和如何减少性能影响原理分析
数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
FastJson反序列化漏洞(复现)
LeetCode318: Maximum product of word lengths
PLSQL Developer安装和配置
简易的命令行入门教程
JMeter Notes 4 | JMeter Interface Introduction
今年这情况。。真心推荐专科的工程师升个本!
C# 连接SQL Sever 数据库与数据查询实例 数据仓库
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询







![Valid bracketed strings [greedy exercise]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)

