INVPHI - Smallest Inverse Euler Totient Function

This task is the inverse of ETF problem, given an integer n find smallest integer i such that φ(i)=n, where φ denotes Euler's totient function.


The first line is an integer T(1 ≤ T ≤ 100,000), denoting the number of test cases. Then, T test cases follow.

For each test case, there is an integer n(1 ≤ n ≤ 100,000,000) written in one line. (one ineger per line)


For each test case, output Smallest Inverse Euler's Totient Function of n. if n doesn't have inverse, output -1.





Time Limit ≈ 3*(My Program Top Speed)


See also: Another problem added by Tjandra Satria Gunawan

hide comments
ajkdrag: 2018-09-04 13:31:26

Getting WA, some test cases please. For n = 1 , case the result should be 1, right?

straw__hat: 2018-05-31 11:44:31

@admin Why I'm getting runtime error for my this sol'n 21755292?

cat_got_bored: 2016-12-03 17:42:56

The solution is straightforward but there are some pitfalls. some tips :
1) Don't do binary search. Sorting will take too much time (TLE).
2)Check for n=1.
3)Use all int arrays (Or will get SEGKILL). inside loops, use long long to avoid overflow.
3)Finally, here n means the the value of totient function, you need to find x. where, ETF(x) = n. Even though, n will be at most 10^8, you need to find x, which can be bigger than 10^8.
4)The maximum x for this problem is 202918035, when n=99683840.
Thanks to problemsetter, Nice problem :)

Chetan: 2015-06-30 13:22:12

Please Help me out here with SPOJ submission 14571312
I've no idea why I keep getting SIGSEGV or SIGKILL here.
I've provided the correct limiting conditions, according to me! :/

[Lakshman]: 2014-01-08 18:52:48

@(Tjandra Satria Gunawan)(曾毅昆)
Can you please tell me why I am getting WA, I think My algorithm is correct.
Please help.

anurag garg: 2013-12-29 04:28:12

@(Tjandra Satria Gunawan)(曾毅昆)
my code is giving WA
Edit: AC after 20 or so many WA
very nice question learned so many new concepts

Last edit: 2014-01-07 20:41:13
anurag garg: 2013-12-29 04:28:12

Last edit: 2013-12-29 04:28:38
Sandeep Pathry: 2013-07-12 16:23:38

Finally!!! AC...
n=1 case costed me 15 WAs... :D

Last edit: 2013-07-13 08:28:38
Ritam Shukla: 2013-01-19 10:30:39

Though i have a doubt. Why is it that even if i try to run my accepted solution on ideone, it gives a runtime error SIGKILL? Is it that memory limit there is very tight? And is there any special option available there that enables us to use the Cube?
Ans: I don't know... but seems that memory limit for ideone still 256 MB, for more info about this click here.

Last edit: 2013-01-19 11:17:22
Ritam Shukla: 2013-01-18 21:22:57

One of the finest tasks I've ever done! Great job, Tjandra. Keep it up. :)
Ans: Thanks for your appreciation :-)

Last edit: 2013-01-19 11:18:15

Added by:Tjandra Satria Gunawan
Time limit:20s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own Problem