#P9774. [HUSTFC 2023] 新取模运算

[HUSTFC 2023] 新取模运算

Problem Description

In this problem, we define a new operator \oplus, called the new modulo operation.

When computing xyx \oplus y, if xx is not a multiple of yy, the result is the remainder of xx divided by yy. Otherwise, keep dividing xx by yy until xx is no longer a multiple of yy. Let it be xx', and then take the remainder of xx' divided by yy. For example, 45=44\oplus 5=4, 205=420\oplus 5=4, 1005=4100\oplus 5=4.

Given a prime pp, there are multiple queries. In each query, an integer nn is given, and you need to compute the value of n!pn!\oplus p. Here, n!n! is the factorial of nn, i.e. the product of all positive integers less than or equal to nn.

Input Format

The first line contains two integers T (1T105)T\ (1\le T\le 10^5) and p (2p106)p\ (2\le p\le 10^6), representing the number of queries and the given prime.

The next TT lines each contain an integer n (1n1018)n\ (1\le n\le 10^{18}), with the meaning as described above.

Output Format

For each query, output one line containing one integer, the value of n!pn!\oplus p.

3 7
11
45
14
4
1
2

2 10007
1919
810
3152
3679

Hint

Translated by ChatGPT 5