A - Adjacent Product

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。 また、B_i=A_i\times A_{i+1}\ (1\leq i\leq N-1) と定めます。

B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力してください。

制約

  • 2\leq N \leq 100
  • 1\leq A_i \leq 100
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力せよ。


入力例 1

3
3 4 6

出力例 1

12 24

B_1=A_1\times A_2 = 12, B_2=A_2\times A_3 = 24 です。


入力例 2

5
22 75 26 45 72

出力例 2

1650 1950 1170 3240

Score: 100 points

Problem Statement

You are given N integers A_1, A_2, \dots, A_N. Also, define B_i = A_i \times A_{i+1}\ (1 \leq i \leq N-1).

Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.


Sample Input 1

3
3 4 6

Sample Output 1

12 24

We have B_1 = A_1 \times A_2 = 12, B_2 = A_2 \times A_3 = 24.


Sample Input 2

5
22 75 26 45 72

Sample Output 2

1650 1950 1170 3240
B - Piano

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

無限に長いピアノの鍵盤があります。 この鍵盤内の連続する区間であって、白鍵 W 個と黒鍵 B 個からなるものは存在しますか?

文字列 wbwbwwbwbwbw を無限に繰り返してできる文字列を S とおきます。

S の部分文字列であって、W 個の wB 個の b からなるものは存在しますか?

S の部分文字列とは S の部分文字列とは、ある 2 つの正整数 l,r\ (l\leq r) に対して、Sl 文字目、l+1 文字目、\dotsr 文字目をこの順に繋げてできる文字列のことをいいます。

制約

  • W,B は整数
  • 0\leq W,B \leq 100
  • W+B \geq 1

入力

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

W B

出力

S の部分文字列であって、W 個の wB 個の b からなるものが存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

3 2

出力例 1

Yes

S の最初の 15 文字は wbwbwwbwbwbwwbw であり、S11 文字目から 15 文字目までを取り出してできる文字列 bwwbw3 個の w2 個の b からなる部分文字列の一例です。


入力例 2

3 0

出力例 2

No

3 個の w0 個の b からなる文字列は www のみですが、これは S の部分文字列ではありません。


入力例 3

92 66

出力例 3

Yes

Score: 200 points

Problem Statement

There is an infinitely long piano keyboard. Is there a continuous segment within this keyboard that consists of W white keys and B black keys?

Let S be the string formed by infinitely repeating the string wbwbwwbwbwbw.

Is there a substring of S that consists of W occurrences of w and B occurrences of b?

What is a substring of S? A substring of S is a string that can be formed by concatenating the l-th, (l+1)-th, \dots, r-th characters of S in this order for some two positive integers l and r (l\leq r).

Constraints

  • W and B are integers.
  • 0\leq W,B \leq 100
  • W+B \geq 1

Input

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

W B

Output

If there is a substring of S that consists of W occurrences of w and B occurrences of b, print Yes; otherwise, print No.


Sample Input 1

3 2

Sample Output 1

Yes

The first 15 characters of S are wbwbwwbwbwbwwbw. You can take the 11-th through 15-th characters to form the string bwwbw, which is a substring consisting of three occurrences of w and two occurrences of b.


Sample Input 2

3 0

Sample Output 2

No

The only string consisting of three occurrences of w and zero occurrences of b is www, which is not a substring of S.


Sample Input 3

92 66

Sample Output 3

Yes
C - Σ

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) および正整数 K が与えられます。

1 以上 K 以下の整数のうち、A の中に一度も現れないものの総和を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 2\times 10^9
  • 1\leq A_i \leq 2\times 10^9
  • 入力は全て整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 5
1 6 3 1

出力例 1

11

1 以上 5 以下の整数のうち、A の中に一度も現れないものは 2,4,53 つです。

よって、それらの総和である 2+4+5=11 を出力します。


入力例 2

1 3
346

出力例 2

6

入力例 3

10 158260522
877914575 24979445 623690081 262703497 24979445 1822804784 1430302156 1161735902 923078537 1189330739

出力例 3

12523196466007058

Score: 250 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N and a positive integer K.

Find the sum of the integers between 1 and K, inclusive, that do not appear in the sequence A.

Constraints

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

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 5
1 6 3 1

Sample Output 1

11

Among the integers between 1 and 5, three numbers, 2, 4, and 5, do not appear in A.

Thus, print their sum: 2+4+5=11.


Sample Input 2

1 3
346

Sample Output 2

6

Sample Input 3

10 158260522
877914575 24979445 623690081 262703497 24979445 1822804784 1430302156 1161735902 923078537 1189330739

Sample Output 3

12523196466007058
D - Gomamayo Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

0, 1 からなる長さ N の文字列 S が与えられます。

0, 1 からなる長さ N の文字列 T は以下の条件を満たすとき、またそのときに限り 良い文字列 であると定義します。

  • 1 \leq i \leq N - 1 を満たす整数 i であって、Ti 文字目と i + 1 文字目が一致するようなものがちょうど 1 つ存在する。

i = 1,2,\ldots, N について以下の操作を 1 度行うか行わないか選ぶことができます。

  • Si 文字目が 0 であるとき Si 文字目を 1 に、そうでないとき Si 文字目を 0 に置き換える。操作を行った場合、C_i のコストがかかる。

S を良い文字列にするために必要なコストの総和の最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • S は長さ N0,1 からなる文字列
  • 1 \leq C_i \leq 10^9
  • N, C_i は整数

入力

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

N
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

5
00011
3 9 2 6 4

出力例 1

7

i = 1, 5 に対して操作を行い、i = 2, 3, 4 に対して操作を行わないことで S = 10010 となり、S は良い文字列となります。このときかかるコストは 7 であり、コスト 7 未満で S を良い文字列にすることはできないため、7 を出力します。


入力例 2

4
1001
1 2 3 4

出力例 2

0

入力例 3

11
11111100111
512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427

出力例 3

2286846953

Score: 400 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.

A string T of length N consisting of 0 and 1 is a good string if and only if it satisfies the following condition:

  • There is exactly one integer i such that 1 \leq i \leq N - 1 and the i-th and (i + 1)-th characters of T are the same.

For each i = 1,2,\ldots, N, you can choose whether or not to perform the following operation once:

  • If the i-th character of S is 0, replace it with 1, and vice versa. The cost of this operation, if performed, is C_i.

Find the minimum total cost required to make S a good string.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1 \leq C_i \leq 10^9
  • N and C_i are integers.

Input

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

N
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

5
00011
3 9 2 6 4

Sample Output 1

7

Performing the operation for i = 1, 5 and not performing it for i = 2, 3, 4 makes S = 10010, which is a good string. The cost incurred in this case is 7, and it is impossible to make S a good string for less than 7, so print 7.


Sample Input 2

4
1001
1 2 3 4

Sample Output 2

0

Sample Input 3

11
11111100111
512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427

Sample Output 3

2286846953
E - Paint

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 450

問題文

HW 列のグリッドがあり、はじめすべてのマスは色 0 で塗られています。

これから i = 1, 2, \ldots, M の順で以下の操作を行います。

  • T_i = 1 のとき、A_i 行目のマスをすべて色 X_i に塗り替える

  • T_i = 2 のとき、A_i 列目のマスをすべて色 X_i に塗り替える

すべての操作を終えたとき、最終的に色 i で塗られたマスが存在するような各色 i についてその色で塗られたマスの個数を求めてください。

制約

  • 1 \leq H, W, M \leq 2 \times 10^5
  • T_i \in \lbrace 1, 2 \rbrace
  • T_i = 1 なる i に対して 1 \leq A_i \leq H
  • T_i = 2 なる i に対して 1 \leq A_i \leq W
  • 0 \leq X_i \leq 2 \times 10^5
  • 入力される値はすべて整数

入力

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

H W M
T_1 A_1 X_1
T_2 A_2 X_2
\vdots
T_M A_M X_M

出力

i で塗られたマスが存在するような整数 i の個数を K として、K + 1 行出力せよ。

1 行目には K の値を出力せよ。

2 行目以降には色 i で塗られたマスが存在するような各色 i について、色の番号およびその色で塗られたマスの個数を出力せよ。

具体的には、i + 1 (1 \leq i \leq K) 行目には色の番号 c_i と色 c_i で塗られたマスの個数 x_i をこの順に空白区切りで出力せよ。

ただし、色の番号は昇順で出力せよ。すなわち、c_1 < c_2 < \ldots < c_K を満たすように出力せよ。また、x_i > 0 が必要であることに注意せよ。


入力例 1

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

出力例 1

3
0 5
2 4
5 3

操作によってグリッドの各マスの色は以下のように変化します。

0000   0000   0000   0000   0000
0000 → 5555 → 5550 → 5550 → 5550 
0000   0000   0000   3333   2222

最終的に色 0 で塗られたマスは 5 つ、色 2 で塗られたマスは 4 つ、色 5 で塗られたマスは 3 つです。


入力例 2

1 1 5
1 1 1
1 1 10
2 1 100
1 1 1000
2 1 10000

出力例 2

1
10000 1

入力例 3

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

出力例 3

5
6 5
7 5
8 5
9 5
10 5

Score: 450 points

Problem Statement

There is a grid with H rows and W columns. Initially, all cells are painted with color 0.

You will perform the following operations in the order i = 1, 2, \ldots, M.

  • If T_i = 1, repaint all cells in the A_i-th row with color X_i.

  • If T_i = 2, repaint all cells in the A_i-th column with color X_i.

After all operations are completed, for each color i that exists on the grid, find the number of cells that are painted with color i.

Constraints

  • 1 \leq H, W, M \leq 2 \times 10^5
  • T_i \in \lbrace 1, 2 \rbrace
  • 1 \leq A_i \leq H for each i such that T_i = 1,
  • 1 \leq A_i \leq W for each i such that T_i = 2.
  • 0 \leq X_i \leq 2 \times 10^5
  • All input values are integers.

Input

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

H W M
T_1 A_1 X_1
T_2 A_2 X_2
\vdots
T_M A_M X_M

Output

Let K be the number of distinct integers i such that there are cells painted with color i. Print K + 1 lines.

The first line should contain the value of K.

The second and subsequent lines should contain, for each color i that exists on the grid, the color number i and the number of cells painted with that color.

Specifically, the (i + 1)-th line (1 \leq i \leq K) should contain the color number c_i and the number of cells x_i painted with color c_i, in this order, separated by a space.

Here, print the color numbers in ascending order. That is, ensure that c_1 < c_2 < \ldots < c_K. Note also that x_i > 0 is required.


Sample Input 1

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

Sample Output 1

3
0 5
2 4
5 3

The operations will change the colors of the cells in the grid as follows:

0000   0000   0000   0000   0000
0000 → 5555 → 5550 → 5550 → 5550 
0000   0000   0000   3333   2222

Eventually, there are five cells painted with color 0, four with color 2, and three with color 5.


Sample Input 2

1 1 5
1 1 1
1 1 10
2 1 100
1 1 1000
2 1 10000

Sample Output 2

1
10000 1

Sample Input 3

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

Sample Output 3

5
6 5
7 5
8 5
9 5
10 5
F - SSttrriinngg in StringString

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 525

問題文

長さ n の文字列 X に対して、Xk 回繰り返して得られる文字列を f(X,k) と表記し、X1 文字目、2 文字目、\dotsn 文字目を k 回ずつこの順に繰り返して得られる文字列を g(X,k) と表記します。 例えば、X= abc のとき、f(X,2)= abcabcg(X,3)= aaabbbccc です。 また、任意の文字列 X に対して、f(X,0)g(X,0) は共に空文字列です。

正整数 N および文字列 S,T が与えられます。 g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を求めてください。 なお、定義より、g(T,0) は常に f(S,N) の部分列であることに注意してください。

部分列とは 文字列 X の(連続とは限らない)部分列とは、X から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。 例えば、acatcoder (空文字列)などはどれも atcoder の部分列ですが、taatcoder の部分列ではありません。

制約

  • N は整数
  • 1\leq N\leq 10^{12}
  • S, T は英小文字からなる長さ 1 以上 10^5 以下の文字列

入力

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

N
S
T

出力

g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を出力せよ。


入力例 1

3
abc
ab

出力例 1

2

f(S,3)= abcabcabc です。 g(T,2)= aabbf(S,3) の部分列ですが、g(T,3)= aaabbbf(S,3) の部分列ではないので、2 を出力します。


入力例 2

3
abc
arc

出力例 2

0

入力例 3

1000000000000
kzazkakxkk
azakxk

出力例 3

344827586207

Score: 525 points

Problem Statement

For a string X of length n, let f(X,k) denote the string obtained by repeating k times the string X, and g(X,k) denote the string obtained by repeating k times the first character, the second character, \dots, the n-th character of X, in this order. For example, if X= abc, then f(X,2)= abcabc, and g(X,3)= aaabbbccc. Also, for any string X, both f(X,0) and g(X,0) are empty strings.

You are given a positive integer N and strings S and T. Find the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N). Note that g(T,0) is always a subsequence of f(S,N) by definition.

What is a subsequence? A (not necessarily contiguous) subsequence of a string X is a string obtained by removing zero or more characters from X and then concatenating the remaining elements without changing the order. For example, ac, atcoder, and (empty string) are all subsequences of atcoder, but ta is not.

Constraints

  • N is an integer.
  • 1\leq N\leq 10^{12}
  • S and T are strings consisting of lowercase English letters with lengths between 1 and 10^5, inclusive.

Input

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

N
S
T

Output

Print the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N).


Sample Input 1

3
abc
ab

Sample Output 1

2

We have f(S,3)= abcabcabc. g(T,2)= aabb is a subsequence of f(S,3), but g(T,3)= aaabbb is not, so print 2.


Sample Input 2

3
abc
arc

Sample Output 2

0

Sample Input 3

1000000000000
kzazkakxkk
azakxk

Sample Output 3

344827586207
G - Alone

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 575

問題文

整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。

整数の組 (L, R) であって、以下の条件を満たすものの個数を求めてください。

  • 1 \leq L \leq R \leq N
  • A_L, A_{L + 1}, \ldots, A_R の中に 1 度だけ出現する数が存在する。より厳密には、ある整数 x が存在して、A_i = x かつ L \leq i \leq R を満たす整数 i の個数がちょうど 1 個である。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
2 2 1 2 1

出力例 1

12

(L, R) = (1, 1), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5), (5, 5)12 個の整数の組が条件を満たします。


入力例 2

4
4 4 4 4

出力例 2

4

入力例 3

10
1 2 1 4 3 3 3 2 2 4

出力例 3

47

Score: 575 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \ldots, A_N).

Find the number of pairs of integers (L, R) that satisfy the following conditions:

  • 1 \leq L \leq R \leq N
  • There is a number that appears exactly once among A_L, A_{L + 1}, \ldots, A_R. More precisely, there is an integer x such that exactly one integer i satisfies A_i = x and L \leq i \leq R.

Constraints

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

Input

The 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

5
2 2 1 2 1

Sample Output 1

12

12 pairs of integers satisfy the conditions: (L, R) = (1, 1), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5), (5, 5).


Sample Input 2

4
4 4 4 4

Sample Output 2

4

Sample Input 3

10
1 2 1 4 3 3 3 2 2 4

Sample Output 3

47