CF1762E.Tree Sum
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let us call an edge-weighted tree with n vertices numbered from 1 to n good if the weight of each edge is either 1 or −1 and for each vertex i , the product of the edge weights of all edges having i as one endpoint is −1 .
You are given a positive integer n . There are nn−2⋅2n−1 distinct † edge-weighted trees with n vertices numbered from 1 to n such that each edge is either 1 or −1 . Your task is to find the sum of d(1,n)‡ of all such trees that are good. Since the answer can be quite large, you only need to find it modulo 998244353 .
† Two trees are considered to be distinct if either:
- there exists two vertices such that there is an edge between them in one of the trees, and not in the other.
- there exists two vertices such that there is an edge between them in both trees but the weight of the edge between them in one tree is different from the one in the other tree.
Note that by Cayley's formula, the number of trees on n labeled vertices is nn−2 . Since we have n−1 edges, there are 2n−1 possible assignment of weights(weight can either be 1 or −1 ). That is why total number of distinct edge-weighted tree is nn−2⋅2n−1 .
‡ d(u,v) denotes the sum of the weight of all edges on the unique simple path from u to v .
输入格式
The first and only line of input contains a single integer n ( 1≤n≤5⋅105 ).
输出格式
The only line of output should contain a single integer, the required answer, modulo 998244353 .
输入输出样例
输入#1
2
输出#1
998244352
输入#2
1
输出#2
0
输入#3
4
输出#3
998244343
输入#4
10
输出#4
948359297
输入#5
43434
输出#5
86232114
说明/提示
In the first test case, there is only 1 distinct good tree. The value of d(1,2) for that tree is −1 , which is 998244352 under modulo 998244353 .
In the second test case, the value of d(1,1) for any tree is 0 , so the answer is 0 .
In the third test case, there are 16 distinct good trees. The value of d(1,4) is:
- −2 for 2 trees;
- −1 for 8 trees;
- 0 for 4 trees;
- 1 for 2 trees.
The sum of d(1,4) over all trees is 2⋅(−2)+8⋅(−1)+4⋅(0)+2⋅(1)=−10 , which is 998244343 under modulo 998244353 .