NOI Online2022普及组 数学游戏
推导
令\(d=\gcd(x,y),x=pd,y=qd,z=pqd^3\)
\(\because \gcd(p,q)=1\)
\(\therefore \gcd(p^2,q)=1\)
\(\therefore d^2=\gcd(p^2d^2,qd^2)=gcd(x^2,\frac z x)\)
\(\therefore d=\sqrt{\gcd(x^2,\frac zx)}\)
\(\therefore y=\frac z{xd}=\frac{z}{x\sqrt{{\gcd(x^2,\frac zx)}}}\)
code
#include<bits/stdc++.h>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define LL long long
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
int main(){
int T;LL x,z,Gcd,y;
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&x,&z);
y=z/x/(LL)(sqrt(gcd(x*x,z/x)));
if(y*x*gcd(x,y)==z)printf("%lld\n",y);
else puts("-1");
}
return 0;
}

浙公网安备 33010602011771号