EASYFACT - Easy Factorials

Finding factorials are easy but they become large quickly that is why Lucky hate factorials. Today he have another task related to factorials.

For a given number n how many ways factorial n can expressed as a sum of two or more consecutive positive integers. Can you help lucky ?

Input

First line contains single integer T < 5001, next T lines followed by an integer N<10^8 and M<10^9.

where M is a prime number.

Output

Print the desired result mod M.

Example

Input:
1
3 7

Output:
1

Explanation:: 3! = 1+2+3 only one way.

Speed Adicts My best time for all cases is 1.57s. Best of Luck have fun:) .


Added by:[Lakshman]
Date:2015-05-13
Time limit:5s-10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 JS-MONKEY

hide comments
2015-05-27 20:24:11 goyal
give some other test cases
2015-05-25 06:56:26 The next big thing
Nice problem.....got ac, but still 4 times slower than you....any tips for speeding up?

--(Lakshman)--> My algorithm is different from others. Fast p***e computation, Fast I/O, less memory usage.

Last edit: 2015-05-25 16:09:30
2015-05-20 18:22:51 tech_phobic
The final answer has to be taken mod with M ?

--Lakshman-->Yes.

Last edit: 2015-05-20 21:10:40
2015-05-20 02:06:26 simararorarox9
Awesome problem. Big Like. (Y)

--Lakshman-->Thanks

Last edit: 2015-05-20 14:12:03
2015-05-20 01:00:32 simararorarox9
Hey [Lakshman] Can you say whether my approach is correct or not. ?

Last edit: 2015-05-20 02:06:58
2015-05-17 06:17:54 saksham agrawal
Why are we entering M??

--Lakshman-->Because answer will not fit into 64 bit integer.

Last edit: 2015-05-17 08:23:20
2015-05-14 13:30:49 Jan Kowalski
Could You perphaps provide correct result for n=8 ?

--Lakshman->No other test case will be provided.

Last edit: 2015-05-14 14:04:49
2015-05-14 12:10:41 [Lakshman]
@RIVU DAS I can't give you the complexity details because it will give some hint of solution.

Last edit: 2015-05-14 16:37:33
2015-05-14 11:00:02 RIVU DAS
Expected complexity for this question??
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.