CF 716E. Digit Tree [点分治]
题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除$P$
路径统计一般就是点分治了
\]
\]
经过一个点的路径,统计出从根走到一个点的数字\(b\),和从点走到根的数字\(a\),然后a排序枚举b二分查找范围就行了
然后再减去同一颗子树的
这样可以避免使用平衡树
Candy?这个沙茶一开始ab搞反了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
#define pii pair<ll, ll>
#define MP make_pair
#define fir first
#define sec second
const int N=1e5+5, INF=1e9;
int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, P, u, v, w;
struct edge{int v, w, ne;}e[N<<1];
int cnt, h[N];
inline void ins(int u, int v, int w) {
e[++cnt]=(edge){v, w, h[u]}; h[u]=cnt;
e[++cnt]=(edge){u, w, h[v]}; h[v]=cnt;
}
int size[N], f[N], root, All, vis[N];
void dfsRt(int u, int fa) {
size[u]=1; f[u]=0;
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].v != fa) {
dfsRt(e[i].v, u);
size[u] += size[e[i].v];
f[u] = max(f[u], size[e[i].v]);
}
f[u] = max(f[u], All-size[u]);
if(f[u] < f[root]) root = u;
}
ll Pow[N];
int deep[N], m;
ll a[N], ans; pii g[N], b[N];
void dfsIfo(int u, int fa) { //printf("dfsIfo %d %d %lld %lld\n", u, deep[u], g[u].fir, g[u].sec);
a[++m] = g[u].sec, b[m] = MP(g[u].fir, deep[u]);
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].v != fa) {
deep[e[i].v] = deep[u]+1;
g[e[i].v] = MP( (g[u].fir * 10 + e[i].w)%P, (e[i].w * Pow[deep[u]] + g[u].sec)%P );
dfsIfo(e[i].v, u);
}
}
void exgcd(int a, int b, int &d, int &x, int &y) {
if(b==0) d=a, x=1, y=0;
else exgcd(b, a%b, d, y, x), y -= (a/b)*x;
}
ll inv(int a, int b) {
int d, x, y;
exgcd(a, b, d, x, y);
return d==1 ? (x+b)%b : -1;
}
void cal(int u, int val) { //printf("\ncal %d %d %lld\n",u, val, ans);
sort(a+1, a+1+m); //for(int i=1; i<=m; i++) printf("%lld ",a[i]);puts("");
for(int i=1; i<=m; i++) {
ll x = b[i].fir, d = b[i].sec;
ll v = (P - x) * inv(Pow[d], P) % P;
int l = lower_bound(a+1, a+1+m, v) - a, r = upper_bound(a+1, a+1+m, v) - a;
//printf("vvv %lld %d %d %d %d\n",x, d, v,l,r);
ans += (r-l)*val;
}
//printf("ans %d\n\n",ans);
}
void dfsSol(int u) { //printf("\nDDDDDDDDDDDDDDDdfsSol %d\n",u);
vis[u]=1;
deep[u]=0; g[u].fir = g[u].sec = 0;
m=0; dfsIfo(u, 0); cal(u, 1);
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v]) {
int v = e[i].v;
deep[v]=1; g[v].fir = g[v].sec = e[i].w;
m=0; dfsIfo(v, 0); cal(v, -1);
All = size[v]; root=0; dfsRt(v, 0); //printf("hiroot %d %d\n",v,root);
dfsSol(root);
}
}
int main() {
//freopen("in","r",stdin);
n=read(); P=read(); Pow[0]=1;
for(int i=1; i<n; i++) u=read()+1, v=read()+1, ins(u, v, read()%P), Pow[i] = Pow[i-1]*10%P;
All=n; root=0; f[0]=INF;
dfsRt(1, 0); //printf("root %d\n",root);
dfsSol(root);
//printf("%lld",ans-n);
cout << ans-n;
}
CF 716E. Digit Tree [点分治]的更多相关文章
- CF716E Digit Tree 点分治
题意: 给出一个树,每条边上写了一个数字,给出一个P,求有多少条路径按顺序读出的数字可以被P整除.保证P与10互质. 分析: 统计满足限制的路径,我们首先就想到了点分治. 随后我们就需要考量,我们是否 ...
- 【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\) 条边.每条边上都有一个非零的数字. ...
- 【题解】Digit Tree
[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...
- CF715C:Digit Tree
传送门 一句话怎么说来着 算法+高级数据结构=OI 现在我感觉到的是 我会的算法+我会的高级数据结构=WA 这道题提交了三四十次,从刚看题到完全写好花了好几天..,主要死于看错费马小定理的适用条件. ...
- Codeforces 716 E Digit Tree
E. Digit Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- 【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 ...
随机推荐
- DFS(dfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2212 DFS Time Limit: 5000/2000 MS (Java/Others) Me ...
- oracle和mysql分页
mysql分页 关键字limit,limit m,n 其中m表示起始位置的下标,下标从0开始.n表示要显示的条数,比如要查询一个表的第2到5条数据. ,; oracle分页 关键字rownum, ro ...
- oracle创建触发器及作用举例
--创建触发器及作用举例 create or replace trigger tri before delete on emp --在删除emp表数据之前需要做的事根据自己的业务去写,before是在 ...
- Unity 小笔记
1,Time.deltatime放在Update和fixedupdate中得到的值是不一样的.还以为是通过两个值来获取. 2,VR中绘制射线可以使用LineRender. 3,Unity中判断一个东西 ...
- Map,List,POJO深拷贝(序列化实现)方法与注意事项
转载请注明出处,谢谢! 方法1: /*jdk >= 1.5*/ @SuppressWarnings("unchecked") public static <T> ...
- arclistsg文档独立模型标签
[标签名称] arclistsg [标签简介] 单表独立模型的文档列表调用标记 [功能说明] 用于调用单表模型的内容,在V5.3系统以上版本中加入了单表模型的概念,脱离了以前的主从表的数据表关联结构, ...
- ZooKeeper集群的安装、配置、高可用测试
Dubbo注册中心集群Zookeeper-3.4.6 Dubbo建议使用Zookeeper作为服务的注册中心. Zookeeper集群中只要有过半的节点是正常的情况下,那么整个集群对外就是可用的.正是 ...
- Linux文件
Linux文件类型 对于内核而言,所有打开的文件都是通过文件描述符引用(FD),文件描述符是一个非负整数,当打开现有问价或创建一个新文件时,内核向进程返回一个文件描述符. 按照惯例,shell把文件描 ...
- NtDuplicateObject小解读
源进程和目标进程可以是一个吗 当然执行进程可以是同一个吗 ,当然标志位重要!有一个关闭源进程的标志位 第一步通过ObReferenceHandleTable获得源进程对象(数据结构) //为新的句柄构 ...
- 利用mk-table-checksum监测Mysql主从数据一致性操作记录
前面已经提到了mysql主从环境下数据一致性检查:mysql主从同步(3)-percona-toolkit工具(数据一致性监测.延迟监控)使用梳理今天这里再介绍另一种Mysql数据一致性自动检测工具: ...