#abc290f. F - Maximum Diameter

F - Maximum Diameter

Score : 500500 points

问题描述

对于一个长度为 NN 的正整数序列 X=(X1,X2,,XN)X=(X_1,X_2,\ldots,X_N),我们定义 f(X)f(X) 如下:

  • 如果存在一棵具有 NN 个顶点的树,其第 ii(1iN)(1 \leq i \leq N) 顶点的度数为 XiX_i,则称这棵树为好树。若存在这样的好树,则 f(X)f(X) 为该好树的最大直径;若不存在好树,则 f(X)=0f(X)=0

此处,两顶点之间的距离是指从一个顶点到另一个顶点所必须经过的最少边数,而树的直径则是任意两个顶点间距离的最大值。

求所有可能的正整数序列 XX(长度为 NN)上 f(X)f(X) 的和,并对 998244353998244353 取模。可以证明 f(X)f(X) 的总和是一个有限值。

给定 TT 组测试用例,请分别计算每组的答案。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

For a sequence X=(X1,X2,XN)X=(X_1,X_2\ldots,X_N) of length NN consisting of positive integers, we define f(X)f(X) as follows:

  • A tree with NN vertices is said to be good if and only if the degree of the ii-th (1iN)(1 \leq i \leq N) vertex is XiX_i. If a good tree exists, f(X)f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0f(X)=0.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of a tree is the maximum distance between two vertices.

Find the sum, modulo 998244353998244353, of f(X)f(X) over all possible sequences XX of length NN consisting of positive integers. We can prove that the sum of f(X)f(X) is a finite value.

Given TT test cases, find the answer for each of them.

Constraints

  • 1T2×1051\leq T \leq 2\times 10^5
  • 2N1062 \leq N \leq 10^6
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where testi\mathrm{test}_i denotes the ii-th test case:

TT

test1\mathrm{test}_1

test2\mathrm{test}_2

\vdots

testT\mathrm{test}_T

Each test case is given in the following format:

NN

Output

Print TT lines.

The ii-th (1iT)(1\leq i \leq T) line should contain the answer to the ii-th test case.

Sample Input 1

10
2
3
5
8
13
21
34
55
89
144

Sample Output 1

1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

If N=3N=3,

for example,

  • when X=(1,1,1)X=(1,1,1), there is no tree with three vertices whose degrees are 1,11,1, and 11, so f(X)=0f(X)=0.
  • When X=(2,1,1)X=(2,1,1), the only possible tree is illustrated below. The diameter of this tree is 22, so f(X)=2f(X)=2.

3-vertex tree

For X=(2,1,1),(1,2,1),(1,1,2)X=(2,1,1),(1,2,1),(1,1,2), we have f(X)=2f(X)=2; for other XX, we have f(X)=0f(X)=0. Thus, the answer is 66.

update @ 2024/3/10 12:07:32