luogu#P4684. [IOI 2008] Fish

[IOI 2008] Fish

Problem Description

According to Scheherazade, there is a lake in a faraway desert. Initially, there are FF fish in the lake. Choose the most valuable KK types of gems, and for each of the FF fish, feed it exactly one gem. Note that since KK may be smaller than FF, two or more fish may swallow the same type of gem.

As time goes by, some fish eat other fish. A fish can eat another fish if and only if its length is at least twice the length of the fish being eaten (AA can eat BB if and only if LA2LBL_A \geq 2L_B). There is no rule saying when a fish will eat another fish. Some fish may eat several smaller fish one after another, and some fish may eat none, even if they are able to. When a fish eats a smaller fish, its length does not change, but the gems inside the smaller fish remain intact and move into the bigger fish’s stomach.

According to Scheherazade, if you can find that lake, you will be allowed to catch one fish and take the gems inside its stomach. You want to try your luck, but before setting out, you want to know how many different gem collections you might obtain by catching a fish.

Write a program that, given the length of each fish and the type of gem it initially swallowed, finds the number of different gem collections that can appear in a fish’s stomach, modulo a given integer MM. A collection is defined by the count of each type of gem, and the order of gems does not matter. Any two gems of the same type are indistinguishable.

Input Format

Your program should read the following data from standard input:

  • The first line contains an integer FF, the initial number of fish in the lake.
  • The second line contains an integer KK, the number of gem types. Different gem types are represented by integers from 11 to KK.
  • The third line contains an integer MM.
  • Each of the next FF lines contains two integers separated by a space, describing a fish: its length and the type of gem in its stomach.

Note: In all test cases, each of the KK gem types appears at least once.

Output Format

Your program should output to standard output a single integer between 00 and M1M-1 (inclusive), the number of all possible different gem collections modulo MM, on one line. Note that in solving the problem, the value MM has no purpose other than simplifying computation.

5
3
7
2 2
5 1
8 3
4 1
2 3

4

Hint

Constraints

There are testdata worth 70 points in total where KK does not exceed 7,0007,000. Among these, there are testdata worth 25 points where KK does not exceed 2020.

For all testdata, 1F500,0001 \leq F \leq 500,000, 1KF1 \leq K \leq F, 2M30,0002 \leq M \leq 30,000, 1LX1,000,000,0001 \leq L_X \leq 1,000,000,000.

Sample Explanation

There are 1111 possible collections, so you should output 44, which is 1111 modulo 77. These collections are: $[1] [1,2] [1,2,3] [1,2,3,3] [1,3] [1,3,3] [2] [2,3] [2,3,3] [3]$ and [3,3][3,3]. (For each collection, we list the gems it contains. For example, [2,3,3][2,3,3] contains one type 22 gem and two type 33 gems.)

These collections can be obtained in the following ways:

[1][1]: If you catch the second fish (or the fourth fish) before it eats any other fish.

[1,2][1,2]: If the second fish eats the first fish, it will have one type 11 gem (the one it swallowed initially) and one type 22 gem (obtained from the first fish’s stomach).

[1,2,3][1,2,3]: One possible way is: the fourth fish eats the first fish, then the third fish eats it. If you catch the third fish at this moment, it has one gem of each of these three types in its stomach.

[1,2,3,3][1,2,3,3]: The fourth fish eats the first fish, the third fish eats the fourth fish, the third fish eats the fifth fish, and you catch the third fish.

[1,3][1,3]: The third fish eats the fourth fish, and you catch the third fish.

[1,3,3][1,3,3]: The third fish eats the fifth fish, the third fish eats the fourth fish, and you catch the third fish.

[2][2]: You catch the first fish.

[2,3][2,3]: The third fish eats the first fish, and you catch the third fish.

[2,3,3][2,3,3]: The third fish eats the first fish, the third fish eats the fifth fish, and you catch the third fish.

[3][3]: You catch the third fish.

[3,3][3,3]: The third fish eats the fifth fish, and you catch the third fish.

Translated by ChatGPT 5