题意:
给出两个数p和q。
求最大的x, 使得p%x==0 && q %x !=0
思路:
如果p%q!=0, x就是p。
如果p % q ==0。那么q是p的因子,q的因子都是p的因子。q可以表示为, ci是质因数,p也一样。
x只需要有一个质因子和q不同就行了。
求最大的x,就先让x等于p, 然后一直去除以q的质因子,直到x不能整除q,取最大值。
#pragma warning(disable:4996)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 505;
const ll mod = 10000;
ll p, q;
int main()
{
ll n, x, t;
scanf("%lld", &n);
while (n--)
{
scanf("%lld%lld", &p, &q);
if (p %q != 0)printf("%lld\n", p);
else
{
t = q;
x = 0;
for (ll i = 2; i*i <= t; i++)
{
if (t%i)continue;//不能整除
ll now = p;
while (now %q == 0)now /= i;
while (t%i == 0)t /= i;
x = max(x, now);
}
if (t > 1)//如果t是质数的情况
{
ll now = p;
while (now %q == 0)now /= t;
x = max(x, now);
}
printf("%lld\n", x);
}
}
return 0;
}