E - Calendar Validator

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

10^{100}7 列の行列 A があり、任意の整数対 (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7) についてその (i,j) 成分は (i-1) \times 7 + j です。

NM 列の行列 B が与えられるので、BA から一部の矩形領域を(向きを変えずに)切り出したものであるかを判定してください。

制約

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

出力

BA から一部の矩形領域を切り出したものであれば Yes と、そうでないなら No と出力せよ。


入力例 1

2 3
1 2 3
8 9 10

出力例 1

Yes

与えられる B は、A の左上 23 列を切り出したものとなっています。


入力例 2

2 1
1
2

出力例 2

No

与えられる B90 度回転させると A の左上 12 列と一致しますが、問題文中に「向きを変えずに」とある通り回転による一致は認められていないため、答えは No となります。


入力例 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

出力例 3

Yes

Score : 300 points

Problem Statement

There is a 10^{100} \times 7 matrix A, where the (i,j)-th entry is (i-1) \times 7 + j for every pair of integers (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7).

Given an N \times M matrix B, determine whether B is some (unrotated) rectangular part of A.

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

Output

If B is some rectangular part of A, print Yes; otherwise, print No.


Sample Input 1

2 3
1 2 3
8 9 10

Sample Output 1

Yes

The given matrix B is the top-left 2 \times 3 submatrix of A.


Sample Input 2

2 1
1
2

Sample Output 2

No

Although the given matrix B would match the top-left 1 \times 2 submatrix of A after rotating 90 degrees, the Problem Statement asks whether B is an unrotated part of A, so the answer is No.


Sample Input 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

Sample Output 3

Yes
F - Graph Isomorphism

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君と青木君は、それぞれ N 個のボールに M 本のひもを取り付けたおもちゃを持っています。

高橋君のおもちゃにおいて、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール A_i とボール B_i を結んでいます。

青木君のおもちゃにおいても同様に、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール C_i とボール D_i を結んでいます。

それぞれのおもちゃにおいて、同一のボールを結ぶようなひもは存在せず、2 つのボールを 2 本以上の異なるひもが結んでいることはありません。

すぬけ君は、2 人のおもちゃが同じ形であるかどうか気になっています。
ここで、2 人のおもちゃが同じ形であるとは、以下の条件を満たす数列 P が存在することをいいます。

  • P(1, \dots, N) を並べ替えて得られる。
  • 任意の 1 以上 N 以下の整数 i, j に対し、以下が成り立つ。
    • 高橋君のおもちゃにおいてボール i, j がひもで繋がれていることと、青木君のおもちゃにおいてボール P_i, P_j がひもで繋がれていることは同値である。

2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力してください。

制約

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
  • 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
  • (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
  • 入力は全て整数である。

入力

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

N M
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_M D_M

出力

2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力せよ。


入力例 1

4 4
1 2
1 3
1 4
3 4
1 3
1 4
2 3
3 4

出力例 1

Yes

高橋君のおもちゃは下図の左側のような形をしており、青木君のおもちゃは下図の右側のような形をしています。

yes1

次の図から、2 人のおもちゃが同じ形であることがわかります。例えば P = (3, 2, 1, 4) とすると問題文中の条件を満たします。

yes2


入力例 2

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

出力例 2

No

2 人のおもちゃは同じ形ではありません。

no


入力例 3

8 0

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi and Aoki each have a toy made by attaching M cords to N balls.

In Takahashi's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball A_i and B_i.

Similarly, in Aoki's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball C_i and D_i.

In each toy, no cord ties a ball to itself, and no two balls are tied by two or more different cords.

Snuke is wondering whether the two toys have the same shape.
Here, they are said to have the same shape when there is a sequence P that satisfies the conditions below.

  • P is a permutation of (1, \dots, N).
  • For every pair of integers i, j between 1 and N (inclusive), the following holds.
    • Balls i and j in Takahashi's toy are tied by a cord if and only if Balls P_i and P_j in Aoki's toy are tied by a cord.

If the two toys have the same shape, print Yes; otherwise, print No.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
  • 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
  • (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_M D_M

Output

If the two toys have the same shape, print Yes; otherwise, print No.


Sample Input 1

4 4
1 2
1 3
1 4
3 4
1 3
1 4
2 3
3 4

Sample Output 1

Yes

Takahashi's toy is illustrated on the left in the figure below, and Aoki's is illustrated on the right.

yes1

The following figure shows that the two toys have the same shape. The condition in the statement is satisfied when P = (3, 2, 1, 4), for example.

yes2


Sample Input 2

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

Sample Output 2

No

The two toys do not have the same shape.

no


Sample Input 3

8 0

Sample Output 3

Yes
G - Another Sigma Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

正整数 x,y に対して f(x,y) を以下で定義します。

  • 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。

例えば f(3,14)=314, f(100,1)=1001 です。

長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を 998244353 で割ったあまりを求めてください。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j)


制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力される数値は全て整数

入力

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

N 
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 14 15

出力例 1

2044
  • f(A_1,A_2)=314
  • f(A_1,A_3)=315
  • f(A_2,A_3)=1415

なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 2044 です。


入力例 2

5
1001 5 1000000 1000000000 100000

出力例 2

625549048

式の値を 998244353 で割ったあまりを求めることに注意してください。

Score: 400 points

Problem Statement

For positive integers x and y, define f(x, y) as follows:

  • Interpret the decimal representations of x and y as strings and concatenate them in this order to obtain a string z. The value of f(x, y) is the value of z when interpreted as a decimal integer.

For example, f(3, 14) = 314 and f(100, 1) = 1001.

You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression modulo 998244353:

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j).


Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 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 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 14 15

Sample Output 1

2044
  • f(A_1, A_2) = 314
  • f(A_1, A_3) = 315
  • f(A_2, A_3) = 1415

Thus, the answer is f(A_1, A_2) + f(A_1, A_3) + f(A_2, A_3) = 2044.


Sample Input 2

5
1001 5 1000000 1000000000 100000

Sample Output 2

625549048

Be sure to calculate the value modulo 998244353.

H - Apple Baskets on Circle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1,2,\ldots,N の番号がついた N 個のかごが円状に置かれています。
1\leq i \leq N-1 についてかご i の右隣にはかご i+1 があり、かご N の右隣にはかご 1 があります。

かご i の中には A_i 個りんごが入っています。

高橋君は最初かご 1 の前におり、以下の行動を繰り返します。

  • 目の前にあるかごの中にりんごがあれば 1 個かごから取り出して食べる。その後、りんごを食べたかどうかにかかわらず、右隣のかごの前に移動する。

高橋君がちょうど K 個のりんごを食べた時点で、各かごの中に残っているりんごの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • りんごは全部で K 個以上ある。すなわち、\sum_{i=1}^{N}A_i\geq K
  • 入力に含まれる値は全て整数である

入力

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

N K
A_1 A_2 \ldots A_N

出力

N 個の整数を空白区切りで出力せよ。
i 番目には、高橋君がちょうど K 個のりんごを食べた時点で、かご i の中に残っているりんごの個数を出力せよ。


入力例 1

3 3
1 3 0

出力例 1

0 1 0 

高橋君は次のように行動します。

  • 目の前にあるかご 1 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,3,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 3 の中にりんごはない。かご 1 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 1 の中にりんごはない。かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,1,0) である。

入力例 2

2 1000000000000
1000000000000 1000000000000

出力例 2

500000000000 500000000000 

Score : 500 points

Problem Statement

There are N baskets numbered 1, 2, \ldots, N arranged in a circle.
For each 1\leq i \leq N-1, basket i+1 is to the immediate right of basket i, and basket 1 is to the immediate right of basket N.

Basket i now contains A_i apples.

Takahashi starts in front of basket 1 and repeats the following action.

  • If the basket he is facing contains an apple, take one and eat it. Then, regardless of whether he has eaten an apple now, go on to the next basket to the immediate right.

Find the number of apples remaining in each basket when Takahashi has eaten exactly K apples in total.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • There are at least K apples in total. That is, \sum_{i=1}^{N}A_i\geq K.
  • All values in the input are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Print N integers, with spaces in between.
The i-th integer should be the number of apples remaining in basket i when Takahashi has eaten exactly K apples in total.


Sample Input 1

3 3
1 3 0

Sample Output 1

0 1 0 

Takahashi will do the following.

  • Basket 1, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 2. Now, the baskets have 0,3,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,2,0 apples.
  • Basket 3, which he is facing, contains no apple. Then, he goes on to basket 1. Now, the baskets have 0,2,0 apples.
  • Basket 1, which he is facing, contains no apple. Then, he goes on to basket 2. Now, the baskets have 0,2,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,1,0 apple(s).

Sample Input 2

2 1000000000000
1000000000000 1000000000000

Sample Output 2

500000000000 500000000000 
I - Skate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

HW 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

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

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

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

出力例 1

4

図は、(s_x,s_y)S で、(g_x,g_y)G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

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

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.