CF1034C.Region Separation

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn cities in the Kingdom of Autumn, numbered from 11 to nn. People can travel between any two cities using n−1n-1 two-directional roads.

This year, the government decides to separate the kingdom. There will be regions of different levels. The whole kingdom will be the region of level 11. Each region of ii-th level should be separated into several (at least two) regions of i+1i+1-th level, unless ii-th level is the last level. Each city should belong to exactly one region of each level and for any two cities in the same region, it should be possible to travel between them passing the cities in the same region only.

According to research, for each city ii, there is a value aia_i, which describes the importance of this city. All regions of the same level should have an equal sum of city importances.

Your task is to find how many plans there are to determine the separation of the regions that all the conditions are satisfied. Two plans are considered different if and only if their numbers of levels are different or there exist two cities in the same region of one level in one plan but in different regions of this level in the other plan. Since the answer may be very large, output it modulo 109+710^9+7.

王国秋之国有 nn 座城市,编号为 11 到 nn。人们可以通过 n−1n-1 条双向道路在任意两座城市之间通行。

今年,政府决定将该王国进行分区。分区将分为若干不同层级。整个王国本身构成第 11 级区域。每个第 ii 级区域(除非第 ii 级已是最后一级)必须被划分为若干个(至少两个)第 i+1i+1 级区域。每座城市在每一级上必须且仅属于一个区域;并且,对任一区域内任意两座城市,必须存在一条仅经过该区域内城市的路径,使其相互连通。

根据研究,对每座城市 ii,均有一个数值 aia_i,用以描述该城市的重要性。同一级的所有区域的城市重要性之和必须相等。

你的任务是:求出满足上述所有条件的分区方案总数。若两个方案的层级总数不同,或存在某一级别,使得某两座城市在其中一个方案中属于同一区域、而在另一个方案中属于不同区域,则认为这两个方案不同。由于答案可能非常大,请输出其对 109+710^9+7 取模的结果。

输入格式

The first line contains one integer nn (1≤n≤1061 \leq n \leq 10^6) — the number of the cities.

The second line contains nn integers, the ii-th of which is aia_i (1≤ai≤1091 \leq a_i \leq 10^9) — the value of each city.

The third line contains n−1n-1 integers, p1,p2,…,pn−1p_1, p_2, \ldots, p_{n-1}; pip_i (pi≤ip_i \leq i) describes a road between cities pip_i and i+1i+1.

第一行包含一个整数 nn(1≤n≤1061 \leq n \leq 10^6)—— 城市的数量。

第二行包含 nn 个整数,其中第 ii 个为 aia_i(1≤ai≤1091 \leq a_i \leq 10^9)—— 每座城市的权值。

第三行包含 n−1n-1 个整数 p1,p2,…,pn−1p_1, p_2, \ldots, p_{n-1};其中 pip_i(满足 pi≤ip_i \leq i)表示城市 pip_i 与城市 i+1i+1 之间存在一条道路。

输出格式

Print one integer — the number of different plans modulo 109+710^9+7.

输出一个整数——不同方案的数量对 109+710^9+7 取模的结果。

输入输出样例

  • 输入#1

    4
    1 1 1 1
    1 2 3

    输出#1

    4
  • 输入#2

    4
    1 1 1 1
    1 2 2

    输出#2

    2
  • 输入#3

    4
    1 2 1 2
    1 1 3

    输出#3

    3

说明/提示

For the first example, there are 44 different plans:

Plan 11: Level-11: 1,2,3,4{1,2,3,4}.

Plan 22: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1,2{1,2},3,4{3,4}.

Plan 33: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1{1},2{2},3{3},4{4}.

Plan 44: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1,2{1,2},3,4{3,4}, Level-33: 1{1},2{2},3{3},4{4}.

For the second example, there are 22 different plans:

Plan 11: Level-11: 1,2,3,4{1,2,3,4}.

Plan 22: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1{1},2{2},3{3},4{4}.

For the third example, there are 33 different plans:

Plan 11: Level-11: 1,2,3,4{1,2,3,4}.

Plan 22: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1,2{1,2},3,4{3,4}.

Plan 33: Level-11: 1,2,3,4{1,2,3,4}, Level-22: 1,3{1,3},2{2},4{4}.

第一个样例有 44 种不同的方案:

方案 11:第 11 层:1,2,3,4{1,2,3,4}。

方案 22:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1,2{1,2}、3,4{3,4}。

方案 33:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1{1}、2{2}、3{3}、4{4}。

方案 44:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1,2{1,2}、3,4{3,4},第 33 层:1{1}、2{2}、3{3}、4{4}。

第二个样例有 22 种不同的方案:

方案 11:第 11 层:1,2,3,4{1,2,3,4}。

方案 22:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1{1}、2{2}、3{3}、4{4}。

第三个样例有 33 种不同的方案:

方案 11:第 11 层:1,2,3,4{1,2,3,4}。

方案 22:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1,2{1,2}、3,4{3,4}。

方案 33:第 11 层:1,2,3,4{1,2,3,4},第 22 层:1,3{1,3}、2{2}、4{4}。

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

首页