「OICon-02」Subtree Value

Luogu
IDLGP10175
Time1000ms
Memory512MB
DifficultyP6
动态规划 DPO2优化树形 DP组合数学
给出一棵 $n$ 个节点的树,每个点有点权 $a_v$。定义一棵树的一个子连通块为一个树中点的**非空集合**,满足这些点在树上形成一个连通块。定义子连通块 $S$ 的权值为 $\prod_{v\in S}(a_v+|S|)$。求所有子连通块的权值之和对 $U^V$ 取模。 ## Input 第一行,三个正整数 $n,U,V$,分别表示节点个数,以及模数($U^V$)。 第二行,$n-1$ 个正整数 $f_2,f_3,\dots,f_n$,分别表示以 $1$ 节点为根节点的情况下第 $i$ 个点的父亲节点。 第三行,$n$ 个非负整数 $a_i$,表示每个点的点权。 ## Output 一行,一个正整数,表示所有子联通块的权值之和,对 $U^V$ 取模。 [samples] ## Note ### 样例解释 对于样例 $1$,以下子连通块的权值分别是: * $\{1\}$:$(1+1)=2$; * $\{2\}$:$(2+1)=3$; * $\{3\}$:$(3+1)=4$; * $\{1,2\}$:$(1+2)\times(2+2)=12$; * $\{1,3\}$:$(1+2)\times(3+2)=15$; * $\{1,2,3\}$:$(1+3)\times(2+3)\times(3+3)=120$。 总和为 $2+3+4+12+15+120=156$,对 $10^6$ 取模后为 $156$。 ### 数据范围 **本题采用捆绑测试。** | $\text{Subtask}$ | 特殊性质 | $\text{Score}$ | |:--:|:--:|:--:| | $1$ | $n\leq10$ | $5$ | | $2$ | $n\leq150$ | $8$ | | $3$ | $n\leq500$ | $11$ | | $4$ | $U=2,V=1$ | $7$ | | $5$ | $V=1$ | $23$ | | $6$ | $U\mid a_i$ | $23$ | | $7$ | 无特殊限制 | $23$ | 对于 $100\%$ 的数据:$1\leq n\leq2000$,$1\leq f_i<i$,$2\leq U\leq10$,$1\leq V\leq6$,$0\leq a_i< U^V$。
Samples
Input #1
3 10 6
1 1
1 2 3
Output #1
156
Input #2
11 4 6
1 1 2 3 4 4 4 5 6 7
325 190 400 325 380 165 334 400 80 171 340
Output #2
678
API Response (JSON)
{
  "problem": {
    "name": "「OICon-02」Subtree Value",
    "description": {
      "content": "给出一棵 $n$ 个节点的树,每个点有点权 $a_v$。定义一棵树的一个子连通块为一个树中点的**非空集合**,满足这些点在树上形成一个连通块。定义子连通块 $S$ 的权值为 $\\prod_{v\\in S}(a_v+|S|)$。求所有子连通块的权值之和对 $U^V$ 取模。",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 524288
    },
    "difficulty": {
      "LuoguStyle": "P6"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP10175"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "给出一棵 $n$ 个节点的树,每个点有点权 $a_v$。定义一棵树的一个子连通块为一个树中点的**非空集合**,满足这些点在树上形成一个连通块。定义子连通块 $S$ 的权值为 $\\prod_{v\\in S}(a_v+|S|)$。求所有子连通块的权值之和对 $U^V$ 取模。\n\n## Input\n\n第一行,三个正整数 $n,U,V$,分别表示节点个数,以及模数($U^V$)。\n\n第二行,$n-1$ ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments