题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除$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. 数学3(博弈+splya)

    数学3(博弈+splya) 标签: 数学 hdu_5194 (打表找规律) 题意 有n和黑球和m个白球,现在一个个的取出这些球,如果是黑球则当前标记为1,白球为0,那么当取完这些球会得到一些序列.问你 ...

  2. .NET MongoDB Driver 2.2 API注释

    主要内容 1 MongoClient 1.1构造函数 1.2 方法 2 IMongoDatabase 3 IMongoCollection 4 IMongoCollectionExtensions 5 ...

  3. javascript如何处理多级的实时监听

    今日工作中遇到需求,要求js代码对表单中的input内容进行实时监听,当input中的值改变时触发一些事件. 按照常规思维,代码很快写完了. $(function () { $("#inpu ...

  4. [学习OpenCV攻略][016][RedHat下安装OpenCV]

    安装环境 操作系统: Red Hat Enterprise Linux Server 6.3 相关软件: ffmpeg-0.8.15.tar.bz2.cmake-3.5.1.tar.gz.OpenCV ...

  5. 用PHPMailer在本地win环境,可以接收到邮件和附件,但在linux环境只能接收邮件信息接不到附件,是我的路

    解决了,Linux区分大小写问题

  6. Angular CLI: 发布到 GitHub Pages

    发布 Angular 应用的简单方式是使用 GitHub Pages. 首先需要创建一个 GitHub 账号,随后,为您的项目创建一个仓库.记下 GitHub 中的用户名和项目名称. 例如,我的 Gi ...

  7. 【编程技巧】 iOS 5的StoryBoard(故事板)的一些用法

    从StroyBoard得到一个View UIViewController *viewController = [[UIStoryboard storyboardWithName:@"Main ...

  8. Centos6.9安装部署nginx服务器

    (一)依赖包安装 首先,gcc,pcre,zlib,openssl安装一边(可以用非-devel,但是嫌麻烦....用非-devel的看这个链接) yum  -y install gcc ------ ...

  9. 注释中不允许出现字符串 "--"。

    问题: 在启动tomcat时会出现如上错误,同时有可能会出现xml无法解析等错误 解决办法: 注释中不能出现字符串 "--",即需要把xml文件中多余的“--”去掉,例如: < ...

  10. Maven1-HelloWorld简单入门

    编写POM(Project Object Model) Maven项目的核心是pom.xml,它定义了项目的基本信息,用于描述项目如何构建,声明项目依赖 创建文件夹,名称为hello-world 创建 ...