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
2015-06-20 13:56:01 Vipul Srivastava
Can get AC with scanf and printf also.

Last edit: 2015-06-20 13:56:28
2014-01-24 03:31:50 Hector Monteo
use gets and puts in place scanf/cin and printf/cout.
2013-10-22 17:46:23 Achmet ibn Rashid
AC in slightly under 1sec with O(n) solution. This problem is yet another I/O race. I wonder how the top solutions got as low as 0.24 though; is using fread/read that much faster than getchar_unlocked or do you have to use something else?

EDIT: Silly me; I was using printf. Got 0.27 now that I fixed that.

Last edit: 2013-10-22 17:51:35
2013-04-21 02:57:04 nondescript
i am getting nzec...does anyone know what is wrong? i am returning 0 so its an issue about that....my code is very simple..is the server working?
2013-01-15 18:24:30 Surya kiran
using getchar_unlocked() is the fastest way ... try that for AC
2012-12-21 08:40:38 Pranshul Agarwal
got AC.. :D Those getting TLE can try HPYNOS , ACPC10B and HPYNOSI first..
2012-08-22 16:00:31 god_father
use gets and puts in place scanf and printf got Ac with that...!!
2012-08-10 19:06:48 StupidGuy
Even scanf(), printf() is getting tle.
Any suggestions?
2012-06-27 13:25:39 Kushagra Kumar
optimized IO is the only way to solve this problem.
2012-06-19 04:46:04 jaans
@nilendu_das :: I love u :*
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.