Editorial for JDCC '16 Contest 2 P1 - Simplify

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: aurpine

To simplify a fraction you find the greatest common denominator (GCD) and divide both the numerator and the denominator by it.

Iterating through the smaller number is sufficient for the problem for \(\mathcal{O}(\sqrt{\min(N, D)})\)

However a faster algorithm is using Euclid's algorithm for GCD.

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);

Time Complexity: \(\mathcal{O}(\log \min(N, D))\)


There are no comments at the moment.