CF1806D.DSU Master

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given an integer nn and an array aa of length n−1n-1 whose elements are either 00 or 11.

Let us define the value of a permutation†^\dagger pp of length m−1m-1 (m≤nm \leq n) by the following process.

Let GG be a graph of mm vertices labeled from 11 to mm that does not contain any edges. For each ii from 11 to m−1m-1, perform the following operations:

  • define uu and vv as the (unique) vertices in the weakly connected components‡^\ddagger containing vertices pip_i and pi+1p_i+1 respectively with only incoming edges††^{\dagger\dagger};
  • in graph GG, add a directed edge from vertex vv to uu if api=0a_{p_i}=0, otherwise add a directed edge from vertex uu to vv (if api=1a_{p_i}=1).

Note that after each step, it can be proven that each weakly connected component of GG has a unique vertex with only incoming edges.

Then, the value of pp is the number of incoming edges of vertex 11 of GG.

For each kk from 11 to n−1n-1, find the sum of values of all k!k! permutations of length kk. Since this value can be big, you are only required to compute this value under modulo 998 244 353998\,244\,353.

Operations when n=3n=3, a=[0,1]a=[0,1] and p=[1,2]p=[1,2]

†^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

‡^\ddagger The weakly connected components of a directed graph is the same as the components of the undirected version of the graph. Formally, for directed graph GG, define a graph HH where for all edges a→ba \to b in GG, you add an undirected edge a↔ba \leftrightarrow b in HH. Then the weakly connected components of GG are the components of HH.

††^{\dagger\dagger} Note that a vertex that has no edges is considered to have only incoming edges.

给你一个整数 nn 和一个长度为 n−1n-1 的数组 aa,其中每个元素均为 00 或 11。

我们通过如下过程定义一个长度为 m−1m-1(其中 m≤nm \leq n)的排列†^\dagger pp 的值:

令 GG 是一个包含 mm 个顶点(编号从 11 到 mm)且初始不含任何边的有向图。对每个 ii 从 11 到 m−1m-1,执行以下操作:

  • 设 uu 和 vv 分别为在当前图 GG 的弱连通分量‡^\ddagger 中、唯一具有仅入边††^{\dagger\dagger} 的顶点,且这些弱连通分量分别包含顶点 pip_i 和 pi+1p_i+1;
  • 在图 GG 中:若 api=0a_{p_i}=0,则添加一条从顶点 vv 指向顶点 uu 的有向边;否则(即 api=1a_{p_i}=1),添加一条从顶点 uu 指向顶点 vv 的有向边。

注意:在每一步操作后,可以证明图 GG 的每个弱连通分量中均存在唯一一个仅有入边的顶点。

那么,排列 pp 的值即为图 GG 中顶点 11 的入度(即指向顶点 11 的边的数量)。

对每个 kk 从 11 到 n−1n-1,求所有 k!k! 个长度为 kk 的排列的值之和。由于该值可能很大,你只需输出结果对 998 244 353998\,244\,353 取模后的值。

当 n=3n=3、a=[0,1]a=[0,1]、p=[1,2]p=[1,2] 时的操作示意图

†^\dagger 长度为 nn 的排列是指由 11 到 nn 中 nn 个互不相同的整数按任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

‡^\ddagger 有向图的弱连通分量,等价于将其所有有向边视为无向边后所得无向图的连通分量。形式化地,对有向图 GG,构造无向图 HH:对 GG 中每条有向边 a→ba \to b,在 HH 中加入无向边 a↔ba \leftrightarrow b;则 GG 的弱连通分量即为 HH 的连通分量。

††^{\dagger\dagger} 注意:一个没有关联任何边的孤立顶点,也被视为具有“仅入边”。

输入格式

The first line contains a single integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (2≤n≤5⋅1052\le n\le 5 \cdot 10^5).

The second line of each test case contains n−1n-1 integers a1,a2,…,an−1a_1, a_2, \ldots, a_{n-1} (aia_i is 00 or 11).

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤5⋅1052\le n\le 5 \cdot 10^5)。

每个测试用例的第二行包含 n−1n-1 个整数 a1,a2,…,an−1a_1, a_2, \ldots, a_{n-1}(每个 aia_i 为 00 或 11)。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, output n−1n-1 integers in a line, the ii-th integer should represent the answer when k=ik=i, under modulo 998 244 353998\,244\,353.

对于每个测试用例,在一行中输出 n−1n-1 个整数,其中第 ii 个整数表示当 k=ik=i 时的答案,结果对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    2
    3
    0 0
    9
    0 1 0 0 0 1 0 0

    输出#1

    1 3 
    1 2 7 31 167 1002 7314 60612

说明/提示

Consider the first test case.

When k=1k=1, there is only 11 permutation pp.

  • When p=[1]p=[1], we will add a single edge from vertex 22 to 11. Vertex 11 will have 11 incoming edge. So the value of [1][1] is 11.

Therefore when k=1k=1, the answer is 11.

When k=2k=2, there are 22 permutations pp.

  • When p=[1,2]p=[1,2], we will add an edge from vertex 22 to 11 and an edge from 33 to 11. Vertex 11 will have 22 incoming edges. So the value of [1,2][1,2] is 22.
  • When p=[2,1]p=[2,1], we will add an edge from vertex 33 to 22 and an edge from 22 to 11. Vertex 11 will have 11 incoming edge. So the value of [2,1][2,1] is 11.

Therefore when k=2k=2, the answer is 2+1=32+1=3.

考虑第一个测试用例。

当 k=1k=1 时,仅存在 11 个排列 pp。

  • 当 p=[1]p=[1] 时,我们将添加一条从顶点 22 到 11 的边。顶点 11 将具有 11 条入边。因此 [1][1] 的值为 11。

因此当 k=1k=1 时,答案为 11。

当 k=2k=2 时,存在 22 个排列 pp。

  • 当 p=[1,2]p=[1,2] 时,我们将添加一条从顶点 22 到 11 的边和一条从顶点 33 到 11 的边。顶点 11 将具有 22 条入边。因此 [1,2][1,2] 的值为 22。
  • 当 p=[2,1]p=[2,1] 时,我们将添加一条从顶点 33 到 22 的边和一条从顶点 22 到 11 的边。顶点 11 将具有 11 条入边。因此 [2,1][2,1] 的值为 11。

因此当 k=2k=2 时,答案为 2+1=32+1=3。

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

首页