Luogu P8255 数学游戏 题解

Luogu P8255 数学游戏 题解

题目大意

现有若干正整数 x,y,z 满足 z=x×y×gcd(x,y) ,给定 z,x ,求 y 。无解输出 1

I/O

Input

第一行一个整数,表示有 t 对正整数 x,y

接下来 t 行,每行两个正整数 x,z

Output

对于每对数字输出一行 y ,无解输出 1

I/O e.g.

Input#1
1
10 240
Output#1
12
Input#2
3
5 30
4 8
11 11
Output#2
6
-1
1

题解

i) x,y,zx|  z,ii)  x|z x=a×d,y=b×d,gcd(a,b)=1 gcd(x,y)=d z=x×y×gcd(x,y)=a×b×d3 zx=b×d2 gcd(a,b)=1 gcd(a2,b)=1 gcd(x2,zx)=gcd(a2d2,bd2)=d2gcd(a2,b)=d2 gcd(x2,zx) zxgcd(x2,zx)=b×d2d=bd=y

Code

#include<bits/stdc++.h>
using namespace std;
#define GO(u,v,i) for(register int i=u;i<=v;i++)
template<class t>inline t fr(){
    register t num=0,dis=1;
    register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')dis=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){num=(num<<1)+(num<<3)+(ch^48);ch=getchar();}
    return num*dis;
}
template<class t>inline void fw(t num){
    if(num>9)fw(num/10);
    putchar(num%10+'0');
}
template<class t>inline void fw(t num,char ch){
    if(num<0)num=-num,putchar('-');
    fw(num);putchar(ch);
}
typedef long long lld;
template<class t>inline t gcd(t a,t b){return b==0?a:gcd(b,a%b);}
lld x,z;
lld t;

inline bool check(lld num){
    lld ans=(int)sqrt(num);
    return num==ans*ans;
}

signed main(){
    t=fr<lld>();
    GO(1,t,i){
        x=fr<lld>(),z=fr<lld>();
        if(z%x){
            fw(-1,'\n');continue;
        }
        z/=x;
        lld cc=gcd(x*x,z);
        if(check(cc)){
            lld ans=z/(int)sqrt(cc);
            fw(ans,'\n');
        }
        else fw(-1,'\n');
    }
    return 0;
}
posted @   Locked_Fog  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示
主题色彩