A - Rock-paper-scissors

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

サーバル、フェネック、アライグマの 3 人がじゃんけんをして、あいこになりました。

フェネックが出した手を表す文字 x とアライグマが出した手を表す文字 y が与えられます。それぞれ、0 はグー、1 はチョキを、2 はパーを表します。

サーバルが出した手を表す文字を出力してください。なお、答えは一意に定まります。

制約

  • x, y0, 1, 2 のいずれか

入力

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

x y

出力

答えを出力せよ。出力についても、0 はグー、1 はチョキを、2 はパーを表すものとする。


入力例 1

0 1

出力例 1

2

フェネックが出した手はグー、アライグマが出した手はチョキでした。あいこになるのはサーバルがパーを出したときです。


入力例 2

0 0

出力例 2

0

フェネックが出した手はグー、アライグマが出した手はグーでした。あいこになるのはサーバルがグーを出したときです。

Score : 100 points

Problem Statement

Serval, Fennec, and Raccoon played rock-paper-scissors and had a draw.

You are given characters x and y representing the hand thrown by Fennec and Raccoon, respectively. Here, 0 stands for rock, 1 stands for scissors, and 2 stands for paper.

Print the character corresponding to the hand thrown by Serval, which can be uniquely determined.

Constraints

  • Each of x and y is 0, 1, or 2.

Input

Input is given from Standard Input in the following format:

x y

Output

Print the answer, which should be 0 for rock, 1 for scissors, or 2 for paper.


Sample Input 1

0 1

Sample Output 1

2

Fennec threw rock, and Raccoon threw scissors. To have a draw, Serval must have thrown paper.


Sample Input 2

0 0

Sample Output 2

0

Fennec threw rock, and Raccoon threw rock. To have a draw, Serval must have thrown rock.

B - Nuts

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 本の木があり、 i 番目の木には A_i 個の木の実が実っています。

シマリスは、次のルールで全ての木から木の実を収穫します。

  • 実っている木の実が 10 個以下の木からは木の実を収穫しない
  • 実っている木の実が 10 個より多い木からは、10 個を残して残りの全てを収穫する

シマリスが収穫する木の実の個数の合計を求めてください。

制約

  • 1 \leq N \leq 1000
  • 0 \leq A_i \leq 1000
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
6 17 28

出力例 1

25

3 本の木からそれぞれ 0,7,18 個の木の実を収穫します。よって合計は 25 個です。


入力例 2

4
8 9 10 11

出力例 2

1

Score : 200 points

Problem Statement

There are N trees. The i-th tree bears A_i nuts.

Chipmunk will harvest nuts from the trees in the following manner:

  • From a tree with 10 or fewer nuts, she does not take nuts.
  • From a tree with more than 10 nuts, she takes all but 10 nuts.

Find the total number of nuts Chipmunk will take from the trees.

Constraints

  • 1 \leq N \leq 1000
  • 0 \leq A_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
6 17 28

Sample Output 1

25

From the three trees, Chipmunk will take 0, 7, and 18 nuts, for a total of 25 nuts.


Sample Input 2

4
8 9 10 11

Sample Output 2

1
C - Tour

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

AtCoder 国には 1 から N の番号がついた N 個の都市と、1 から M の番号がついた M 個の道路があります。

道路 i を通ると都市 A_i から B_i へ移動することができます。都市 B_i から都市 A_i への通行はできません。

ピューマは、どこかの都市からスタートし、0 本以上の道路を使い移動して、どこかの都市をゴールとするような旅行の計画を立てようとしています。

スタート地点とゴール地点の都市の組として考えられるものは何通りありますか?

制約

  • 2 \leq N \leq 2000
  • 0 \leq M \leq \min(2000,N(N-1))
  • 1 \leq A_i,B_i \leq N
  • A_i \neq B_i
  • (A_i,B_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

答えを出力せよ。


入力例 1

3 3
1 2
2 3
3 2

出力例 1

7

スタート地点とゴール地点の組として考えられるものは (1,1),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3)7 通りです。


入力例 2

3 0

出力例 2

3

スタート地点とゴール地点の組として考えられるものは (1,1),(2,2),(3,3)3 通りです。


入力例 3

4 4
1 2
2 3
3 4
4 1

出力例 3

16

スタート地点とゴール地点の組として全ての都市の組み合わせが可能です。

Score : 300 points

Problem Statement

The republic of AtCoder has N cities numbered 1 through N and M roads numbered 1 through M.

Road i leads from City A_i to City B_i, but you cannot use it to get from City B_i to City A_i.

Puma is planning her journey where she starts at some city, travels along zero or more roads, and finishes at some city.

How many pairs of cities can be the origin and destination of Puma's journey? We distinguish pairs with the same set of cities in different orders.

Constraints

  • 2 \leq N \leq 2000
  • 0 \leq M \leq \min(2000,N(N-1))
  • 1 \leq A_i,B_i \leq N
  • A_i \neq B_i
  • (A_i,B_i) are distinct.
  • 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

Output

Print the answer.


Sample Input 1

3 3
1 2
2 3
3 2

Sample Output 1

7

We have seven pairs of cities that can be the origin and destination: (1,1),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3).


Sample Input 2

3 0

Sample Output 2

3

We have three pairs of cities that can be the origin and destination: (1,1),(2,2),(3,3).


Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

16

Every pair of cities can be the origin and destination.

D - Cooking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は料理 1 から NN 品の料理を作ろうとしています。

料理 i はオーブンを連続した T_i 分間使うことで作れます。1 つのオーブンを 2 つ以上の料理のために同時に使うことはできません。

2 つのオーブンを使えるとき、N 品の料理を全て作るまでに最短で何分かかりますか? なお、オーブンを使う時間以外は無視できるものとします。

制約

  • 1 \leq N \leq 100
  • 1 \leq T_i \leq 10^3
  • 入力に含まれる値は全て整数である

入力

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

N
T_1 \ldots T_N

出力

答えを出力せよ。


入力例 1

5
8 3 7 2 5

出力例 1

13

例えば 2 つのオーブンを次のように使うことで、13 分で全ての料理を作ることができます。

  • 1 つ目のオーブン:料理 5,1 を順に作る。
  • 2 つ目のオーブン:料理 2,4,3 を順に作る。

入力例 2

2
1000 1

出力例 2

1000

入力例 3

9
3 14 15 9 26 5 35 89 79

出力例 3

138

Score : 400 points

Problem Statement

Takahashi is going to cook N dishes called Dish 1 through N.

Dish i can be cooked by using an oven for T_i consecutive minutes. An oven cannot be used for two or more dishes simultaneously.

If Takahashi has two ovens to use, what is the shortest number of minutes needed to cook all the N dishes? Assume that all processes other than using ovens take negligible time.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq T_i \leq 10^3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 \ldots T_N

Output

Print the answer.


Sample Input 1

5
8 3 7 2 5

Sample Output 1

13

We can, for example, use the two ovens as follows to cook all the dishes in 13 minutes.

  • The first oven: Cook Dishes 5 and 1 in this order.
  • The second oven: Cook Dishes 2, 4, and 3 in this order.

Sample Input 2

2
1000 1

Sample Output 2

1000

Sample Input 3

9
3 14 15 9 26 5 35 89 79

Sample Output 3

138
E - Rush Hour 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder国には N 個の都市と M 本の道路があります。

都市には 1 から N の番号が、道路には 1 から M の番号が振られています。道路 i は都市 A_i と都市 B_i を双方向に結びます。

AtCoder国には時刻 0 をピークとするラッシュアワーがあり、時刻 t に道路 i の通行を始めると、移動するのに C_i+ \left\lfloor \frac{D_i}{t+1} \right\rfloor の時間がかかります。 ( \lfloor x\rfloorx を超えない最大の整数を表す)

高橋君は時刻 0 またはそれ以降の 整数時刻に 都市 1 を出発して、道路を通行することで都市 N へ向かおうとしています。

高橋君が各都市で 整数時間 待機することができるとき、高橋君が都市 N に到達することができる最も早い時刻を出力してください。なお、制約の下で答えは整数になることが証明できます。

ただし、都市 N に到達できないときはかわりに -1 を出力してください。

制約

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • 0 \leq C_i,D_i \leq 10^9
  • 入力は全て整数

入力

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

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

出力

高橋君が都市 N に到達することができる最も早い時刻を整数で出力せよ。ただし、都市 N に到達できないときはかわりに -1 を出力せよ。


入力例 1

2 1
1 2 2 3

出力例 1

4

最初に都市 1 で時刻 1 まで待機します。そして時刻 1 に道路 1 を使って移動をすると、移動に 2+\left\lfloor \frac{3}{1+1} \right\rfloor = 3 の時間がかかり、都市 2 には時刻 4 に到着することができます。

時刻 4 より早く都市 2 に到着することはできません。


入力例 2

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

出力例 2

3

同じ都市の組を結ぶ道路が複数ある場合や、同じ都市に戻ってくる道路がある場合もあります。


入力例 3

4 2
1 2 3 4
3 4 5 6

出力例 3

-1

都市 1 から都市 N に至る経路がないこともあります。


入力例 4

6 9
1 1 0 0
1 3 1 2
1 5 2 3
5 2 16 5
2 6 1 10
3 4 3 4
3 5 3 10
5 6 1 100
4 2 0 110

出力例 4

20

Score : 500 points

Problem Statement

The Republic of AtCoder has N cities and M roads.

The cities are numbered 1 through N, and the roads are numbered 1 through M. Road i connects City A_i and City B_i bidirectionally.

There is a rush hour in the country that peaks at time 0. If you start going through Road i at time t, it will take C_i+ \left\lfloor \frac{D_i}{t+1} \right\rfloor time units to reach the other end. (\lfloor x\rfloor denotes the largest integer not exceeding x.)

Takahashi is planning to depart City 1 at time 0 or some integer time later and head to City N.

Print the earliest time when Takahashi can reach City N if he can stay in each city for an integer number of time units. It can be proved that the answer is an integer under the Constraints of this problem.

If City N is unreachable, print -1 instead.

Constraints

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • 0 \leq C_i,D_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print an integer representing the earliest time when Takahashi can reach City N, or -1 if City N is unreachable.


Sample Input 1

2 1
1 2 2 3

Sample Output 1

4

We will first stay in City 1 until time 1. Then, at time 1, we will start going through Road 1, which will take 2+\left\lfloor \frac{3}{1+1} \right\rfloor = 3 time units before reaching City 2 at time 4.

It is impossible to reach City 2 earlier than time 4.


Sample Input 2

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

Sample Output 2

3

There may be multiple roads connecting the same pair of cities, and a road going from a city to the same city.


Sample Input 3

4 2
1 2 3 4
3 4 5 6

Sample Output 3

-1

There may be no path from City 1 to City N.


Sample Input 4

6 9
1 1 0 0
1 3 1 2
1 5 2 3
5 2 16 5
2 6 1 10
3 4 3 4
3 5 3 10
5 6 1 100
4 2 0 110

Sample Output 4

20
F - Hanjo 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

H メートル、横 W メートルの長方形の部屋があります。
この部屋に 2 メートル × 1 メートルの区別できない畳 (長方形のタイル) と、1 メートル × 1 メートルの区別できない半畳 (正方形のタイル) を敷き詰めます。 2 メートル × 1 メートルの畳は縦長にも横長にも使うことができます。
敷き詰める方法は何通りあるでしょうか?
回転や反転を行うことで初めて一致するような敷き詰め方は区別します。

答えは非常に大きくなる可能性があるので 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq H \leq 6
  • 1 \leq W \leq 10^{12}

入力

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

H W

出力

答えを出力せよ。


入力例 1

2 2

出力例 1

7

以下の 7 つです。


入力例 2

3 3

出力例 2

131

入力例 3

5 100

出力例 3

379944232

998244353 で割ったあまりを求めてください。

Score : 600 points

Problem Statement

We have a rectangular room that is H meters long and W meters wide.
We will fill this entire room with tatami (rectangular mats) that are 2 meters long and 1 meter wide, and hanjo (square mats) that are 1 meter long and 1 meter wide. Each tatami can be placed vertically or horizontally.
How many ways are there to fill the room?
We distinguish ways that match only after rotation or reflection.

Since the count can be enormous, find it modulo 998244353.

Constraints

  • 1 \leq H \leq 6
  • 1 \leq W \leq 10^{12}

Input

Input is given from Standard Input in the following format:

H W

Output

Print the answer.


Sample Input 1

2 2

Sample Output 1

7

We have the following seven ways:


Sample Input 2

3 3

Sample Output 2

131

Sample Input 3

5 100

Sample Output 3

379944232

Be sure to find the count modulo 998244353.