A - B +/- A

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

正整数 A, B が与えられます。

AB の約数なら A + B を、そうでなければ B - A を出力してください。

制約

  • 入力は全て整数である。
  • 1 \leq A \leq B \leq 20

入力

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

A B

出力

AB の約数なら A + B を、そうでなければ B - A を出力せよ。


入力例 1

4 12

出力例 1

16

412 の約数なので 4 + 12 = 16 を出力します。


入力例 2

8 20

出力例 2

12

入力例 3

1 1

出力例 3

2

11 の約数です。

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

  • All values in input are integers.
  • 1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.


Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.


Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.

B - Foods Loved by Everyone

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

カツサンドくんはオムライスが好きです。

他にも明太子や寿司、クリームブリュレやテンダーロインステーキなどが好きで、これらの食べ物は全て、誰もが好きだと信じています。

その仮説を証明するために、N 人の人に M 種類の食べ物について好きか嫌いかの調査を行いました。

調査の結果、i 番目の人は A_{i1} 番目, A_{i2} 番目, ..., A_{iK_i} 番目の食べ物だけ好きだと答えました。

N 人全ての人が好きだと答えた食べ物の種類数を求めてください。

制約

  • 入力は全て整数である。
  • 1 \leq N, M \leq 30
  • 1 \leq K_i \leq M
  • 1 \leq A_{ij} \leq M
  • i (1 \leq i \leq N) について A_{i1}, A_{i2}, ..., A_{iK_i} は全て異なる。

入力

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

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

出力

N 人全ての人が好きだと答えた食べ物の種類数を出力せよ。


入力例 1

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

出力例 1

1

3 人全員が好きだと答えた食べ物は 3 番目の食べ物だけなので 1 を出力します。


入力例 2

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

出力例 2

0

カツサンドくんの仮説は全く正しくありませんでした。


入力例 3

1 30
3 5 10 30

出力例 3

3

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

  • All values in input are integers.
  • 1 \leq N, M \leq 30
  • 1 \leq K_i \leq M
  • 1 \leq A_{ij} \leq M
  • For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.


Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.


Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.


Sample Input 3

1 30
3 5 10 30

Sample Output 3

3
C - Monsters Battle Royale

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 体のモンスターが居て、それぞれ 1, 2, ..., N と番号付けられています。

はじめ、モンスター i の体力は A_i です。

以降、体力が 1 以上のモンスターを生きているモンスターと呼びます。

生きているモンスターが 1 体になるまで以下を繰り返します。

  • ランダムに 1 体の生きているモンスターがランダムに別の生きているモンスターに攻撃します。
  • その結果、攻撃されたモンスターの体力を攻撃したモンスターの体力と同じ値だけ減らします。

最後に生き残ったモンスターの最終的な体力の最小値を求めてください。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9

入力

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

N
A_1 A_2 ... A_N

出力

最後に生き残ったモンスターの最終的な体力の最小値を出力せよ。


入力例 1

4
2 10 8 40

出力例 1

2

1 番目のモンスターだけが攻撃し続けた場合、最後に生き残ったモンスターの体力は 2 となり、このときが最小です。


入力例 2

4
5 13 8 1000000000

出力例 2

1

入力例 3

3
1000000000 1000000000 1000000000

出力例 3

1000000000

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

  • A random alive monster attacks another random alive monster.
  • As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.


Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.


Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000
D - Match Matching

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

ちょうど N 本のマッチ棒を使って作れる整数の中で最大のものを求めてください。

ただし、以下の条件を満たさなければなりません。

  • 作る整数の各桁は、1 から 9 までの数字のうち A_1, A_2, ..., A_M (1 \leq A_i \leq 9) のいずれかでなければならない。
  • 数字 1, 2, 3, 4, 5, 6, 7, 8, 91 つ作るには、それぞれちょうど 2, 5, 5, 4, 5, 6, 3, 7, 6 本のマッチ棒を使う。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 10^4
  • 1 \leq M \leq 9
  • 1 \leq A_i \leq 9
  • A_i は全て異なる。
  • ちょうど N 本のマッチ棒を使って条件を満たすように作れる整数が存在する。

入力

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

N M
A_1 A_2 ... A_M

出力

問題文の条件下でちょうど N 本のマッチ棒を使って作れる整数の最大値を出力せよ。


入力例 1

20 4
3 7 8 4

出力例 1

777773

整数 7777733 + 3 + 3 + 3 + 3 + 5 = 20 本のマッチ棒を使って作れ、ちょうど 20 本のマッチ棒を使って条件を満たすように作れる整数の中でこれが最大です。


入力例 2

101 9
9 8 7 6 5 4 3 2 1

出力例 2

71111111111111111111111111111111111111111111111111

出力が 64 ビット整数型に収まらない場合があります。


入力例 3

15 3
5 4 6

出力例 3

654

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

  • Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).
  • The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 10^4
  • 1 \leq M \leq 9
  • 1 \leq A_i \leq 9
  • A_i are all different.
  • There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.


Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.


Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.


Sample Input 3

15 3
5 4 6

Sample Output 3

654