A - B = C

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

L 以上 R 以下の整数 A,B,C の組であって、A-B=C を満たすものは何通りありますか?

T 個のケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 2\times 10^4
  • 0\le L \le R \le 10^6
  • 入力はすべて整数

入力

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

T
\text{case}_1
\vdots
\text{case}_T

各ケースは以下の形式で与えられる。

L R

出力

T 個の値を出力せよ。i 個目には \text{case}_i に対応する答えを出力せよ。


入力例 1

5
2 6
0 0
1000000 1000000
12345 67890
0 1000000

出力例 1

6
1
0
933184801
500001500001

最初のケースの答えは以下の 6 通りです。

  • 4 - 2 = 2
  • 5 - 2 = 3
  • 5 - 3 = 2
  • 6 - 2 = 4
  • 6 - 3 = 3
  • 6 - 4 = 2

Score : 300 points

Problem Statement

How many triples A,B,C of integers between L and R (inclusive) satisfy A-B=C?

You will be given T cases. Solve each of them.

Constraints

  • 1 \leq T \leq 2\times 10^4
  • 0\le L \le R \le 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
\text{case}_1
\vdots
\text{case}_T

Each case is in the following format:

L R

Output

Print T values; the i-th of them should be the answer for \text{case}_i.


Sample Input 1

5
2 6
0 0
1000000 1000000
12345 67890
0 1000000

Sample Output 1

6
1
0
933184801
500001500001

In the first case, we have the following six triples:

  • 4 - 2 = 2
  • 5 - 2 = 3
  • 5 - 3 = 2
  • 6 - 2 = 4
  • 6 - 3 = 3
  • 6 - 4 = 2
B - -- - B

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

すぬけくんは、整数 B を持って整数やさんを訪れました。 整数やさんでは、お金を払うことで、持っている整数を別の整数にしてもらうことができます。

具体的には、次の 2 種類のサービスを好きな順で好きなだけ購入することができます。

  • 1 円を払い、持っている整数を -1 倍する。
  • 2 円を払い、持っている整数から 1 を引く。

すぬけくんが C 円以内で作ることのできる整数は何通りありますか?

制約

  • -10^{18}\le B \le 10^{18}
  • 1\le C \le 10^{18}
  • 入力はすべて整数

入力

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

B C

出力

答えを出力せよ。


入力例 1

11 2

出力例 1

3

以下のように、-11,10,113 通りの数を作ることができます。

  • 何もしないとき、0 円を使って 11 を作ることができる
  • 11-1 倍すると、1 円を使って -11 を作ることができる
  • 11 から 1 を引くと、2 円を使って 10 を作ることができる

入力例 2

0 4

出力例 2

4

以下のように、-2,-1,0,14 通りの数を作ることができます。

  • 何もしないとき、0 円を使って 0 を作ることができる
  • 0 から 1 を引くと、2 円を使って -1 を作ることができる
  • 0 から 1 を引いて -1 倍すると、3 円を使って 1 を作ることができる
  • 0 から 1 を引いて 1 を引くと、4 円を使って -2 を作ることができる

入力例 3

112 20210213

出力例 3

20210436

入力例 4

-211 1000000000000000000

出力例 4

1000000000000000422

Score : 400 points

Problem Statement

Snuke has come to Seisu-ya (integer shop) with an integer B in his hand. In Seiyu-ya, you can exchange your integer for another integer by paying money.

More specifically, you can use the following two services any number of times in any order:

  • Pay 1 yen (the currency of Japan) to multiply your integer by -1.
  • Pay 2 yen to subtract 1 from your integer.

How many integers are there that Snuke can get for at most C yen?

Constraints

  • -10^{18}\le B \le 10^{18}
  • 1\le C \le 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

B C

Output

Print the answer.


Sample Input 1

11 2

Sample Output 1

3

There are three numbers that Snuke can get: -11, 10, and 11, as follows:

  • by doing nothing, he can get 11 for 0 yen;
  • by multiplying 11 by -1, he can get -11 for 1 yen;
  • by subtracting 1 from 11, he can get 10 for 2 yen.

Sample Input 2

0 4

Sample Output 2

4

There are four numbers that Snuke can get: -2, -1, 0, and 1, as follows:

  • by doing nothing, he can get 0 for 0 yen;
  • by subtracting 1 from 0, he can get -1 for 2 yen;
  • by subtracting 1 from 0 and then multiplying by -1, he can get 1 for 3 yen;
  • by subtracting 1 from 0 and then subtracting 1 again, he can get -2 for 4 yen.

Sample Input 3

112 20210213

Sample Output 3

20210436

Sample Input 4

-211 1000000000000000000

Sample Output 4

1000000000000000422
C - DFS Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋くんと青木くんは n 頂点の根付き木を使ったゲームをします。 木の頂点には 1 から n の整数がふられています。 この木の根は頂点 1 であり、v=2,\dots,n について、頂点 v の親は p_v です。 最初、各頂点にコインが 1 枚ずつ置かれています。また、頂点 1 にコマが置かれています。

高橋くんと青木くんは、高橋くんから始めて交互に、以下の操作を行います。

  • コマの置かれている頂点にコインがある場合、そのコインを獲得し、手番を終える。
  • コマの置かれている頂点にコインがない場合、以下のルールでコマを動かす (またはゲームを終える)。
    • コマが置かれている頂点の子のうち、コインが置かれている頂点が存在するときは、そのうちのいずれかの頂点を選んでその頂点にコマを動かし、手番を終える。
    • 存在しない場合、コマが置かれている頂点が根ならゲームを終える。そうでないとき、コマが置かれている頂点の親にコマを動かし、手番を終える。

高橋くんも青木くんも、自分が獲得するコインの枚数を最大にするために最適な行動をします。高橋くんが獲得するコインの枚数を求めてください。

制約

  • 2\le n \le 10^5
  • 1\le p_v \lt v

入力

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

n
p_2 \dots p_n

出力

両者が最適な行動をしたときに、高橋くんが獲得するコインの枚数を出力せよ。


入力例 1

10
1 2 3 4 5 6 7 8 9

出力例 1

10

どちらのプレイヤーにも選択肢が与えられず、必ず以下のようにゲームが進むため、高橋くんがすべてのコインを獲得します。

  • 高橋くんが頂点 1 に置かれたコインを獲得する。
  • 青木くんがコマを頂点 2 に動かす。
  • 高橋くんが頂点 2 に置かれたコインを獲得する。
  • 青木くんがコマを頂点 3 に動かす。
  • \vdots
  • 高橋くんが頂点 10 に置かれたコインを獲得する。
  • 青木くんがコマを頂点 9 に動かす。
  • 高橋くんがコマを頂点 8 に動かす。
  • 青木くんがコマを頂点 7 に動かす。
  • \vdots
  • 高橋くんがコマを頂点 2 に動かす。
  • 青木くんがコマを頂点 1 に動かす。
  • ゲームを終える。

入力例 2

5
1 2 3 1

出力例 2

2

まず、高橋くんが頂点 1 に置かれたコインを獲得します。 その後、青木くんは頂点 2 か頂点 5 のどちらかにコマを動かすことができます。 もし頂点 2 に動かした場合、青木くんは最終的に頂点 5 に置かれたコインのみを獲得します。 一方で、頂点 5 に動かした場合、青木くんは最終的に頂点 2,3,4 に置かれたコインを獲得します。 青木くんは自分が獲得するコインの枚数を最大にするために最適な行動をするため、コマを頂点 5 に動かすことを選びます。 よって、高橋くんが獲得するコインは 2 枚です。


入力例 3

10
1 1 3 1 3 6 7 6 6

出力例 3

5

Score : 500 points

Problem Statement

Takahashi and Aoki will play a game using a rooted tree with n vertices, numbered 1 through n. The root of the tree is Vertex 1. For each v=2,\dots,n, the parent of Vertex v is p_v. Initially, there is one coin on each vertex. Additionally, there is a piece on Vertex 1.

Takahashi and Aoki will alternately do the following operation, with Takahashi going first:

  • If there is a coin on the vertex occupied by the piece: get the coin and end his turn.
  • If there is no coin on the vertex occupied by the piece: move the piece (or end the game) as follows.
    • If there are vertices with coins on them among the children of the vertex occupied by the piece, choose one such vertex, move the piece to that vertex, and end his turn.
    • Otherwise, if the piece is on the root, end the game; if not, move the piece to the parent of the vertex occupied by the piece and end his turn.

Both Takahashi and Aoki will play optimally to maximize the number of coins they will get. Find the number of coins Takahashi will get.

Constraints

  • 2\le n \le 10^5
  • 1\le p_v \lt v

Input

Input is given from Standard Input in the following format:

n
p_2 \dots p_n

Output

Print the number of coins Takahashi will get when the two players play optimally.


Sample Input 1

10
1 2 3 4 5 6 7 8 9

Sample Output 1

10

There is no choice for the players, and the game will always go as follows, so Takahashi will get every coin.

  • Takahashi gets the coin on Vertex 1.
  • Aoki moves the piece to Vertex 2.
  • Takahashi gets the coin on Vertex 2.
  • Aoki moves the piece to Vertex 3.
  • \vdots
  • Takahashi gets the coin on Vertex 10.
  • Aoki moves the piece to Vertex 9.
  • Takahashi moves the piece to Vertex 8.
  • Aoki moves the piece to Vertex 7.
  • \vdots
  • Takahashi moves the piece to Vertex 2.
  • Aoki moves the piece to Vertex 1.
  • The game ends.

Sample Input 2

5
1 2 3 1

Sample Output 2

2

First, Takahashi gets the coin on Vertex 1. Then, Aoki can move the piece to Vertex 2 or Vertex 5. If he moves it to Vertex 2, Aoki will eventually get just the coin on Vertex 5. On the other hand, if he moves it to Vertex 5, Aoki will eventually get the coins on Vertex 2, 3, and 4. Since Aoki will play optimally to maximize the number of coins he will get, he will choose to move the piece to Vertex 5. Thus, Takahashi will get two coins.


Sample Input 3

10
1 1 3 1 3 6 7 6 6

Sample Output 3

5
D - Skate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋くんは HW 列のマスからなるスケートリンクにいます。 北から r 行目、西から c 列目をマス (r, c) と表すことにします。 各マスは氷または地面であり、マス (r, c)S_{r, c}# であるときは地面、. であるときは氷です。 スケートリンクの外側は壁で覆われています。

高橋くんは、スケートリンク上で静止しているとき、東西南北のいずれかに移動を開始することができます。 移動開始後、高橋くんは同じ方向に動き続け、壁にぶつかるか、(移動を開始したマス以外の)地面のマスの にたどりついたときに静止します。

以下の条件を満たす時、「 マス (r, c) から見て、スケートリンクは無駄がない」と言います:

  • 高橋くんがマス (r, c) で静止している状態から、うまく移動を繰り返すことによって、全てのマスを 通過 することができる。(各マスの上で静止できるかどうかは問わない。)

高橋くんはどのマスから見てもスケートリンクに無駄がないようにするために、いくつかの氷のマスを地面のマスに変更したいです。 最小でいくつのマスを変更すればよいか求めてください。

制約

  • 2\le H,W \le 1000
  • S_{r,c}# または .

入力

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

H W
S_{1,1}\dots S_{1,W}
\vdots
S_{H,1}\dots S_{H,W}

出力

どのマスから見てもスケートリンクに無駄がないようにするために変更する必要のあるマスの数の最小値を出力せよ。


入力例 1

3 9
.........
.........
.........

出力例 1

1

最初の状態では、マス (1,1) から始めてマス (2,2) を通過することができません。 例えば以下のように変更すれば条件を満たせます。

.........
........#
.........

入力例 2

10 10
..........
#...#.....
..........
..........
..........
....#.....
.#......#.
..........
..........
..........

出力例 2

6

Score : 600 points

Problem Statement

Takahashi is on a skating rink consisting of H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the north and c-th column from the west. Each square is ice or ground; (r, c) is ground if S_{r, c} is # and ice if S_{r, c} is .. There are walls around the rink.

When Takahashi is standing still on the rink, he can start moving to the east, west, south, or north. After he starts moving, he will keep moving in the same direction, until he bumps into a wall or he is on a ground square (other than the square where he started moving).

We say the rink is efficient for (r, c) when the following condition is satisfied:

  • There is a sequence of moves that starts at (r, c), standing still, and passes every square. (It is not required to stop on each square.)

Takahashi wants to change some number of ice squares to ground squares so that the rink is efficient for every square. How many squares does he need to change, at least?

Constraints

  • 2\le H,W \le 1000
  • S_{r,c} is # or ..

Input

Input is given from Standard Input in the following format:

H W
S_{1,1}\dots S_{1,W}
\vdots
S_{H,1}\dots S_{H,W}

Output

Print the minimum number of changes needed to make the rink efficient for every square.


Sample Input 1

3 9
.........
.........
.........

Sample Output 1

1

Initially, it is impossible to start at (1, 1) and pass (2, 2). One way to meet the objective is to change the rink as follows:

.........
........#
.........

Sample Input 2

10 10
..........
#...#.....
..........
..........
..........
....#.....
.#......#.
..........
..........
..........

Sample Output 2

6
E - Cigar Box

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

数列 (1,2,\dots,n) に対して、以下の操作をちょうど m 回繰り返したところ、(a_1,\dots ,a_n) になりました。

  • 項を 1 つ選んで消す。その後、消した項を数列の先頭か末尾に付け加える。

m 回の操作列としてありうるものの数を 998244353 で割ったあまりを求めてください。

ただし、2 つの操作列が同じであるとは、「どの操作についても、選んだ項と付け加えた位置がどちらも等しいこと」とします。

制約

  • 2\le n \le 3000
  • 1\le m \le 3000
  • a_1,\dots ,a_n1,\dots,n の順列

入力

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

n m
a_1 \dots a_n

出力

操作列としてありうるものの数を 998244353 で割ったあまりを出力せよ。


入力例 1

5 2
1 2 4 5 3

出力例 1

5

以下の 5 通りの操作列がありえます。

  • 1 を消して先頭に付け加える。数列は (1,2,3,4,5) となる。その後、3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。
  • 3 を消して先頭に付け加える。数列は (3,1,2,4,5) となる。その後、3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。
  • 3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。その後、1 を消して先頭に付け加える。数列は (1,2,4,5,3) となる。
  • 3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。その後、3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。
  • 5 を消して末尾に付け加える。数列は (1,2,3,4,5) となる。その後、3 を消して末尾に付け加える。数列は (1,2,4,5,3) となる。

入力例 2

2 16
1 2

出力例 2

150994942

4 種類の操作のうち、2 種類では数列が全く変わらず、もう 2 種類では 2 項が入れ替わります。 このことから、全ての操作列のうち半分である 4^m/2 = 2^{31} = 2147483648 が求める操作列の数であることが示せます。 よって、2147483648998244353 で割ったあまりである 150994942 が答えです。


入力例 3

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

出力例 3

129989699

Score : 700 points

Problem Statement

We applied the following operation m times on the sequence (1,2,\dots,n), and it resulted in (a_1,\dots ,a_n).

  • Erase one element. Then, add the erased element to the beginning or end of the sequence.

Find the number of possible sequences of the m operations, modulo 998244353.

Here, two sequences of operations are considered the same when, in every operation, the same element is chosen and added to the same position.

Constraints

  • 2\le n \le 3000
  • 1\le m \le 3000
  • a_1,\dots ,a_n is a permutation of 1,\dots,n.

Input

Input is given from Standard Input in the following format:

n m
a_1 \dots a_n

Output

Print the number of possible sequences of the m operations, modulo 998244353.


Sample Input 1

5 2
1 2 4 5 3

Sample Output 1

5

There are five possible sequences of operations, as follows:

  • Erase 1 and add it to the beginning, which results in (1,2,3,4,5). Then, erase 3 and add it to the end, which results in (1,2,4,5,3).
  • Erase 3 and add it to the beginning, which results in (3,1,2,4,5). Then, erase 3 and add it to the end, which results in (1,2,4,5,3).
  • Erase 3 and add it to the end, which results in (1,2,4,5,3). Then, erase 1 and add it to the beginning, which results in (1,2,4,5,3).
  • Erase 3 and add it to the end, which results in (1,2,4,5,3). Then, erase 3 and add it to the end, which results in (1,2,4,5,3).
  • Erase 5 and add it to the end, which results in (1,2,3,4,5). Then, erase 3 and add it to the end, which results in (1,2,4,5,3).

Sample Input 2

2 16
1 2

Sample Output 2

150994942

Two of the four kinds of operations have no effect on the sequence, and the other two swap the two elements. From this fact, we can show that there are 4^m/2 = 2^{31} = 2147483648 sequences of operations - half of all possible sequences - that we want to count. Thus, the answer is 2147483648 modulo 998244353, that is, 150994942.


Sample Input 3

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

Sample Output 3

129989699
F - Die Siedler

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 900

問題文

すぬけくんはカード 1 からカード nn 種類のカードを使うゲームで遊んでいます。 このゲームには、カードパックが m 種類存在し、i 種類目のカードパックにはカード js_{i,j} 枚含まれています。 どのカードパックにもカードが 1 枚以上含まれています。

最初、すぬけくんはカード jc_j 枚持っています(合計で 1 枚以上のカードを持っていることが保証されます)。

すぬけくんは、次の操作を好きな順番で好きな回数行うことができます。

  • カードパックをもらう:i=1,\dots,m のうちひとつを選ぶ。i 種類目のパックに含まれるカードをすべて手に入れる。
  • カードを交換する:j=1,\dots,n のうちひとつを選ぶ。カード j2j 枚捨てて、カード ((j \text{ mod } n) + 1)1 枚手に入れる。(カード j2j 枚以上持っていなければならない。)

すぬけくんは持っているカードの総数をできるだけ少なくしたいです。すぬけくんが達成できるカードの総数の最小値を求めてください。

制約

  • 2 \le n \le 16
  • 1 \le m \le 50
  • 0 \le s_{i,j}, c_j \lt 2j
  • c_1+\dots +c_n>0
  • s_{i,1}+\dots +s_{i,n}>0

入力

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

n m
c_1 \dots c_n
s_{1,1} \dots s_{1,n}
\vdots
s_{m,1} \dots s_{m,n}

出力

すぬけくんが達成できるカードの総数の最小値を出力せよ。


入力例 1

3 1
0 3 5
0 1 0

出力例 1

1

カード 1a_1 枚、カード 2a_2 枚、カード 3a_3 枚持っていることを (a_1, a_2, a_3) と書くことにします。 以下のように操作をすると、カードの総数を 1 枚にすることができます。

  • 1 種類目のカードパックをもらう。(0,4,5) となる。
  • j=2 を選んでカードを交換する。(0,0,6) となる。
  • j=3 を選んでカードを交換する。(1,0,0) となる。

カードの総数を 0 枚にすることはできないため、これが最小です。


入力例 2

5 2
0 0 1 7 0
0 3 2 0 0
1 0 4 0 0

出力例 2

2

2 種類目のカードパックを 2 パックもらったあと、1 種類目のカードパックをもらい、その後何度かカードを交換すると、 カード 4 とカード 51 枚ずつ持っている状態が作れます。


入力例 3

12 10
0 2 4 4 1 5 6 8 0 9 18 19
1 2 4 3 4 0 6 10 9 18 5 7
0 3 0 1 1 4 11 13 9 19 9 10
1 2 4 1 5 8 1 6 15 0 11 1
0 2 0 6 9 3 13 14 16 9 14 14
1 3 2 5 6 1 9 7 1 7 6 22
0 0 4 5 2 3 8 3 13 14 17 4
0 3 3 4 0 7 0 9 14 2 17 14
0 2 4 1 3 3 3 14 17 6 15 13
0 0 1 0 1 0 4 5 9 4 17 17
1 2 1 3 5 7 0 13 7 6 1 0

出力例 3

9

Score : 900 points

Problem Statement

Snuke is playing a game using n kinds of cards called Card 1 through Card n. There are m kinds of card packs available in this game; the i-th pack contains s_{i,j} copies of Card j. Every pack contains at least one card.

Initially, Snuke has c_j copies of Card j (it is guaranteed that he has at least one card in total).

He can do the following operations any number of times in any order:

  • Get a card pack: Choose i=1, \dots, or m and get all cards contained in the i-th pack.
  • Exchange cards: Choose j=1, \dots, or n, discard 2j copies of Card j, and get one copy of Card ((j \text{ mod } n) + 1). (He must have at least 2j copies of Card j.)

Snuke wants to have as few cards as possible in total. Find the minimum number of cards that Snuke can end up with.

Constraints

  • 2 \le n \le 16
  • 1 \le m \le 50
  • 0 \le s_{i,j}, c_j \lt 2j
  • c_1+\dots +c_n>0
  • s_{i,1}+\dots +s_{i,n}>0

Input

Input is given from Standard Input in the following format:

n m
c_1 \dots c_n
s_{1,1} \dots s_{1,n}
\vdots
s_{m,1} \dots s_{m,n}

Output

Print the minimum number of cards that Snuke can end up with.


Sample Input 1

3 1
0 3 5
0 1 0

Sample Output 1

1

Let (a_1, a_2, a_3) denote the fact that Snuke has a_1 copies of Card 1, a_2 copies of Card 2, and a_3 copies of Card 3. The following sequence of operations leaves Snuke with one card:

  • get the 1-st card pack, which results in (0,4,5);
  • exchange cards with j=2, which results in (0,0,6);
  • exchange cards with j=3, which results in (1,0,0).

It is impossible to end up with zero cards, so this is the minimum possible number.


Sample Input 2

5 2
0 0 1 7 0
0 3 2 0 0
1 0 4 0 0

Sample Output 2

2

By getting two packs of the second kind and one pack of the first kind, and then exchanging cards some number of times, Snuke can end up with one copy of Card 4 and one copy of Card 5.


Sample Input 3

12 10
0 2 4 4 1 5 6 8 0 9 18 19
1 2 4 3 4 0 6 10 9 18 5 7
0 3 0 1 1 4 11 13 9 19 9 10
1 2 4 1 5 8 1 6 15 0 11 1
0 2 0 6 9 3 13 14 16 9 14 14
1 3 2 5 6 1 9 7 1 7 6 22
0 0 4 5 2 3 8 3 13 14 17 4
0 3 3 4 0 7 0 9 14 2 17 14
0 2 4 1 3 3 3 14 17 6 15 13
0 0 1 0 1 0 4 5 9 4 17 17
1 2 1 3 5 7 0 13 7 6 1 0

Sample Output 3

9