C - First Query Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 N と長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。

クエリが Q 個与えられるので、与えられた順番に処理してください。 クエリは次の 2 種類のいずれかです。

  • 1 k x : A _ k の値を x に変更する。
  • 2 k : A _ k の値を出力する。

制約

  • 1 \leq N \leq 10 ^ 5
  • 1 \leq Q \leq 10 ^ 5
  • 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
  • どのクエリについても、1\leq k\leq N
  • 1 番目の形式のクエリについて、0\leq x\leq 10 ^ 9
  • 2 番目の形式のクエリが 1 つ以上存在する
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N
Q
\operatorname{query} _ 1
\operatorname{query} _ 2
\vdots
\operatorname{query} _ Q

ただし、\operatorname{query} _ ii 個目のクエリを表しており、次の形式のいずれかで与えられる。

1 k x
2 k

出力

2 番目の形式のクエリの回数を q 回として q 行出力せよ。 j\ (1\leq j\leq q) 行目には、2 番目の形式のクエリのうち j 個目のものに対する答えを出力せよ。


入力例 1

3
1 3 5
7
2 2
2 3
1 3 0
2 3
1 2 8
2 2
2 1

出力例 1

3
5
0
8
1

はじめ、A=(1,3,5) です。

  • 1 つめのクエリにおいて、A=(1,3,5) です。A _ 2=3 なので、3 を出力します。
  • 2 つめのクエリにおいて、A=(1,3,5) です。A _ 3=5 なので、5 を出力します。
  • 3 つめのクエリでは、A _ 3 の値を 0 に変更し、A=(1,3,0) となります。
  • 4 つめのクエリにおいて、A=(1,3,0) です。A _ 3=0 なので、0 を出力します。
  • 5 つめのクエリでは、A _ 2 の値を 8 に変更し、A=(1,8,0) となります。
  • 6 つめのクエリにおいて、A=(1,8,0) です。A _ 2=8 なので、8 を出力します。
  • 7 つめのクエリにおいて、A=(1,8,0) です。A _ 1=1 なので、1 を出力します。

入力例 2

5
22 2 16 7 30
10
1 4 0
1 5 0
2 2
2 3
2 4
2 5
1 4 100
1 5 100
2 3
2 4

出力例 2

2
16
0
0
16
100

入力例 3

7
478 369 466 343 541 42 165
20
2 1
1 7 729
1 6 61
1 6 838
1 3 319
1 4 317
2 4
1 1 673
1 3 176
1 5 250
1 1 468
2 6
1 7 478
1 5 595
2 6
1 6 599
1 6 505
2 3
2 5
2 1

出力例 3

478
317
838
838
176
595
468

Score : 200 points

Problem Statement

You are given an integer N and a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.

Given Q queries, process them in the given order. Each query is of one of the following two kinds:

  • 1 k x : set the value A _ k to x.
  • 2 k : print the value A _ k.

Constraints

  • 1 \leq N \leq 10 ^ 5
  • 1 \leq Q \leq 10 ^ 5
  • 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
  • 1\leq k\leq N for all queries.
  • 0\leq x\leq 10 ^ 9 for all queries of the first kind.
  • There is at least one query of the second kind.
  • All values in the input are integers.

Input

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

N
A _ 1 A _ 2 \ldots A _ N
Q
\operatorname{query} _ 1
\operatorname{query} _ 2
\vdots
\operatorname{query} _ Q

Here, \operatorname{query} _ i denotes the i-th query, given in one of the following formats:

1 k x
2 k

Output

Print q lines, where q is the number of queries of the second kind. The j-th (1\leq j\leq q) line should contain the response to the j-th such query.


Sample Input 1

3
1 3 5
7
2 2
2 3
1 3 0
2 3
1 2 8
2 2
2 1

Sample Output 1

3
5
0
8
1

Initially, A=(1,3,5).

  • For the 1-st query, A=(1,3,5), where A _ 2=3, so 3 should be printed.
  • For the 2-nd query, A=(1,3,5), where A _ 3=5, so 5 should be printed.
  • The 3-rd query sets the value A _ 3 to 0, making A=(1,3,0).
  • For the 4-th query, A=(1,3,0), where A _ 3=0, so 0 should be printed.
  • The 5-th query sets the value A _ 2 to 8, making A=(1,8,0).
  • For the 6-th query, A=(1,8,0), where A _ 2=8, so 8 should be printed.
  • For the 7-th query, A=(1,8,0), where A _ 1=1, so 1 should be printed.

Sample Input 2

5
22 2 16 7 30
10
1 4 0
1 5 0
2 2
2 3
2 4
2 5
1 4 100
1 5 100
2 3
2 4

Sample Output 2

2
16
0
0
16
100

Sample Input 3

7
478 369 466 343 541 42 165
20
2 1
1 7 729
1 6 61
1 6 838
1 3 319
1 4 317
2 4
1 1 673
1 3 176
1 5 250
1 1 468
2 6
1 7 478
1 5 595
2 6
1 6 599
1 6 505
2 3
2 5
2 1

Sample Output 3

478
317
838
838
176
595
468
D - 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
E - Route Map

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 鉄道のとある路線には N 個の駅が存在し、始点から終点に向かって i \, (1 \leq i \leq N) 番目の駅の名前は S_i です。

普通列車は全ての駅に止まりますが、急行列車は全ての駅に止まるとは限りません。具体的には、急行列車は M \, (M \leq N) 個の駅にのみ止まり、j \, (1 \leq j \leq M) 番目に止まる駅の名前は T_j です。
ただし、T_1 = S_1 かつ T_M = S_N、すなわち急行列車は始点と終点の両方に止まることが保証されます。

N 個の駅それぞれについて、その駅に急行列車が止まるかどうか判定してください。

制約

  • 2 \leq M \leq N \leq 10^5
  • N, M は整数
  • S_i \, (1 \leq i \leq N) は英小文字のみからなる 1 文字以上 10 文字以下の文字列
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 かつ T_M = S_N
  • (T_1, \dots, T_M)(S_1, \dots, S_N) から 0 個以上の文字列を選んで取り除き、残った文字列を元の順序で並べることで得られる

入力

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

N M
S_1 \ldots S_N
T_1 \ldots T_M

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、始点から終点に向かって i 番目の駅に急行列車が止まるなら Yes、そうでないなら No と出力せよ。


入力例 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

出力例 1

Yes
No
Yes
No
Yes

入力例 2

7 7
a t c o d e r
a t c o d e r

出力例 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

急行列車が全ての駅に止まることもあります。

Score : 300 points

Problem Statement

There are N stations on a certain line operated by AtCoder Railway. The i-th station (1 \leq i \leq N) from the starting station is named S_i.

Local trains stop at all stations, while express trains may not. Specifically, express trains stop at only M \, (M \leq N) stations, and the j-th stop (1 \leq j \leq M) is the station named T_j.
Here, it is guaranteed that T_1 = S_1 and T_M = S_N, that is, express trains stop at both starting and terminal stations.

For each of the N stations, determine whether express trains stop at that station.

Constrains

  • 2 \leq M \leq N \leq 10^5
  • N and M are integers.
  • S_i (1 \leq i \leq N) is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 and T_M = S_N.
  • (T_1, \dots, T_M) is obtained by removing zero or more strings from (S_1, \dots, S_N) and lining up the remaining strings without changing the order.

Input

Input is given from Standard Input in the following format:

N M
S_1 \ldots S_N
T_1 \ldots T_M

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if express trains stop at the i-th station from the starting station, and No otherwise.


Sample Input 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

Sample Output 1

Yes
No
Yes
No
Yes

Sample Input 2

7 7
a t c o d e r
a t c o d e r

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

Express trains may stop at all stations.

F - Distribution

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 人のすぬけ君が円周上に並んでおり、反時計回りに 1,2,...,N の番号がついています。

i\, (1 \leq i \leq N) 番目のすぬけ君は時刻 t に宝石をもらうと S_i 単位時間後、すなわち時刻 t+S_i にその宝石を (i+1) 番目のすぬけ君に渡します。ただし、(N+1) 番目のすぬけ君とは 1 番目のすぬけ君のことを指すとします。

また、高橋君は時刻 T_ii 番目のすぬけ君に宝石を渡します。

全ての i\, (1 \leq i \leq N) について、i 番目のすぬけ君が初めて宝石をもらう時刻を求めてください。なお、宝石の受け渡しにかかる時間は無視できるものとします。

制約

  • 1 \leq N \leq 200000
  • 1 \leq S_i,T_i \leq 10^9
  • 入力は全て整数である。

入力

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

N
S_1 S_2 \ldots S_N
T_1 T_2 \ldots T_N

出力

N 行出力せよ。i\, (1 \leq i \leq N) 行目には、i 番目のすぬけ君が初めて宝石をもらう時刻を出力すること。


入力例 1

3
4 1 5
3 10 100

出力例 1

3
7
8

時刻 13 までのすぬけ君と高橋君の行動を時系列順に並べます。

時刻 3 : 高橋君が 1 番目のすぬけ君に宝石を渡します。

時刻 7 : 1 番目のすぬけ君が 2 番目のすぬけ君に宝石を渡します。

時刻 8 : 2 番目のすぬけ君が 3 番目のすぬけ君に宝石を渡します。

時刻 10 : 高橋君が 2 番目のすぬけ君に宝石を渡します。

時刻 11 : 2 番目のすぬけ君が 3 番目のすぬけ君に宝石を渡します。

時刻 13 : 3 番目のすぬけ君が 1 番目のすぬけ君に宝石を渡します。

時刻 14 以降も彼らは宝石の受け渡しを行いますが、答えには影響しません。


入力例 2

4
100 100 100 100
1 1 1 1

出力例 2

1
1
1
1

S_iT_i が相異なるとは限らないことに注意してください。


入力例 3

4
1 2 3 4
1 2 4 7

出力例 3

1
2
4
7

あるすぬけくんが同時刻に複数の宝石の受け渡しをする可能性があること、特に高橋くんとすぬけくんの両方から同時に宝石を貰う可能性があることに注意してください。


入力例 4

8
84 87 78 16 94 36 87 93
50 22 63 28 91 60 64 27

出力例 4

50
22
63
28
44
60
64
27

Score : 300 points

Problem Statement

There are N creatures standing in a circle, called Snuke 1, 2, ..., N in counter-clockwise order.

When Snuke i (1 \leq i \leq N) receives a gem at time t, S_i units of time later, it will hand that gem to Snuke i+1 at time t+S_i. Here, Snuke N+1 is Snuke 1.

Additionally, Takahashi will hand a gem to Snuke i at time T_i.

For each i (1 \leq i \leq N), find the time when Snuke i receives a gem for the first time. Assume that it takes a negligible time to hand a gem.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq S_i,T_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 \ldots S_N
T_1 T_2 \ldots T_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the time when Snuke i receives a gem for the first time.


Sample Input 1

3
4 1 5
3 10 100

Sample Output 1

3
7
8

We will list the three Snuke's and Takahashi's actions up to time 13 in chronological order.

Time 3: Takahashi hands a gem to Snuke 1.

Time 7: Snuke 1 hands a gem to Snuke 2.

Time 8: Snuke 2 hands a gem to Snuke 3.

Time 10: Takahashi hands a gem to Snuke 2.

Time 11: Snuke 2 hands a gem to Snuke 3.

Time 13: Snuke 3 hands a gem to Snuke 1.

After that, they will continue handing gems, though it will be irrelevant to the answer.


Sample Input 2

4
100 100 100 100
1 1 1 1

Sample Output 2

1
1
1
1

Note that the values S_i and T_i may not be distinct.


Sample Input 3

4
1 2 3 4
1 2 4 7

Sample Output 3

1
2
4
7

Note that a Snuke may perform multiple transactions simultaneously. Particularly, a Snuke may receive gems simultaneously from Takahashi and another Snuke.


Sample Input 4

8
84 87 78 16 94 36 87 93
50 22 63 28 91 60 64 27

Sample Output 4

50
22
63
28
44
60
64
27
G - Multiply and Rotate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

正の整数 a があります。また、黒板に 1 個の数が 10 進表記で書かれています。
黒板に現在書かれている数を x としたとき、高橋君は次のいずれかの操作を行い、黒板に書かれている数を変化させることができます。

  • x を消し、 xa 倍した数を 10 進表記で新たに書きこむ。
  • x を文字列とみなして、列の末尾の数字を文字列の先頭に移動させる。
    ただし、この操作は x \geq 10 かつ x10 で割り切れないときにしか行えない。

たとえば a = 2, x = 123 であるとき、高橋君は次のいずれかの操作を行うことができます。

  • x を消して、 x \times a = 123 \times 2 = 246 を新たに書きこむ。
  • x を文字列とみなして、123 の末尾の数字である 3 を先頭に移動させる。黒板に書かれている数は 123 から 312 に変化する。

はじめ、黒板には 1 が書かれています。書かれている数を N に変化させるには最小で何回の操作が必要ですか?ただし、どのように操作しても書かれている数を N に変化させられない場合は -1 を出力してください。

制約

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • 入力はすべて整数である。

入力

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

a N

出力

答えを出力せよ。


入力例 1

3 72

出力例 1

4

以下に説明する操作を行うことで、 黒板に書かれている数を 4 回で 1 から 72 に変化させることができます。

  • 1 つ目の操作を行う。黒板に書かれている数は 1 \to 3 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 3 \to 9 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 9 \to 27 に変わる。
  • 2 つ目の操作を行う。黒板に書かれている数は 27 \to 72 に変わる。

3 回以下の操作で 72 に変化させることはできないため、答えは 4 になります。


入力例 2

2 5

出力例 2

-1

どのように操作しても黒板に書かれている数を 5 に変化させることはできません。


入力例 3

2 611

出力例 3

12

適切に操作を選ぶことで、 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 61112 回の操作で黒板に書かれている数を 611 に変化させることができ、これが最小です。


入力例 4

2 767090

出力例 4

111

Score : 400 points

Problem Statement

We have a positive integer a. Additionally, there is a blackboard with a number written in base 10.
Let x be the number on the blackboard. Takahashi can do the operations below to change this number.

  • Erase x and write x multiplied by a, in base 10.
  • See x as a string and move the rightmost digit to the beginning.
    This operation can only be done when x \geq 10 and x is not divisible by 10.

For example, when a = 2, x = 123, Takahashi can do one of the following.

  • Erase x and write x \times a = 123 \times 2 = 246.
  • See x as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123 to 312.

The number on the blackboard is initially 1. What is the minimum number of operations needed to change the number on the blackboard to N? If there is no way to change the number to N, print -1.

Constraints

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a N

Output

Print the answer.


Sample Input 1

3 72

Sample Output 1

4

We can change the number on the blackboard from 1 to 72 in four operations, as follows.

  • Do the operation of the first type: 1 \to 3.
  • Do the operation of the first type: 3 \to 9.
  • Do the operation of the first type: 9 \to 27.
  • Do the operation of the second type: 27 \to 72.

It is impossible to reach 72 in three or fewer operations, so the answer is 4.


Sample Input 2

2 5

Sample Output 2

-1

It is impossible to change the number on the blackboard to 5.


Sample Input 3

2 611

Sample Output 3

12

There is a way to change the number on the blackboard to 611 in 12 operations: 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611, which is the minimum possible.


Sample Input 4

2 767090

Sample Output 4

111