実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
正整数 N が与えられます。
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) であって、 \displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j を最小化するものを一つ求めてください。
ただし、 \displaystyle \bigoplus_{1\le j\le i} P_j は P_1,P_2,\ldots,P_i のビット単位 \mathrm{XOR} として定義されます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
ビット単位 \mathrm{XOR} 演算とは
非負整数 A, B のビット単位 \mathrm{XOR} 、A \oplus B は、以下のように定義されます。
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。
制約
- 1\le T\le 10^3
- 1\le N
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、\displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j を最小化する (1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) を空白区切りで出力せよ。
最小化する P が複数存在する場合、どれを出力しても正答となる。
入力例 1
3 3 1 7
出力例 1
1 3 2 1 4 5 3 2 6 7 1
1 番目のテストケースについて考えます。
P=(1,3,2) とすると、\displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j=1 + (1 \oplus 3) + (1 \oplus 3 \oplus 2) = 1+2+0=3 となります。
\displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j の値を 3 未満にすることはできないので、P=(1,3,2) を出力すると正答となります。
他にも P=(2,3,1) を出力しても正答となります。
Score : 500 points
Problem Statement
You are given a positive integer N.
Find one permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) that minimizes \displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j.
Here, \displaystyle \bigoplus_{1\le j\le i} P_j is defined as the bitwise \mathrm{XOR} of P_1,P_2,\ldots,P_i.
You are given T test cases; solve each of them.
What is bitwise \mathrm{XOR}?
The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows.
- In the binary representation of A \oplus B, the digit at the 2^k (k \geq 0) place is 1 if exactly one of the digits at the 2^k place in the binary representations of A and B is 1, and 0 otherwise.
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.
Constraints
- 1\le T\le 10^3
- 1\le N
- 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
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, output a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) that minimizes \displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j, separated by spaces.
If there are multiple permutations P that achieve the minimum, any of them will be accepted.
Sample Input 1
3 3 1 7
Sample Output 1
1 3 2 1 4 5 3 2 6 7 1
Consider the first test case.
If P=(1,3,2), then \displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j=1 + (1 \oplus 3) + (1 \oplus 3 \oplus 2) = 1+2+0=3.
The value of \displaystyle \sum_{i=1}^N \bigoplus_{1\le j\le i} P_j cannot be made less than 3, so outputting P=(1,3,2) is correct.
Outputting P=(2,3,1) is also correct.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
整数 N,K と長さ K の整数列 A=(A_1,A_2,\ldots,A_K) が与えられます。A の各要素は 1 以上 N 以下で相異なることが保証されます。
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) に対して f(P) を以下のように定義します。
- P に対して以下の操作を行うことのできる回数の最大値。
- P_i < \max(P_1,P_2,\ldots,P_{i-1}) を満たす 2\le i\le N を選び、P_i を先頭に移動させる。つまり、P を (P_i,P_1,P_2,\ldots,P_{i-1},P_{i+1},\ldots,P_N) で置き換える。
i=1,2,\ldots,K に対し P_i=A_i を満たす (1,2,\ldots,N) の順列 P は (N-K)! 通りありますが、それら全てに対する f(P) の総和を 998244353 で割ったあまりを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 10^5
- 1\le K\le N
- 全てのテストケースにおける N の総和は 5\times 10^5 以下
- 1\le A_i\le N
- A の各要素は相異なる
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K A_1 A_2 \ldots A_K
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
3 3 1 1 4 2 3 2 10 3 2 1 7
出力例 1
2 6 1382640
1 番目のテストケースについて考えます。
i=1,2,\ldots,K に対し P_i=A_i を満たす (1,2,\ldots,N) の順列 P は P=(1,2,3),(1,3,2) の 2 つです。
P=(1,2,3) のとき、操作を 1 回も行うことができないので f(P)=0 です。
P=(1,3,2) のとき、以下のようにすることで操作を 2 回行うことができます。
- i=3 を選ぶ。P=(2,1,3) となる。
- i=2 を選ぶ。P=(1,2,3) となる。
3 回以上操作することはできないので、この場合は f(P)=2 です。
以上より、求める答えは 0+2=2 となります。
Score : 700 points
Problem Statement
You are given integers N,K and an integer sequence A=(A_1,A_2,\ldots,A_K) of length K. It is guaranteed that each element of A is between 1 and N, inclusive, and all elements are distinct.
For a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N), define f(P) as follows.
- The maximum number of times the following operation can be performed on P.
- Choose 2\le i\le N satisfying P_i < \max(P_1,P_2,\ldots,P_{i-1}), and move P_i to the front. That is, replace P with (P_i,P_1,P_2,\ldots,P_{i-1},P_{i+1},\ldots,P_N).
There are (N-K)! permutations P of (1,2,\ldots,N) satisfying P_i=A_i for i=1,2,\ldots,K. Find the sum, modulo 998244353, of f(P) over all such permutations.
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 10^5
- 1\le K\le N
- The sum of N over all test cases is at most 5\times 10^5.
- 1\le A_i\le N
- All elements of A are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K A_1 A_2 \ldots A_K
Output
Output the answer for the test cases in order, separated by newlines.
Sample Input 1
3 3 1 1 4 2 3 2 10 3 2 1 7
Sample Output 1
2 6 1382640
Consider the first test case.
The permutations P of (1,2,\ldots,N) satisfying P_i=A_i for i=1,2,\ldots,K are P=(1,2,3) and P=(1,3,2), giving two permutations.
When P=(1,2,3), no operation can be performed, so f(P)=0.
When P=(1,3,2), the operation can be performed twice as follows.
- Choose i=3. Now P=(2,1,3).
- Choose i=2. Now P=(1,2,3).
The operation cannot be performed three or more times, so f(P)=2 in this case.
Thus, the answer is 0+2=2.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
AtCoder 商店には N 個の品物があります。i 番目の品物は A_i 円です。
AtCoder 商店に N 人の人が順番にやってきます。各人の所持金は C 円で、以下の手続きを行います。
- 買い物に使う予算として、1 以上 C 以下の整数 x を一様ランダムに選ぶ。
- AtCoder 商店に残っている品物の中に x 円以下のものが存在すれば、その中で最も高価なものを 1 つ購入する。存在しない場合は何も購入せず店を去る。
AtCoder 商店の経営者であるあなたは、品物が何個売れるか知りたくなりました。k=0,1,2,\ldots,N について、最終的に品物がちょうど k 個売れる確率を \ \text{mod}\ 998244353 で求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
確率 \ \text{mod}\ 998244353 の定義
求める確率は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、求める有理数を既約分数 \displaystyle \frac{P}{Q} で表した時、Q \neq 0 \bmod 998244353 となることが証明できます。 よって、R \times Q \equiv P \bmod 998244353, 0 \leq R \lt 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。
制約
- 1\le T
- 1\le N
- 全てのテストケースにおける N の総和は 100 以下
- 1\le A_i \le C < 998244353
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N C A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、k=0,1,\ldots,N に対する答えを順に空白区切りで出力せよ。
入力例 1
4 2 3 1 3 3 17 9 9 8 5 2025 1 1 1 1 1 6 1000 544 105 450 715 479 992
出力例 1
0 776412275 221832079 465698363 588015298 487439081 455335965 0 0 0 0 0 1 366062443 766314649 169448288 553531286 643499511 890090646 604030590
1 番目のテストケースについて考えます。
例えば各人の手続きは以下のように進行します。
- 1 人目:x=2 を選ぶ。2 円以下で最も値段が高い品物は 1 番目の品物なので、1 番目の品物を購入する。
- 2 人目:x=1 を選ぶ。1 円以下の品物は存在しないので、何も購入せず店を去る。
この場合、最終的に品物はちょうど 1 個売れます。
品物がちょうど 0,1,2 個売れる確率はそれぞれ \displaystyle 0, \frac49,\frac59 です。
Score : 700 points
Problem Statement
AtCoder Store has N items. The i-th item costs A_i yen.
N people visit the store one by one. Each person has C yen and performs the following procedure.
- Choose an integer x uniformly at random between 1 and C, inclusive, as the budget for shopping.
- If there is an item remaining in the store that costs at most x yen, purchase one of the most expensive ones among them. Otherwise, leave the store without purchasing anything.
As the owner of the store, you want to know how many items will be sold. For k=0,1,2,\ldots,N, find the probability, modulo 998244353, that exactly k items are sold in the end.
You are given T test cases; solve each of them.
Definition of probability \ \text{mod}\ 998244353
It can be proved that the probabilities to be found are always rational numbers. Moreover, under the constraints of this problem, when each such rational number is expressed as an irreducible fraction \displaystyle \frac{P}{Q}, it can be proved that Q \neq 0 \bmod 998244353. Thus, there is a unique integer R satisfying R \times Q \equiv P \bmod 998244353, 0 \leq R \lt 998244353. Find this R.
Constraints
- 1\le T
- 1\le N
- The sum of N over all test cases is at most 100.
- 1\le A_i \le C < 998244353
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N C A_1 A_2 \ldots A_N
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, output the answers for k=0,1,\ldots,N in order, separated by spaces.
Sample Input 1
4 2 3 1 3 3 17 9 9 8 5 2025 1 1 1 1 1 6 1000 544 105 450 715 479 992
Sample Output 1
0 776412275 221832079 465698363 588015298 487439081 455335965 0 0 0 0 0 1 366062443 766314649 169448288 553531286 643499511 890090646 604030590
Consider the first test case.
For example, the procedure for each person proceeds as follows.
- Person 1: Chooses x=2. The most expensive item that costs at most 2 yen is the first item, so they purchase the first item.
- Person 2: Chooses x=1. There are no items costing at most 1 yen, so they leave without purchasing anything.
In this case, exactly one item is sold in the end.
The probabilities that exactly 0,1,2 items are sold are \displaystyle 0, \frac49,\frac59, respectively.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
正整数 N,M と長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
正整数 k に対し、f(k) を以下の問題の答えとして定義します。
高橋君はお金を k 円持っています。また、AtCoder 商店には N 個の品物があり、i 番目の品物は A_i 円です。
高橋君は、i=1,2,\ldots,N の順に以下の行動を行います。
- 今持っているお金が A_i 円以上ならば i 番目の品物を購入する。
高橋君が買った品物の値段の合計を求めてください。
\displaystyle \bigoplus_{1\le k\le M} (k\times f(k)) を求めてください。ただし、\displaystyle \bigoplus_{1\le k\le M} (k\times f(k)) は 1\times f(1), 2\times f(2), \ldots, M\times f(M) のビット単位 \mathrm{XOR} として定義されます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
ビット単位 \mathrm{XOR} 演算とは
非負整数 A, B のビット単位 \mathrm{XOR} 、A \oplus B は、以下のように定義されます。
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。
制約
- 1\le T \le 10
- 1\le N,M
- 全てのテストケースにおける N の総和は 5\times 10^5 以下
- 全てのテストケースにおける M の総和は 5\times 10^7 以下
- 1\le A_i \le M
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N M A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
3 3 6 2 3 1 5 10 3 1 4 1 5 7 12 4 2 3 4 2 3 3
出力例 1
61 115 190
1 番目のテストケースについて考えます。
例えば高橋君の最初の所持金が 3 円の場合、高橋君は以下のように行動します。
- i=1 のとき:高橋君の所持金は 3 円であり、2 円以上なので 1 番目の品物を買う。
- i=2 のとき:高橋君の所持金は 1 円であり、3 円以上ではないので 2 番目の品物を買わない。
- i=3 のとき:高橋君の所持金は 1 円であり、1 円以上なので 3 番目の品物を買う。
これより f(3)=2+1=3 が分かります。
f(1),f(2),f(3),f(4),f(5),f(6) の値はそれぞれ 1,2,3,3,5,6 です。したがって、1,4,9,12,25,36 のビット単位 \mathrm{XOR} である 61 が答えになります。
Score : 700 points
Problem Statement
You are given positive integers N,M and a sequence of positive integers A=(A_1,A_2,\ldots,A_N) of length N.
For a positive integer k, define f(k) as the answer to the following problem.
Takahashi has k yen. AtCoder Store has N items, and the i-th item costs A_i yen.
He performs the following action for i=1,2,\ldots,N in order.
- If the amount of money he currently has is at least A_i yen, he purchases the i-th item.
Find the total price of the items he purchased.
Find \displaystyle \bigoplus_{1\le k\le M} (k\times f(k)). Here, \displaystyle \bigoplus_{1\le k\le M} (k\times f(k)) is defined as the bitwise \mathrm{XOR} of 1\times f(1), 2\times f(2), \ldots, M\times f(M).
You are given T test cases; solve each of them.
What is bitwise \mathrm{XOR}?
The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows.
- In the binary representation of A \oplus B, the digit at the 2^k (k \geq 0) place is 1 if exactly one of the digits at the 2^k place in the binary representations of A and B is 1, and 0 otherwise.
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.
Constraints
- 1\le T \le 10
- 1\le N,M
- The sum of N over all test cases is at most 5\times 10^5.
- The sum of M over all test cases is at most 5\times 10^7.
- 1\le A_i \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N M A_1 A_2 \ldots A_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
3 3 6 2 3 1 5 10 3 1 4 1 5 7 12 4 2 3 4 2 3 3
Sample Output 1
61 115 190
Consider the first test case.
For example, when Takahashi's initial amount of money is 3 yen, he acts as follows.
- For i=1: he has 3 yen, which is at least 2 yen, so he purchases the first item.
- For i=2: he has 1 yen, which is less than 3 yen, so he does not purchase the second item.
- For i=3: he has 1 yen, which is at least 1 yen, so he purchases the third item.
From this, we get f(3)=2+1=3.
The values of f(1),f(2),f(3),f(4),f(5),f(6) are 1,2,3,3,5,6, respectively. Thus, the answer is the bitwise \mathrm{XOR} of 1,4,9,12,25,36, which is 61.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 900 点
問題文
頂点に 1 から N までの番号がついた N 頂点の木が与えられます。ここで N は 2 以上です。i 番目 (1\le i\le N-1) の辺は頂点 u_i と頂点 v_i を相互に結んでいます。
この木に対して操作をちょうど K 回行います。k 回目 (1\le k\le K) の操作は以下の通りです。
- その時点での木の辺 N-2+k 本の中から、一本の辺を一様ランダムに選ぶ。選んだ辺が結ぶ頂点を u,v として、新たに頂点 N+k を用意し、頂点 u,N+k を結ぶ辺、頂点 v,N+k を結ぶ辺を追加する。その後、選んだ辺を削除する。
k=1,2,\ldots,K に対し、k 回目の操作後に、与えられた木は N+k 頂点の木になっていることに注意してください。
K 回の操作後の \displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) の期待値を \text{mod}\ 998244353 で求めてください。ただし、\text{dist}(i,j) で木の頂点 i と頂点 j の距離を表します。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
期待値 \ \text{mod}\ 998244353 の定義
求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、求める有理数を既約分数 \displaystyle \frac{P}{Q} で表した時、Q \neq 0 \bmod 998244353 となることが証明できます。 よって、R \times Q \equiv P \bmod 998244353, 0 \leq R \lt 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。
制約
- 1\le T
- 2\le N
- 全てのテストケースにおける N の総和は 3\times 10^5 以下
- 1\le K
- 全てのテストケースにおける K の総和は 10^6 以下
- 1\le u_i < v_i \le N
- 与えられるグラフは木
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
3 4 2 1 2 1 3 1 4 4 5 1 2 2 3 3 4 6 13 1 2 1 3 2 4 2 5 3 6
出力例 1
499122208 120 713032723
1 番目のテストケースについて考えます。
例えば 2 回の操作は以下のように進行します。
- 1 回目:頂点 1 と頂点 2 を結ぶ辺が選ばれる。頂点 1 と頂点 5 を結ぶ辺、頂点 2 と頂点 5 を結ぶ辺が追加された後、頂点 1 と頂点 2 を結ぶ辺が削除される。
- 2 回目:頂点 2 と頂点 5 を結ぶ辺が選ばれる。頂点 2 と頂点 6 を結ぶ辺、頂点 5 と頂点 6 を結ぶ辺が追加された後、頂点 2 と頂点 5 を結ぶ辺が削除される。
この場合の \displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) の値は 32 です。
\displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) の値は \displaystyle \frac12 の確率で 31 に、\displaystyle \frac12 の確率で 32 になります。
Score : 900 points
Problem Statement
You are given a tree with N vertices numbered 1 to N. Here, N is at least 2. The i-th edge (1\le i\le N-1) connects vertices u_i and v_i.
Perform the following operation on this tree exactly K times. The k-th operation (1\le k\le K) is as follows.
- Choose one of the N-2+k edges of the tree at that point uniformly at random. Let u and v be the vertices connected by the chosen edge. Prepare a new vertex N+k, add edges connecting vertices u and N+k and connecting vertices v and N+k, then delete the chosen edge.
Note that for k=1,2,\ldots,K, the tree will have N+k vertices after the k-th operation.
Find the expected value, modulo 998244353, of \displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) after K operations. Here, \text{dist}(i,j) denotes the distance between vertices i and j in the tree.
You are given T test cases; solve each of them.
Definition of expected value \ \text{mod}\ 998244353
It can be proved that the expected value to be found is always a rational number. Moreover, under the constraints of this problem, when this rational number is expressed as an irreducible fraction \displaystyle \frac{P}{Q}, it can be proved that Q \neq 0 \bmod 998244353. Thus, there is a unique integer R satisfying R \times Q \equiv P \bmod 998244353, 0 \leq R \lt 998244353. Find this R.
Constraints
- 1\le T
- 2\le N
- The sum of N over all test cases is at most 3\times 10^5.
- 1\le K
- The sum of K over all test cases is at most 10^6.
- 1\le u_i < v_i \le N
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
3 4 2 1 2 1 3 1 4 4 5 1 2 2 3 3 4 6 13 1 2 1 3 2 4 2 5 3 6
Sample Output 1
499122208 120 713032723
Consider the first test case.
For example, the two operations proceed as follows.
- Operation 1: The edge connecting vertices 1 and 2 is chosen. After adding edges connecting vertices 1 and 5 and connecting vertices 2 and 5, the edge connecting vertices 1 and 2 is deleted.
- Operation 2: The edge connecting vertices 2 and 5 is chosen. After adding edges connecting vertices 2 and 6 and connecting vertices 5 and 6, the edge connecting vertices 2 and 5 is deleted.
In this case, the value of \displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) is 32.
The value of \displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j) is 31 with probability \displaystyle \frac12 and 32 with probability \displaystyle \frac12.