IRECSQRT - Inverse of Recurrence Problem With a Square Root

Given this recurrence formula (be careful, it's in inverse form):

Formula

Given n (0 ≤ n < 264) and m (0 < m < 264), your task is to compute an modulo m.

It's guaranteed that an is always an integer.

Input

First line containing an integer T (0 < T ≤ 5×104), than T cases follow.

For each test case there are two integers n and m, written in one line, separated by a space.

Output

For each test case, output the required answer: an modulo m.

Example

Input:
10
0 10
1 10
2 10
3 10
10 10
100 100
1000 1000
10000 10000
100000 100000
9876543210123456789 1234567890987654321

Output:
1
2
5
5
5
51
251
6251
6251
657422418465782775

Time limit ~7x My program speed: Click here to see my submission history and time record for this problem

See also: Another problem added by Tjandra Satria Gunawan


Added by:Tjandra Satria Gunawan
Date:2013-03-14
Time limit:13s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ONMIPA sel 2013

hide comments
2019-12-31 13:09:38 Dune
AC in python and pypy without wolfram nor OEIS, 10 lines of python code.
2019-06-23 19:42:52
Very easy to solve using WolframAlpha, OEIS, and Python! Using fast IO, I got 1.74 s, 28 MB

Last edit: 2019-06-23 19:44:20
2015-08-07 00:11:40 Alei Reyes
TLE in python, AC with PyPy
2014-12-29 07:57:44 Sahil Sharma
@ (Tjandra Satria Gunawan)(曾毅昆) really nice problem :) I got an AC but have a doubt, your python solution got an AC in about 1 s whereas mine too about 9 seconds. Could you please see why that is the case? Do I need to optimize the IO?

Thanks
2014-11-24 20:49:17 shubham tiwari
I got the recursion in linear form.. but I am not able to implement it on c++. I was trying it with vectors but it doesn't work.. I am just one step behind with ac.. please help
2014-03-04 18:25:43 (Tjandra Satria Gunawan)(曾毅昆)
@Min_25: case are generated randomly, if you want to help me to improve this problem, you can tell me that difficult case, and I'll add it when I have fast and stable internet connection (because the data is ~2MB)..
2014-03-03 07:33:01 Min_25
The most difficult case (in C++) does not seem to be included.

EDIT:
@Tjandra Satria Gunawan (曾毅昆)
I think the most difficult case is m = 3^40 (=12157665459056928801). BigInteger (> 64 bit) class is required only in that case.

Last edit: 2014-12-25 07:47:37
2013-07-05 10:40:56 [Lakshman]
@Tjandra Satria Gunawan)(曾毅昆) Can you please help me now I am getting correct output but getting TLE can you please tell me if I am doing something wrong??..

Last edit: 2013-08-09 11:47:02
2013-06-15 10:52:20 Aayush Tripathi
I'm getting two values for a(1). 2 and 7. On what basis should I ignore 7 as the answer is 2 ?
--ans(francky)--> If you think a_1 = 7, then by definition a_0 = 42/16, and this is wrong. I don't (want to) know how you get 7 but it's a wrong answer. Good luck.
--> Sorry i meant a(2).

Last edit: 2013-06-15 13:33:06
2013-05-26 11:59:26 Gaurav Parida
Thanks...

PS- Sorry, next time i will try to post such things in forums rather than here...

Last edit: 2013-05-27 10:57:11
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.