# CF 1445C

## 题目传送门

1.$$p_i < q_i$$时,答案为$$p_i$$.

2.$$p_i \mod q_i \neq 0$$时,答案为$$p_i$$.

3.利用唯一分解定理分解$$p_i$$$$q_i$$:

$$p_i = k_1^{t_1} * k_2^{t_2} * k_3^{t_3} * ... * k_n^{t_n}$$

$$p_i = k_1^{r_1} * k_2^{r_2} * k_3^{r_3} * ... * k_n^{r_n}$$

//AC
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

long long t,tot,zs;
long long num[1000001],sum[1000001];
long long x,y,ans,mx;
bool vis[1000001];

inline void oula() {
vis[1] = 1;
for(long long i = 2;i <= 1000000; i++) {
if(!vis[i]) num[++tot] = i;
for(long long j = 1;j <= tot; j++) {
if(num[j] * i > 1000000) break;
vis[num[j]*i] = 1;
if(i % num[j] == 0) break;
}
}
}

inline void fenjie(long long a) {
for(long long i = tot;i >= 1; i--) {
while(a % num[i] == 0) {
sum[i]++;
a = a / num[i];
}
}
}

int main() {
scanf("%lld",&t);
oula();
while(t--) {
memset(sum,0,sizeof(sum));
ans = 1;
scanf("%lld%lld",&x,&y);
if(x < y) {
printf("%lld\n",x);
continue;
}
if(x % y != 0) {
printf("%lld\n",x);
continue;
}
fenjie(y);
for(long long i = tot;i >= 1; i--) {
if(sum[i] == 0) continue;
long long uu = x,vv = y,m1 = 0,m2 = 0;
while(uu % num[i] == 0) uu /= num[i],m1++;
while(vv % num[i] == 0) vv /= num[i],m2++;
for(int j = 1;j <= m2 - 1; j++) uu *= num[i];
ans = max(ans,uu);
}
if(ans == 1) {
while(x % y == 0) x /= y;
ans = x;
}
printf("%lld\n",ans);
}
return 0;
}

//WA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

long long t,tot,zs;
long long num[1000001],sum[1000001];
long long x,y,ans,mx;
bool vis[1000001];

inline void oula() {
vis[1] = 1;
for(long long i = 2;i <= 1000000; i++) {
if(!vis[i]) num[++tot] = i;
for(long long j = 1;j <= tot; j++) {
if(num[j] * i > 1000000) break;
vis[num[j]*i] = 1;
if(i % num[j] == 0) break;
}
}
}

inline void fenjie(long long a) {
for(long long i = tot;i >= 1; i--) {
while(a % num[i] == 0) {
zs++;
sum[i]++;
a = a / num[i];
}
}
}

int main() {
scanf("%lld",&t);
oula();
while(t--) {
zs = 0;
memset(sum,0,sizeof(sum));
ans = 1;
scanf("%lld%lld",&x,&y);
if(x < y) {
printf("%lld\n",x);
continue;
}
if(x % y != 0) {
printf("%lld\n",x);
continue;
}
fenjie(y);
for(long long i = tot;i >= 1; i--) {
if(sum[i] == 0) continue;
long long uu = y / num[i];
long long vv = x / y;
while(vv % num[i] == 0) vv = vv / num[i];
ans = max(ans,vv * uu);
}
if(zs <= 1) {
while(x % y == 0) x /= y;
ans = x;
}
printf("%lld\n",ans);
}
return 0;
}

posted @ 2020-11-03 20:49  Mr^Simon  阅读(112)  评论(0编辑  收藏  举报