QOJ.ac

QOJ

実行時間制限: 0.2 s メモリ制限: 512 MB 満点: 100

#13110. 快乐数

統計

考虑如下为任意自然数 $n$ 定义的函数 $f$:

$f(n)$ 是将 $n$ 的十进制数字的平方求和后得到的数。 例如,若 $n = 19$,则 $f(19) = 82$,因为 $1^2 + 9^2 = 82$。

重复应用此函数 $f$,某些自然数最终会变为 $1$。这样的数被称为“快乐数”(happy numbers)。例如,$19$ 是一个快乐数,因为重复应用函数 $f$ 于 $19$ 的结果如下:

$f(19) = 1^2 + 9^2 = 82$ $f(82) = 8^2 + 2^2 = 68$ $f(68) = 6^2 + 8^2 = 100$ $f(100) = 1^2 + 0^2 + 0^2 = 1$

然而,并非所有自然数都是快乐数。你可以尝试 $5$,你会发现 $5$ 不是一个快乐数。如果 $n$ 不是快乐数,数学家已经证明,重复应用函数 $f$ 于 $n$ 会达到以下循环:

$4 \to 16 \to 37 \to 58 \to 89 \to 145 \to 42 \to 20 \to 4$

编写一个程序,判断给定的自然数 $n$ 是否为快乐数。

输入格式

程序从标准输入读取数据。输入包含一行,为一个整数 $n$ ($1 \le n \le 1,000,000,000$)。

输出格式

程序向标准输出写入数据。输出仅一行。如果给定的数 $n$ 是快乐数,输出 HAPPY;否则,输出 UNHAPPY

样例

样例输入 1

19

样例输出 1

HAPPY

样例输入 2

5

样例输出 2

UNHAPPY

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.