fork download
  1. #include <iostream>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. int Pow(int a, int b) {
  7. int res = 1;
  8. while (b) {
  9. if (b&1) res *= a;
  10. b >>= 1;
  11. a *= a;
  12. }
  13. return res;
  14. }
  15.  
  16. signed main() {
  17. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  18. int t; cin >> t;
  19. while (t--) {
  20. int x, y, res = 0; cin >> x >> y;
  21. if (x%y) {
  22. cout << x << '\n';
  23. continue;
  24. }
  25. for (int i = 2; i*i <= y; ++i)
  26. if (y%i == 0) {
  27. int count = 0;
  28. while (y%i == 0) y /= i, ++count;
  29. int cnt = 0;
  30. while (x%i == 0) x /= i, ++cnt;
  31. res = max(res, x/Pow(i, cnt)*Pow(i, min(cnt, count-1)));
  32. }
  33. if (y > 1) {
  34. int cnt = 0;
  35. for (int j = y; x%j == 0; j *= y) ++cnt;
  36. res = max(res, x/Pow(y, cnt));
  37. }
  38. cout << res << '\n';
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2