[CF1034C] Region Separation
题目描述
There are $ n $ cities in the Kingdom of Autumn, numbered from $ 1 $ to $ n $ . People can travel between any two cities using $ 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 $ . Each region of $ i $ -th level should be separated into several (at least two) regions of $ i+1 $ -th level, unless $ 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 $ , there is a value $ a_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 $ 10^9+7 $ .
输入格式
The first line contains one integer $ n $ ( $ 1 \leq n \leq 10^6 $ ) — the number of the cities.
The second line contains $ n $ integers, the $ i $ -th of which is $ a_i $ ( $ 1 \leq a_i \leq 10^9 $ ) — the value of each city.
The third line contains $ n-1 $ integers, $ p_1, p_2, \ldots, p_{n-1} $ ; $ p_i $ ( $ p_i \leq i $ ) describes a road between cities $ p_i $ and $ i+1 $ .
题解
设 \(s=\sum\limits_{i=1}^n a_i\)
设现在要把树分成 \(i\) 份,如何判定是否可以分成 \(i\) 份呢?首先一定要满足 \(i|s\)。钦定树根为 1,定义 \(b_i\) 为 \(i\) 的子树的 \(a\) 的和。那么结论是如果存在至少(其实至多也是) \(i\) 个 \(b_i\) 满足 \(\frac{s}{i}|b_i\)。很明显,一种方案中,一个连通块的点权和等于一个 \(b_i\) 减去若干个 \(b_j\),然后他们如果都是 \(\frac{s}{i}\) 的倍数,那么减完后满足这个连通块也是 \(\frac{s}{i}\) 的倍数,所以满足每个连通块都是 \(\frac{s}{i}\) 的倍数。换句话说,如果 \(\frac{s}{i}|b_i\),那么就把 \(i\) 和父亲的连边断去,就是一个构造。
然而我们如何统计树上有多少个 \(b_j\) 满足 \(\frac{s}{i}|b_j\) 呢?暴力是 \(O(n^2)\)。同时有一个性质是 \(i\le n\),在这里做手脚,把条件转化为 \(i\) 有关的。如果 \(\frac{s}{i}|b_j\),同时 \(\frac{s}{i}|s\),所以 \(\frac{s}{i}|\gcd(b_j,s)\)。定义 \(k=\tfrac{\gcd(b_j,s)}{\frac{s}{i}}\),\(\frac{ks}{i}=\gcd(b_j,s),k\tfrac{s}{\gcd(b_j,s)}=i,\tfrac{s}{\gcd(b_j,s)}|i\)
很成功转成了 \(i\) 的条件,那么就可以直接一个筛法统计了。统计完后要计算方案数,如果可以分成 \(i\) 份,然后枚举上一次是分成几份后再分成这样的,那么 \(dp_i=1+\sum\limits_{j|i}dp_j\)。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+5,P=998244353;
int f[N],g[N],n,hd[N],e_num,ret;
LL a[N],s,ans,c[N];
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
LL gcd(LL x,LL y)
{
if(!y)
return x;
return gcd(y,x%y);
}
void dfs(int x,int y)
{
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v!=y)
dfs(e[i].v,x),a[x]+=a[e[i].v];
LL k=s/gcd(s,a[x]);
if(k<=n)
c[k]++;
}
int main()
{
freopen("xiongaktenioi.in","r",stdin);
freopen("xiongaktenioi.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",a+i),s+=a[i];
for(int i=2,x;i<=n;i++)
scanf("%d",&x),add_edge(x,i);
// printf("qzmakioi") ;
dfs(1,0);
// printf("qzmakioi") ;
for(int i=n-1;i>=1;i--)
for(int j=2;j*i<=n;j++)
c[j*i]+=c[i];
g[1]=1;
// printf("qzmakioi") ;
for(int i=1;i<=n;i++)
{
if(s%i==0&&c[i]==i)
{
(ret+=g[i])%=P;
for(int k=2;k*i<=n;k++)
(g[k*i]+=g[i])%=P;
}
}
printf("%d",ret);
}
[CF1034C] Region Separation的更多相关文章
- 【动态规划】cf1034C. Region Separation
质因数分解套路的复杂度分析的动态规划 题目大意 有一颗$n$个节点有点权的树,初始整棵树为$1$号区域,要求满足下列规则: 除非$i$是最后一个等级,否则每一个$i$级区域都要被分成至少两个$i+1$ ...
- Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)
题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...
- 2018.9.21 Codeforces Round #511(Div.2)
只写了AB,甚至还WA了一次A题,暴露了蒟蒻的本质=.= 感觉考的时候有好多正确或和正解有关的思路,但是就想不出具体的解法或者想的不够深(长)(怕不是过于鶸) 话说CF的E题怎么都这么清奇=.= A. ...
- [CF]Round511
这场比赛我及时的参加了,但是打的时候状态实在是太烂了,只做出来了Div2的AB题. A Little C loves 3 I 直接构造就行. B Cover Points 应该很容易就看出来这个等腰三 ...
- Android 2D Graphics学习 Region和Canvas裁剪
1.首先介绍Region类 Region,中文意思即区域的意思,它表示的是canvas图层上的某一块封闭的区域. /**构造方法*/ public Region() //创建一个空的区域 publi ...
- #region Json转DataTable
#region Json转DataTable private DataTable Json2Dtb(string json) { JavaScrip ...
- CVPR 2011 Global contrast based salient region detection
Two salient region detection methods are proposed in this paper: HC AND RC HC: Histogram based contr ...
- C++中类似C# region的功能
使用#pragma region和#pragma endregion关键字,来定义可以展开和收缩的代码区域的开头和结尾, 可以把这些代码行收缩为一行,以后要查看其细节时,可以再次展开它. 例如: // ...
- bdb log为什么 有 region buffer 和 log cursor buf
对bdb log来说, 在共享内存中 有一块 buffer, 同时每一个 log cursor 都自带一个 malloc的buf. why? 我认为: region buffer存的是log最末尾, ...
- iOS-地图报错超出了经纬度范围Invalid Region
做地图定位的时候,使用一下代码 // 经纬度 CLLocationDegrees latitude = [storeDict[@"lat"] doubleValue]; CLLoc ...
随机推荐
- 苹果MacOS系统傻瓜式本地部署AI绘画Stable Diffusion教程
Stable Diffusion的部署对小白来说非常麻烦,特别是又不懂技术的人.今天分享两个一键傻瓜式安装包,对小白来说非常有用.下面两个任选一个安装就可以. 一.DiffusionBee 简单介绍 ...
- 通过API接口获取到数据后的使用方法以及储存方法
API接口是许多应用程序和服务所必需的,可以将多个应用程序连接起来,允许不同应用程序之间的数据共享.在本文中,我们将探讨如何使用API接口获取数据,以及如何储存这些数据. 1.使用API接口获取数据 ...
- Blazor前后端框架Known-V1.2.14
V1.2.14 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Git ...
- 小知识:PPT的幻灯片放映设置
最近给某客户讲课时,碰到了幻灯片自动翻页的情况,发现是因为之前做过粗略的计时演练,有些片子就快速过了. 问题现象: 结果导致放映时也出现了某些片子快速被自动翻页. 解决方案: 设置成手动推进幻灯片的方 ...
- 使用 Sealos 一键部署高可用 MinIO,开启对象存储之旅
大家好!今天这篇文章主要向大家介绍如何通过 Sealos 一键部署高可用 MinIO 集群. MinIO 对象存储是什么? 对象是二进制数据,例如图像.音频文件.电子表格甚至二进制可执行代码.对象的大 ...
- 「luogu - P4313」文理分科 Mincut
link. Pretty nice practice for the min-cut trick. Starting out we eliminate the constraint that if f ...
- Springboot简单功能示例-4 自定义加密进行登录验证
springboot-sample 介绍 springboot简单示例 跳转到发行版 查看发行版说明 软件架构(当前发行版使用) springboot hutool-all 非常好的常用java工具库 ...
- Python基础——二分法、面向过程编程思想、有名函数、lambda、max、_min的应用、sorted排序、map的应用、filter的应用、reduce的应用
文章目录 内容回顾 二分法 伪代码模板 面向过程编程思想 函数式 def用于定义有名函数 lambda用于定义匿名函数 调用匿名函数 匿名函数作用 匿名函数的示范 max的应用 min的应用 sort ...
- 物理standby database的日常维护【转】
1.停止Standby select process, status from v$managed_standby; --查看备库是否在应用日志进行恢复 alter database recover ...
- the solution of Mining Your Own Business
the description of problem (我看的是 PDF 里面的原题所以这里描述会和题目不一样,但是大意一致) 给定一个未必连通的无向图,问最少在几个点设置出口,可以保证任意一个点坍塌 ...