Oleg’s favorite subjects are History and Math, and his favorite branch of mathematics is division.
To improve his division skills, Oleg came up with 𝑡 pairs of integers 𝑝𝑖 and 𝑞𝑖 and for each pair decided to find the greatest integer 𝑥𝑖, such that:
𝑝𝑖 is divisible by 𝑥𝑖;
𝑥𝑖 is not divisible by 𝑞𝑖
Oleg is really good at division and managed to find all the answers quickly, how about you?
Input
The first line contains an integer 𝑡(1≤𝑡≤50) — the number of pairs.
Each of the following 𝑡 lines contains two integers 𝑝𝑖 and 𝑞𝑖 (1≤𝑝𝑖≤1018; 2≤𝑞𝑖≤109) — the 𝑖-th pair of integers.
Output
Print
𝑡 integers: the 𝑖-th integer is the largest 𝑥𝑖 such that 𝑝𝑖 is divisible by 𝑥𝑖, but 𝑥𝑖 is not divisible by 𝑞𝑖.
One can show that there is always at least one value of 𝑥𝑖 satisfying the divisibility conditions for the given constraints.
Example
Input
3
10 4
12 6
179 822
Output
10
4
179
Note
For the first pair, where 𝑝1=10 and 𝑞1=4, the answer is 𝑥1=10, since it is the greatest divisor of 10 and 10 is not divisible by 4.
For the second pair, where 𝑝2=12 and 𝑞2=6, note that 12 is not a valid 𝑥2, since 12 is divisible by 𝑞2=6; 6 is not valid 𝑥2 as well: 6 is also divisible by 𝑞2=6 .
The next available divisor of 𝑝2=12 is 4, which is the answer, since 4 is not divisible by 6
题目大意:给出q,p,找出一个最大的x能被q整除不能被整除
官方题解:
大佬写的代码—
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
int t;
long long p, q;
int a[55], cnt;
long long k;
int main()
{
int i;
long long x;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld", &p, &q);//输入p,q
cnt = 0;
for (i = 2, x = q; (long long)i * i <= q; i++)//筛出q的p1,p2,p3...(唯一分解定理里面的底数)
if (!(x % i))
{
a[++cnt] = i;
while (!(x % i))
x /= i;
}
//例如q=300000时,q=2^5*3^1*5^5,此时数组a中存储的元素是2,3,5,cnt=3;
if (x > 1) //如果最后x大于1,即最后产生了一个小的素数,直接保存即可
a[++cnt] = x, x = 1;
//执行完上面的代码后 x==1,本题如果没有的话就输出1;
for (i = 1; i <= cnt; i++)
{
k = p;
while (!(k % q))//找到不能变小的
k /= a[i];
if (k > x)//找出来最大的
x = k;
}
printf("%lld\n", x);
}
return 0;
}