#P16175. [ICPC 2014 NAIPC] Cheats
[ICPC 2014 NAIPC] Cheats
题目描述
Cosmo is busy playing the little-known latest installment in the Legend of Zelda series of video games, Skyward Wind Mask of Twilight Time. In this game, the player must complete all objectives as the young adventurer Link. However, some objectives must be done before others! Each objective , , has a prerequisite, , which must be completed before . Of course, the first objective (always number 1) has no prerequisite. There are no cycles of dependencies which would cause an objective to indirectly depend on itself.
Like all games, however, this game has hidden cheats. There is one cheat for each objective which allows it to be completed before its prerequisite! However, things still can't be done too much out of order. If objective 's cheat is used, then instead of objective being completed after it's prerequisite , it just has to be completed after it's prerequisite's prerequisite, (unless , in which case it can be completed at any point, since objective 1 has no prerequisite). Furthermore, using multiple cheats too close to each other can lead to unpredictable effects, so each objective can be involved in at most one use of a cheat. In other words, if you use a cheat on objective so that you can complete it before its prerequisite , then you can't use a cheat on , nor on any other objective that has as a prerequisite.
Cosmo would like to complete the game while exploiting at most cheats. In how many different orders can he complete all objectives, subject to these constraints? As this value can be very large, output it modulo .
输入格式
There will be multiple test cases in the input. Each test case will begin with two integers () and (), indicating the number of objectives Cosmo must complete () and the maximum number of cheats he's willing to use (). On the next line will be space-separated integers (), indicating the prerequisite objective for objectives 2, 3, ..., (skipping 1, since objective 1 has no prerequisite). The input will end with a line with two 0s.
输出格式
For each test case, output a single integer, which indicates the number of ways Cosmo can achieve all objectives while using or fewer cheats. Output this number modulo . Do not output any spaces, and do not print any blank lines between answers.
5 1
1 1 5 1
0 0
38
提示
This table lists all of the orders in which Cosmo can achieve all of the objectives for the Sample Input/Output using at most one cheat.
:::align{center}
:::