Guest User

Untitled

a guest
Aug 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define s second
  5. #define f first
  6. #define fast_in std::ios::sync_with_stdio(false);
  7. #define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
  8.  
  9. //Sieve
  10. #define sieveMax 50000 //maximum for which u need primality test
  11. vector<int> Prime;
  12. void sieve(){
  13. bool neverMakeThisName[sieveMax];
  14. memset(neverMakeThisName,false, sizeof(neverMakeThisName));
  15. neverMakeThisName[0]=neverMakeThisName[1]=true;
  16. for(int i=4; i<sieveMax; i+=2) neverMakeThisName[i]=true;
  17.  
  18. for(int i=3; i<=sqrt(sieveMax); i+=2)
  19. if(neverMakeThisName[i]==false) {
  20. for(int j=i*i; j<sieveMax; j+=i) neverMakeThisName[j]=true;
  21. }
  22.  
  23. for(int i=0; i<sieveMax; i++) if(neverMakeThisName[i]==false) Prime.push_back(i);
  24. }
  25. //end Sieve
  26.  
  27. //set having all prime factors of (A1,B1)
  28. set<int> probables;
  29. void findPrimeFactors(ll temp){
  30. for(int i=0; i<Prime.size() && Prime[i]*Prime[i]<=temp; i++){
  31. if(temp%Prime[i]) continue;
  32. while((temp%Prime[i])==0){
  33. temp/=Prime[i];
  34. }
  35. probables.insert(Prime[i]);
  36. }
  37. if(temp>1){
  38. probables.insert(temp);
  39. }
  40. }
  41.  
  42. int main(){
  43. fast_cin;
  44. int n;cin>>n;
  45. pair<ll,ll> arr[n];
  46. for(int i=0; i<n; i++){
  47. cin>>arr[i].f>>arr[i].s;
  48. }
  49.  
  50. //ignore below three line
  51. //and function if you use something
  52. //different for finding Prime Factors
  53. sieve();
  54. findPrimeFactors(arr[0].f);
  55. findPrimeFactors(arr[0].s);
  56. //end ignore
  57.  
  58. for(auto it=probables.begin(); it!=probables.end(); it++){
  59. bool ans=true;
  60. ll val=*it;
  61. //val can be possible solution
  62. for(int i=0; i<n; i++){
  63. if((arr[i].f%val)==0 || (arr[i].s%val)==0){
  64. continue;
  65. }
  66. //if even one pair is not
  67. //divisble, this can't
  68. //be our solution
  69. ans=false;
  70. break;
  71. }
  72. if(ans){
  73. cout << val << endl;
  74. return 0;
  75. }
  76. }
  77. //none of probables divided all pairs
  78. //therefore no solution exists
  79. cout << -1 << endl;
  80. return 0;
  81. }
Add Comment
Please, Sign In to add comment