【数论】P1029 最大公约数和最小公倍数问题

题目链接

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

思路

如果有两个数a和b,他们的gcd(a,b)和lcm(a,b)的乘积就等于ab。
也就是:
a
b=gcd(a,b)*lcm(a,b)
那么,接下来我们需要关注一下数据范围:2≤x0<=100000,2≤y0<=1000000
如果暴力枚举x0和y0,那么你就咕咕咕了。
然而,y0-x0的值是很小的,我们就会想,如果枚举y0-x0这个区间,会不会方便些呢?当然,很显然的是这个区间就是第一个数a。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#define ll long long int
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int a,b;
int ans=0;
int gcd(int x,int y) { 
	if(x%y==0)return y;
	return gcd(y,x%y);
}
int main() {
	scanf("%d %d",&a,&b);
	for(int i=a; i<=b; i++) { 
		if(a*b%i==0) { 
			int j=0;
			j=a*b/i;
			if(gcd(i,j)==a)ans++;
		}
	}
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/pyyyyyy/p/10827733.html