Editorial for Đồng dạng (OLP MT&TN 2021 CT)


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.

Authors: SPyofgame


\(\color{red}{\text{Spoiler Alert}_{{}_{{}^{{}^{v2.0}}}}}\)

\(\color{red}{\text{Khuyến khích bạn đọc trước khi đọc phần lời giải xin hãy thử code ra thuật của mình dù nó có sai hay đúng}}\)

\(\color{red}{\text{Sau đó từ phần bài giải và thuật toán trước đó mà đối chiếu, rút nhận xét với thuật của mình và thu được bài học (không lãng phí thời gian đâu).}}\)



\(\color{orange}{\text{Hướng dẫn}}\)

  • Sắp xếp lại các giá trị \(a_1 < b_1\)\(a_2 < b_2\)

  • Nếu \(\frac{a_1}{b_1} = \frac{a_2}{b_2}\) thì xuất "YES" ngược lại xuất "NO"


\(\color{goldenrod}{\text{Tiếp cận}}\)

  • Để kiểm tra \(\frac{a}{b} = \frac{c}{d}\) với \(a, b, c, d \in \mathbb{N}^*\) thì kiểm tra \(\frac{a}{gcd(a, b)} = \frac{c}{gcd(c, d)}\)\(\frac{b}{gcd(a, b)} = \frac{d}{gcd(c, d)}\)

\(\color{purple}{\text{Độ phức tạp}}\)

  • Tìm \(gcd(a, b)\) tính trong \(O(log(max(a, b)))\)

\(\color{green}{\text{Code tham khảo }}\): Toán học

\(^{^{\color{purple}{\text{Độ phức tạp : }} O(log n)\ \color{purple}{\text{thời gian}}\ ||\ O(log n)\ \color{purple}{\text{bộ nhớ}}}}\)

C++
#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;
int main()
{
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a > b) swap(a, b);
    if (c > d) swap(c, d);

    ll x = __gcd(a, b);
    ll y = __gcd(c, d);
    a /= x;
    b /= x;
    c /= y;
    d /= y;
    /// (a / b) = (c / d)
    /// a / gcd(a, b) = c / gcd(c, d)
    /// b / gcd(a, b) = d / gcd(c, d)

    cout << ((a == c) && (b == d) ? "YES" : "NO");
    return 0;
}


Comments (1)

Order by
Loading comments...