AT_abc474_g.LRUD Moving 2

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given positive integers NN and KK.

There is an N×NN\times N grid. The cell at the rr-th row from the top and the cc-th column from the left is denoted as cell (r,c)(r,c).

Initially, a piece is placed on cell (1,1)(1,1).

You will perform the following operation exactly N2−1N^2-1 times so that the piece ends up in cell (N,N)(N,N):

  • Move the piece one cell to a cell vertically or horizontally adjacent to the cell it is currently on.

Here, over the course of the movement, each of the N2N^2 cells must be visited exactly once. Cell (1,1)(1,1), where the piece is initially placed, is considered visited.

Determine whether there exists a sequence of operations where the piece moves one cell to the right exactly KK times, and if it exists, find one such sequence.

You are given TT test cases; solve each of them.

给你两个正整数 NN 和 KK。

有一个 N×NN \times N 的网格。从上往下第 rr 行、从左往右第 cc 列的格子记为格子 (r,c)(r,c)。

初始时,一枚棋子被放置在格子 (1,1)(1,1) 上。

你需要恰好执行 N2−1N^2-1 次如下操作,使得棋子最终位于格子 (N,N)(N,N):

  • 将棋子向当前所在格子的上、下、左、右四个相邻格子之一移动一格(即仅允许垂直或水平方向的单位移动)。

在整个移动过程中,N2N^2 个格子中的每一个都必须恰好被访问一次。初始位置 (1,1)(1,1) 也被视为已访问。

请判断:是否存在一种操作序列,使得棋子恰好向右移动 KK 次?若存在,请构造出这样一种序列。

你将收到 TT 组测试数据,请对每组数据分别求解。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each test case is given in the following format:

NN KK

输入从标准输入中以如下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例以如下格式给出:

NN KK

输出格式

Output the answers for the test cases in order, separated by newlines.

For each test case, if there is no sequence of operations satisfying the condition, output No.

If there exists a sequence of operations satisfying the condition, output it in the following format:

Yes
S1S2…SN2−1S_1S_2\dots S_{N^2-1}

Here, SkS_k represents the kk-th move, and is one of the following:

  • Sk=S_k= L if the piece moves one cell to the left
  • Sk=S_k= R if the piece moves one cell to the right
  • Sk=S_k= U if the piece moves one cell up
  • Sk=S_k= D if the piece moves one cell down

If there are multiple sequences of operations satisfying the condition, any of them will be accepted.

按测试用例的顺序输出答案,各答案之间用换行符分隔。

对于每个测试用例,若不存在满足条件的操作序列,则输出 No。

若存在满足条件的操作序列,则按如下格式输出:

Yes
S1S2…SN2−1S_1S_2\dots S_{N^2-1}

其中,SkS_k 表示第 kk 步操作,取值为以下之一:

  • Sk=S_k= L:表示棋子向左移动一格;
  • Sk=S_k= R:表示棋子向右移动一格;
  • Sk=S_k= U:表示棋子向上移动一格;
  • Sk=S_k= D:表示棋子向下移动一格。

若存在多个满足条件的操作序列,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    3 4
    2 1
    5 10

    输出#1

    Yes
    RRDLLDRR
    No
    Yes
    RRRRDDDLLLURRULLLDDDRRRR

说明/提示

Sample 1 Explanation:
Consider the first test case.

By moving from cell (1,1)(1,1) in order to cells (1,2),(1,3),(2,3),(2,2),(2,1),(3,1),(3,2),(3,3)(1,2),(1,3),(2,3),(2,2),(2,1),(3,1),(3,2),(3,3), you can move one cell to the right four times and reach cell (3,3)(3,3).

Constraints

  • 1≤T≤5×1031\le T\le 5\times 10^3
  • 2≤N≤1032\le N\le 10^3
  • 0≤K≤N2−10\le K\le N^2-1
  • The sum of N2N^2 over all test cases is at most 10610^6.
  • All input values are integers.

样例 1 解释:
考虑第一个测试用例。

通过从单元格 (1,1)(1,1) 出发,依次经过单元格 (1,2)(1,2)、(1,3)(1,3)、(2,3)(2,3)、(2,2)(2,2)、(2,1)(2,1)、(3,1)(3,1)、(3,2)(3,2)、(3,3)(3,3),你可以向右移动四次,最终到达单元格 (3,3)(3,3)。

约束条件

  • 1≤T≤5×1031\le T\le 5\times 10^3
  • 2≤N≤1032\le N\le 10^3
  • 0≤K≤N2−10\le K\le N^2-1
  • 所有测试用例的 N2N^2 之和不超过 10610^6。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页