Advertisement
Guest User

Untitled

a guest
May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. #include <cmath>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <cassert>
  10.  
  11. using namespace std;
  12.  
  13. int gcd(int a, int b) {
  14.     if (b == 0) return a;
  15.     else return gcd(b, a % b);
  16. }
  17.  
  18. int main()
  19. {
  20.     int t,n,k; cin >> t;
  21.     for (int i = 0; i < t; i++)
  22.     {
  23.         cin >> n >> k;
  24.         int ans = 0;
  25.         bool err = true;
  26.         if (n == 1)
  27.         {
  28.             ans = 0;
  29.         }
  30.         else
  31.         if (k == 1)
  32.         {
  33.             ans = -1;
  34.             err = false;
  35.         }
  36.         else
  37.         {
  38.  
  39.  
  40.             for (int j = 2; j * j <= n; j++)
  41.             {
  42.                 int cnt = 0;
  43.                 while (n % j == 0)
  44.                 {
  45.                     n /= j;         cnt++;
  46.                 }
  47.  
  48.  
  49.  
  50.                 if (cnt == 0) continue;
  51.  
  52.                 int cnt1 = 0;
  53.  
  54.  
  55.                 while (k % j == 0)
  56.                 {
  57.                     k /= j;
  58.                     cnt1++;
  59.                 }
  60.  
  61.  
  62.  
  63.  
  64.                 if (cnt1 == 0)
  65.                 {
  66.                     err = false;
  67.                     break;
  68.                 }
  69.                 if (cnt <= cnt1)continue;
  70.                 ans = max(ans, (cnt - 1) / cnt1);
  71.             }
  72.  
  73.  
  74.  
  75.             if (n > 1) {
  76.                 int cnt = 1;
  77.                 int cnt1 = 0;
  78.                 while (k % n == 0)
  79.                 {
  80.                     k /= n;
  81.                     cnt1++;
  82.  
  83.                 }
  84.  
  85.  
  86.  
  87.                 if (cnt1 == 0)
  88.                 {
  89.                     err = false;
  90.                 }
  91.  
  92.                 else if (cnt >= cnt1) ans = max(ans, (cnt - 1) / cnt1);
  93.             }
  94.         }
  95.        
  96.        
  97.        
  98.         if (err == false) cout << -1 << endl; else cout << ans + 1 << endl;;
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement