• 【数论】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;
    }
    
  • 相关阅读:
    计算机专业术语中英对照
    PhpStorm如何下载github上的代码到本地
    PDO学习
    Shell中特殊的变量
    Shell中变量的使用
    修改cmd的字体
    Shell的输入输出
    Shell入门第一课
    设计模式--观察者(Observer)
    eclipse中使用git提交代码到github
  • 原文地址:https://www.cnblogs.com/pyyyyyy/p/10827733.html
Copyright © 2020-2023  润新知