/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられるので、 N がハッピー数であるかを判定してください。
ハッピー数とは、以下の操作を有限回繰り返すことで 1 になる非負整数を指します。
- 自身を、十進法表記の各桁の数字の二乗和を取った整数に置き換える。
- 例えば、操作前に 2026 であれば、この操作を 1 度行うと自身が 2^2+0^2+2^2+6^2 = 4+0+4+36 = 44 に置き換えられます。
具体的なハッピー数の例は入出力例の説明を参照してください。
制約
- N は 1 以上 2026 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N がハッピー数であるなら Yes 、ハッピー数でないなら No と出力せよ。
入力例 1
2026
出力例 1
Yes
2026 はハッピー数です。
- 2026 の十進法表記の各桁は 2,0,2,6 であり、それらの二乗和を取ると 2^2+0^2+2^2+6^2 = 4+0+4+36 = 44 となります。
- 44 の十進法表記の各桁は 4,4 であり、それらの二乗和を取ると 4^2+4^2 = 16+16 = 32 となります。
- 32 の十進法表記の各桁は 3,2 であり、それらの二乗和を取ると 3^2+2^2 = 9+4 = 13 となります。
- 13 の十進法表記の各桁は 1,3 であり、それらの二乗和を取ると 1^2+3^2 = 1+9 = 10 となります。
- 10 の十進法表記の各桁は 1,0 であり、それらの二乗和を取ると 1^2+0^2 = 1+0 = 1 となります。
自身を十進法表記の各桁の数字の二乗和を取った整数に置き換える操作を 5 回繰り返すことで 1 になったので、 2026 はハッピー数です。
入力例 2
439
出力例 2
No
439 はハッピー数ではありません。
操作を繰り返すことで 439 \rightarrow 106 \rightarrow 37 \rightarrow 58 \rightarrow 89 \rightarrow 145 \rightarrow 42 \rightarrow 20 \rightarrow 4 \rightarrow 16 \rightarrow 37 \rightarrow \dotsb と変化し、ここから操作を何度繰り返しても 1 にならないことが証明できます。
入力例 3
440
出力例 3
Yes
440 はハッピー数です。
Score : 200 points
Problem Statement
You are given a positive integer N. Determine whether N is a happy number.
A happy number is a non-negative integer that becomes 1 after repeating the following operation a finite number of times:
- Replace it with the integer obtained by taking the sum of the squares of the digits in its decimal representation.
- For example, performing this operation once on 2026 replaces it with 2^2+0^2+2^2+6^2 = 4+0+4+36 = 44.
For examples of happy numbers, refer to the explanations of sample inputs and outputs.
Constraints
- N is an integer between 1 and 2026, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
If N is a happy number, output Yes; otherwise, output No.
Sample Input 1
2026
Sample Output 1
Yes
2026 is a happy number.
- The digits of 2026 in decimal representation are 2,0,2,6, and taking the sum of their squares gives 2^2+0^2+2^2+6^2 = 4+0+4+36 = 44.
- The digits of 44 in decimal representation are 4,4, and taking the sum of their squares gives 4^2+4^2 = 16+16 = 32.
- The digits of 32 in decimal representation are 3,2, and taking the sum of their squares gives 3^2+2^2 = 9+4 = 13.
- The digits of 13 in decimal representation are 1,3, and taking the sum of their squares gives 1^2+3^2 = 1+9 = 10.
- The digits of 10 in decimal representation are 1,0, and taking the sum of their squares gives 1^2+0^2 = 1+0 = 1.
2026 is a happy number because it became 1 after repeating the operation of replacing itself with the integer obtained by taking the sum of the squares of the digits in its decimal representation five times.
Sample Input 2
439
Sample Output 2
No
439 is not a happy number.
Repeating the operation makes it 439 \rightarrow 106 \rightarrow 37 \rightarrow 58 \rightarrow 89 \rightarrow 145 \rightarrow 42 \rightarrow 20 \rightarrow 4 \rightarrow 16 \rightarrow 37 \rightarrow \dotsb, and it can be proved that no matter how many times the operation is repeated from here, it will not become 1.
Sample Input 3
440
Sample Output 3
Yes
440 is a happy number.