Sorcerers of the Round Table
Time limit2sMemory limit512 MB
Count the circular seatings of the other sorcerers, with the chairman fixed at height n, where neighbors' hat heights differ by at most p and no disliked pair sits adjacent.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Bit manipulation, Divide and conquer, Combinatorics
- Solved
- No attempts yet
Problem
The sorcerers of the round table are holding their privy council again. As always, they began to squabble before the agenda was even presented, and the seating order around the table proved controversial enough.
There are sorcerers, each identified by the height of their pointed hat. The hat heights are pairwise distinct integers from 1 to , and a taller hat means a more senior sorcerer. For aesthetic reasons, the hat heights of two sorcerers seated next to each other must differ by at most .
Some sorcerers dislike others. If sorcerer dislikes sorcerer , then may not sit immediately to the right of . The chairman, whose hat has height , has already sat down at the table. In how many ways can the remaining sorcerers be arranged around the table?
Input
The first line contains three integers , , and (, , ), separated by single spaces. They are the number of sorcerers, the number of dislikes, and the maximum allowed difference in height between adjacent hats.
Each of the next lines contains two integers and (, ), separated by a single space. This means the sorcerer with hat height dislikes the sorcerer with hat height . Each ordered pair appears at most once.
One set of tests worth 16% of the total score has . Another, disjoint set worth 16% has .
Output
Print a single integer: the number of possible arrangements, modulo .
Hint
The sorcerers can sit around the table in one of the six orders: 53124, 53142, 52143, 53412, 52314, 53214.