数论好题

· · 题解


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull t, x, z, y, k,zu;
int gcd(int xx, int yy)
{
    while(yy^=xx^=yy^=xx%=yy);
    return xx;
}
ull is_yinzi[32000];
int main() {
    ios::sync_with_stdio(false);
    freopen("math.in", "r", stdin);
    freopen("math.out", "w", stdout);
    cin >>zu;
    while (zu--) {
        y = 1;
        memset(is_yinzi, 0, sizeof(is_yinzi));
        cin >> x >> z;
        int xx=x;
        k = z / x;
        t = sqrt(x) + 1;
        if(k*x!=z)
        {
            cout<<"-1\n";
            continue;
        }
        for (ull i = 2; i <= t; ++i) {
            while (x % i == 0) {
                x /= i;
                is_yinzi[i]++;
            }
        }

        for (ull i = 2; i <= t; ++i) {
            while (is_yinzi[i]&&(k%(i*i)==0)) {
                is_yinzi[i]--;
                y *= i;
                k/=i;
                xx/=i;
                k/=i;
            }
        }
        if(gcd(xx,k)!=1)
        {
            cout<<"-1\n";
        }
        else
        {
            cout<<y*k<<"\n";

        }
    }
}