Codeforces Round #372 (Div. 2) E. Digit Tree(点分治,好题)
发布日期:2021-11-12 00:25:51 浏览次数:21 分类:技术文章

本文共 3773 字,大约阅读时间需要 12 分钟。

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 70 1 24 2 42 0 13 0 92 5 7
output
7
input
5 111 2 32 0 33 0 34 3 3
output
8
Note

In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different. 

In the second sample case, the interesting pairs are (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.

题意:

给一棵带边权的树,问树上有多少条(u,v)点对满足u->v的十进制大数表达形式能被m整除。

题解:

问题求的是路径方案数,我们考虑点分治,每次找到重心作为根后,做两遍dfs分别求出一条路径的十进制前缀形式和后缀形式,然后由后缀形式的模我们可以通过求乘法逆元来得到和它对应的前缀形式的模,用map保留前边遍历过的所有前缀形式的模的方案数,这样顺着做一遍只能求出所有前缀在前遍历后缀在后遍历的方案数,所以我们还要倒着边的顺序再来一遍。总复杂度最多应该是n*log(n)*log(n)。

这个题敲了好久,调试好久才AC啊==,太弱了。。。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define rep(i,a,n) for (int i=a;i
=a;i--)#define pb push_back#define fi first#define se secondtypedef vector
VI;typedef long long ll;typedef pair
PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100000+100;ll fact[maxn],inv[maxn];int n;ll m;int sz[maxn],mx[maxn];int vis[maxn];int root,mi;ll ans;map
mp;bool flag;ll ex_gcd(ll a,ll b,ll &x,ll &y)//扩展欧几里得{ if (a == 0 && b == 0) return -1; if (b == 0) {x = 1 ;y = 0;return a;} ll d = ex_gcd(b,a%b,y,x); y -= a/b*x; return d;}ll mod_inverse(ll a,ll n)//乘法逆元{ ll x,y; ll d = ex_gcd(a,n,x,y); return (x%n+n)%n;}void init(){ fact[0]=1;inv[0]=1; for(int i=1;i<=100000;i++) { fact[i]=fact[i-1]*10ll%m; inv[i]=mod_inverse(fact[i],m); }}vector
G[maxn];void dfssize(int u,int f){ sz[u]=1,mx[u]=0; for(int i=0;i
mx[u]) mx[u]=sz[v]; }}void dfsroot(int r,int u,int f){ if(sz[r]-sz[u]>mx[u]) mx[u]=sz[r]-sz[u]; if(mx[u]
=0;i--) { int v=G[u][i].first,w=G[u][i].second; if(!vis[v]) { dfs1(v,u,1,w%m); dfs2(v,u,1,w%m); } }}void dfs(int u){ mi=inf; dfssize(u,-1); dfsroot(u,u,-1); cal(root); vis[root]=1; int p=root; //这个地方要注意!因为root为全局变量,递归调用后再返回root不变,所以要寄存一下! for(int i=0;i

转载地址:https://blog.csdn.net/fouzhe/article/details/56278412 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SPOJ-TTM - To the moon(主席树,经典)
下一篇:Codeforces Beta Round #3 D. Least Cost Bracket Sequence(贪心,想法,好题)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月22日 00时34分11秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

oracle8i substr,Oracle中的INSTR,NVL和SUBSTR函数的用法详解 2019-04-21
导出oracle11g的空表,轻松解决oracle11g 空表不能 exp 导出 的问题。 2019-04-21
php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理 2019-04-21
oracle numlist,oracle sql str2numlist numtabletype 2019-04-21
php红包平均分配,红包平均分配算法 2019-04-21
linux磁盘的命令是,linux磁盘相关的命令 2019-04-21
linux服务器 缓存,Linux服务器内存使用分析及内存缓存 2019-04-21
linux查进程内存问题,关于linux 查看服务进程内存,cpu,内存占用的一些基础命令... 2019-04-21
linux英文包安装教程视频,Linux源码包安装过程讲解 2019-04-21
linux 关闭rsync服务器,linux下配置rsync服务器和实时同步 2019-04-21
linux初始化TCP服务失败,深入Linux系统追踪TCP初始化 2019-04-21
arch Linux添加源,在Arch Linux系统中使用Archlinuxcn源(清华源)的方法 2019-04-21
私人linux远程连接,Linux远程连接 - osc_5g1gl9wp的个人空间 - OSCHINA - 中文开源技术交流社区... 2019-04-21
windows文件迁移到linux,从Windows到Linux迁移之文件服务器(Samba和AD完美结合) 2019-04-21
linux下vi编辑器的命令大全,linux下VI编辑器命令大全(超级完整版) 2019-04-21
linux结构体数组的定义数组,task_struct结构体中的run_list和array域 2019-04-21
C语言极坐标转直角坐标,C语言实现直角坐标转换为极坐标的方法 2019-04-21
16F877A和24C02通信汇编语言,PIC16f877A读写24c02程序 2019-04-21
用c语言编写小于n的所有素数,关于求N以内素数的一点小问题(N小于一亿) 2019-04-21
华为100万部鸿蒙,2019年Q4发布 华为100万部鸿蒙OS手机已开测 2019-04-21