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

    ID: 11014 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2023O2优化高校校赛

[HUSTFC 2023] 新取模运算

Problem Description

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

When computing x⊕yx \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 x′x', and then take the remainder of x′x' divided by yy. For example, 4⊕5=44\oplus 5=4, 20⊕5=420\oplus 5=4, 100⊕5=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 (1≤T≤105)T\ (1\le T\le 10^5) and p (2≤p≤106)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 (1≤n≤1018)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