题目:https://codeforces.com/contest/1445/problem/C
题目大意:
给p,q求满足一下条件最大的x
1.p%x==0
2.x%q!=0
题解:分两种情况
1.假如p%q!=0 则x最大为p
2.假如p%q==0,则说明p,q有共同因子,又进一步得出p,q,x有共同因子,而他们的共同因子差别在于因子的幂次方不同,因此只需求得p与q的共同因子比该因子在q中的幂次方少一即满足x%q!=0
,求共同因子采用质因数分解,使用vector记录共同因此和相应的幂次方,最后采用遍历。
#include<iostream>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll inf = 0x3f3f3f3f;
ll quickpower(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1) { ans = ans * x; }
x= x * x;
y >>= 1;
}
return ans;
}
int main()
{
int T;
cin >> T;
while (T--)
{
vector<pair<ll, ll>>vp;
vector<pair<ll, ll>>vq;
ll power_q = 0;
ll power_p = 0;
ll p, q;
cin >> p >> q;
if (p % q != 0) { cout << p << endl; continue; }
for (int i = 2; i <= sqrt(q); i++)
{
if (q % i == 0)
{
power_q = 0;
while (q % i == 0)
{
q /= i;
++power_q;
}
vq.push_back({ i,power_q });
}
}
if (q > 1) { vq.push_back({ q,1 }); }
ll pp = p;
for (int i = 0; i < vq.size(); i++)
{
power_p = 0;
while (pp % vq[i].first == 0)
{
pp /= vq[i].first;
++power_p;
}
vp.push_back({ vq[i].first,power_p });
}
ll min_n = 1e18;
for (int i = 0; i < vp.size(); i++)
{
min_n = min(min_n, quickpower(vp[i].first, vp[i].second - vq[i].second + 1));
}
cout << p / min_n << endl;
}
return 0;
}