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 (1^{2} + 2^{2} + 5^{2}) = 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 1...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
1) 19 : 1^{2} + 9^{2} = 82 2) 82 : 82 + 2^{2} = 68 3) 68 : 6^{2} + 8^{2} = 100 4) 100 : 1^{2 }+ 0^{2} + 0^{2} = 1
The solution for 19 is 4 because we discovered that the integer 19 is happy after we repeated the process 4 times.
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.
hide comments
nadstratosfer:
20180408 03:37:02
Optimized (!) O(1) in PyPy gets 0.50 in the tutorial version, hence TLE here. Don't waste your time on this garbage, use INTEST for IO optimizations instead. 

karthik1997:
20180101 21:05:50
Man , used floyd cycle detection and all i could do is to solve it in 0.10s. Cant get below it . :( 

coder_hsnake:
20160830 13:12:45
wow same solution of happy number got me AC in one go!!!!! and this is no I/O race scanf and printf works fine 

Piyush Kumar:
20160624 20:05:35
So the difference between this and HYPNOS is just IO. :( 

shantanu tripathi:
20150829 21:25:16
fread_unlocked will work... very strict i/o...rest is precomputation :) 

Ankit:
20150818 16:25:51
use getchar_unlocked :) 

:.Mohib.::
20150801 17:42:54
Nice..!! 

Aditya Kumar:
20150629 11:12:26
finally AC.....


ASHUTOSH DWIVEDI:
20150625 23:11:36
hell of a problem


Vipul Srivastava:
20150620 14:00:45
My 2 different solutions one giving 1 for 1000000 and the other giving 2 for the same get AC. 
Added by:  Rofael Emil 
Date:  20101105 
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 