1025B - Weakened Common Divisor - CodeForces Solution


brute force greedy number theory *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define dbg(x) cout << "[" << #x << " = " << x << "] ";
#define ff first
#define ss second

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using vi = vector<int>;

using tii = tuple<int,int,int>;
// auto [a,b,c] = ...
// .insert({a,b,c})

const int oo = (int)1e9 + 5; //INF to INT
const ll OO = 0x3f3f3f3f3f3f3f3fLL; //INF to LL

/*
Muito tempo preso no problema? Próximo
Reler enunciado
casos de borda: n = 0, n = 1
cuidado com overflow
*/

set<int> p;

void fat(int x) {
    for(int u = 2; 1LL * u * u <= 1LL * x; u++) {
        if(x % u == 0) {
            p.insert(u);
            while(x % u == 0)
                x /= u;
        }
    }
    
    if(x > 1) {
        p.insert(x);
    }
    
}

void solve() {  

    int n;
    cin >> n;
        
    vector<pii> arr(n);
    
    for(auto &x : arr) {
        cin >> x.ff >> x.ss;
    }
    
    fat(arr[0].ff);
    fat(arr[0].ss);
    
    for(auto &prime : p) {
        
        bool ans = true;
        
        for(auto &k : arr) {
            if(k.ff % prime && k.ss % prime) {
                ans = false;
                break;
            }
        }
        
        if(ans) {
            cout << prime << "\n";
            return;
        }
    
    }    
    
    cout << -1 << "\n";


}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    while(t--)
        solve();


}


Comments

Submit
0 Comments
More Questions

1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale