実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は、AtCoder 語を勉強しています。
高橋君は英単語に対して AtCoder 語の単語を対応させて覚えています。
高橋君は英語における red
, blue
, green
は AtCoder 語ではそれぞれ SSS
, FFF
, MMM
に対応していることを覚えており、他の単語は覚えていません。
英小文字からなる文字列 S が与えられます。S が高橋君が AtCoder 語との対応を覚えている英単語の表記と一致しているならば S に対応している AtCoder 語の単語を、そうでないならば文字列 Unknown
を出力してください。
制約
- S は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
問題文中の指示に従い、文字列を出力せよ。
入力例 1
red
出力例 1
SSS
英語における red
は AtCoder 語における SSS
に対応します。
入力例 2
atcoder
出力例 2
Unknown
Score : 100 points
Problem Statement
Takahashi is learning AtCoderish Language.
He memorizes AtCoderish words corresponding to English words.
He knows that red
, blue
, and green
in English respectively correspond to SSS
, FFF
, and MMM
in AtCoderish, and he knows no other words.
You are given a string S consisting of lowercase English letters. If S equals an English word that Takahashi knows corresponds to an AtCoderish word, output the AtCoderish word corresponding to S; otherwise, output the string Unknown
.
Constraints
- S is a string of length between 1 and 10, inclusive, consisting of English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output a string according to the instructions in the problem statement.
Sample Input 1
red
Sample Output 1
SSS
red
in English corresponds to SSS
in AtCoderish.
Sample Input 2
atcoder
Sample Output 2
Unknown
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
中に何も入っていない袋があります。
Q 個のクエリが与えられるので、これらのクエリを順に処理したときのタイプ 2 の各クエリの答えを出力してください。
各クエリは、以下のいずれかの形式です。
-
タイプ 1 :
1 x
の形式で入力として与えられる。整数 x が書かれたボールを 1 つ袋の中に入れる。 -
タイプ 2 :
2
の形式で入力として与えられる。袋に入っているボールのうち書かれている整数が最小のものを 1 つ取り出し、その整数を答えとする。ただし、袋の中にボールが入っていないときにはこのクエリは与えられない。
制約
- 2 \leq Q \leq 100
- タイプ 1 のクエリにおいて、1 \leq x \leq 100
- タイプ 2 のクエリが与えられたとき、袋は空ではない
- タイプ 2 のクエリが 1 つ以上与えられる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
Q \text{query}_1 \text{query}_2 \ldots \text{query}_Q
ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 x
2
出力
タイプ 2 のクエリの個数を q として、q 行出力せよ。
i 行目には、i 番目のタイプ 2 のクエリの答えを出力せよ。
入力例 1
5 1 6 1 7 2 1 1 2
出力例 1
6 1
はじめ、袋の中にボールは入っていません。
1 番目のクエリでは、袋の中に 6 が書かれたボールを入れます。
2 番目のクエリでは、袋の中に 7 が書かれたボールを入れます。
3 番目のクエリでは、袋の中には 6 が書かれたボールと 7 が書かれたボールが入っているため、6 が書かれたボールを取り出します。このクエリの答えは 6 となります。
4 番目のクエリでは、袋の中に 1 が書かれたボールを入れます。
5 番目のクエリでは、袋の中には 1 が書かれたボールと 7 が書かれたボールが入っているため、1 が書かれたボールを取り出します。このクエリの答えは 1 となります。
入力例 2
8 1 5 1 1 1 1 1 9 2 2 1 2 2
出力例 2
1 1 2
同じ整数が書かれたボールを複数個袋の中に入れることもあります。
Score : 200 points
Problem Statement
There is an empty bag.
You are given Q queries. Process these queries in order and output the answer to each type-2 query.
Each query is of one of the following types.
-
Type 1: Given as input in the format
1 x
. Put a ball with the integer x written on it into the bag. -
Type 2: Given as input in the format
2
. Pick out one ball with the minimum integer written on it from the balls in the bag, and report that integer as the answer. This query is not given when the bag contains no balls.
Constraints
- 2 \leq Q \leq 100
- In a type-1 query, 1 \leq x \leq 100.
- When a type-2 query is given, the bag is not empty.
- At least one type-2 query is given.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q \text{query}_1 \text{query}_2 \ldots \text{query}_Q
Here, \text{query}_i is the i-th query and is given in one of the following formats:
1 x
2
Output
Let q be the number of type-2 queries, and output q lines.
The i-th line should contain the answer to the i-th type-2 query.
Sample Input 1
5 1 6 1 7 2 1 1 2
Sample Output 1
6 1
Initially, the bag contains no balls.
The 1st query puts a ball with 6 written on it into the bag.
The 2nd query puts a ball with 7 written on it into the bag.
In the 3rd query, the bag contains a ball with 6 written on it and a ball with 7 written on it, so you pick out the ball with 6 written on it. The answer to this query is 6.
The 4th query puts a ball with 1 written on it into the bag.
In the 5th query, the bag contains a ball with 1 written on it and a ball with 7 written on it, so you pick out the ball with 1 written on it. The answer to this query is 1.
Sample Input 2
8 1 5 1 1 1 1 1 9 2 2 1 2 2
Sample Output 2
1 1 2
The bag may contain multiple balls with the same integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
10^9 行 10^9 列のグリッドがあり、このグリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。
グリッド上には N 人の人がおり、はじめ i 人目の人はマス (R_i, C_i) にいます。
はじめ時刻は 0 であり、各人は、時刻 1, 2, 3, 4, \ldots に以下のような移動をすることができます。
- その場に留まるか、8 近傍のマスに移動する。ただし、グリッドの外側に出ることはできない。厳密には、現在いるマスをマス (i, j) としてマス (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) のうち存在するマスのいずれかに移動する。また、移動には時間がかからないものとする。
N 人の人が全員同じマスに集まる時刻として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
答えを出力せよ。
入力例 1
3 2 3 5 1 8 1
出力例 1
3
以下のようにそれぞれの人が移動することによって、全員が時刻 3 にマス (5, 4) に集まります。
-
時刻 1 では、1 人目の人はマス (3, 4)、2 人目の人はマス (6, 2)、3 人目の人はマス (7, 2) に移動する。
-
時刻 2 では、1 人目の人はマス (4, 4)、2 人目の人はマス (5, 3)、3 人目の人はマス (6, 3) に移動する。
-
時刻 3 では、1 人目の人はマス (5, 4)、2 人目の人はマス (5, 4)、3 人目の人はマス (5, 4) に移動する。
入力例 2
5 6 7 6 7 6 7 6 7 6 7
出力例 2
0
はじめからすべての人は同じマスにいます。
入力例 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
出力例 3
44
Score : 300 points
Problem Statement
There is a grid with 10^9 rows and 10^9 columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
There are N people on the grid. Initially, the i-th person is at square (R_i, C_i).
The time starts at 0. Each person can do the following move at times 1, 2, 3, 4, \ldots.
- Stay at the current position, or move to an 8-adjacent square. It is forbidden to leave the grid. Formally, let square (i, j) be the current square, and move to one of the squares (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) that exists. Assume that the move takes no time.
Find the minimum possible time when the N people are at the same square.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Output the answer.
Sample Input 1
3 2 3 5 1 8 1
Sample Output 1
3
All people will be at square (5, 4) at time 3 if each person moves as follows.
-
At time 1, the 1st person moves to square (3, 4), the 2nd person moves to square (6, 2), and the 3rd person moves to square (7, 2).
-
At time 2, the 1st person moves to square (4, 4), the 2nd person moves to square (5, 3), and the 3rd person moves to square (6, 3).
-
At time 3, the 1st person moves to square (5, 4), the 2nd person moves to square (5, 4), and the 3rd person moves to square (5, 4).
Sample Input 2
5 6 7 6 7 6 7 6 7 6 7
Sample Output 2
0
All people start at the same square.
Sample Input 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
Sample Output 3
44
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の英小文字列 S, T および M 個の整数の組 (L_1,R_1),(L_2,R_2),\ldots,(L_M,R_M) が与えられます。
i=1,2,\ldots,M の順に以下の操作を行います。
- S の L_i 文字目から R_i 文字目と、T の L_i 文字目から R_i 文字目を入れ替える。
- 例えば、S が
abcdef
、T がghijkl
、(L_i,R_i)=(3,5) のとき、操作後の S,T はそれぞれabijkf
、ghcdel
となる。
- 例えば、S が
M 回の操作を行った後の S を求めてください。
制約
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 2\times 10^5
- S,T は長さ N の英小文字列
- 1\leq L_i\leq R_i\leq N
- N,M,L_i,R_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S T L_1 R_1 L_2 R_2 \vdots L_M R_M
出力
M 回の操作を行った後の S を出力せよ。
入力例 1
5 3 apple lemon 2 4 1 5 5 5
出力例 1
lpple
はじめ S,T はそれぞれ apple
, lemon
です。
- i=1 の操作後の S,T はそれぞれ
aemoe
,lppln
です。 - i=2 の操作後の S,T はそれぞれ
lppln
,aemoe
です。 - i=3 の操作後の S,T はそれぞれ
lpple
,aemon
です。
よって、3 回の操作を行った後の S は lpple
です。
入力例 2
10 5 lemwrbogje omsjbfggme 5 8 4 8 1 3 6 6 1 4
出力例 2
lemwrfogje
Score : 400 points
Problem Statement
You are given length-N lowercase English strings S and T, and M pairs of integers (L_1,R_1),(L_2,R_2),\ldots,(L_M,R_M).
Perform the following operation for i=1,2,\ldots,M in order:
- Swap the L_i-th through R_i-th characters of S and the L_i-th through R_i-th characters of T.
- For example, if S is
abcdef
, T isghijkl
, and (L_i,R_i)=(3,5), then S and T becomeabijkf
andghcdel
, respectively.
- For example, if S is
Find the string S after performing the M operations.
Constraints
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 2\times 10^5
- Each of S and T is a length-N lowercase English strings.
- 1\leq L_i\leq R_i\leq N
- N, M, L_i, and R_i are integers.
Input
The input is given from Standard Input in the following format:
N M S T L_1 R_1 L_2 R_2 \vdots L_M R_M
Output
Output the S after performing the M operations.
Sample Input 1
5 3 apple lemon 2 4 1 5 5 5
Sample Output 1
lpple
Initially, S and T are apple
and lemon
, respectively.
- After the operation for i=1, S and T are
aemoe
andlppln
, respectively. - After the operation for i=2, S and T are
lppln
andaemoe
, respectively. - After the operation for i=3, S and T are
lpple
andaemon
, respectively.
Thus, the string S after the three operations is lpple
.
Sample Input 2
10 5 lemwrbogje omsjbfggme 5 8 4 8 1 3 6 6 1 4
Sample Output 2
lemwrfogje
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。
あなたの目的は、以下の操作を繰り返し行うことにより、A のすべての長さ L の連続部分列についてその総和が M の倍数であるようにすることです。
- 1 \leq i \leq N なる整数 i を選び、A_i の値を 1 増やす。
目的を達成するまでの操作回数として考えられる最小値を求めてください。
制約
- 1 \leq N, M \leq 500
- 1 \leq L \leq N
- 0 \leq A_i < M
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M L A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 5 3 4 2 1 3
出力例 1
4
i = 2 を選ぶ操作を 1 回、i = 3 を選ぶ操作を 2 回、i = 4 を選ぶ操作を 1 回行うことで合計 4 回の操作で A = (4, 3, 3, 4) となり、すべての長さ 3 の連続部分列の総和が 5 の倍数となります。
入力例 2
7 10 4 7 0 9 1 6 4 2
出力例 2
10
Score : 475 points
Problem Statement
You are given a length-N integer sequence A = (A_1, A_2, \ldots, A_N).
Your goal is to perform the following operation repeatedly so that for every length-L contiguous subarray of A, the sum is a multiple of M.
- Choose an integer i such that 1 \leq i \leq N, and increase the value of A_i by 1.
Find the minimum possible number of operations before achieving the goal.
Constraints
- 1 \leq N, M \leq 500
- 1 \leq L \leq N
- 0 \leq A_i < M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M L A_1 A_2 \ldots A_N
Output
Output the answer.
Sample Input 1
4 5 3 4 2 1 3
Sample Output 1
4
By performing the operation once choosing i = 2, twice choosing i = 3, and once choosing i = 4, you get A = (4, 3, 3, 4) with a total of four operations, where every length-3 contiguous subarray sums to a multiple of 5.
Sample Input 2
7 10 4 7 0 9 1 6 4 2
Sample Output 2
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
N 個の英小文字列 S_1,S_2,\ldots,S_N および整数 L が与えられます。
長さ L の英小文字列であって、S_1,S_2,\ldots,S_N をすべて部分文字列として含むものの個数を 998244353 で割った余りを求めてください。
部分文字列とは
S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。例えば、
ab
, bc
, bcd
は abcd
の部分文字列ですが、ac
, dc
, e
は abcd
の部分文字列ではありません。
制約
- 1\leq N\leq 8
- 1\leq L\leq 100
- N,L は整数
- S_i は長さが 1 以上 10 以下の英小文字からなる文字列
- S_i\neq S_j\ (i\neq j)
入力
入力は以下の形式で標準入力から与えられる。
N L S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
2 4 ab c
出力例 1
153
条件を満たす文字列として、たとえば abcz
や cabc
があります。acbd
は ab
を部分文字列として含まないため条件を満たしません。
入力例 2
2 6 abc cde
出力例 2
54
入力例 3
5 50 bbfogggj zkbach eedirhyc ffgd oemmswj
出力例 3
689020583
Score : 550 points
Problem Statement
You are given N lowercase English strings S_1,S_2,\ldots,S_N, and an integer L.
Find the number, modulo 998244353, of length-L lowercase English strings that contain all of S_1,S_2,\ldots,S_N as substrings.
What is a substring?
A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.For example,
ab
, bc
, and bcd
are substrings of abcd
, while ac
, dc
, and e
are not substrings of abcd
.
Constraints
- 1\leq N\leq 8
- 1\leq L\leq 100
- N and L are integers.
- Each S_i is a string of length 1 and 10, inclusive, consisting of lowercase English letters.
- S_i\neq S_j\ (i\neq j)
Input
The input is given from Standard Input in the following format:
N L S_1 S_2 \vdots S_N
Output
Output the answer.
Sample Input 1
2 4 ab c
Sample Output 1
153
Some of the strings that satisfy the condition are abcz
and cabc
. acbd
does not contain ab
as a substring, so it does not satisfy the condition.
Sample Input 2
2 6 abc cde
Sample Output 2
54
Sample Input 3
5 50 bbfogggj zkbach eedirhyc ffgd oemmswj
Sample Output 3
689020583
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
頂点に 1 から N の番号がついた N 頂点 M 辺の単純連結無向グラフが与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。
各 k=1,2,\ldots,N-1 に対して、頂点 1 から頂点 N までの単純パスであって、パスに含まれる辺の個数が k であるようなものの個数を求めてください。
制約
- 2\leq N\leq 2\times 10^5
- N-1\leq M\leq N+20
- 1\leq u_i\lt v_i\leq N
- 与えられるグラフは単純連結無向グラフ
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを以下の形式で出力せよ。
\mathrm{ans}_1 \mathrm{ans}_2 \ldots \mathrm{ans}_{N-1}
\mathrm{ans}_i は頂点 1 から頂点 N までの単純パスであって、パスに含まれる辺の個数が i であるようなものの個数である。
入力例 1
5 6 1 2 1 3 2 4 3 4 3 5 4 5
出力例 1
0 1 2 1
各 k=1,2,3,4 について、頂点 1 から頂点 5 までの単純パスであって、パスに含まれる辺の個数が k であるようなものは以下の通りです。
- k=1 : 存在しない
- k=2 : 1\to 3\to 5
- k=3 : 1\to 2\to 4\to 5 および 1\to 3\to 4\to 5
- k=4 : 1\to 2\to 4\to 3\to 5
入力例 2
11 15 1 2 1 3 2 3 3 4 3 5 4 5 5 6 5 7 6 7 7 8 7 9 8 9 9 10 9 11 10 11
出力例 2
0 0 0 0 1 5 10 10 5 1
入力例 3
7 18 6 7 4 5 1 7 2 7 1 4 2 5 4 6 2 3 5 6 5 7 1 5 2 4 2 6 1 2 1 3 3 4 1 6 3 5
出力例 3
1 3 11 29 50 42
Score : 600 points
Problem Statement
You are given a simple connected undirected graph with N vertices numbered 1 to N and M edges. The i-th edge connects vertices u_i and v_i.
For each k=1,2,\ldots,N-1, find the number of simple paths from vertex 1 to vertex N that contain exactly k edges.
Constraints
- 2\leq N\leq 2\times 10^5
- N-1\leq M\leq N+20
- 1\leq u_i\lt v_i\leq N
- The given graph is a simple connected undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Output the answers in the following format:
\mathrm{ans}_1 \mathrm{ans}_2 \ldots \mathrm{ans}_{N-1}
\mathrm{ans}_i is the number of simple paths from vertex 1 to vertex N that contain exactly i edges.
Sample Input 1
5 6 1 2 1 3 2 4 3 4 3 5 4 5
Sample Output 1
0 1 2 1
For each k=1,2,3,4, the simple paths from vertex 1 to vertex 5 that contain exactly k edges are as follows.
- k=1 : None
- k=2 : 1\to 3\to 5
- k=3 : 1\to 2\to 4\to 5 and 1\to 3\to 4\to 5
- k=4 : 1\to 2\to 4\to 3\to 5
Sample Input 2
11 15 1 2 1 3 2 3 3 4 3 5 4 5 5 6 5 7 6 7 7 8 7 9 8 9 9 10 9 11 10 11
Sample Output 2
0 0 0 0 1 5 10 10 5 1
Sample Input 3
7 18 6 7 4 5 1 7 2 7 1 4 2 5 4 6 2 3 5 6 5 7 1 5 2 4 2 6 1 2 1 3 3 4 1 6 3 5
Sample Output 3
1 3 11 29 50 42