C - Qual B

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

あるプログラミングコンテストの予選に N 人が参加し、参加者全員が異なる順位を得ました。
長さ N の文字列 S が与えられ、この文字列は決勝への参加希望の有無を表現します。具体的には下記の通りです。

  • Si 文字目が o なら、予選 i 位の参加者が決勝への参加を希望した。
  • Si 文字目が x なら、予選 i 位の参加者が決勝への参加を希望しなかった。

決勝への参加を希望した参加者のうち順位の小さい方から K 人が予選を通過します。

以下の条件を満たす長さ N の文字列 T を出力してください。

  • 予選 i 位の参加者が予選を通過する場合、 Ti 文字目は o
  • 予選 i 位の参加者が予選を通過しない場合、 Ti 文字目は x

制約

  • N,K は整数
  • 1 \le K \le N \le 100
  • Sox からなる長さ N の文字列
  • S には少なくとも K 個の o が含まれる

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

10 3
oxxoxooxox

出力例 1

oxxoxoxxxx

この入力の場合、予選の参加者は N=10 人であり、予選を通過する人数は K=3 人です。

  • 予選 1 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 1 人です。
  • 予選 2,3 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 4 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 2 人です。
  • 予選 5 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 6 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 3 人です。
  • ここで、予選を通過した人数が 3 人となりました。なので、予選 7 位以下の参加者は予選を通過しません。

Score : 200 points

Problem Statement

There were N contestants in the qualification round of a programming contest. All contestants got distinct ranks.
You are given a length-N string S, which represents whether the contestants want to participate in the final round or not. Specifically,

  • if the i-th character of S is o, the contestant ranked i-th in the qualification wants to participate in the final;
  • if the i-th character of S is x, the contestant ranked i-th in the qualification does not want to participate in the final.

Among those who want to participate in the final, K contestants with the smallest ranks advance to the final.

Print a string T of length N that satisfies the following conditions:

  • if the contestant ranked i-th in the qualification advances to the final, the i-th character of T is o;
  • if the contestant ranked i-th in the qualification does not advance to the final, the i-th character of T is x.

Constraints

  • N and K are integers.
  • 1 \le K \le N \le 100
  • S is a string of length N consisting of o and x.
  • S has at least K o's.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

10 3
oxxoxooxox

Sample Output 1

oxxoxoxxxx

In this input, N=10 people took part in the qualification round, and K=3 of them advance to the final.

  • The participant who ranked 1-st in the qualification wants to participate in the final, so the participant advances to the final. 1 participant has advanced so far.
  • The participants who ranked 2-nd and 3-rd in the qualification do not want to participate in the final, so the participants do not advance to the final.
  • The participant who ranked 4-th in the qualification wants to participate in the final, so the participant advances to the final. 2 participants have advanced so far.
  • The participants who ranked 5-th in the qualification does not want to participate in the final, so the participant does not advance to the final.
  • The participant who ranked 6-th in the qualification wants to participate in the final, so the participant advances to the final. 3 participants have advanced so far.
  • Now that 3 people have advanced to the final, no participants ranked 7-th or lower advance to the final.
D - Caesar Cipher

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は英小文字からなる文字列 S を持っています。

高橋君は文字列 S に対して、下記の操作をちょうど 1 回行います。

  • まず、非負整数 K を選ぶ。
  • その後、S の各文字を K 個後ろの英小文字に変更する。

ただし、

  • a1 個後ろの英小文字は b であり、
  • b1 個後ろの英小文字は c であり、
  • c1 個後ろの英小文字は d であり、
  • \cdots
  • y1 個後ろの英小文字は z であり、
  • z1 個後ろの英小文字は a です。

例えば、b4 個後ろの英小文字は f であり、y3 個後ろの英小文字は b です。

文字列 T が与えられます。 高橋君が上記の操作によって ST に一致させることができるかを判定してください。

制約

  • ST はそれぞれ英小文字からなる長さ 1 以上 10^5 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

高橋君が ST に一致させることができる場合は Yes と出力し、 できない場合は No と出力せよ。


入力例 1

abc
ijk

出力例 1

Yes

高橋君が K=8 を選ぶと、

  • a8 個後ろの i
  • b8 個後ろの j
  • c8 個後ろの k

それぞれ変更され、ST が一致します。
高橋君が ST に一致させることができるため Yes と出力します。


入力例 2

z
a

出力例 2

Yes

高橋君が K=1 を選ぶと ST が一致します。
z1 個後ろの英小文字は a であることに注意してください。


入力例 3

ppq
qqp

出力例 3

No

高橋君は非負整数 K をどのように選んでも ST に一致させることができません。 よって、No と出力します。


入力例 4

atcoder
atcoder

出力例 4

Yes

高橋君が K=0 を選ぶと ST が一致します。

Score : 200 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

On this string, he will do the operation below just once.

  • First, choose a non-negative integer K.
  • Then, shift each character of S to the right by K (see below).

Here,

  • a shifted to the right by 1 is b;
  • b shifted to the right by 1 is c;
  • c shifted to the right by 1 is d;
  • \cdots
  • y shifted to the right by 1 is z;
  • z shifted to the right by 1 is a.

For example, b shifted to the right by 4 is f, and y shifted to the right by 3 is b.

You are given a string T. Determine whether Takahashi can make S equal T by the operation above.

Constraints

  • Each of S and T is a string of length between 1 and 10^5 (inclusive) consisting of lowercase English letters.
  • The lengths of S and T are equal.

Input

Input is given from Standard Input in the following format:

S
T

Output

If Takahashi can make S equal T, print Yes; if not, print No.


Sample Input 1

abc
ijk

Sample Output 1

Yes

When Takahashi chooses K=8,

  • a is shifted to the right by 8 and becomes i,
  • b is shifted to the right by 8 and becomes j,
  • c is shifted to the right by 8 and becomes k,

and now S and T are equal.
Therefore, he can make S equal T, so Yes should be printed.


Sample Input 2

z
a

Sample Output 2

Yes

Choosing K=1 makes S and T equal.
Note that the letter on the right of z is a.


Sample Input 3

ppq
qqp

Sample Output 3

No

There is no non-negative integer K that he can choose to make S equal T, so No should be printed.


Sample Input 4

atcoder
atcoder

Sample Output 4

Yes

Choosing K=0 makes S and T equal.

E - Rotate Colored Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2\ldots 、色 MM 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、Si 文字目は色 C_i で塗られています。

i = 1, 2, \ldots, M について、この順番に下記の操作を行います。

  • S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 Sp_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、Sp_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。

上記の操作をすべて行った後の、最終的な S を出力してください。

なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, C_i はすべて整数
  • S は英小文字からなる長さ N の文字列
  • 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ

入力

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

N M
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

出力例 1

cszapqbr

はじめ、S = apzbqrcs です。

  • i = 1 に対する操作では、S1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cpzaqrbs となります。
  • i = 2 に対する操作では、S2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります。
  • i = 3 に対する操作では、S3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります(操作の前後で S は変わりません)。

よって、最終的な S である cszapqbr を出力します。


入力例 2

2 1
aa
1 1

出力例 2

aa

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.

For each i = 1, 2, \ldots, M in this order, let us perform the following operation.

  • Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.

Print the final S after the above operations.

The constraints guarantee that at least one character of S is painted in each of the M colors.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, and C_i are all integers.
  • S is a string of length N consisting of lowercase English letters.
  • For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.

Input

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

N M
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

Sample Output 1

cszapqbr

Initially, S = apzbqrcs.

  • For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S = cpzaqrbs.
  • For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S = cszapqbr.
  • For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S = cszapqbr (here, S is not changed).

Thus, you should print cszapqbr, the final S.


Sample Input 2

2 1
aa
1 1

Sample Output 2

aa
F - ABC conjecture

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正の整数 N が与えられます。

A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。

なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。

制約

  • 1 \leq N \leq 10^{11}
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

5

条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2)5 つです。


入力例 2

100

出力例 2

323

入力例 3

100000000000

出力例 3

5745290566750

Score : 300 points

Problem Statement

You are given a positive integer N.

Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.

The Constraints guarantee that the answer is less than 2^{63}.

Constraints

  • 1 \leq N \leq 10^{11}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

5

There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).


Sample Input 2

100

Sample Output 2

323

Sample Input 3

100000000000

Sample Output 3

5745290566750
G - Three Days Ago

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

20230322 は並べ替えると 02320232 となり、これは 02322 度繰り返しています。
このように、数字のみからなる文字列であって、適切に文字を並び替える (そのままでもよい) ことによって同じ列を 2 度繰り返すようにできるものを 嬉しい列 と呼びます。
数字のみからなる文字列 S が与えられるので、以下の条件を全て満たす整数の組 (l,r) はいくつあるか求めてください。

  • 1 \le l \le r \le |S| ( |S|S の長さ)
  • Sl 文字目から r 文字目までの (連続する) 部分文字列は嬉しい列である。

制約

  • S は数字のみからなる長さ 1 以上 5 \times 10^5 以下の文字列

入力

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

S

出力

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


入力例 1

20230322

出力例 1

4

S= 20230322 です。
条件を満たす整数組 (l,r)(1,6),(1,8),(2,7),(7,8)4 つです。


入力例 2

0112223333444445555556666666777777778888888889999999999

出力例 2

185

S の先頭が 0 である場合もあります。


入力例 3

3141592653589793238462643383279502884197169399375105820974944

出力例 3

9

Score : 400 points

Problem Statement

The string 20230322 can be rearranged into 02320232, which is a repetition of 0232 twice.
Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.
You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions.

  • 1 \le l \le r \le |S|. (|S| is the length of S.)
  • The (contiguous) substring formed of the l-th through r-th characters of S is happy.

Constraints

  • S is a string consisting of digits whose length is between 1 and 5 \times 10^5, inclusive.

Input

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

S

Output

Print an integer representing the answer.


Sample Input 1

20230322

Sample Output 1

4

We have S= 20230322.

Here are the four pairs of integers (l,r) that satisfy the condition: (1,6), (1,8), (2,7), and (7,8).


Sample Input 2

0112223333444445555556666666777777778888888889999999999

Sample Output 2

185

S may begin with 0.


Sample Input 3

3141592653589793238462643383279502884197169399375105820974944

Sample Output 3

9