CF1941D.Rudolf and the Ball Game

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Rudolf and Bernard decided to play a game with their friends. nn people stand in a circle and start throwing a ball to each other. They are numbered from 11 to nn in the clockwise order.

Let's call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or counterclockwise.

Let's call the clockwise (counterclockwise) distance from player y1y_1 to player y2y_2 the number of transitions clockwise (counterclockwise) that need to be made to move from player y1y_1 to player y2y_2. For example, if n=7n=7 then the clockwise distance from 22 to 55 is 33, and the counterclockwise distance from 22 to 55 is 44.

Initially, the ball is with the player number xx (players are numbered clockwise). On the ii-th move the person with the ball throws it at a distance of rir_i (1≤ri≤n−11 \le r_i \le n - 1) clockwise or counterclockwise. For example, if there are 77 players, and the 22nd player, after receiving the ball, throws it a distance of 55, then the ball will be caught by either the 77th player (throwing clockwise) or the 44th player (throwing counterclockwise). An illustration of this example is shown below.

The game was interrupted after mm throws due to unexpected rain. When the rain stopped, the guys gathered again to continue. However, no one could remember who had the ball. As it turned out, Bernard remembered the distances for each of the throws and the direction for some of the throws (clockwise or counterclockwise).

Rudolf asks you to help him and based on the information from Bernard, calculate the numbers of the players who could have the ball after mm throws.

鲁道夫和伯纳德决定和朋友们玩一个游戏。nn 个人围成一个圆圈,并开始互相抛球。他们按顺时针方向编号为 11 到 nn。

我们称一次“传递”为球从一名玩家传给其相邻玩家的动作。该传递可以是顺时针方向,也可以是逆时针方向。

我们定义玩家 y1y_1 到玩家 y2y_2 的“顺时针距离”(或“逆时针距离”)为:从玩家 y1y_1 出发,沿顺时针方向(或逆时针方向)传递球所需经过的最少传递次数。例如,若 n=7n=7,则玩家 22 到玩家 55 的顺时针距离为 33,而逆时针距离为 44。

初始时,球在编号为 xx 的玩家手中(玩家编号按顺时针顺序)。在第 ii 次传递中,持球者将球沿顺时针或逆时针方向传递 rir_i(其中 1≤ri≤n−11 \le r_i \le n - 1)个位置。例如,若有 77 名玩家,且第 22 号玩家接到球后将其传递 55 个位置,则球将被第 77 号玩家(顺时针传递)或第 44 号玩家(逆时针传递)接住。该示例的图示如下:

由于突发降雨,游戏在进行了 mm 次传递后被迫中断。雨停后,大家重新聚集准备继续游戏,但无人记得此时球在谁手中。结果发现,伯纳德还记得每次传递的距离 rir_i,以及其中部分传递的方向(顺时针或逆时针)。

鲁道夫请你帮忙,根据伯纳德提供的信息,计算出经过 mm 次传递后,球可能在哪些编号的玩家手中。

输入格式

The first line of the input contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case contains three integers n,m,xn, m, x (2≤n≤10002 \le n \le 1000, 1≤m≤10001 \le m \le 1000, 1≤x≤n1 \le x \le n) — the number of players, the number of throws made, and the number of the player who threw the ball first, respectively.

The next mm lines contain information about each throw in order. Each of them contains an integer rir_i (1≤ri≤n−11 \le r_i \le n - 1) — the distance at which the ii-th throw was made, and a symbol cic_i, equal to '0', '1', or '?':

  • if cic_i='0', then the ii-th throw was made clockwise,
  • if cic_i='1', then the ii-th throw was made counterclockwise,
  • if cic_i='?', then Bernard does not remember the direction and the ii-th throw could have been made either clockwise or counterclockwise.

It is guaranteed that the sum n⋅mn \cdot m (nn multiplied by mm) over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含三个整数 n,m,xn, m, x(2≤n≤10002 \le n \le 1000,1≤m≤10001 \le m \le 1000,1≤x≤n1 \le x \le n),分别表示玩家数量、投球次数以及第一个投球的玩家编号。

接下来的 mm 行按顺序描述每次投球的信息。每行包含一个整数 rir_i(1≤ri≤n−11 \le r_i \le n - 1)——表示第 ii 次投球的距离,以及一个字符 cic_i,其值为 '0'、'1' 或 '?':

  • 若 ci=’0’c_i = \text{'0'},则第 ii 次投球为顺时针方向;
  • 若 ci=’1’c_i = \text{'1'},则第 ii 次投球为逆时针方向;
  • 若 ci=’?’c_i = \text{'?'},则 Bernard 不记得投球方向,第 ii 次投球可能为顺时针或逆时针方向。

保证所有测试用例中 n⋅mn \cdot m(即 nn 与 mm 的乘积)之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output two lines.

In the first line, output the number of players kk (1≤k≤n1 \le k \le n) who could have the ball at the end of the game.

In the next line, output kk numbers bib_i (1≤bi≤n1 \le b_i \le n) — the numbers of the players in increasing order. All numbers must be different.

对于每个测试用例,输出两行。

第一行输出最终可能持球的玩家人数 kk(1≤k≤n1 \le k \le n)。

下一行输出 kk 个数 bib_i(1≤bi≤n1 \le b_i \le n),表示这些玩家的编号,按升序排列。所有数字必须互不相同。

输入输出样例

  • 输入#1

    5
    6 3 2
    2 ?
    2 ?
    2 ?
    12 1 2
    3 1
    10 7 4
    2 ?
    9 1
    4 ?
    7 0
    2 0
    8 1
    5 ?
    5 3 1
    4 0
    4 ?
    1 ?
    4 1 1
    2 ?

    输出#1

    3
    2 4 6 
    1
    11 
    4
    3 5 7 9 
    3
    2 3 5 
    1
    3

说明/提示

Below is an illustration of three throws for the first test case. The arrows denote possible throw directions. Players who could have the ball after the throw are highlighted in gray.

以下是第一个测试用例的三次传球示意图。箭头表示可能的传球方向。传球后可能持球的球员以灰色高亮显示。

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

首页