CF716E Digit Tree 点分治
题意:
给出一个树,每条边上写了一个数字,给出一个P,求有多少条路径按顺序读出的数字可以被P整除。保证P与10互质。
分析:
统计满足限制的路径,我们首先就想到了点分治。
随后我们就需要考量,我们是否能统计过某个点的合法路径。我们看,由题目性质,我们可以求对于一个根,所有点到根的路径组成的数,以及根到所有点的路径所组成的数。然后我们就可以对前者开一个数组(其实是一个map容器),后者在上面查询,然后用点分治的去重套路,保证答案不重不漏。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=,inf=0x3f3f3f3f;
struct node{int y,z,nxt;}e[N*];int ans;
int sm,h[N],c=,vis[N],d[N],siz[N],mx[N];
int rt,n,m,pw[N],tot,dis[N];map<int,int>mp;
void add(int x,int y,int z){
e[++c]=(node){y,z,h[x]};h[x]=c;
e[++c]=(node){x,z,h[y]};h[y]=c;
} void getrt(int x,int fa){
siz[x]=;mx[x]=;
for(int i=h[x],y;i;i=e[i].nxt)
if((y=e[i].y)!=fa&&!vis[y]){
getrt(y,x);siz[x]+=siz[y];
mx[x]=max(mx[x],siz[y]);
} mx[x]=max(mx[x],sm-siz[x]);
if(mx[x]<mx[rt]) rt=x;return ;
} void exgcd(int a,int b,int &d,int &x,int &y){
if(!b) d=a,x=,y=;
else exgcd(b,a%b,d,y,x),y-=x*(a/b);
} int inv(int a,int p){
int x,y,d;exgcd(a,p,d,x,y);
return d==?(x%p+p)%p:-;
} void dfs(int x,int fa,int v1,int v2,int dep){
if(dep){
mp[v2%m]++;dis[++tot]=v1;d[tot]=dep;
} for(int i=h[x],y;i;i=e[i].nxt)
if((y=e[i].y)!=fa&&!vis[y]){
dfs(y,x,(v1*%m+e[i].z)%m,
(v2+e[i].z*pw[dep]%m)%m,dep+);
} return ;
} int calc(int x,int v,int op){
int res=;tot=;mp.clear();
dfs(x,,v,v,op);
for(int i=;i<=tot;i++){
int val=(m-dis[i]*inv(pw[d[i]],m)%m)%m;
if(mp.find(val)!=mp.end()) res+=mp[val];
if(!op) res+=(dis[i]==);
} if(!op) res+=mp[];return res;
} void solve(int x){
ans+=calc(x,,);vis[x]=;
for(int i=h[x],y;i;i=e[i].nxt)
if(!vis[y=e[i].y]){
ans-=calc(y,e[i].z,);
mx[rt=]=inf;sm=siz[y];
getrt(y,);solve(rt);
} return ;
} signed main(){
scanf("%lld%lld",&n,&m);
for(int i=,x,y,z;i<n;i++)
scanf("%lld%lld%lld",&x,&y,&z),
add(x+,y+,z);
pw[]=;for(int i=;i<=n;i++)
pw[i]=1ll*pw[i-]*%m;
mx[rt=]=inf;sm=n;
getrt(,);solve(rt);
printf("%lld\n",ans);
return ;
}
点分治
CF716E Digit Tree 点分治的更多相关文章
- CF 716E. Digit Tree [点分治]
题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除\(P\) 路径统计一般就是点分治了 \[ a*10^{deep} + b \ \equiv \pmod P\ ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- 【Codeforces 715C】Digit Tree(点分治)
Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...
- Codeforces 716 E Digit Tree
E. Digit Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- 【题解】Digit Tree
[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
随机推荐
- SpringBoot第九篇:整合Spring Data JPA
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10910059.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 前面几章, ...
- EJ20170430-hm
alignment n. 结盟; 队列,排成直线; 校直,调整; [工] 准线 aspect ratio n. 宽高比; 纵横比,屏幕高宽比 aspect n. 方面; thick n. ...
- wpf 中AxShockwaveFlash重写以及屏蔽鼠标右键
在wpf中需要用到flash播放swf或者图片,需要使用 AxShockwaveFlashObjects.dll和ShockwaveFlashObjects.dll 在项目中使用的时候遇到 问题1.使 ...
- 在MacOs上安装sqlsrv Mojave - 找不到'php.h'文件
Mojave没有安装标头. 要安装标头: open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_1 ...
- [BZOJ5120]无限之环
Description 曾经有一款流行的游戏,叫做InfinityLoop,先来简单的介绍一下这个游戏: 游戏在一个n×m的网格状棋盘上进行,其中有些小方格中会有水管,水管可能在方格某些方向的边界的中 ...
- Salazar Slytherin's Locket CodeForces - 855E
Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- eclipse导入php项目
整个工程的都在一个文件夹里面 怎么把它导入到eclipse里面呢:在eclipse里新建一个与要导入的工程同名工程.
- Retrofit Upload multiple files and parameters
Retrofit 的介绍以及基本使用 这里不再说明. 关于多文件上传 以及上传文件的同时携带多个参数说明 网上涉及到的不是太多. 上一张帅图: 代码: apiService: /** params 参 ...
- [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
本文转自:http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NE ...