eolymp
bolt
Try our new interface for solving problems

Fish

It was told by Scheherazade that far away, in the middle of the desert, there is a lake. Originally this lake had \textbf{F} fish in it. \textbf{K} different kinds of gemstones were chosen among the most valuable on Earth, and to each of the \textbf{F} fish exactly one gem was given for it to swallow. Note, that since \textbf{K} might be less than \textbf{F}, two or more fish might swallow gems of the same kind. As time went by, some fish ate some of the other fish. One fish can eat another if and only if it is at least twice as long (fish \textbf{A} can eat fish \textbf{B} if and only if \textbf{LA} >= \textbf{2 * LB}). There is no rule as to when a fish decides to eat. One fish might decide to eat several smaller fish one after another, while some fish may decide not to eat any fish, even if they can. When a fish eats a smaller one, its length doesn’t change, but the gems in the stomach of the smaller fish end up undamaged in the stomach of the larger fish. Scheherazade has said that if you are able to find the lake, you will be allowed to take out one fish and keep all the gems in its stomach for yourself. You are willing to try your luck, but before you head out on the long journey, you want to know how many different combinations of gems you could obtain by catching a single fish. Write a program that given the length of each fish and the kind of gemstone originally swallowed by each fish, finds the number of different combinations of gems that can end up in the stomach of any fish, modulo some given integer \textbf{M}. A combination is defined only by the number of gems from each of the \textbf{K} kinds. There is no notion of order between gems, and any two gems of the same kind are indistinguishable. \textbf{1} <= \textbf{F} <= \textbf{500,000} The original number of fish in the lake. \textbf{1} <= \textbf{K} <= \textbf{F} The number of different gemstone kinds. \textbf{2} <= \textbf{M} <= \textbf{30,000} \textbf{1} <= \textbf{LX} <= \textbf{1,000,000,000} The length of fish \textbf{X}. \InputFile Your program must read from the standard input the following data: \begin{enumerate} \item Line \textbf{1} contains the integer \textbf{F}, the original number of fish in the lake. \item Line \textbf{2} contains the integer \textbf{K}, the number of kinds of gemstones. The kinds of gemstones are represented by integers \textbf{1} to \textbf{K}, inclusive. \item Line \textbf{3} contains the integer \textbf{M}. \item Each of the following F lines describes one fish using \textbf{2} integers separated by a single space: the length of the fish followed by the kind of gemstone originally swallowed by that fish. \end{enumerate} \textbf{Note}: For all test cases used for evaluation, it is guaranteed that there is at least one gemstone from each of the \textbf{K} kinds. \OutputFile Your program must write to the standard output a single line containing one integer between \textbf{0} and \textbf{M‐1} (inclusive): the number of different possible combinations of gemstones modulo \textbf{M}. Note that for solving the task, the value of \textbf{M} has no importance other than simplifying computations. For a number of tests, worth a total of \textbf{70} points, \textbf{K} will not exceed \textbf{7,000}. Also, for some of these tests, worth a total of \textbf{25} points, \textbf{K} will not exceed \textbf{20} .
Time limit 9 seconds
Memory limit 64 MiB
Input example #1
5
3
7
2 2
5 1
8 3
4 1
2 3
Output example #1
4