Editorial for Đồng dư (DHHV 2021)
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
\(\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}}\)
-
\(a \equiv b \equiv c\ (mod\ d) \Leftrightarrow \big|a - b\big| \equiv \big|b - c\big| \equiv \big|c - a\big|\ (mod\ d) \Leftrightarrow \big(\big|a - b\big|\ \vdots\ d \big)\) và \(\big(\big|b - c\big|\ \vdots\ d \big)\) và \(\big(\big|c - a\big|\ \vdots\ d \big)\) \(\Leftrightarrow \big|gcd(a - b, b - c, c - a)\big|\ \vdots\ d\)
-
Vì \(a, b, c, d \in \mathbb{N}^*\) và cần tìm \(max(d)\) nên \(d = \big|gcd(a - b, b - c, c - a)\big|\)
\(\color{goldenrod}{\text{Tiếp cận}}\)
-
Tính \(gcd(a, b) = gcd(min(a, b), max(a, b) - min(a, b)) = gcd(min(a, b), max(a, b)\ mod\ min(a, b))\)
-
Tính \(gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(b, gcd(c, a)) = gcd(c, gcd(a, b))\)
\(\color{purple}{\text{Độ phức tạp}}\)
- Trong trường hợp xấu nhất thì \(a, b, c\) là các số fibonacci, độ phức tạp rơi vào \(\approx O(log(max(a, b, c)))\)
\(\color{green}{\text{Code tham khảo }}\): Ước chung lớn nhất
\(^{^{\color{purple}{\text{Độ phức tạp : }} O(log(n))\ \color{purple}{\text{thời gian}}\ ||\ O(1)\ \color{purple}{\text{bộ nhớ}}}}\)
C++
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
long long a, b, c;
cin >> a >> b >> c;
cout << __gcd(abs(a - b), __gcd(abs(b - c), abs(c - a)));
return 0;
}
Comments