#P16301. [蓝桥杯 2026 省 Python C 组] 双人成行

    ID: 18316 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>数学2026蓝桥杯省赛

[蓝桥杯 2026 省 Python C 组] 双人成行

Problem Description

Given a grid with NN rows and MM columns. There are two players. They need to choose two cells as their initial positions, and the two initial positions must be different. There is also an operation sequence SS, where each operation is one of the following four types:

  • U: move up by one cell;
  • D: move down by one cell;
  • L: move left by one cell;
  • R: move right by one cell.

The two players move simultaneously according to the operation sequence SS. For each operation in the sequence, both players try to move one cell in the corresponding direction at the same time. If a player would move out of the grid boundary in that direction, then in this step the player stays in the original cell and does not move.

Now, you need to compute how many different pairs of initial positions can make the two players end up in the same cell after executing the entire operation sequence.

In particular, the pair of initial positions is considered an ordered pair: if the two players' initial positions are (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2), then ((r1,c1),(r2,c2))((r_1, c_1), (r_2, c_2)) and ((r2,c2),(r1,c1))((r_2, c_2), (r_1, c_1)) are considered two different solutions.

Input Format

The input has two lines.

The first line contains two integers N,MN, M.

The second line contains a string SS, representing the operation sequence.

Output Format

Output one integer, representing the number of pairs of initial positions that satisfy the condition.

10 9
UULDURRRDLDLDD
284

Hint

Constraints

  • For 30%30\% of the testdata, N,M≤50N, M \le 50, ∣S∣≤1000|S| \le 1000.
  • For all testdata, N,M≤5000N, M \le 5000, 1≤∣S∣≤1061 \le |S| \le 10^6.

Translated by ChatGPT 5