E - Popcorn

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

AtCoder Land には 1 から N までの番号が付けられた N 個のポップコーン売り場があります。 売られているポップコーンの味には味 1,2,\dots,M の M 種類がありますが、すべての売り場ですべての味のポップコーンを売っているわけではありません。

高橋君は、それぞれのポップコーン売り場でどの味のポップコーンを売っているかの情報を入手しました。 この情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N によって表され、S_i の j 文字目が o であるとき売り場 i が味 j のポップコーンを売っていることを、 x であるとき売っていないことを示します。 どの売り場も最低 1 種類の味のポップコーンを売っており、どの味のポップコーンも最低 1 つの売り場で売られています。

高橋君は全種類のポップコーンを食べたがっていますが、あまり何度も移動はしたくありません。 高橋君がすべての味のポップコーンを購入するためには最低何個の売り場を訪れる必要があるか求めてください。

制約

  • N,M は整数
  • 1\leq N,M \leq 10
  • S_i は o および x からなる長さ M の文字列
  • すべての i\ (1\leq i\leq N) について、S_i の中には o が 1 つ以上存在する
  • すべての j\ (1\leq j\leq M) について、S_i の j 文字目が o であるような i が 1 つ以上存在する

入力

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

N M
S_1
S_2
\vdots
S_N

出力

高橋君がすべての味のポップコーンを購入するために訪れる必要がある売り場の個数の最小値を出力せよ。


入力例 1

3 5
oooxx
xooox
xxooo

出力例 1

2

1 つめの売り場と 3 つめの売り場を訪れることで、すべての味のポップコーンを購入することができます。 1 つの売り場ですべての味のポップコーンを購入することはできないので、答えは 2 です。


入力例 2

3 2
oo
ox
xo

出力例 2

1

入力例 3

8 6
xxoxxo
xxoxxx
xoxxxx
xxxoxx
xxoooo
xxxxox
xoxxox
oxoxxo

出力例 3

3

Score : 300 points

Problem Statement

In AtCoder Land, there are N popcorn stands numbered 1 to N. They have M different flavors of popcorn, labeled 1, 2, \dots, M, but not every stand sells all flavors of popcorn.

Takahashi has obtained information about which flavors of popcorn are sold at each stand. This information is represented by N strings S_1, S_2, \dots, S_N of length M. If the j-th character of S_i is o, it means that stand i sells flavor j of popcorn. If it is x, it means that stand i does not sell flavor j. Each stand sells at least one flavor of popcorn, and each flavor of popcorn is sold at least at one stand.

Takahashi wants to try all the flavors of popcorn but does not want to move around too much. Determine the minimum number of stands Takahashi needs to visit to buy all the flavors of popcorn.

Constraints

  • N and M are integers.
  • 1 \leq N, M \leq 10
  • Each S_i is a string of length M consisting of o and x.
  • For every i (1 \leq i \leq N), there is at least one o in S_i.
  • For every j (1 \leq j \leq M), there is at least one i such that the j-th character of S_i is o.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the minimum number of stands Takahashi needs to visit to buy all the flavors of popcorn.


Sample Input 1

3 5
oooxx
xooox
xxooo

Sample Output 1

2

By visiting the 1st and 3rd stands, you can buy all the flavors of popcorn. It is impossible to buy all the flavors from a single stand, so the answer is 2.


Sample Input 2

3 2
oo
ox
xo

Sample Output 2

1

Sample Input 3

8 6
xxoxxo
xxoxxx
xoxxxx
xxxoxx
xxoooo
xxxxox
xoxxox
oxoxxo

Sample Output 3

3
F - Jumping Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は数直線上の座標 0 の位置にいます。

これから高橋君は N 回のジャンプを行います。i \, (1 \leq i \leq N) 回目のジャンプでは、正の方向に a_i または b_i 移動します。

N 回のジャンプの後に座標 X の位置にいるようにすることはできますか?

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • 入力は全て整数

入力

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

N X
a_1 b_1
\vdots
a_N b_N

出力

N 回のジャンプの後に座標 X の位置にいるようにすることができるならば Yes と、そうでないなら No と出力せよ。


入力例 1

2 10
3 6
4 5

出力例 1

Yes

1 回目のジャンプでは b_1 (= 6) 移動し、2 回目のジャンプでは a_2 (= 4) 移動することで、座標 X (= 10) の位置にいるようにすることができます。


入力例 2

2 10
10 100
10 100

出力例 2

No

1 回目のジャンプの後に座標 X (= 10) の位置にいるようにすることはできますが、全てのジャンプの後に座標 X (= 10) の位置にいるようにすることはできません。


入力例 3

4 12
1 8
5 7
3 4
2 6

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi is standing at the coordinate 0 on a number line.

He will now perform N jumps. In the i-th jump (1 \leq i \leq N), he moves a_i or b_i in the positive direction.

Is it possible for him to be at the coordinate X after N jumps?

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
a_1 b_1
\vdots
a_N b_N

Output

If it is possible for Takahashi to be at the coordinate X after N jumps, print Yes; otherwise, print No.


Sample Input 1

2 10
3 6
4 5

Sample Output 1

Yes

By moving b_1 (= 6) in the first jump and a_2 (= 4) in the second jump, he can be at the coordinate X (= 10).


Sample Input 2

2 10
10 100
10 100

Sample Output 2

No

He can be at the coordinate X (= 10) after the first jump, but not after all jumps.


Sample Input 3

4 12
1 8
5 7
3 4
2 6

Sample Output 3

Yes
G - Cuboid Sum Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

正整数 N と、 1 \leq x,y,z \leq N を満たす整数の組 (x,y,z) に対して、整数 A_{x,y,z} が与えられます。

次の形式の Q 個のクエリが与えられるので、それぞれに答えてください。

i 個目 (1 \leq i \leq Q) のクエリでは 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N,1 \leq Lz_i \leq Rz_i \leq N をすべて満たす整数の組 (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) が与えられるので、

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}

を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x,y,z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数

入力

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

出力

Q 行出力せよ。 i 行目には i 個目のクエリに対する答えを出力せよ。


入力例 1

2
1 2
3 4
5 6
7 8
2
1 2 2 2 1 1
2 2 1 2 1 2

出力例 1

10
26

1 個目のクエリについて、求めるべき値は A_{1,2,1}+A_{2,2,1}=3+7=10 です。よって、10 を出力します。

2 個目のクエリについて、求めるべき値は A_{2,1,1}+A_{2,1,2}+A_{2,2,1}+A_{2,2,2}=5+6+7+8=26 です。よって、26 を出力します。


入力例 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

出力例 2

687
3917
551
1631
5180
3311
1010
4326

Score : 400 points

Problem Statement

You are given a positive integer N, and an integer A_{x,y,z} for each triple of integers (x, y, z) such that 1 \leq x, y, z \leq N.

You will be given Q queries in the following format, which must be processed in order.

For the i-th query (1 \leq i \leq Q), you are given a tuple of integers (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) such that 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N, and 1 \leq Lz_i \leq Rz_i \leq N. Find:

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x, y, z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • All input values are integers.

Input

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

2
1 2
3 4
5 6
7 8
2
1 2 2 2 1 1
2 2 1 2 1 2

Sample Output 1

10
26

For the 1st query, the sought value is A_{1,2,1} + A_{2,2,1} = 3 + 7 = 10. Thus, print 10.

For the 2nd query, the sought value is A_{2,1,1} + A_{2,1,2} + A_{2,2,1} + A_{2,2,2} = 5 + 6 + 7 + 8 = 26. Thus, print 26.


Sample Input 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

Sample Output 2

687
3917
551
1631
5180
3311
1010
4326
H - Count Arithmetic Subsequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。各 k=1,2,\dots,N について、A の長さ k の(連続するとは限らない)部分列であって等差数列であるようなものの個数を 998244353 で割ったあまりを求めてください。ただし、2 つの部分列が列として同じでも、取り出す位置が異なるならば区別するものとします。

部分列とは 数列 A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。

制約

  • 1 \leq N \leq 80
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

k=1,2,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。


入力例 1

5
1 2 3 2 3

出力例 1

5 10 3 0 0
  • 長さ 1 の部分列は全部で 5 個あり、これらはすべて長さ 1 の等差数列です。
  • 長さ 2 の部分列は全部で 10 個あり、これらはすべて長さ 2 の等差数列です。
  • 長さ 3 の部分列であって等差数列であるものは、(A_1,A_2,A_3),(A_1,A_2,A_5),(A_1,A_4,A_5) の 3 つです。
  • 長さ 4 以上の部分列であって等差数列であるものは存在しません。

入力例 2

4
1 2 3 4

出力例 2

4 6 2 1

入力例 3

1
100

出力例 3

1

Score : 475 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N. For each k = 1, 2, \dots, N, find the number, modulo 998244353, of (not necessarily contiguous) subsequences of A of length k that are arithmetic sequences. Two subsequences are distinguished if they are taken from different positions, even if they are equal as sequences.

What is a subsequence? A subsequence of a sequence A is a sequence obtained by deleting zero or more elements from A and arranging the remaining elements without changing the order.

Constraints

  • 1 \leq N \leq 80
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answers for k = 1, 2, \dots, N in this order, in a single line, separated by spaces.


Sample Input 1

5
1 2 3 2 3

Sample Output 1

5 10 3 0 0
  • There are 5 subsequences of length 1, all of which are arithmetic sequences.
  • There are 10 subsequences of length 2, all of which are arithmetic sequences.
  • There are 3 subsequences of length 3 that are arithmetic sequences: (A_1, A_2, A_3), (A_1, A_2, A_5), and (A_1, A_4, A_5).
  • There are no arithmetic subsequences of length 4 or more.

Sample Input 2

4
1 2 3 4

Sample Output 2

4 6 2 1

Sample Input 3

1
100

Sample Output 3

1
I - Earn to Advance

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

縦 N 行横 N 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。

高橋君は最初マス (1,1) におり、所持金は 0 です。

高橋君はマス (i,j) にいるとき、1 回の行動で以下のいずれかを行うことができます。

  • 同じマスにとどまり、所持金を P_{i,j} 増やす。
  • 所持金から R_{i,j} 払ってマス (i,j+1) に移動する。
  • 所持金から D_{i,j} 払ってマス (i+1,j) に移動する。

所持金が負になる移動、グリッドの外に出る移動はできません。

高橋君が最適に行動したとき、何回の行動でマス (N,N) にたどり着くことができますか。

制約

  • 2 \leq N \leq 80
  • 1 \leq P_{i,j} \leq 10^9
  • 1 \leq R_{i,j},D_{i,j} \leq 10^9
  • 入力は全て整数である

入力

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

N
P_{1,1} \ldots P_{1,N}
\vdots
P_{N,1} \ldots P_{N,N}
R_{1,1} \ldots R_{1,N-1}
\vdots
R_{N,1} \ldots R_{N,N-1}
D_{1,1} \ldots D_{1,N}
\vdots
D_{N-1,1} \ldots D_{N-1,N}

出力

答えを出力せよ。


入力例 1

3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3

出力例 1

8

図

以下のようにして 8 回の行動でマス (3,3) にたどり着くことができます。

  • マス (1,1) にとどまり、所持金を 1 増やす。所持金は 1 になる。
  • 所持金から 1 払ってマス (2,1) に移動する。所持金は 0 になる。
  • マス (2,1) にとどまり、所持金を 3 増やす。所持金は 3 になる。
  • マス (2,1) にとどまり、所持金を 3 増やす。所持金は 6 になる。
  • マス (2,1) にとどまり、所持金を 3 増やす。所持金は 9 になる。
  • 所持金から 4 払ってマス (2,2) に移動する。所持金は 5 になる。
  • 所持金から 3 払ってマス (3,2) に移動する。所持金は 2 になる。
  • 所持金から 2 払ってマス (3,3) に移動する。所持金は 0 になる。

入力例 2

3
1 1 1
1 1 1
1 1 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000

出力例 2

4000000004

Score: 550 points

Problem Statement

There is a grid with N rows and N columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

Takahashi is initially at square (1,1) with zero money.

When Takahashi is at square (i,j), he can perform one of the following in one action:

  • Stay at the same square and increase his money by P_{i,j}.
  • Pay R_{i,j} from his money and move to square (i,j+1).
  • Pay D_{i,j} from his money and move to square (i+1,j).

He cannot make a move that would make his money negative or take him outside the grid.

If Takahashi acts optimally, how many actions does he need to reach square (N,N)?

Constraints

  • 2 \leq N \leq 80
  • 1 \leq P_{i,j} \leq 10^9
  • 1 \leq R_{i,j},D_{i,j} \leq 10^9
  • All input values are integers.

Input

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

N
P_{1,1} \ldots P_{1,N}
\vdots 
P_{N,1} \ldots P_{N,N}
R_{1,1} \ldots R_{1,N-1}
\vdots
R_{N,1} \ldots R_{N,N-1}
D_{1,1} \ldots D_{1,N}
\vdots
D_{N-1,1} \ldots D_{N-1,N}

Output

Print the answer.


Sample Input 1

3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3

Sample Output 1

8

Figure

It is possible to reach square (3,3) in eight actions as follows:

  • Stay at square (1,1) and increase money by 1. His money is now 1.
  • Pay 1 money and move to square (2,1). His money is now 0.
  • Stay at square (2,1) and increase money by 3. His money is now 3.
  • Stay at square (2,1) and increase money by 3. His money is now 6.
  • Stay at square (2,1) and increase money by 3. His money is now 9.
  • Pay 4 money and move to square (2,2). His money is now 5.
  • Pay 3 money and move to square (3,2). His money is now 2.
  • Pay 2 money and move to square (3,3). His money is now 0.

Sample Input 2

3
1 1 1
1 1 1
1 1 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000

Sample Output 2

4000000004