A - Reverse and Minimize

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

正の整数 x に対し、以下の問題の答えを f(x) とします。

x に次の操作を 0 回以上何度でも行えます。

  • x の十進表記を左右に反転して得られる整数を x' とする。そして、xx' に置き換える。これによって x の先頭に 1 個以上のゼロが並んだ場合、それらのゼロを削除して先頭がゼロでない状態にする。  

たとえば、 x=1420 に対して 1 回操作を行うと x=241 に、2 回操作を行うと x=142 に、3 回操作を行うと x=241 になります。
操作後の x の最小値を求めてください。

1 \leq x \leq N かつ f(x)=K を満たす整数 x の個数を求めてください。

制約

  • 1 \leq N,K \leq 10^{12}
  • 入力はすべて整数

入力

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

N K

出力

答えを出力せよ。


入力例 1

1420 142

出力例 1

3

x=142, 241, 14203 つが 1 \leq x \leq 1420 かつ f(x)=142 を満たします。


入力例 2

1419 142

出力例 2

2

入力例 3

6 19

出力例 3

0

Score : 300 points

Problem Statement

For a positive integer x, let f(x) be the answer to the question below.

The following operation on x can be performed zero or more times.

  • Let x' be the integer obtained by reversing the decimal notation of x. Then, replace x with x'. If x now has one or more leading zeros, delete them so that it begins with a non-zero digit.

For example, from x=1420, you get x=241 after one operation, x=142 after two operations, and x=241 after three operations.
Find the minimum possible value of x after operations.

Find the number of integers x such that 1 \leq x \leq N and f(x)=K.

Constraints

  • 1 \leq N,K \leq 10^{12}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.


Sample Input 1

1420 142

Sample Output 1

3

Three integers x=142, 241, and 1420 satisfy 1 \leq x \leq 1420 and f(x)=142.


Sample Input 2

1419 142

Sample Output 2

2

Sample Input 3

6 19

Sample Output 3

0
B - Unbalanced Squares

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N \times N のマス目があります。このマス目の上から i 行目、左から j 列目をマス (i,j) と呼びます。
全てのマスに整数を 1 つずつ書き込む方法であって、以下の条件を満たすようなものを 1 つ求めてください。

  • 1 以上 N^2 以下の整数がそれぞれちょうど 1 つずつ書き込まれる。
  • すべての整数 i,j\, (1 \leq i,j \leq N) に対し、マス (i,j) が次の条件を満たす。
    • マス (i,j) の上下左右斜めに隣接するマス(最大 8 個)のうち、書かれている整数がマス (i,j) に書かれている整数よりも大きいものの個数を a、小さいものの個数を b とする。この時、 a \neq b が成り立つ。

なお、この問題の制約の下、条件を満たす整数の書き込み方が必ず存在することが証明できます。

制約

  • 2 \leq N \leq 500
  • N は整数

入力

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

N

出力

条件をみたす整数の書き込み方を以下の形式で出力せよ。

x_{1,1} \ldots x_{1,N}
\vdots
x_{N,1} \ldots x_{N,N}

ただし、x_{i,j} はマス (i,j) に書き込まれる整数とする。
答えが複数存在する場合はどれを出力しても正解とみなされる。


入力例 1

2

出力例 1

1 2
3 4

この出力は 1 以上 N^2\, (=4) 以下の整数がそれぞれちょうど 1 つずつ書き込まれているため、1 つ目の条件を満たしています。
また、マス (1,1) の上下左右斜めに隣接するマスのうち書かれている整数がマス (1,1) に書かれているものより大きいものはマス (1,2)、マス (2,1)、マス (2,2)3 個で、小さいものは 0 個です。
このことからマス (1,1) に対しては a=3,b=0 となり、a\neq b が成り立ちます。
他のマスに対しても同様にして a\neq b が成り立つことが確かめられるため、この出力は 2 つ目の条件を満たしています。
以上より、この出力は正当です。


入力例 2

3

出力例 2

1 2 3
5 4 6
7 8 9

Score : 400 points

Problem Statement

We have an N \times N grid. Let (i,j) denote the square at the i-th row from the top and j-th column from the left in this grid.
Find one way to write an integer on every square to satisfy the conditions below.

  • Each integer between 1 and N^2 is written exactly once.
  • For every pair of integers i,j\, (1 \leq i,j \leq N), the square (i,j) satisfies the following.
    • Among the squares horizontally, vertically, or diagonally adjacent to (i,j) (there are at most eight of them), let a and b be the number of squares with integers larger than and smaller than that of (i,j), respectively. Then, a \neq b holds.

Under the Constraints of this problem, it can be proved that such a way to write integers always exists.

Constraints

  • 2 \leq N \leq 500
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print a way to write integers to satisfy the conditions, in the following format:

x_{1,1} \ldots x_{1,N}
\vdots
x_{N,1} \ldots x_{N,N}

Here, x_{i,j} is the integer to write on the square (i,j).
If multiple solutions exist, any of them will be accepted.


Sample Input 1

2

Sample Output 1

1 2
3 4

This output contains each integer between 1 and N^2\, (=4) exactly once, so the first condition is satisfied.
Additionally, among the squares horizontally, vertically, or diagonally adjacent to the square (1,1), three squares (1,2), (2,1), and (2,2) have integers larger than that of (1,1), and none has an integer smaller than that.
Thus, for (1,1), we have a=3 and b=0, so a\neq b holds.
Similarly, it can be verified that a\neq b also holds for the other squares, so the second condition is satisfied.
Therefore, this output is valid.


Sample Input 2

3

Sample Output 2

1 2 3
5 4 6
7 8 9
C - Tree Queries

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の木があり、各頂点には 1,\ldots,N と番号が付けられています。
また、各整数 u,v\, (1 \leq u,v \leq N) に対し、頂点 u,v の距離 d_{u,v} を次のように定めます。

  • 頂点 u と頂点 v を結ぶ最短パスに含まれる辺の本数

あなたは次のような質問を 0 回以上 2N 回以下行うことが出来ます。

  • 1\leq u,v \leq N かつ u+v>3 を満たす整数 u,v を指定し、頂点 u,v の距離 d_{u,v} を聞く。

頂点 1,2 の距離 d_{1,2} を求めてください。

制約

  • 3 \leq N \leq 100
  • N は整数
  • 木はプログラムとジャッジの対話の開始前に決定される

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)である。

まず、あなたのプログラムに標準入力から正の整数 N が与えられる。

N

その後、あなたは質問を行うことが出来る。
質問は標準出力に以下の形式で出力せよ(末尾に改行を入れること)。

? u v

質問が正当な場合、その質問への答え d_{u,v} が標準入力から与えられる。

d_{u,v}

質問の形式が間違っている・質問を規定の回数より多く行った等の理由から質問が不正と判定された場合、質問への答えの代わりに -1 が与えられる。

-1

この時、提出はすでに不正解と判定されている。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましい。

答え d_{1,2} が分かったら、標準出力に以下の形式で出力せよ(末尾に改行を入れること)。

! d_{1,2}

注意点

  • 出力を行うたびに標準出力をflushせよ。そうしなかった場合、ジャッジ結果が TLE となる可能性がある。
  • 答えを出力したら(または -1 を受け取ったら)直ちにプログラムを正常終了せよ。そうしなかった場合、ジャッジ結果は不定である。
  • 不正なフォーマットの出力を行った場合のジャッジ結果は不定である。
  • 特に、余計な改行も不正なフォーマットの出力とみなされるので注意せよ。

入出力例

木がこの画像のような時の対話の一例を示します。

入力 出力 説明
3 まず整数 N が与えられます。
? 1 3 1 回目の質問として頂点 1,3 の距離を聞きます。
1 頂点 1,3 の距離が与えられます。
? 2 2 2 回目の質問として頂点 2,2 の距離を聞きます。
0 頂点 2,2 の距離が与えられます。
? 2 3 3 回目の質問として頂点 2,3 の距離を聞きます。
1 頂点 2,3 の距離が与えられます。
? 3 1 4 回目の質問として頂点 3,1 の距離を聞きます。
1 頂点 3,1 の距離が与えられます。
? 3 2 5 回目の質問として頂点 3,2 の距離を聞きます。
1 頂点 3,2 の距離が与えられます。
? 2 2 6 回目の質問として頂点 2,2 の距離を聞きます。
0 頂点 2,2 の距離が与えられます。
! 2 頂点 1,2 の距離を回答し、終了します。実際に木の頂点 1,2 の距離は 2 であるため AC が得られます。

Score : 500 points

Problem Statement

There is a tree with N vertices, numbered 1, \ldots, N.
For each pair of integers u,v\, (1 \leq u,v \leq N), the distance d_{u,v} between Vertices u, v is defined as the following.

  • The number of edges contained in the shortest path connecting Vertices u and v.

You are allowed to ask between 0 and 2N questions (inclusive) in the following form.

  • Ask the distance d_{u,v} between Vertices u,v for integers u,v of your choice such that 1\leq u,v \leq N and u+v>3.

Find the distance d_{1,2} between Vertices 1,2.

Constraints

  • 3 \leq N \leq 100
  • N is an integer.
  • The tree is determined before the start of the interaction between your program and the judge.

Input and Output

This is an interactive task, in which your program and the judge interact via input and output.

First, your program is given a positive integer N from Standard Input:

N

Then, you get to ask questions.
A question should be printed in the following format (with a newline at the end):

? u v

If the question is valid, the response d_{u,v} is given from Standard Input:

d_{u,v}

If the question is judged invalid because, for example, it is malformed or you have asked too many questions, you get -1 instead of the response:

-1

At this point, your submission is already judged incorrect. The judge's program then terminates; yours should too, desirably.

When you find the answer d_{1,2}, print it to Standard Output in the following format (with a newline at the end):

! d_{1,2}

Notices

  • Flush Standard Output after each output. Otherwise, you might get the TLE verdict.
  • After printing the answer (or receiving -1), immediately terminate the program normally. Otherwise, the verdict would be indeterminate.
  • The verdict for the case of a malformed output would be indeterminate.
  • Specifically, note that too many newlines would also be seen as a malformed output.

Sample Input and Output

The following is a sample interaction for the tree shown in this image.

Input Output Description
3 Interaction starts with the integer N.
? 1 3 As the 1-st question, you ask the distance between Vertices 1 and 3.
1 The distance between Vertices 1,3 is returned.
? 2 2 As the 2-nd question, you ask the distance between Vertices 2 and 2.
0 The distance between Vertices 2,2 is returned.
? 2 3 As the 3-rd question, you ask the distance between Vertices 2 and 3.
1 The distance between Vertices 2,3 is returned.
? 3 1 As the 4-th question, you ask the distance between Vertices 3 and 1.
1 The distance between Vertices 3,1 is returned.
? 3 2 As the 5-th question, you ask the distance between Vertices 3 and 2.
1 The distance between Vertices 3,2 is returned.
? 2 2 As the 6-th question, you ask the distance between Vertices 2 and 2.
0 The distance between Vertices 2,2 is returned.
! 2 You guess the distance between Vertices 1,2 and terminate. The actual distance between them is 2, so you would get the AC verdict.
D - Deterministic Placing

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 頂点の木があり、各頂点には 1,\ldots,N と番号が付けられています。 i=1,\ldots,N-1 に対し、 i 番目の辺は頂点 a_i と頂点 b_i を結びます。
この木の各頂点に高々 1 個の駒が置かれた状態に対する操作を次のように定めます。

  • すべての駒を、同時に、隣接する頂点のうちの 1 つへ移動させる。

また、以下の条件を満たす操作を 良い操作 と呼びます。

  • すべての辺について、その辺を通して移動する駒は高々 1 個である。
  • 操作後に各頂点に置かれている駒は高々 1 個である。

高橋君は 1 個以上の頂点を選び、駒を 1 個ずつ置くことにしました。 駒を置く方法は 2^N-1 通りありますが、そのうち次の条件を満たすものの個数を 998244353 で割った余りを求めてください。

  • すべての非負整数 K について以下の条件を満たす。
    • 良い操作を K 回行うことができる。
    • 良い操作を K 回行った時点で駒が置かれている頂点の集合を S_K とする。この時、 S_K は一意に定まる。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

出力

答えを出力せよ。


入力例 1

3
1 2
1 3

出力例 1

2

次の 2 通りが条件を満たします。

  • 頂点 1 と頂点 2 を選び、駒を置く。
  • 頂点 1 と頂点 3 を選び、駒を置く。

頂点を 1 個以上選ぶ必要があることに気を付けてください。


入力例 2

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

出力例 2

0

条件を満たす駒の置き方が存在しない場合があります。


入力例 3

19
9 14
2 13
1 3
17 19
13 18
12 19
4 5
2 10
4 9
8 11
3 15
6 8
8 10
6 19
9 13
11 15
7 17
16 17

出力例 3

100

Score : 800 points

Problem Statement

We have a tree with N vertices, numbered 1, \ldots, N. For each i=1,\ldots,N-1, the i-th edge connects Vertex a_i and Vertex b_i.
An operation on this tree when at most one piece is put on each vertex is defined as follows.

  • Simultaneously move every piece to one of the vertices adjacent to the one occupied by the piece.

An operation is said to be good when the conditions below are satisfied.

  • Each edge is traversed by at most one piece.
  • Each vertex will be occupied by at most one piece after the operation.

Takahashi will put a piece on one or more vertices of his choice. Among the 2^N-1 ways to put pieces, find the number of ones that satisfy the condition below, modulo 998244353.

  • For every non-negative integer K, the following holds.
    • It is possible to perform a good operation K times.
    • Let S_K be the set of vertices occupied by pieces just after K good operations. Then, S_K is unique.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

Output

Print the answer.


Sample Input 1

3
1 2
1 3

Sample Output 1

2

Here are the two ways to satisfy the condition.

  • Put a piece on Vertex 1 and Vertex 2.
  • Put a piece on Vertex 1 and Vertex 3.

Note that he must put a piece on at least one vertex.


Sample Input 2

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

Sample Output 2

0

There might be no way to satisfy the condition.


Sample Input 3

19
9 14
2 13
1 3
17 19
13 18
12 19
4 5
2 10
4 9
8 11
3 15
6 8
8 10
6 19
9 13
11 15
7 17
16 17

Sample Output 3

100
E - Pairing Wizards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

N 人の魔法使いがいて、1, \ldots ,N と番号付けられています。
魔法使い i の強さは a_i です。また、魔法使い i は強さが b_i のモンスターを倒そうとしています。

あなたは次の操作を何度でも行えます。

  • 好きな魔法使いを 1 人選び、その強さを 1 増やす。

また、魔法使い i と魔法使い j のペア (以降ペア (i,j) と呼ぶ) が良いペアであるとは、以下の条件のうち少なくとも一方を満たすことを指します。

  • 魔法使い i の強さが b_i 以上かつ魔法使い j の強さが b_j 以上
  • 魔法使い i の強さが b_j 以上かつ魔法使い j の強さが b_i 以上

あなたの目的は i=1,\ldots, M すべてに対し、ペア (x_i,y_i) が良いペアであるようにすることです。
目的を達成するために必要な操作の回数の最小値を求めてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq a_i,b_i \leq 100
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq x_i \lt y_i \leq N
  • i\neq j ならば (x_i,y_i) \neq (x_j,y_j)
  • 入力はすべて整数

入力

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

N
a_1 b_1
\vdots
a_N b_N
M
x_1 y_1
\vdots
x_M y_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

2

魔法使い 1 と魔法使い 41 回ずつ操作を行えば最小の操作回数で目的を達成できます。


入力例 2

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

出力例 2

0

1 回も操作を行う必要がありません。


入力例 3

9
1 1
2 4
5 5
7 10
9 3
9 13
10 9
3 9
2 9
7
1 5
2 5
1 6
2 4
3 4
4 9
8 9

出力例 3

22

Score : 900 points

Problem Statement

There are N wizards, numbered 1, \ldots, N.
Wizard i has a strength of a_i and plans to defeat a monster with a strength of b_i.

You can perform the following operation any number of times.

  • Increase the strength of any wizard of your choice by 1.

A pair of Wizard i and Wizard j (let us call this pair (i, j)) is said to be good when at least one of the following conditions is satisfied.

  • Wizard i has a strength of at least b_i and Wizard j has a strength of at least b_j.
  • Wizard i has a strength of at least b_j and Wizard j has a strength of at least b_i.

Your objective is to make the pair (x_i, y_i) good for every i=1, \ldots, M.
Find the minimum number of operations needed to achieve it.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq a_i,b_i \leq 100
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq x_i \lt y_i \leq N
  • (x_i,y_i) \neq (x_j,y_j), if i\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_N b_N
M
x_1 y_1
\vdots
x_M y_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

2

You can perform the operation once for Wizard 1 and once for Wizard 4 to achieve the objective with the fewest operations.


Sample Input 2

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

Sample Output 2

0

No operation is needed.


Sample Input 3

9
1 1
2 4
5 5
7 10
9 3
9 13
10 9
3 9
2 9
7
1 5
2 5
1 6
2 4
3 4
4 9
8 9

Sample Output 3

22
F - Paired Wizards

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

2 人の魔法使い X,Y がモンスターと戦っています。

初め、2 人の魔力はそれぞれ 0 です。また、以下の 2 種類の魔法を習得しています。

  • 魔法 1 : 使用者の魔力が 1 増える。
  • 魔法 2 : 使用者の魔力と同じだけモンスターにダメージを与える。

2 人はそれぞれ N 回ずつ魔法を使用した後に撤退します。
i=1,\ldots,N に対し、2 人が i 番目に使用する魔法の組み合わせは以下のどちらかです。

  • X が魔法 a_i を、Y が魔法 b_i を使用する。
  • X が魔法 c_i を、Y が魔法 d_i を使用する。

2 人が撤退するまでにモンスターに与えられるダメージの総和の最大値を求めてください。

制約

  • 1 \leq N \leq 8000
  • a_i,b_i,c_i,d_i \in \{1,2\}
  • 入力はすべて整数

入力

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

N
a_1 b_1 c_1 d_1
\vdots
a_N b_N c_N d_N

出力

答えを出力せよ。


入力例 1

3
1 1 2 2
2 1 2 2
2 1 1 1

出力例 1

3

次のようにすると最大値を達成出来ます。

  • 1 回目の魔法は a_1=1,\, b_1=1 を使用する。 X,Y の魔力がともに 1 になる。
  • 2 回目の魔法は c_2=2,\, d_2=2 を使用する。 合計でモンスターに 2 のダメージを与える。
  • 3 回目の魔法は a_3=2,\, b_3=1 を使用する。 X の魔法でモンスターに 1 のダメージを与え、Y は魔力が 2 になる。

入力例 2

5
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2

出力例 2

0

魔力が 0 の状態で魔法 2 を使ってもダメージを与えられません。


入力例 3

8
1 1 2 2
2 2 2 1
1 1 2 1
1 1 2 2
2 1 1 1
1 2 1 2
2 1 1 2
2 1 2 1

出力例 3

20

入力例 4

20
2 1 2 1
2 1 1 1
1 2 1 1
2 2 1 2
2 2 2 1
1 1 2 1
1 2 2 2
2 2 2 1
1 1 1 2
1 2 1 2
1 2 2 2
2 1 1 2
2 1 1 1
1 2 1 2
1 2 1 2
1 1 1 2
1 1 2 1
2 2 1 1
1 2 2 2
2 1 1 2

出力例 4

138

Score : 1000 points

Problem Statement

Two wizards, X and Y, are fighting with a monster.

Initially, each of the wizards has a magic power of 0. They know the following two spells.

  • Spell 1: Increases the caster's magic power by 1.
  • Spell 2: Deals damage equal to the caster's magic power to the monster.

After each wizard uses a spell N times, they will withdraw from the battle.
For each i=1, \ldots, N, they must use one of the following combinations of spells for their i-th spells.

  • X casts Spell a_i, and Y casts Spell b_i.
  • X casts Spell c_i, and Y casts Spell d_i.

Find the maximum total damage that can be dealt to the monster before the withdrawal.

Constraints

  • 1 \leq N \leq 8000
  • a_i,b_i,c_i,d_i \in \{1,2\}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1 d_1
\vdots
a_N b_N c_N d_N

Output

Print the answer.


Sample Input 1

3
1 1 2 2
2 1 2 2
2 1 1 1

Sample Output 1

3

The maximum total damage can be achieved as follows.

  • As the 1-st spells, use a_1=1,\, b_1=1, increasing both X's and Y's magic powers to 1.
  • As the 2-nd spells, use c_2=2,\, d_2=2, dealing 2 points of damage in total.
  • As the 3-rd spells, use a_3=2,\, b_3=1, dealing 1 point of damage by X's spell and increasing Y's magic power to 2.

Sample Input 2

5
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2

Sample Output 2

0

Casting Spell 2 with a magic power of 0 deals no damage.


Sample Input 3

8
1 1 2 2
2 2 2 1
1 1 2 1
1 1 2 2
2 1 1 1
1 2 1 2
2 1 1 2
2 1 2 1

Sample Output 3

20

Sample Input 4

20
2 1 2 1
2 1 1 1
1 2 1 1
2 2 1 2
2 2 2 1
1 1 2 1
1 2 2 2
2 2 2 1
1 1 1 2
1 2 1 2
1 2 2 2
2 1 1 2
2 1 1 1
1 2 1 2
1 2 1 2
1 1 1 2
1 1 2 1
2 2 1 1
1 2 2 2
2 1 1 2

Sample Output 4

138