Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋くんは、N 個の品物と 1 つのカバンを持っています。
i 番目 (1\le i\le N) の品物の大きさは A _ i で、カバンの大きさは M です。
カバンに入れようとしている品物の大きさの合計が M 以下のとき、かつそのときに限り、それらの品物をすべて同時にカバンに入れることができます。
高橋くんが N 個の品物すべてを同時にカバンに入れることができるなら Yes
、そうでなければ No
と出力してください。
制約
- 1\le N\le100
- 1\le M\le10000
- 1\le A _ i\le100\ (1\le i\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N
出力
高橋くんがすべての品物を同時にカバンに入れられるなら Yes
を、そうでなければ No
を出力せよ。
入力例 1
5 15 3 1 4 1 5
出力例 1
Yes
5 つの品物の大きさの合計は 3+1+4+1+5=14 です。
これは、カバンの大きさ 15 以下なので、高橋くんはすべての品物を同時にカバンに入れることができます。
なので、Yes
を出力してください。
入力例 2
5 5 3 1 4 1 5
出力例 2
No
5 つの品物の大きさの合計は 14 で、カバンの大きさ 5 より大きいため、高橋くんはすべての品物を同時にカバンに入れることができません。
なので、No
を出力してください。
入力例 3
1 10000 100
出力例 3
Yes
Score : 100 points
Problem Statement
Takahashi has N items and one bag.
The size of the i-th (1\le i\le N) item is A_i, and the size of the bag is M.
If and only if the total size of the items he is trying to put in the bag is at most M, he can put all those items in the bag simultaneously.
If he can put all N items in the bag simultaneously, print Yes
; otherwise, print No
.
Constraints
- 1\le N\le100
- 1\le M\le10000
- 1\le A_i\le100\ (1\le i\le N)
- All input values are integers.
Input
The input is given from standard input in the following format:
N M A_1 A_2 \ldots A_N
Output
If Takahashi can put all items in the bag simultaneously, print Yes
; otherwise, print No
.
Sample Input 1
5 15 3 1 4 1 5
Sample Output 1
Yes
The total size of the 5 items is 3+1+4+1+5=14.
Since this is not greater than the bag size 15, Takahashi can put all items in the bag simultaneously.
Thus, print Yes
.
Sample Input 2
5 5 3 1 4 1 5
Sample Output 2
No
The total size of the 5 items is 14, which is greater than the bag size 5, so he cannot put all items in the bag simultaneously.
Thus, print No
.
Sample Input 3
1 10000 100
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
下の画像で示す図において、a 番の点と b 番の点が線で直接結ばれているかを答えてください。
制約
- 1 \leq a \lt b \leq 15
- a,b は整数
入力
入力は以下の形式で標準入力から与えられる。
a b
出力
a 番の点と b 番の点が線で直接結ばれているなら Yes
、結ばれていないなら No
を出力せよ。
入力例 1
1 2
出力例 1
Yes
問題文で示した図において、1 番の点と 2 番の点は線で直接結ばれています。 よって、Yes
を出力します。
入力例 2
2 8
出力例 2
No
問題文で示した図において、2 番の点と 8 番の点は線で直接結ばれていません。 よって、No
を出力します。
入力例 3
14 15
出力例 3
No
Score : 100 points
Problem Statement
Determine if there is a segment that directly connects the points numbered a and b in the figure below.
Constraints
- 1 \leq a \lt b \leq 15
- a and b are integers.
Input
The input is given from Standard Input in the following format:
a b
Output
Print Yes
if there is a segment that directly connects the points numbered a and b; print No
otherwise.
Sample Input 1
1 2
Sample Output 1
Yes
In the figure in the Problem Statement, there is a segment that directly connects the points numbered 1 and 2, so Yes
should be printed.
Sample Input 2
2 8
Sample Output 2
No
In the figure in the Problem Statement, there is no segment that directly connects the points numbered 2 and 8, so No
should be printed.
Sample Input 3
14 15
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。
長さ 8 の文字列 S が与えられます。S には K
, Q
がちょうど 1 文字ずつ、R
, B
, N
がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。
-
S において左から x,y\ (x < y) 文字目が
B
であるとする。このとき x と y の偶奇が異なる。 -
K
は 2 つのR
の間にある。より形式的には、S において左から x,y\ (x < y) 文字目がR
であり、 z 文字目がK
であるとする。このとき、 x< z < y が成り立つ。
制約
- S は 長さ 8 の文字列であり、
K
,Q
がちょうど 1 文字ずつ、R
,B
,N
がちょうど 2 文字ずつ含まれる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が条件を満たす場合 Yes
を、そうでない場合 No
を出力せよ。
入力例 1
RNBQKBNR
出力例 1
Yes
B
は左から 3 番目、6 番目にあり、3 と 6 は偶奇が異なります。
また、K
は 2 つの R
の間にあります。よって条件を満たします。
入力例 2
KRRBBNNQ
出力例 2
No
K
は 2 つの R
の間にありません。
入力例 3
BRKRBQNN
出力例 3
No
Score : 200 points
Problem Statement
Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.
You are given a string S of length eight. S has exactly one K
and Q
, and exactly two R
's, B
's , and N
's. Determine if S satisfies all of the following conditions.
-
Suppose that the x-th and y-th (x < y) characters from the left of S are
B
; then, x and y have different parities. -
K
is between twoR
's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S areR
and the z-th isK
; then x< z < y.
Constraints
- S is a string of length 8 that contains exactly one
K
andQ
, and exactly twoR
's,B
's , andN
's.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes
if S satisfies the conditions; print No
otherwise.
Sample Input 1
RNBQKBNR
Sample Output 1
Yes
The 3-rd and 6-th characters are B
, and 3 and 6 have different parities.
Also, K
is between the two R
's. Thus, the conditions are fulfilled.
Sample Input 2
KRRBBNNQ
Sample Output 2
No
K
is not between the two R
's.
Sample Input 3
BRKRBQNN
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
キーエンスには世界各地に N 個の拠点があり、1 から N までの番号が付けられています。 拠点 i には W_i 人の社員が所属しており、世界標準時で 0 時のとき拠点 i は X_i 時です。
あなたはキーエンス全社で 1 時間の会議を開きたいです。 各社員は、会議の開催時間帯が所属する拠点における 9:00-18:00 の時間帯に完全に含まれる場合にのみ会議に参加できます。 なるべく多くの社員が参加できるように会議の開催時間帯を決めるとき、会議に参加できる社員の数の最大値を求めてください。
制約
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N W_1 X_1 W_2 X_2 \vdots W_N X_N
出力
会議に参加できる社員の数の最大値を出力せよ。
入力例 1
3 5 0 3 3 2 18
出力例 1
8
世界標準時で 14:00-15:00 の時間帯に会議を開催することを考えます。
- 拠点 1 における 14:00-15:00 の時間帯に会議が開催されるため、拠点 1 に所属する 5 人の社員は会議に参加できます。
- 拠点 2 における 17:00-18:00 の時間帯に会議が開催されるため、拠点 2 に所属する 3 人の社員は会議に参加できます。
- 拠点 3 における 8:00-9:00 の時間帯に会議が開催されるため、拠点 3 に所属する 2 人の社員は会議に参加できません。
よって、合計で 5+3=8 人の社員が会議に参加できます。 これより多くの社員が参加できるような会議の開催時間帯はありません。
入力例 2
2 1 10 1000000 20
出力例 2
1000000
入力例 3
6 31 3 20 8 11 5 4 3 47 14 1 18
出力例 3
67
Score : 250 points
Problem Statement
Keyence has N bases worldwide, numbered 1 to N. Base i has W_i employees, and at 0 o'clock in Coordinated Universal Time (UTC), it is X_i o'clock at base i.
You want to hold a one-hour meeting across the entire company. Each employee can only participate in the meeting if the meeting time is completely within the 9:00-18:00 time slot at their base. Find the maximum number of employees who can participate when deciding the meeting time to allow as many employees as possible to participate.
Constraints
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W_1 X_1 W_2 X_2 \vdots W_N X_N
Output
Print the maximum number of employees who can participate in the meeting.
Sample Input 1
3 5 0 3 3 2 18
Sample Output 1
8
Consider holding the meeting from 14:00 to 15:00 in UTC.
- The meeting is held from 14:00 to 15:00 at base 1, so the 5 employees at base 1 can participate in the meeting.
- The meeting is held from 17:00 to 18:00 at base 2, so the 3 employees at base 2 can participate in the meeting.
- The meeting is held from 8:00 to 9:00 at base 3, so the 2 employees at base 3 cannot participate in the meeting.
Thus, a total of 5+3=8 employees can participate in the meeting. No meeting time allows more employees to participate.
Sample Input 2
2 1 10 1000000 20
Sample Output 2
1000000
Sample Input 3
6 31 3 20 8 11 5 4 3 47 14 1 18
Sample Output 3
67
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。
長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。
- C を A と B を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
- C を昇順にソートする。
A _ 1,A _ 2,\ldots,A _ N と B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。
制約
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
出力
答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
入力例 1
4 3 3 14 15 92 6 53 58
出力例 1
1 3 4 7 2 5 6
C は (3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。
入力例 2
4 4 1 2 3 4 100 200 300 400
出力例 2
1 2 3 4 5 6 7 8
入力例 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
出力例 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Score : 300 points
Problem Statement
You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.
- Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
- Sort C in ascending order.
For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.
Constraints
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
Output
Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.
Sample Input 1
4 3 3 14 15 92 6 53 58
Sample Output 1
1 3 4 7 2 5 6
C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).
Sample Input 2
4 4 1 2 3 4 100 200 300 400
Sample Output 2
1 2 3 4 5 6 7 8
Sample Input 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
Sample Output 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A があり、最初 A_i = i です。 以下のクエリを全部で Q 個処理してください。
- タイプ 1 : A_p を x に変更する。
- タイプ 2 : A_p を出力する。
- タイプ 3 : 「 A の先頭の要素を末尾にする」という操作を k 回繰り返す。
- 厳密には、 A を (A_2,A_3,\dots,A_N,A_1) に置き換えることを k 回繰り返す。
制約
- 入力は全て整数
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- 全てのクエリは以下の制約を満たす
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 p x
タイプ 2 のクエリは以下の形式で与えられる。
2 p
タイプ 3 のクエリは以下の形式で与えられる。
3 k
出力
タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
入力例 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
出力例 1
3 1 1000000
この入力には 5 個のクエリが含まれます。
- 最初、 A=(1,2,3,4,5) です。
- 1 番目のクエリはタイプ 2 で、 A_3=3 を出力します。
- 2 番目のクエリはタイプ 1 で、 A_2=1000000 に置き換えます。
- クエリの結果、 A=(1,1000000,3,4,5) となります。
- 3 番目のクエリはタイプ 3 で、「 A の先頭の要素を末尾にする」という操作を 4 回繰り返します。
- クエリの結果、 A=(5,1,1000000,3,4) となります。
- 4 番目のクエリはタイプ 2 で、 A_2=1 を出力します。
- 5 番目のクエリはタイプ 2 で、 A_3=1000000 を出力します。
入力例 2
1000000 2 1 1000000 999999 3 1000000000
出力例 2
出力が空である場合もあります。
Score : 300 points
Problem Statement
There is an integer sequence A of length N, initially A_i = i. Process a total of Q queries of the following types:
- Type 1: Change A_p to x.
- Type 2: Output A_p.
- Type 3: Repeat the operation "move the first element of A to the end" k times.
- Formally, replace A with (A_2,A_3,\dots,A_N,A_1) k times.
Constraints
- All input values are integers.
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- All queries satisfy the following constraints:
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
Input
The input is given from Standard Input in the following format:
N Q \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
Here, \rm{Query}_i represents the i-th query.
Type 1 queries are given in the following format:
1 p x
Type 2 queries are given in the following format:
2 p
Type 3 queries are given in the following format:
3 k
Output
For each Type 2 query, output the answer on a line.
Sample Input 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
Sample Output 1
3 1 1000000
This input contains 5 queries.
- Initially, A=(1,2,3,4,5).
- The 1st query is Type 2: output A_3=3.
- The 2nd query is Type 1: replace A_2 with 1000000.
- After the query, A=(1,1000000,3,4,5).
- The 3rd query is Type 3: repeat the operation "move the first element of A to the end" 4 times.
- After the query, A=(5,1,1000000,3,4).
- The 4th query is Type 2: output A_2=1.
- The 5th query is Type 2: output A_3=1000000.
Sample Input 2
1000000 2 1 1000000 999999 3 1000000000
Sample Output 2
The output may be empty.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N 頂点 M 辺の単純無向グラフ G があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。
あなたは、以下の 2 つの操作を好きな順序で好きな回数繰り返すことができます。
- G に無向辺を 1 つ追加する
- G から無向辺を 1 つ削除する
G をすべての頂点の次数が 2 である単純無向グラフにするために行う操作回数として考えられる最小値を求めてください。
単純無向グラフとは
単純無向グラフとは、自己ループと多重辺を持たない無向グラフのことを指します。
制約
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- 入力で与えられるグラフ G は単純無向グラフ
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
5 4 1 2 1 5 2 4 4 5
出力例 1
3
例えば以下のようにすることで、3 回の操作で G をすべての頂点の次数が 2 の単純無向グラフにすることができます。
- 頂点 2 と頂点 3 を結ぶ辺を追加する。
- 頂点 2 と頂点 4 を結ぶ辺を削除する。
- 頂点 3 と頂点 4 を結ぶ辺を追加する。
入力例 2
3 0
出力例 2
3
入力例 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
出力例 3
2
入力例 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
出力例 4
13
Score : 425 points
Problem Statement
There is a simple undirected graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and the i-th edge is an undirected edge connecting vertices A_i and B_i.
You can repeat the following two operations in any order and any number of times:
- Add one undirected edge to G
- Remove one undirected edge from G
Find the minimum number of operations to make G a simple undirected graph where all vertices have degree 2.
What is a simple undirected graph?
A simple undirected graph refers to an undirected graph that has no self-loops and no multi-edges.
Constraints
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- The graph G given in the input is a simple undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Output the answer.
Sample Input 1
5 4 1 2 1 5 2 4 4 5
Sample Output 1
3
For example, the following three operations make G a simple undirected graph where all vertices have degree 2.
- Add an edge connecting vertices 2 and 3.
- Remove the edge connecting vertices 2 and 4.
- Add an edge connecting vertices 3 and 4.
Sample Input 2
3 0
Sample Output 2
3
Sample Input 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
Sample Output 3
2
Sample Input 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
Sample Output 4
13
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
この問題は問題 G と似た設定です。問題文の相違点を赤字で示します。
H 行 W 列のグリッドがあり、グリッドの各マスは赤色あるいは緑色に塗られています。
グリッドの上から i 行目、左から j 列目のマスをマス (i,j) と表記します。
マス (i,j) の色は文字 S_{i,j} で表され、S_{i,j} = .
のときマス (i,j) は赤色、S_{i,j} = #
のときマス (i,j) は緑色に塗られています。
グリッドにおいて、緑色に塗られたマスを頂点集合、隣り合った 2 つの緑色のマスを結ぶ辺全体を辺集合としたグラフにおける連結成分の個数を 緑の連結成分数 と呼びます。ただし、2 つのマス (x,y) と (x',y') が隣り合っているとは、|x-x'| + |y-y'| = 1 であることを指します。
赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えたとき、塗り替え後のグリッドの緑の連結成分数の期待値を \text{mod } 998244353 で出力してください。
「期待値を \text{mod } 998244353 で出力」とは
求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、 R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ 1 つ存在することが証明できます。この R を出力してください。制約
- 1 \leq H,W \leq 1000
- S_{i,j} =
.
または S_{i,j} =#
- S_{i,j} =
.
なる (i,j) が存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W S_{1,1}S_{1,2}\ldotsS_{1,W} S_{2,1}S_{2,2}\ldotsS_{2,W} \vdots S_{H,1}S_{H,2}\ldotsS_{H,W}
出力
答えを出力せよ。
入力例 1
3 3 ##. #.# #..
出力例 1
499122178
マス (1,3) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (2,2) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (3,2) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
マス (3,3) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
よって、赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えた後の緑の連結成分数の期待値は (1+1+2+2)/4 = 3/2 となります。
入力例 2
4 5 ..#.. .###. ##### ..#..
出力例 2
598946613
入力例 3
3 4 #... .#.# ..##
出力例 3
285212675
Score : 450 points
Problem Statement
This problem has a similar setting to Problem G. Differences in the problem statement are indicated in red.
There is a grid with H rows and W columns, where each cell is painted red or green.
Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The color of cell (i,j) is represented by the character S_{i,j}, where S_{i,j} = .
means cell (i,j) is red, and S_{i,j} = #
means cell (i,j) is green.
The number of green connected components in the grid is the number of connected components in the graph with the vertex set being the green cells and the edge set being the edges connecting two adjacent green cells. Here, two cells (x,y) and (x',y') are considered adjacent when |x-x'| + |y-y'| = 1.
Consider choosing one red cell uniformly at random and repainting it green. Print the expected value of the number of green connected components in the grid after repainting, modulo 998244353.
What does "print the expected value modulo 998244353" mean?
It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if that value is expressed as \frac{P}{Q} using two coprime integers P and Q, there is exactly one integer R such that R \times Q \equiv P \pmod{998244353} and 0 \leq R < 998244353. Print this R.Constraints
- 1 \leq H,W \leq 1000
- S_{i,j} =
.
or S_{i,j} =#
. - There is at least one (i,j) such that S_{i,j} =
.
.
Input
The input is given from Standard Input in the following format:
H W S_{1,1}S_{1,2}\ldotsS_{1,W} S_{2,1}S_{2,2}\ldotsS_{2,W} \vdots S_{H,1}S_{H,2}\ldotsS_{H,W}
Output
Print the answer.
Sample Input 1
3 3 ##. #.# #..
Sample Output 1
499122178
If cell (1,3) is repainted green, the number of green connected components becomes 1.
If cell (2,2) is repainted green, the number of green connected components becomes 1.
If cell (3,2) is repainted green, the number of green connected components becomes 2.
If cell (3,3) is repainted green, the number of green connected components becomes 2.
Therefore, the expected value of the number of green connected components after choosing one red cell uniformly at random and repainting it green is (1+1+2+2)/4 = 3/2.
Sample Input 2
4 5 ..#.. .###. ##### ..#..
Sample Output 2
598946613
Sample Input 3
3 4 #... .#.# ..##
Sample Output 3
285212675
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
英小文字のみからなる文字列 S が与えられます。 下記の条件を満たす空でない文字列 T の個数を 998244353 で割ったあまりを出力してください。
T を 2 つ連結して得られる文字列 TT が、S に(連続とは限らない)部分列として含まれる。
制約
- S は英小文字のみからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ababbaba
出力例 1
8
問題文中の条件を満たす文字列 T は、a
、aa
、ab
、aba
、b
、ba
、bab
、bb
の 8 個です。
入力例 2
zzz
出力例 2
1
問題文中の条件を満たす文字列 T は、z
のみです。
S = S_1S_2S_3 = zzz
から、文字列 zz
を部分列として得る方法は、
S_1S_2 = zz
、S_1S_3 = zz
、S_2S_3 = zz
の 3 通りありますが、文字列 z
は答えに 1 回だけ寄与することに注意してください。
入力例 3
ppppqqppqqqpqpqppqpqqqqpppqppq
出力例 3
580
Score : 500 points
Problem Statement
You are given a string S consisting of lowercase English letters. Print the number of non-empty strings T that satisfy the following condition, modulo 998244353.
The concatenation TT of two copies of T is a subsequence of S (not necessarily contiguous).
Constraints
- S is a string consisting of lowercase English letters whose length is between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ababbaba
Sample Output 1
8
The eight strings satisfying the condition are a
, aa
, ab
, aba
, b
, ba
, bab
, and bb
.
Sample Input 2
zzz
Sample Output 2
1
The only string satisfying the condition is z
.
Note that this string contributes to the answer just once, although there are three ways to extract the subsequence zz
from S = S_1S_2S_3 = zzz
: S_1S_2 = zz
, S_1S_3 = zz
, and S_2S_3 = zz
.
Sample Input 3
ppppqqppqqqpqpqppqpqqqqpppqppq
Sample Output 3
580