跳到正文

题解

数论好题

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

文章信息

收录
2026年8月14日
更新
2026年8月14日
篇幅
约 2 分钟
题目
P8255

版本

  1. v1当前
C++
#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";
			
		}
	}
}
原文