codevs1012 最大公约数和最小公倍数问题

来源:互联网 发布:高仿mcm淘宝店推荐 编辑:程序博客网 时间:2024/05/16 05:08
题目描述 Description

输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数

条件:  1.P,Q是正整数

2.要求P,Q以x0为最大公约数,以y0为最小公倍数.

试求:满足条件的所有可能的两个正整数的个数.

输入描述 Input Description

二个正整数x0,y0

输出描述 Output Description

满足条件的所有可能的两个正整数的个数

样例输入 Sample Input

3 60

样例输出 Sample Output

4

#include <iostream>#include<algorithm>using namespace std;int judge(int x, int y)//这个函数是判定x,y是否互质,互质返回1,否则返回0{for (int i = 2; i <= min(x, y); ++i){if (x%i == 0 && y%i == 0){return 0;}}return 1;}int main(){int counter = 0;int x, y;//x是最大公约数,y是最小公倍数cin >> x >> y;if (y%x != 0)//如果输入的最小公倍数没有最大公约数这个因子,则程序结束{cout << 0;return 0;}for (int i = 1; i <= y / x; ++i)//因为两个都有x这个因子,所以将x从y里去除{for (int j = i; j <= y / x; ++j)//遍历所有组合{if (i*j == y / x&&judge(i, j))counter = counter + 2;}}cout << counter;cin >> counter;return 0;}



原创粉丝点击