题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除$P$


路径统计一般就是点分治了

\[a*10^{deep} + b \ \equiv \pmod P
\]

\[a = (P-b)*inv(10^{deep})
\]

经过一个点的路径,统计出从根走到一个点的数字\(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 [点分治]的更多相关文章

  1. CF716E Digit Tree 点分治

    题意: 给出一个树,每条边上写了一个数字,给出一个P,求有多少条路径按顺序读出的数字可以被P整除.保证P与10互质. 分析: 统计满足限制的路径,我们首先就想到了点分治. 随后我们就需要考量,我们是否 ...

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

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

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

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

  4. 【题解】Digit Tree

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

  5. CF715C:Digit Tree

    传送门 一句话怎么说来着 算法+高级数据结构=OI 现在我感觉到的是 我会的算法+我会的高级数据结构=WA 这道题提交了三四十次,从刚看题到完全写好花了好几天..,主要死于看错费马小定理的适用条件. ...

  6. Codeforces 716 E Digit Tree

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

  7. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  8. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  9. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

随机推荐

  1. UVA 1584 字符串

    VJ 该题 链接  https://vjudge.net/problem/UVA-1584 AC代码   字典序最小输出 #include <stdio.h> #include <m ...

  2. D. Longest Subsequence

    D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. Java数据结构和算法(十二)——2-3-4树

    通过前面的介绍,我们知道在二叉树中,每个节点只有一个数据项,最多有两个子节点.如果允许每个节点可以有更多的数据项和更多的子节点,就是多叉树.本篇博客我们将介绍的——2-3-4树,它是一种多叉树,它的每 ...

  4. APP测试时常用adb命令

    ADB全称Android Debug Bridge, 是android sdk里的一个工具, 用这个工具可以直接操作管理android模拟器或者真实的andriod设备(手机),故在其实工作可以给我们 ...

  5. Logback日志配置的简单使用

    Logback介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access ...

  6. zookeeper命令行操作

    创建 #[-s] 顺序 #[-e] 临时节点 #path 节点 #data 该节点存储的数据 #acl 证书 create [-s] [-e] path data acl -s或-e指定节点特性:顺序 ...

  7. css background之设置图片为背景技巧

    css background之设置图片为背景技巧-css 背景 Background是什么意思,翻译过来有背景意思.同样在css里面作为css属性一成员同样是有背景意思,并且是设置背景图片.背景颜色. ...

  8. IOS @proporty 关键字(一)retain strong

    @interface User : NSObject @property (nonatomic,retain) NSString* tRetain; @property (nonatomic,assi ...

  9. 2017-07-04(sudo wc sort)

    sudo 作用 root把本来只能超级用户执行的命令,赋予普通用户执行. 添加 运行visudo命令,在文件底部添加信息即可! sudo -l  查看用户可以运行的命令 use1  ALL=(ALL) ...

  10. tone()函数的有趣的使用案例

    tong()除了可以驱动蜂鸣器之外,还可以驱动步进电机(测试很好用) 一个引脚上产生一个特定频率的方波(%占空比).持续时间可以设定,否则波形会一直产生直到调用noTone()函数.该引脚可以连接压电 ...