CF1047E Region Separation

原题链接:http://codeforces.com/contest/1047/problem/E

Region Separation

There are n n n cities in the Kingdom of Autumn, numbered from 1 1 1 to n n n. People can travel between any two cities using n − 1 n−1 n1 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 1 1 1. Each region of i i i-th level should be separated into several (at least two) regions of i + 1 i+1 i+1-th level, unless i i i-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 i i i, there is a value a i a_i ai, 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 1 0 9 + 7 10^9+7 109+7.

Input

The first line contains one integer n ( 1 ≤ n ≤ 1 0 6 ) n (1≤n≤10^6) n(1n106) — the number of the cities.

The second line contains n n n integers, the i i i-th of which is a i ( 1 ≤ a i ≤ 1 0 9 ) a_i (1≤a_i≤10^9) ai(1ai109) — the value of each city.

The third line contains n − 1 n−1 n1 integers, p 1 , p 2 , ⋯   , p n − 1 p_1,p_2,\cdots,p_{n−1} p1,p2,,pn1 ; $ p_i (p_i≤i) $ escribes a road between cities p i p_i pi and i + 1 i+1 i+1.

Output

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

Examples
input

4
1 1 1 1
1 2 3

output

4

input

4
1 1 1 1
1 2 2

output

2

input

4
1 2 1 2
1 1 3

output

3

Note

For the first example, there are 4 4 4 different plans:

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

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

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

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

For the second example, there are 2 2 2 different plans:

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

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

For the third example, there are 3 3 3 different plans:

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

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

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

题目大意

给定一棵大小为 n n n的树,点有点权,在一个方案中可以将整棵树划分多次,要求每次划分后各个联通块的权值和相等,问有多少种划分方案。

题解

考虑如何将一棵树划分为 k k k份,有一个简单的 d f s dfs dfs做法,设整棵树的权值和为 S S S,当一棵子树的权值和等于 S k \frac{S}{k} kS时,就砍下来。

经观察可以发现,每次我们砍掉的子树的权值和都是 S k \frac{S}{k} kS的倍数,当整棵树存在合法划分时,这样的子树恰好有 k k k个。

当一棵子树的权值和为 x x x时,它会对 k k k产生贡献当且仅当 S k ∣ x \frac{S}{k}|x kSx,就有 S g c d ( S , x ) ∣ k \frac{S}{gcd(S,x)}|k gcd(S,x)Sk,由此我们便可以统计每棵子树的贡献,进而得知这棵树是否可以被划分为 k k k份。

如果这棵树可以划分为 k k k份,那么也一定可以划分为 k ′ ( k ′ ∣ k ) k'(k'|k) k(kk)份,所以 k ′ k' k的方案数也可以加到 k k k上,我们再递推累加一下即可。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=1e6+5,mod=1e9+7;
long long val[M];
int dad[M],dp[M],cot[M],ans,n,i,j;
void in(){scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d",&val[i]);for(int i=2;i<=n;++i)scanf("%d",&dad[i]);}
void ac()
{
	for(i=n;i>1;--i)val[dad[i]]+=val[i];
	for(i=n;i;--i)if((val[i]=val[1]/__gcd(val[1],val[i]))<=n)++cot[val[i]];
	for(i=n;i;--i)for(j=i;(j+=i)<=n;)(cot[j]+=cot[i])%=mod;
	for(dp[1]=i=1;i<=n;++i)if(cot[i]==i)for((ans+=dp[j=i])%=mod;(j+=i)<=n;)(dp[j]+=dp[i])%=mod;
	printf("%d",ans);
}
int main(){in();ac();}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值