A - Thumbnail

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 200

問題文

ドワンゴ社員のニワンゴくんは、投稿された動画からサムネイルを生成する機能を実装することになりました。
動画からサムネイルとするフレームを選択する手順は以下の通りです。

  • 動画の総フレーム数 N と、動画の各フレームを1つの整数で表現した長さ N の整数列 a を入力として受け取る
  • 各フレームには、先頭から順番に 0, 1, ..., N-1 のフレーム番号が付いており、フレーム番号 i を表現する整数は a_i である
  • a の平均値に最も近いフレームをサムネイルとする
  • そのようなフレームが複数ある場合、それらの中で最もフレーム番号の小さいものをサムネイルとする

この手順で得られる、サムネイルとして選択されるフレーム番号を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 100
  • 入力として与えられる数値はすべて整数である

入力

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

N
a_{0} a_{1} ... a_{N-1}

出力

答えを出力せよ。


入力例1

3
1 2 3

出力例1

1

a の平均値は 2 ですので、最も近いフレーム番号 1 が選択されます。


入力例2

4
2 5 2 5

出力例2

0

a の平均値は 3.5 ですが、平均値との距離は全フレーム同じです。よって、それらの中で最もフレーム番号の小さい 0 が選択されます。

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.
One day, he is asked to generate a thumbnail from a video a user submitted.
To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

  • Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.
  • Select t-th frame whose representation a_t is nearest to the average of all frame representations.
  • If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 100
  • All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.


Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.


Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.
In this case, every frame has the same distance from its representation to the average.
Therefore, Niwango-kun should select index 0, the smallest index among them.

B - Sum AND Subarrays

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 400

問題

ある日、ドワンゴ社員のニワンゴくんは、長さ N の整数列 (a_1, ..., a_N) を見つけました。ニワンゴくんは、数列 a の性質に興味を持っています。

数列 a の空でない連続する部分列 a_l, ..., a_r (1 \leq l \leq r \leq N)美しさ は、 a_l + ... + a_r と定義されます。ニワンゴくんは、ありうる N(N+1)/2 個の空でない連続する部分列のうち、 K 個を選んで取ってきて、それらの美しさのビット毎の論理積 (AND) を計算したとき、その最大値がどうなるかを知りたがっています (取ってくる部分列の間で重複する要素があっても構いません)。

彼の代わりに最大値を求めてください。

制約

  • 2 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9
  • 1 \leq K \leq N(N+1)/2
  • 入力として与えられる数値はすべて整数である

入力

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

N K
a_1 a_2 ... a_N

出力

答えを出力せよ。


入力例 1

4 2
2 5 2 5

出力例 1

12

異なる空でない連続する部分列は 10 個存在します。全列挙すると、

  • 1 番目から始まるもの: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}
  • 2 番目から始まるもの: \{5\}, \{5, 2\}, \{5, 2, 5\}
  • 3 番目から始まるもの: \{2\}, \{2, 5\}
  • 4 番目から始まるもの: \{5\}

です (数列の要素が同じでも、異なる添字から始まる列は異なるものとみなすことに注意してください)。

このうち異なる 2 個の部分列の美しさのビット毎の論理積 (AND) として得られる値の最大値は 12 です。 これは \{5, 2, 5\} (美しさ 12) と \{2, 5, 2, 5\} (美しさ 14) を選んだ時に達成できます。


入力例 2

8 4
9 1 8 2 7 5 6 4

出力例 2

32

Score : 400 points

Problem Statement

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N. He is interested in properties of the sequence a.

For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

Find the maximum possible value for him.

Constraints

  • 2 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9
  • 1 \leq K \leq N(N+1)/2
  • All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the answer.


Sample Input 1

4 2
2 5 2 5

Sample Output 1

12

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

  • contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}
  • contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}
  • contiguous subsequences starting from the third element: \{2\}, \{2, 5\}
  • contiguous subsequences starting from the fourth element: \{5\}

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12. This can be achieved by choosing \{5, 2, 5\} (with beauty 12) and \{2, 5, 2, 5\} (with beauty 14).


Sample Input 2

8 4
9 1 8 2 7 5 6 4

Sample Output 2

32
C - k-DMC

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 600

問題文

ドワンゴのコンテンツ配信基盤システム Dwango Media Cluster は、略して DMC と呼ばれています。
この名前をかっこ良いと感じたニワンゴくんは、文字列の DMC らしさを数値として定義することにしました。
具体的には、長さ N のある文字列 S と3以上の整数 k が与えられた時、以下を満たす整数の3つ組 (a,b,c) の個数を Sk-DMC 数と呼ぶことにします。

  • 0 \leq a < b < c \leq N - 1
  • S[a] = D
  • S[b] = M
  • S[c] = C
  • c-a < k

ここで、S[a]Sa 番目の文字を表します。先頭の文字は 0 文字目として扱います (つまり、0 \leq a \leq N - 1 です)。

ある文字列 SQ 個の整数 k_0, k_1, ..., k_{Q-1} に対して、k_i-DMC 数をそれぞれ計算してください。

制約

  • 3 \leq N \leq 10^6
  • SA - Z からなる文字列
  • 1 \leq Q \leq 75
  • 3 \leq k_i \leq N
  • 入力として与えられる数値はすべて整数である

入力

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

N
S
Q
k_{0} k_{1} ... k_{Q-1}

出力

Q 行出力せよ。
i 行目 (0 \leq i \leq Q-1) には、文字列 Sk_i-DMC 数を出力せよ。


入力例1

18
DWANGOMEDIACLUSTER
1
18

出力例1

1

(a,b,c) = (0, 6, 11) が条件を満たします。
Dwango Media Cluster は、ニワンゴくんの定義では意外と DMC らしくないようです。


入力例2

18
DDDDDDMMMMMCCCCCCC
1
18

出力例2

210

6\times 5\times 7 個の組み合わせがありえます。


入力例3

54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40

出力例3

0
1
2

c-a < k_i 以外の条件は (a, b, c) = (0, 23, 36), (8, 23, 36) が満たします。
ちなみに、DWANGO は「Dial-up Wide Area Network Gaming Operation」の頭文字です。


入力例4

30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20

出力例4

10
52
110
140

Score : 600 points

Problem Statement

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.
The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions:

  • 0 \leq a < b < c \leq N - 1
  • S[a] = D
  • S[b] = M
  • S[c] = C
  • c-a < k

Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds.

For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1).

Constraints

  • 3 \leq N \leq 10^6
  • S consists of uppercase English letters
  • 1 \leq Q \leq 75
  • 3 \leq k_i \leq N
  • All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
S
Q
k_{0} k_{1} ... k_{Q-1}

Output

Print Q lines. The i-th line should contain the k_i-DMC number of the string S.


Sample Input 1

18
DWANGOMEDIACLUSTER
1
18

Sample Output 1

1

(a,b,c) = (0, 6, 11) satisfies the conditions.
Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition.


Sample Input 2

18
DDDDDDMMMMMCCCCCCC
1
18

Sample Output 2

210

The number of triples can be calculated as 6\times 5\times 7.


Sample Input 3

54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40

Sample Output 3

0
1
2

(a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i.
By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".


Sample Output 4

30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20

Sample Output 4

10
52
110
140
D - Square Rotation

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 800

問題

ドワンゴ社員のニワンゴくんはテレビちゃんが大好きなので、テレビちゃんのぬいぐるみを大量に集めて床に一面に並べていました。

ニワンゴくんはレアな黒いテレビちゃんのぬいぐるみを N 個持っていて、普通のテレビちゃんのぬいぐるみと一緒に並べていました。しかし、あちこちに置いておくと管理が難しいので近くにまとめることにしました。

無限に広い二次元平面上のすべての格子点上にぬいぐるみが置いてあります。N 個の黒いぬいぐるみの座標 (x_i,y_i) が与えられます。ぬいぐるみは点として扱います。

ニワンゴくんは以下の操作を任意の回数行うことができます。

  • 辺が軸に平行な一辺の長さが D の正方形を、各頂点が格子点と重なるように任意の座標に置き、正方形の角の 4 点を、そこにある 4 個のぬいぐるみと一緒に 90 度回転させる。つまり、正方形の左下の頂点を (x,y) とした場合、(x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) の順に、4 点を同時に回転させる

配置の乱雑さを、すべての黒いぬいぐるみを囲うのに必要な辺が軸に平行な正方形の一辺の長さの最小値とします。 ここで、正方形の辺上にあるぬいぐるみも正方形に囲われているものとします。

ニワンゴくんが何度か操作を行ったあとの乱雑さとしてありうる値のうち、最小値を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq D \leq 1000
  • 0 \leq x_i, y_i \leq 10^9
  • 与えられる座標はすべて異なる
  • 入力として与えられる数値はすべて整数である

部分点

  • 1 \leq D \leq 30 を満たすデータセットに正解すると、500 点が与えられる

入力

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

N D
x_1 y_1
:
x_N y_N

出力

答えを出力せよ。


入力例 1

3 1
0 0
1 0
2 0

出力例 1

1

入力例 2

19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0

出力例 2

4

入力例 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

出力例 3

4

Score : 800 points

Problem Statement

Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.

Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.

In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).

He may perform the following operation arbitrarily many times:

  • Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.

Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.

Find the minimum scatteredness after he performs arbitrarily many operations.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq D \leq 1000
  • 0 \leq x_i, y_i \leq 10^9
  • Given coordinates are pairwise distinct
  • All numbers given in input are integers

Partial Scores

  • 500 points will be awarded for passing the test set satisfying 1 \leq D \leq 30.

Input

Input is given from Standard Input in the following format:

N D
x_1 y_1
:
x_N y_N

Output

Print the answer.


Sample Input 1

3 1
0 0
1 0
2 0

Sample Output 1

1

Sample Input 2

19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0

Sample Output 2

4

Sample Input 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

Sample Output 3

4
E - Cyclic GCDs

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

ニワンゴくんは \(N\) 羽のニワトリを飼っています。それぞれのニワトリは \(1\) から \(N\) の番号で識別され、\(i\) 番目のニワトリの大きさは正の整数 \(a_i\) です。

\(N\) 羽のニワトリは手をつなぎ合っていくつかの輪を作ることにしました。 輪の作り方は \(1, \ldots ,N\) を並び替えた順列 \(p\) を用いて表され、ニワトリ \(i\) が右手 (ないし右翼) でニワトリ \(p_i\) の左手を取ることを表します。ニワトリは自分自身の手を取ることもあります。

ニワトリ \(i\) を含む を、ニワトリ \(p_i, p_{p_i}, \ldots, p_{\ddots_i} = i\) からなる集合とします。 こうして \(N\) 羽全てのニワトリが手を取ったとき、\(N\) 羽のニワトリたちは何個かの輪に分割できることが証明できます。

輪の作り方の 美しさ \(f(p)\) はそれぞれの輪に含まれる最も小さいニワトリの大きさの積で定義されます。

\(1 \leq i \leq N\) を満たす \(i\) について、上の方法で輪が \(i\) 個できるような順列 \(p\) について \(f(p)\) を足し合わせたものを \(b_i\) とします。

\(b_1, \ldots, b_N\) の最大公約数を、modulo \(998244353\) で求めてください。

制約

  • \(1 \leq N \leq 10^5\)
  • \(1 \leq a_i \leq 10^9\)
  • 入力で与えられる数値は全て整数である

入力

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

\(N\)
\(a_1\) \(a_2\) \(\ldots\) \(a_N\)

出力

答えを出力せよ。


入力例1

2
4 3

出力例1

3

\(N = 2, a = [ 4, 3 ]\) である。

順列 \(p = [ 1, 2 ]\) について、ニワトリ \(1\) のみからなる輪とニワトリ \(2\) のみからなる輪ができるので、\(f([1, 2]) = a_1 \times a_2 = 12\) である。

順列 \(p = [ 2, 1 ]\) について、ニワトリ \(1, 2\) からなる輪ができ、その輪の最も小さいニワトリの大きさは \(a_2 = 3\) なので、\(f([2, 1]) = a_2 = 3\) である。

以上から \(b_1 = f([2, 1]) = 3, b_2 = f([1, 2]) = 12\) なので、\(b_1\) と \(b_2\) の最大公約数は \(3\) である。


入力例2

4
2 5 2 5

出力例2

2

ニワトリは大きさが同じであっても互いに区別できるため、常に \(N!\) 通りの順列が存在する。

以下の図は \(p = (2, 1, 4, 3), p = (3, 4, 1, 2)\) の場合の輪の作り方および美しさである。

bdd8ce0a7db3b4f920b04551c60aa207.png

Score : 1000 points

Problem Statement

Niwango-kun has \(N\) chickens as his pets. The chickens are identified by numbers \(1\) to \(N\), and the size of the \(i\)-th chicken is a positive integer \(a_i\).

\(N\) chickens decided to take each other's hand (wing) and form some cycles. The way to make cycles is represented by a permutation \(p\) of \(1, \ldots , N\). Chicken \(i\) takes chicken \(p_i\)'s left hand by its right hand. Chickens may take their own hand.

Let us define the cycle containing chicken \(i\) as the set consisting of chickens \(p_i, p_{p_i}, \ldots, p_{\ddots_i} = i\). It can be proven that after each chicken takes some chicken's hand, the \(N\) chickens can be decomposed into cycles.

The beauty \(f(p)\) of a way of forming cycles is defined as the product of the size of the smallest chicken in each cycle. Let \(b_i \ (1 \leq i \leq N)\) be the sum of \(f(p)\) among all possible permutations \(p\) for which \(i\) cycles are formed in the procedure above.

Find the greatest common divisor of \(b_1, b_2, \ldots, b_N\) and print it \({\rm mod} \ 998244353\).

Constraints

  • \(1 \leq N \leq 10^5\)
  • \(1 \leq a_i \leq 10^9\)
  • All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

\(N\)
\(a_1\) \(a_2\) \(\ldots\) \(a_N\)

Output

Print the answer.


Sample Input 1

2
4 3

Sample Output 1

3

In this case, \(N = 2, a = [ 4, 3 ]\).

For the permutation \(p = [ 1, 2 ]\), a cycle of an only chicken \(1\) and a cycle of an only chicken \(2\) are made, thus \(f([1, 2]) = a_1 \times a_2 = 12\).

For the permutation \(p = [ 2, 1 ]\), a cycle of two chickens \(1\) and \(2\) is made, and the size of the smallest chicken is \(a_2 = 3\), thus \(f([2, 1]) = a_2 = 3\).

Now we know \(b_1 = f([2, 1]) = 3, b_2 = f([1, 2]) = 12\), and the greatest common divisor of \(b_1\) and \(b_2\) is \(3\).


Sample Input 2

4
2 5 2 5

Sample Output 2

2

There are always \(N!\) permutations because chickens of the same size can be distinguished from each other.

The following picture illustrates the cycles formed and their beauties when \(p = (2, 1, 4, 3)\) and \(p = (3, 4, 1, 2)\), respectively.

bdd8ce0a7db3b4f920b04551c60aa207.png