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

Region Separation

There are nnn cities in the Kingdom of Autumn, numbered from 111 to nnn. People can travel between any two cities using n−1n−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 111. Each region of iii-th level should be separated into several (at least two) regions of i+1i+1i+1-th level, unless iii-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 iii, there is a value aia_iai​, 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+7109+7.

Input

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

The second line contains nnn integers, the iii-th of which is ai(1≤ai≤109)a_i (1≤a_i≤10^9)ai​(1≤ai​≤109) — the value of each city.

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

Output

Print one integer — the number of different plans modulo 109+710^9+7109+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 444 different plans:

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

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

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

Plan 444: Level-111: {1,2,3,4}\{1,2,3,4\}{1,2,3,4}, Level-222: {1,2},{3,4}\{1,2\},\{3,4\}{1,2},{3,4}, Level-333: {1},{2},{3},{4}\{1\},\{2\},\{3\},\{4\}{1},{2},{3},{4}.

For the second example, there are 222 different plans:

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

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

For the third example, there are 333 different plans:

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

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

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

题目大意

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

题解

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

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

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

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

代码
#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();}

CF1047E Region Separation相关推荐

  1. 【动态规划】cf1034C. Region Separation

    质因数分解套路的复杂度分析的动态规划 题目大意 有一颗$n$个节点有点权的树,初始整棵树为$1$号区域,要求满足下列规则: 除非$i$是最后一个等级,否则每一个$i$级区域都要被分成至少两个$i+1$ ...

  2. Codeforces Round #511 (Div. 1) 题解

    在学校熬夜打$cf$找死啊. A - Enlarge GCD 先求个$gcd$,然后对于每个$a[i]$都除以$gcd$. 之后我们只需要统计每个质数整除的个数即可. 因为带上所有数的$gcd$一定是 ...

  3. 论文阅读:Automatic Detection and Classi cation of Teeth in CT Data

    [论文信息] MICCAI 2012 会议论文 文章实现了中全自动的牙齿检测和分类,对象为CBCT/MSCT,实验数据集是43套临床头部CT图像. 主要是两个步骤: 1. 分割上颌骨: 2. 分成16 ...

  4. cell 发布 Phase separation 研究指南

    转自: https://mp.weixin.qq.com/s/2YAtRPbGyY5APKvBOCz8NA 细胞是生物体结构和功能的基本单位,细胞内的各种组分如何在正确的时间以及空间上聚集以执行其相应 ...

  5. Halcon学习之六:获取Image图像中Region区域的特征参数

    area_center_gray ( Regions, Image : : : Area, Row, Column )    计算Image图像中Region区域的面积Area和重心(Row,Colu ...

  6. 深入理解PHP原理之变量分离/引用(Variables Separation)

    引自: http://www.laruence.com/ [风雪之隅 ] 在前面的文章中我已经介绍了PHP的变量的内部表示(深入理解PHP原理之变量(Variables inside PHP)),以及 ...

  7. 如何在OpenStack环境中实现多Region

    OpenStack很早的版本就支持多Region,所谓多Region,就是多个区域,每个区域一套OpenStack,共享Keystone和Horizon. 用户登录Dashboard以后,可以多个Re ...

  8. G1的Region是如何划分数量和大小的?

    说明:JDK7和JDK8的Region划分实现略有不同(差异非常小,且只有-Xmx和-Xms的值不一样才有区别),本篇文章讲解的是JDK8中Region的划分实现:如果要了解JDK7的Region划分 ...

  9. java预处理指令region_VS #region

    1.C# 预处理指令 #region使您得以在使用Visual Studio代码编辑器的大纲显示功能时指定可展开或折叠的代码块.    #region   name    其中:name      希 ...

  10. matplotlib可视化时间序列数据、并高亮时间序列中的指定区域(Highlight a Region of Time-Series Plot with Matplotlib)

    matplotlib可视化时间序列数据.并高亮时间序列中的指定区域(Highlight a Region of Time-Series Plot with Matplotlib) 目录

最新文章

  1. spring中的RowMapper
  2. Android内存泄露总结
  3. sql 新建发布 找不到存储过程_pgRouting教程九:使用GeoServer发布WMS/WFS接口
  4. idea编辑springboot,如何打成war包
  5. mpvue 小程序 页面跳转获取参数
  6. 大哥你需求里说只要工作流引擎组件,怎么真正需要的东西这么.悲剧了,客户需求无止境...
  7. window.onload中调用函数报错的问题
  8. WF4.0 基础篇 (六) 数据的传递 Arguments 参数
  9. CocoaPods 添加第三方库报错
  10. html5 星际摩托,HTML5 星际陨石环绕动效
  11. oracle日期时间函数
  12. 下docfetcher先下Java,DocFetcher ── 以 JAVA 编写的开源桌面本地文件全文搜索工具...
  13. 英伟达实时 3D 设计协作和仿真平台已正式发布
  14. Oracle数据库恢复删除数据的方法
  15. ibm量子云计算机,IBM量子云的16个量子比特全被纠缠起来了!
  16. 紫乌鸦服务器维护后多久刷,魔兽世界紫乌鸦怎么刷 刷新点在哪什么时候去蹲点...
  17. 苹果中国官网新增蚂蚁花呗 24 期分期免息服务
  18. 大学生面试20个经典问题及回答思路!
  19. NetworkX中文使用手册
  20. 企业级大数据平台智能运维好帮手——星环科技多模数据平台监控软件Aquila Insight

热门文章

  1. 只在一点处连续且可导的例子
  2. 695.岛屿的最大面积(力扣leetcode) 博主可答疑该问题
  3. java中的并发是什么意思_java中的并发是什么
  4. 【转】Intellij Idea识别Java Web项目
  5. TelerikUI_RadGrid_Filter 自定义方法
  6. 2000条你应知的WPF小姿势 基础篇40-44 启动关闭,Xaml,逻辑树
  7. 七个不放过和四项原则
  8. 如何解决缓存与数据库不一致?
  9. 高并发下如何保证数据库和缓存双写一致性?
  10. Linux系统下zookeeper的安装和配置