当前位置:网站首页>[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还是能过的)。
边栏推荐
- Games101 review: shading, rendering pipelines
- Swin Transformer【Backbone】
- JVM内存模型解析
- Golang 中‘...‘的用法
- The El table header merges the first four columns into one cell
- Nahamcon CTF 2022 babyrev reverse analysis
- Annotation development
- AMD64 (x86_64) architecture ABI document: medium
- Jenkins' study notes are detailed
- [pure theory] Yolo v4: optimal speed and accuracy of object detection
猜你喜欢

信息系统项目管理师必背核心考点(五十)合同内容约定不明确规定

JVM内存模型解析
![[translation] cloud like internal load balancer for kubernetes?](/img/e5/f003ebed05a94d2936cfef5617f745.jpg)
[translation] cloud like internal load balancer for kubernetes?

Games101 review: rasterization

文件操作(一)——文件简介与文件的打开方式和关闭

多线程编程

C language layered understanding (C language function)

DFS Niuke maze problem
![[pure theory] Yolo v4: optimal speed and accuracy of object detection](/img/1f/f38c3b38feed9e831ad84b4bbf81c0.png)
[pure theory] Yolo v4: optimal speed and accuracy of object detection

Multithreaded programming
随机推荐
Standardize your own debug process
多线程编程
File operation (I) -- File introduction and file opening and closing methods
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)
ES6高级-利用原型对象继承方法
[C language] deeply understand integer lifting and arithmetic conversion
Is it safe to open galaxy securities account by mobile phone?
el-table 表头合并前四列,合并成一个单元格
Case: using kept+haproxy to build a Web Cluster
Parallelloopbody in opencv
Pinia plugin persist, a data persistence plug-in of Pinia
(九)属性自省
After clicking play, the variables in editorwindow will be destroyed inexplicably
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
JS get the time composition array of two time periods
图像识别(七)| 池化层是什么?有什么作用?
How to effectively prevent others from wearing the homepage snapshot of the website
Oxycon 2022 network capture frontier conference is about to open!
How to speed up matrix multiplication
基础知识-网络与服务器