质数进阶
A - Division
题目链接
题意
找一个最大的
思路
质因数分解
p % q ! = 0 ">p % q != 0,x = p ">x=pp % q = 0 ">p % q = 0 , 那么p ">p一定包含q ">q的所有质因数分解的结果。
举例:
要使
所以,我们只需要枚举
代码
#includeusing namespace std; typedef long long ll; int main() { int t; cin >> t; while (t--) { ll a, b; cin >> a >> b; if (a % b != 0) { cout << a << endl; continue; } ll mas = 0; for (int i = 2; i * i <= b; i++) { if (b % i) continue; ll c = a; while (c % b == 0) c /= i; mas = max(mas, c); c = a; while (c % b == 0) c /= (b / i); mas = max(mas, c); } while (a % b == 0) a /= b; mas = max(mas, a); cout << mas << endl; } return 0; }