#P9707. Maximum Damped Wave Intensity
Maximum Damped Wave Intensity
Maximum Damped Wave Intensity
Xiao Yuan's fleet uses sonic weapons to attack, but the enemy's walls are made of a special material called Pring which weakens the effect of the sonic waves. When a wave with intensity (n) passes through Pring, its intensity is reduced to (n \bmod m), where (m) is the weakening coefficient.
Now, Xiao Yuan fires (r-l+1) waves with intensities (l!), ((l+1)!), ((l+2)!), (\dots), (r!) respectively. Given that the wall weakens each wave using a damping coefficient (k) (i.e. it transforms an intensity (n) into (n \bmod k)), your task is to determine the maximum wave intensity after the weakening.
Recall that the factorial is defined as (n! = 1 \times 2 \times \dots \times n) with the special case (0! = 1).
inputFormat
Input consists of three space-separated integers: (l), (r), and (k), where (0 \leq l \leq r) and (k \geq 1).
outputFormat
Output a single integer representing the maximum value among (n! \bmod k) for all (n) such that (l \leq n \leq r).
sample
1 5 7
6