A - A Unique Letter

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

長さ 3 の文字列 S が与えられます。
S1 度だけ含まれる文字を 1 つ出力してください。
但し、そのような文字が存在しない場合は代わりに -1 と出力してください。

制約

  • S は英小文字のみからなる 3 文字の文字列

入力

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

S

出力

答えを出力せよ。正解が複数ある場合、どれを出力してもよい。


入力例 1

pop

出力例 1

o

popo1 度だけ含まれます。


入力例 2

abc

出力例 2

a

abca, b, c はどれも 1 度だけ含まれるので、どれを出力しても構いません。


入力例 3

xxx

出力例 3

-1

xxx1 度だけ含まれる文字はありません。

Score : 100 points

Problem Statement

You are given a string S of length 3.
Print a character that occurs only once in S.
If there is no such character, print -1 instead.

Constraints

  • S is a string of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer. If multiple solutions exist, you may print any of them.


Sample Input 1

pop

Sample Output 1

o

o occurs only once in pop.


Sample Input 2

abc

Sample Output 2

a

a, b, and c occur once each in abc, so you may print any of them.


Sample Input 3

xxx

Sample Output 3

-1

No character occurs only once in xxx.

B - Better Students Are Needed!

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

入学試験の受験者が N 人います。
試験の結果、 i 番の受験生は数学で A_i 点、英語で B_i 点を取りました。

試験の合格者は次のように決められます。

  1. 数学の点が高い方から X 人を合格とする。
  2. 次に、この時点でまだ合格となっていない受験者のうち、英語の点が高い方から Y 人を合格とする。
  3. 次に、この時点でまだ合格となっていない受験者のうち、数学と英語の合計点が高い方から Z 人を合格とする。
  4. ここまでで合格となっていない受験者は、不合格とする。

ただし、 1. から 3. までのどの段階についても、同点であった場合は受験生の番号の小さい方を優先します。入出力例も参照してください。

以上の手順で合格者を決める時、合格となった受験生の番号を小さい方から順に改行区切りで出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 1000
  • 0 \le X,Y,Z \le N
  • 1 \le X+Y+Z \le N
  • 0 \le A_i,B_i \le 100

入力

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

N X Y Z
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

合格となった受験生の番号を小さい方から順に改行区切りで出力せよ。


入力例 1

6 1 0 2
80 60 80 60 70 70
40 20 50 90 90 80

出力例 1

1
4
5
  • まず、数学の点が高い方から 1 人が合格となります。
    • 数学の最高点は 80 点で 1 番の受験生と 3 番の受験生が並んでいますが、受験生の番号が小さい方が優先され 1 番の受験生が合格となります。
  • 次に、まだ合格となっていない受験者のうち、英語の点が高い方から 0 人が合格となります。
    • 明らかに、ここで合格者が増えることはありません。
  • 次に、まだ合格となっていない受験者のうち、数学と英語の合計点が高い方から 2 人が合格となります。
    • まず、まだ合格となっていない受験者の中で、合計点が 160 点と最も高い 5 番の受験生が合格となります。
    • 次に、まだ合格となっていない受験者の中で、合計点が 150 点の 4 番の受験生と 6 番の受験生が並んでいます。受験生の番号の小さい方が優先され、 4 番の受験生が合格となります。

以上より、合格となる受験生の番号は 1,4,5 なので、小さい方から出力してください。


入力例 2

5 2 1 2
0 100 0 100 0
0 0 100 100 0

出力例 2

1
2
3
4
5

全員が合格となることもあります。


入力例 3

15 4 3 2
30 65 20 95 100 45 70 85 20 35 95 50 40 15 85
0 25 45 35 65 70 80 90 40 55 20 20 45 75 100

出力例 3

2
4
5
6
7
8
11
14
15

Score : 200 points

Problem Statement

N examinees took an entrance exam.
The examinee numbered i scored A_i points in math and B_i points in English.

The admissions are determined as follows.

  1. X examinees with the highest math scores are admitted.
  2. Then, among the examinees who are not admitted yet, Y examinees with the highest English scores are admitted.
  3. Then, among the examinees who are not admitted yet, Z examinees with the highest total scores in math and English are admitted.
  4. Those examinees who are not admitted yet are rejected.

Here, in each of the steps 1. to 3., ties are broken by examinees' numbers: an examinee with the smaller examinee's number is prioritized. See also Sample Input and Output.

Print the examinees' numbers of the admitted examinees determined by the steps above in ascending order, separated by newlines.

Constraints

  • All values in input are integers.
  • 1 \le N \le 1000
  • 0 \le X,Y,Z \le N
  • 1 \le X+Y+Z \le N
  • 0 \le A_i,B_i \le 100

Input

Input is given from Standard Input in the following format:

N X Y Z
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the examinees' number of the admitted examinees in ascending order, separated by newlines.


Sample Input 1

6 1 0 2
80 60 80 60 70 70
40 20 50 90 90 80

Sample Output 1

1
4
5
  • First, 1 examinee with the highest math score is admitted.
    • Examinee 1 is tied with Examinee 3, scoring the highest 80 points in math, and the tie is broken by the examinees' numbers, so Examinee 1 is admitted.
  • Then, among the examinees who are not admitted yet, 0 examinees with the highest English scores are admitted.
    • Obviously, it does not affect the admissions.
  • Then, among the examinees who are not admitted yet, 2 examinees with the highest total scores in math and English are admitted.
    • First, among the examinees who are not admitted yet, Examinee 5 is admitted, scoring the highest total score of 160 points.
    • Next, among the examinees who are not admitted yet, Examinee 4 is tied with Examinee 6, scoring a total score of 150 points. The tie is broken by the examinees' numbers, and Examinee 4 is admitted.

Therefore, the examinees' numbers of the admitted examinees are 1, 4, and 5. Print them in ascending order.


Sample Input 2

5 2 1 2
0 100 0 100 0
0 0 100 100 0

Sample Output 2

1
2
3
4
5

All examinees may be admitted.


Sample Input 3

15 4 3 2
30 65 20 95 100 45 70 85 20 35 95 50 40 15 85
0 25 45 35 65 70 80 90 40 55 20 20 45 75 100

Sample Output 3

2
4
5
6
7
8
11
14
15
C - Changing Jewels

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。

  • レベル n の赤い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
  • レベル n の青い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。

高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?

制約

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • 入力される値はすべて整数

入力

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

N X Y

出力

答えを出力せよ。


入力例 1

2 3 4

出力例 1

12

次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。

  • まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
  • 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
  • これ以上変換を行うことはできません。

12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。


入力例 2

1 5 5

出力例 2

0

高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。


入力例 3

10 5 5

出力例 3

3942349900

答えが 32 bit 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.

  • Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
  • Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".

Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

Print the answer.


Sample Input 1

2 3 4

Sample Output 1

12

Takahashi can obtain 12 blue jewels of level 1 by the following conversions.

  • First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
    • After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
  • Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
    • After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
  • He cannot perform a conversion anymore.

He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.


Sample Input 2

1 5 5

Sample Output 2

0

Takahashi may not be able to obtain a blue jewel of level 1.


Sample Input 3

10 5 5

Sample Output 3

3942349900

Note that the answer may not fit into a 32-bit integer type.

D - Draw Your Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 から N が書かれた N 枚のカードが裏向きで積まれた山札があり、上から i 枚目のカードには整数 P_i が書かれています。

この山札を使って、以下の行動を N ターン繰り返します。

  • 山札の一番上のカードを引いて、そこに書かれた整数を X とする。
  • 場に見えている表向きのカードであって書かれた整数が X 以上であるもののうち、書かれた整数が最小のものの上に、引いたカードを表向きで重ねる。もし場にそのようなカードがなければ、引いたカードをどれにも重ねずに表向きで場に置く。
  • その後、表向きのカードが K 枚重ねられた山が場にあればその山のカードを全て食べる。食べられたカードは全て場から消滅する。

各カードについて、何ターン目に食べられるか、あるいは最後まで食べられないかを求めてください。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 2 \times 10^5
  • P(1,2,\dots,N) の順列 ( (1,2,\dots,N) を並べ替えて得られる列 ) である

入力

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

N K
P_1 P_2 \dots P_N

出力

N 行出力せよ。
そのうち i (1 \le i \le N) 行目には、整数 i が書かれたカードについて、以下のように出力せよ。

  • もし i が書かれたカードが食べられるなら、それが何ターン目であるかを整数として出力せよ。
  • そうでないなら -1 と出力せよ。

入力例 1

5 2
3 5 2 1 4

出力例 1

4
3
3
-1
4

この入力では、 P=(3,5,2,1,4),K=2 です。

  • 1 ターン目に、 3 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 2 ターン目に、 5 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 3 ターン目に、 2 が書かれたカードが 3 が書かれたカードの上に表向きで重ねられます。
    • この時点で上から 2,3 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
  • 4 ターン目に、 1 が書かれたカードが 5 が書かれたカードの上に表向きで重ねられます。
    • この時点で上から 1,5 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
  • 5 ターン目に、 4 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 4 が書かれたカードは、最後まで食べられませんでした。

入力例 2

5 1
1 2 3 4 5

出力例 2

1
2
3
4
5

K=1 である場合、全てのカードは場に置かれたターンに食べられます。


入力例 3

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

出力例 3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15

Score : 400 points

Problem Statement

There is a deck consisting of N face-down cards with an integer from 1 through N written on them. The integer on the i-th card from the top is P_i.

Using this deck, you will perform N moves, each consisting of the following steps:

  • Draw the topmost card from the deck. Let X be the integer written on it.
  • Stack the drawn card, face up, onto the card with the smallest integer among the face-up topmost cards on the table with an integer greater than or equal to X written on them. If there is no such card on the table, put the drawn card on the table, face up, without stacking it onto any card.
  • Then, if there is a pile consisting of K face-up cards on the table, eat all those cards. The eaten cards all disappear from the table.

For each card, find which of the N moves eats it. If the card is not eaten until the end, report that fact.

Constraints

  • All values in input are integers.
  • 1 \le K \le N \le 2 \times 10^5
  • P is a permutation of (1,2,\dots,N) (i.e. a sequence obtained by rearranging (1,2,\dots,N)).

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \dots P_N

Output

Print N lines.
The i-th line (1 \le i \le N) should describe the card with the integer i written on it. Specifically,

  • if the card with i written on it is eaten in the x-th move, print x;
  • if that card is not eaten in any move, print -1.

Sample Input 1

5 2
3 5 2 1 4

Sample Output 1

4
3
3
-1
4

In this input, P=(3,5,2,1,4) and K=2.

  • In the 1-st move, the card with 3 written on it is put on the table, face up, without stacked onto any card.
  • In the 2-nd move, the card with 5 written on it is put on the table, face up, without stacked onto any card.
  • In the 3-rd move, the card with 2 written on it is stacked, face up, onto the card with 3 written on it.
    • Now there is a pile consisting of K=2 face-up cards, on which 2 and 3 from the top are written, so these cards are eaten.
  • In the 4-th move, the card with 1 written on it is stacked, face up, onto the card with 5 written on it.
    • Now there is a pile consisting of K=2 face-up cards, on which 1 and 5 from the top are written, so these cards are eaten.
  • In the 5-th move, the card with 4 written on it is put on the table, face up, without stacked onto any card.
  • The card with 4 written on it was not eaten until the end.

Sample Input 2

5 1
1 2 3 4 5

Sample Output 2

1
2
3
4
5

If K=1, every card is eaten immediately after put on the table within a single move.


Sample Input 3

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

Sample Output 3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15
E - At Least One

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

整数 M および N 個の整数の組 (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N) が与えられます。
すべての i について 1 \leq A_i \lt B_i \leq M が成り立っています。

次の条件を満たす数列 S良い数列と呼びます。

  • S は数列 (1,2,3,..., M) の連続部分列である。
  • すべての i について SA_i, B_i の少なくとも一方を含んでいる。

長さ k の良い数列としてあり得るものの個数を f(k) とします。
f(1), f(2), \dots, f(M) を列挙してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq M
  • 入力される値はすべて整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを以下の形式で出力せよ。

f(1) f(2) \dots f(M)

入力例 1

3 5
1 3
1 4
2 5

出力例 1

0 1 3 2 1

良い数列としてあり得るものを列挙すると次のようになります。

  • (1,2)
  • (1,2,3)
  • (2,3,4)
  • (3,4,5)
  • (1,2,3,4)
  • (2,3,4,5)
  • (1,2,3,4,5)

入力例 2

1 2
1 2

出力例 2

2 1

入力例 3

5 9
1 5
1 7
5 6
5 8
2 6

出力例 3

0 0 1 2 4 4 3 2 1

Score : 500 points

Problem Statement

You are given an integer M and N pairs of integers (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N).
For all i, it holds that 1 \leq A_i \lt B_i \leq M.

A sequence S is said to be a good sequence if the following conditions are satisfied:

  • S is a contiguous subsequence of the sequence (1,2,3,..., M).
  • For all i, S contains at least one of A_i and B_i.

Let f(k) be the number of possible good sequences of length k.
Enumerate f(1), f(2), \dots, f(M).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answers in the following format:

f(1) f(2) \dots f(M)

Sample Input 1

3 5
1 3
1 4
2 5

Sample Output 1

0 1 3 2 1

Here is the list of all possible good sequences.

  • (1,2)
  • (1,2,3)
  • (2,3,4)
  • (3,4,5)
  • (1,2,3,4)
  • (2,3,4,5)
  • (1,2,3,4,5)

Sample Input 2

1 2
1 2

Sample Output 2

2 1

Sample Input 3

5 9
1 5
1 7
5 6
5 8
2 6

Sample Output 3

0 0 1 2 4 4 3 2 1
F - Find 4-cycle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

S+T 頂点 M 辺の単純無向グラフ G があります。頂点には 1 から S+T の番号が、辺には 1 から M の番号が割り振られています。辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、頂点集合 V_1 = \lbrace 1, 2,\dots, S\rbrace および V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace はともに独立集合です。

長さ 4 のサイクルを 4-cycle と呼びます。
G が 4-cycle を含む場合、4-cycle をどれか 1 つ選び、選んだサイクルに含まれる頂点を出力してください。頂点を出力する順番は自由です。
G が 4-cycle を含まない場合、 -1 を出力してください。

独立集合とは? グラフ G の独立集合とは、G に含まれるいくつかの頂点からなる集合 V' であって、V' に含まれる任意の 2 つの頂点の間に辺がないものを言います。

制約

  • 2 \leq S \leq 3 \times 10^5
  • 2 \leq T \leq 3000
  • 4 \leq M \leq \min(S \times T,3 \times 10^5)
  • 1 \leq u_i \leq S
  • S + 1 \leq v_i \leq S + T
  • i \neq j ならば (u_i, v_i) \neq (u_j, v_j)
  • 入力される値はすべて整数

入力

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

S T M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

G が 4-cycle を含む場合は、任意の 4-cycle を 1 つ選び、選んだサイクルに含まれている相異なる 4 個の頂点の番号を出力せよ。(頂点の順番は問わない。)
G が 4-cycle を含まない場合は -1 を出力せよ。


入力例 1

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

出力例 1

1 2 4 5

頂点 14422551 の間に辺があるので 頂点 1,2,4,5 は 4-cycle をなします。よって 1, 2, 4, 5 を出力すればよいです。
頂点を出力する順番は自由で、出力例のほかにも例えば 2 5 1 4 のような出力も正答となります。


入力例 2

3 2 4
1 4
1 5
2 5
3 5

出力例 2

-1

G が 4-cycle を含まない入力もあります。


入力例 3

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

出力例 3

1 7 2 9

Score : 500 points

Problem Statement

We have a simple undirected graph G with (S+T) vertices and M edges. The vertices are numbered 1 through (S+T), and the edges are numbered 1 through M. Edge i connects Vertices u_i and v_i.
Here, vertex sets V_1 = \lbrace 1, 2,\dots, S\rbrace and V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace are both independent sets.

A cycle of length 4 is called a 4-cycle.
If G contains a 4-cycle, choose any of them and print the vertices in the cycle. You may print the vertices in any order.
If G does not contain a 4-cycle, print -1.

What is an independent set? An independent set of a graph G is a set V' of some of the vertices in G such that no two vertices of V' have an edge between them.

Constraints

  • 2 \leq S \leq 3 \times 10^5
  • 2 \leq T \leq 3000
  • 4 \leq M \leq \min(S \times T,3 \times 10^5)
  • 1 \leq u_i \leq S
  • S + 1 \leq v_i \leq S + T
  • If i \neq j, then (u_i, v_i) \neq (u_j, v_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

S T M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If G contains a 4-cycle, choose any of them, and print the indices of the four distinct vertices in the cycle. (The order of the vertices does not matter.)
If G does not contain a 4-cycle, print -1.


Sample Input 1

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

Sample Output 1

1 2 4 5

There are edges between Vertices 1 and 4, 4 and 2, 2 and 5, and 5 and 1, so Vertices 1, 2, 4, and 5 form a 4-cycle. Thus, 1, 2, 4, and 5 should be printed.
The vertices may be printed in any order. Besides the Sample Output, 2 5 1 4 is also considered correct, for example.


Sample Input 2

3 2 4
1 4
1 5
2 5
3 5

Sample Output 2

-1

Some inputs may give G without a 4-cycle.


Sample Input 3

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

Sample Output 3

1 7 2 9
G - Scalene Triangle Area

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N \times N のグリッドがあり、このグリッドの上から i マス目、左から j マス目を (i,j) と呼びます。
このグリッドの各マスには高々 1 個のコマが置かれています。
グリッドの状態は N 個の文字列 S_i として与えられ、

  • S_ij 文字目が O であるとき (i,j)1 つコマが置かれていること
  • S_ij 文字目が X であるとき (i,j) にコマは置かれていないこと

を表します。

整数 M が与えられます。 この M を使って、 (s,t) に置かれているコマ P について、以下の条件を全て満たすマス (u,v)P が守っているマスと定義します。

  • s \le u \le N
  • t \le v \le N
  • (u - s) + \frac{(v - t)}{2} < M

Q 個のマス (X_i,Y_i) について、そのマスを守っているコマの個数を求めてください。

制約

  • N,M,X_i,Y_i,Q は整数
  • 1 \le N \le 2000
  • 1 \le M \le 2 \times N
  • S_iO, X からなる
  • 1 \le Q \le 2 \times 10^5
  • 1 \le X_i,Y_i \le N

入力

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

N M
S_1
S_2
\vdots
S_N
Q
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

出力

Q 行出力せよ。
そのうち i ( 1 \le i \le Q ) 行目には、マス (X_i,Y_i) を守っているコマの個数を整数として出力せよ。


入力例 1

4 2
OXXX
XXXX
XXXX
XXXX
6
1 1
1 4
2 2
2 3
3 1
4 4

出力例 1

1
1
1
0
0
0

マス (1,1) のみにコマが置かれ、このコマによって以下の # のマスが守られます。

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

入力例 2

5 10
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
5
1 1
2 3
3 4
4 2
5 5

出力例 2

1
6
12
8
25

入力例 3

8 5
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
6
7 2
8 1
4 5
8 8
3 4
1 7

出力例 3

5
3
9
14
5
3

Score : 600 points

Problem Statement

We have an N \times N grid. The square at the i-th row from the top and j-th column from the left in this grid is called (i,j).
Each square of the grid has at most one piece.
The state of the grid is given by N strings S_i:

  • if the j-th character of S_i is O, then (i,j) has a piece on it;
  • if the j-th character of S_i is X, then (i,j) has no piece on it.

You are given an integer M. Using this M, we define that a piece P placed at (s,t) covers a square (u,v) if all of the following conditions are satisfied:

  • s \le u \le N
  • t \le v \le N
  • (u - s) + \frac{(v - t)}{2} < M

For each of Q squares (X_i,Y_i), find how many pieces cover the square.

Constraints

  • N, M, X_i, Y_i, and Q are integers.
  • 1 \le N \le 2000
  • 1 \le M \le 2 \times N
  • S_i consists of O and X.
  • 1 \le Q \le 2 \times 10^5
  • 1 \le X_i,Y_i \le N

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N
Q
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

Output

Print Q lines.
The i-th line ( 1 \le i \le Q ) should contain the number of pieces that covers (X_i,Y_i) as an integer.


Sample Input 1

4 2
OXXX
XXXX
XXXX
XXXX
6
1 1
1 4
2 2
2 3
3 1
4 4

Sample Output 1

1
1
1
0
0
0

Only Square (1,1) contains a piece, which covers the following # squares:

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

Sample Input 2

5 10
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
5
1 1
2 3
3 4
4 2
5 5

Sample Output 2

1
6
12
8
25

Sample Input 3

8 5
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
6
7 2
8 1
4 5
8 8
3 4
1 7

Sample Output 3

5
3
9
14
5
3
Ex - Colorfulness

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号がついた N 個のボールがあります。ボール i には色 a_i がついています。

(1, 2, \dots, N) を並べ替えた列 P = (P_1, P_2, \dots, P_N) に対して C(P) を次のように定めます。

  • ボールを P_1, P_2, \dots, P_N という順番に一列に並べたときの、異なる色のボールが隣り合っている場所の数。

(1, 2, \dots, N) を並べ替えた列全体の集合を S_N と置きます。また、F(k) を次のように置きます。

\displaystyle F(k) = \left(\sum_{P \in S_N}C(P)^k \right) \bmod 998244353

F(1), F(2), \dots, F(M) を列挙してください。

制約

  • 2 \leq N \leq 2.5 \times 10^5
  • 1 \leq M \leq 2.5 \times 10^5
  • 1 \leq a_i \leq N
  • 入力される値はすべて整数

入力

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

N M
a_1 a_2 \dots a_N

出力

答えを以下の形式で出力せよ。

F(1) F(2) \dots F(M)

入力例 1

3 4
1 1 2

出力例 1

8 12 20 36

(P, C(P)) の組としてあり得るものを列挙すると次のようになります。

  • P=(1,2,3) のとき C(P) = 1
  • P=(1,3,2) のとき C(P) = 2
  • P=(2,1,3) のとき C(P) = 1
  • P=(2,3,1) のとき C(P) = 2
  • P=(3,1,2) のとき C(P) = 1
  • P=(3,2,1) のとき C(P) = 1

これらの値を F(k) に代入すれば答えを計算することができます。例えば F(1) = 1^1 + 2^1 + 1^1 + 2^1 + 1^1 + 1^1 = 8 です。


入力例 2

2 1
1 1

出力例 2

0

入力例 3

10 5
3 1 4 1 5 9 2 6 5 3

出力例 3

30481920 257886720 199419134 838462446 196874334

Score : 600 points

Problem Statement

There are N balls numbered 1 through N. Ball i is painted in Color a_i.

For a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N), let us define C(P) as follows:

  • The number of pairs of adjacent balls with different colors when Balls P_1, P_2, \dots, P_N are lined up in a row in this order.

Let S_N be the set of all permutations of (1, 2, \dots, N). Also, let us define F(k) by:

\displaystyle F(k) = \left(\sum_{P \in S_N}C(P)^k \right) \bmod 998244353.

Enumerate F(1), F(2), \dots, F(M).

Constraints

  • 2 \leq N \leq 2.5 \times 10^5
  • 1 \leq M \leq 2.5 \times 10^5
  • 1 \leq a_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 \dots a_N

Output

Print the answers in the following format:

F(1) F(2) \dots F(M)

Sample Input 1

3 4
1 1 2

Sample Output 1

8 12 20 36

Here is the list of all possible pairs of (P, C(P)).

  • If P=(1,2,3), then C(P) = 1.
  • If P=(1,3,2), then C(P) = 2.
  • If P=(2,1,3), then C(P) = 1.
  • If P=(2,3,1), then C(P) = 2.
  • If P=(3,1,2), then C(P) = 1.
  • If P=(3,2,1), then C(P) = 1.

We may obtain the answers by assigning these values into F(k). For instance, F(1) = 1^1 + 2^1 + 1^1 + 2^1 + 1^1 + 1^1 = 8.


Sample Input 2

2 1
1 1

Sample Output 2

0

Sample Input 3

10 5
3 1 4 1 5 9 2 6 5 3

Sample Output 3

30481920 257886720 199419134 838462446 196874334