Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2232
Directly go to the topic. The answer is a combination of conditions that are extremely wide and wide. It seems that when you see this question, we can see how huge the answer to this question will be, so there will be a tip like % 9937 in the question. For such a large operation, it is okay to take only one step. There are nine cases: (Two Clockwise and counterclockwise rotations plus two adjacent switching locations (four switching locations for one pair, exchange two pairs of two times) and add the last one that is not in the same position). This is troublesome. The calculation result shows that there are 633 possibilities in the two steps, it is sufficient to see how complicated the following situations are.
After a variety of Baidu, I accepted the good methods described below. A certain amount of information is retained, obtained by recursion, and then solved on the information. The information retained is the number of solutions that each robot reaches each position after the nth walk. After putting forward this preliminary plan, we should be able to think that the purpose of this method is to make the square of the four people walk more clean. We should first calculate its features by a robot.
Assume that the layout is as follows (robots A, B, C, and D are standing in sequence ):
"0"-a "1"-B
"2"-c "3"-d
For the first robot a located in "0", the number of solutions in "0" is 1, and the number of solutions in the other three regions is 0, which is obvious, what about the next time? In this case, the number of "0" solutions becomes 1, the number of "1" solutions is 1, and the number of "2" solutions is 1, the number of solutions in "3" is 0. Oh, it turns out that this is the number of solutions that are added each time adjacent to the "X" position and the previous step of the position.
Has: DP [N] [0] = DP [N-1] [0] + dp [N-1] [1] + dp [N-1] [2]; // forgive me for taking DP as the variable name. It looks really pleasing to the eye
DP [N] [1] = DP [N-1] [0] + dp [N-1] [1] + dp [N-1] [3];
DP [N] [2] = DP [N-1] [0] + dp [N-1] [2] + dp [N-1] [3];
DP [N] [3] = DP [N-1] [1] + dp [N-1] [2] + dp [N-1] [3];
After performing the same steps on the robots whose initialization positions are "1", "2", and "3", the total number of solutions for each robot in each region can be calculated at Step n.
With the above preparations, we can use this information to further solve this thorny problem. Let's take a look at the simplest step to analyze it, because we already know the number of solutions for each robot in four positions after one step.
Well, let's start counting. First, we rotate clockwise. At this time, a reaches "1", B reaches "3", and C reaches "0 ", d arrives at "2". Well, this situation occurs, so we have to find out how many solutions A has in "1" in the data in step n = 1, n = 1 in turn when B is in "3", C is in "0", and D is in "2. Multiply them to get the number of solutions in this final state. The next step is the number of solutions corresponding to the remaining eight States. In step n = 1, the total number of these solutions is one.
But the new problem comes out again. Do I have to estimate all the possible situations in each step? In fact, there is a brute force method here, it is to calculate all possible final states. A big deal is to add a few more zeros. We can calculate the total number of final states as 24, that is, 4 !.
After this analysis, the question will be understandable. Click it.
The Code is as follows:
View code
1 # include <cstdio>
2 # include <cstdlib>
3 # include <cmath>
4 # include <cstring>
5 # include <iostream>
6 # include <algorithm>
7 using namespace STD;
8
9 struct statu
10 {
11 int DP [105] [4];
12} A [4]; // location information of the four robots
13
14 void deal (void)
15 {
16 For (INT I = 0; I <4; ++ I)
17 {
18 For (Int J = 1; j <= 100; ++ J)
19 {
20 A [I]. DP [J] [0] = (a [I]. DP [J-1] [0] + A [I]. DP [J-1] [1] + A [I]. DP [J-1] [2]) % 9937;
21 A [I]. DP [J] [1] = (a [I]. DP [J-1] [0] + A [I]. DP [J-1] [1] + A [I]. DP [J-1] [3]) % 9937;
22 A [I]. DP [J] [2] = (a [I]. DP [J-1] [0] + A [I]. DP [J-1] [2] + A [I]. DP [J-1] [3]) % 9937;
23 A [I]. DP [J] [3] = (a [I]. DP [J-1] [1] + A [I]. DP [J-1] [2] + A [I]. DP [J-1] [3]) % 9937;
24}
25}
26}
27
28 int slove (int n)
29 {
30 int sum = 0, T, base [4] = {0, 1, 2, 3 };
31 For (INT I = 0; I <24; ++ I)
32 {
33 t = (a [0]. DP [N] [base [0] % 9937) * (a [1]. DP [N] [base [1] % 9937 );
34 t %= 9937;
35 T * = (a [2]. DP [N] [base [2] % 9937 );
36 t%= 9937;
37 T * = (a [3]. DP [N] [base [3] % 9937 );
38 sum = (sum + T) % 9937;
39 next_permutation (base, base + 4 );
40}
41 return sum;
42}
43
44 int main ()
45 {
46 for (INT I = 0; I <4; ++ I)
47 {
48 A [I]. DP [0] [I] = 1;
49}
50
51 deal ();
52
53 int N;
54 while (scanf ("% d", & N )! = EOF)
55 {
56 printf ("% d \ n", slove (n ));
57}
58
59 return 0;
60}