Log inSign up
Problems / B096

Assigning parking slots

300 ptsCases 16Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

A parking lot has parking slots numbered 1 through N standing in a single row. Today N cars numbered 1 through N come into this lot, and one slot takes only one car. So every slot ends up filled with exactly one car.

The rule is that a car goes into the slot with the same number as itself, but a slot right beside that one is allowed too. That is, car K can use slot K-1, slot K or slot K+1. Car 1 has no slot to its left, so it can use only slot 1 and slot 2, and for the same reason car N can use only slot N-1 and slot N.

M of the slots, though, have a charger fitted. A slot with a charger fitted can be used only by the car whose number matches that slot. If slot 4 has a charger fitted, for example, car 4 has to go into slot 4, and no other car can use slot 4.

Write a program that calculates how many ways there are to park all N cars.

For example, suppose N=9 and the slots with a charger fitted are slot 4 and slot 7. Writing the state of the slots as a table gives the following.

Slot123456789
Chargernonenonenonefittednonenonefittednonenone

The places of car 4 and car 7 are settled first, so the slots left over are cut into three groups with the charging slots as the boundaries. The cars in each group cannot leave that group. Counting the number of ways for each group gives the following.

GroupSlots 1 to 3Slots 5 to 6Slots 8 to 9
Number of slots322
Number of ways322

In a group of three slots in a row there are 3 ways, all three cars going into their own slots, the first two cars trading places, and the last two cars trading places. In a group of two slots in a row there are 2 ways, going in as they are and trading places.

The three groups do not affect one another, so the number of ways overall is the number of ways in each group all multiplied together. So the answer in this example is 3 × 2 × 2 = 12.

There are also cases with no slot that has a charger fitted. The slots are then not cut anywhere and slot 1 through slot N stay as one group. A charging slot may on the other hand be slot 1 or slot N. There is no slot outside it then, so there is one group fewer.

Input and Output Examples

Example 1

Input

9
2
4
7

Output

12

Example 2

Input

9
3
2
4
7

Output

4

Example 3

Input

40
0

Output

165580141

Input

  • First the number of parking slots N is given on the first line. N is a natural number from 1 to 40. The number of cars coming in is also N.

  • Next the number of slots with a charger fitted M is given on the second line. M is an integer from 0 to 20, and it is not larger than N.

  • Then M lines follow, each holding the number of one slot with a charger fitted, one per line. Each number is a natural number from 1 to N, they are all different, and they are given in ascending order from the smallest. If M is 0, none of these lines is given.

Output

  • Print the number of ways to park all N cars on one line. The way where every car goes into the slot with its own number always meets the conditions, so the number of ways is always 1 or more. Even when there is only one way, print that number, 1.

Requirements

  • Every slot is filled with exactly one car. No slot may be left empty
  • Car K can use only slot K-1, slot K or slot K+1
  • A slot with a charger fitted is used only by the car with the same number
  • The numbers of the charging slots are given in ascending order
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 16 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 16 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 16 cases to solve this.

Input
Expected output
9
3
2
4
7
4
40
0
165580141
9
2
4
7
12

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 2.0ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal243ms274 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.