A - Fairness

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君、中橋君、低橋君は、それぞれ整数 A,B,C を持っています。 以下の操作を K 回行った後、高橋君の持っている整数から中橋君の持っている整数を引いた値を求めてください。

  • 3 人は同時に、他の 2 人の持っている整数の和を求める。その後、自分の持っている整数を求めた整数で置き換える。

ただし、答えの絶対値が 10^{18} を超える場合は、代わりに Unfair と出力してください。

制約

  • 1 \leq A,B,C \leq 10^9
  • 0 \leq K \leq 10^{18}
  • 入力はすべて整数である

入力

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

A B C K

出力

操作を K 回行った後の、高橋君の持っている整数から中橋君の持っている整数を引いた値を出力せよ。 ただし、答えの絶対値が 10^{18} を超える場合は、代わりに Unfair と出力せよ。


入力例 1

1 2 3 1

出力例 1

1

1 回の操作後、高橋君、中橋君、低橋君の持っている整数はそれぞれ (5,4,3) となります。5-4=1 を出力します。


入力例 2

2 3 2 0

出力例 2

-1

入力例 3

1000000000 1000000000 1000000000 1000000000000000000

出力例 3

0

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively. After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

  • Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

  • 1 \leq A,B,C \leq 10^9
  • 0 \leq K \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times. If the absolute value of the answer exceeds 10^{18}, print Unfair instead.


Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.


Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0
B - Backfront

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 以上 N 以下の整数を並び替えてできる数列 (P_1,P_2,...,P_N) が与えられます。 次の操作を繰り返してこの列を昇順に並び替えるとき、操作の回数の最小値を求めてください。

  • 数列の要素を 1 つ選び、その要素を列の先頭または末尾のうち好きなほうに移動する

なお、この操作によって与えられた列を昇順に並び替えられることは証明できます。

制約

  • 1 \leq N \leq 2\times 10^5
  • (P_1,P_2,...,P_N)(1,2,...,N) の並び替えである
  • 入力はすべて整数である

入力

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

N
P_1
:
P_N

出力

操作の回数の最小値を出力せよ。


入力例 1

4
1
3
2
4

出力例 1

2

例えば、以下の操作によって列を昇順に並び替えることができます。

  • 2 を先頭に移動する。新しい数列は (2,1,3,4) となる。
  • 1 を先頭に移動する。新しい数列は (1,2,3,4) となる。

入力例 2

6
3
2
5
1
4
6

出力例 2

4

入力例 3

8
6
3
1
2
7
4
8
5

出力例 3

5

Score : 500 points

Problem Statement

You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:

  • Choose an element in the sequence and move it to the beginning or the end of the sequence.

Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • (P_1,P_2,...,P_N) is a permutation of (1,2,...,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1
:
P_N

Output

Print the minimum number of operations required.


Sample Input 1

4
1
3
2
4

Sample Output 1

2

For example, the sequence can be sorted in ascending order as follows:

  • Move 2 to the beginning. The sequence is now (2,1,3,4).
  • Move 1 to the beginning. The sequence is now (1,2,3,4).

Sample Input 2

6
3
2
5
1
4
6

Sample Output 2

4

Sample Input 3

8
6
3
1
2
7
4
8
5

Sample Output 3

5
C - Sequence Growing Easy

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

長さ N の数列 X があり、最初はすべての要素が 0 です。Xi 項目を X_i で表すことにします。

長さ N の数列 A が与えられます。Ai 項目は A_i です。 以下の操作を繰り返すことで XA と等しくすることができるかどうか判定し、できるなら最小の操作回数を求めてください。

  • 1\leq i\leq N-1 なる整数 i を選ぶ。X_{i+1} の値を X_i の値に 1 を足したもので置き換える。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9(1\leq i\leq N)
  • 入力はすべて整数である

入力

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

N
A_1
:
A_N

出力

操作を繰り返すことで XA と等しくすることができるなら最小の操作回数を、できないなら -1 を出力せよ。


入力例 1

4
0
1
1
2

出力例 1

3

次のようにして、XA と等しくすることができます。

  • i=2 に対して操作する。X(0,0,1,0) となる。
  • i=1 に対して操作する。X(0,1,1,0) となる。
  • i=3 に対して操作する。X(0,1,1,2) となる。

入力例 2

3
1
2
1

出力例 2

-1

入力例 3

9
0
1
1
0
1
2
2
1
2

出力例 3

8

Score : 700 points

Problem Statement

There is a sequence X of length N, where every element is initially 0. Let X_i denote the i-th element of X.

You are given a sequence A of length N. The i-th element of A is A_i. Determine if we can make X equal to A by repeating the operation below. If we can, find the minimum number of operations required.

  • Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9(1\leq i\leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

If we can make X equal to A by repeating the operation, print the minimum number of operations required. If we cannot, print -1.


Sample Input 1

4
0
1
1
2

Sample Output 1

3

We can make X equal to A as follows:

  • Choose i=2. X becomes (0,0,1,0).
  • Choose i=1. X becomes (0,1,1,0).
  • Choose i=3. X becomes (0,1,1,2).

Sample Input 2

3
1
2
1

Sample Output 2

-1

Sample Input 3

9
0
1
1
0
1
2
2
1
2

Sample Output 3

8
D - Isomorphism Freak

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

G の頂点を何色かで塗り分ける方法が 良い彩色 であるとは、同じ色で塗られたどの 2 つの頂点 u,v に対しても、 Gu を根とする根付き木として見た木と v を根とする根付き木として見た木が同型であることを指します。

また、Gカラフルさ とは、G の良い彩色で使われる色の種類数の最小値を指します。

N 頂点の木が与えられます。頂点には 1 から N までの番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。 この木に以下の操作を何度か繰り返し施し、木 T を作ります。

  • 新たな頂点を 1 つ作る。現在の木の頂点をひとつ選び、その頂点と新しく作った頂点を辺で結ぶ。

T としてありうるもののカラフルさの最小値を求めてください。 さらに、その最小値を達成する木 T の葉(次数 1 の頂点)の数の最小値を出力してください。

ノート

Gu を根とする根付き木として見た木と v を根とする根付き木として見た木が同型であるとは、 G の頂点集合からそれ自身への全単射な写像 f_{uv} であって、以下を満たすものが存在することを指します。

  • f_{uv}(u)=v
  • どの 2 頂点の組 (a,b) についても、(a,b) 辺があることと (f_{uv}(a),f_{uv}(b)) 辺があることが同値である

制約

  • 2 \leq N \leq 100
  • 1 \leq a_i,b_i \leq N(1\leq i\leq N-1)
  • 与えられるグラフは木である

入力

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

N
a_1 b_1
:
a_{N-1} b_{N-1}

出力

整数 2 つを空白区切りで出力せよ。 まずはじめに、作ることのできる木 T の中での、カラフルさの最小値を出力せよ。 その後、その時の木の葉の数の最小値を出力せよ。

なお、この問題の制約の下で、出力すべき値は 64 ビット符号付き整数に収まることが示せる。


入力例 1

5
1 2
2 3
3 4
3 5

出力例 1

2 4

頂点 6 を用意し、頂点 2 と結んだとき、頂点 (1,4,5,6) を赤で、頂点 (2,3) を青で塗る彩色は良い彩色です。 1 色で全頂点を塗る彩色は良い彩色ではないので、作った木のカラフルさは 2 となることが分かります。 この場合が最適であり、葉は 4 つあるので、24 を出力します。


入力例 2

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

出力例 2

3 4

入力例 3

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

出力例 3

4 6

入力例 4

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

出力例 4

4 12

Score : 1100 points

Problem Statement

Coloring of the vertices of a tree G is called a good coloring when, for every pair of two vertices u and v painted in the same color, picking u as the root and picking v as the root would result in isomorphic rooted trees.

Also, the colorfulness of G is defined as the minimum possible number of different colors used in a good coloring of G.

You are given a tree with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i. We will construct a new tree T by repeating the following operation on this tree some number of times:

  • Add a new vertex to the tree by connecting it to one of the vertices in the current tree with an edge.

Find the minimum possible colorfulness of T. Additionally, print the minimum number of leaves (vertices with degree 1) in a tree T that achieves the minimum colorfulness.

Notes

The phrase "picking u as the root and picking v as the root would result in isomorphic rooted trees" for a tree G means that there exists a bijective function f_{uv} from the vertex set of G to itself such that both of the following conditions are met:

  • f_{uv}(u)=v
  • For every pair of two vertices (a,b), edge (a,b) exists if and only if edge (f_{uv}(a),f_{uv}(b)) exists.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq a_i,b_i \leq N(1\leq i\leq N-1)
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print two integers with a space in between. First, print the minimum possible colorfulness of a tree T that can be constructed. Second, print the minimum number of leaves in a tree that achieves it.

It can be shown that, under the constraints of this problem, the values that should be printed fit into 64-bit signed integers.


Sample Input 1

5
1 2
2 3
3 4
3 5

Sample Output 1

2 4

If we connect a new vertex 6 to vertex 2, painting the vertices (1,4,5,6) red and painting the vertices (2,3) blue is a good coloring. Since painting all the vertices in a single color is not a good coloring, we can see that the colorfulness of this tree is 2. This is actually the optimal solution. There are four leaves, so we should print 2 and 4.


Sample Input 2

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

Sample Output 2

3 4

Sample Input 3

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

Sample Output 3

4 6

Sample Input 4

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

Sample Output 4

4 12
E - Sequence Growing Hard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

次の条件を満たす数列の組 (A_0,A_1,...,A_N) としてありうるものの個数を M で割ったあまりを求めてください。

  • 全ての i(0\leq i\leq N) に対し、A_i1 以上 K 以下の整数からなる長さ i の数列である
  • 全ての i(1\leq i\leq N) に対し、A_{i-1}A_i の部分列である。すなわち、ある 1\leq x_i\leq i が存在し、A_ix_i 文字目を取り除いてできる数列が A_{i-1} に一致する
  • 全ての i(1\leq i\leq N) に対し、A_i は辞書順で A_{i-1} より大きい

制約

  • 1 \leq N,K \leq 300
  • 2 \leq M \leq 10^9
  • N,K,M は整数である

入力

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

N K M

出力

数列の組 (A_0,A_1,...,A_N) としてありうるものの個数を M で割ったあまりを出力せよ。


入力例 1

2 2 100

出力例 1

5

以下の 5 つの組が条件を満たします。

  • (),(1),(1,1)
  • (),(1),(1,2)
  • (),(1),(2,1)
  • (),(2),(2,1)
  • (),(2),(2,2)

入力例 2

4 3 999999999

出力例 2

358

入力例 3

150 150 998244353

出力例 3

186248260

Score : 1200 points

Problem Statement

Find the number of the possible tuples of sequences (A_0,A_1,...,A_N) that satisfy all of the following conditions, modulo M:

  • For every i (0\leq i\leq N), A_i is a sequence of length i consisting of integers between 1 and K (inclusive);
  • For every i (1\leq i\leq N), A_{i-1} is a subsequence of A_i, that is, there exists 1\leq x_i\leq i such that the removal of the x_i-th element of A_i would result in a sequence equal to A_{i-1};
  • For every i (1\leq i\leq N), A_i is lexicographically larger than A_{i-1}.

Constraints

  • 1 \leq N,K \leq 300
  • 2 \leq M \leq 10^9
  • N, K and M are integers.

Input

Input is given from Standard Input in the following format:

N K M

Output

Print the number of the possible tuples of sequences (A_0,A_1,...,A_N), modulo M.


Sample Input 1

2 2 100

Sample Output 1

5

Five tuples below satisfy the conditions:

  • (),(1),(1,1)
  • (),(1),(1,2)
  • (),(1),(2,1)
  • (),(2),(2,1)
  • (),(2),(2,2)

Sample Input 2

4 3 999999999

Sample Output 2

358

Sample Input 3

150 150 998244353

Sample Output 3

186248260
F - Simple Subsequence Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 2300

問題文

0,1 からなる文字列からなる集合 S と整数 K が与えられます。 S に属する異なる K 個以上の文字列の部分列であるような最長の文字列を求めてください。 条件を満たすものが複数ある場合、辞書順で最小になるものを求めてください。

ただし、S は以下の形式で与えられます。

  • 整数 N と、N+1 個の文字列が与えられる。N+1 個の文字列は順に X_0,X_1,...,X_N であり、全ての i(0\leq i\leq N) に対し、X_i の長さは 2^i である。
  • どの整数の組 (i,j)(0\leq i\leq N,0\leq j\leq 2^i-1) に対しても、X_ij 文字目(ただし、最初の文字を 0 文字目、最後の文字を 2^i-1 文字目とする)が 1 であることと、j を(必要なら最初に 0 を補って) i 桁からなる二進表記で表してできる文字列が S に属することが同値である。
  • 長さ N+1 以上の文字列は、S には含まれない。

また、文字列 A が 文字列 B の部分列であるとは、ある整数列 t_1 < ... < t_{|A|} が存在して、全ての i(1\leq i\leq |A|) に対し Ai 文字目と Bt_i 文字目が等しいことを指します。

制約

  • 0 \leq N \leq 20
  • X_i(0\leq i\leq N) の長さは 2^i であり、01 のみからなる
  • 1 \leq K \leq |S|
  • K は整数である

入力

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

N K
X_0
:
X_N

出力

S に属する異なる K 個以上の文字列の部分列であるような最長の文字列のうち、辞書順最小のものを出力せよ。


入力例 1

3 4
1
01
1011
01001110

出力例 1

10

S に属する文字列は、(空文字列),1,00,10,11,001,100,101,110 です。 これらのうち 4 つ以上の部分列となる最長の文字列のうち辞書順最小のものは、10 です。


入力例 2

4 6
1
01
1011
10111010
1101110011111101

出力例 2

100

入力例 3

2 5
0
11
1111

出力例 3



空文字列が答えになります。

Score : 2300 points

Problem Statement

You are given a set S of strings consisting of 0 and 1, and an integer K.

Find the longest string that is a subsequence of K or more different strings in S. If there are multiple strings that satisfy this condition, find the lexicographically smallest such string.

Here, S is given in the format below:

  • The data directly given to you is an integer N, and N+1 strings X_0,X_1,...,X_N. For every i (0\leq i\leq N), the length of X_i is 2^i.
  • For every pair of two integers (i,j) (0\leq i\leq N,0\leq j\leq 2^i-1), the j-th character of X_i is 1 if and only if the binary representation of j with i digits (possibly with leading zeros) belongs to S. Here, the first and last characters in X_i are called the 0-th and (2^i-1)-th characters, respectively.
  • S does not contain a string with length N+1 or more.

Here, a string A is a subsequence of another string B when there exists a sequence of integers t_1 < ... < t_{|A|} such that, for every i (1\leq i\leq |A|), the i-th character of A and the t_i-th character of B is equal.

Constraints

  • 0 \leq N \leq 20
  • X_i(0\leq i\leq N) is a string of length 2^i consisting of 0 and 1.
  • 1 \leq K \leq |S|
  • K is an integer.

Input

Input is given from Standard Input in the following format:

N K
X_0
:
X_N

Output

Print the lexicographically smallest string among the longest strings that are subsequences of K or more different strings in S.


Sample Input 1

3 4
1
01
1011
01001110

Sample Output 1

10

The following strings belong to S: the empty string, 1, 00, 10, 11, 001, 100, 101 and 110. The lexicographically smallest string among the longest strings that are subsequences of four or more of them is 10.


Sample Input 2

4 6
1
01
1011
10111010
1101110011111101

Sample Output 2

100

Sample Input 3

2 5
0
11
1111

Sample Output 3



The answer is the empty string.