luogu#P9707. [KMOI R1] 音波武器

[KMOI R1] 音波武器

Background

In Military Operation on Planet YY, the fleet encountered some difficulties when capturing City pp.

Problem Description

Xiao Yuan’s fleet uses sonic wave weapons to attack, but the opponent’s city walls are made of a special material—Pulin (pinyin), so the sonic attacks are not very effective.

We define the weakening coefficient mm as follows: when a sonic wave with intensity nn is received, after penetrating Pulin it will be weakened to a sonic wave with intensity nmodmn\bmod m.

Now Xiao Yuan fires rl+1r-l+1 sonic waves with intensities l!,(l+1)!,(l+2)!(r1)!,r!l!,(l+1)!,(l+2)!\dots (r-1)!,r!. The opponent uses Pulin with weakening coefficient kk. Xiao Yuan wants to know, after weakening, what the maximum intensity among these sonic waves is.

The symbol !! here denotes factorial: x!=1×2××xx!=1\times 2\times\dots\times x. For example, 4!=1×2×3×44!=1\times2\times3\times4. In particular, 0!=10!=1.

Input Format

Three positive integers l,r,kl,r,k, with the meanings as described above.

Output Format

One integer ansans, representing the maximum intensity.

1 3 4
2
3 6 10
6
100 1000 911451407
911273936

Hint

Sample 11 Explanation

The final intensities are 1,2,21,2,2, so output 22.

Sample 22 Explanation

The final intensities are 6,4,0,06,4,0,0, so output 66.

Constraints

For 20%20\% of the testdata: 1lr201\le l\le r\le 20, 1k1021\le k\le 10^2.

For 50%50\% of the testdata: 1lr20001\le l\le r\le 2000, 1k1051\le k\le 10^5.

For 100%100\% of the testdata: 1lr2×1061\le l\le r\le 2\times 10^6, 1k10121\le k\le 10^{12}.

Translated by ChatGPT 5