跳到正文

题解

数学游戏

pcl5约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月13日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P8255

版本

  1. v1当前

计算过程

有 x×y×gcd⁡(x,y)=zx \times y \times \gcd (x,y) = z
不妨令 x=a×q,y=a×p,gcd⁡(p,q)=1x = a \times q,y = a \times p, \gcd (p,q) = 1
原式 =a3×p×q=z= a ^ 3 \times p \times q = z
注意到 y=a2×pa=z÷xay = a^2 \times \frac p a = \frac {z \div x} a
又注意到 gcd⁡(zx,x2)=gcd⁡(a2×p,a2×q2)\gcd (\frac z x,x ^ 2) = \gcd (a^2 \times p,a^2 \times q ^ 2)
因为 gcd⁡(p,q)=1\gcd (p,q) = 1
所以 gcd⁡(a2×p,a2×q2)=a2\gcd (a^2 \times p,a^2 \times q ^ 2) = a ^ 2
所以 y=(z/x)/a=(z/x)/sqrt(gcd⁡(z/x,x2))y = (z / x) / a = (z / x) / sqrt( \gcd (z / x,x ^ 2))
当 z mod x≠0z \bmod x \ne 0, gcd⁡(zx,x2)\gcd (\frac z x,x ^ 2) 不为完全平方数时不成立

Code

C++
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int t,x,z;
signed main()
{
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld",&x,&z);
		if(z % x != 0)cout << -1;
		else
		{
			int t = z / x;
			int a = sqrt(__gcd(t,x * x));
			if(a * a != __gcd(t,x * x))
			{
				puts("-1");
				continue;
			}
			cout << t/a;
		}
	}
	return 0;
}
原文