A - Good Bye, A527!

Time Limit: 1 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

Morgorithm は延世大学で唯一の競技プログラミングサークルです。第1工学館の A527 号室を占拠し、部室として使っています。しかし、来年からは A527 号室を部室として使えなくなるそうです!

ジュウォンは新しい部室に使える場所を早く見つけようとしています。まず、延世大学内のすべての部屋を見て回り、部室に使えそうな N 個の候補を見つけました。そして、それぞれの部屋の名前と広さを記録しました。

次に、ジュウォンは学科の事務室や管理部門などに連絡し、さまざまな用途ですでに予約されていて使用できない 10 個の部屋の一覧を入手しました。

ジュウォンは候補のうち、この一覧に含まれていない部屋の中で最も広い部屋を選び、無断で占拠して部室として使おうとしています。最も広い部屋が複数ある場合は、そのうちどれを選んでもかまいません。

ジュウォンが選べる部屋を求めてください。

制約

  • 1 \le N \le 100
  • 1 \le t_i \le 100
  • 1 \le |s_i|, |w_i| \le 10
  • 部屋の名前は英大文字と数字のみからなる。
  • 候補の部屋の名前 s_i は互いに異なる。
  • 使用できない部屋の名前 w_i は互いに異なる。
  • 使用できる候補の部屋が少なくとも 1 個存在する。
  • N およびすべての t_i は整数である。

入力

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

N
s_1 t_1
\vdots
s_N t_N
w_1
\vdots
w_{10}

s_i と t_i は、それぞれ i 番目の候補の部屋の名前と広さを表す。w_i は使用できない部屋の名前を表す。

出力

使用できる候補の部屋のうち、広さが最大の部屋の名前を出力せよ。そのような部屋が複数ある場合は、どれを出力してもよい。


入力例 1

4
A527 100
B101 50
C202 80
D303 70
A527
A528
B201
C404
D915
AUDITORIUM
LIBRARY
BBALJAM
BAEKYANGRO
KOREAUNIV

出力例 1

C202

表示言語

/ /

Score : 100 points

Problem Statement

Morgorithm is Yonsei University's only competitive programming club. It has occupied room A527 in Engineering Building 1 and uses it as its club room. However, starting next year, A527 will no longer be available as a club room!

Juwon wants to quickly find a new place for the club. After looking around all the rooms at Yonsei University, Juwon has found N candidate rooms suitable for use as a club room and recorded each room's name and size.

Juwon then contacted department offices and administrative teams and obtained a list of 10 rooms that are unavailable because they have already been reserved for various purposes.

Among the candidate rooms that are not on this list, Juwon wants to choose a room of maximum size, occupy it without permission, and use it as the club room. If several such rooms have the maximum size, Juwon may choose any of them.

Find a room that Juwon can choose.

Constraints

  • 1 \le N \le 100
  • 1 \le t_i \le 100
  • 1 \le |s_i|, |w_i| \le 10
  • Each room name consists only of uppercase English letters and digits.
  • The candidate room names s_i are pairwise distinct.
  • The unavailable room names w_i are pairwise distinct.
  • At least one candidate room is available.
  • N and all t_i are integers.

Input

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

N
s_1 t_1
\vdots
s_N t_N
w_1
\vdots
w_{10}

Here, s_i and t_i are the name and size of the i-th candidate room, respectively. Each w_i is the name of an unavailable room.

Output

Print the name of an available candidate room of maximum size. If there are multiple such rooms, you may print any of them.


Sample Input 1

4
A527 100
B101 50
C202 80
D303 70
A527
A528
B201
C404
D915
AUDITORIUM
LIBRARY
BBALJAM
BAEKYANGRO
KOREAUNIV

Sample Output 1

C202

表示言語

/ /

Score : 100 points

문제

모르고리즘은 연세대 유일의 PS 동아리로, 제1공학관 A527호를 점거하여 동아리방으로 쓰고 있다. 그런데 내년부터는 A527호를 동아리방으로 쓸 수 없다고 한다!

주원이는 빠르게 동아리방으로 쓸 수 있는 장소를 찾고자 한다. 먼저, 주원이는 연세대 안에 있는 모든 방을 둘러보며 동아리방으로 쓸 만한 N개의 후보를 찾았다. 주원이는 각 후보의 방 이름과 방의 크기를 적어 두었다.

다음으로 주원이는 과사무실과 행정팀 등에 연락을 돌려 다양한 용도로 이미 예약되어 사용할 수 없는 방 10개의 목록을 얻었다. 이제 주원이는 후보 중 목록에 없는 방들 가운데 가장 큰 방을 골라 무단 점거하고 동아리방으로 사용하고자 한다. 가장 큰 방이 여러 개라면 주원이는 그중 아무 방이나 골라서 점거할 것이다.

주원이를 위해 동아리방으로 점거할 수 있는 방을 찾아주자.

제한

  • 1 \le N \le 100
  • 1 \le t_i \le 100
  • 1 \le |s_i|, |w_i| \le 10
  • 방 이름은 알파벳 대문자와 숫자로만 이루어진다.
  • 후보 방의 이름은 모두 서로 다르다.
  • 사용할 수 없는 방의 이름은 모두 서로 다르다.
  • 점거할 수 있는 후보 방이 하나 이상 존재한다.

입력

첫 번째 줄에 동아리방 후보의 개수 N이 주어진다. (1 \leq N \leq 100)

두 번째 줄부터 N개의 줄 중 i번째 줄에 각 후보의 방 이름을 나타내는 문자열 s_i와 방의 크기를 나타내는 정수 t_i가 공백으로 구분되어 주어진다. 문자열 s_i는 길이가 1 이상 10 이하이고 알파벳 대문자와 숫자로만 이루어져 있다. (1 \le t_i \le 100)

그다음 줄부터 열 개의 줄 중 i번째 줄에 사용할 수 없는 방의 이름을 나타내는 문자열 w_i가 주어진다. 문자열 w_i는 길이가 1 이상 10 이하이고 알파벳 대문자와 숫자로만 이루어져 있다.

후보 방의 이름 s_i는 모두 서로 다르다.

사용할 수 없는 방의 이름 w_i는 모두 서로 다르다.

방을 점거하지 못할 수도 있는 현실과 달리, 점거할 수 있는 방이 하나 이상 존재함이 보장된다.

N
s_1 t_1
\vdots
s_N t_N
w_1
\vdots
w_{10}

출력

점거할 방의 이름을 출력한다. 점거할 수 있는 방이 여러 개라면 그중 아무거나 출력한다.


입력 예 1

4
A527 100
B101 50
C202 80
D303 70
A527
A528
B201
C404
D915
AUDITORIUM
LIBRARY
BBALJAM
BAEKYANGRO
KOREAUNIV

출력 예 1

C202
B - X XOR XZ

Time Limit: 1 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

長さ N の 2 進数 X が与えられます.長さ D の 2 進数 Z を X の後ろに連結してできる,長さ N+D の新しい 2 進数を XZ とします.

D=1,2,\cdots,M のそれぞれについて,X \oplus XZ を最大にする長さ D の 2 進数 Z を求めてください.

ただし,X や Z は 0 から始まることもあります.また,長さの異なる 2 つの 2 進数の XOR (\oplus) は,短い方の先頭に 0 を補って長さを揃えてから計算します.

2 つの 2 進数 A と B の XOR (A \oplus B) は,対応する各ビットについて,ビットが異なれば 1,同じならば 0 とする演算です.

制約

  • 1 \leq N \leq 200\,000
  • 1 \leq M \leq 1\,000
  • N, M は整数
  • X は 0 と 1 からなる長さ N の文字列

入力

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

N M
X

出力

M 行出力せよ.

i 行目には,X \oplus XZ を最大にする長さ i の 2 進数 Z を出力せよ.先頭の 0 も省略せず,ちょうど i 文字出力すること.


入力例 1

4 3
1001

出力例 1

0
10
110

入力例 2

3 5
100

出力例 2

1
11
011
1011
11011

表示言語

/ /

Score : 100 points

Problem Statement

You are given a binary number X of length N. For a binary number Z of length D, let XZ be the new binary number of length N+D obtained by appending Z to the end of X.

For each length D=1,2,\cdots,M, find the binary number Z of length D that maximizes X \oplus XZ.

Here, X and Z may start with 0. The XOR (\oplus) of two binary numbers of different lengths is computed after padding the shorter one with leading 0s so that both have the same length.

The XOR of two binary numbers A and B (A \oplus B) is the operation that, for each pair of corresponding bits, yields 1 if the bits differ and 0 if they are the same.

Constraints

  • 1 \leq N \leq 200\,000
  • 1 \leq M \leq 1\,000
  • N and M are integers.
  • X is a string of length N consisting of 0 and 1.

Input

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

N M
X

Output

Print M lines.

The i-th line should contain the binary number Z of length i that maximizes X \oplus XZ. Do not omit leading zeros; print exactly i characters.


Sample Input 1

4 3
1001

Sample Output 1

0
10
110

Sample Input 2

3 5
100

Sample Output 2

1
11
011
1011
11011

表示言語

/ /

Score : 100 points

문제

길이가 N인 이진수 X가 주어진다. 길이가 D인 이진수 Z를 X의 뒤에 붙여 만든 길이가 N+D인 새로운 이진수를 XZ라고 하자.

길이 D=1,2,\cdots,M 각각에 대해, X \oplus XZ를 최대로 만드는 길이가 D인 이진수 Z를 구하여라.

단, X와 Z는 0으로 시작할 수 있으며, 길이가 다른 두 이진수의 XOR (\oplus) 연산은 길이가 짧은 이진수의 앞에 0을 채워 길이를 맞춘 후 수행한다.

두 이진수 A와 B의 XOR 연산 (A \oplus B)은 두 이진수의 대응하는 각 비트에 대해, 비트가 서로 다르면 1, 같으면 0이 되는 연산이다.

제한

  • 1 \leq N \leq 200\,000
  • 1 \leq M \leq 1\,000
  • N, M은 정수
  • X는 0과 1로 이루어진 길이가 N인 문자열

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

N M
X

출력

총 M개의 줄을 출력하여라.

i번째 줄에는 X \oplus XZ를 최대로 만드는 길이가 i인 이진수 Z를 출력하여라. 이때 앞에 오는 0도 생략하지 않고 정확히 i개의 문자를 출력해야 한다.


입력 예 1

4 3
1001

출력 예 1

0
10
110

입력 예 2

3 5
100

출력 예 2

1
11
011
1011
11011
C - 마지막 치팅

Time Limit: 2 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

この問題はオンラインクエリ問題です。出力の節を参照してください。

氷上は黒部の能力を試すために,N 個の問題を用意しました。試験の内容は次のとおりです。

はじめに,長さ N の数列 A が与えられます。

その後,N = Q_1 + Q_2 個のクエリが与えられます。種類 1 のクエリは Q_1 個,種類 2 のクエリは Q_2 個あります。

  • 1 i: 現在の数列の平均値の小数点以下を切り捨てた値を A_i に加えます。(1 \leq i \leq N) 平均値を計算するときは,998\,244\,353 で割った余りを取らないことに注意してください。
  • 2 t: 種類 1 のクエリを t 回目に実行した直後の数列の総和を,998\,244\,353 で割った余りを出力します。(1 \leq t \leq Q_1) このクエリでは,まだ入力されていない未来の t 回目の種類 1 のクエリについて尋ねられることがあります。

この試験は黒部にとって難しすぎます。黒部の代わりに,氷上の問題をすべて解いてください。

制約

  • 2 \leq N \leq 500\,000
  • 1 \leq A_i \leq 10^9
  • Q_1 + Q_2 = N
  • 1 \leq Q_1 \leq N-1
  • 1 \leq Q_2 \leq \min(N-1, 10\,000)
  • 入力はすべて整数です。

入力

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

N
A_1 A_2 \cdots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_N

1 行目には整数 N が与えられます。

2 行目には数列 A_1, A_2, \dots, A_N が与えられます。

続く N 行には,クエリが 1 行に 1 個ずつ与えられます。各クエリは,問題文で説明した 1 i または 2 t の形式です。

各種類のクエリの個数 Q_1, Q_2 は,入力として与えられないことに注意してください。

出力

種類 2 のクエリを受け取るたびに,その答えを 1 行に出力し,標準出力を flush してください。flush しない場合,次のクエリが与えられず,実行時間制限超過となる可能性があります。値が大きくなることがあるため,998\,244\,353 で割った余りを出力してください。

各言語で出力バッファを flush する方法の例を以下に示します。

  • C: fflush(stdout)
  • C++: cout << ans << endl; または cout.flush();
  • Python: print(ans, flush=True) または sys.stdout.flush()
  • Java: System.out.flush()
  • その他の言語については,各言語のドキュメントを参照してください。

入力例 1

6
1 3 9 2 3 1
2 3
1 2
1 3
2 2
1 5
2 1

出力例 1

29
25
22

種類 1 のクエリを 1 回目に実行した後,数列は [1, 6, 9, 2, 3, 1] となり,総和は 22 です。

種類 1 のクエリを 2 回目に実行した後,数列は [1, 6, 12, 2, 3, 1] となり,総和は 25 です。

種類 1 のクエリを 3 回目に実行した後,数列は [1, 6, 12, 2, 7, 1] となり,総和は 29 です。

種類 2 のクエリは,順に未来,現在,過去について尋ねています。クエリ 2 3 の答えは 29,2 2 の答えは 25,2 1 の答えは 22 です。

入出力例 1 のやり取り

実際の入出力は,下の表の上から下への順に行われます。種類 2 のクエリを受け取るたびに,その答えを出力して flush するまで,次の入力を受け取ることはできません。

入力 出力
6
1 3 9 2 3 1
2 3
29
1 2
1 3
2 2
25
1 5
2 1
22

入力例 2

2
1 998244354
1 1
2 1

出力例 2

499122179

表示言語

/ /

Score : 100 points

Problem Statement

This is an online query problem. See the Output section.

Hikami has prepared N questions to test Kurobe's abilities. The test is as follows.

First, you are given a sequence A of length N.

Then, you are given N = Q_1 + Q_2 queries. There are Q_1 queries of type 1 and Q_2 queries of type 2.

  • 1 i: Add the average of the current sequence, rounded down to an integer, to A_i. (1 \leq i \leq N) Note that you must not take the remainder modulo 998\,244\,353 when calculating the average.
  • 2 t: Output the sum of the sequence immediately after the t-th query of type 1 is performed, modulo 998\,244\,353. (1 \leq t \leq Q_1) This query may ask about a future point in time: the t-th query of type 1 may not have been given in the input yet.

This test is too difficult for Kurobe. Solve all of Hikami's questions on Kurobe's behalf.

Constraints

  • 2 \leq N \leq 500\,000
  • 1 \leq A_i \leq 10^9
  • Q_1 + Q_2 = N
  • 1 \leq Q_1 \leq N-1
  • 1 \leq Q_2 \leq \min(N-1, 10\,000)
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_N

The first line contains the integer N.

The second line contains the sequence A_1, A_2, \dots, A_N.

The next N lines contain one query each. Each query has the form 1 i or 2 t, as described in the Problem Statement.

Note that the numbers of queries of each type, Q_1 and Q_2, are not given in the input.

Output

Whenever you receive a query of type 2, output its answer on a separate line and flush Standard Output. If you do not flush the output, you will not receive the next query, which may result in Time Limit Exceeded. Since the values can become large, output the remainder modulo 998\,244\,353.

The following are ways to flush the output buffer in various languages.

  • C: fflush(stdout)
  • C++: cout << ans << endl; or cout.flush();
  • Python: print(ans, flush=True) or sys.stdout.flush()
  • Java: System.out.flush()
  • For other languages, refer to the documentation for your language.

Sample Input 1

6
1 3 9 2 3 1
2 3
1 2
1 3
2 2
1 5
2 1

Sample Output 1

29
25
22

After the first query of type 1, the sequence is [1, 6, 9, 2, 3, 1], whose sum is 22.

After the second query of type 1, the sequence is [1, 6, 12, 2, 3, 1], whose sum is 25.

After the third query of type 1, the sequence is [1, 6, 12, 2, 7, 1], whose sum is 29.

The queries of type 2 ask about the future, the present, and the past, respectively. The answer to query 2 3 is 29, the answer to 2 2 is 25, and the answer to 2 1 is 22.

Order of Input and Output in Sample 1

The actual input and output occur in the order shown below, from top to bottom. After each query of type 2, you must output its answer and flush the output before you can receive further input.

Input Output
6
1 3 9 2 3 1
2 3
29
1 2
1 3
2 2
25
1 5
2 1
22

Sample Input 2

2
1 998244354
1 1
2 1

Sample Output 2

499122179

表示言語

/ /

Score : 100 points

문제

이 문제는 온라인 쿼리 문제입니다. 출력 문단을 참고하세요.

히카미는 쿠로베의 능력을 시험하기 위해 N개의 문제를 내었다. 그 내용은 다음과 같다.

먼저, 길이 N인 수열 A가 주어진다.

이후 N = Q_1 + Q_2개의 쿼리가 주어진다. 1번 쿼리는 Q_1번, 2번 쿼리는 Q_2번 주어진다.

  • 1 i: A_i에 현재 수열의 평균을 정수로 버림한 값을 더한다. (1 \leq i \leq N) 평균을 계산할 때 998\,244\,353으로 나눈 나머지를 취하지 않음에 유의하라.
  • 2 t: t번째 1번 쿼리가 수행된 직후의 수열의 합을 998\,244\,353으로 나눈 나머지를 출력한다. (1 \leq t \leq Q_1) 이 쿼리는 아직 입력이 들어오지 않은 미래의 시점 t를 질문할 수 있다.

쿠로베에게 이 시험은 너무 어렵다. 쿠로베 대신 히카미의 시험을 모두 풀어보자.

제한

  • 2 \leq N \leq 500\,000
  • 1 \leq A_i \leq 10^9
  • Q_1 + Q_2 = N
  • 1 \leq Q_1 \leq N-1
  • 1 \leq Q_2 \leq \min(N-1, 10\,000)
  • 입력은 모두 정수이다.

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

N
A_1 A_2 \cdots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_N

첫째 줄에 정수 N이 주어진다.

둘째 줄에 수열 A_1, A_2, \dots, A_N이 주어진다.

다음 N개의 줄에 쿼리가 하나씩 주어진다. 각 쿼리는 문제에서 설명한 1 i 또는 2 t 형식이다.

각 쿼리의 개수 Q_1, Q_2는 입력으로 주어지지 않음에 유의하라.

출력

2번 쿼리가 주어질 때마다 답을 한 줄에 하나씩 출력하고, flush한다. flush하지 않으면 다음 쿼리가 주어지지 않으며, 시간 초과를 받게 될 수 있다. 값이 커질 수 있으므로 998\,244\,353으로 나눈 나머지를 출력한다.

다음은 언어별로 출력 버퍼를 비우는 방법이다.

  • C: fflush(stdout)
  • C++: cout << ans << endl; 또는 cout.flush();
  • Python: print(ans, flush=True) 또는 sys.stdout.flush()
  • Java: System.out.flush()
  • 그 외의 언어는 각 언어의 문서를 참고한다.

입력 예 1

6
1 3 9 2 3 1
2 3
1 2
1 3
2 2
1 5
2 1

출력 예 1

29
25
22

1번째 1번 쿼리가 주어진 후 수열은 [1, 6, 9, 2, 3, 1]이고, 합은 22이다.

2번째 1번 쿼리가 주어진 후 수열은 [1, 6, 12, 2, 3, 1]이고, 합은 25이다.

3번째 1번 쿼리가 주어진 후 수열은 [1, 6, 12, 2, 7, 1]이고, 합은 29이다.

2번 쿼리는 각각 순서대로 미래, 현재, 과거를 묻는 쿼리이다. 쿼리 2 3의 답은 29, 2 2의 답은 25, 2 1의 답은 22이다.

예제 1의 입출력 순서

실제 입출력은 아래 표의 위에서 아래 순서로 이루어진다. 각 2번 쿼리의 답을 출력하고 flush한 후에야 다음 입력을 받을 수 있다.

입력 출력
6
1 3 9 2 3 1
2 3
29
1 2
1 3
2 2
25
1 5
2 1
22

입력 예 2

2
1 998244354
1 1
2 1

출력 예 2

499122179
D - 매직 덱

Time Limit: 2 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

この問題はインタラクティブ形式の問題です。

Alice と Bob は有名なマジシャンです。2 人のマジックに興味を持った大富豪の Catherine は、次のマジックを成功させれば莫大な富を与えると提案しました。

このマジックでは、数字 0、1、2 が書かれたカードと、秘密の文字列 A = A_1A_2\cdots A_{N-1} を使います。それぞれの数字が書かれたカードは、必要なだけ用意されています。A の各文字は L または R です。

Catherine は秘密の文字列 A を Alice だけに教えます。

Bob は Catherine を通じて、Alice に次のような質問をすることができます。

  1. Bob は数字 0、1、2 が書かれたカードの中から好きなカードを N 枚選び、左から右へ一列に並べます。並べたカードに書かれた数字を左から順に読んで得られる、長さ N の文字列を S とします。
  2. Catherine は、Bob が並べたカードを同じ順序のまま Alice に渡します。
  3. Alice はカードを置くための新しい空の列を用意します。その後、i=1,2,\cdots,N-1 の順に、次の操作を行います。
    • A_i が L ならば、Alice は残っているカードのうち最も左にあるカードを取り出し、新しい列の右端に置きます。
    • A_i が R ならば、Alice は残っているカードのうち最も右にあるカードを取り出し、新しい列の右端に置きます。
  4. Alice は最後に残った 1 枚のカードも新しい列の右端に置きます。新しい列のカードに書かれた数字を左から順に読んで得られる文字列を S' とします。
  5. Catherine は文字列 S' を Bob に教えます。

Bob は質問ごとに使うカードを選び直すことができます。それぞれの質問で使う文字列 S は、それまでの質問の結果に応じて決めてもかまいません。すべての質問を終えたら、Bob は秘密の文字列 A を Catherine に伝えなければなりません。

Alice と Bob が Catherine のマジックを成功させ、莫大な富を得られるように手伝ってください。

制約

  • 2 \le N \le 1\,000\,000
  • N は整数である。
  • 秘密の文字列 A は L と R のみからなる長さ N-1 の文字列である。

入出力

最初の行にカードの枚数 N が与えられます。

秘密の文字列 A は対話が始まる前に決定され、Bob の質問に応じて変更されることはありません。

次の形式でジャッジに質問することができます。

? S

S は 0、1、2 のみからなる長さ N の文字列でなければなりません。

各質問を出力した後、Alice が作った文字列 S' をジャッジから 1 行で受け取ります。S' は 0、1、2 のみからなる長さ N の文字列です。

質問は最大 5 回まで行うことができます。

秘密の文字列 A が分かったら、次の形式で出力してください。

! A

最終回答の出力は質問の回数に含まれません。最終回答を出力した後は、プログラムを直ちに終了してください。出力した文字列が秘密の文字列と異なる場合、不正解と判定されます。

質問の形式が正しくない場合、条件を満たさない文字列 S を出力した場合、または質問の回数が 5 回を超えた場合、ジャッジは -1 を出力して対話を終了します。


入力例 1

4
0012
1200

出力例 1

? 0210
? 1002
! LRR

注意

出力するたびに、必ず標準出力のバッファをフラッシュしてください。各言語では、次のようにして標準出力のバッファをフラッシュできます。

  • C: fflush(stdout)
  • C++: std::cout.flush()
  • Java, Kotlin: System.out.flush()
  • Python: sys.stdout.flush()

Display language

/ /

Score : 100 points

Problem Statement

This is an interactive problem.

Alice and Bob are famous magicians. Catherine, an immensely wealthy person interested in their magic, has offered them a vast fortune if they can successfully perform the following magic trick.

The trick uses cards bearing the digits 0, 1, and 2, and a secret string A = A_1A_2\cdots A_{N-1}. As many cards as needed are available for each digit. Each character of A is either L or R.

Catherine reveals the secret string A only to Alice.

Bob can make queries to Alice through Catherine as follows.

  1. Bob chooses any N cards bearing the digits 0, 1, and 2, and arranges them in a row from left to right. Let S be the string of length N obtained by reading the digits on these cards from left to right.
  2. Catherine passes the cards arranged by Bob to Alice, keeping them in the same order.
  3. Alice prepares a new, empty row for the cards. Then, for i=1,2,\cdots,N-1 in this order, Alice performs the following operation.
    • If A_i is L, Alice removes the leftmost remaining card and places it at the right end of the new row.
    • If A_i is R, Alice removes the rightmost remaining card and places it at the right end of the new row.
  4. Alice also places the last remaining card at the right end of the new row. Let S' be the string obtained by reading the digits on the cards in the new row from left to right.
  5. Catherine tells Bob the string S'.

Bob may choose the cards anew for each query. The string S used in each query may depend on the results of previous queries. After completing all queries, Bob must tell Catherine the secret string A.

Help Alice and Bob successfully perform Catherine's magic trick and earn a vast fortune.

Constraints

  • 2 \le N \le 1\,000\,000
  • N is an integer.
  • The secret string A has length N-1 and consists only of L and R.

Input and Output

First, the number of cards N is given on the first line.

The secret string A is fixed before the interaction begins and does not change in response to Bob's queries.

You can make a query to the judge in the following format.

? S

S must be a string of length N consisting only of 0, 1, and 2.

After printing each query, read the string S' produced by Alice from the judge on one line. S' is a string of length N consisting only of 0, 1, and 2.

You may make at most 5 queries.

Once you have determined the secret string A, print it in the following format.

! A

Printing the final answer does not count as a query. After printing the final answer, terminate your program immediately. If the printed string differs from the secret string, your submission will be judged Wrong Answer.

If you make an incorrectly formatted query, print a string S that does not satisfy the requirements, or make more than 5 queries, the judge will print -1 and end the interaction.


Sample Input 1

4
0012
1200

Sample Output 1

? 0210
? 1002
! LRR

Notes

Flush the standard output buffer after every output. You can do this in each language as follows.

  • C: fflush(stdout)
  • C++: std::cout.flush()
  • Java, Kotlin: System.out.flush()
  • Python: sys.stdout.flush()

표시 언어

/ /

Score : 100 points

문제

이 문제는 인터랙티브 문제이다.

Alice와 Bob은 유명한 마술사이다. 두 사람의 마술에 관심을 가진 엄청난 부자 Catherine은 다음과 같은 마술을 성공시키면 막대한 부를 주겠다고 제안했다.

마술에는 숫자 0, 1, 2가 적힌 카드와 비밀 문자열 A = A_1A_2\cdots A_{N-1}이 사용된다. 각 숫자가 적힌 카드는 필요한 만큼 준비되어 있다. A의 각 문자는 L 또는 R이다.

Catherine은 비밀 문자열 A를 Alice에게만 알려 준다.

Bob은 Catherine을 통해 Alice에게 다음과 같이 질의할 수 있다.

  1. Bob은 숫자 0, 1, 2가 적힌 카드들 중 원하는 카드 N장을 골라 왼쪽에서 오른쪽으로 한 줄로 나열한다. 나열된 카드에 적힌 숫자를 왼쪽부터 읽어 얻은 길이 N의 문자열을 S라고 하자.
  2. Catherine은 Bob이 나열한 카드들을 순서 그대로 Alice에게 전달한다.
  3. Alice는 카드를 놓을 새로운 빈 줄을 준비한다. 이후 i=1,2,\cdots,N-1의 순서대로 다음을 수행한다.
    • A_i가 L이면, Alice는 남아 있는 카드 중 가장 왼쪽 카드를 꺼내 새로운 줄의 오른쪽 끝에 놓는다.
    • A_i가 R이면, Alice는 남아 있는 카드 중 가장 오른쪽 카드를 꺼내 새로운 줄의 오른쪽 끝에 놓는다.
  4. Alice는 마지막으로 남은 카드 한 장도 새로운 줄의 오른쪽 끝에 놓는다. 새로운 줄의 카드에 적힌 숫자를 왼쪽부터 읽어 얻은 문자열을 S'이라고 하자.
  5. Catherine은 문자열 S'을 Bob에게 알려 준다.

Bob은 질의마다 사용할 카드들을 새로 선택할 수 있다. 각 질의에서 사용할 문자열 S는 이전 질의의 결과에 따라 정해도 된다. 질의를 모두 마치면 Bob은 Catherine에게 비밀 문자열 A를 말해야 한다.

Alice와 Bob이 Catherine의 마술을 성공시키고 막대한 부를 얻을 수 있도록 도와주자.

제한

  • 2 \le N \le 1\,000\,000
  • N은 정수이다.
  • 비밀 문자열 A는 L과 R로만 이루어진 길이 N-1의 문자열이다.

입출력

첫 번째 줄에 카드의 개수 N이 주어진다.

비밀 문자열 A는 상호작용이 시작되기 전에 정해지며, Bob의 질의에 따라 변경되지 않는다.

채점기에 다음과 같은 질의를 할 수 있다.

? S

S는 0, 1, 2로만 이루어진 길이 N의 문자열이어야 한다.

각 질의를 출력한 뒤, 채점기로부터 Alice가 만든 문자열 S'을 한 줄로 입력받는다. S'은 0, 1, 2로만 이루어진 길이 N의 문자열이다.

질의는 최대 5번까지 할 수 있다.

비밀 문자열 A를 알아냈다면 다음 형식으로 출력한다.

! A

정답 출력은 질의 횟수에 포함되지 않는다. 정답을 출력한 뒤에는 프로그램을 즉시 종료해야 한다. 출력한 문자열이 비밀 문자열과 다르면 틀렸습니다 판정을 받는다.

잘못된 형식의 질의를 하거나, 조건을 만족하지 않는 문자열 S를 출력하거나, 질의 횟수가 5번을 초과하면 채점기는 -1을 출력하고 상호작용을 종료한다.


입력 예 1

4
0012
1200

출력 예 1

? 0210
? 1002
! LRR

노트

모든 출력 뒤에는 반드시 표준 출력 버퍼를 비워야 한다. 각 언어에서 표준 출력 버퍼를 비우는 방법은 다음과 같다.

  • C: fflush(stdout)
  • C++: std::cout.flush()
  • Java, Kotlin: System.out.flush()
  • Python: sys.stdout.flush()
E - 벼락치기 고수가 될 거야!

Time Limit: 1 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

期末試験を目前に控えたジホは、今学期にあまりにも多くの科目を履修してしまったことに気づきました。しかし、ジホは諦めず、詰め込み勉強によってできるだけ高い得点を取ろうとしています。

ジホは N 個の科目を履修しています。各科目の試験の得点は、s_i 以上 e_i 以下の整数の中から一様ランダムに決まります。各科目の満点は M 点です。

試験まで残り T 時間あります。ジホは 1 時間ごとに科目を 1 つ選び、次の操作のいずれかを行うことができます。

  1. その科目の得点の下限を上げます。

    s_i := \min(s_i + 2,e_i).

  2. その科目の得点の上限を上げます。

    e_i := \min(e_i + 1,M).

ジホは残り T 時間で高い得点を取るために、最適な戦略を取ろうとしています。正確には、全科目の得点の合計の期待値を最大化しようとしています。この期待値の最大値の 2 倍を求めてください。

制約

  • 1 \leq N,M \leq 10^6
  • 0 \leq s_i \leq e_i \leq M
  • 0 \leq T \leq 10^9
  • 入力はすべて整数である。

入力

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

N M
s_1 e_1
\vdots
s_N e_N
T

1 行目には、科目数 N と各科目の満点 M が与えられます。

続く N 行には、各科目の得点の下限 s_i と上限 e_i が与えられます。

最後の行には、残り時間 T が与えられます。

出力

最適な戦略を取ったときの、全科目の得点の合計の期待値の最大値の 2 倍を出力してください。この値は常に整数です。


入力例 1

3 100
80 90
60 70
60 95
24

出力例 1

503

入力例 2

1 1
0 1
1

出力例 2

2

Display language

/ /

Score : 100 points

Problem Statement

With final exams approaching, Jeeho realizes that he has taken too many subjects this semester. Nevertheless, he refuses to give up and wants to get the highest possible scores by cramming.

Jeeho is taking N subjects. For each subject, his exam score is chosen uniformly at random from the integers between s_i and e_i, inclusive. The maximum score for each subject is M.

There are T hours left until the exams. During each hour, Jeeho may choose one subject and perform one of the following operations.

  1. Increase the lower bound of the score for that subject.

    s_i := \min(s_i + 2,e_i).

  2. Increase the upper bound of the score for that subject.

    e_i := \min(e_i + 1,M).

Jeeho wants to use the best possible strategy during the remaining T hours to obtain high scores. More precisely, he wants to maximize the expected sum of his scores across all subjects. Find 2 times this maximum expected value.

Constraints

  • 1 \leq N,M \leq 10^6
  • 0 \leq s_i \leq e_i \leq M
  • 0 \leq T \leq 10^9
  • All input values are integers.

Input

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

N M
s_1 e_1
\vdots
s_N e_N
T

The first line contains the number of subjects N and the maximum score for each subject M.

The next N lines contain the lower bound s_i and the upper bound e_i of the score for each subject.

The last line contains the remaining number of hours T.

Output

Print 2 times the maximum expected sum of the scores across all subjects under an optimal strategy. This value is always an integer.


Sample Input 1

3 100
80 90
60 70
60 95
24

Sample Output 1

503

Sample Input 2

1 1
0 1
1

Sample Output 2

2

표시 언어

/ /

Score : 100 points

문제

기말고사를 앞둔 지호는 자신이 이번 학기에 너무 많은 과목을 수강했다는 사실을 깨달았다. 하지만 지호는 포기하지 않고, 벼락치기를 통해 최대한 높은 점수를 거두고자 한다.

지호는 N개의 과목을 수강하며, 각 과목의 시험 점수는 s_i 이상 e_i 이하 범위 안의 정수들 중 균등하게 정해진다. 각 과목의 만점은 M점이다.

현재 지호에게는 시험까지 T시간이 남아 있다. 지호는 한 시간마다 과목 하나를 골라 다음의 시행 중 하나를 할 수 있다.

  1. 해당 과목의 저점을 올린다.

    s_i := \min(s_i + 2,e_i).

  2. 해당 과목의 고점을 올린다.

    e_i := \min(e_i + 1,M).

지호는 남은 T시간 동안 높은 점수를 받기 위한 최선의 전략을 취하려고 한다. 정확히는 모든 과목 점수 합의 기댓값을 최대화하려고 한다. 이때의 기댓값의 2배를 구하여라.

제한

  • 1 \leq N,M \leq 10^6
  • 0 \leq s_i \leq e_i \leq M
  • 0 \leq T \leq 10^9
  • 입력으로 주어지는 모든 수는 정수이다.

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

N M
s_1 e_1
\vdots
s_N e_N
T

첫째 줄에 과목 수 N과 각 과목의 만점 M이 주어진다.

둘째 줄부터 N개 줄에 걸쳐 각 과목의 저점 s_i와 고점 e_i가 주어진다.

마지막 줄에 남은 시간 T가 주어진다.

출력

최적의 전략을 취할 때 모든 과목 점수 합의 기댓값의 2배를 출력하여라. 이 값은 항상 정수이다.


입력 예 1

3 100
80 90
60 70
60 95
24

출력 예 1

503

입력 예 2

1 1
0 1
1

출력 예 2

2
F - 정렬수의 합

Time Limit: 1 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

1 から 9 までの数字が,それぞれ a_1, a_2, \cdots, a_9 個ずつあります.

ヨンスリは,これらの中から 1 個以上の数字を選び,昇順に並べて 1 つの数を作ろうとしています.

数字の選び方は全部で (a_1+1)(a_2+1) \cdots (a_9+1) - 1 通りあります.すべての選び方について作られる数の総和を 998\,244\,353 で割ったあまりを求めてください.

例えば,1 が 1 個,2 が 2 個,3 から 9 までが 0 個ずつある場合,数字の選び方は全部で 5 通りあり,作られる数は 1, 2, 12, 22, 122 で,これらの総和は 159 です.

制約

  • 0 \leq a_i \leq 200\,000 (1 \leq i \leq 9)
  • a_1 + a_2 + \cdots + a_9 \geq 1
  • 入力はすべて整数

入力

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

a_1 a_2 \cdots a_9

出力

作られる数すべての総和を 998\,244\,353 で割ったあまりを出力せよ.


入力例 1

1 2 0 0 0 0 0 0 0

出力例 1

159

表示言語

/ /

Score : 100 points

Problem Statement

For each i (1 \leq i \leq 9), you are given a_i copies of the digit i.

Yonsuri wants to choose one or more of these digits and arrange them in ascending order to form a single number.

There are (a_1+1)(a_2+1) \cdots (a_9+1) - 1 ways to choose the digits. Find the sum of the numbers formed over all these ways, modulo 998\,244\,353.

For example, if you are given one 1, two 2's, and zero copies of each digit from 3 to 9, there are 5 ways to choose the digits, and the numbers that can be formed are 1, 2, 12, 22, 122, whose sum is 159.

Constraints

  • 0 \leq a_i \leq 200\,000 (1 \leq i \leq 9)
  • a_1 + a_2 + \cdots + a_9 \geq 1
  • All input values are integers.

Input

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

a_1 a_2 \cdots a_9

Output

Output the sum of all the numbers that can be formed, modulo 998\,244\,353.


Sample Input 1

1 2 0 0 0 0 0 0 0

Sample Output 1

159

表示言語

/ /

Score : 100 points

문제

1부터 9까지 숫자가 각각 a_1, a_2, \cdots, a_9개씩 주어져 있다.

연수리는 이 중 1개 이상의 숫자를 선택한 뒤, 오름차순으로 정렬하여 하나의 수를 만들려고 한다.

숫자를 선택할 수 있는 총 (a_1+1)(a_2+1) \cdots (a_9+1) - 1가지 경우에 대해, 각 경우마다 만들 수 있는 수들의 총합을 998\,244\,353으로 나눈 나머지를 구하여라.

예를 들어, 1이 1개, 2가 2개, 나머지 3부터 9까지는 0개 주어진 경우, 선택할 수 있는 경우의 수는 총 5가지이며 만들 수 있는 수는 1, 2, 12, 22, 122로 이 수들의 합은 159이다.

제한

  • 0 \leq a_i \leq 200\,000 (1 \leq i \leq 9)
  • a_1 + a_2 + \cdots + a_9 \geq 1
  • 입력은 모두 정수

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

a_1 a_2 \cdots a_9

출력

만들 수 있는 모든 수의 합을 998\,244\,353으로 나눈 나머지를 출력한다.


입력 예 1

1 2 0 0 0 0 0 0 0

출력 예 1

159
G - 마지막 변론

Time Limit: 2 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

前任の検察官がこの地を去った後,アリサはその後を継いで新たな検察官になりました.

アリサは,被告人にできるだけ重い刑を求めようとしています.これに対し,ミリアは被告人の弁護を引き受けました.

アリサが主張する罪は N 個あります.i 番目の罪には,刑の加算値 a_i と 刑の上限 b_i が定められています.現在の刑の重さが S のとき,i 番目の罪を適用すると,刑の重さは \min(S + a_i, b_i) になります.最初の刑の重さは 0 です.

弁護人のミリアは,N 個の罪のうちちょうど 1 個に異議を唱え,その罪を完全に無効にすることができます.その後,アリサは 最終的な刑が最も重くなるように,残りの N-1 個の罪を好きな順序で適用します.

ミリアは,被告人の 最終的な刑が最も軽くなるように弁護します.異議を唱えるべき罪の番号と,そのときの最終的な刑の重さを求めてください.

制約

  • 2 \leq N \leq 300\,000
  • 1 \leq a_i \leq b_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である.

入力

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

N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

1 行目には,検察官が主張する罪の個数 N が与えられる.

続く N 行のうち i 行目には,i 番目の罪の刑の加算値 a_i と刑の上限 b_i が空白区切りで与えられる.

出力

ミリアが最善の弁護をしたときに異議を唱えるべき罪の番号と,そのときの最終的な刑の重さを,空白区切りで出力せよ.条件を満たす罪の番号が複数ある場合は,そのうちどれを出力してもよい.


入力例 1

5
3 3
1 6
2 5
1 1
1 2

出力例 1

3 4

ミリアが 3 番目の罪に異議を唱えると,アリサは 5, 4, 1, 2 番目の罪をこの順に適用します.刑の重さは 0 \rightarrow 1 \rightarrow 1 \rightarrow 3 \rightarrow 4 と変化し,最終的な刑の重さは最小になります.

表示言語

/ /

Score : 100 points

Problem Statement

After the previous prosecutor left this place, Alisa took over as the new prosecutor.

Alisa wants to seek the heaviest possible sentence for the defendant. Miria has taken on the defendant's defense.

Alisa alleges N offenses. The i-th offense has a sentence increment a_i and a sentence cap b_i. If the current sentence is S, applying the i-th offense changes the sentence to \min(S + a_i, b_i). The initial sentence is 0.

The defense attorney Miria can object to exactly one of the N offenses and have it completely dismissed. Then, Alisa applies the remaining N-1 offenses in any order she chooses to maximize the final sentence.

Miria chooses her objection to minimize the defendant's final sentence. Find the index of an offense she should object to and the resulting final sentence.

Constraints

  • 2 \leq N \leq 300\,000
  • 1 \leq a_i \leq b_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

The first line contains N, the number of offenses alleged by the prosecutor.

The next N lines describe the offenses. The i-th of these lines contains the sentence increment a_i and the sentence cap b_i of the i-th offense, separated by a space.

Output

Print the index of an offense Miria should object to when she defends optimally, followed by the resulting final sentence, separated by a space. If multiple offense indices are valid, you may print any of them.


Sample Input 1

5
3 3
1 6
2 5
1 1
1 2

Sample Output 1

3 4

If Miria objects to the 3-rd offense, Alisa applies the 5-th, 4-th, 1-st, and 2-nd offenses in this order. The sentence changes as 0 \rightarrow 1 \rightarrow 1 \rightarrow 3 \rightarrow 4, giving the minimum possible final sentence.

表示言語

/ /

Score : 100 points

문제

전직 검사가 이곳을 떠난 뒤, 아리사는 그 자리를 이어받아 새로운 검사가 되었다.

아리사는 피고인에게 가능한 한 무거운 형을 구형하려고 한다. 이에 맞서 미리아는 피고인의 변호를 맡았다.

아리사가 주장하는 죄는 N개이다. i번째 죄에는 가중 형량 a_i와 처단형 상한 b_i가 정해져 있다. 현재 형량이 S일 때, i번째 죄를 적용하면 형량은 \min(S + a_i, b_i)가 된다. 초기 형량은 0이다.

변호인 미리아는 N개의 죄 중 정확히 하나에 이의를 제기하여 그 죄를 완전히 무효화시킬 수 있다. 그 후, 아리사는 최종 형량이 최대화되도록 남은 N-1개의 죄를 원하는 순서대로 적용한다.

미리아는 피고인의 최종 형량이 최소화되도록 변호한다. 이의를 제기할 죄의 번호와, 이때의 최종 형량을 구해보자.

제한

  • 2 \leq N \leq 300\,000
  • 1 \leq a_i \leq b_i \leq 10^9 (1 \leq i \leq N)
  • 입력은 모두 정수이다.

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

첫째 줄에 검사가 주장하는 죄의 수 N이 주어진다.

다음 N개의 줄에 i번째 죄의 가중 형량 a_i와 처단형 상한 b_i가 공백을 사이에 두고 주어진다.

출력

미리아가 최선을 다해 변호했을 때, 이의를 제기할 죄의 번호와 이때의 최종 형량을 공백을 사이에 두고 출력한다. 가능한 죄의 번호가 여러 가지일 경우, 그중 아무것이나 출력한다.


입력 예 1

5
3 3
1 6
2 5
1 1
1 2

출력 예 1

3 4

미리아가 3번째 죄에 이의를 제기하면, 아리사는 5, 4, 1, 2번째 죄를 순서대로 적용한다. 최종 형량은 0 \rightarrow 1 \rightarrow 1 \rightarrow 3 \rightarrow 4로 최소이다.

H - 쌀 도둑

Time Limit: 2 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

アリスとボブはお腹が空いています。キャサリンは大量の米を持っており、アリスとボブはその米を盗む方法を探しています。

アリスとボブは、N 個の頂点と、相異なる 2 頂点を結ぶ M 本の無向辺からなるグラフ上にいます。はじめ、アリスとボブはともに頂点 1 にいます。他の各頂点には、キャサリンの米俵が 1 つずつ置かれています。

各時刻に、アリスとボブはそれぞれ、自分がいる頂点に接続する辺を 1 本選び、その辺の反対側の頂点へただちに移動します。その場にとどまるとキャサリンに見つかるおそれがあるので、毎回必ず両方が移動します。米俵は重いため、ある頂点の米俵を盗むには、アリスとボブが同時にその頂点に到着する必要があります。

キャサリンは、各辺に防犯カメラかモーションセンサーのどちらか一方を設置しました。どの頂点にも、防犯カメラが設置された辺とモーションセンサーが設置された辺がそれぞれ少なくとも 1 本接続しています。

アリスとボブが同じ時刻に同じ種類の防犯装置が設置された辺を通ると、キャサリンに見つかります。そのため、毎回、アリスとボブが選ぶ 2 本の辺には異なる種類の防犯装置が設置されていなければなりません。

米俵が置かれている N-1 個の頂点それぞれについて、アリスとボブがその頂点の米俵を盗めるか判定してください。

制約

  • 1 \le T \le 10^5
  • 2 \le N \le 200000
  • 2 \le M \le 500000
  • 1 \le u_i < v_i \le N
  • w_i \in {1,2}
  • 各テストケースにおいて、組 (u_i,v_i,w_i) は互いに異なる。
  • 各頂点には、防犯装置の種類ごとに少なくとも 1 本の辺が接続している。
  • 全テストケースにおける N の総和は 200000 以下である。
  • 全テストケースにおける M の総和は 500000 以下である。
  • 入力はすべて整数である。

入力

最初の行にテストケースの個数 T が与えられる。続いて T 個のテストケースが与えられる。各テストケースは以下の形式で与えられる。

N M
u_1 v_1 w_1
\vdots
u_M v_M w_M

i 本目の辺は頂点 u_i と頂点 v_i を結ぶ。w_i=1 のとき、その辺には防犯カメラが設置されており、w_i=2 のとき、モーションセンサーが設置されている。同じ 2 頂点を結ぶ辺は、防犯装置の種類ごとに高々 1 本である。

出力

各テストケースの答えを 1 行ずつ、合計 T 行出力せよ。各テストケースについて、長さ N-1 の文字列 s_1s_2\cdots s_{N-1} を出力せよ。頂点 j+1 の米俵を盗めるなら s_j を 1 とし、そうでなければ 0 とせよ。


入力例 1

3
2 2
1 2 1
1 2 2
6 6
1 2 1
2 3 2
3 4 1
4 5 2
5 6 1
1 6 2
4 4
1 2 1
3 4 1
1 2 2
3 4 2

出力例 1

1
00100
100

入力例に含まれる 3 個のテストケースのグラフを以下に示す。

sample-graphs.png

1 番目のテストケースでは、アリスとボブは同時に頂点 2 に移動し、その米俵を盗める。

2 番目のテストケースでは、アリスが頂点 2,3,4 の順に移動し、ボブが頂点 6,5,4 の順に移動すると、同時に頂点 4 に到着して米俵を盗める。他の頂点の米俵は盗めない。

表示言語

/ /

Score : 100 points

Problem Statement

Alice and Bob are hungry. Catherine has a huge amount of rice, and Alice and Bob want to find a way to steal it.

They are on a graph with N vertices and M undirected edges, each joining two distinct vertices. Alice and Bob both start at vertex 1. Catherine has one sack of rice at each of the other vertices.

At each time step, Alice and Bob each choose an edge incident to their current vertex and immediately move to its other endpoint. Both must move at every step, since Catherine could spot them if they stayed still. A sack of rice is heavy, so they can steal it from a vertex only if they reach that vertex at the same time.

Catherine has installed exactly one security device on every edge: either a CCTV camera or a motion sensor. Every vertex has at least one incident edge with a CCTV camera and at least one incident edge with a motion sensor.

If Alice and Bob traverse edges with the same kind of device in the same time step, Catherine will spot them. Therefore, the two edges they choose at each step must have different kinds of devices.

For each of the N-1 vertices with a sack of rice, determine whether Alice and Bob can steal the sack at that vertex.

Constraints

  • 1 \le T \le 10^5
  • 2 \le N \le 200000
  • 2 \le M \le 500000
  • 1 \le u_i < v_i \le N
  • w_i \in {1,2}
  • Within each test case, the triples (u_i,v_i,w_i) are pairwise distinct.
  • Each vertex has at least one incident edge of each device type.
  • The sum of N over all test cases is at most 200000.
  • The sum of M over all test cases is at most 500000.
  • All input values are integers.

Input

The first line contains the number of test cases T. Then the test cases follow. Each test case is given in the following format:

N M
u_1 v_1 w_1
\vdots
u_M v_M w_M

The i-th edge connects vertices u_i and v_i. It has a CCTV camera if w_i=1 and a motion sensor if w_i=2. Two vertices can be joined by at most one edge of each device type.

Output

Print T lines, one for each test case. For each test case, print a string s_1s_2\cdots s_{N-1} of length N-1. Character s_j is 1 if Alice and Bob can steal the sack at vertex j+1, and 0 otherwise.


Sample Input 1

3
2 2
1 2 1
1 2 2
6 6
1 2 1
2 3 2
3 4 1
4 5 2
5 6 1
1 6 2
4 4
1 2 1
3 4 1
1 2 2
3 4 2

Sample Output 1

1
00100
100

The diagram shows the graphs in the three sample test cases.

sample-graphs.png

In the first test case, Alice and Bob can reach vertex 2 together in one step and steal its sack.

In the second test case, Alice can visit vertices 2, 3, and 4 in that order while Bob visits vertices 6, 5, and 4. They arrive at vertex 4 together and can steal its sack. They cannot steal any other sack in this test case.

表示言語

/ /

Score : 100 points

문제

앨리스와 밥은 배가 고프다. 캐서린은 엄청난 양의 쌀을 가지고 있는데, 앨리스와 밥은 캐서린의 쌀을 훔칠 수 있는 방법을 찾고 싶다.

앨리스와 밥은 N개의 정점과 서로 다른 두 정점을 잇는 M개의 방향 없는 간선으로 이루어진 그래프 위에 있다. 초기에 앨리스와 밥은 1번 정점에 있고, 나머지 모든 정점에는 캐서린의 쌀이 한 가마니씩 있다. 이제 앨리스와 밥은 그래프 위를 이동하며 각 정점에 있는 쌀을 훔칠 것이다.

앨리스와 밥은 단위 시간마다 자신이 현재 위치한 정점에 연결된 간선을 각자 하나씩 골라 다른 정점으로 즉시 이동할 수 있다. 이동하지 않으면 캐서린에게 위치를 발각당할 수 있으므로, 매 단위 시간마다 앨리스와 밥 각각 한 번씩 이동해야 한다. 쌀가마니는 무겁기 때문에, 어떤 정점에 있는 쌀을 훔치려면 앨리스와 밥이 동시에 그 정점에 도달해야 한다.

캐서린은 자신의 쌀을 아주 소중히 여기기에, 앨리스와 밥이 쌀을 훔치러 다니는 것을 확인할 수 있는 방범 장치인 CCTV와 모션 센서를 각 간선에 설치했다. 모든 간선에는 CCTV와 모션 센서 중 정확히 한 가지가 설치되어 있고, 모든 정점에는 CCTV가 설치된 간선과 모션 센서가 설치된 간선이 각각 하나 이상 연결되어 있다.

앨리스와 밥이 어느 순간에 동일한 방범 장치가 설치된 간선을 따라 이동하면 캐서린에게 위치를 발각당한다. 따라서, 매 단위시간마다 앨리스와 밥이 고르는 두 간선에는 서로 다른 방범 장치가 설치되어 있어야 한다.

앨리스와 밥은 쌀가마니를 얼마나 훔칠 수 있는지 궁금해졌다. 쌀가마니가 있는 N-1개의 정점에 대해, 앨리스와 밥이 이 정점에 있는 쌀가마니를 훔칠 수 있는지 확인하는 프로그램을 작성하여 앨리스와 밥을 도와주자.

제한

  • 1 \le T \le 10^5
  • 2 \le N \le 200000
  • 2 \le M \le 500000
  • 1 \le u_i < v_i \le N
  • w_i \in {1,2}
  • 하나의 테스트케이스에서 (u_i,v_i,w_i)는 모두 서로 다르다.
  • 모든 정점에는 방범 장치 종류별로 간선이 적어도 하나씩 연결되어 있다.
  • 모든 테스트케이스에서 N의 합은 200000 이하이고, M의 합은 500000 이하이다.
  • 입력은 모두 정수이다.

입력

첫 번째 줄에 테스트케이스의 개수 T가 주어진다. (1 \le T \le 10^5) 두 번째 줄부터 각 테스트케이스의 입력이 줄바꿈으로 구분되어 주어진다.

각 테스트케이스의 첫 번째 줄에 양의 정수 N과 M이 공백으로 구분되어 주어진다. (2 \le N \le 200\,000; 2 \le M \le 500\,000)

각 테스트케이스의 두 번째 줄부터 M개의 줄에 간선 정보가 주어진다. 그중 i번째 줄에는 양의 정수 u_i, v_i, w_i가 공백으로 구분되어 주어진다. 이는 u_i번 정점과 v_i번 정점을 잇는 간선이 있음을 의미하며, w_i=1인 경우 이 간선에 CCTV가, w_i=2인 경우 모션 센서가 설치되어 있음을 의미한다. (1 \le u_i < v_i \le N; w_i \in {1,2})

동일한 두 정점을 잇는 간선은 방범 장치별로 최대 한 개이다. 즉, 하나의 테스트케이스에서 i \neq j이면 (u_i,v_i,w_i)\neq(u_j,v_j,w_j)이다.

모든 테스트케이스에서 N의 합은 200\,000을 넘지 않는다.

모든 테스트케이스에서 M의 합은 500\,000을 넘지 않는다.

각 테스트케이스의 입력 형식은 다음과 같다.

N M
u_1 v_1 w_1
\vdots
u_M v_M w_M

출력

T개의 줄에 걸쳐 각 테스트케이스에서의 답을 한 줄에 하나씩 출력한다.

그중 i번째 줄에는 길이 N-1의 문자열 s_1s_2s_3\cdots s_{N-1}을 출력한다. j+1번 정점에 있는 쌀가마니를 훔칠 수 있다면 s_j= 1, 아니면 s_j= 0으로 한다.


입력 예 1

3
2 2
1 2 1
1 2 2
6 6
1 2 1
2 3 2
3 4 1
4 5 2
5 6 1
1 6 2
4 4
1 2 1
3 4 1
1 2 2
3 4 2

출력 예 1

1
00100
100

예제의 3개 테스트케이스에 등장한 그래프는 다음 사진과 같다.

sample-graphs.png

1번 테스트케이스에서, 앨리스와 밥이 동시에 2번 정점으로 이동하여 쌀가마니를 훔칠 수 있다.

2번 테스트케이스에서, 앨리스는 2, 3, 4번 정점 순으로, 밥은 6, 5, 4번 정점 순으로 이동하면 4번 정점에 동시에 도달하여 쌀가마니를 훔칠 수 있다. 다른 정점에 있는 쌀가마니는 훔칠 수 없다.

I - 수열과 쿼리

Time Limit: 3 sec / Memory Limit: 1024 MiB

表示言語

/ /

配点 : 100 点

問題文

長さ N の整数列 A_1,A_2,\cdots,A_N が与えられます。この数列に Q 個のクエリを順に適用します。クエリには次の 2 種類があります。

  • 1 x v: A_x を v に変更します。
  • 2 l r: \sum_{i=l}^{r} A_i を出力します。

この問題は簡単すぎると考えたジホは、すべてのタイプ 1 のクエリから x を消してしまいました。

各タイプ 2 のクエリについて、それより前に現れるすべてのタイプ 1 のクエリの消された x を、それぞれ 1 以上 N 以下の整数として自由に定められるとき、出力される値の最大値を求めてください。

各タイプ 2 のクエリは独立に考えます。 つまり、異なるタイプ 2 のクエリに答える際には、同じタイプ 1 のクエリの x を異なる値に定めてもかまいません。また、異なるタイプ 1 のクエリの x を同じ値に定めてもかまいません。

制約

  • 1 \leq N \leq 200\,000
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq Q \leq 200\,000
  • タイプ 1 のクエリでは -10^9 \leq v \leq 10^9
  • タイプ 2 のクエリでは 1 \leq l \leq r \leq N
  • タイプ 2 のクエリが少なくとも 1 つ与えられる
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Q 個のクエリは順に与えられ、それぞれ次のいずれかの形式で与えられる。

  • 1 v: タイプ 1 のクエリ。
  • 2 l r: タイプ 2 のクエリ。

出力

タイプ 2 のクエリごとに、出力される値の最大値を 1 行に 1 つずつ出力せよ。


入力例 1

3
1 2 3
6
2 1 2
1 10
2 1 2
1 -5
2 1 3
2 2 3

出力例 1

3
12
8
13

Display language

/ /

Score : 100 points

Problem Statement

You are given an integer sequence A_1,A_2,\cdots,A_N of length N. You apply Q queries to this sequence in order. There are two types of queries:

  • 1 x v: Change A_x to v.
  • 2 l r: Print \sum_{i=l}^{r} A_i.

Thinking that this problem is too easy, Jeeho erased x from every type-1 query.

For each type-2 query, find the maximum value that can be printed when the erased x in every preceding type-1 query can be freely chosen as an integer between 1 and N, inclusive.

Each type-2 query is considered independently. In particular, the same type-1 query may be assigned different values of x when answering different type-2 queries. Different type-1 queries may also be assigned the same value of x.

Constraints

  • 1 \leq N \leq 200\,000
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq Q \leq 200\,000
  • In each type-1 query, -10^9 \leq v \leq 10^9.
  • In each type-2 query, 1 \leq l \leq r \leq N.
  • There is at least one type-2 query.
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

The Q queries are given in order, each in one of the following two formats:

  • 1 v: A type-1 query.
  • 2 l r: A type-2 query.

Output

For each type-2 query, print the maximum value that can be printed, one per line.


Sample Input 1

3
1 2 3
6
2 1 2
1 10
2 1 2
1 -5
2 1 3
2 2 3

Sample Output 1

3
12
8
13

표시 언어

/ /

Score : 100 points

문제

길이가 N인 정수 수열 A_1,A_2,\cdots,A_N이 주어진다. 이 수열에 Q개의 쿼리를 순서대로 적용한다. 쿼리는 다음 두 종류이다.

  • 1 x v: A_x를 v로 바꾼다.
  • 2 l r: \sum_{i=l}^{r} A_i를 출력한다.

이 문제가 너무 쉽다고 생각한 지호는 1번 쿼리에 적힌 x를 모두 지워 버렸다.

각 2번 쿼리마다, 그보다 앞에 등장한 모든 1번 쿼리의 지워진 x를 1 이상 N 이하의 정수로 자유롭게 정할 수 있을 때 출력될 수 있는 값의 최댓값을 구하여라.

각 2번 쿼리는 독립적이다. 즉, 서로 다른 두 2번 쿼리에서 같은 1번 쿼리의 x를 서로 다르게 정해도 된다. 서로 다른 두 1번 쿼리의 x를 같은 값으로 정해도 된다.

제한

  • 1 \leq N \leq 200\,000
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq Q \leq 200\,000
  • 1번 쿼리에서 -10^9 \leq v \leq 10^9
  • 2번 쿼리에서 1 \leq l \leq r \leq N
  • 2번 쿼리가 하나 이상 주어진다.
  • 입력으로 주어지는 모든 수는 정수이다.

입력

입력은 다음 형식으로 표준 입력에서 주어진다.

N
A_1 A_2 \cdots A_N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Q개의 쿼리는 순서대로 주어지며, 각 쿼리는 다음 두 형식 중 하나이다.

  • 1 v: 1번 쿼리를 나타낸다.
  • 2 l r: 2번 쿼리를 나타낸다.

출력

2번 쿼리가 주어질 때마다 출력될 수 있는 값의 최댓값을 한 줄에 하나씩 출력한다.


입력 예 1

3
1 2 3
6
2 1 2
1 10
2 1 2
1 -5
2 1 3
2 2 3

출력 예 1

3
12
8
13
J - 숫자 슬라임

Time Limit: 2 sec / Memory Limit: 1024 MiB

表示言語

/ /

Score: 100 points

問題文

1 から n までの番号が付いた n 匹のスライムがいます。各スライムの色は赤または青で、大きさは正の整数です。

大きさ q のスライムが大きさ p のスライムを食べると、食べられたスライムは消えます。食べたスライムの番号と色は変わらず、大きさは次のように変わります。

  • 2 匹の色が同じなら、新しい大きさは p と q の最小公倍数になります。
  • 2 匹の色が異なるなら、新しい大きさは p と q の最大公約数になります。

実験では、あらかじめ決められた順番に従ってスライムが他のスライムを食べ、最後にちょうど 1 匹が残ります。実験を始める前に、同じ色のスライムを 2 匹選んで大きさを交換する操作を、好きな回数だけ行えます。一度も交換しなくてもかまいません。

最後に残るスライムの大きさとしてあり得るすべての値の最小公倍数を求め、10^9+7 で割った余りを出力してください。

制約

  • 1 \leq n \leq 5000
  • 1 \leq i \leq n に対して c_i \in \{0,1\}
  • 1 \leq i \leq n に対して 1 \leq a_i \leq 10^9
  • 1 \leq k \leq n-1 に対して 1 \leq i_k,j_k \leq n かつ i_k \neq j_k
  • 各ステップに登場する 2 匹は、その時点でどちらも残っている。すべてのステップが終了すると、ちょうど 1 匹が残る。
  • 入力はすべて整数である。

入力

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

n
c_1 c_2 \ldots c_n
a_1 a_2 \ldots a_n
i_1 j_1
\vdots
i_{n-1} j_{n-1}

c_i=0 ならスライム i は青、c_i=1 なら赤である。a_i は大きさを交換する前のスライム i の大きさである。

続く n-1 行は、実験のステップを行う順に表す。ステップ k ではスライム i_k がスライム j_k を食べる。n=1 のとき、入力は 3 行目で終わる。

出力

最後に残るスライムの大きさとしてあり得るすべての値の最小公倍数を、10^9+7 で割った余りを出力せよ。


入力例 1

3
1 0 1
1 2 1
1 2
1 3

出力例 1

1

赤いスライム 1 と 3 の大きさはどちらも 1 なので、交換しても結果は変わりません。まず、スライム 1 が青いスライム 2 を食べ、大きさは \gcd(1,2)=1 になります。次に赤いスライム 3 を食べ、大きさは \operatorname{lcm}(1,1)=1 になります。最後の大きさとしてあり得る値は 1 だけです。


入力例 2

3
1 1 0
2 3 1
1 3
2 1

出力例 2

6

赤いスライム 1 が、大きさ 1 の青いスライム 3 を食べます。スライム 1 の最初の大きさによらず、食べた後の大きさは 1 です。次に、赤いスライム 2 がスライム 1 を食べます。

交換しなければ、スライム 2 の最初の大きさは 3 なので、最後の大きさは \operatorname{lcm}(3,1)=3 です。先に赤い 2 匹の大きさを交換すると、スライム 2 の最初の大きさは 2 になり、最後の大きさは \operatorname{lcm}(2,1)=2 です。したがって、答えは 2 と 3 の最小公倍数である 6 です。


入力例 3

3
0 1 1
2 2 3
1 2
1 3

出力例 3

1

Language

/ /

Score: 100 points

Problem Statement

You have n slimes numbered from 1 to n. Each slime is either red or blue and has a positive integer size.

When a slime of size q eats a slime of size p, the slime that was eaten disappears. The eater keeps its number and color. Its new size depends on the two slimes' colors:

  • If their colors are the same, its new size is the least common multiple of p and q.
  • If their colors are different, its new size is the greatest common divisor of p and q.

In an experiment, the slimes eat one another in a prescribed order until exactly one slime remains. Before the experiment starts, you may swap the sizes of two slimes of the same color any number of times, including zero.

Find the least common multiple of all possible sizes of the last remaining slime. Print the result modulo 10^9+7.

Constraints

  • 1 \leq n \leq 5000
  • c_i \in \{0,1\} for 1 \leq i \leq n
  • 1 \leq a_i \leq 10^9 for 1 \leq i \leq n
  • 1 \leq i_k,j_k \leq n and i_k \neq j_k for 1 \leq k \leq n-1
  • Both slimes involved in each step are still present at that step. Exactly one slime remains after all the steps.
  • All input values are integers.

Input

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

n
c_1 c_2 \ldots c_n
a_1 a_2 \ldots a_n
i_1 j_1
\vdots
i_{n-1} j_{n-1}

Slime i is blue if c_i=0 and red if c_i=1. Its size before any swaps is a_i.

The last n-1 lines give the steps in chronological order. At step k, slime i_k eats slime j_k. If n=1, the input ends after the third line.

Output

Print the least common multiple of all possible final sizes, modulo 10^9+7.


Sample Input 1

3
1 0 1
1 2 1
1 2
1 3

Sample Output 1

1

The two red slimes, 1 and 3, both have size 1, so swapping their sizes has no effect. Slime 1 first eats the blue slime 2 and becomes size \gcd(1,2)=1. It then eats the red slime 3 and becomes size \operatorname{lcm}(1,1)=1. The only possible final size is 1.


Sample Input 2

3
1 1 0
2 3 1
1 3
2 1

Sample Output 2

6

The red slime 1 eats the blue slime 3, whose size is 1. This makes slime 1's size 1 regardless of its initial size. The red slime 2 then eats slime 1.

With no swap, slime 2 starts at size 3 and ends at size \operatorname{lcm}(3,1)=3. If you first swap the sizes of the two red slimes, slime 2 starts at size 2 and ends at size \operatorname{lcm}(2,1)=2. The possible final sizes are 2 and 3, so the answer is 6.


Sample Input 3

3
0 1 1
2 2 3
1 2
1 3

Sample Output 3

1