A - Starry Sky Observation Log

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君は天文部に所属しており、夜空の観測記録を整理しています。観測データは HW 列のグリッド状の画像として記録されており、各マスには何も写っていないか、星が写っているかのどちらかです。

マスの位置は、左上のマスを (1, 1) として、上から i 行目・左から j 列目のマスを (i, j) で表します。観測データの i 行目は文字列 S_i で表され、S_ij 文字目が T ならばマス (i, j) に星が写っており、. ならば何も写っていないことを意味します。

観測データが与えられるので、星が写っているマスの位置をすべて求めてください。星は行番号が小さい順に、行番号が同じ場合は列番号が小さい順に出力してください。

制約

  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • H, W は整数である
  • S_i (1 \leq i \leq H).T のみからなる長さ W の文字列である
  • T1 つも含まれない(星が 0 個の)場合もありうる

入力

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

H W
S_1
S_2
\vdots
S_H
  • 1 行目には、観測データの行数 H と列数 W がスペース区切りで与えられる。
  • 続く H 行のうち i 行目 (1 \leq i \leq H) には、観測データの i 行目の状態を表す長さ W の文字列 S_i が与えられる。

出力

以下の形式で出力せよ。

K
r_1 c_1
r_2 c_2
\vdots
r_K c_K
  • 1 行目には、星の総数 K を出力する。
  • 続く K 行には、星が写っているマスの位置を、行番号が小さい順に、行番号が同じ場合は列番号が小さい順に、1 行に 1 つずつ出力する。k 行目 (1 \leq k \leq K) には、k 番目の星の行番号 r_k と列番号 c_k をスペース区切りで出力する。
  • K = 0 の場合は、1 行目に 0 のみを出力すればよい(後続の行は不要である)。

入力例 1

3 5
T..T.
.....
..T..

出力例 1

3
1 1
1 4
3 3

入力例 2

5 8
........
.T....T.
........
.T..T...
....T..T

出力例 2

6
2 2
2 7
4 2
4 5
5 5
5 8

入力例 3

10 15
T.............T
...T...........
.......T.......
...............
.T.............
...............
..........T....
...............
.............T.
T..T..T..T..T..

出力例 3

12
1 1
1 15
2 4
3 8
5 2
7 11
9 14
10 1
10 4
10 7
10 10
10 13

Score : 233 pts

Problem Statement

Takahashi is a member of the astronomy club and is organizing his nighttime sky observation records. The observation data is recorded as a grid image with H rows and W columns, where each cell either contains nothing or contains a star.

Cell positions are represented as (i, j), where (1, 1) is the top-left cell, and (i, j) denotes the cell in the i-th row from the top and the j-th column from the left. The i-th row of the observation data is represented by the string S_i, where if the j-th character of S_i is T, it means a star is captured in cell (i, j), and if it is ., it means nothing is captured there.

Given the observation data, find the positions of all cells that contain stars. Output the stars in ascending order of row number, and for stars in the same row, in ascending order of column number.

Constraints

  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • H, W are integers
  • S_i (1 \leq i \leq H) is a string of length W consisting only of . and T
  • It is possible that no T is included (i.e., there are 0 stars)

Input

The input is given from standard input in the following format:

H W
S_1
S_2
\vdots
S_H
  • The first line contains the number of rows H and the number of columns W of the observation data, separated by a space.
  • In the following H lines, the i-th line (1 \leq i \leq H) contains a string S_i of length W representing the state of the i-th row of the observation data.

Output

Output in the following format:

K
r_1 c_1
r_2 c_2
\vdots
r_K c_K
  • On the first line, output the total number of stars K.
  • On the following K lines, output the positions of cells containing stars, one per line, in ascending order of row number, and for the same row number, in ascending order of column number. On the k-th line (1 \leq k \leq K), output the row number r_k and column number c_k of the k-th star, separated by a space.
  • If K = 0, simply output 0 on the first line (no subsequent lines are needed).

Sample Input 1

3 5
T..T.
.....
..T..

Sample Output 1

3
1 1
1 4
3 3

Sample Input 2

5 8
........
.T....T.
........
.T..T...
....T..T

Sample Output 2

6
2 2
2 7
4 2
4 5
5 5
5 8

Sample Input 3

10 15
T.............T
...T...........
.......T.......
...............
.T.............
...............
..........T....
...............
.............T.
T..T..T..T..T..

Sample Output 3

12
1 1
1 15
2 4
3 8
5 2
7 11
9 14
10 1
10 4
10 7
10 10
10 13
B - Meeting Room Where Everyone Can Attend

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333

問題文

高橋君は会社の総務部で働いています。今度、 N 人の社員が全員参加する会議を開催することになりました。

会社には M 個の会議室があり、各会議室には 1 から M までの番号が付けられています。

社員 i1 \leq i \leq N )には、スケジュールの都合により参加条件を表す正の整数 P_i が定まっています。社員 i は、番号が P_i の倍数である会議室に限り参加することができます。言い換えると、会議室 j に社員 i が参加できるのは、 jP_i の倍数であるとき、またそのときに限ります。

高橋君は、すべての社員が参加できる会議室を見つけたいと考えています。すなわち、ある会議室の番号 j1 \leq j \leq M )であって、すべての社員 i1 \leq i \leq N )について jP_i の倍数であるようなものが存在するかどうかを判定してください。

そのような会議室が存在する場合は Yes を、存在しない場合は No を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^{18}
  • 1 \leq P_i \leq 10^9
  • 入力はすべて整数である

入力

N M
P_1 P_2 \ldots P_N
  • 1 行目には、社員の人数を表す整数 N と、会議室の数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各社員の参加条件を表す N 個の整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。

出力

すべての社員が参加できる会議室が存在する場合は Yes を、存在しない場合は No1 行で出力してください。


入力例 1

3 12
2 3 4

出力例 1

Yes

入力例 2

4 100
6 10 15 3

出力例 2

Yes

入力例 3

5 1000000000000000000
12 18 24 36 48

出力例 3

Yes

入力例 4

3 50
7 11 13

出力例 4

No

Score : 333 pts

Problem Statement

Takahashi works in the general affairs department of a company. He needs to organize a meeting that all N employees must attend.

The company has M meeting rooms, numbered from 1 to M.

For each employee i (1 \leq i \leq N), due to scheduling constraints, there is a positive integer P_i representing their participation condition. Employee i can only attend in a meeting room whose number is a multiple of P_i. In other words, employee i can attend in meeting room j if and only if j is a multiple of P_i.

Takahashi wants to find a meeting room that all employees can attend. Specifically, determine whether there exists a meeting room number j (1 \leq j \leq M) such that j is a multiple of P_i for every employee i (1 \leq i \leq N).

If such a meeting room exists, output Yes; otherwise, output No.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^{18}
  • 1 \leq P_i \leq 10^9
  • All input values are integers.

Input

N M
P_1 P_2 \ldots P_N
  • The first line contains an integer N representing the number of employees and an integer M representing the number of meeting rooms, separated by a space.
  • The second line contains N integers P_1, P_2, \ldots, P_N representing the participation conditions of each employee, separated by spaces.

Output

If there exists a meeting room that all employees can attend, output Yes; otherwise, output No, in a single line.


Sample Input 1

3 12
2 3 4

Sample Output 1

Yes

Sample Input 2

4 100
6 10 15 3

Sample Output 2

Yes

Sample Input 3

5 1000000000000000000
12 18 24 36 48

Sample Output 3

Yes

Sample Input 4

3 50
7 11 13

Sample Output 4

No
C - Maximizing Investment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は N 個の株式銘柄に投資しています。銘柄 i1 \leq i \leq N)の現在の資産価値は S_i です。

高橋君はこれから、以下の操作をちょうど K 回行います。

  • N 個の銘柄の中から 1 つを選び、その銘柄の現時点での資産価値を 2 倍にする。

各回の操作で選ぶ銘柄は自由であり、同じ銘柄を複数回選ぶこともできます。同じ銘柄を複数回選んだ場合、2 倍にする操作はそのたびに累積的に適用されます。例えば、資産価値が S の銘柄を 3 回選んだ場合、その銘柄の資産価値は 2^3 \times S = 8S になります。

高橋君は、K 回の操作をすべて行った後の N 個の銘柄の資産価値の合計をできるだけ大きくしたいと考えています。

操作の対象とする銘柄の選び方を最適にしたとき、K 回の操作後における N 個の銘柄の資産価値の合計の最大値を求めてください。ただし、答えが非常に大きくなることがあるため、合計の最大値を 10^9 + 7 で割った余りを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq S_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である

入力

N K
S_1 S_2 \ldots S_N
  • 1 行目には、銘柄の個数を表す整数 N と、操作の回数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各銘柄の操作前の資産価値を表す整数 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。

出力

K 回の操作を最適に行ったときの、N 個の銘柄の資産価値の合計の最大値を 10^9 + 7 で割った余りを 1 行で出力せよ。


入力例 1

3 2
1 3 2

出力例 1

15

入力例 2

4 3
5 5 1 2

出力例 2

48

入力例 3

10 20
12 7 25 3 18 30 1 9 14 22

出力例 3

31457391

入力例 4

40 123456789012345678
100000000 250000000 333333333 123456789 987654321 456789123 789123456 111111111 222222222 444444444 555555555 666666666 777777777 888888888 999999937 314159265 271828182 161803398 141421356 173205080 999999999 1 2 3 999999998 500000000 600000000 700000000 800000000 900000000 135791357 246802468 102030405 908070605 112233445 556677889 424242424 123123123 321321321 999000999

出力例 4

448506850

入力例 5

1 1000000000000000000
1000000000

出力例 5

963666222

Score : 366 pts

Problem Statement

Takahashi is investing in N stocks. The current asset value of stock i (1 \leq i \leq N) is S_i.

Takahashi will now perform the following operation exactly K times.

  • Choose one of the N stocks and double its current asset value.

He may freely choose which stock to select for each operation, and the same stock may be chosen multiple times. If the same stock is chosen multiple times, the doubling operation is applied cumulatively each time. For example, if a stock with asset value S is chosen 3 times, its asset value becomes 2^3 \times S = 8S.

Takahashi wants to maximize the total asset value of all N stocks after performing all K operations.

When the choice of stocks for the operations is made optimally, find the maximum possible total asset value of the N stocks after K operations. Since the answer can be very large, output the maximum total modulo 10^9 + 7.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N K
S_1 S_2 \ldots S_N
  • The first line contains an integer N representing the number of stocks and an integer K representing the number of operations, separated by a space.
  • The second line contains integers S_1, S_2, \ldots, S_N representing the asset values of each stock before the operations, separated by spaces.

Output

Output in one line the maximum total asset value of the N stocks when the K operations are performed optimally, modulo 10^9 + 7.


Sample Input 1

3 2
1 3 2

Sample Output 1

15

Sample Input 2

4 3
5 5 1 2

Sample Output 2

48

Sample Input 3

10 20
12 7 25 3 18 30 1 9 14 22

Sample Output 3

31457391

Sample Input 4

40 123456789012345678
100000000 250000000 333333333 123456789 987654321 456789123 789123456 111111111 222222222 444444444 555555555 666666666 777777777 888888888 999999937 314159265 271828182 161803398 141421356 173205080 999999999 1 2 3 999999998 500000000 600000000 700000000 800000000 900000000 135791357 246802468 102030405 908070605 112233445 556677889 424242424 123123123 321321321 999000999

Sample Output 4

448506850

Sample Input 5

1 1000000000000000000
1000000000

Sample Output 5

963666222
D - Card Taking Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君と青木君が、カードを使ったゲームで遊びます。

テーブルの上に N 枚のカードが一列に並べられており、左から i 番目のカードには整数 V_i が書かれています。なお、V_i は負の値である場合もあります。

二人は以下のルールに従ってカードを取り合います。

  • 高橋君が先手、青木君が後手で、交互に行動します。
  • 自分のターンでは、テーブルに残っているカードの列の 左端 または 右端 のカードをちょうど1枚選んで取り、自分の手元に置きます。取られたカードはテーブルから除かれ、残りのカードの並び順は変わりません。
  • すべてのカードが取られたらゲームは終了します。

二人はお互いの戦略を含むすべての情報を知っており、それぞれ自分が取ったカードに書かれた整数の合計を最大化するように最適に行動します。

二人が最適に行動したとき、高橋君が取ったカードに書かれた整数の合計から青木君が取ったカードに書かれた整数の合計を引いた値を求めてください。

制約

  • 1 \leq N \leq 3000
  • -10^9 \leq V_i \leq 10^9
  • 入力はすべて整数である。

入力

N
V_1 V_2 \ldots V_N
  • 1 行目には、カードの枚数を表す整数 N が与えられる。
  • 2 行目には、各カードに書かれた整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。ここで V_i は左から i 番目のカードに書かれた整数である。

出力

二人が最適に行動したとき、高橋君が取ったカードに書かれた整数の合計から青木君が取ったカードに書かれた整数の合計を引いた値を 1 行で出力せよ。


入力例 1

4
3 1 2 5

出力例 1

3

入力例 2

4
-1 5 -3 2

出力例 2

11

入力例 3

10
8 -3 5 12 -7 2 9 -1 6 4

出力例 3

15

入力例 4

20
15 -8 23 4 -12 7 19 -3 11 6 -5 14 2 -9 18 1 -6 10 8 -2

出力例 4

53

入力例 5

1
-1000000000

出力例 5

-1000000000

Score : 400 pts

Problem Statement

Takahashi and Aoki play a game using cards.

N cards are arranged in a row on the table, and the i-th card from the left has an integer V_i written on it. Note that V_i may be negative.

The two players take cards according to the following rules:

  • Takahashi goes first, Aoki goes second, and they take turns alternately.
  • On their turn, a player chooses exactly one card from either the left end or the right end of the row of cards remaining on the table, takes it, and places it in their hand. The taken card is removed from the table, and the order of the remaining cards does not change.
  • The game ends when all cards have been taken.

Both players know all information including each other's strategies, and each plays optimally to maximize the sum of the integers written on the cards they have taken.

When both players play optimally, find the value obtained by subtracting the sum of the integers written on the cards Aoki took from the sum of the integers written on the cards Takahashi took.

Constraints

  • 1 \leq N \leq 3000
  • -10^9 \leq V_i \leq 10^9
  • All inputs are integers.

Input

N
V_1 V_2 \ldots V_N
  • The first line contains an integer N representing the number of cards.
  • The second line contains the integers V_1, V_2, \ldots, V_N written on each card, separated by spaces. Here, V_i is the integer written on the i-th card from the left.

Output

Print in one line the value obtained by subtracting the sum of the integers written on the cards Aoki took from the sum of the integers written on the cards Takahashi took, when both players play optimally.


Sample Input 1

4
3 1 2 5

Sample Output 1

3

Sample Input 2

4
-1 5 -3 2

Sample Output 2

11

Sample Input 3

10
8 -3 5 12 -7 2 9 -1 6 4

Sample Output 3

15

Sample Input 4

20
15 -8 23 4 -12 7 19 -3 11 6 -5 14 2 -9 18 1 -6 10 8 -2

Sample Output 4

53

Sample Input 5

1
-1000000000

Sample Output 5

-1000000000
E - Number of Blocks in an Interval

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君は、N 個のマスが横一列に並んだ塗り絵を管理している。マスには左から順に 1 番から N 番までの番号が付いている。

各マス i には色 C_i が塗られている。マスの列において、同じ色が連続するこれ以上延ばせない極大な区間のことを ブロック と呼ぶ。区間 [L, R]ブロック数 とは、マスの列 C_L, C_{L+1}, \dots, C_R をブロックに分けたときのブロックの個数である。

例えば、列が 1, 1, 2, 2, 2, 1 であればブロックは (1, 1), (2, 2, 2), (1)3 つであり、ブロック数は 3 である。

青木君は以下の 2 種類の操作を合計 Q 回行う。各操作を順に処理し、問い合わせに答えよ。

  • 1 L R X :区間 [L, R] のすべてのマスの色を X に変更する。
  • 2 L R :現在の区間 [L, R] のブロック数を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq C_i \leq 10^9
  • 操作 1 L R X について、1 \leq L \leq R \leq N1 \leq X \leq 10^9
  • 操作 2 L R について、1 \leq L \leq R \leq N
  • 入力はすべて整数である。

入力

N Q
C_1 C_2 \dots C_N
T_1
T_2
\vdots
T_Q
  • 1 行目には、マスの数 N と操作の回数 Q が、スペース区切りで与えられる。
  • 2 行目には、各マスの初期の色 C_1, C_2, \dots, C_N が、スペース区切りで与えられる。
  • 続く Q 行の i 行目には、i 番目の操作 T_i が与えられる。各操作は以下のいずれかの形式である。
  • 1 L R X:区間 [L, R] のすべてのマスの色を X に変更する。
  • 2 L R:現在の区間 [L, R] のブロック数を求める問い合わせ。

出力

操作 2 L R ごとに、その区間のブロック数を 1 行に 1 つずつ出力せよ。


入力例 1

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

出力例 1

3
2
1
3

入力例 2

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

出力例 2

3
1
1
2
1

入力例 3

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

出力例 3

7
4
4
4
3
1
3

入力例 4

20 18
1 1 2 3 3 4 4 4 5 6 6 7 8 8 9 9 9 10 1 1
2 1 20
2 4 17
1 3 8 3
2 1 10
1 10 15 8
2 8 18
1 1 2 2
2 1 5
1 16 20 8
2 10 20
1 5 5 2
2 1 20
1 6 14 1
2 1 20
2 6 14
1 1 20 7
2 1 20
2 11 11

出力例 4

11
7
4
5
2
1
6
5
1
1
1

入力例 5

1 5
1000000000
2 1 1
1 1 1 1
2 1 1
1 1 1 1000000000
2 1 1

出力例 5

1
1
1

Score : 466 pts

Problem Statement

Takahashi manages a coloring sheet consisting of N cells arranged in a horizontal row. The cells are numbered from 1 to N from left to right.

Each cell i is painted with color C_i. In a sequence of cells, a maximal consecutive interval of the same color that cannot be extended further is called a block. The number of blocks in an interval [L, R] is the number of blocks when the cell sequence C_L, C_{L+1}, \dots, C_R is divided into blocks.

For example, if the sequence is 1, 1, 2, 2, 2, 1, the blocks are (1, 1), (2, 2, 2), (1), giving 3 blocks, so the number of blocks is 3.

Aoki performs a total of Q operations of the following two types. Process each operation in order and answer the queries.

  • 1 L R X: Change the color of all cells in the interval [L, R] to X.
  • 2 L R: Output the number of blocks in the current interval [L, R].

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq C_i \leq 10^9
  • For operation 1 L R X: 1 \leq L \leq R \leq N, 1 \leq X \leq 10^9
  • For operation 2 L R: 1 \leq L \leq R \leq N
  • All input values are integers.

Input

N Q
C_1 C_2 \dots C_N
T_1
T_2
\vdots
T_Q
  • The first line contains the number of cells N and the number of operations Q, separated by a space.
  • The second line contains the initial colors of each cell C_1, C_2, \dots, C_N, separated by spaces.
  • The i-th of the following Q lines contains the i-th operation T_i. Each operation is in one of the following formats:
  • 1 L R X: Change the color of all cells in the interval [L, R] to X.
  • 2 L R: A query to find the number of blocks in the current interval [L, R].

Output

For each operation 2 L R, output the number of blocks in that interval, one per line.


Sample Input 1

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

Sample Output 1

3
2
1
3

Sample Input 2

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

Sample Output 2

3
1
1
2
1

Sample Input 3

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

Sample Output 3

7
4
4
4
3
1
3

Sample Input 4

20 18
1 1 2 3 3 4 4 4 5 6 6 7 8 8 9 9 9 10 1 1
2 1 20
2 4 17
1 3 8 3
2 1 10
1 10 15 8
2 8 18
1 1 2 2
2 1 5
1 16 20 8
2 10 20
1 5 5 2
2 1 20
1 6 14 1
2 1 20
2 6 14
1 1 20 7
2 1 20
2 11 11

Sample Output 4

11
7
4
5
2
1
6
5
1
1
1

Sample Input 5

1 5
1000000000
2 1 1
1 1 1 1
2 1 1
1 1 1 1000000000
2 1 1

Sample Output 5

1
1
1