最大公约数和最小公倍数问题(洛谷-P1029)
生活随笔
收集整理的這篇文章主要介紹了
最大公约数和最小公倍数问题(洛谷-P1029)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
輸入?2?個正整數?x0,y0?(2≤x0?<100000,2≤y0?<=1000000)?,求出滿足下列條件的?P,Q?的個數
條件:
- P,Q?是正整數
- 要求?P,Q?以?x0??為最大公約數,以?y0??為最小公倍數.
試求:滿足條件的所有可能的?2?個正整數的個數.
輸入輸出格式
輸入格式:
2 個正整數?x0?,y0?
輸出格式:
1?個數,表示求出滿足條件的?P,Q?的個數
輸入輸出樣例
輸入樣例#1:
3 60
輸出樣例#1:
4
思路:利用 LCM*GCD=x*y 枚舉即可
源代碼
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 50001 #define MOD 1e9+7 #define E 1e-6 #define LL long long using namespace std; int GCD(int a,int b) {return b==0?a:GCD(b,a%b); } int main() {int x,y;cin>>x>>y;int temp=x*y;int cnt=0;for(int i=x;i<=y;i++){if(temp%i==0){int maxx=max(i,(int)(temp/i));int minn=min(i,(int)(temp/i));int gcd=GCD(maxx,minn);if(gcd==x)cnt++;}}cout<<cnt<<endl;return 0; }?
總結
以上是生活随笔為你收集整理的最大公约数和最小公倍数问题(洛谷-P1029)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奇数单增序列(信息学奥赛一本通-T117
- 下一篇: 高精除(信息学奥赛一本通-T1308)