A - Study Scheduling

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は H_1M_1 分ちょうどに起き、H_2M_2 分ちょうどに寝ます。 彼は、起きている時間のうち連続する K 分間に勉強をすることにしました。 勉強を開始することができる時間帯の長さは何分でしょうか。

制約

  • 0 \le H_1, H_2 \le 23
  • 0 \le M_1, M_2 \le 59
  • H_1M_1 分は H_2M_2 分より前の時刻である
  • K \ge 1
  • 高橋君が起きている時間の長さは K 分以上である
  • 入力は全て整数である

入力

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

H_1 M_1 H_2 M_2 K

出力

答えを整数として出力せよ。


入力例 1

10 0 15 0 30

出力例 1

270

高橋君は 10 時ちょうどに起き、15 時ちょうどに寝ます。勉強にかかる時間は 30 分なので、勉強は 10 時ちょうどから 1430 分ちょうどまでの 270 分間の時間帯の間に開始することができます。よって、270 を出力します。


入力例 2

10 0 12 0 120

出力例 2

0

高橋君は 10 時ちょうどに起き、12 時ちょうどに寝ます。勉強にかかる時間は 120 分なので、高橋君はちょうど 10 時に勉強を開始する必要があります。よって、0 を出力します。

Score : 100 points

Problem Statement

In this problem, we use the 24-hour clock.

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.) He has decided to study for K consecutive minutes while he is up. What is the length of the period in which he can start studying?

Constraints

  • 0 \le H_1, H_2 \le 23
  • 0 \le M_1, M_2 \le 59
  • The time H_1 : M_1 comes before the time H_2 : M_2.
  • K \ge 1
  • Takahashi is up for at least K minutes.
  • All values in input are integers (without leading zeros).

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

Print the length of the period in which he can start studying, as an integer.


Sample Input 1

10 0 15 0 30

Sample Output 1

270

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon. It takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.


Sample Input 2

10 0 12 0 120

Sample Output 2

0

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.

B - Postdocs

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

英大文字 P および D からなる文字列 S について、S が連続する部分文字列として含む D および PD の個数の和を S の「博士・PD 指数」と呼びます。例えば S = PPDDP のとき、S は連続する部分文字列として 2 個の D1 個の PD を含んでいるので、S の博士・PD 指数は 3 です。

P, D, ? からなる文字列 T があります。

T に含まれる ? をそれぞれ P または D のいずれかで置き換えてできる文字列の中で、博士・PD 指数が最大のものを 1 つ求めてください。

制約

  • 1 \leq |T| \leq 2 \times 10^5
  • TP, D, ? からなる。

入力

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

T

出力

T に含まれる ? をそれぞれ P または D で置き換えてできる文字列の中で、博士・PD 指数が最大のものを 1 つ出力せよ。 そのような文字列が複数ある場合、どれを出力しても構わない。


入力例 1

PD?D??P

出力例 1

PDPDPDP

この文字列は連続する部分文字列として 3 個の D3 個の PD を含みます。 よってこの文字列の博士・PD 指数は 6 です。 T に含まれる ? をそれぞれ P または D で置き換えてできる文字列の中で、これは最大の博士・PD 指数です。


入力例 2

P?P?

出力例 2

PDPD

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

  • 1 \leq |T| \leq 2 \times 10^5
  • T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D. If there are multiple such strings, you may print any of them.


Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.


Sample Input 2

P?P?

Sample Output 2

PDPD
C - Folia

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N + 1 の整数列 A_0, A_1, A_2, \ldots, A_N が与えられます。深さ N の二分木であって、d = 0, 1, \ldots, N に対して深さ d の葉の個数がちょうど A_d であるものは存在するでしょうか?存在する場合はそのような二分木の頂点数の最大値を、存在しない場合は -1 を出力してください。

注釈

  • 二分木とは、根付き木であって、それぞれの頂点の (直接の) 子の個数が 2 以下であるものを指す。
  • 根付き木の葉とは、子の個数が 0 である頂点を指す。
  • 根付き木の頂点 v の深さとは、根付き木の根から v までの距離を指す。(根の深さは 0 である。)
  • 根付き木の深さとは、根付き木の頂点の深さの最大値を指す。

制約

  • 0 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{8} (0 \leq i \leq N)
  • A_N \geq 1
  • 入力はすべて整数である

入力

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

N
A_0 A_1 A_2 \cdots A_N

出力

答えを整数で出力せよ。


入力例 1

3
0 1 1 2

出力例 1

7

以下の二分木が最善です。この二分木の頂点数は 7 であるため、7 を出力します。

0d8d99d13df036f23b0c9fcec52b842b.png


入力例 2

4
0 0 1 0 2

出力例 2

10

入力例 3

2
0 3 1

出力例 3

-1

入力例 4

1
1 1

出力例 4

-1

入力例 5

10
0 0 1 1 2 3 5 8 13 21 34

出力例 5

264

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

  • A binary tree is a rooted tree such that each vertex has at most two direct children.
  • A leaf in a binary tree is a vertex with zero children.
  • The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)
  • The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

  • 0 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{8} (0 \leq i \leq N)
  • A_N \geq 1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.


Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

0d8d99d13df036f23b0c9fcec52b842b.png


Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264
D - Urban Planning

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

1, 2, \cdots, N の番号がついた N 個の町があります。

現在、2 つの相異なる町を双方向に結ぶ道をいくつか作ることが計画されています。現時点では、町を結ぶ道はありません。

この計画において、各町は他の町を 1 つ選んで、道を 1 本以上使ってその町に移動できるように要請します。

N 個の町の要請は配列 P_1, P_2, \cdots, P_N で表され、町 i の要請は、P_i = -1 のときまだ決定されていないこと、1 \leq P_i \leq N であるとき町 P_i を選んだことを表します。

P_i = -1 である町の個数を K 個としたとき、全体では (N-1)^K 通りの要請方法が考えられます。それぞれの要請方法について、すべての町の要請を満たすために作る必要がある道の本数の最小値を求め、その総和を 10^9+7 で割った余りを出力してください。

制約

  • 2 \leq N \leq 5000
  • P_i = -1 または 1 \leq P_i \leq N
  • P_i \neq i
  • 入力は全て整数である

入力

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

N
P_1 P_2 \cdots P_N

出力

それぞれの要請方法について、すべての町の要請を満たすために作る必要がある道の本数の最小値を求め、その総和を 10^9+7 で割った余りを出力せよ。


入力例 1

4
2 1 -1 3

出力例 1

8

要請方法としては次の 3 通りがあります。

  • P_1 = 2, P_2 = 1, P_3 = 1, P_4 = 3 とする。このとき、例えば道 (1,2),(1,3),(3,4)3 つを作ることですべての町の要請を満たすことができ、これが最小です。
  • P_1 = 2, P_2 = 1, P_3 = 2, P_4 = 3 とする。このとき、例えば道 (1,2),(1,3),(3,4)3 つを作ることですべての町の要請を満たすことができ、これが最小です。
  • P_1 = 2, P_2 = 1, P_3 = 4, P_4 = 3 とする。このとき、例えば道 (1,2),(3,4)2 つを作ることですべての町の要請を満たすことができ、これが最小です。

必ずしも町 i と町 P_i が直接繋がっている必要がないことに注意してください。

よって、総和は 8 です。


入力例 2

2
2 1

出力例 2

1

初めから要請が 1 通りに決まっている場合もあります。


入力例 3

10
2 6 9 -1 6 9 -1 -1 -1 -1

出力例 3

527841

Score : 700 points

Problem Statement

There are N towns numbered 1, 2, \cdots, N.

Some roads are planned to be built so that each of them connects two distinct towns bidirectionally. Currently, there are no roads connecting towns.

In the planning of construction, each town chooses one town different from itself and requests the following: roads are built so that the chosen town is reachable from itself using one or more roads.

These requests from the towns are represented by an array P_1, P_2, \cdots, P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq P_i \leq N, it means that Town i has chosen Town P_i.

Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in which the towns can make the requests. For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Constraints

  • 2 \leq N \leq 5000
  • P_i = -1 or 1 \leq P_i \leq N.
  • P_i \neq i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \cdots P_N

Output

For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).


Sample Input 1

4
2 1 -1 3

Sample Output 1

8

There are three ways to make requests, as follows:

  • Choose P_1 = 2, P_2 = 1, P_3 = 1, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) - are needed to meet the requests.
  • Choose P_1 = 2, P_2 = 1, P_3 = 2, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) - are needed to meet the requests.
  • Choose P_1 = 2, P_2 = 1, P_3 = 4, P_4 = 3. In this case, at least two roads - for example, (1,2),(3,4) - are needed to meet the requests.

Note that it is not mandatory to connect Town i and Town P_i directly.

The sum of the above numbers is 8.


Sample Input 2

2
2 1

Sample Output 2

1

There may be just one fixed way to make requests.


Sample Input 3

10
2 6 9 -1 6 9 -1 -1 -1 -1

Sample Output 3

527841
E - Binary Programming

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

高橋くんは、空文字列 S と、0 で初期化された変数 x を持っています。

また 0 および 1 のみからなる文字列 T があります。

高橋くんはこれから、以下の 2 ステップからなる操作を |T| 回繰り返します。

  • S の好きな位置に 0 または 1 を挿入する。
  • 次に、S の左から奇数番目に書かれた数字の総和を x に加算する。例えば現在 S01101 であるなら、S の左から奇数番目に書かれた数字は左から 0, 1, 1 なので、2x に加算する。

最終的に ST に一致するような操作列における、最終的な x の値の最大値を出力してください。

制約

  • 1 \leq |T| \leq 2 \times 10^5
  • T0 および 1 のみからなる。

入力

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

T

出力

最終的に ST に一致するような操作列における、最終的な x の値の最大値を出力せよ。


入力例 1

1101

出力例 1

5

例えば以下のような操作列が、最終的な x の値を 5 に最大化します。

  • S の先頭に 1 を挿入する。S1 になり、x1 を加算する。
  • S1 文字目の直後に 0 を挿入する。S10 になり、x1 を加算する。
  • S2 文字目の直後に 1 を挿入する。S101 になり、x2 を加算する。
  • S の先頭に 1 を挿入する。S1101 になり、x1 を加算する。

入力例 2

0111101101

出力例 2

26

Score : 900 points

Problem Statement

Takahashi has an empty string S and a variable x whose initial value is 0.

Also, we have a string T consisting of 0 and 1.

Now, Takahashi will do the operation with the following two steps |T| times.

  • Insert a 0 or a 1 at any position of S of his choice.
  • Then, increment x by the sum of the digits in the odd positions (first, third, fifth, ...) of S. For example, if S is 01101, the digits in the odd positions are 0, 1, 1 from left to right, so x is incremented by 2.

Print the maximum possible final value of x in a sequence of operations such that S equals T in the end.

Constraints

  • 1 \leq |T| \leq 2 \times 10^5
  • T consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

T

Output

Print the maximum possible final value of x in a sequence of operations such that S equals T in the end.


Sample Input 1

1101

Sample Output 1

5

Below is one sequence of operations that maximizes the final value of x to 5.

  • Insert a 1 at the beginning of S. S becomes 1, and x is incremented by 1.
  • Insert a 0 to the immediate right of the first character of S. S becomes 10, and x is incremented by 1.
  • Insert a 1 to the immediate right of the second character of S. S becomes 101, and x is incremented by 2.
  • Insert a 1 at the beginning of S. S becomes 1101, and x is incremented by 1.

Sample Input 2

0111101101

Sample Output 2

26
F - Sorting Game

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

高橋くんとすぬけくんは、数列を使った次のようなゲームを思いつきました。

  • 0 以上 2^N 未満の整数からなる、長さ M の数列 a = a_1, a_2, \ldots, a_M を用意する。

  • まずすぬけくんは、以下の操作を好きな回数行う。

    • ある正整数 d を選び、全ての i~(1 \leq i \leq M) について、a_i2 進数で表したときの d 桁目(最下位桁は 1 桁目とする)を 0 にする。
  • すぬけくんの操作が全て終わったあと高橋くんは、以下の操作を好きな回数行い a を昇順に並べ替えることを目指す。ここで a が昇順であるとは、任意の i ~ (1 \leq i \leq M - 1) について a_i \leq a_{i + 1} であることを言う。

    • a の隣接する 2 要素 a_i, a_{i + 1} を選び、a_i, a_{i + 1}2 進数で表したときちょうど 1 桁が異なる場合、a_i, a_{i + 1} をスワップする。

ゲームで使うことができる、0 以上 2^N 未満の整数からなる、長さ M の数列は 2^{NM} 個存在します。

このうちゲームで使ったとき、すぬけくんがどのように操作を行ったとしても、高橋くんが適当な操作を行うことで昇順に並べ替えることができるものは何個あるでしょうか。 10^9 + 7 で割った余りを求めてください。

制約

  • 入力は全て整数である。
  • 1 \leq N \leq 5000
  • 2 \leq M \leq 5000

入力

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

N M

出力

ゲームで使ったとき、すぬけくんがどのように操作を行ったとしても、高橋くんが適当な操作を行うことで昇順に並べ替えることができる数列の個数を 10^9 + 7 で割った余りを出力せよ。


入力例 1

2 5

出力例 1

352

例えば a = 1, 3, 1, 3, 1 の場合を考えます。このとき、

  • a の各要素の 1 桁目を 0 にすると a = 0, 2, 0, 2, 0 に、
  • a の各要素の 2 桁目を 0 にすると a = 1, 1, 1, 1, 1 に、
  • a の各要素の 1, 2 桁目を 0 にすると a = 0, 0, 0, 0, 0 に、

なります。 すぬけくんが操作を行わず a が変わらない場合も含めて、いずれの場合も、2 進数でちょうど 1 桁が異なる隣接要素のスワップを繰り返して、昇順に並び替えることができます。 よってこの数列は、ゲームで使ったとき、すぬけくんがどのように操作を行っても高橋くんが適当な操作を行うことで昇順に並べ替えることができる数列の 1 つです。


入力例 2

2020 530

出力例 2

823277409

Score : 1000 points

Problem Statement

Takahashi and Snuke came up with a game that uses a number sequence, as follows:

  • Prepare a sequence of length M consisting of integers between 0 and 2^N-1 (inclusive): a = a_1, a_2, \ldots, a_M.

  • Snuke first does the operation below as many times as he likes:

    • Choose a positive integer d, and for each i (1 \leq i \leq M), in binary, set the d-th least significant bit of a_i to 0. (Here the least significant bit is considered the 1-st least significant bit.)
  • After Snuke finishes doing operations, Takahashi tries to sort a in ascending order by doing the operation below some number of times. Here a is said to be in ascending order when a_i \leq a_{i + 1} for all i (1 \leq i \leq M - 1).

    • Choose two adjacent elements of a: a_i and a_{i + 1}. If, in binary, these numbers differ in exactly one bit, swap a_i and a_{i + 1}.

There are 2^{NM} different sequences of length M consisting of integers between 0 and 2^N-1 that can be used in the game.

How many among them have the following property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations? Find the count modulo (10^9 + 7).

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 5000
  • 2 \leq M \leq 5000

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number, modulo (10^9 + 7), of sequences with the property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations.


Sample Input 1

2 5

Sample Output 1

352

Consider the case a = 1, 3, 1, 3, 1 for example.

  • When the least significant bit of each element of a is set to 0, a = 0, 2, 0, 2, 0;
  • When the second least significant bit of each element of a is set to 0, a = 1, 1, 1, 1, 1;
  • When the least two significant bits of each element of a are set to 0, a = 0, 0, 0, 0, 0.

In all of the cases above and the case when Snuke does no operation to change a, we can sort the sequence by repeatedly swapping adjacent elements that differ in exactly one bit. Thus, this sequence has the property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations.


Sample Input 2

2020 530

Sample Output 2

823277409