MATHLOVE - Math is Love


Rashid loves mathematics. He recently started to learn how the various function works in mathematics. On his journey through the math world, he came in touch with a function named Y.

He also came to know that, here Y means the summation of number from 0 to n. For example if n = 5, then 

    Y = 0 + 1 + 2 + 3 + 4 + 5 = 15

Now he is wondering if the knows the value of Y, can he find the value of n for the above equation?

Being so very new to the math world, he could not figure out how to approach this new problem. So he came to you for help. Can you help him?

Input

First line of input will be T (1 <= T <= 100000), denoting the number of test cases.

Next T lines will contain the number Y (1 <= Y <= 3*10^9), value of the above function.

Output

For each test case, if there is exist a value of n for which the above function gives the output Y then print that value of n, otherwise print “NAI” (quotes for clarification).

Note: Data set is huge, use scanf, printf for faster input, output.

Sample

Input
5
11
15
21
1
7626

Output
NAI
5
6
1
123

hide comments
li0n_mmn: 2023-11-14 03:12:56

AC in one go using binary search.
sum of 1 to n ={ n*(n+1) } / 2
left = 1, right = Y;

do4z: 2023-04-27 21:01:42

what if i say that , i can solve it with 7th standard mathematics in O(1) complexity

nahid5702: 2023-02-08 13:34:54

you can simply use binary search

sanyam19: 2018-06-07 11:57:08

easy 1.... use double for decimal but remember of changing into an integer as a final ans... :)

aadarsh45: 2018-04-06 17:26:51

Use long long int instead of double , i got 3 WA because of this

ayushgupta1997: 2017-09-27 22:10:43

No actual binary search required, tried precomputation didn't worked failed on test case 5,simple number theory brute force works out beautifully :D

avi_pandey: 2017-07-19 14:28:11

easy problem . AC in one go . use inputs in double.

Marcin: 2017-05-28 00:03:08

There is definitely something wrong with the upper bound of the input. I got WA for 3*10^9, but correct for 10*10^9. After first few steps on doing binary search on submissions, I found that the upper bound is somewhere between 4.9*10^9 and 6.4*10^9

Shubham Jadhav: 2017-05-10 20:25:27

easy points :)

candide: 2017-04-10 12:45:55

@shreeshiv : 0 is not a valid input (because1<=Y<=3*10^9) but my own code gives answer=0. BTW, you can try Online SPOJ toolkit.

Last edit: 2017-04-11 21:25:19

Added by:Jamil Siam
Date:2017-04-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All