Editorial for Vấn đề 2^k


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.

\(\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{Hint 1 <Brute-force>}}\)

  • Thử từng số có dạng 2^k và duyệt qua mảng xem có thỏa mãn hay không

\(\color{orange}{\text{Hint 2 <Binary-search>}}\)

  • Chặt nhị phân để tìm số \(2^k\) thỏa mãn, với \(k \in [0, log_2(max\_val)]\)

\(\color{orange}{\text{Hint 3 <Factorization>}}\)

  • Tìm ước \(2^k\) lớn nhất của các phần tử trong mảng

\(\color{orange}{\text{Hint 4 <Bitwise>}}\)

  • Số \(2^k\) lớn nhất mà \(x\) chia hết là số lượng số 0 ở cuối số \(x\) trong biểu diễn nhị phân

\(\color{orange}{\text{Hint 5 <Online Solving>}}\)

  • Bài này ta có thể giải trực tiếp từ kết quả của lần tình trước

\(\color{green}{\text{Preference AC Code }}\): Online Solving, Bitwise

\(^{^{\color{purple}{\text{Complexity : }} O(n)\ \color{purple}{\text{time}}\ ||\ O(1)\ \color{purple}{\text{memory}}}}\)

C++
int main()
{
    ll res = 1;
    for (int n = readInt(); n--; )
    {      
        ll tmp;
        getUnsign(tmp);
        ll val = 1LL << __builtin_ctzll(tmp);
        maximize(res, val);
    }

    cout << res;
    return 0;
}


Comments

There are no comments at the moment.