[NOIP2009]Hankson的趣味题

题目链接

这是一道有关公约数、公倍数的问题。

由题可得:①gcd(x,a0)=a1 ;②lcm(x,bo)=b1  又有公式:gcd(x,y)*lcm(x,y)=x*y 代入得 b0*x=b1*gcd(x,b0)  移项得 x=b1/b0*gcd(x,b0)

从这里开始就只需要检验等式是否成立,令 i=gcd(x,b0)(1≤i≤sqrt(b0)),x=b1/b0*  i或b0/i,这里稍微注意一下完全平方数的处理,就可以完美的解决问题了。

#include<cmath>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int read(){
    int res=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
int n,x,a0,a1,b0,b1,ans,k;
int main(){
    n=read();
    for(int i=1;i<=n;++i){
        a0=read();a1=read();b0=read();b1=read();ans=0;
        if(b1%b0){cout<<0<<endl;continue;}
        for(int j=1;j*j<b0;++j){
            if(b0%j==0){
                x=b1/b0*j;
                if(gcd(x,b0)==j&&gcd(x,a0)==a1)ans++;
                x=b1/b0*(b0/j);
                if(gcd(x,b0)==b0/j&&gcd(x,a0)==a1)ans++;
            }
        }
        k=int(sqrt(b0));
        if(k*k==b0){
            x=b1/b0*k;
            if(gcd(x,b0)==k&&gcd(x,a0)==a1)ans++;
        }
        cout<<ans<<endl;
    }
return 0;
}
原文地址:https://www.cnblogs.com/clockwhite/p/10658193.html