E - Yamanote Line Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君と青木君は 2 人で次の対戦ゲームをします。

高橋君が先手でゲームを始め、ゲームが終了するまでの間、 2 人は交互に 1 以上 2N+1 以下の整数を 1 つずつ宣言します。 どちらかが一度でも宣言した整数は、それ以降どちらも二度と宣言することが出来ません。 先に整数を宣言することが出来なくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。

このゲームでは必ず高橋君が勝ちます。 高橋君の立場で実際にゲームを行い、ゲームに勝ってください。

制約

  • 1 \leq N \leq 1000
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
あなたのプログラムが高橋君の立場で、ジャッジプログラムが青木君の立場でゲームを行います。

まず、あなたのプログラムに標準入力から正の整数 N が与えられます。 その後、ゲームが終了するまで下記の手順を繰り返します。

  1. あなたのプログラムが、高橋君が宣言する整数として、1 以上 2N+1 以下の整数を標準出力に出力します。(どちらかのプレイヤーによってすでに宣言されている整数を出力することは出来ません。)
  2. ジャッジプログラムによって、青木君が宣言する整数があなたのプログラムに標準入力から与えられます。(どちらかのプレイヤーによってすでに宣言されている整数が入力されることはありません。) ただし、青木君が宣言できる整数が残っていない場合は、代わりに 0 が与えられ高橋君の勝ちでゲームが終了します。

注意点

  • 出力を行うたびに標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 高橋君の勝ちでゲームが終了したあと、あなたのプログラムは直ちに終了しなければなりません。そうしなかった場合、ジャッジ結果が AC とならない可能性があります。
  • ゲームの途中で不正な出力を行った場合(例えば、すでにどちらかのプレイヤーによって宣言されている整数を出力した場合)は不正解となりますが、そのときのジャッジ結果は不定です。WA になるとは限りません。

入出力例

入力 出力 説明
2 まず整数 N が与えられます。
1 高橋君が 1 を宣言します。
3 青木君が 3 を宣言します。
2 高橋君が 2 を宣言します。
4 青木君が 4 を宣言します。
5 高橋君が 5 を宣言します。
0 青木君が宣言できる整数が残っていないため、高橋君の勝ちでゲームが終了します。

Score : 300 points

Problem Statement

Takahashi and Aoki will play the following game against each other.

Starting from Takahashi, the two alternatingly declare an integer between 1 and 2N+1 (inclusive) until the game ends. Any integer declared by either player cannot be declared by either player again. The player who is no longer able to declare an integer loses; the player who didn't lose wins.

In this game, Takahashi will always win. Your task is to actually play the game on behalf of Takahashi and win the game.

Constraints

  • 1 \leq N \leq 1000
  • N is an integer.

Input and Output

This task is an interactive task (in which your program and the judge program interact with each other via inputs and outputs).
Your program plays the game on behalf of Takahashi, and the judge program plays the game on behalf of Aoki.

First, your program is given a positive integer N from Standard Input. Then, the following procedures are repeated until the game ends.

  1. Your program outputs an integer between 1 and 2N+1 (inclusive) to Standard Output, which defines the integer that Takahashi declares. (You cannot output an integer that is already declared by either player.)
  2. The integer that Aoki declares is given by the judge program to your program from Standard Input. (No integer that is already declared by either player will be given.) If Aoki has no more integer to declare, 0 is given instead, which means that the game ended and Takahashi won.

Notes

  • After each output, you must flush Standard Output. Otherwise, you may get TLE.
  • After the game ended and Takahashi won, the program must be terminated immediately. Otherwise, the judge does not necessarily give AC.
  • If your program outputs something that violates the rules of the game (such as an integer that has already been declared by either player), your answer is considered incorrect. In such case, the verdict is indeterminate. It does not necessarily give WA.

Sample Input and Output

Input Output Description
2 First, an integer N is given.
1 Takahashi declares an integer 1.
3 Aoki declares an integer 3.
2 Takahashi declares an integer 2.
4 Aoki declares an integer 4.
5 Takahashi declares an integer 5.
0 Aoki has no more integer to declare, so Takahashi wins, and the game ends.
F - Just K

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字のみからなる N 個の文字列 S_1,S_2,\dots,S_N が与えられます。

S_1,S_2,\dots,S_N から文字列を好きな個数選ぶことを考えます。

このとき、「選んだ文字列の中でちょうど K 個の文字列に登場する英小文字」の種類数としてあり得る最大値を求めてください。

制約

  • 1 \le N \le 15
  • 1 \le K \le N
  • N,K は整数
  • S_i は英小文字からなる空でない文字列である。
  • 1 \le i \le N を満たす整数 i に対し、S_i に同じ文字は 2 個以上含まれない。
  • i \neq j ならば S_i \neq S_j である。

入力

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

N K
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

4 2
abi
aef
bc
acg

出力例 1

3

S_1,S_3,S_4 を選んだ場合、a,b,c がちょうど 2 個の文字列に含まれます。

4 個以上の文字がちょうど 2 個の文字列に含まれるような選び方は存在しないため、答えは 3 です。


入力例 2

2 2
a
b

出力例 2

0

同じ文字列を複数回選ぶことはできません。


入力例 3

5 2
abpqxyz
az
pq
bc
cy

出力例 3

7

Score : 300 points

Problem Statement

You are given N strings S_1,S_2,\dots,S_N consisting of lowercase English alphabets.

Consider choosing some number of strings from S_1,S_2,\dots,S_N.

Find the maximum number of distinct alphabets that satisfy the following condition: "the alphabet is contained in exactly K of the chosen strings."

Constraints

  • 1 \le N \le 15
  • 1 \le K \le N
  • N and K are integers.
  • S_i is a non-empty string consisting of lowercase English alphabets.
  • For each integer i such that 1 \le i \le N, S_i does not contain two or more same alphabets.
  • If i \neq j, then S_i \neq S_j.

Input

Input is given from Standard Input in the following format:

N K
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

4 2
abi
aef
bc
acg

Sample Output 1

3

When S_1,S_3, and S_4 are chosen, a,b, and c occur in exactly two of the strings.

There is no way to choose strings so that 4 or more alphabets occur in exactly 2 of the strings, so the answer is 3.


Sample Input 2

2 2
a
b

Sample Output 2

0

You cannot choose the same string more than once.


Sample Input 3

5 2
abpqxyz
az
pq
bc
cy

Sample Output 3

7
G - Step Up Robot

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

無限に続く階段があります。 一番下は 0 段目で、1 段のぼるごとに 1 段目、2 段目と続きます。

0 段目に階段登りロボットがいます。 階段登りロボットは、一回の動作で A _ 1,A _ 2,\ldots,A _ N 段ぶん階段をのぼることができます。 つまり、階段登りロボットが i 段目にいるとき、一回動作をした後は i+A _ 1 段目、i+A _ 2 段目、⋯、i+A _ N 段目のいずれかにいることができます。 それ以外の段数を一回の動作でのぼることはできません。 階段登りロボットは階段を下ることもできません。

階段の B _ 1,B _ 2,\ldots,B _ M 段目にはモチが設置されています。 モチが設置されている段へのぼるとロボットは動けなくなり、他の段に移動することができなくなります。

階段登りロボットは階段のちょうど X 段目にのぼりたいです。 階段登りロボットが階段のちょうど X 段目にのぼることが可能か判定してください。

制約

  • 1\leq N\leq10
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^5
  • 1\leq M\leq10^5
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\lt X\leq10^5
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N
M
B _ 1 B _ 2 \ldots B _ M
X

出力

階段登りロボットが階段のちょうど X 段目にのぼることができるとき Yes を、そうでないとき No1 行に出力せよ。


入力例 1

3
3 4 5
4
4 5 6 8
15

出力例 1

Yes

例えば、次のようにして 15 段目に到達することができます。

  • 階段を 3 段のぼる。ロボットは 3 段目に移動する。
  • 階段を 4 段のぼる。ロボットは 7 段目に移動する。
  • 階段を 5 段のぼる。ロボットは 12 段目に移動する。
  • 階段を 3 段のぼる。ロボットは 15 段目に移動する。

入力例 2

4
2 3 4 5
4
3 4 5 6
8

出力例 2

No

どのように移動しても階段登りロボットが階段のちょうど 8 段目にいることはできません。


入力例 3

4
2 5 7 8
5
2 9 10 11 19
20

出力例 3

Yes

Score : 400 points

Problem Statement

There is a staircase with infinite steps. The foot of the stairs is the 0-th step, the next step is the 1-st step, the next is the 2-nd, and so on.

There is a stair-climbing robot on the 0-th step. The robot can climb up A _ 1,A _ 2,\ldots, or A _ N steps at a time. In other words, when the robot is on the i-th step, it can step onto one of the (i+A _ 1)-th step, (i+A _ 2)-th step, \ldots, and (i+A _ N)-th step, but not onto the others in a single step. The robot cannot descend the stairs, either.

There are traps on the B _ 1-th, B _ 2-th, \ldots, and B _ M-th steps. Once the robot steps onto a step with a trap, it cannot move anymore.

The robot wants to step onto the X-th step. Determine whether it is possible to do so.

Constraints

  • 1\leq N\leq10
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^5
  • 1\leq M\leq10^5
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\lt X\leq10^5
  • 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
M
B _ 1 B _ 2 \ldots B _ M
X

Output

In a single line, print Yes if the robot can step onto the X-th step, and No otherwise.


Sample Input 1

3
3 4 5
4
4 5 6 8
15

Sample Output 1

Yes

For example, the robot can reach the 15-th step as follows.

  • Climb up 3 steps. The robot is now on the 3-rd step.
  • Climb up 4 steps. The robot is now on the 7-th step.
  • Climb up 5 steps. The robot is now on the 12-th step.
  • Climb up 3 steps. The robot is now on the 15-th step.

Sample Input 2

4
2 3 4 5
4
3 4 5 6
8

Sample Output 2

No

No matter how the robot moves, it cannot step onto the 8-th step.


Sample Input 3

4
2 5 7 8
5
2 9 10 11 19
20

Sample Output 3

Yes
H - Modulo MST

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の重み付き単純連結無向グラフと正整数 K が与えられます。
i\ (1\leq i\leq M) は頂点 u _ i​ と頂点 v _ i を結んでおり、重みは w _ i です。

このグラフの全域木 T に対して、T のコストを T に含まれる辺の重みの総和を K で割ったあまりで定めます。 このグラフの全域木のコストの最小値を求めてください。

制約

  • 2\leq N\leq8
  • N-1\leq M\leq\dfrac{N(N-1)}2
  • 1\leq K\leq10^{15}
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
  • 0\leq w _ i\lt K\ (1\leq i\leq M)
  • 与えられるグラフは単純かつ連結
  • 入力はすべて整数

入力

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

N M K
u _ 1 v _ 1 w _ 1
u _ 2 v _ 2 w _ 2
\vdots
u _ M v _ M w _ M

出力

答えを出力せよ。


入力例 1

5 6 328
1 2 99
1 3 102
2 3 86
2 4 94
2 5 95
3 4 81

出力例 1

33

与えられるグラフは次のようになります。

1,3,5,64 本を含む全域木のコストは (99+86+81+95)\bmod{328}=361\bmod{328}=33 となります。 このグラフの全域木のコストはすべて 33 以上であるため、33 を出力してください。


入力例 2

6 5 998244353
1 2 337361568
1 6 450343304
2 3 61477244
2 5 745383438
4 5 727360840

出力例 2

325437688

このグラフのただ一つの全域木のコスト 325437688 を出力してください。


入力例 3

8 28 936294041850197
1 2 473294720906780
1 3 743030800139244
1 4 709363019414774
1 5 383643612490312
1 6 557102781022861
1 7 623179288538138
1 8 739618599410809
2 3 857687812294404
2 4 893923168139714
2 5 581822471860662
2 6 740549363586558
2 7 307226438833222
2 8 447399029952998
3 4 636318083622768
3 5 44548707643622
3 6 307262781240755
3 7 12070267388230
3 8 700247263184082
4 5 560567890325333
4 6 704726113717147
4 7 588263818615687
4 8 549007536393172
5 6 779230871080408
5 7 825982583786498
5 8 713928998174272
6 7 751331074538826
6 8 449873635430228
7 8 11298381761479

出力例 3

11360716373

入力や答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。

Score : 475 points

Problem Statement

You are given a weighted simple connected undirected graph with N vertices and M edges, where vertices are numbered 1 to N, and edges are numbered 1 to M. Additionally, a positive integer K is given.
Edge i\ (1\leq i\leq M) connects vertices u_i and v_i and has a weight of w_i.

For a spanning tree T of this graph, the cost of T is defined as the sum, modulo K, of the weights of the edges in T. Find the minimum cost of a spanning tree of this graph.

Constraints

  • 2\leq N\leq8
  • N-1\leq M\leq\dfrac{N(N-1)}2
  • 1\leq K\leq10^{15}
  • 1\leq u_i\lt v_i\leq N\ (1\leq i\leq M)
  • 0\leq w_i\lt K\ (1\leq i\leq M)
  • The given graph is simple and connected.
  • All input values are integers.

Input

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

N M K
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

Output

Print the answer.


Sample Input 1

5 6 328
1 2 99
1 3 102
2 3 86
2 4 94
2 5 95
3 4 81

Sample Output 1

33

The given graph is shown below:

The cost of the spanning tree containing edges 1,3,5,6 is (99+86+81+95)\bmod{328}=361\bmod{328}=33. The cost of every spanning tree of this graph is at least 33, so print 33.


Sample Input 2

6 5 998244353
1 2 337361568
1 6 450343304
2 3 61477244
2 5 745383438
4 5 727360840

Sample Output 2

325437688

Print the cost of the only spanning tree of this graph, which is 325437688.


Sample Input 3

8 28 936294041850197
1 2 473294720906780
1 3 743030800139244
1 4 709363019414774
1 5 383643612490312
1 6 557102781022861
1 7 623179288538138
1 8 739618599410809
2 3 857687812294404
2 4 893923168139714
2 5 581822471860662
2 6 740549363586558
2 7 307226438833222
2 8 447399029952998
3 4 636318083622768
3 5 44548707643622
3 6 307262781240755
3 7 12070267388230
3 8 700247263184082
4 5 560567890325333
4 6 704726113717147
4 7 588263818615687
4 8 549007536393172
5 6 779230871080408
5 7 825982583786498
5 8 713928998174272
6 7 751331074538826
6 8 449873635430228
7 8 11298381761479

Sample Output 3

11360716373

Note that the input and the answer may not fit into a 32\operatorname{bit} integer.

I - Yet Another Grid Task

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

N \times M のグリッドがあります。
このグリッドの上から i 行目、左から j 列目のマスを (i,j) と書きます。
このグリッドの各マスは 白 か 黒 であり、その情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N として与えられます。

  • もし S_ij 文字目が . なら、マス (i,j) は 白 である。
  • もし S_ij 文字目が # なら、マス (i,j) は 黒 である。

以下の条件を満たすグリッドを 美しい グリッドと呼びます。

  • 全ての 1 \le i \le N, 1 \le j \le M を満たす整数組 (i,j) について、マス (i,j) が 黒 であれば、その下と右下のマスも (存在すれば) 黒 である。
  • 厳密には、以下の条件を全て満たす。
    • マス (i,j) が 黒 でありマス (i+1,j) が存在するなら、マス (i+1,j) も 黒 である。
    • マス (i,j) が 黒 でありマス (i+1,j+1) が存在するなら、マス (i+1,j+1) も 黒 である。

高橋くんは、 白 のマスを 0 個以上何個でも 黒 に塗ることができ、この操作によってグリッドを美しくしようとしています。
高橋くんが作ることのできる美しいグリッドの種類数を 998244353 で割った余りを求めてください。
但し、ある 2 つのグリッドが異なるとは、両者で色が異なるマスが存在することを指します。

制約

  • 1 \le N,M \le 2000
  • S_i.# からなる長さ M の文字列

入力

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

N M
S_1
S_2
\vdots
S_N

出力

答えを整数として出力せよ。


入力例 1

2 2
.#
..

出力例 1

3

作ることのできる美しいグリッドは以下の 3 種類です。

.#  .#  ##
.#  ##  ##

入力例 2

5 5
....#
...#.
..#..
.#.#.
#...#

出力例 2

92

入力例 3

25 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................

出力例 3

604936632

998244353 で割った余りを求めてください。

Score : 525 points

Problem Statement

There is an N \times M grid and a player standing on it.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left of this grid.
Each square of this grid is black or white, which is represented by N strings S_1,S_2,\dots,S_N of length M as follows:

  • if the j-th character of S_i is ., square (i,j) is white;
  • if the j-th character of S_i is #, square (i,j) is black.

The grid is said to be beautiful when the following condition is satisfied.

  • For every pair of integers (i,j) such that 1 \le i \le N, 1 \le j \le M, if square (i,j) is black, the square under (i,j) and the square to the immediate lower right of (i,j) are also black (if they exist).
  • Formally, all of the following are satisfied.
    • If square (i,j) is black and square (i+1,j) exists, square (i+1,j) is also black.
    • If square (i,j) is black and square (i+1,j+1) exists, square (i+1,j+1) is also black.

Takahashi can paint zero or more white squares black, and he will do so to make the grid beautiful.
Find the number of different beautiful grids he can make, modulo 998244353.
Two grids are considered different when there is a square that has different colors in those two grids.

Constraints

  • 1 \le N,M \le 2000
  • S_i is a string of length M consisting of . and #.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the answer as an integer.


Sample Input 1

2 2
.#
..

Sample Output 1

3

He can make the following three different beautiful grids:

.#  .#  ##
.#  ##  ##

Sample Input 2

5 5
....#
...#.
..#..
.#.#.
#...#

Sample Output 2

92

Sample Input 3

25 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................

Sample Output 3

604936632

Find the count modulo 998244353.