原题链接: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 n−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 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(1≤n≤106) — 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(1≤ai≤109) — the value of each city.
The third line contains n − 1 n−1 n−1 integers, p 1 , p 2 , ⋯   , p n − 1 p_1,p_2,\cdots,p_{n−1} p1,p2,⋯,pn−1 ; $ 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 kS∣x,就有 S g c d ( S , x ) ∣ k \frac{S}{gcd(S,x)}|k gcd(S,x)S∣k,由此我们便可以统计每棵子树的贡献,进而得知这棵树是否可以被划分为 k k k份。
如果这棵树可以划分为 k k k份,那么也一定可以划分为 k ′ ( k ′ ∣ k ) k'(k'|k) k′(k′∣k)份,所以 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();}