CF1762E.Tree Sum

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let us call an edge-weighted tree with nn vertices numbered from 11 to nn good if the weight of each edge is either 11 or −1-1 and for each vertex ii, the product of the edge weights of all edges having ii as one endpoint is −1-1.

You are given a positive integer nn. There are nn−2⋅2n−1n^{n-2} \cdot 2^{n-1} distinct†^\dagger edge-weighted trees with nn vertices numbered from 11 to nn such that each edge is either 11 or −1-1. Your task is to find the sum of d(1,n)‡d(1,n)^\ddagger of all such trees that are good. Since the answer can be quite large, you only need to find it modulo 998 244 353998\,244\,353.

†^\dagger 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 nn labeled vertices is nn−2n^{n-2}. Since we have n−1n-1 edges, there are 2n−12^{n-1} possible assignment of weights(weight can either be 11 or −1-1). That is why total number of distinct edge-weighted tree is nn−2⋅2n−1n^{n-2} \cdot 2^{n-1}.

‡^\ddagger d(u,v)d(u,v) denotes the sum of the weight of all edges on the unique simple path from uu to vv.

我们称一棵具有 nn 个顶点(编号为 11 到 nn)且每条边带权的树为好树,若其每条边的权值均为 11 或 −1-1,且对每个顶点 ii,所有以 ii 为一个端点的边的权值之积为 −1-1。

给定一个正整数 nn。在所有顶点编号为 11 到 nn 的边带权树中,满足每条边权值为 11 或 −1-1 的不同†^\dagger 树共有 nn−2⋅2n−1n^{n-2} \cdot 2^{n-1} 棵。你的任务是:对所有这样的好树,求其 d(1,n)‡d(1,n)^\ddagger 的总和。由于答案可能非常大,你只需输出该总和对 998 244 353998\,244\,353 取模的结果。

†^\dagger 两棵树被视为不同,当且仅当满足以下任一条件:

  • 存在两个顶点,在其中一棵树中它们之间有边相连,而在另一棵树中没有;
  • 存在两个顶点,在两棵树中它们之间均有边相连,但这两条边的权值不同。

注意,根据 Cayley 公式,nn 个带标号顶点的树的总数为 nn−2n^{n-2}。由于每棵树恰有 n−1n-1 条边,而每条边可独立地赋予权值 11 或 −1-1,故权值分配方式共 2n−12^{n-1} 种。因此,不同的边带权树总数为 nn−2⋅2n−1n^{n-2} \cdot 2^{n-1}。

‡^\ddagger d(u,v)d(u,v) 表示从 uu 到 vv 的唯一简单路径上所有边的权值之和。

输入格式

The first and only line of input contains a single integer nn (1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5).

输入仅有一行,包含一个整数 nn(1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5)。

输出格式

The only line of output should contain a single integer, the required answer, modulo 998 244 353998\,244\,353.

输出仅包含一行,其中为一个整数,即所求答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#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 11 distinct good tree. The value of d(1,2)d(1,2) for that tree is −1-1, which is 998 244 352998\,244\,352 under modulo 998 244 353998\,244\,353.

In the second test case, the value of d(1,1)d(1,1) for any tree is 00, so the answer is 00.

In the third test case, there are 1616 distinct good trees. The value of d(1,4)d(1,4) is:

  • −2-2 for 22 trees;
  • −1-1 for 88 trees;
  • 00 for 44 trees;
  • 11 for 22 trees.

The sum of d(1,4)d(1,4) over all trees is 2⋅(−2)+8⋅(−1)+4⋅(0)+2⋅(1)=−102 \cdot (-2) + 8 \cdot (-1) + 4 \cdot (0) + 2 \cdot (1) = -10, which is 998 244 343998\,244\,343 under modulo 998 244 353998\,244\,353.

在第一个测试用例中,仅有 11 棵不同的好树。该树的 d(1,2)d(1,2) 值为 −1-1,在模 998 244 353998\,244\,353 意义下等于 998 244 352998\,244\,352。

在第二个测试用例中,任意树的 d(1,1)d(1,1) 值均为 00,因此答案为 00。

在第三个测试用例中,共有 1616 棵不同的好树。其中 d(1,4)d(1,4) 的值满足:

  • 对于 22 棵树,d(1,4)=−2d(1,4) = -2;
  • 对于 88 棵树,d(1,4)=−1d(1,4) = -1;
  • 对于 44 棵树,d(1,4)=0d(1,4) = 0;
  • 对于 22 棵树,d(1,4)=1d(1,4) = 1。

所有树的 d(1,4)d(1,4) 值之和为 2⋅(−2)+8⋅(−1)+4⋅(0)+2⋅(1)=−102 \cdot (-2) + 8 \cdot (-1) + 4 \cdot (0) + 2 \cdot (1) = -10,在模 998 244 353998\,244\,353 意义下等于 998 244 343998\,244\,343。

输入解题思路,AI测评打分。不知道怎么写?

首页