C - Takahashi's Secret

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君には N 人の友達がいます。N 人の友達はそれぞれ、友達 1 、友達 2\ldots 、友達 N というあだ名で呼ばれています。

ある日、高橋君はある恥ずかしい秘密を、友達の一人である友達 X に知られてしまいました。
i = 1, 2, \ldots, N について、友達 i が高橋君の秘密を知ったとき、友達 A_i がまだ高橋君の秘密を知らなければ、友達 i は高橋君の秘密を友達 A_i にも教えてしまいます。

高橋君の秘密は最終的に何人の友達に知られることになるでしょうか?

制約

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N X
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

4 2
3 1 1 2

出力例 1

3

高橋君の秘密は以下の流れで友達 1 、友達 2 、友達 33 人に知れ渡ります。

  • ある日、高橋君は秘密を友達 2 に知られてしまいました。
  • 秘密を知った友達 2 は、その秘密を友達 1 に教えます。
  • 秘密を知った友達 1 は、その秘密を友達 3 に教えます。

高橋君の秘密は最終的に 3 人の友達に知られることになるため、3 を出力します。


入力例 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

出力例 2

7

Score : 200 points

Problem Statement

Takahashi has N friends. They have nicknames: Friend 1, Friend 2, \ldots, Friend N.

One day, Takahashi accidentally let one of his friends, Friend X, learn his shameful secret.
For each i = 1, 2, \ldots, N, when Friend i learns the secret, he/she will share it with Friend A_i, if Friend A_i has not already learned it.

How many of Takahashi's friends will learn the secret in the end?

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4 2
3 1 1 2

Sample Output 1

3

Takahashi's secret will be learned by Friend 1, Friend 2, and Friend 3, as follows.

  • One day, Takahashi let Friend 2 learn the secret.
  • Friend 2 shares it with Friend 1.
  • Friend 1 shares it with Friend 3.

In the end, three of his friends learn the secret, so we print 3.


Sample Input 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

Sample Output 2

7
D - Integer Division Returns

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lceil \dfrac{X}{10} \right\rceil を出力してください。
ここで、\left\lceil a \right\rceila 以上の整数のうち最小のものを意味します。

制約

  • -10^{18} \leq X \leq 10^{18}
  • X は整数

入力

入力は以下の形式で標準入力から与えられる。

X

出力

\left\lceil \dfrac{X}{10} \right\rceil を整数として出力せよ。


入力例 1

27

出力例 1

3

\frac{27}{10} = 2.7 以上の整数は 3, 4, 5, \dots です。この中で一番小さい整数は 3 なので、\left \lceil \frac{27}{10} \right \rceil = 3 となります。


入力例 2

-13

出力例 2

-1

\frac{-13}{10} = -1.3 以上の整数は、全ての正整数および 0, -1 です。この中で一番小さい整数は -1 なので、\left \lceil \frac{-13}{10} \right \rceil = -1 となります。


入力例 3

40

出力例 3

4

\frac{40}{10} = 4 以上の整数で一番小さい整数は 4 自身です。


入力例 4

-20

出力例 4

-2

入力例 5

123456789123456789

出力例 5

12345678912345679

Score: 200 points

Problem Statement

Given an integer X between -10^{18} and 10^{18}, inclusive, print \left\lceil \dfrac{X}{10} \right\rceil.
Here, \left\lceil a \right\rceil denotes the smallest integer not less than a.

Constraints

  • -10^{18} \leq X \leq 10^{18}
  • X is an integer.

Input

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

X

Output

Print \left\lceil \dfrac{X}{10} \right\rceil as an integer.


Sample Input 1

27

Sample Output 1

3

The integers not less than \frac{27}{10} = 2.7 are 3, 4, 5, \dots. Among these, the smallest is 3, so \left \lceil \frac{27}{10} \right \rceil = 3.


Sample Input 2

-13

Sample Output 2

-1

The integers not less than \frac{-13}{10} = -1.3 are all positive integers, 0, and -1. Among these, the smallest is -1, so \left \lceil \frac{-13}{10} \right \rceil = -1.


Sample Input 3

40

Sample Output 3

4

The smallest integer not less than \frac{40}{10} = 4 is 4 itself.


Sample Input 4

-20

Sample Output 4

-2

Sample Input 5

123456789123456789

Sample Output 5

12345678912345679
E - Rotation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。

以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。

  • 1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。
  • 2 x: Sx 番目の文字を出力する。

制約

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S は英小文字からなる。
  • 2 x の形式のクエリが 1 個以上与えられる。
  • N,Q,x はすべて整数。

入力

入力は以下の形式で標準入力から与えられる。

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

それぞれのクエリは以下の形式で与えられる。ここで、t1 または 2 である。

t x

出力

2 x の形式の各クエリについて、答えを一行に出力せよ。


入力例 1

3 3
abc
2 2
1 1
2 2

出力例 1

b
a

1 個目のクエリのとき、Sabc なので 2 文字目の b を出力します。 2 個目のクエリのとき、Sabc から cab に変わります。 3 個目のクエリのとき、Scab なので 2 文字目の a を出力します。


入力例 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

出力例 2

c
u
c
u

Score : 300 points

Problem Statement

You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.

Process Q queries. Each query is of one of the following two types.

  • 1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.
  • 2 x: Print the x-th character of S.

Constraints

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S consists of lowercase English letters.
  • At least one query in the format 2 x.
  • N, Q, x are all integers.

Input

Input is given from Standard Input in the following format:

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in the following format, where t is 1 or 2:

t x

Output

For each query in the format 2 x, print the answer in a single line.


Sample Input 1

3 3
abc
2 2
1 1
2 2

Sample Output 1

b
a

In the 1-st query, S is abc, so the 2-nd character b should be printed. In the 2-nd query, S is changed from abc to cab. In the 3-rd query, S is cab, so the 2-nd character a should be printed.


Sample Input 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

Sample Output 2

c
u
c
u
F - Belt Conveyor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j}U, D, L, R のいずれかです。

あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。

あなたは現在 (i,j) にいるとする。
G_{i,j}U であり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j}D であり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j}L であり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j}R であり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。

操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1 を出力してください。

制約

  • 1 \leq H, W \leq 500
  • G_{i,j}U, D, L, R のいずれかである。
  • H, W は整数である。

入力

入力は以下の形式で標準入力から与えられる。

H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}

出力

操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。

i j

また、無限に動き続ける場合は -1 を出力せよ。


入力例 1

2 3
RDU
LRU

出力例 1

1 3

あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。


入力例 2

2 3
RRD
ULL

出力例 2

-1

あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1 を出力します。


入力例 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

出力例 3

9 5

Score : 300 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U, D, L, or R.

You are initially at (1,1). You repeat the following operation until you cannot make a move.

Let (i,j) be the square you are currently at.
If G_{i,j} is U and i \neq 1, move to (i-1,j).
If G_{i,j} is D and i \neq H, move to (i+1,j).
If G_{i,j} is L and j \neq 1, move to (i,j-1).
If G_{i,j} is R and j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.

Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.

Constraints

  • 1 \leq H, W \leq 500
  • G_{i,j} is U, D, L, or R.
  • H and W are integers.

Input

Input is given from Standard Input in the following format:

H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}

Output

If you end up at (i, j), print it in the following format:

i j

If you indefinitely repeat moving, print -1.


Sample Input 1

2 3
RDU
LRU

Sample Output 1

1 3

You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).


Sample Input 2

2 3
RRD
ULL

Sample Output 2

-1

You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1 should be printed in this case.


Sample Input 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

Sample Output 3

9 5
G - Marking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

0 から N-1 までの番号がつけられた N 個のマスが並んでいます。 今から、すぬけくんが以下の手順に従って全てのマスに印をつけていきます。

  1. マス 0 に印をつける。
  2. 次の i - iii の手順を N−1 回繰り返す。
    1. 最後に印をつけたマスの番号を A としたとき、変数 x(A+D) \bmod N で初期化する。
    2. マス x に印が付いている限り、 x(x+1) \bmod N に更新することを繰り返す。
    3. マス x に印をつける。

すぬけくんが K 番目に印をつけるマスの番号を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 10^5
  • 1\leq K\leq N \leq 10^9
  • 1\leq D \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_ii 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N D K

出力

T 行出力せよ。

i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。


入力例 1

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5

出力例 1

0
2
1
3
0
3
1
4
2

N=4,D=2 のとき、すぬけくんは以下のように印をつけていきます。

  1. マス 0 に印をつける。
  2. (1回目) x=(0+2)\bmod 4=2 と初期化する。マス 2 は印がついていないので、印をつける。
    (2回目) x=(2+2)\bmod 4=0 と初期化する。マス 0 は印がついているので、x=(0+1)\bmod 4=1 と更新する。マス 1 は印がついていないので、印をつける。
    (3回目) x=(1+2)\bmod 4=3 と初期化する。マス 3 は印がついていないので、印をつける。

Score : 400 points

Problem Statement

There are N squares indexed 0 through (N-1) arranged in a line. Snuke is going to mark every square by the following procedure.

  1. Mark square 0.
  2. Repeat the following steps i - iii (N-1) times.
    1. Initialize a variable x with (A+D) \bmod N, where A is the index of the square marked last time.
    2. While square x is marked, repeat replacing x with (x+1) \bmod N.
    3. Mark square x.

Find the index of the square that Snuke marks for the K-th time.

Given T test cases, find the answer for each of them.

Constraints

  • 1\leq T \leq 10^5
  • 1\leq K\leq N \leq 10^9
  • 1\leq D \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N D K

Output

Print T lines.

The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.


Sample Input 1

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5

Sample Output 1

0
2
1
3
0
3
1
4
2

If N=4 and D=2, Snuke marks the squares as follows.

  1. Mark square 0.
  2. (1-st iteration) Let x=(0+2)\bmod 4=2. Since square 2 is not marked, mark it.
    (2-nd iteration) Let x=(2+2)\bmod 4=0. Since square 0 is marked, let x=(0+1)\bmod 4=1. Since square 1 is not marked, mark it.
    (3-rd iteration) Let x=(1+2)\bmod 4=3. Since square 3 is not marked, mark it.