luogu#P4935. 口袋里的纸飞机

    ID: 3962 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学素数判断,质数,筛法生成函数洛谷月赛

口袋里的纸飞机

Background

Now I have come to the center of my own story, which is hard to describe with words. From this moment on, the lack of language begins to show, because describing anything depends on shared understanding between speakers, and what I experienced was an experience beyond any ordinary life. When the sages described things beyond this world to the public, they often used grand concepts. Chinese Taoist scholars say the heavens have nine layers. The Vedas mention that the land we live on is only one of countless copies. The Eskimos believe that everything hatched from a giant egg. A more suitable metaphor is the so-called Dirac sea, that is, above and outside all space and time. Although it is impossible to describe an infinite entity with limited words, I remembered a part of it, perhaps the most important part:

I saw an infinitely wide sea and an infinitely vast sky, meeting at the horizon infinitely far away. At the very center of my view stood a girl with long purple hair. My identity was different from hers: I was a visitor invited by her, while the girl on the sea was the resident here, or rather, a prisoner. Just as we cannot freely visit a world above our world, she can never have even the slightest intersection with our life. I understood that I would not stay here for long, and she could only have called me here for one reason. Then I heard my own voice echo over the sea, fading into the void:

“I will remember you.”

She smiled at me. The white light lit up once again, and the girl’s figure gradually dissipated as if scorched by invisible flames. I understood that I could not keep this moment, so I cried. What made me cry was not only the eternal farewell, but also pity and remorse for this child who had once accompanied us through endless time.
I felt infinite reverence, infinite sorrow.

— Xi Jiang, “Pocket”.

Problem Description

Given a sequence of length nn, {ai}\{a_i\}, where each number is in the range [1,R][1,R].

For each sequence, you can generate an n×nn\times n grid, where the value in cell (i,j)(i,j) is ai×ajmodPa_i\times a_j \bmod P.

For example, if the sequence is {1,2,3}\{1,2,3\} and P=5P=5, then the generated grid is

1 2 3
2 4 1
3 1 4(因为2*3%5=1,3*3%5=4)

For a grid, define its “fafa value” as the number of distinct values appearing in it. For the grid above, it is 44, i.e. {1,2,3,4}\{1,2,3,4\}.

Now you need to compute the sum of the fafa values over all sequences, and take it modulo 109+710^9+7.

Input Format

The first line contains positive integers n,P,Rn,P,R.

Output Format

Output the answer modulo 109+710^9+7.

2 3 3
15
4 7 5
2845
70 43 22
992103136
500 2011 999980895
767094932

Hint

Explanation for Sample 1:

{ai}={1,1}:
1 1
1 1
(ans=1)
{ai}={1,2}:
1 2
2 1
(ans=2)
{ai}={1,3}:
1 0
0 0
(ans=2)
{ai}={2,1}:
1 2
2 1
(ans=2)
{ai}={2,2}:
1 1
1 1
(ans=1)
{ai}={2,3}:
1 0
0 0
(ans=2)
{ai}={3,1}:
0 0
0 1
(ans=2)
{ai}={3,2}:
0 0
0 1
(ans=2)
{ai}={3,3}:
0 0
0 0
(ans=1)
Total is 15.

It is guaranteed that PP is a prime number with P3P\geq 3.

Constraints

Test Point NN RR PP
1,2 N5N\leq 5 R5R\leq 5 R×R<P20R\times R<P\leq 20
3,4,5,6 N15N\leq 15 R10R\leq 10 R×R<P200R\times R<P\leq 200
7,8 N30N\leq 30 R×R<P500R\times R<P\leq 500
9,10,11,12 N100N\leq 100
13,14,15,16 N300N\leq 300 R109R\leq 10^9 P1000P\leq 1000
17,18,19,20 N500N\leq 500 P5000P\leq 5000

For all data, n500,P5000,R109n\leq 500,P\leq 5000,R\leq 10^9.

Translated by ChatGPT 5