E. Digit Tree
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

ZS the Coder has a large tree. It can be represented as an undirected connected graph of n vertices numbered from 0 to n - 1 and n - 1edges between them. There is a single nonzero digit written on each edge.

One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer M, which is coprime to 10, i.e. .

ZS consider an ordered pair of distinct vertices (u, v) interesting when if he would follow the shortest path from vertex u to vertex v and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by M.

Formally, ZS consider an ordered pair of distinct vertices (u, v) interesting if the following states true:

  • Let a1 = u, a2, ..., ak = v be the sequence of vertices on the shortest path from u to v in the order of encountering them;
  • Let di (1 ≤ i < k) be the digit written on the edge between vertices ai and ai + 1;
  • The integer  is divisible by M.

Help ZS the Coder find the number of interesting pairs!

Input

The first line of the input contains two integers, n and M (2 ≤ n ≤ 100 000, 1 ≤ M ≤ 109, ) — the number of vertices and the number ZS has chosen respectively.

The next n - 1 lines contain three integers each. i-th of them contains ui, vi and wi, denoting an edge between vertices ui and vi with digit wi written on it (0 ≤ ui, vi < n,  1 ≤ wi ≤ 9).

Output

Print a single integer — the number of interesting (by ZS the Coder's consideration) pairs.

Examples
input
6 7
0 1 2
4 2 4
2 0 1
3 0 9
2 5 7
output
7
input
5 11
1 2 3
2 0 3
3 0 3
4 3 3
output
8

虽然不是很难想但是差点调死我hhhhh
首先这道题和常规点分治不太一样的地方是,普通的点分治一般是无向路径,我们往往不用考虑起点和终点而是直接考虑路径的两个端点就行了。
但是本题是有向路径,不同的方向意味着不同的数字。 而且本题还要一个坑爹的地方是知道终点好找起点,但是知道起点不好找终点。。。。。
当然有两种解决方法:
1.对于当前的重心选任意两条路径统计一遍,再把在同一颗子树内的减掉。
2.考虑到起点要么比终点先被扫到,要么晚被扫到,那么我们就正反两遍常规的calc,这样还不用去重。 我就是用的第二种方法。。。 然后千万别忘了起点或终点是重心的情况,,,,但这样不太可能,因为这样例都过不了hhhh 对于点分的每层我们用map记录一下起点的情况,然后用扫到的终点更新答案。
至于怎么更新答案,就是一个式子,推一下就好了也不难hhhh 当然如果你把向下和向上的路径写反了(就像一开始的我)是要调很久的hhhh,因为样例里并没有长度>=3的路径。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#define ll long long
#define maxn 100005
using namespace std;
map<int,int> mmp;
int ci[maxn],n,m,num=,b[maxn];
int to[maxn*],ne[maxn*];
int hd[maxn],sz,minn,root;
int d[maxn],tot,ni[maxn];
int siz[maxn],val[maxn*];
ll ans;
bool done[maxn]; inline void add(int uu,int vv,int ww){
to[++num]=vv,ne[num]=hd[uu],hd[uu]=num,val[num]=ww;
} void froot(int x,int fa){
siz[x]=;
int bal=;
for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
froot(to[i],x);
siz[x]+=siz[to[i]];
bal=max(bal,siz[to[i]]);
} bal=max(bal,sz-siz[x]);
if(bal<minn) minn=bal,root=x;
} int fsiz(int x,int fa){
int an=;
for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
an+=fsiz(to[i],x);
}
return an;
} void dfs(int x,int fa,int dep,int dx,int dy,int tmp){
int tox=(ll)(m-dy)*(ll)ni[dep]%m;
if(mmp.count(tox)) ans+=(ll)mmp[tox];
if(tmp){
if(!dx) ans++;
if(!dy) ans++;
} d[++tot]=dx; for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
dfs(to[i],x,dep+,((ll)dx+(ll)val[i]*(ll)ci[dep])%m,((ll)dy*10ll+(ll)val[i])%m,tmp);
}
} inline void calc(int pos,int va,int tmp){
int pre=tot+;
dfs(pos,pos,,va,va,tmp);
for(;pre<=tot;pre++){
if(!mmp.count(d[pre])) mmp[d[pre]]=;
else mmp[d[pre]]++;
}
} inline void work(int x,int trsiz){
sz=trsiz,minn=<<;
froot(x,x);
done[root]=; int len=;
for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
b[++len]=i;
calc(to[i],val[i],);
} mmp.clear(),tot=; for(;len;len--){
calc(to[b[len]],val[b[len]],);
} mmp.clear(),tot=;
for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
work(to[i],fsiz(to[i],to[i]));
}
} void gcd(int aa,int bb,ll &xx,ll &yy){
if(!bb){
xx=,yy=;
return;
} gcd(bb,aa%bb,yy,xx);
yy-=xx*(ll)(aa/bb);
} inline int get_ni(int x){
ll xx,yy;
gcd(x,m,xx,yy);
return (xx+m)%m;
} int main(){
scanf("%d%d",&n,&m);
ci[]=ni[]=;
for(int i=;i<=n;i++) ci[i]=(ll)ci[i-]*10ll%m,ni[i]=get_ni(ci[i]);
//printf("%d %d %d\n",i,ci[i],ni[i]); int uu,vv,ww;
for(int i=;i<n;i++){
scanf("%d%d%d",&uu,&vv,&ww),uu++,vv++;
ww%=m;
add(uu,vv,ww),add(vv,uu,ww);
} work(,n); cout<<ans<<endl;
return ;
}
 

Codeforces 716 E Digit Tree的更多相关文章

  1. 【Codeforces 715C】Digit Tree(点分治)

    Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...

  2. 【题解】Digit Tree

    [题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...

  3. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  4. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  5. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  6. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  7. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  9. [Codeforces 715C] Digit Tree

    [题目链接] https://codeforces.com/contest/715/problem/C [算法] 考虑点分治 一条路径(x , y)合法当且仅当 : d(x) * 10 ^ dep(x ...

随机推荐

  1. at用法小记

    By francis_hao    Aug 22,2017   at – 设置稍后执行的作业. 概要 at [-V] [-f file] [-mMlv] timespec...at [-V] [-f ...

  2. GROUP_CONCAT(expr)

    This function returns a string result with the concatenated non-NULL values from a group. It returns ...

  3. [bzoj 2733]启发式合并权值线段树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 平衡树待学习.从一个博客学到了合并权值线段树的姿势:http://blog.csdn ...

  4. centos的网络设置问题

    遭遇了多次centos的网络连接问题,现将正确配置总结下: 这里是使用vmware虚拟平台,因为涉及到中间这层,所以需要设置下: 保证centos也能连上网,首先物理机连上网,接着物理机的vmware ...

  5. BigDecimal与Long、int之间的互换

    在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigDecimal 数据类型 //int 转换成 big ...

  6. c++ 批量初始化数组 fill和fill_n函数的应用

    转自c++ 如何批量初始化数组 fill和fill_n函数的应用 std::fill(a+,a+,0x3f3f3f3f);///从下标2到下标10 前闭后开 共8个 std::fill_n(a+,,0 ...

  7. MySql binlog(理论篇)

    1.什么是binlog? binlog日志用于记录所有更新了数据的sql语句或保存被修改的记录Row: 有了binlog,可以用于实时备份,master/slave主从同步: 在5.0版本前支持文本格 ...

  8. viewDidLoad方法的调用顺序

      https://www.evernote.com/shard/s227/sh/01307822-de66-448d-8bac-7954f01ffc64/185b44c2bf5b6e266f4bed ...

  9. 【洛谷 P3834】 可持久化线段树1(主席树)

    题目链接 主席树=可持久化权值线段树. 如果你不会可持久化线段树,请右转 如果你不会权值线段树,请自行脑补,就是线段树维护值域里有多少个数出现. 可持久化线段树是支持查询历史版本的. 我们对每个数都进 ...

  10. [bzoj4518][Sdoi2016]征途-斜率优化

    Brief Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须 ...