1. Problem
Assume that there are n seats on the World Cup watching platform, and visitors come here to occupy space freely. Generally, the first consideration of a visitor is that there are no seats on both sides. The second consideration is that there are no seats on one side, you can only select a seat with people on both sides. Suppose there are n tourists standing here in sequence, and each person chooses his/her own seat according to the above rules. How many kinds of sequence are there for the n tourists standing here?
Input description:There are multiple groups of test data, each of which contains a positive integer N (0 <n <1000000 ).
Output description:For each group of data, because the answer may be large, the output is: Answer % 1000000007
2. Analysis
Note that the sequence of audience visits is fixed and the venue is circular.
Analysis: the first (first few) audience must select the positions of no one on both sides, the second (next) audience must select the one with no one, and the third (last) Audience) you can only select seats on both sides. Therefore, we can arrange the N people by batch, that is, first arrange the first batch, then arrange the second batch, and finally arrange the third batch.
Definition: When the seats are {persons, no persons}, the four seats are called TB modules. Each TB module is separated by two empty seats. N indicates the number of seats, TB indicates the number of TB modules, and interval indicates the number of intervals.
First Batch:After the first audience is seated, ensure that at least one of the remaining empty seats is available.
Second Batch:There are only seats with no one on one side. The number of seats is twice that of the TB module and the number of TB modules. The total number is 2 MB * m! .
Third Batch:Arrange for C seats for both sides. Obviously, there are a total of C! .
We will discuss the number of TB modules. For each case, there are: Pi = (N * cintervaltb * (interval-1 )!) * (2 TB * TB !) * (N-interval-TB )!) Order. Where (N * cintervaltb * (interval-1 )!) For the first batch, (2 TB * TB !) For the second batch, (n-interval-TB )!) The third batch. For the first batch, there are a total of interval intervals, of which TB modules, so there are a total of cintervaltb seating interval arrangements, the first person has selected the location, so other (interval-1) total (interval-1 )! So the first batch has n * cintervaltb * (interval-1 )! . For the second batch with a total of TB modules, there are a total of 2 TB order, TB individual total TB! So there are 2 TB * TB in total! . The remaining third batch has n-interval-TB seats, so there are (n-interval-TB )! .
The sum of the number of sequential values is the final result. If n is an odd number, the minimum value of TB is 1. Otherwise, the minimum value of TB is 0.
Example 1:Assume there are 8 seats,
(1) Case 1:The TB module does not exist. There are eight choices for the first person. When the first person is selected, the other three seats are also fixed (for example, if the first person chooses location 1, the other three seats must be 3, 5, 7), and the next three people can have 3! . Therefore, we can obtain 8 * C40 * 3 in the first case of the first batch of people! . If the TB module does not exist, the second batch of scheduling does not exist. The number of seats to be arranged in the third batch is 4, totally 4! . A total of P1 = 8*3 is possible! * 4! .
(2) Case 2:There are two TB modules. Simple analysis shows that there is no TB module. At this time, the number of intervals is (8-3*2)/2 + 2 = 3, that is, one interval is 2 and the other is 2. When the first person is fixed, there are a total of c32 allocation schemes at intervals. In this case, there are 8 * c32 * 2! . A total of 22*2 persons are available in the second batch! Order, the third batch of people has a total of 3! . Case 2: P2 = (8 * c32 * 2 !) * (22*2 !) * (3 !) .
(3) Case 3:There are 4 TB modules. Obviously, 8 is not possible, so the first batch of persons are handled in different situations.
In summary, when the number of seats is 8, there are a total of P1 + p2 orders.
Note: in each case, two TB modules are added until the upper limit of the TB module is reached, and the operation is terminated.
Example 2:Assume there are nine seats,
Scenario 1:One TB module exists. The number of separated seats is (9-3*1)/2 + 1 = 4, that is, the number of three separated seats is 1, and the number of separated seats is 2. When the first person chooses a total of c41 allocation schemes, the total number of allocation schemes is 9 * c41 * 3! . Apparently there are 21*1 people in the second batch! In this case, there are 4 in the third batch! . In this case, a total of P3 = (9 * c41 * 3 !) * (21*1 !) * (4 !) .
Scenario 2:There are 3 TB modules. The number of intervals is (9-3*3)/2 + 3 = 3, that is, the number of seats with 0 intervals is 2 for 1 and 3. When the first person selects a total of 33 c33 allocation schemes, the first batch has 9 * c33 * 2! . Easy to get. There are 23*3 people in the second batch! Order, the third batch of people has a total of 3! . Case 2: P4 = (9 * c33 * 2 !) * (23*3 !) * (3 !) .
Case 3:Obviously, there are no 5 TB modules. Stops running.
In summary, when the number of seats is 9, there are a total of P = P3 + P4 orders.
It can be obtained from Example 1 and Example 2. When the number of seats is an odd or even number, the situation is different. In the same case, two TB modules are incremented in each case.
3. Implementation
N indicates the number of seats, TB indicates the number of TB modules, interval indicates the number of intervals, and num indicates the order of all audience, in each case, Pi = (N * cintervaltb * (interval-1 )!) * (2 TB * TB !) * (N-interval-TB )!).
Pseudocode:
Input:N
Output:Num
Num = 0;
If n is odd
TB = 1;
Else
TB = 0;
Interval = (n-3 * TB)/2 + Tb;
While n ≥ 3 * TB
P = (N * cintervaltb * (interval-1 )!) * (2 TB * TB !) * (N-interval-TB )!);
Num + = P;
TB + = 2;
Interval --;
Return num;
4. Legacy issues
When you only need to retain the remainder, the remainder of the factorial can be obtained before each multiplication, that is, (a * B) % C = (a % C) * (B % C )) % C. But how does CEN % C solve this problem? Waiting for the advanced people to give advice ......