数学游戏

· · 题解

计算过程

x \times y \times \gcd (x,y) = z\ 不妨令 x = a \times q,y = a \times p, \gcd (p,q) = 1\ 原式 = a ^ 3 \times p \times q = z\ 注意到 y = a^2 \times \frac p a = \frac {z \div x} a\ 又注意到 \gcd (\frac z x,x ^ 2) = \gcd (a^2 \times p,a^2 \times q ^ 2)\ 因为 \gcd (p,q) = 1\ 所以 \gcd (a^2 \times p,a^2 \times q ^ 2) = a ^ 2\ 所以 y = (z / x) / a = (z / x) / sqrt( \gcd (z / x,x ^ 2))\ 当 z \bmod x \ne 0, \gcd (\frac z x,x ^ 2) 不为完全平方数时不成立

Code

#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;
}