Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。
注記
ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。
- ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。
ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。
- 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。
制約
- 1 \le N \le M \le 10
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。
入力例 1
2 3
出力例 1
1 2 1 3 2 3
条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。
入力例 2
3 5
出力例 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Score : 300 points
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
Notes
For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:
- there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.
An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:
- A_i < A_{i+1} for all integers i (1 \le i \le N-1).
Constraints
- 1 \le N \le M \le 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).
Sample Input 1
2 3
Sample Output 1
1 2 1 3 2 3
The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.
Sample Input 2
3 5
Sample Output 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。
- 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i を 1 減らし、A_j を 1 増やす。
A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 4 7 3 7
出力例 1
3
以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。
- i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
- i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
- i=4,j=3 として操作を行う。A=(5,6,5,5) になる。
3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。
入力例 2
1 313
出力例 2
0
入力例 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
出力例 3
2499999974
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).
- Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.
Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 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 the answer as an integer.
Sample Input 1
4 4 7 3 7
Sample Output 1
3
By the following three operations, the difference between the minimum and maximum values of A becomes at most one.
- Choose i=2 and j=3 to make A=(4,6,4,7).
- Choose i=4 and j=1 to make A=(5,6,4,6).
- Choose i=4 and j=3 to make A=(5,6,5,5).
You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.
Sample Input 2
1 313
Sample Output 2
0
Sample Input 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
Sample Output 3
2499999974
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
AtCoder 国には 1 から N の番号がついた N 個の都市と、1 から N-1 の番号がついた N-1 個の道路があります。
道路 i を通ると都市 A_i と都市 B_i の間を相互に移動することができます。全ての都市は道路を使って互いに行き来できることが保証されます。
高橋くんは都市 1 を出発し、次のルールで旅をします。
- いまいる都市と道路で直接つながっている都市のうち、まだ訪れたことがない都市が存在するとき、そのような都市のうち番号が最も小さい都市へ移動する
- そのような都市が存在しないとき
- いまいる都市が都市 1 なら旅を終了する
- そうでないなら、いまいる都市を初めて訪れたときに直前にいた都市へ移動する
高橋くんが旅の過程で訪れる都市を順に答えてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq N
- 全ての都市は道路を使って互いに行き来できる
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
出力
高橋くんが訪れる都市を、旅の開始時及び終了時の都市 1 も含めて順に空白区切りで出力せよ。
入力例 1
4 1 2 4 2 3 1
出力例 1
1 2 4 2 1 3 1
高橋くんの旅は次のようになります。
- 最初都市 1 にいます。
- 都市 1 から道路で直接つながっている都市のうち未訪問なのは都市 2,3 です。番号が小さい都市 2 へ移動します。
- 都市 2 から道路で直接つながっている都市のうち未訪問なのは都市 4 です。都市 4 へ移動します。
- 都市 4 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 2 へ移動します。
- 都市 2 から道路で直接つながっている都市のうち未訪問の都市はありません。初めて都市 2 に来る直前にいた都市 1 へ移動します。
- 都市 1 から道路で直接つながっている都市のうち未訪問なのは都市 3 です。都市 3 へ移動します。
- 都市 3 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 1 へ移動します。
- 都市 1 から道路で直接つながっている都市のうち未訪問の都市はありません。旅を終了します。
入力例 2
5 1 2 1 3 1 4 1 5
出力例 2
1 2 1 3 1 4 1 5 1
Score : 400 points
Problem Statement
The Republic of AtCoder has N cities numbered 1 through N and N-1 roads numbered 1 through N-1. Road i connects City A_i and City B_i bidirectionally. It is guaranteed that one can travel between every pair of cities using roads.
Takahashi will depart City 1 and have a journey by repeating the following.
- If there are unvisited cities that are directly connected to the city Takahashi is in now, he goes to the city with the smallest number among those cities.
- Otherwise,
- if he is in City 1, he ends the journey;
- otherwise, he goes to the city he was in just before visiting the current city for the first time.
Find the sequence of cities visited by Takahashi in the order he visits them.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq N
- It is possible to travel between every pair of cities using roads.
Input
Input is given from Standard Input in the following format:
N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Output
Print the sequence of cities visited by Takahashi in the order he visits them, including City 1 at the beginning and end of the journey, with spaces in between.
Sample Input 1
4 1 2 4 2 3 1
Sample Output 1
1 2 4 2 1 3 1
His journey will be as follows.
- Start in City 1.
- The unvisited cities directly connected to City 1 are City 2 and 3. Go to the city with the smaller number, that is, City 2.
- The unvisited city directly connected to City 2 is City 4. Go there.
- There is no unvisited city directly connected to City 4. Go back to City 2.
- There is no unvisited city directly connected to City 2. Go to City 1, where he was in just before visiting City 2 for the first time.
- The unvisited city directly connected to City 1 is City 3. Go there.
- There is no unvisited city directly connected to City 3. Go back to City 1.
- There is no unvisited city directly connected to City 1. End the journey.
Sample Input 2
5 1 2 1 3 1 4 1 5
Sample Output 2
1 2 1 3 1 4 1 5 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
最初、体力が N であるモンスターが 1 体います。
高橋君はモンスターに対し、モンスターの体力が 1 以上残っている限り繰り返し攻撃を行います。
高橋君は 1 回の攻撃で、\frac{P}{100} の確率でモンスターの体力を 2 減らし、 1-\frac{P}{100} の確率でモンスターの体力を 1 減らします。
モンスターの体力が 0 以下になるまでに行う攻撃回数の期待値を \text{mod } 998244353 で出力してください(注記参照)。
注記
求める期待値は必ず有限値かつ有理数となることが証明できます。また、この問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を出力してください。
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq P \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N P
出力
高橋君の攻撃回数の期待値を \text{mod } 998244353 で出力せよ。
入力例 1
3 10
出力例 1
229596204
高橋君は 1 回の攻撃で、 \frac{10}{100}=\frac{1}{10} の確率でモンスターの体力を 2 減らし、 1-\frac{10}{100}=\frac{9}{10} の確率でモンスターの体力を 1 減らします。
- 最初、モンスターの体力は 3 です。
- 1 回目の攻撃の後、\frac{9}{10}の確率でモンスターの体力は 2、\frac{1}{10}の確率でモンスターの体力は 1 となります。
- 2 回目の攻撃の後、\frac{81}{100}の確率でモンスターの体力は 1、\frac{18}{100} の確率でモンスターの体力は 0、\frac{1}{100} の確率でモンスターの体力は -1 となります。 \frac{18}{100}+\frac{1}{100}=\frac{19}{100} の確率で体力は 0 以下となり、高橋君は 2 回で攻撃をやめます。
- 2 回目の攻撃の後で体力が 1 残っている場合、3 回目の攻撃の後でモンスターの体力は必ず 0 以下となり、高橋君は 3 回で攻撃をやめます。
よって、期待値は 2\times \frac{19}{100}+3\times\left(1-\frac{19}{100}\right)=\frac{281}{100} となります。229596204 \times 100 \equiv 281\pmod{998244353} であるため、229596204 を出力します。
入力例 2
5 100
出力例 2
3
高橋君は 1 回の攻撃で、つねにモンスターの体力を 2 減らします。 2 回目の攻撃が終わった時点では体力が 5-2\times 2=1 残っているため、3 回目の攻撃を行う必要があります。
入力例 3
280 59
出力例 3
567484387
Score : 500 points
Problem Statement
There is a monster with initial stamina N.
Takahashi repeatedly attacks the monster while the monster's stamina remains 1 or greater.
An attack by Takahashi reduces the monster's stamina by 2 with probability \frac{P}{100} and by 1 with probability 1-\frac{P}{100}.
Find the expected value, modulo 998244353 (see Notes), of the number of attacks before the monster's stamina becomes 0 or less.
Notes
We can prove that the sought expected value is always a finite rational number. Moreover, under the Constraints of this problem, when the value is represented as \frac{P}{Q} by two coprime integers P and Q, we can show that there exists a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Print such R.
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq P \leq 100
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P
Output
Find the expected value, modulo 998244353, of the number of Takahashi's attacks.
Sample Input 1
3 10
Sample Output 1
229596204
An attack by Takahashi reduces the monster's stamina by 2 with probability \frac{10}{100}=\frac{1}{10} and by 1 with probability \frac{100-10}{100}=\frac{9}{10}.
- The monster's initial stamina is 3.
- After the first attack, the monster's stamina is 2 with probability \frac{9}{10} and 1 with probability \frac{1}{10}.
- After the second attack, the monster's stamina is 1 with probability \frac{81}{100}, 0 with probability \frac{18}{100}, and -1 with probability \frac{1}{100}. With probability \frac{18}{100}+\frac{1}{100}=\frac{19}{100}, the stamina becomes 0 or less, and Takahashi stops attacking after two attacks.
- If the stamina remains 1 after two attacks, the monster's stamina always becomes 0 or less by the third attack, so he stops attacking after three attacks.
Therefore, the expected value is 2\times \frac{19}{100}+3\times\left(1-\frac{19}{100}\right)=\frac{281}{100}. Since 229596204 \times 100 \equiv 281\pmod{998244353}, print 229596204.
Sample Input 2
5 100
Sample Output 2
3
Takahashi's attack always reduces the monster's stamina by 2. After the second attack, the stamina remains 5-2\times 2=1, so the third one is required.
Sample Input 3
280 59
Sample Output 3
567484387
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
以下のいずれかの条件を満たす文字列を正しい括弧列と定義します。
- 空文字列
- ある正しい括弧列 A が存在して、
(, A,)をこの順に連結した文字列 - ある空でない正しい括弧列 A, B が存在して、A, B をこの順に連結した文字列
( と ) のみからなる長さ N の文字列 S があります。
Q 個のクエリ \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q が与えられるので、順番に処理してください。クエリには 2 種類があり、入力形式とクエリの内容は以下の通りです。
1 l r: S の l 文字目と r 文字目を入れ替える。2 l r: S の l 文字目から r 文字目までの連続部分文字列が正しい括弧列であるか判定する。
制約
- 1 \leq N,Q \leq 2 \times 10^5
- S は
(と)のみからなる長さ N の文字列 - 1 \leq l < r \leq N
- N,Q,l,r はいずれも整数
- 各クエリは
1 l r、2 l rのいずれかの形式で与えられる。 2 l rの形式のクエリは 1 つ以上与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
出力
2 l r の形式の各クエリに対して、連続部分文字列が正しい括弧列である場合 Yes、そうでない場合 No と出力し、改行せよ。
入力例 1
5 3 (())( 2 1 4 2 1 2 2 4 5
出力例 1
Yes No No
1 つ目のクエリにおいて、(()) は正しい括弧列です。
2 つ目のクエリにおいて、(( は正しい括弧列ではありません。
3 つ目のクエリにおいて、)( は正しい括弧列ではありません。
入力例 2
5 3 (())( 2 1 4 1 1 4 2 1 4
出力例 2
Yes No
1 つ目のクエリにおいて、(()) は正しい括弧列です。
2 つ目のクエリによって、S は )()(( となります。
3 つ目のクエリにおいて、)()( は正しい括弧列ではありません。
入力例 3
8 8 (()(())) 2 2 7 2 2 8 1 2 5 2 3 4 1 3 4 1 3 5 1 1 4 1 6 8
出力例 3
Yes No No
Score : 500 points
Problem Statement
Let us define a correct parenthesis sequence as a string that satisfies one of the following conditions.
- It is an empty string.
- It is a concatenation of
(, A,), in this order, for some correct parenthesis sequence A. - It is a concatenation of A, B, in this order, for some correct parenthesis sequences A and B.
We have a string S of length N consisting of ( and ).
Given Q queries \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q, process them in order. There are two kinds of queries, with the following formats and content.
1 l r: Swap the l-th and r-th characters of S.2 l r: Determine whether the contiguous substring from the l-th through the r-th character is a correct parenthesis sequence.
Constraints
- 1 \leq N,Q \leq 2 \times 10^5
- S is a string of length N consisting of
(and). - 1 \leq l < r \leq N
- N,Q,l,r are all integers.
- Each query is in the format
1 l ror2 l r. - There is at least one query in the format
2 l r.
Input
Input is given from Standard Input in the following format:
N Q
S
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
Output
For each query in the format 2 l r, print Yes if the contiguous substring is a correct parenthesis sequence, and No otherwise, followed by a newline.
Sample Input 1
5 3 (())( 2 1 4 2 1 2 2 4 5
Sample Output 1
Yes No No
In the first query, (()) is a correct parenthesis sequence.
In the second query, (( is not a correct parenthesis sequence.
In the third query, )( is not a correct parenthesis sequence.
Sample Input 2
5 3 (())( 2 1 4 1 1 4 2 1 4
Sample Output 2
Yes No
In the first query, (()) is a correct parenthesis sequence.
In the second query, S becomes )()((.
In the third query, )()( is not a correct parenthesis sequence.
Sample Input 3
8 8 (()(())) 2 2 7 2 2 8 1 2 5 2 3 4 1 3 4 1 3 5 1 1 4 1 6 8
Sample Output 3
Yes No No