A - Plus Minus

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は 2 つの整数 X, Y を持っています。

X + YX - Y を計算した結果、AB になりました。

高橋君は計算後に XY を忘れてしまったので、XY を求めてあげてください。

制約

  • -100 \leq A, B \leq 100
  • 与えられる A, B に対して X + Y = A, X - Y = B を満たす整数 X, Y が一意に存在する
  • 入力は全て整数である

入力

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

A B

出力

XY を出力せよ。


入力例 1

2 -2

出力例 1

0 2

X = 0, Y = 2 とすると 0 + 2 = 2, 0 - 2 = -2 で辻褄が合います。


入力例 2

3 1

出力例 2

2 1

Score : 100 points

Problem Statement

Takahashi has two integers X and Y.

He computed X + Y and X - Y, and the results were A and B, respectively.

Now he cannot remember what X and Y were. Find X and Y for him.

Constraints

  • -100 \leq A, B \leq 100
  • For the given integers A and B, there uniquely exist integers X and Y such that X + Y = A and X - Y = B.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print X and Y.


Sample Input 1

2 -2

Sample Output 1

0 2

If X = 0 and Y = 2, they match the situation: 0 + 2 = 2 and 0 - 2 = -2.


Sample Input 2

3 1

Sample Output 2

2 1
B - DNA Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

A, T, C, G から成る長さ N の文字列 S があります。

長さの等しい文字列 T_1, T_2 が相補的とは、|T_1| = l としたとき、どの 1 \leq i \leq l についても T_1, T_2i 文字目の組み合わせが (AT), または (CG) の組み合わせのいずれかであることを指します。(例えば AT の組み合わせのとき、どちらの文字が T_1 に属してもよいです)

S の連続する空でない部分文字列 T であって、次の条件を満たすものの個数を求めてください。

  • T と相補的であるような、T の文字を並び替えた文字列が存在する。

ただし、文字列として同じであっても S 内の位置が異なれば違う部分列とみなします。

制約

  • 1 \leq N \leq 5000
  • SA, T, C, G のみから成る

入力

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

N S

出力

S の連続する空でない部分文字列 T であって、条件を満たすものの個数を出力せよ。


入力例 1

4 AGCT

出力例 1

2

次の 2 つの部分文字列が条件を満たします。

  • GC (2 文字目から 3 文字目) は、これを並び替えた CG と相補的です。
  • AGCT (1 文字目から 4 文字目) は、これを並び替えた TCGA と相補的です。

入力例 2

4 ATAT

出力例 2

4

次の 4 つの部分文字列が条件を満たします。

  • AT (1 文字目から 2 文字目) は、これを並び替えた TA と相補的です。
  • TA (2 文字目から 3 文字目) は、これを並び替えた AT と相補的です。
  • AT (3 文字目から 4 文字目) は、これを並び替えた TA と相補的です。
  • ATAT (1 文字目から 4 文字目) は、これを並び替えた TATA と相補的です。

入力例 3

10 AAATACCGCG

出力例 3

6

Score : 400 points

Problem Statement

We have a string S of length N consisting of A, T, C, and G.

Strings T_1 and T_2 of the same length are said to be complementary when, for every i (1 \leq i \leq l), the i-th character of T_1 and the i-th character of T_2 are complementary. Here, A and T are complementary to each other, and so are C and G.

Find the number of non-empty contiguous substrings T of S that satisfies the following condition:

  • There exists a string that is a permutation of T and is complementary to T.

Here, we distinguish strings that originate from different positions in S, even if the contents are the same.

Constraints

  • 1 \leq N \leq 5000
  • S consists of A, T, C, and G.

Input

Input is given from Standard Input in the following format:

N S

Output

Print the number of non-empty contiguous substrings T of S that satisfies the condition.


Sample Input 1

4 AGCT

Sample Output 1

2

The following two substrings satisfy the condition:

  • GC (the 2-nd through 3-rd characters) is complementary to CG, which is a permutation of GC.
  • AGCT (the 1-st through 4-th characters) is complementary to TCGA, which is a permutation of AGCT.

Sample Input 2

4 ATAT

Sample Output 2

4

The following four substrings satisfy the condition:

  • AT (the 1-st through 2-nd characters) is complementary to TA, which is a permutation of AT.
  • TA (the 2-st through 3-rd characters) is complementary to AT, which is a permutation of TA.
  • AT (the 3-rd through 4-th characters) is complementary to TA, which is a permutation of AT.
  • ATAT (the 1-st through 4-th characters) is complementary to TATA, which is a permutation of ATAT.

Sample Input 3

10 AAATACCGCG

Sample Output 3

6
C - Fair Elevator

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

下の階から順に 1, 2, \ldots, 2N の番号がついた 2N 階から成る建物があります。

この建物のエレベーターが 1 度だけ 1 階から 2N 階まで動きました。

この途中で、 N 人が乗り降りしました。人 i (1 \leq i \leq N) は、それぞれエレベーターに A_i 階で乗り、B_i 階で降りました。ただし、1 \leq A_i < B_i \leq 2N であり、それぞれの階で乗り降りした人はただ 1 人です。

また、この N 人は気難しいため、以下の条件が満たされていました。

  • i (1 \leq i \leq N) がエレベーターに乗っているとき、他の人が乗り降りした回数を C_i (= B_i - A_i - 1) で表すと、次の条件が成り立つ
    • i と人 j が同時にエレベーターに乗っていた瞬間が存在するならば、C_i = C_j である

A, B は記録されていましたが、残念なことに、記録の一部が消えてしまいました。A_i, B_i が消えている場合は -1 として与えられます。

また、残っている記録も誤っている可能性があります。

残っている記録に矛盾しないような A, B の組み合わせが存在するかどうか判定してください。

制約

  • 1 \leq N \leq 100
  • A_i = -1 または 1 \leq A_i \leq 2N
  • B_i = -1 または 1 \leq B_i \leq 2N
  • 入力は全て整数である

入力

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

N
A_1 B_1
A_2 B_2
:
A_N B_N

出力

残っている記録に矛盾しないような A, B の組み合わせが存在する場合は Yes を、しない場合は No を出力せよ。


入力例 1

3
1 -1
-1 4
-1 6

出力例 1

Yes

例えば、B_1 = 3, A_2 = 2, A_3 = 5 であった場合、全ての条件を満たします。

この場合、人 1, 2 が同時にエレベーターに乗っている瞬間がありますが、C_1 = C_2 = 1 であるので問題ありません。


入力例 2

2
1 4
2 3

出力例 2

No

1, 2 が同時にエレベーターに乗っている瞬間がありますが、C_1 = 2, C_2 = 0 なのでいずれかの情報が誤っています。


入力例 3

2
4 1
2 4

出力例 3

No

記録は全て残っているように見えますが、明らかに誤っています。

Score : 600 points

Problem Statement

There is a building with 2N floors, numbered 1, 2, \ldots, 2N from bottom to top.

The elevator in this building moved from Floor 1 to Floor 2N just once.

On the way, N persons got on and off the elevator. Each person i (1 \leq i \leq N) got on at Floor A_i and off at Floor B_i. Here, 1 \leq A_i < B_i \leq 2N, and just one person got on or off at each floor.

Additionally, because of their difficult personalities, the following condition was satisfied:

  • Let C_i (= B_i - A_i - 1) be the number of times, while Person i were on the elevator, other persons got on or off. Then, the following holds:
    • If there was a moment when both Person i and Person j were on the elevator, C_i = C_j.

We recorded the sequences A and B, but unfortunately, we have lost some of the records. If the record of A_i or B_i is lost, it will be given to you as -1.

Additionally, the remaining records may be incorrect.

Determine whether there is a pair of A and B that is consistent with the remaining records.

Constraints

  • 1 \leq N \leq 100
  • A_i = -1 or 1 \leq A_i \leq 2N.
  • B_i = -1 or 1 \leq B_i \leq 2N.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

If there is a pair of A and B that is consistent with the remaining records, print Yes; otherwise, print No.


Sample Input 1

3
1 -1
-1 4
-1 6

Sample Output 1

Yes

For example, if B_1 = 3, A_2 = 2, and A_3 = 5, all the requirements are met.

In this case, there is a moment when both Person 1 and Person 2 were on the elevator, which is fine since C_1 = C_2 = 1.


Sample Input 2

2
1 4
2 3

Sample Output 2

No

There is a moment when both Person 1 and Person 2 were on the elevator. Since C_1 = 2, C_2 = 0, some of the information is incorrect.


Sample Input 3

2
4 1
2 4

Sample Output 3

No

The records are seemingly intact but clearly are incorrect.

D - Multiset Mean

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 700

問題文

正の整数 N, K, M が与えられるので、1 以上 N 以下の全ての整数 x について、次の問題を解いてください。

  • 1, 2, 3 \cdots, N の各整数をそれぞれ 0 個以上 K 個以下含むような空でない多重集合であって、平均が x であるものの個数を M で割った余りを求めよ。

制約

  • 1 \leq N, K \leq 100
  • 10^8 \leq M \leq 10^9 + 9
  • M は素数である
  • 入力は全て整数である

入力

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

N K M

出力

以下の形式で出力せよ。

c_1
c_2
:
c_N

ただし c_x は、平均が x である多重集合の個数を M で割った余りを表す。


入力例 1

3 1 998244353

出力例 1

1
3
1

1 以上 3 以下の整数をそれぞれ 0 個以上 1 個以下含むような空でない多重集合を考えます。

  • 平均が x = 1 である多重集合は、\{1\}1 個です。
  • 平均が x = 2 である多重集合は、\{2\}, \{1, 3\}, \{1, 2, 3\}3 個です。
  • 平均が x = 3 である多重集合は、\{3\}1 個です。

入力例 2

1 2 1000000007

出力例 2

2

1 以上 1 以下の整数をそれぞれ 0 個以上 2 個以下含むような空でない多重集合を考えます。

  • 平均が x = 1 である多重集合は、\{1\}, \{1, 1\}2 個です。

入力例 3

10 8 861271909

出力例 3

8
602
81827
4054238
41331779
41331779
4054238
81827
602
8

Score : 700 points

Problem Statement

Given positive integers N, K and M, solve the following problem for every integer x between 1 and N (inclusive):

  • Find the number, modulo M, of non-empty multisets containing between 0 and K (inclusive) instances of each of the integers 1, 2, 3 \cdots, N such that the average of the elements is x.

Constraints

  • 1 \leq N, K \leq 100
  • 10^8 \leq M \leq 10^9 + 9
  • M is prime.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K M

Output

Use the following format:

c_1
c_2
:
c_N

Here, c_x should be the number, modulo M, of multisets such that the average of the elements is x.


Sample Input 1

3 1 998244353

Sample Output 1

1
3
1

Consider non-empty multisets containing between 0 and 1 instance(s) of each of the integers between 1 and 3. Among them, there are:

  • one multiset such that the average of the elements is k = 1: \{1\};
  • three multisets such that the average of the elements is k = 2: \{2\}, \{1, 3\}, \{1, 2, 3\};
  • one multiset such that the average of the elements is k = 3: \{3\}.

Sample Input 2

1 2 1000000007

Sample Output 2

2

Consider non-empty multisets containing between 0 and 2 instances of each of the integers between 1 and 1. Among them, there are:

  • two multisets such that the average of the elements is k = 1: \{1\}, \{1, 1\}.

Sample Input 3

10 8 861271909

Sample Output 3

8
602
81827
4054238
41331779
41331779
4054238
81827
602
8
E - Random LIS

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

長さ N の整数列 A_1, A_2, \cdots, A_N が与えられます。

同じく長さ N の整数列 X は、 各 i (1 \leq i \leq N) について独立に、 1 \leq X_i \leq A_i を満たす整数の一様分布からランダムに選ばれます。

このとき、X の最長増加部分列の長さの期待値を mod 1000000007 で計算してください。

より正確には、期待値が有理数、つまりある既約分数 \frac{P}{Q} で表せること、更に R \times Q \equiv P \pmod {1000000007}, 0 \leq R < 1000000007 を満たす整数 R が一意に定まることがこの問題の制約より証明できますので、この R を出力してください。

注釈

X の部分列とは X の要素をいくつか抜き出して元の順に並べてできる列のことを指し、また、列 X の最長増加部分列とは、X の狭義単調増加な部分列の中で列の長さが最大のものを指します。

制約

  • 1 \leq N \leq 6
  • 1 \leq A_i \leq 10^9
  • 入力は全て整数である

入力

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

N
A_1 A_2 \cdots A_N

出力

期待値を mod 1000000007 で出力せよ。


入力例 1

3
1 2 3

出力例 1

2

X はそれぞれ確率 1/6 で次のいずれかになります。

  • X = (1,1,1) のとき、最長増加部分列の長さは 1 です。
  • X = (1,1,2) のとき、最長増加部分列の長さは 2 です。
  • X = (1,1,3) のとき、最長増加部分列の長さは 2 です。
  • X = (1,2,1) のとき、最長増加部分列の長さは 2 です。
  • X = (1,2,2) のとき、最長増加部分列の長さは 2 です。
  • X = (1,2,3) のとき、最長増加部分列の長さは 3 です。

よって、最長増加部分列の長さの期待値は (1 + 2 + 2 + 2 + 2 + 3) / 6 \equiv 2 \pmod {1000000007} です。


入力例 2

3
2 1 2

出力例 2

500000005

X はそれぞれ確率 1/4 で次のいずれかになります。

  • X = (1,1,1) のとき、最長増加部分列の長さは 1 です。
  • X = (1,1,2) のとき、最長増加部分列の長さは 2 です。
  • X = (2,1,1) のとき、最長増加部分列の長さは 1 です。
  • X = (2,1,2) のとき、最長増加部分列の長さは 2 です。

よって、最長増加部分列の長さの期待値は (1 + 2 + 1 + 2) / 4 = 3 / 2 ですが、2 \times 500000005 \equiv 3 \pmod {1000000007} であるので、500000005 を出力してください。


入力例 3

6
8 6 5 10 9 14

出力例 3

959563502

Score : 700 points

Problem Statement

Given is an integer sequence of length N: A_1, A_2, \cdots, A_N.

An integer sequence X, which is also of length N, will be chosen randomly by independently choosing X_i from a uniform distribution on the integers 1, 2, \ldots, A_i for each i (1 \leq i \leq N).

Compute the expected value of the length of the longest increasing subsequence of this sequence X, modulo 1000000007.

More formally, under the constraints of the problem, we can prove that the expected value can be represented as a rational number, that is, an irreducible fraction \frac{P}{Q}, and there uniquely exists an integer R such that R \times Q \equiv P \pmod {1000000007} and 0 \leq R < 1000000007, so print this integer R.

Notes

A subsequence of a sequence X is a sequence obtained by extracting some of the elements of X and arrange them without changing the order. The longest increasing subsequence of a sequence X is the sequence of the greatest length among the strictly increasing subsequences of X.

Constraints

  • 1 \leq N \leq 6
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the expected value modulo 1000000007.


Sample Input 1

3
1 2 3

Sample Output 1

2

X becomes one of the following, with probability 1/6 for each:

  • X = (1,1,1), for which the length of the longest increasing subsequence is 1;
  • X = (1,1,2), for which the length of the longest increasing subsequence is 2;
  • X = (1,1,3), for which the length of the longest increasing subsequence is 2;
  • X = (1,2,1), for which the length of the longest increasing subsequence is 2;
  • X = (1,2,2), for which the length of the longest increasing subsequence is 2;
  • X = (1,2,3), for which the length of the longest increasing subsequence is 3.

Thus, the expected value of the length of the longest increasing subsequence is (1 + 2 + 2 + 2 + 2 + 3) / 6 \equiv 2 \pmod {1000000007}.


Sample Input 2

3
2 1 2

Sample Output 2

500000005

X becomes one of the following, with probability 1/4 for each:

  • X = (1,1,1), for which the length of the longest increasing subsequence is 1;
  • X = (1,1,2), for which the length of the longest increasing subsequence is 2;
  • X = (2,1,1), for which the length of the longest increasing subsequence is 1;
  • X = (2,1,2), for which the length of the longest increasing subsequence is 2.

Thus, the expected value of the length of the longest increasing subsequence is (1 + 2 + 1 + 2) / 4 = 3 / 2. Since 2 \times 500000005 \equiv 3 \pmod {1000000007}, we should print 500000005.


Sample Input 3

6
8 6 5 10 9 14

Sample Output 3

959563502
F - Visibility Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

一列に並んだ N 棟のビルが建設中であり、ビルには左から順番に 1, 2, \ldots, N の番号がついています。

長さ N の整数列 X が与えられ、ビル i の高さ H_i は、1 以上 X_i 以下の整数のいずれかにすることができます。

1 \leq i \leq N に対して、P_i を次のように定めます。

  • H_j > H_i を満たすような整数 j (1 \leq j < i) が存在する場合はそのような j の最大値、存在しない場合は -1 とする

全てのビルの高さの組み合わせを考えるとき、 P としてありうる整数列の個数を 1000000007 で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq X_i \leq 10^5
  • 入力は全て整数である

入力

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

N
X_1 X_2 \cdots X_N

出力

全てのビルの高さの組み合わせを考えるとき、 P としてありうる整数列の個数を 1000000007 で割った余りを出力せよ。


入力例 1

3
3 2 1

出力例 1

4

H としては、次の 6 通りが考えられます。

  • H = (1, 1, 1) のとき、P(-1, -1, -1) である
  • H = (1, 2, 1) のとき、P(-1, -1, 2) である
  • H = (2, 1, 1) のとき、P(-1, 1, 1) である
  • H = (2, 2, 1) のとき、P(-1, -1, 2) である
  • H = (3, 1, 1) のとき、P(-1, 1, 1) である
  • H = (3, 2, 1) のとき、P(-1, 1, 2) である

よって、P としてありうる整数列は (-1, -1, -1), (-1, -1, 2), (-1, 1, 1), (-1, 1, 2)4 個です。


入力例 2

3
1 1 2

出力例 2

1

H としては、次の 2 通りが考えられます。

  • H = (1, 1, 1) のとき、P(-1, -1, -1) である
  • H = (1, 1, 2) のとき、P(-1, -1, -1) である

よって、P としてありうる整数列は 1 個です。


入力例 3

5
2 2 2 2 2

出力例 3

16

入力例 4

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

出力例 4

31155

Score : 1000 points

Problem Statement

There are N buildings under construction arranged in a row, numbered 1, 2, \ldots, N from left to right.

Given is an integer sequence X of length N. The height of Building i, H_i, can be one of the integers between 1 and X_i (inclusive).

For each i such that 1 \leq i \leq N, let us define P_i as follows:

  • If there is an integer j (1 \leq j < i) such that H_j > H_i, P_i is the maximum such j. Otherwise, P_i is -1.

Considering all possible combinations of the buildings' heights, find the number of integer sequences that P can be, modulo 1000000007.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq X_i \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 \cdots X_N

Output

Print the number of integer sequences that P can be when all possible combinations of the buildings' heights are considered, modulo 1000000007.


Sample Input 1

3
3 2 1

Sample Output 1

4

We have six candidates for H, as follows:

  • H = (1, 1, 1), for which P is (-1, -1, -1);
  • H = (1, 2, 1), for which P is (-1, -1, 2);
  • H = (2, 1, 1), for which P is (-1, 1, 1);
  • H = (2, 2, 1), for which P is (-1, -1, 2);
  • H = (3, 1, 1), for which P is (-1, 1, 1);
  • H = (3, 2, 1), for which P is (-1, 1, 2).

Thus, there are four integer sequences that P can be: (-1, -1, -1), (-1, -1, 2), (-1, 1, 1), and (-1, 1, 2).


Sample Input 2

3
1 1 2

Sample Output 2

1

We have two candidates for H, as follows:

  • H = (1, 1, 1), for which P is (-1, -1, -1);
  • H = (1, 1, 2), for which P is (-1, -1, -1).

Thus, there is one integer sequence that P can be.


Sample Input 3

5
2 2 2 2 2

Sample Output 3

16

Sample Input 4

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

Sample Output 4

31155