You are given an integer βNβ and a prime number βPβ. Your task is to find the N! modulo P.
The first line contains an integer βTβ, which denotes the number of test cases to be run. Then, the T test cases follow.
The first and only line of each test case contains two integers, βNβ, and βPβ in that order, where N is the integer and P is a prime number.
Output Format :
For each test case, print the βN! % Pβ, as described in the problem statement.
Output for each test case will be printed in a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^9
1 <= P <= 10^9
|N - P| <= 1000
Time Limit: 1 sec
2
5 3
4 5
0
4
Test Case 1:
5! = 5*4*3*2*1 = 120 and and it will give remainder as 0 when divided by 3.
Test Case 2:
4! = 4*3*2*1 = 24 and it will give the remainder 4 when divided by 5.
2
5 11
10 7
10
0
Try to use the brute-force with %p in each iteration.
O(N), where 'N; is the number given in the problem.
We are finding the factorial of βNβ using the loop from β1β to βNβ. Hence, the overall time complexity will be O(N).
O(1).
As we are only using the constant extra space.