Editorial for FNUM


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.

Spoiler Alert


Hint 1

  • Nhận xét rằng hàm \(f(x) =\) tổng các chữ số của \(x\) nó sẽ giảm rất nhanh

Cụ thể: \(f(x) \leq 9 * \lceil\log_{10}x\rceil\)

Sau đó ta chỉ cần tính f(f(..f(x)..)) tới khi nào 1 ≤ x ≤ 9


Hint 2

  • Đầu tiên ta tính \(x = f(input_string)\)

\(f(max\_val) \leq 9 * \lceil\log_{10}(max\_val)\rceil \leq 9 * 1000000\)

Nên khi ta tính trước, ta sẽ đưa số \(x\) về kiểu dữ liệu \(int\)


Hint 3

  • Online Solving: Ta có thể không cần nhận xâu

Reference AC code | \(O(\log_{10}n)\) time | \(O(1)\) auxiliary space | Online Solving, Math

C++
inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
int main()
{
    int n = 0;
    for (char c; isDigit(c = getchar()); n += (c - '0'));
    while (n > 9)
    {
        int new_n = 0;
        do new_n += n % 10; while (n /= 10);
        n = new_n;
    }
    cout << n;
    return 0;
}


Comments

There are no comments at the moment.