TIP2 - Totient in permutation (medium)

no tags 

In number theory, Euler's totient (or PHI function), is an arithmetic function that counts the number of positive integers less than or equal to a positive integer N that are relatively prime to this number N.

That is, if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.

Interestingly, PHI(87109)=79180, and it can be seen that 87109 is a permutation of 79180.

Input

The input begins with the number T of test cases in a single line.
In each of the next T lines there are an integer M.

Output

For each given M, you have to print on a single line the value of N, for which 1 < N < M, PHI(N) is a permutation of N and the ratio N/PHI(N) produces a minimum. If there's several answers output the greatest, or if need, "No solution." without quotes.
Leading zeros are not allowed for integers greater than 0.

Example

Input:
3
22
222
2222

Output:
21
63
291

Explanations : For the first case, in the range ]1..22[, the lonely number n for witch phi(n) is in permutations(n) is 21, (we have phi(21)=12). So the answer is obviously 21.
For the second case, in the range ]1..222[, there's two numbers n for witch phi(n) is in permutations(n), we have phi(21)=12 and phi(63)=36. But as 63/36 is equal to 21/12, we're taking the greater : 63.
For the third case, in the range ]1..2222[, there's four numbers n for witch phi(n) is in permutations(n), phi(21)=12, phi(63)=36, phi(291)=192 and phi(502)=250. Within those solutions 291/192 is the minimum, we output 291.

Constraints

1 < T < 10^2
1 < M < 10^12

Code size limit is 10kB ; the upper bound was set at 10^12 to make a (C/pascal/...)-solution easier to write. Constraints allow Python3 users to get AC under 1.86s (with a sub-optimal solution). (Edit 2017-02-11, after compiler updates)
If if you get TLE, you should try first TIP1.
If it's too easy for you TIP3 is made for you ;-)


hide comments
[Rampage] Blue.Mary: 2016-06-07 05:13:43

I agree with @mikhaelkh. My program works with uniformly random input. If the input is log-uniform random ones, it has a large probability to get TLE.

=(Francky)=> TIP2 and TIP3 were designed the same day, TIP2 being much easier. I didn't thought about a method that would be slower with log-uniform vs uniform ; it's the opposite with my methods. I think it's too late to add a new input file ; don't you think ? I could do so...
Well done for your AC.

Edit after solving TIP3: I spent a lot of time to figure out the pruning I used to get AC for TIP2 is indeed wrong. Finally I came up with the correct pruning.(I've already proved that.)

Last edit: 2018-05-07 05:40:38
[Lakshman]: 2015-07-06 10:38:34

Thanks @The next big thing for pointing out the mistake.

[Lakshman]: 2015-07-03 05:30:22

@Francky Can you please check My code I am getting WA, will this approach work? or I have to find some other method.

=(Francky)=> There's very few errors in your last submission. I will not tell you more. Good luck, and yet well done.

Last edit: 2015-07-03 17:56:23
Michael Kharitonov: 2013-03-11 18:33:18

Game, set and match.
--ans-->Congratulations, I must admit your algo involves an unexpected creative method, and it's really impressive.

Last edit: 2013-03-11 18:44:55
Michael Kharitonov: 2013-03-11 11:10:10

I think tests are weak. For example no tests below 1e7. I'd use log-uniform distribution plus some tricky tests.
--ans--> Here input is random uniform, so yes there's some easy cases... TIP3 is made for you, if you find TIP2 too easy ; test cases are stronger and of equal difficulties.
Good job again.

Last edit: 2013-03-11 11:21:23

Added by:Francky
Date:2013-01-06
Time limit:3s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:TIP1 extension