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 ...
随机推荐
- Java集合系列[4]----LinkedHashMap源码分析
这篇文章我们开始分析LinkedHashMap的源码,LinkedHashMap继承了HashMap,也就是说LinkedHashMap是在HashMap的基础上扩展而来的,因此在看LinkedHas ...
- centos7+cdh5.10.0搭建
一.选择环境: 1.说明 本次部署使用台机器,3台用于搭建CDH集群,1台为内部源.内部源机器是可以连接公网的,可以提前部署好内部源,本次部署涉及到的服务器的hosts配置如下: 192.168.10 ...
- linux(CENTOS)系统各个目录的作用详解
Linux(CentOS)系统各个目录的作用详解 文件的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.S ...
- Android中与task相关的几个属性
1.与任务相关的属性 taskAffinity :修改任何给定Activity的关联 系统使用包名标识应用的默认任务关联: taskAffinity属性取字符串值,必须不同于包名: taskAffin ...
- [国嵌攻略][092][UDP网络程序设计]
server.c #include <sys/socket.h> #include <netinet/in.h> #include <strings.h> #inc ...
- JQeury添加和删除class内部实现代码(简化版)
下面是JQuery对元素class操作的简单实现,请看代码: 添加class: //增加class function addClass(elem,value) { var classes, cur, ...
- setTimeout()方法,你真的懂吗?
今天在群里看到了一道经典的javascript题型,之前也遇到过,可是再次遇到时,还是做错,还是不理解,因此这里来做个笔记吧! 不说了,直接上代码吧 for(var i=1; i<=9; i++ ...
- UE4 AsnycTask
使用AsnycTask可以将制定代码放在指定线程中执行,例如更新文理必须放在游戏线程. AsyncTask(ENamedThreads::GameThread, [=](){ updateT ...
- PHP中put和post区别
1. 使用支持和范围的区别: PHP提供了对PUT方法的支持,在Http定义的与服务器的交互方法中,PUT是把消息本体中的消息发送到一个URL,形式上跟POST类似; PHP 提供对诸如 Netsca ...
- vue前后台数据交互vue-resource文档
地址:https://segmentfault.com/a/1190000007087934 Vue可以构建一个完全不依赖后端服务的应用,同时也可以与服务端进行数据交互来同步界面的动态更新. Vue通 ...