当前位置:网站首页>[NOIP2001 普及组] 最大公约数和最小公倍数问题
[NOIP2001 普及组] 最大公约数和最小公倍数问题
2022-07-26 03:01:00 【ceshyong】
题目链接[NOIP2001 普及组] 最大公约数和最小公倍数问题 - 洛谷
https://www.luogu.com.cn/problem/P1029
题目描述
输入两个正整数x0,y0,求出满足下列条件的 P, Q的个数:
P,Q 是正整数。
要求 P, Q以x0为最大公约数,以y0为最小公倍数。
试求:满足条件的所有可能的P,Q的个数。
输入
一行两个正整数x0,y0。
输出
一行一个数,表示求出满足条件的P, Q的个数。
样例组
输入
3 60
输出 4题目思路
这是一道水题比较简单的题目。首先输入x0,y0。然后将x0,y0相乘存入一个变量R中。然后判断R是否为完全平方数(为一个数的平方),如果是则答案变量ans--;然后开一重循环从1循环到根号R,判断R是否能被循环变量i整除而且i与R/i的最大公因数是x0(只要求出一个即可),若符合条件则ans+=2;
最后输出ans即可。(注意这些变量都需要开long long)
至于最大公因数,可以直接使用系统函数库里的__gcd,或者专门写一个gcd函数:
#define ll long long
ll gcd(ll a,ll b)//与__gcd等价
{return (b==0?a:gcd(b,a%b));}//三元运算符这道题目的主程序如下:
int getans(int n,int m)
{
int r=n*m,ans=0;
if(n==m) ans--;
for(int i=1;i*i<=r;i++)//这样写比使用sqrt(r)更快
if(n%i==0&&gcd(i,n/i)==m) ans+=2;//这里也可以直接使用__gcd
return ans;
}AC代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long m,n,k,ans;
ll gcd(ll a,ll b)//与__gcd等价
{return (b==0?a:gcd(b,a%b));}
int main()
{
cin>>m>>n;k=n*m;
if(n==m) ans--;
for(ll i=1;i*i<=k;i++)
if(k%i==0&&gcd(i,k/i)==m) ans+=2;//这里也可以直接使用__gcd
cout<<ans;
return 0;
}这道题目就这么多(不开O2还是能过的)。
边栏推荐
- (pc+wap) dream weaving template vegetable and fruit websites
- I hope you can help me with MySQL
- Win11麦克风权限的开启方法
- [SQL] CASE表达式
- 万维网、因特网和互联网的区别
- assert _ Aligns
- Arthas view the source code of the loaded class (JAD)
- Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
- Longest Substring Without Repeating Characters
- AMD64(x86_64)架构abi文档:
猜你喜欢

简单使用 MySQL 索引

Detailed explanation of extended physics informedneural networks paper

Standardize your own debug process

Masscode is an excellent open source code fragment manager

Influence of middle tap change on ZVS oscillation circuit
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

STM32——DMA笔记
![[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)](/img/b8/56c4541602c5a6e787e2455f80febd.png)
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)

MySQL教程:MySQL数据库学习宝典(从入门到精通)

Shardingsphere data slicing
随机推荐
Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
[steering wheel] tool improvement: common shortcut key set of sublime text 4
万维网、因特网和互联网的区别
Extended Physics-InformedNeural Networks论文详解
Golang 中‘...‘的用法
富文本转化为普通文本
The source of everything, the choice of code branching strategy
pbootcms上传缩略图尺寸自动缩小变模糊
How can users create data tables on Web pages and store them in the database
Usage of fuser and lsof
ES6高级-利用构造函数继承父类属性
Pbootcms upload thumbnail size automatically reduces and blurs
What if the test / development programmer gets old? Lingering cruel facts
Parallelloopbody in opencv
Detailed explanation of extended physics informedneural networks paper
基础知识-网络与服务器
Method of manually cloning virtual machine in esxi6.7
(PC+WAP)织梦模板蔬菜水果类网站
[sql] case expression
Study notes of pytorch deep learning practice: convolutional neural network (Advanced)