C - Practical Computing

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。

  • i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
  • i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_ij+1 番目の値 a_{i,j} は次のように定められる。

    • j=0 または j=i の時、a_{i,j}=1
    • それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}

制約

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

入力

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

N

出力

N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。


入力例 1

3

出力例 1

1
1 1
1 2 1

入力例 2

10

出力例 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Score : 200 points

Problem Statement

Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.

  • For each i (0\leq i \leq N-1), the length of A_i is i+1.
  • For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
    • a_{i,j}=1, if j=0 or j=i.
    • a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.


Sample Input 1

3

Sample Output 1

1
1 1
1 2 1

Sample Input 2

10

Sample Output 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
D - Adjacency Matrix

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150

問題文

N 頂点の単純無向グラフ G があり、グラフの頂点には 1,2,\ldots, N の番号が付けられています。

G の隣接行列 (A_{i,j}) が与えられます。すなわち、GA_{i,j} = 1 であるとき、またそのときに限り頂点 i と頂点 j を結ぶ辺を持ちます。

i = 1, 2, \ldots, N について、頂点 i と直接結ばれている頂点の番号を昇順に出力してください。

ただし、頂点 i と頂点 j が直接結ばれているとは、頂点 i と頂点 j を結ぶ辺が存在することをいいます。

制約

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • 入力される値はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

出力

N 行出力せよ。 i 行目には頂点 i と直接結ばれている頂点の番号を昇順に空白区切りで出力せよ。


入力例 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

出力例 1

2 3
1 4
1
2

頂点 1 と直接結ばれている頂点は頂点 2, 3 です。したがって、1 行目には 2, 3 をこの順で出力します。

同様に、2 行目には 1, 4 をこの順に、3 行目には 1 を、4 行目には 2 を出力します。


入力例 2

2
0 0
0 0

出力例 2



G に辺が存在しないこともあります。


入力例 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

出力例 3

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

Score: 150 points

Problem Statement

There is a simple undirected graph G with N vertices labeled with numbers 1, 2, \ldots, N.

You are given the adjacency matrix (A_{i,j}) of G. That is, G has an edge connecting vertices i and j if and only if A_{i,j} = 1.

For each i = 1, 2, \ldots, N, print the numbers of the vertices directly connected to vertex i in ascending order.

Here, vertices i and j are said to be directly connected if and only if there is an edge connecting vertices i and j.

Constraints

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • All input values are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

Output

Print N lines. The i-th line should contain the numbers of the vertices directly connected to vertex i in ascending order, separated by a space.


Sample Input 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

Sample Output 1

2 3
1 4
1
2

Vertex 1 is directly connected to vertices 2 and 3. Thus, the first line should contain 2 and 3 in this order.

Similarly, the second line should contain 1 and 4 in this order, the third line should contain 1, and the fourth line should contain 2.


Sample Input 2

2
0 0
0 0

Sample Output 2



G may have no edges.


Sample Input 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

Sample Output 3

2 4 5
1 4
5
1 2 5
1 3 4
E - Find it!

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。

注釈

この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。

  • M \ge 2
  • B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
  • B_M から B_1 に辺が伸びている。
  • i \neq j ならば B_i \neq B_j

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

入力

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

N
A_1 A_2 \dots A_N

出力

以下の形式で出力せよ。

M
B_1 B_2 \dots B_M

M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

答えとして考えられるものが複数ある場合、どれを出力しても正解となる。


入力例 1

7
6 7 2 1 3 4 5

出力例 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。

4
2 7 5 3
3
4 1 6

グラフが連結であるとは限らないことに注意してください。


入力例 2

2
2 1

出力例 2

2
1 2

1 \rightarrow 22 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 21 \rightarrow 22 \rightarrow 1 の辺が双方あることを表現しています。


入力例 3

8
3 7 4 7 3 3 8 2

出力例 3

3
2 7 8

この入力に対応するグラフは以下の通りです。

Score : 350 points

Problem Statement

There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:

  • M \geq 2
  • The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
  • The edge from vertex B_M to vertex B_1 exists.
  • If i \neq j, then B_i \neq B_j.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

Input

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

N
A_1 A_2 \dots A_N

Output

Print a solution in the following format:

M
B_1 B_2 \dots B_M

M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

If multiple solutions exist, any of them will be accepted.


Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.

Here is the graph corresponding to this input:

Here are other acceptable outputs:

4
2 7 5 3
3
4 1 6

Note that the graph may not be connected.


Sample Input 2

2
2 1

Sample Output 2

2
1 2

This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.

Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:


Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8

Here is the graph corresponding to this input:

F - Just K

実行時間制限: 2 sec / メモリ制限: 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 - Masked Popcount

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

整数 N,M が与えられるので、 \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M)998244353 で割った余りを求めてください。

ただし、 \mathbin{\&} はビット単位 \rm{AND} 演算を表します。

ビット単位 \rm{AND} 演算とは? 非負整数 a と非負整数 b とのビット単位 \rm{AND} 演算の結果 x = a \mathbin{\&} b は次のように定義されます。
  • x は全ての非負整数 k について以下の条件を満たすただ 1 つの非負整数である。
    • a2 進法で書き下した際の 2^k の位と b2 進法で書き下した際の 2^k の位が共に 1 なら、 x2 進法で書き下した際の 2^k の位は 1 である。
    • そうでないとき、 x2 進法で書き下した際の 2^k の位は 0 である。
例えば 3=11_{(2)}, 5=101_{(2)} なので、 3 \mathbin{\&} 5 = 1 となります。
\rm{popcount} とは? \rm{popcount}(x) は、 x2 進法で書き下した際に登場する 1 の個数を表します。
例えば 13=1101_{(2)} なので、 \rm{popcount}(13) = 3 となります。

制約

  • N0 以上 2^{60} 未満の整数
  • M0 以上 2^{60} 未満の整数

入力

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

N M

出力

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


入力例 1

4 3

出力例 1

4
  • \rm{popcount}(0\mathbin{\&}3) = 0
  • \rm{popcount}(1\mathbin{\&}3) = 1
  • \rm{popcount}(2\mathbin{\&}3) = 1
  • \rm{popcount}(3\mathbin{\&}3) = 2
  • \rm{popcount}(4\mathbin{\&}3) = 0

であり、これらの和は 4 です。


入力例 2

0 0

出力例 2

0

N=0 である場合や M=0 である場合もあります。


入力例 3

1152921504606846975 1152921504606846975

出力例 3

499791890

998244353 で割った余りを求めることに注意してください。

Score : 400 points

Problem Statement

Given integers N and M, compute the sum \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M), modulo 998244353.

Here, \mathbin{\&} represents the bitwise \rm{AND} operation.

What is the bitwise \rm{AND} operation? The result x = a \mathbin{\&} b of the bitwise \rm{AND} operation between non-negative integers a and b is defined as follows:
  • x is the unique non-negative integer that satisfies the following conditions for all non-negative integers k:
    • If the 2^k place in the binary representation of a and the 2^k place in the binary representation of b are both 1, then the 2^k place in the binary representation of x is 1.
    • Otherwise, the 2^k place in the binary representation of x is 0.
For example, 3=11_{(2)} and 5=101_{(2)}, so 3 \mathbin{\&} 5 = 1.
What is \rm{popcount}? \rm{popcount}(x) represents the number of 1s in the binary representation of x.
For example, 13=1101_{(2)}, so \rm{popcount}(13) = 3.

Constraints

  • N is an integer between 0 and 2^{60} - 1, inclusive.
  • M is an integer between 0 and 2^{60} - 1, inclusive.

Input

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

N M

Output

Print the answer as an integer.


Sample Input 1

4 3

Sample Output 1

4
  • \rm{popcount}(0\mathbin{\&}3) = 0
  • \rm{popcount}(1\mathbin{\&}3) = 1
  • \rm{popcount}(2\mathbin{\&}3) = 1
  • \rm{popcount}(3\mathbin{\&}3) = 2
  • \rm{popcount}(4\mathbin{\&}3) = 0

The sum of these values is 4.


Sample Input 2

0 0

Sample Output 2

0

It is possible that N = 0 or M = 0.


Sample Input 3

1152921504606846975 1152921504606846975

Sample Output 3

499791890

Remember to compute the result modulo 998244353.