HPYNOSII - Happy Numbers II

The process of “breaking” an integer is defined as summing the squares of its digits. For example, the result of breaking the integer 125 is (12 + 22 + 52) = 30. An integer N is happy if after “breaking” it repeatedly the result reaches 1. If the result never reaches 1 no matter how many times the “breaking” is repeated, then N is not a happy number.

Task

Write a program that given an integer T (number of test cases) and T integers, determines for each number whether it is a happy number or not.

Constraints

1 ≤ T ≤ 1,080,000

2 ≤ N ≤ 2,147,483,647 (number for determining whether it is happy or not)

Input

The first line contains an integer T.

Next T lines contain an integer N for detemining whether it is happy or not.

Output

T lines containing a single integer N which is the number of times the process had to be done to determine that N is happy, or -1 if N is not happy.

Example

Input:
2
19
204

Output:
4
-1

Explanation

First test case:

  • 19 : 12 + 92 = 82
  • 82 : 82 + 22 = 68
  • 68 : 62 + 82 = 100
  • 100 : 12 + 02 + 02 = 1

The solution for 19 is 4 because we discovered that the integer 19 is happy after we repeated the process 4 times.

Second test case:

204 → 20 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 → 145 ...

204 is not a happy number because after breaking it several times the results start repeating so we can deduce that if we continue breaking it, the result will never reach 1.


Added by:Rofael Emil
Date:2010-11-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:(modified) Egyptian Olympiad in Informatics ( EOI ) 2009, August 14 - 21, Cairo

hide comments
2010-12-13 22:07:20 .::Manish Kumar::.
did anyone get AC using scanf?

Last edit: 2010-11-10 19:45:22
2010-12-13 22:07:20 numerix
Yes, 3 s is a good timelimit. Even enough for a (well optimized) Python solution, and by far enough for a Pascal solution.
Edit: Timelimit once again changed to 2s?!

Last edit: 2010-11-14 21:23:44
2010-12-13 22:07:20 Rofael Emil
Is it Ok, Now?
2010-12-13 22:07:20 numerix
That has nothing to do with an optimal algorithm - most languages are excluded because of large I/O. If you look at Problem INTEST you can guess what languages that will be.
Edit: After spending some more time on it: It is not only a matter of I/O!

Last edit: 2010-11-14 21:05:55
2010-12-13 22:07:20 Rofael Emil
Verifing the corectness of submited algorithms and their optimality with respect to time

Last edit: 2010-11-09 11:34:46
2010-12-13 22:07:20 .::Manish Kumar::.
after happy numbers 1 ...what is the purpose of happy numbers 2..?
may be I/O.
2010-12-13 22:07:20 :(){ :|: & };:

Time limit is a bit tight,the use of Fast I/O should be left as an option not a mandatory business.
2010-12-13 22:07:20 Ehor Nechiporenko
Yes, I have submitted this task!
Instead of scanf/printf, use fread/fwrite.
I have used fread/putc.
2010-12-13 22:07:20 Priyank
scanf() timeouts! I don't think any problem should be such that this happens.
The author should make appropriate changes to the constraints and test data.
2010-12-13 22:07:20 Ehor Nechiporenko
Maybe it's better to make problem less I/O related and instead of reading big count of N numbers, change task and give intervals [a,b]. Then we can ask to caculate SUM(f(n)) where n in interval [a,b].
It will make problem less I/O related.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.