A - Synthetic Fiber Ribbon

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は長さ A センチメートルの化繊のリボンを持っています。これを左端から B センチメートルずつ切っていき、長さ B センチメートルのリボンをできるだけ多く作ります。

切り終えたとき、 B センチメートルに足りない余りの部分が残らないなら Yes を、余りが残るなら No を出力してください。

制約

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 入力はすべて整数

入力

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

A B
  • 1 行目には、リボンの長さ A と切り分ける長さ B がスペース区切りで与えられる。

出力

余りが残らないなら Yes を、残るなら No を 1 行で出力せよ。


入力例 1

12 4

出力例 1

Yes

入力例 2

10 3

出力例 2

No

入力例 3

7 7

出力例 3

Yes

入力例 4

956 673

出力例 4

No

入力例 5

702 351

出力例 5

Yes

Score : 200 pts

Problem Statement

Takahashi has a synthetic fiber ribbon of length A centimeters. He cuts it into pieces of B centimeters each, starting from the left end, making as many ribbons of length B centimeters as possible.

When he finishes cutting, print Yes if no leftover part shorter than B centimeters remains, and No if a remainder remains.

Constraints

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • All inputs are integers.

Input

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

A B
  • The first line contains the length of the ribbon A and the length to cut B, separated by a space.

Output

Print Yes on a single line if no remainder remains, and No if a remainder remains.


Sample Input 1

12 4

Sample Output 1

Yes

Sample Input 2

10 3

Sample Output 2

No

Sample Input 3

7 7

Sample Output 3

Yes

Sample Input 4

956 673

Sample Output 4

No

Sample Input 5

702 351

Sample Output 5

Yes
B - The Last Radish

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君の畑は横一列に並んだ N 個のマスからなり、左から i 番目のマスの様子は文字列 S の i 文字目で表されます。 D はそのマスに大根が植えられていることを、 . は何も植えられていないことを表します。

高橋君は最も右にある大根を収穫します。最も右にある大根は左から何番目のマスにあるかを求めてください。大根が 1 つもない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 100
  • S は D と . のみからなる長さ N の文字列

入力

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

N
S
  • 1 行目には、マスの数 N が与えられる。
  • 2 行目には、畑の様子を表す文字列 S が与えられる。

出力

最も右にある大根が左から何番目のマスにあるかを 1 行で出力せよ。大根がない場合は -1 を出力せよ。


入力例 1

6
..D...

出力例 1

3

入力例 2

4
....

出力例 2

-1

入力例 3

3
D..

出力例 3

1

入力例 4

1
.

出力例 4

-1

入力例 5

1
D

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi's field consists of N cells arranged in a row, and the state of the i-th cell from the left is represented by the i-th character of the string S. D means that a radish is planted in that cell, and . means that nothing is planted there.

Takahashi harvests the rightmost radish. Find the position (counted from the left) of the cell containing the rightmost radish. If there is no radish at all, print -1.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting only of D and .

Input

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

N
S
  • The first line contains the number of cells N.
  • The second line contains the string S representing the state of the field.

Output

Print, on a single line, the position from the left of the cell containing the rightmost radish. If there is no radish, print -1.


Sample Input 1

6
..D...

Sample Output 1

3

Sample Input 2

4
....

Sample Output 2

-1

Sample Input 3

3
D..

Sample Output 3

1

Sample Input 4

1
.

Sample Output 4

-1

Sample Input 5

1
D

Sample Output 5

1
C - Alchemy Showdown

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君と青木君は錬金術師で、素材の合成についての勝負をしています。

作業台の上に N 個の素材が一列に並んでおり、i 番目の素材には純度を表す 0 以上 P - 1 以下の整数 A_i が記されています。ここで P は素数です。

二人は第 1 ターンから第 N - 1 ターンまで、合計 N - 1 回の操作を順に行います。奇数回目のターンは高橋君が、偶数回目のターンは青木君が操作します。各ターンでは、操作を行うプレイヤーは必ず以下の操作を 1 回行います。

  • 現在の列の中で隣り合っている 2 つの素材を 1 組選ぶ。選んだ 2 つの素材の純度をそれぞれ x, y とするとき、それら 2 つの素材を列から取り除き、代わりにそれらが連続して並んでいた位置に純度 (x \times y) \bmod P(すなわち x \times y を P で割った余り)の新しい素材 1 つを挿入する。

この操作により、他の素材の相対的な並び順は変化せず、列に含まれる素材の個数は 1 つ減ります。

N - 1 回の操作の後、素材はちょうど 1 つだけ残ります。最終的に残った素材の純度を V とします。

高橋君は V をできるだけ 小さく したいと考えており、青木君は V をできるだけ 大きく したいと考えています。

両者が最適に行動したとき、最終的に残る素材の純度を求めてください。

なお、N = 1 の場合は操作を行う必要がなく、最初から残っている唯一の素材の純度がそのまま答えとなります。

制約

  • 1 \leq N \leq 10^6
  • 2 \leq P \leq 10^9 + 7
  • P は素数である
  • 0 \leq A_i \leq P - 1
  • 入力はすべて整数である

入力

N P
A_1 A_2 \ldots A_N
  • 1 行目には、素材の個数を表す整数 N と、素数 P が、スペース区切りで与えられる。
  • 2 行目には、各素材の純度を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。各 A_i は 0 以上 P - 1 以下である。

出力

両者が最適に行動したときの、最終的に残る素材の純度(0 以上 P - 1 以下の整数)を 1 行で出力せよ。


入力例 1

3 7
2 3 4

出力例 1

3

入力例 2

4 5
1 0 3 4

出力例 2

0

入力例 3

12 101
17 42 0 8 99 23 56 74 31 65 12 90

出力例 3

0

入力例 4

30 1000000007
123456789 987654321 314159265 271828182 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393

出力例 4

0

入力例 5

1 2
0

出力例 5

0

Score : 333 pts

Problem Statement

Takahashi and Aoki are alchemists competing in a game of synthesizing materials.

There are N materials lined up in a row on a workbench. The i-th material has an integer A_i (0 \leq A_i \leq P - 1) written on it, representing its purity. Here, P is a prime number.

The two players perform a total of N - 1 operations in order, from Turn 1 to Turn N - 1. Takahashi takes the odd-numbered turns, and Aoki takes the even-numbered turns. On each turn, the player taking the turn must perform the following operation exactly once:

  • Choose a pair of adjacent materials in the current row. Let x and y be the purities of the chosen materials. Remove these two materials from the row, and insert a single new material with purity (x \times y) \bmod P (the remainder when x \times y is divided by P) at the position where they were consecutively located.

This operation does not change the relative order of the other materials, and the number of materials in the row decreases by 1.

After N - 1 operations, exactly one material will remain. Let V be the purity of the final remaining material.

Takahashi wants to make V as small as possible, while Aoki wants to make V as large as possible.

Find the purity of the final remaining material when both players play optimally.

Note that if N = 1, no operations are performed, and the purity of the only material initially present is the answer.

Constraints

  • 1 \leq N \leq 10^6
  • 2 \leq P \leq 10^9 + 7
  • P is a prime number.
  • 0 \leq A_i \leq P - 1
  • All input values are integers.

Input

N P
A_1 A_2 \ldots A_N
  • The first line contains the integer N, representing the number of materials, and the prime number P, separated by a space.
  • The second line contains the integers A_1, A_2, \ldots, A_N, representing the purity of each material, separated by spaces. Each A_i satisfies 0 \leq A_i \leq P - 1.

Output

Print the purity of the final remaining material (an integer between 0 and P - 1, inclusive) when both players play optimally in a single line.


Sample Input 1

3 7
2 3 4

Sample Output 1

3

Sample Input 2

4 5
1 0 3 4

Sample Output 2

0

Sample Input 3

12 101
17 42 0 8 99 23 56 74 31 65 12 90

Sample Output 3

0

Sample Input 4

30 1000000007
123456789 987654321 314159265 271828182 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393

Sample Output 4

0

Sample Input 5

1 2
0

Sample Output 5

0
D - Intensive Summer Vacation Training

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は N 日間の夏休みに向けて、勉強の計画を立てています。

夏休みの i 日目 (1 \le i \le N) に勉強を行うと、学力が A_i 上がります。

高橋君は、夏休みの中から連続する 1 日以上 K 日以下の期間を選んで「集中特訓」を行うことにしました。

集中特訓の開始日を l 日目、終了日を r 日目とするとき、集中特訓によって得られる学力の合計は \sum_{i=l}^r A_i となります。

高橋君は、集中特訓で得られる学力の合計を X 以上にしたいと考えています。

以下の条件をすべて満たす整数の組 (l, r) の個数を求めてください。

  • 1 \le l \le r \le N
  • r - l + 1 \le K
  • \sum_{i=l}^r A_i \ge X

制約

  • 1 \le K \le N \le 10^6
  • 1 \le X \le 10^{15}
  • 1 \le A_i \le 10^9 (1 \le i \le N)
  • 入力はすべて整数

入力

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

N K X
A_1 A_2 \dots A_N

出力

条件を満たす整数の組 (l, r) の個数を 1 行に出力せよ。


入力例 1

4 3 6
3 1 4 2

出力例 1

3

入力例 2

5 2 15
3 5 2 7 4

出力例 2

0

入力例 3

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

出力例 3

11

入力例 4

25 8 5000000000
720194821 391029481 849201948 194820194 629104820 950182740 410293847 839201847 501928374 291048291 938471920 648291038 184920384 759302841 829401823 481920384 392018472 892019482 610293847 520194829 739102847 419203847 891028374 293847192 684920183

出力例 4

4

入力例 5

1 1 1
1

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is making a study plan for his N-day summer vacation.

Studying on the i-th day (1 \le i \le N) of the summer vacation increases his academic ability by A_i.

Takahashi has decided to conduct an "intensive training" during a contiguous period of at least 1 and at most K days from the summer vacation.

If the intensive training starts on day l and ends on day r, the total increase in academic ability gained from the intensive training will be \sum_{i=l}^r A_i.

Takahashi wants the total increase in academic ability gained from the intensive training to be at least X.

Find the number of pairs of integers (l, r) that satisfy all of the following conditions:

  • 1 \le l \le r \le N
  • r - l + 1 \le K
  • \sum_{i=l}^r A_i \ge X

Constraints

  • 1 \le K \le N \le 10^6
  • 1 \le X \le 10^{15}
  • 1 \le A_i \le 10^9 (1 \le i \le N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K X
A_1 A_2 \dots A_N

Output

Print the number of pairs of integers (l, r) that satisfy the conditions in a single line.


Sample Input 1

4 3 6
3 1 4 2

Sample Output 1

3

Sample Input 2

5 2 15
3 5 2 7 4

Sample Output 2

0

Sample Input 3

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

Sample Output 3

11

Sample Input 4

25 8 5000000000
720194821 391029481 849201948 194820194 629104820 950182740 410293847 839201847 501928374 291048291 938471920 648291038 184920384 759302841 829401823 481920384 392018472 892019482 610293847 520194829 739102847 419203847 891028374 293847192 684920183

Sample Output 4

4

Sample Input 5

1 1 1
1

Sample Output 5

1
E - Placement of Street Lights

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君の住む町には、一本の長い直線道路があります。道路沿いには N 個の区画が一列に並んでおり、左から順に区画 1, 2, \ldots, N と番号が振られています。

各区画 i には、夜間の危険度を表す値 A_i が定められています。これは、その区画の交通量や治安状況などから算出されたものです。

町の予算では、ちょうど K 台の街灯を設置することができます。街灯は N 個の区画のうち任意の K 個に 1 台ずつ設置できます(同じ区画に複数台は設置できません)。

街灯が設置された区画の最終的な危険度は 0 になります。街灯が設置されていない区画 j の最終的な危険度は、以下のように決まります:

  • 区画 j から最も近い街灯が設置された区画までの距離を d_j とする(距離は区画番号の差の絶対値)。
  • 区画 j の最終的な危険度は A_j \times d_j である。

すなわち、街灯から離れるほど照明の効果が弱まり、元の危険度に距離を掛けた値がその区画に残る危険度となります。

高橋君は、全区画の最終的な危険度の最大値をできるだけ小さくしたいと考えています。

K 台の街灯を最適に配置したとき、全区画の最終的な危険度の最大値として達成可能な最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • 0 \leq A_i \leq 10^9
  • 入力はすべて整数である。

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、区画の数を表す N と、街灯の台数を表す K が、スペース区切りで与えられる。
  • 2 行目には、各区画の危険度を表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

全区画の最終的な危険度の最大値として達成可能な最小値を 1 行で出力せよ。


入力例 1

5 2
3 1 4 1 5

出力例 1

4

入力例 2

4 1
2 0 3 1

出力例 2

3

入力例 3

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

出力例 3

9

入力例 4

30 7
12 5 20 0 7 15 3 25 8 1 30 6 14 2 18 10 4 22 9 13 0 16 11 27 5 19 2 24 7 21

出力例 4

36

入力例 5

1 1
1000000000

出力例 5

0

Score : 433 pts

Problem Statement

In the town where Takahashi lives, there is a single long straight road. Along the road, there are N sections arranged in a line, numbered 1, 2, \ldots, N from left to right.

For each section i, a value A_i representing the level of danger at night is determined. This is calculated based on factors such as traffic volume and public safety in that section.

The town's budget allows for exactly K streetlights to be installed. A streetlight can be installed in any K distinct sections out of the N sections (at most one streetlight per section).

The final danger level of a section where a streetlight is installed becomes 0. The final danger level of a section j where no streetlight is installed is determined as follows:

  • Let d_j be the distance from section j to the nearest section with a streetlight (the distance is the absolute difference between their section numbers).
  • The final danger level of section j is A_j \times d_j.

In other words, the further a section is from a streetlight, the weaker the lighting effect becomes, and the remaining danger level of that section is its original danger level multiplied by the distance.

Takahashi wants to make the maximum value of the final danger levels among all sections as small as possible.

Find the minimum possible maximum value of the final danger levels among all sections when K streetlights are optimally placed.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • 0 \leq A_i \leq 10^9
  • All input values are integers.

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains N, the number of sections, and K, the number of streetlights, separated by a space.
  • The second line contains A_1, A_2, \ldots, A_N, representing the danger level of each section, separated by spaces.

Output

Print the minimum possible maximum value of the final danger levels among all sections in a single line.


Sample Input 1

5 2
3 1 4 1 5

Sample Output 1

4

Sample Input 2

4 1
2 0 3 1

Sample Output 2

3

Sample Input 3

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

Sample Output 3

9

Sample Input 4

30 7
12 5 20 0 7 15 3 25 8 1 30 6 14 2 18 10 4 22 9 13 0 16 11 27 5 19 2 24 7 21

Sample Output 4

36

Sample Input 5

1 1
1000000000

Sample Output 5

0