E - Enumerate Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

以下の条件を満たす長さ N の整数列を辞書順で小さい方から順に全て出力して下さい。

  • i 番目の要素は 1 以上 R_i 以下
  • 総和が K の倍数
数列の辞書順とは? 数列 A = (A_1, \ldots, A_{|A|})B = (B_1, \ldots, B_{|B|}) より辞書順で真に小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。
  1. |A|<|B| かつ (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}) である。
  2. ある整数 1\leq i\leq \min\{|A|,|B|\} が存在して、下記の 2 つがともに成り立つ。
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

制約

  • 入力は全て整数
  • 1 \le N \le 8
  • 2 \le K \le 10
  • 1 \le R_i \le 5

入力

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

N K
R_1 R_2 \dots R_N

出力

出力すべき数列が X 個あり、そのうち i 個目が A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}) であったとき、答えを以下の形式で出力せよ。

A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}

入力例 1

3 2
2 1 3

出力例 1

1 1 2
2 1 1
2 1 3

出力すべき数列は 3 個で、辞書順で (1,1,2),(2,1,1),(2,1,3) です。


入力例 2

1 2
1

出力例 2


出力すべき数列が無い場合もあります。
この場合、出力は空で構いません。


入力例 3

5 5
2 3 2 3 2

出力例 3

1 1 1 1 1
1 2 2 3 2
1 3 1 3 2
1 3 2 2 2
1 3 2 3 1
2 1 2 3 2
2 2 1 3 2
2 2 2 2 2
2 2 2 3 1
2 3 1 2 2
2 3 1 3 1
2 3 2 1 2
2 3 2 2 1

Score : 300 points

Problem Statement

Print all integer sequences of length N that satisfy the following conditions, in ascending lexicographical order.

  • The i-th element is between 1 and R_i, inclusive.
  • The sum of all elements is a multiple of K.
What is lexicographical order for sequences? A sequence A = (A_1, \ldots, A_{|A|}) is lexicographically smaller than B = (B_1, \ldots, B_{|B|}) if either 1. or 2. below holds:
  1. |A|<|B| and (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
  2. There exists an integer 1\leq i\leq \min\{|A|,|B|\} such that both of the following are true:
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

Constraints

  • All input values are integers.
  • 1 \le N \le 8
  • 2 \le K \le 10
  • 1 \le R_i \le 5

Input

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

N K
R_1 R_2 \dots R_N

Output

Print the answer in the following format, where X is the number of sequences to print, the i-th of which is A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}):

A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}

Sample Input 1

3 2
2 1 3

Sample Output 1

1 1 2
2 1 1
2 1 3

There are three sequences to be printed, which are (1,1,2),(2,1,1),(2,1,3) in lexicographical order.


Sample Input 2

1 2
1

Sample Output 2


There may be no sequences to print.
In this case, the output can be empty.


Sample Input 3

5 5
2 3 2 3 2

Sample Output 3

1 1 1 1 1
1 2 2 3 2
1 3 1 3 2
1 3 2 2 2
1 3 2 3 1
2 1 2 3 2
2 2 1 3 2
2 2 2 2 2
2 2 2 3 1
2 3 1 2 2
2 3 1 3 1
2 3 2 1 2
2 3 2 2 1
F - Long Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の正整数のみからなる数列 A=(A_1,\dots,A_N) があります。
A10^{100} 回連結した数列を数列 B とします。

B の項を前から順に足したとき、和が初めて X を超えるのは何項目まで足したときですか?
すなわち、以下の式を満たす最小の整数 k を求めてください。

\displaystyle{\sum_{i=1}^{k} B_i \gt X}

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力は全て整数

入力

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

N
A_1 \ldots A_N
X

出力

答えを出力せよ。


入力例 1

3
3 5 2
26

出力例 1

8

B=(3,5,2,3,5,2,3,5,2,\dots) です。
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} であり、k7 以下のとき条件を満たさないので、8 が答えです。


入力例 2

4
12 34 56 78
1000

出力例 2

23

Score : 300 points

Problem Statement

We have a sequence of N positive integers: A=(A_1,\dots,A_N).
Let B be the concatenation of 10^{100} copies of A.

Consider summing up the terms of B from left to right. When does the sum exceed X for the first time?
In other words, find the minimum integer k such that:

\displaystyle{\sum_{i=1}^{k} B_i \gt X}.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N
X

Output

Print the answer.


Sample Input 1

3
3 5 2
26

Sample Output 1

8

We have B=(3,5,2,3,5,2,3,5,2,\dots).
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} holds, but the condition is not satisfied when k is 7 or less, so the answer is 8.


Sample Input 2

4
12 34 56 78
1000

Sample Output 2

23
G - Flip to Gather

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N01 のみからなる文字列 S が与えられます。

あなたは以下の操作を何回でも( 0 回でもよい)行うことができます。

  • 1 \leq i \leq N を満たす整数 i を選び、S_i0 のときは 1 に、1 のときは 0 に変更する。

あなたの目標は S に含まれる 1高々 1の区間をなすようにすることです。目標を達成するために必要な操作回数の最小値を求めてください。

より厳密には以下の条件をともに満たすような整数の組 (l,r) が存在するような S にすることが目標です。目標を達成するために必要な操作回数の最小値を求めてください。

  • 1 \leq l \leq r \leq N+1
  • 1 \leq i \leq N を満たす整数 i に対し、S_i= 1l \leq i < r は同値である。

なお、有限回の操作で必ず目標が達成できることが証明できます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 20000
  • 1 \leq N \leq 2 \times 10^5
  • S01 のみからなる長さ N の文字列
  • 各入力ファイルについて、すべてのテストケースの N の総和は 2 \times 10^5 以下である。
  • T,N は整数

入力

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_ii 番目のテストケースを表し、以下の形式で与えられる。

N
S

出力

T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
5
10011
10
1111111111
7
0000000

出力例 1

1
0
0

1 番目のテストケースにおいて、S_10 に変更する操作を行なうと、11 個の区間をなすようになります。また、S のままでは条件を満たしていません。したがって、答えは 1 です。

2 番目のテストケースにおいて、S01 個もないので、操作を行う必要はありません。したがって答えは 0 です。

3 番目のテストケースにおいて、S11 個もないので、操作を行う必要はありません。したがって答えは 0 です。


入力例 2

5
2
01
10
1000010011
12
111100010011
3
111
8
00010101

出力例 2

0
2
3
0
2

Score : 400 points

Problem Statement

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

You can perform the following operation any number of times (possibly zero):

  • Choose an integer i satisfying 1 \leq i \leq N, and change S_i from 0 to 1, or from 1 to 0.

Your goal is to make the 1s in S form at most one interval. Find the minimum number of operations required to achieve the goal.

More precisely, the goal is to make S such that there exists a pair of integers (l,r) satisfying both of the following conditions. Find the minimum number of operations required to achieve the goal.

  • 1 \leq l \leq r \leq N+1.
  • S_i= 1 and l \leq i < r are equivalent for each integer i satisfying 1 \leq i \leq N.

It can be proved that the goal can always be achieved with a finite number of operations.

T test cases are given, so solve each.

Constraints

  • 1 \leq T \leq 20000
  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • For each input file, the sum of N over all test cases is at most 2 \times 10^5.
  • T,N are integers.

Input

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_i represents the i-th test case and is given in the following format:

N
S

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for the i-th test case.


Sample Input 1

3
5
10011
10
1111111111
7
0000000

Sample Output 1

1
0
0

In the first test case, if we perform the operation to change S_1 to 0, the 1s will form one interval. Also, the initial S does not satisfy the condition. Therefore, the answer is 1.

In the second test case, there are no 0s in S, so no operations need to be performed. Therefore, the answer is 0.

In the third test case, there are no 1s in S, so no operations need to be performed. Therefore, the answer is 0.


Sample Input 2

5
2
01
10
1000010011
12
111100010011
3
111
8
00010101

Sample Output 2

0
2
3
0
2
H - Minimal payments

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

Atcoder 王国では A_1 円, A_2 円, \ldots, A_N 円の N 種類の硬貨が使用されています。
ここで、1=A_1 < \ldots < A_N であり、全ての 1\leq i \leq N-1 に対し、A_{i+1}A_i の倍数です。

硬貨のみを使って X 円を支払うとき、支払いに使う硬貨の枚数とお釣りでもらう硬貨の枚数の合計の最小値はいくつですか?

正確には、YX 以上の整数を自由に動く時、Y 円ちょうどを表すために必要な硬貨の枚数と、Y-X 円ちょうどを表すために必要な硬貨の枚数の和の最小値を求めてください。

制約

  • 入力に含まれる値は全て整数である
  • 1 \leq N \leq 60
  • 1=A_1 < \ldots <A_N \leq 10^{18}
  • 全ての 1\leq i \leq N-1A_{i+1}A_i の倍数である
  • 1\leq X \leq 10^{18}

入力

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

N X
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3 87
1 10 100

出力例 1

5

100 円硬貨 1 枚で支払いを行い、10 円硬貨 1 枚と 1 円硬貨 3 枚をお釣りでもらうと、合計枚数は 5 枚になります。


入力例 2

2 49
1 7

出力例 2

7

7 円硬貨 7 枚で支払いを行うのが最適です。


入力例 3

10 123456789012345678
1 100 10000 1000000 100000000 10000000000 1000000000000 100000000000000 10000000000000000 1000000000000000000

出力例 3

233

Score : 500 points

Problem Statement

There are N kinds of coins used in the Kingdom of Atcoder: A_1-yen, A_2-yen, \ldots, A_N-yen coins.
Here, 1=A_1 < \ldots < A_N holds, and A_{i+1} is a multiple of A_i for every 1\leq i \leq N-1.

When paying for a product worth X yen using just these coins, what is the minimum total number of coins used in payment and returned as change?

Accurately, when Y is an integer at least X, find the minimum sum of the number of coins needed to represent exactly Y yen and the number of coins needed to represent exactly Y-X yen.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 60
  • 1=A_1 < \ldots <A_N \leq 10^{18}
  • A_{i+1} is a multiple of A_i for every 1\leq i \leq N-1.
  • 1\leq X \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N X
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3 87
1 10 100

Sample Output 1

5

If we pay with one 100-yen coin and receive one 10-yen coin and three 1-yen coins as the change, the total number of coins will be 5.


Sample Input 2

2 49
1 7

Sample Output 2

7

It is optimal to pay with seven 7-yen coins.


Sample Input 3

10 123456789012345678
1 100 10000 1000000 100000000 10000000000 1000000000000 100000000000000 10000000000000000 1000000000000000000

Sample Output 3

233
I - Substring of Sorted String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

英小文字からなる長さ N の文字列 SQ 個のクエリが与えられます。クエリを順に処理してください。

クエリは以下の 2 種類です。

  • 1 x cSx 文字目を文字 c に置き換える
  • 2 l rS を文字の昇順に並び替えて得られる文字列を T とする。Sl 文字目から r 文字目までからなる文字列が T の部分文字列であるとき Yes、部分文字列でないとき No を出力する
部分文字列とは? S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • 1\leq N \leq 10^5
  • S は英小文字からなる長さ N の文字列
  • 1 \leq Q \leq 10^5
  • 1 種類目のクエリにおいて、1 \leq x \leq N
  • 1 種類目のクエリにおいて、c は英小文字
  • 2 種類目のクエリにおいて、1 \leq l \leq r \leq N

入力

入力は以下の形式で標準入力から与えられる。ただし、\text{query}_ii 番目のクエリを表す。

N
S
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

出力

問題文中の指示に従ってクエリを処理せよ。


入力例 1

6
abcdcf
4
2 1 3
2 2 6
1 5 e
2 2 6

出力例 1

Yes
No
Yes
  • 1 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabccdf です。 S1 文字目から 3 文字目までからなる文字列は abc であり T の部分文字列です。よって Yes を出力します。
  • 2 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabccdf です。 S2 文字目から 6 文字目までからなる文字列は bcdcf であり T の部分文字列ではありません。よって No を出力します。
  • 3 番目のクエリにより、S5 文字目が e に置き換えられ、Sabcdef となります。
  • 4 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabcdef です。 S2 文字目から 6 文字目までからなる文字列は bcdef であり T の部分文字列です。よって Yes を出力します。

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and Q queries. Process the queries in order.

Each query is of one of the following two kinds:

  • 1 x c : replace the x-th character of S by the character c.
  • 2 l r : let T be the string obtained by sorting the characters of S in ascending order. Print Yes if the string consisting of the l-th through r-th characters of S is a substring of T; print No otherwise.
What is a substring? A substring of S is a string obtained by removing 0 or more initial characters and 0 or more final characters of S. For example, ab is a substring of abc, while ac is not a substring of abc.

Constraints

  • 1\leq N \leq 10^5
  • S is a string of length N consisting of lowercase English letters.
  • 1 \leq Q \leq 10^5
  • For each query of the first kind, 1 \leq x \leq N.
  • For each query of the first kind, c is a lowercase English letter.
  • For each query of the second kind, 1 \leq l \leq r \leq N.

Input

The input is given from Standard Input in the following format, where \text{query}_i denotes the i-th query:

N
S
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Output

Process the queries as instructed in the Problem Statement.


Sample Input 1

6
abcdcf
4
2 1 3
2 2 6
1 5 e
2 2 6

Sample Output 1

Yes
No
Yes
  • In the 1-st query, abccdf is the string T obtained by sorting the characters of S in ascending order. The string abc, consisting of the 1-st through 3-rd characters of S, is a substring of T, so Yes should be printed.
  • In the 2-nd query, abccdf is the string T obtained by sorting the characters of S in ascending order. The string bcdcf, consisting of the 2-nd through 6-th characters of S, is not a substring of T, so No should be printed.
  • The 3-rd query sets the 5-th character of S to e, making S abcdef.
  • In the 4-th query, abcdef is the string T obtained by sorting the characters of S in ascending order. The string bcdef, consisting of the 2-nd through 6-th characters of S, is a substring of T, so Yes should be printed.