「JSOI2018」机器人(“Jsoi2018” robot)-js

「JSOI2018」机器人(“Jsoi2018” robot)

• 观察 1：$$\forall (x, y)$$，若 $$a_{(x – 1) \bmod n, y} \ne a_{x, (y – 1) \bmod m}$$，则要么 $$(x, y)$$ 被经过了两次，要么没有被经过。
• 推论 1：$$\forall a, x, y, a_{x, y} = a_{(x + a) \bmod n, (y – a) \bmod m}$$

• 观察 2：第一次走到一个重复点时一定恰好走完了若干个整周期。
• 观察 3：同一周期内，决定第一次走到重复点时刻的与周期内走的顺序无关，只与周期内往下 / 往右走的次数有关。

• 若 $$\min(\alpha_1, \alpha_2) = 0$$.

• 若 $$\min(\alpha_1, \alpha_2) > 0$$

————————

In this problem, for convenience, we change the coordinate range to \ ((0 \ SIM n – 1, 0 \ SIM m – 1) \), and walking can be regarded as any one dimension \ (+ 1 \) in the sense of module

At the same time, if you notice that a position can only pass through once, you can make \ (a {x, y} \) as \ ((x, y) \) whether the position goes downward or upward to facilitate investigation.

Firstly, the number of schemes of this problem is considered. For this kind of grid graph walking problem, it is generally necessary to investigate the special properties of diagonals.

• Observe 1: \ (\ forall (x, y) \), if \ (a {(x – 1) \ bmod n, y} \ ne a_ {x, (Y – 1) \ bmod m} \), then either \ ((x, y) \) is passed twice or not.
• (a \, a \ {, a \, a \, m \, OD = all) \ uy {, a \, a \, M:}

Investigating the combined representation of inference 1 is equivalent to dividing the whole grid graph into several groups. Each group is composed of several complete sub diagonals, and \ (a \) in each group is the same. Naturally, we can further consider the composition of diagonals in each group.

Copy and extend the grid graph to the left and right, and investigate the set composed of the sub diagonal at the beginning of the first line in the group where \ ((0, 0) \) is located at this time.

It is not difficult to find that it is \ (NX (x \ in \ mathbb {n}) \), which corresponds to the value of \ (nx \ bmod m (x \ in \ mathbb {n}) \) in the original grid diagram.

According to peishu theorem, this value must be \ (DX (d = (n, m), 0 \ Le X & lt; \Frac {m} {D}) \), so you can know that the diagonal set of the group in which \ ((0, 0) \) is distributed every \ (D \) spacing from \ (x + y = 0 \).

Extended to all groups, it is found that group \ (I \) is \ (x + y = I + DX (0 \ Le X & lt; \ frac {n + m} {D}) \)

Therefore, this problem only needs to determine how to walk on the diagonal line of \ (x + y = 0 \ SIM D – 1 \) to determine how to walk in the whole graph, that is, how to walk in the first \ (D \) step.

It is legal to consider how to take the first \ (D \) step, if and only if the first point encountered is just after the \ (nm \) step.

First of all, we find that for a point \ (x + y = k \) on a diagonal, we must just take \ (n + m (D \ mid (n + m)) \) steps before returning to the diagonal again. During this period, we must take several complete cycles, then:

• Observation 2: when you reach a repetition point for the first time, you must have completed several whole cycles.
• Observation 3: in the same cycle, the time determining the first time to go to the repetition point is not related to the order of going in the cycle, but only related to the number of going down / right in the cycle.

Assuming that \ (a \) steps are taken down in a cycle, according to Pei Shu’s theorem, it takes \ (\ frac {n} {(n, a)} D \) steps to walk back in the first dimension and \ (\ frac {m} {(m, D – A)} D \) steps similarly in the second dimension. Therefore, the first time to walk back is: \ (\ mathrm {LCM} \ left (\ frac {n} {(n, a)}, \ frac {m} (m, D – a)} \ right) d \), and we need to meet:

In this regard, we investigate each quality factor \ (P \) contained in \ (n, m \), so that \ (\ alpha_1 = \ max_{p ^ I \ mid n} I, \ alpha_2 = \ max_{p ^ I \ mid m} I \)

• If \ (\ min (\ alpha_1, \ alpha_2) = 0 \)

If \ (P \ mid (n, a) \), then \ (\ alpha_1 & gt; 0 \), it can be seen that the left side of the equation must be less than the right side by at least one factor \ (P \), so \ (P \ nmid (n, a) \), similarly \ (P \ nmid (m, D – a) \)

• If \ (\ min (\ alpha_1, \ alpha_2) > 0 \)

Then \ (P \ mid D \), if \ (P \ mid (n, a) \), then \ (P \ mid a \ rightarrow P \ mid (m, D – a) \), so at least one \ (P \) is removed from both items on the left, which must be at least one \ (P \) less than the right

Similarly \ (P \ nmid (m, D – a) \)

To sum up, \ (\ forall P \ mid n \ lor P \ mid m \) always has \ (P \ nmid (n, a), P \ nmid (m, D – a) \), so there is a necessary condition for the above formula to hold \ ((n, a) = (m, D – a) = 1 \)

This is obviously a sufficient condition, so all the schemes, i.e. \ (\ sum \ limits {a} ^ D \ binom {D} {a}[(n, a) = (m, D – a) = 1] \), are considered to return to the original problem.

Firstly, enumerate the legal \ (a \). For each \ ((x, y) (x \ Le a, y \ le d – a) \), if it is within the path of the first cycle, the position of step \ (x + y + 1 \) in all cycles will be determined accordingly. Therefore, it can also be determined that the position of the most front obstacle in these positions is in step \ (w_{x, y} \)

Therefore, there is a simple scheme in which \ (\ RM DP \) is recorded as \ (f {I, J, K} \) in the first cycle, which determines the walking method of the first \ (I + J \) step, and the minimum value of \ (w \) in all the positions currently reached by \ ((I, J) \) in step \ (\ mathcal {o} (TN ^ 5) \)

Through a certain constant optimization, it can be passed, but there is a better solution to this problem.

Since it is a periodic movement, it is natural to decompose the steps that encounter obstacles into: the number of cycles + the number of steps in the first cycle.

Enumerate the number of steps that finally encounter obstacles in the \ (t \) cycle, and find the scheme that \ (f_{i, J} \) considers the first \ (I + J \) step of the first cycle, the \ (I + J \) step is \ ((I, J) \), and the \ (w \) at each position is at least after the \ (T + 1 \) cycle, And \ (g_{i, J} \) is the scheme that determines that in step \ (I + j + 1 \ SIM D \) (the position before going to \ ((a, D – a) \) (I + j + 1 \) is \ ((I, J) \), and the \ (w \) of each position on the path is at least after the \ (t \) cycle.

Enumerate where obstacles are encountered for the first time, and use \ (F, G \) to merge. Complexity \ (\ mathcal {o} (TN ^ 4) \)