A - Anti-Adjacency

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

1 以上 N 以下の異なる整数を、差が 1 の整数をともに選ばないように K 個選ぶことができるか判定してください。

制約

  • 1\leq N,K\leq 100
  • N,K は整数である

入力

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

N K

出力

整数を K 個選ぶことができるなら YES を、そうでないなら NO を出力せよ。


入力例 1

3 2

出力例 1

YES

1,3 を選べばよいです。


入力例 2

5 5

出力例 2

NO

入力例 3

31 10

出力例 3

YES

入力例 4

10 90

出力例 4

NO

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

  • 1\leq N,K\leq 100
  • N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.


Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.


Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO
B - Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

4 つの街があり、順に 1,2,3,4 と番号が付いています。 道が 3 本あり、i 本目の道は異なる街 a_i,b_i を双方向に結んでいます。 同じ街の対の間を結ぶ道が複数あることはありません。街同士を行き来する手段は、道以外にはありません。 どの 2 つの街の間も、道を何本か通ることで行き来することができます。

すべての道をちょうど 1 回ずつ通ることですべての街を訪れることが可能かどうか判定してください。

制約

  • 1 \leq a_i,b_i \leq 4(1\leq i\leq 3)
  • a_ib_i は異なる (1\leq i\leq 3)
  • 同じ街の対の間を結ぶ道は複数存在しない
  • どの 2 つの街の間も、道を何本か通ることで行き来することができる

入力

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

a_1 b_1
a_2 b_2
a_3 b_3

出力

すべての道をちょうど 1 回ずつ通ることですべての街を訪れることが可能なら YES を、そうでないなら NO を出力せよ。


入力例 1

4 2
1 3
2 3

出力例 1

YES

1,3,2,4 の順に訪れることができます。


入力例 2

3 2
2 4
1 2

出力例 2

NO

入力例 3

2 1
3 2
4 3

出力例 3

YES

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4. Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally. No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

  • 1 \leq a_i,b_i \leq 4(1\leq i\leq 3)
  • a_i and b_i are different. (1\leq i\leq 3)
  • No two roads connect the same pair of towns.
  • Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.


Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.


Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES
C - When I hit my pocket...

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

すぬけ君は最初、ビスケットを 1 枚持っており、日本円は持っていません。 すぬけ君は、以下の操作を好きな順に合計ちょうど K 回行います。

  • 持っているビスケットを叩き、1 枚増やす
  • ビスケット A 枚を 1 円に交換する
  • 1 円をビスケット B 枚に交換する

K 回の操作の後、すぬけ君が持っているビスケットの枚数の最大値を求めてください。

制約

  • 1 \leq K,A,B \leq 10^9
  • K,A,B は整数である

入力

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

K A B

出力

K 回の操作の後、すぬけ君が持っているビスケットの枚数の最大値を出力せよ。


入力例 1

4 2 6

出力例 1

7

以下のように操作を行うと、K 回の操作の後、すぬけ君の持っているビスケットの枚数は最大になります。

  • ビスケットを叩く。すぬけ君は、ビスケット 2 枚と 0 円を持っている。
  • ビスケット 2 枚を 1 円に交換する。すぬけ君は、ビスケット 0 枚と 1 円を持っている。
  • ビスケットを叩く。すぬけ君は、ビスケット 1 枚と 1 円を持っている。
  • 1 円をビスケット 6 枚に交換する。すぬけ君は、ビスケット 7 枚と 0 円を持っている。

入力例 2

7 3 4

出力例 2

8

入力例 3

314159265 35897932 384626433

出力例 3

48518828981938099

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket. He will perform the following operations exactly K times in total, in the order he likes:

  • Hit his pocket, which magically increases the number of biscuits by one.
  • Exchange A biscuits to 1 yen.
  • Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

  • 1 \leq K,A,B \leq 10^9
  • K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.


Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

  • Hit his pocket. Now he has 2 biscuits and 0 yen.
  • Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.
  • Hit his pocket. Now he has 1 biscuits and 1 yen.
  • Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099
D - Ears

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

数直線上にすぬけ君がいます。すぬけ君は L 個の耳を持っており、以下の条件を満たしながら数直線上を連続的に散歩します。

  • 座標 0 未満の点や座標が L より大きい点を通ることはない
  • 整数座標の点で散歩を開始し、整数座標の点で散歩を終了する
  • 整数座標の点でのみ、進む方向を変えることができる

すぬけ君は、座標が整数 i を用いて i-0.5 と表される点を通るたびに、i 番目の耳に石を 1 個入れます。

すぬけ君が散歩を終えた後、りんごさんは以下の操作を好きな順に何度でも繰り返すことで、 各 i に対しすぬけ君の i 番目の耳には A_i 個の石が入っているようにしたいです。

  • すぬけ君の耳をひとつ選び、石を 1 個入れる
  • 石が 1 個以上入っているすぬけ君の耳をひとつ選び、石を 1 個取り出す

すぬけ君の散歩の方法をりんごさんが好きに決められるとき、必要な操作の回数の最小値を求めてください。

制約

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

入力

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

L
A_1
:
A_L

出力

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


入力例 1

4
1
0
2
3

出力例 1

1

すぬけ君が以下のように散歩をするとします。

  • 座標 3 から散歩を開始し、座標 4 で終了する。座標 3,4,3,2,3,4 と順に訪れる。

このとき、すぬけ君の耳には順に 0,0,2,3 個の石が入っています。 りんごさんがすぬけ君の 1 個目の耳に 1 個の石を入れることで、条件を満たすことができます。


入力例 2

8
2
0
0
2
1
3
4
1

出力例 2

3

入力例 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

出力例 3

1

Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

  • He never visits a point with coordinate less than 0, or a point with coordinate greater than L.
  • He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.
  • He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

  • Put a stone in one of Snuke's ears.
  • Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

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

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.


Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

  • He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively. Ringo can satisfy the requirement by putting one stone in the first ear.


Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1
E - Odd Subrectangles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

NM 列のマス目があります。 各マスには 0 または 1 の整数が書かれていて、上から i 行目、左から j 列目に書かれている整数は a_{ij} です。

行の部分集合 A と列の部分集合 B の組 2^{N+M} 通りのうち、以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。

  • A に属する行と B に属する列の交わりに属する |A||B| 個のマスに書かれた整数の総和が奇数である

制約

  • 1 \leq N,M \leq 300
  • 0 \leq a_{i,j} \leq 1(1\leq i\leq N,1\leq j\leq M)
  • 入力はすべて整数である

入力

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

N M
a_{11} ... a_{1M}
:
a_{N1} ... a_{NM}

出力

行の部分集合 A と列の部分集合 B の組のうち、条件を満たすものの個数を 998244353 で割ったあまりを出力せよ。


入力例 1

2 2
0 1
1 0

出力例 1

6

例えば、A として 1 行目のみを選び、B として 1,2 列目を選んだとき、その交わりに属するマスに書かれた整数の和は 0+1=1 になります。


入力例 2

2 3
0 0 0
0 1 0

出力例 2

8

Score : 800 points

Problem Statement

There is a square grid with N rows and M columns. Each square contains an integer: 0 or 1. The square at the i-th row from the top and the j-th column from the left contains a_{ij}.

Among the 2^{N+M} possible pairs of a subset A of the rows and a subset B of the columns, find the number of the pairs that satisfy the following condition, modulo 998244353:

  • The sum of the |A||B| numbers contained in the intersection of the rows belonging to A and the columns belonging to B, is odd.

Constraints

  • 1 \leq N,M \leq 300
  • 0 \leq a_{i,j} \leq 1(1\leq i\leq N,1\leq j\leq M)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_{11} ... a_{1M}
:
a_{N1} ... a_{NM}

Output

Print the number of the pairs of a subset of the rows and a subset of the columns that satisfy the condition, modulo 998244353.


Sample Input 1

2 2
0 1
1 0

Sample Output 1

6

For example, if A consists of the first row and B consists of both columns, the sum of the numbers contained in the intersection is 0+1=1.


Sample Input 2

2 3
0 0 0
0 1 0

Sample Output 2

8
F - Pass

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

N 人のすぬけ君が 1 列に並んでいます。 長さ N の文字列 S が与えられ、前から i 人目のすぬけ君は Si 文字目が 0 のとき赤いボールを 2 つ、1 のとき赤いボールと青いボールを 1 つずつ、2 のとき青いボールを 2 つ持っています。

高橋君は最初、空の列を持っています。以下の操作を 2N 回繰り返してできる列としてありうるものの個数を 998244353 で割った あまりを求めてください。

  • ボールを 1 つ以上持っているすぬけ君は全員同時に、自分が持っているボールの中から 1 つを選び、前のすぬけ君に渡す。ただし、先頭のすぬけ君は、高橋君に渡す。
  • 高橋君は、先頭のすぬけ君から受け取ったボールを、列の末尾に並べる。

制約

  • 1 \leq |S| \leq 2000
  • S0,1,2 からなる

整数 N は入力では与えられず、文字列 S の長さとして間接的に与えられることに注意せよ。


入力

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

S

出力

操作を 2N 回繰り返してできる列としてありうるものの個数を 998244353 で割ったあまりを出力せよ。


入力例 1

02

出力例 1

3

前から i 個目に並んだボールの色が赤のとき r を、青のとき bi 文字目の文字とした長さ 2N の文字列で列を表すことにすれば、 rrbb,rbrb,rbbr が条件を満たします。


入力例 2

1210

出力例 2

55

入力例 3

12001021211100201020

出力例 3

543589959

Score : 900 points

Problem Statement

There are N Snukes lining up in a row. You are given a string S of length N. The i-th Snuke from the front has two red balls if the i-th character in S is 0; one red ball and one blue ball if the i-th character in S is 1; two blue balls if the i-th character in S is 2.

Takahashi has a sequence that is initially empty. Find the number of the possible sequences he may have after repeating the following procedure 2N times, modulo 998244353:

  • Each Snuke who has one or more balls simultaneously chooses one of his balls and hand it to the Snuke in front of him, or hand it to Takahashi if he is the first Snuke in the row.
  • Takahashi receives the ball and put it to the end of his sequence.

Constraints

  • 1 \leq |S| \leq 2000
  • S consists of 0,1 and 2.

Note that the integer N is not directly given in input; it is given indirectly as the length of the string S.


Input

Input is given from Standard Input in the following format:

S

Output

Print the number of the possible sequences Takahashi may have after repeating the procedure 2N times, modulo 998244353.


Sample Input 1

02

Sample Output 1

3

There are three sequences that Takahashi may have: rrbb, rbrb and rbbr, where r and b stand for red and blue balls, respectively.


Sample Input 2

1210

Sample Output 2

55

Sample Input 3

12001021211100201020

Sample Output 3

543589959