実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ M の正整数列が N 個与えられます。i 個目の正整数列は A_i=(A_{i,1},A_{i,2},\dots,A_{i,M}) です。
これら N 個の正整数列から 1 個ずつ要素を選ぶ方法は M^N 通りありますが、その全てに対する「選んだ要素に含まれる整数の種類数」の総和を 998244353 で割った余りを求めてください。
制約
- 1 \le N,M \le 500
- 1 \le A_{i,j} \le NM
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1} A_{1,2} \dots A_{1,M}
A_{2,1} A_{2,2} \dots A_{2,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
出力
答えを出力せよ。
入力例 1
2 2 1 3 2 3
出力例 1
7
例えば、A_{1,1},A_{2,1} を選んだ場合、選んだ要素に含まれる整数は 1,2 の 2 種類となります。
A_{1,2},A_{2,2} を選んだ場合のみ種類数が 1 となり、他の 3 通りでは種類数が 2 となるため答えは 7 です。
入力例 2
2 2 1 1 1 2
出力例 2
6
入力例 3
3 5 3 1 3 4 2 5 2 1 2 3 4 6 2 5 6
出力例 3
327
Score : 400 points
Problem Statement
You are given N sequences of positive integers, each of length M. The i-th sequence is A_i=(A_{i,1},A_{i,2},\dots,A_{i,M}).
There are M^N ways to choose one element from each of these N sequences. Find the sum, modulo 998244353, of "the number of distinct integers among the chosen elements" over all such ways.
Constraints
- 1 \le N,M \le 500
- 1 \le A_{i,j} \le NM
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_{1,1} A_{1,2} \dots A_{1,M}
A_{2,1} A_{2,2} \dots A_{2,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
Output
Output the answer.
Sample Input 1
2 2 1 3 2 3
Sample Output 1
7
For example, if A_{1,1} and A_{2,1} are chosen, there are two distinct integers among the chosen elements: 1 and 2.
The number of distinct integers is 1 only when A_{1,2} and A_{2,2} are chosen, and it is 2 in the other three cases, so the answer is 7.
Sample Input 2
2 2 1 1 1 2
Sample Output 2
6
Sample Input 3
3 5 3 1 3 4 2 5 2 1 2 3 4 6 2 5 6
Sample Output 3
327
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
黒板に N 個の非負整数 A_1,A_2,\dots,A_N が書かれています。
Alice と Bob がゲームをします。Alice から始めて以下の操作を交互に行い、黒板に書かれている整数を 0 個にした方が勝ちです。
- 操作を行う時点で黒板に書かれている最小の非負整数を m とする。
- m > 0 のとき、1 以上 m 以下の正整数 x を選ぶ。黒板に書かれている全ての整数をそれぞれ今の値から x 引いた数に書き換える。
- m = 0 のとき、黒板に書かれている 0 のうち、1 個以上を削除する。
両者が勝つために最善な行動をしたとき、勝つのがどちらか判定してください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
制約
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 2 \times 10^5
- 0 \le A_i \le 10^9
- 全てのテストケースに対する N の総和は 2 \times 10^5 以下
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N A_1 A_2 \dots A_N
出力
T 行出力せよ。i 行目には \mathrm{case}_i について、Alice が勝つ場合は Alice を、Bob が勝つ場合は Bob を出力せよ。
入力例 1
5 1 2 3 1 1 1 4 1 2 3 4 7 3 1 4 1 5 9 2 3 218 503 2026
出力例 1
Alice Bob Bob Bob Alice
1 個目のテストケースについて、あり得るゲームの進行としては、以下のようなものが考えられます。
- 始め、黒板には 2 が書かれている。
- Alice が x = 1 を選ぶ。黒板には 1 が書かれている。
- Bob が x = 1 を選ぶ。黒板には 0 が書かれている。
- Alice が 0 を 1 個選び削除する。黒板には何も書かれていない。
両者が勝つために最善な行動をしたとき、勝つのは Alice です。
2 個目のテストケースについて、あり得るゲームの進行としては、以下のようなものが考えられます。
- 始め、黒板には 1,1,1 が書かれている。
- Alice が x = 1 を選ぶ。黒板には 0,0,0 が書かれている。
- Bob が 0 を 1 個選び削除する。黒板には 0,0 が書かれている。
- Alice が 0 を 1 個選び削除する。黒板には 0 が書かれている。
- Bob が 0 を 1 個選び削除する。黒板には何も書かれていない。
両者が勝つために最善な行動をしたとき、勝つのは Bob です。
Score : 400 points
Problem Statement
There are N non-negative integers A_1,A_2,\dots,A_N written on a blackboard.
Alice and Bob play a game. Starting with Alice, they alternately perform the following operation, and the player who reduces the number of integers written on the blackboard to 0 wins.
- Let m be the minimum non-negative integer currently written on the blackboard.
- If m > 0, choose a positive integer x between 1 and m, inclusive. Replace every integer written on the blackboard with its current value minus x.
- If m = 0, erase one or more of the 0s written on the blackboard.
Determine who wins when both players play optimally to win.
T test cases are given; solve each of them.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 2 \times 10^5
- 0 \le A_i \le 10^9
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N A_1 A_2 \dots A_N
Output
Output T lines. The i-th line should contain Alice if Alice wins in \mathrm{case}_i, or Bob if Bob wins.
Sample Input 1
5 1 2 3 1 1 1 4 1 2 3 4 7 3 1 4 1 5 9 2 3 218 503 2026
Sample Output 1
Alice Bob Bob Bob Alice
For the first test case, one possible game progression is as follows:
- Initially, 2 is written on the blackboard.
- Alice chooses x = 1. Now 1 is written on the blackboard.
- Bob chooses x = 1. Now 0 is written on the blackboard.
- Alice chooses and erases one 0. Now nothing is written on the blackboard.
When both players play optimally to win, Alice wins.
For the second test case, one possible game progression is as follows:
- Initially, 1,1,1 are written on the blackboard.
- Alice chooses x = 1. Now 0,0,0 are written on the blackboard.
- Bob chooses and erases one 0. Now 0,0 are written on the blackboard.
- Alice chooses and erases one 0. Now 0 is written on the blackboard.
- Bob chooses and erases one 0. Now nothing is written on the blackboard.
When both players play optimally to win, Bob wins.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
出来るだけ少ない種類数のあみだくじを用意して、それらを組み合わせることで全ての並べ替えを作れるようにしてください。
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
正整数 N が与えられます。
正整数 m と m 個の (1,2,\dots,N) の順列を好きに決め、出力してください。このうち i 個目の順列を P_i=(P_{i,1},P_{i,2},\dots,P_{i,N}) とします。
その後、(1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) が与えられるので、以下の条件を全て満たすような正整数列 A=(A_1,A_2,\dots,A_k) を出力してください。
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
上記の問題を Q によらずに正解する上での最小の m を m_{\min} とします。あなたの出力する m は m_{\min} でなければなりません。
より厳密には
(1,2,\dots,N) の順列の列 P=(P_1,P_2,\dots,P_m) に対して、以下の条件を満たすとき P を良い順列の列と呼びます。- 任意の (1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) に対して、以下の条件を全て満たす正整数列 A=(A_1,A_2,\dots,A_k) が存在する。
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
あなたの出力する P が良い順列の列である必要がないことに注意してください。与えられた Q に対して、条件を満たす A を出力することが出来れば正解と判断されます。
制約
- 3 \le N \le 500
- Q は (1,2,\dots,N) の順列
- 入力は全て整数
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
まず、ジャッジから正整数 N が以下の形式で与えられます。
N
その後、あなたの選んだ正整数 m と m 個の (1,2,\dots,N) の順列を以下の形式で m+1 行出力してください。ただし、i 個目の順列の j 番目の要素を P_{i,j} とします。出力後、必ず改行してください。
m
P_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
\vdots
P_{m,1}\ P_{m,2}\ \dots\ P_{m,N}
ただし、以下の条件を満たす必要があります。
- m \le 1000
- (P_{i,1},P_{i,2},\dots,P_{i,N}) は (1,2,\dots,N) の順列
あなたの出力した P が上記の条件を満たさない場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。
その後、ジャッジから (1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) が以下の形式で与えられます。
Q_1\ Q_2\ \dots\ Q_N
その後、正整数列 A=(A_1,A_2,\dots,A_k) を以下の形式で出力してください。出力後、必ず改行してください。
k\ A_1\ A_2\ \dots\ A_k
以下の条件を満たすとき、またその時に限りジャッジはあなたの出力を正解と判定します。
- m = m_{\min}
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
-1を受け取ったらただちにプログラムを終了してください。終了させた場合のジャッジ結果は WA となりますが、終了しなかった場合、ジャッジ結果は不定です。- 解答を出力し終えた時もただちにプログラムを終了してください。終了しなかった場合、ジャッジ結果は不定です。
- 余計な改行は不正なフォーマットの出力とみなされるため、行わないでください。
- この問題のジャッジは適応的 (adaptive) ではありません。ジャッジプログラムは、対話前に Q を決定しています。
入出力例
以下は N=3,Q=(2,3,1) のケースです。
| 入力 | 出力 | 説明 |
|---|---|---|
3 |
N が与えられます。 | |
2
|
m = 2,P_1=(2,1,3),P_2=(3,2,1) を選び、出力します。 | |
2 3 1 |
P が条件を満たしているため、Q=(2,3,1) が与えられます。 | |
2 2 1
|
A=(2,1) は条件を満たします。R は (1,2,3) \rightarrow (3,2,1) \rightarrow (2,3,1) となり、Q に一致します。 また、N = 3 の場合 m_{\min} = 2 であり、m = m_{\min} を満たしています。 よって、この出力は正解と判定されます。 必ず k も含めて 1 行で出力してください。 |
Score : 600 points
Problem Statement
Prepare as few types of ladder lotteries as possible and combine them so that all permutations can be produced.
This is an interactive problem (in which your program and the judge program interact via input and output).
You are given a positive integer N.
Choose any positive integer m and m permutations of (1,2,\dots,N), and output them. Let the i-th permutation be P_i=(P_{i,1},P_{i,2},\dots,P_{i,N}).
Then, a permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N) is given. Output a sequence of positive integers A=(A_1,A_2,\dots,A_k) satisfying all of the following conditions.
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Let m_{\min} be the minimum value of m for which the above problem can be solved regardless of Q. Your output m must equal m_{\min}.
More precisely
For a sequence of permutations P=(P_1,P_2,\dots,P_m) of (1,2,\dots,N), we call P a good sequence of permutations if the following condition is satisfied:- For any permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N), there exists a sequence of positive integers A=(A_1,A_2,\dots,A_k) satisfying all of the following conditions.
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Note that your output P does not need to be a good sequence of permutations. Your submission is judged correct if you can output an A satisfying the conditions for the given Q.
Constraints
- 3 \le N \le 500
- Q is a permutation of (1,2,\dots,N).
- All input values are integers.
Input/Output
This is an interactive problem (in which your program and the judge program interact via input and output).
First, the judge gives you a positive integer N in the following format:
N
Then, output your chosen positive integer m and m permutations of (1,2,\dots,N) in the following format over m+1 lines. Here, the j-th element of the i-th permutation is P_{i,j}. Be sure to output a newline at the end.
m
P_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
\vdots
P_{m,1}\ P_{m,2}\ \dots\ P_{m,N}
The following conditions must be satisfied:
- m \le 1000
- (P_{i,1},P_{i,2},\dots,P_{i,N}) is a permutation of (1,2,\dots,N).
If your output P does not satisfy the above conditions, the judge outputs -1. At that point, your submission has already been judged as incorrect. The judge program terminates at that point, so it is advisable for your program to terminate as well.
Then, the judge gives you a permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N) in the following format:
Q_1\ Q_2\ \dots\ Q_N
Then, output a sequence of positive integers A=(A_1,A_2,\dots,A_k) in the following format. Be sure to output a newline at the end.
k\ A_1\ A_2\ \dots\ A_k
Your output is judged as correct if and only if the following conditions are all satisfied:
- m = m_{\min}
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Notes
- After each output, insert a newline and flush the standard output. Failure to do so may result in a judge verdict of TLE.
- If you receive
-1, terminate your program immediately. If you do, the judge verdict will be WA, but if you do not, the judge verdict will be indeterminate. - Terminate your program immediately after outputting your answer as well. Otherwise, the judge verdict will be indeterminate.
- Do not output unnecessary newlines, as they will be considered malformatted.
- The judge for this problem is not adaptive. The judge program determines Q before the interaction begins.
Sample Input/Output
The following is a case with N=3,Q=(2,3,1).
| Input | Output | Explanation |
|---|---|---|
3 |
N is given. | |
2
|
Choose m = 2,P_1=(2,1,3),P_2=(3,2,1) and output them. | |
2 3 1 |
Since P satisfies the conditions, Q=(2,3,1) is given. | |
2 2 1
|
A=(2,1) satisfies the conditions. R becomes (1,2,3) \rightarrow (3,2,1) \rightarrow (2,3,1), which matches Q. Also, m_{\min} = 2 for N = 3, and m = m_{\min} is satisfied. Thus, this output is judged as correct. Be sure to output k as well, all on one line. |
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。
ここで、整数列 x=(x_1,x_2,\dots,x_k) に対して、x のスコアを x_i < x_{i+1} を満たす i の個数と定めます。
以下のクエリを Q 回処理してください。
- 1 \le l \le r \le N を満たす正整数 l,r が与えられる。X = (P_l,P_{l+1},\dots,P_r) に対して以下の問題を解け。
- X の空でない部分列におけるスコアの最大値を M とする。X の空でない部分列のうち、スコアが M であるものの長さの最小値を求めよ。
部分列とは
数列 A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。制約
- 1 \le N,Q \le 2 \times 10^5
- P は (1,2,\dots,N) の順列
- 1 \le l \le r \le N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
P_1\ P_2\ \dots\ P_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
l\ r
出力
Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。
入力例 1
6 4 2 1 4 6 3 5 1 3 3 6 2 2 1 6
出力例 1
2 4 1 5
1 番目のクエリでは、X = (2,1,4) です。スコアの最大値 M は (2,4),(1,4),(2,1,4) によって達成される 1 です。これらの長さの最小値は (2,4),(1,4) によって達成される 2 です。
2 番目のクエリでは、X = (4,6,3,5),M = 2 です。(4,6,3,5) がスコアが 2 であるものの長さの最小値 4 を達成します。
3 番目のクエリでは、X = (1),M = 0 です。(1) がスコアが 0 であるものの長さの最小値 1 を達成します。
4 番目のクエリでは、X = (2,1,4,6,3,5),M = 3 です。(2,4,6,3,5) がスコアが 3 であるものの長さの最小値 5 を達成します。
入力例 2
12 8 8 3 5 7 9 6 11 1 10 4 12 2 3 4 10 11 5 8 3 8 4 10 2 10 5 7 1 8
出力例 2
2 2 2 4 5 7 2 5
Score : 700 points
Problem Statement
You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).
For a sequence of integers x=(x_1,x_2,\dots,x_k), define the score of x as the number of indices i satisfying x_i < x_{i+1}.
Process the following query Q times.
- You are given positive integers l and r with 1 \le l \le r \le N. For X = (P_l,P_{l+1},\dots,P_r), solve the following problem.
- Let M be the maximum score of a non-empty subsequence of X. Find the minimum length of a non-empty subsequence of X whose score equals M.
What is a subsequence?
A subsequence of a sequence A is a sequence obtained by removing zero or more elements from A and arranging the remaining elements in their original order.Constraints
- 1 \le N,Q \le 2 \times 10^5
- P is a permutation of (1,2,\dots,N).
- 1 \le l \le r \le N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
P_1\ P_2\ \dots\ P_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
l\ r
Output
Output Q lines. The i-th line should contain the answer to \mathrm{query}_i.
Sample Input 1
6 4 2 1 4 6 3 5 1 3 3 6 2 2 1 6
Sample Output 1
2 4 1 5
For the first query, X = (2,1,4). The maximum score M = 1 is achieved by (2,4),(1,4),(2,1,4). The minimum length among these is 2, achieved by (2,4),(1,4).
For the second query, X = (4,6,3,5) and M = 2. (4,6,3,5) achieves the minimum length 4 among those with score 2.
For the third query, X = (1) and M = 0. (1) achieves the minimum length 1 with score 0.
For the fourth query, X = (2,1,4,6,3,5) and M = 3. (2,4,6,3,5) achieves the minimum length 5 with score 3.
Sample Input 2
12 8 8 3 5 7 9 6 11 1 10 4 12 2 3 4 10 11 5 8 3 8 4 10 2 10 5 7 1 8
Sample Output 2
2 2 2 4 5 7 2 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 800 点
問題文
(1,2,\dots,N) の順列 p=(p_1,p_2,\dots,p_N) と正整数 M に対して、以下の問題の答えを f(p,M) と置きます。
p に対して以下の操作を M 回行います。
- 1 \le i \le N-1 を満たす整数 i を選び、(p_1,p_2,\dots,p_i) と (p_{i+1},p_{i+2},\dots,p_N) をそれぞれ反転する。形式的には、p を (p_i,p_{i-1},\dots,p_1,p_N,p_{N-1},\dots,p_{i+1}) に置き換える。
操作列としてあり得るものは (N-1)^M 通りありますが、その全てに対する「M 回の操作終了後の p の転倒数」の総和を 998244353 で割った余りを求めてください。
(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。以下のクエリを Q 回処理してください。
- 1 \le x \le N-1 を満たす整数 x と正整数 K が与えられる。P_x,P_{x+1} を swap する。その後、f(P,K) を求めよ。
制約
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- P は (1,2,\dots,N) の順列
- 1 \le x \le N-1
- 1 \le K \le 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
P_1\ P_2\ \dots\ P_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
x K
出力
Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。
入力例 1
3 2 1 3 2 1 1 2 1
出力例 1
4 4
1 番目のクエリについて、P_1,P_2 を swap することで P=(3,1,2) となります。操作列は 2 通りあり、それぞれ以下のようになります。
- i = 1 を選ぶ。P=(3,2,1) となる。転倒数は 3 である。
- i = 2 を選ぶ。P=(1,3,2) となる。転倒数は 1 である。
よって答えは 3+1=4 です。
2 番目のクエリについて、P_2,P_3 を swap することで P=(3,2,1) となります。操作列は 2 通りあり、それぞれ以下のようになります。
- i = 1 を選ぶ。P=(3,1,2) となる。転倒数は 2 である。
- i = 2 を選ぶ。P=(2,3,1) となる。転倒数は 2 である。
よって答えは 2+2=4 です。
入力例 2
4 4 3 2 4 1 2 1 2 2 3 3 1 4
出力例 2
11 28 67 242
入力例 3
10 7 7 9 3 10 5 2 4 6 8 1 2 29 1 86 3 30 8 64 1 24 1 9 5 55
出力例 3
29362950 633265500 847469581 741165544 385334408 653522086 169485402
Score : 800 points
Problem Statement
For a permutation p=(p_1,p_2,\dots,p_N) of (1,2,\dots,N) and a positive integer M, let f(p,M) denote the answer to the following problem.
Perform the following operation M times on p.
- Choose an integer i with 1 \le i \le N-1, and reverse each of (p_1,p_2,\dots,p_i) and (p_{i+1},p_{i+2},\dots,p_N). Formally, replace p with (p_i,p_{i-1},\dots,p_1,p_N,p_{N-1},\dots,p_{i+1}).
There are (N-1)^M possible sequences of operations. Find the sum, modulo 998244353, of "the number of inversions of p after M operations" over all such sequences.
You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N). Process the following query Q times.
- You are given an integer x with 1 \le x \le N-1 and a positive integer K. Swap P_x and P_{x+1}. Then, find f(P,K).
Constraints
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- P is a permutation of (1,2,\dots,N).
- 1 \le x \le N-1
- 1 \le K \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
P_1\ P_2\ \dots\ P_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
x K
Output
Output Q lines. The i-th line should contain the answer to \mathrm{query}_i.
Sample Input 1
3 2 1 3 2 1 1 2 1
Sample Output 1
4 4
For the first query, swapping P_1 and P_2 gives P=(3,1,2). There are two possible sequences of operations as follows:
- Choose i = 1. P becomes (3,2,1). The number of inversions is 3.
- Choose i = 2. P becomes (1,3,2). The number of inversions is 1.
Thus, the answer is 3+1=4.
For the second query, swapping P_2 and P_3 gives P=(3,2,1). There are two possible sequences of operations as follows:
- Choose i = 1. P becomes (3,1,2). The number of inversions is 2.
- Choose i = 2. P becomes (2,3,1). The number of inversions is 2.
Thus, the answer is 2+2=4.
Sample Input 2
4 4 3 2 4 1 2 1 2 2 3 3 1 4
Sample Output 2
11 28 67 242
Sample Input 3
10 7 7 9 3 10 5 2 4 6 8 1 2 29 1 86 3 30 8 64 1 24 1 9 5 55
Sample Output 3
29362950 633265500 847469581 741165544 385334408 653522086 169485402
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
F 問題と F2 問題は同じ問題ですが、M の制約が異なります。F 問題では 1 \le M \le 4 です。
正整数 M が与えられます。ここで、以下の問題を考えます。
Bucket正整数 N と長さ N の非負整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) が与えられます。A,B の要素は全て 0 以上 M 以下です。
1 から N の番号が付いた N 個のバケツがあります。全てのバケツには水が M L まで入ります。始め、バケツ i には A_i L の水が入っています。
あなたは以下の操作を好きな回数行うことが出来ます。
- 異なる 2 個のバケツ i,j を選ぶ。以下の条件を両方満たしている間、バケツ i からバケツ j に水を移し続ける。
- バケツ i にまだ水が残っている。
- バケツ j に入っている水の量が M L 未満である。
あなたの目標は、全ての i についてバケツ i に水が B_i L 入っている状況にすることです。目標を達成することが出来るか判定してください。
M+1 行 M+1 列の非負整数行列 X=(X_{i,j})(0 \le i,j \le M) が与えられます。以下のクエリを Q 回処理してください。
- 0 \le i,j \le M を満たす非負整数 i,j,Y が与えられる。X_{i,j} を Y に変更する。その後、以下の手順で非負整数列 A,B を得る。
- 非負整数列 A,B を空数列で初期化する。
- 以下を i = 0,1,\dots,M の順に行う。
- 以下を j = 0,1,\dots,M の順に行う。
- A の末尾に i を、B の末尾に j を追加する操作を X_{i,j} 回行う。
- 長さ N = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} の非負整数列 A,B について Bucket を解いたときの答えを求めよ。
制約
- 1 \le M \le 4
- 1 \le Q \le 10^6
- 0 \le X_{i,j},Y \le 10^{17}
- 0 \le i,j \le M
- 全ての時点で \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
M Q
X_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
\vdots
X_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
i\ j\ Y
出力
Q 行出力せよ。i 行目には \mathrm{query}_i において、目標を達成することが出来るならば Yes を、出来ないならば No を出力せよ。
入力例 1
3 3 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 2 3 1 2 1 1
出力例 1
Yes No Yes
1 番目のクエリによって、X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} となり、A=(1,1,2),B=(2,2,0) が得られます。
この場合、例えば以下のような操作列によって目標を達成できます。
- i = 1,j = 2 とする。各バケツに入っている水の量が (1,1,2) から (0,2,2) になる。
- i = 3,j = 1 とする。各バケツに入っている水の量が (0,2,2) から (2,2,0) になる。
2 番目のクエリによって、X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} となり、A=(1,1,2,2),B=(2,2,0,3) が得られます。
この場合、どのように操作をしても目標を達成することは出来ません。
3 番目のクエリによって、X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} となり、A=(1,1,2,2,2),B=(2,2,0,1,3) が得られます。
この場合、例えば以下のような操作列によって目標を達成できます。
- i = 1,j = 2 とする。各バケツに入っている水の量が (1,1,2,2,2) から (0,2,2,2,2) になる。
- i = 3,j = 1 とする。各バケツに入っている水の量が (0,2,2,2,2) から (2,2,0,2,2) になる。
- i = 4,j = 5 とする。各バケツに入っている水の量が (2,2,0,2,2) から (2,2,0,1,3) になる。
入力例 2
4 10 45636788580181785 16131322312654301 43477244591521823 6505049084010674 86530627327096446 95921187347997793 55491565467039163 87684565747362311 80318628430974482 12308092878301956 75570615154690027 96403707363045776 14150012766408204 6612197700307407 64417022692908525 5530468643826479 41731276604630756 15675296751519388 59461896803210859 66666666666666666 72767956047192820 18258893791516726 58852629621892634 33333333333333333 29923985408775019 2 1 26541245644686826 2 4 29485791833729050 4 1 21832826336874318 3 2 4953499115446612 2 0 69217973349997921 2 4 23133150029036944 3 4 55834224798559242 1 2 98517007615469735 2 3 3768060024403703 0 4 87241661746072372
出力例 2
No Yes No Yes No Yes No No No No
Score : 700 points
Problem Statement
Problems F and F2 are the same problem with different constraints on M. In Problem F, 1 \le M \le 4.
You are given a positive integer M. Consider the following problem.
BucketYou are given a positive integer N and non-negative integer sequences A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N) of length N. All elements of A and B are between 0 and M, inclusive.
There are N buckets numbered 1 to N. Each bucket can hold up to M liters of water. Initially, bucket i contains A_i liters of water.
You may perform the following operation any number of times.
- Choose two distinct buckets i and j. Continue pouring water from bucket i into bucket j as long as both of the following conditions are satisfied:
- Bucket i still has water remaining.
- The amount of water in bucket j is less than M liters.
Your goal is to have exactly B_i liters of water in bucket i for all i. Determine whether the goal can be achieved.
You are given a non-negative integer matrix X=(X_{i,j})(0 \le i,j \le M) of (M+1) rows and (M+1) columns. Process the following queries Q times.
- You are given non-negative integers i,j,Y with 0 \le i,j \le M. Change X_{i,j} to Y. Then, obtain non-negative integer sequences A and B by the following procedure.
- Initialize non-negative integer sequences A and B as empty sequences.
- For i = 0,1,\dots,M in this order:
- For j = 0,1,\dots,M in this order:
- Do this X_{i,j} times: append i to the end of A and j to the end of B.
- Solve Bucket for the non-negative integer sequences A and B of length N = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j}.
Constraints
- 1 \le M \le 4
- 1 \le Q \le 10^6
- 0 \le X_{i,j},Y \le 10^{17}
- 0 \le i,j \le M
- \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1 at any time.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
M Q
X_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
\vdots
X_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
i\ j\ Y
Output
Output Q lines. The i-th line should contain Yes if the goal can be achieved in \mathrm{query}_i, and No otherwise.
Sample Input 1
3 3 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 2 3 1 2 1 1
Sample Output 1
Yes No Yes
The first query makes X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} , and we obtain A=(1,1,2),B=(2,2,0).
In this case, the goal can be achieved by the following sequence of operations, for example.
- Set i = 1,j = 2. The amount of water in each bucket changes from (1,1,2) to (0,2,2).
- Set i = 3,j = 1. The amount of water in each bucket changes from (0,2,2) to (2,2,0).
The second query makes X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} , and we obtain A=(1,1,2,2),B=(2,2,0,3).
In this case, the goal cannot be achieved no matter how operations are performed.
The third query makes X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix} , and we obtain A=(1,1,2,2,2),B=(2,2,0,1,3).
In this case, the goal can be achieved by the following sequence of operations, for example.
- Set i = 1,j = 2. The amount of water in each bucket changes from (1,1,2,2,2) to (0,2,2,2,2).
- Set i = 3,j = 1. The amount of water in each bucket changes from (0,2,2,2,2) to (2,2,0,2,2).
- Set i = 4,j = 5. The amount of water in each bucket changes from (2,2,0,2,2) to (2,2,0,1,3).
Sample Input 2
4 10 45636788580181785 16131322312654301 43477244591521823 6505049084010674 86530627327096446 95921187347997793 55491565467039163 87684565747362311 80318628430974482 12308092878301956 75570615154690027 96403707363045776 14150012766408204 6612197700307407 64417022692908525 5530468643826479 41731276604630756 15675296751519388 59461896803210859 66666666666666666 72767956047192820 18258893791516726 58852629621892634 33333333333333333 29923985408775019 2 1 26541245644686826 2 4 29485791833729050 4 1 21832826336874318 3 2 4953499115446612 2 0 69217973349997921 2 4 23133150029036944 3 4 55834224798559242 1 2 98517007615469735 2 3 3768060024403703 0 4 87241661746072372
Sample Output 2
No Yes No Yes No Yes No No No No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
F 問題と F2 問題は同じ問題ですが、M の制約が異なります。F2 問題では M = 5 です。
正整数 M が与えられます。ここで、以下の問題を考えます。
Bucket正整数 N と長さ N の非負整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) が与えられます。A,B の要素は全て 0 以上 M 以下です。
1 から N の番号が付いた N 個のバケツがあります。全てのバケツには水が M L まで入ります。始め、バケツ i には A_i L の水が入っています。
あなたは以下の操作を好きな回数行うことが出来ます。
- 異なる 2 個のバケツ i,j を選ぶ。以下の条件を両方満たしている間、バケツ i からバケツ j に水を移し続ける。
- バケツ i にまだ水が残っている。
- バケツ j に入っている水の量が M L 未満である。
あなたの目標は、全ての i についてバケツ i に水が B_i L 入っている状況にすることです。目標を達成することが出来るか判定してください。
M+1 行 M+1 列の非負整数行列 X=(X_{i,j})(0 \le i,j \le M) が与えられます。以下のクエリを Q 回処理してください。
- 0 \le i,j \le M を満たす非負整数 i,j,Y が与えられる。X_{i,j} を Y に変更する。その後、以下の手順で非負整数列 A,B を得る。
- 非負整数列 A,B を空数列で初期化する。
- 以下を i = 0,1,\dots,M の順に行う。
- 以下を j = 0,1,\dots,M の順に行う。
- A の末尾に i を、B の末尾に j を追加する操作を X_{i,j} 回行う。
- 長さ N = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} の非負整数列 A,B について Bucket を解いたときの答えを求めよ。
制約
- M = 5
- 1 \le Q \le 10^6
- 0 \le X_{i,j},Y \le 10^{17}
- 0 \le i,j \le M
- 全ての時点で \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
M Q
X_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
\vdots
X_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
i\ j\ Y
出力
Q 行出力せよ。i 行目には \mathrm{query}_i において、目標を達成することが出来るならば Yes を、出来ないならば No を出力せよ。
入力例 1
5 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 4 0 1 1 5 1
出力例 1
Yes No Yes
それぞれのクエリ時点で得られる A,B は以下の通りです。
- 1 番目のクエリを処理する時点では、A=(0,3,3),B=(0,1,5)
- 2 番目のクエリを処理する時点では、A=(0,3,3,4),B=(0,1,5,0)
- 3 番目のクエリを処理する時点では、A=(0,1,3,3,4),B=(0,5,1,5,0)
入力例 2
5 10 61717749303507807 83849626163233111 84388501405824055 8514730661576685 8408052512772637 42085112989954358 33333333333333333 70477132070314067 43546706335438313 61370380435458585 99101823689638606 77669930608921552 33333333333333333 21459633523757247 8151410984806542 14183403125185219 68299186110683565 35549692732468863 98146856836553017 53630682913685434 12400422817799555 29967281381593348 67521547136428867 48353536740933612 97356055491517734 55777507072845580 59235925940735000 6228770338558507 41778108608223669 60544364859700647 5960356361755846 59147828067221624 65687376011190687 33333333333333333 31563848832259443 98724011871535047 2 3 1333175914347793 3 4 80371774347266293 2 1 20920094361040152 2 3 793636751630698 0 5 68209364995103050 2 0 98643963346205063 3 2 7931291059115265 0 1 79380494404548821 2 5 6440038070576840 2 3 88122600737306767
出力例 2
No No No No No Yes No Yes No Yes
Score : 300 points
Problem Statement
Problems F and F2 are the same problem with different constraints on M. In Problem F2, M = 5.
You are given a positive integer M. Consider the following problem.
BucketYou are given a positive integer N and non-negative integer sequences A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N) of length N. All elements of A and B are between 0 and M, inclusive.
There are N buckets numbered 1 to N. Each bucket can hold up to M liters of water. Initially, bucket i contains A_i liters of water.
You may perform the following operation any number of times.
- Choose two distinct buckets i and j. Continue pouring water from bucket i into bucket j as long as both of the following conditions are satisfied:
- Bucket i still has water remaining.
- The amount of water in bucket j is less than M liters.
Your goal is to have exactly B_i liters of water in bucket i for all i. Determine whether the goal can be achieved.
You are given a non-negative integer matrix X=(X_{i,j})(0 \le i,j \le M) of (M+1) rows and (M+1) columns. Process the following queries Q times.
- You are given non-negative integers i,j,Y with 0 \le i,j \le M. Change X_{i,j} to Y. Then, obtain non-negative integer sequences A and B by the following procedure.
- Initialize non-negative integer sequences A and B as empty sequences.
- For i = 0,1,\dots,M in this order:
- For j = 0,1,\dots,M in this order:
- Do this X_{i,j} times: append i to the end of A and j to the end of B.
- Solve Bucket for the non-negative integer sequences A and B of length N = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j}.
Constraints
- M = 5
- 1 \le Q \le 10^6
- 0 \le X_{i,j},Y \le 10^{17}
- 0 \le i,j \le M
- \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1 at any time.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
M Q
X_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
\vdots
X_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
i\ j\ Y
Output
Output Q lines. The i-th line should contain Yes if the goal can be achieved in \mathrm{query}_i, and No otherwise.
Sample Input 1
5 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 4 0 1 1 5 1
Sample Output 1
Yes No Yes
The sequences A and B obtained at each query are as follows:
- At the time of processing the first query: A=(0,3,3),B=(0,1,5)
- At the time of processing the second query: A=(0,3,3,4),B=(0,1,5,0)
- At the time of processing the third query: A=(0,1,3,3,4),B=(0,5,1,5,0)
Sample Input 2
5 10 61717749303507807 83849626163233111 84388501405824055 8514730661576685 8408052512772637 42085112989954358 33333333333333333 70477132070314067 43546706335438313 61370380435458585 99101823689638606 77669930608921552 33333333333333333 21459633523757247 8151410984806542 14183403125185219 68299186110683565 35549692732468863 98146856836553017 53630682913685434 12400422817799555 29967281381593348 67521547136428867 48353536740933612 97356055491517734 55777507072845580 59235925940735000 6228770338558507 41778108608223669 60544364859700647 5960356361755846 59147828067221624 65687376011190687 33333333333333333 31563848832259443 98724011871535047 2 3 1333175914347793 3 4 80371774347266293 2 1 20920094361040152 2 3 793636751630698 0 5 68209364995103050 2 0 98643963346205063 3 2 7931291059115265 0 1 79380494404548821 2 5 6440038070576840 2 3 88122600737306767
Sample Output 2
No No No No No Yes No Yes No Yes