Advertisement
a53

MPF

a53
Sep 23rd, 2022
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define VMAX 4000000
  3. using namespace std;
  4. int X,Y, auxx, auxy;
  5. int Q;
  6. int Maxp[VMAX+1], Nivel[VMAX+1];
  7. int gcd(int a, int b)
  8. {
  9. int r;
  10. while(b!=0)
  11. {
  12. r=a%b;
  13. a=b;
  14. b=r;
  15. }
  16. return a;
  17. }
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false);
  21. cin.tie(NULL);
  22. cout.tie(NULL);
  23. int i, j;
  24. Maxp[1]=1;
  25. Nivel[1]=0;
  26. for(i=2; i<=VMAX; i++)
  27. if(Maxp[i]==0)
  28. {
  29. Nivel[i]=1;
  30. Maxp[i]=i;
  31. j = i + i;
  32. while(j <= VMAX)
  33. {
  34. Maxp[j]=i;
  35. Nivel[j]=1+Nivel[j/Maxp[j]];
  36. j = j + i;
  37. }
  38. }
  39. cin>>Q;
  40. for(i=1; i<=Q; ++i)
  41. {
  42. cin>>X>>Y;
  43. if(X==Y)
  44. cout<<"0\n";
  45. else
  46. {
  47. auxx=X;
  48. auxy=Y;
  49. int cntx=0,cnty=0;
  50. while(X!=Y && X>1 && Y>1)
  51. {
  52. if(X>Y)
  53. X=X/Maxp[X], cntx++;
  54. else if(Y>X)
  55. Y=Y/Maxp[Y], cnty++;
  56. }
  57. if(X==Y)
  58. cout<<(Nivel[auxx]-Nivel[X])+(Nivel[auxy]-Nivel[X])<<'\n';
  59. else
  60. cout<<(Nivel[auxx]+Nivel[auxy])<<'\n';
  61. }
  62. }
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement