Fish
Time limit3sMemory limit128 MB
Given fish lengths and gem kinds, count how many distinct gem-count combinations a single fish can ever hold, modulo M, where a fish can eat another only if at least twice as long.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Combinatorics, Tree
- Solved
- No attempts yet
Problem
Far away, in the middle of a desert, there is a lake. Originally the lake held fish. Among the most valuable gemstones on Earth, different kinds were chosen, and each of the fish was made to swallow exactly one gem. Because may be smaller than , several fish may have swallowed gems of the same kind.
As time went by, some fish ate others. One fish can eat another if and only if it is at least twice as long: fish can eat fish exactly when . There is no rule about when a fish decides to eat — a fish may eat several smaller fish one after another, or choose to eat none at all even when it could. When a fish eats a smaller one its own length does not change, and every gem in the smaller fish's stomach passes, undamaged, into the larger fish's stomach.
You are allowed to take a single fish out of the lake and keep all the gems currently in its stomach. Before setting out, you want to know how many different gem combinations you could obtain by catching one fish.
Write a program that, given the length of each fish and the kind of gem it originally swallowed, computes the number of different gem combinations that can end up in the stomach of some fish, modulo a given integer . A combination is determined solely by how many gems of each of the kinds it contains: gems have no order, and two gems of the same kind are indistinguishable.
Input
- The first line contains the integer , the original number of fish in the lake ().
- The second line contains the integer , the number of gem kinds. Kinds are numbered through ().
- The third line contains the integer ().
- Each of the next lines describes one fish with two space-separated integers: the fish's length followed by the kind of gem it originally swallowed ().
It is guaranteed that at least one gem of each of the kinds is present.
Output
Print a single line containing one integer between and inclusive: the number of different possible gem combinations, taken modulo . The value of has no meaning beyond keeping the numbers small.