Description

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

一天,程序员 ZS 无聊,他决定研究一下这棵树的一些特性。他选择了一个十进制正整数 \(M\),\(\gcd(M,10)=1\)。

对于一对有序的不同的顶点 \((u, v)\),他沿着从顶点 \(u\) 到顶点 \(v\)的最短路径,按经过顺序写下他在路径上遇到的所有数字(从左往右写),如果得到一个可以被 \(M\) 整除的十进制整数,那么就认为 \((u,v)\) 是有趣的点对。

帮助程序员 ZS 得到有趣的对的数量。

Hint

  • \(1\le n\le 10^5\)
  • \(1\le m\le 10^9,\gcd(m, 10) = 1\)
  • \(1\le \text{边权} < 10\)

Solution

这种树上路径的统计问题基本都是 点分治,而点分治的重点和难点就是如何 统计经过分治中心的满足条件的路径的个数

这里采用 容斥法:即现分治中心为 \(s\),当前答案等于整个子树 \(s\) 的答案减去以 \(s\) 各个子结点为根的子树的答案。

考虑如何统计。

我们设有一条路径是 \(x\rightarrow y\),分治中心为 \(s\),路径 \(x\rightarrow s\) 对应的数字为 \(pd\),\(s\rightarrow y\) 对应 \(nd\),\(s\) 到 \(y\) 的距离为 \(l\)。

那么只有 \(pd \times 10^l + nd \equiv 0 \pmod m\) 成立时满足要求。

变形一下:\(pd \equiv -nd \times 10^{-l}\pmod m\)。

于是我们可以这样搞:把所有的 \(pd\) 用 map 存起来,记录一下个数,用 pair 数组把 \((nd, l)\) 记录下来。

导入所有了路径信息后,枚举 pair 数组,查找 map 中的元素配对即可。

预处理一下 \(10\) 的幂及其逆元的话,时间复杂度 \(O(n\log^2 n)\)。如果用 Hash Table 可以优化到理论 \(O(n\log n)\),但没什么必要。

Code

/*
* Author : _Wallace_
* Source : https://www.cnblogs.com/-Wallace-/
* Problem : Codeforces 715E Digit Tree
*/
#include <cstdio>
#include <map>
#include <utility>
#include <vector> using namespace std;
const int N = 1e5 + 5; namespace Inv {
void extgcd(long long a, long long b, long long& x, long long& y) {
if (!b) x = 1, y = 0;
else extgcd(b, a % b, y, x), y -= a / b * x;
}
inline long long get(long long b, long long p) {
long long x, y;
extgcd(b, p, x, y);
x = (x % p + p) % p;
return x;
}
} int n, m;
long long p10[N], invp[N];
long long ans;
struct edge { int to, len; };
vector<edge> G[N]; int root;
int maxp[N], size[N];
bool centr[N]; int getSize(int x, int f) {
size[x] = 1;
for (auto y : G[x])
if (!centr[y.to] && y.to != f)
size[x] += getSize(y.to, x);
return size[x];
}
void getCentr(int x, int f, int t) {
maxp[x] = 0;
for (auto y : G[x])
if (!centr[y.to] && y.to != f) {
getCentr(y.to, x, t);
maxp[x] = max(maxp[x], size[y.to]);
}
maxp[x] = max(maxp[x], t - size[x]);
if (maxp[x] < maxp[root]) root = x;
} vector<pair<long long, int> > dat;
map<long long, int> cnt; void getData(int x, int f, long long pd, long long nd, int dep) {
if (dep >= 0) cnt[pd]++, dat.push_back(make_pair(nd, dep));
for (auto y : G[x]) {
if(centr[y.to] || y.to == f) continue;
long long tpd = (pd + y.len * p10[dep + 1] % m) % m;
long long tnd = (nd * 10 % m + y.len) % m;
getData(y.to, x, tpd, tnd, dep + 1);
}
} inline long long count(int x, int d) {
long long ret = 0;
cnt.clear(), dat.clear();
if (d == 0) getData(x, 0, 0, 0, -1);
else getData(x, 0, d % m, d % m, 0); for (auto p : dat) {
long long t = ((-p.first * invp[p.second + 1] % m) + m) % m;
if (cnt.count(t)) ret += cnt[t];
if (d == 0 && p.first == 0) ++ret;
}
return ret + (d == 0 ? cnt[0] : 0);
} void solve(int x) {
maxp[root = 0] = N;
getCentr(x, 0, getSize(x, 0));
int s = root; centr[s] = true; for (auto y : G[s])
if (!centr[y.to])
solve(y.to); ans += count(s, 0);
for (auto y : G[s])
if (!centr[y.to])
ans -= count(y.to, y.len);
centr[s] = false;
} signed main() {
scanf("%d%d", &n, &m);
for (register int i = 1; i < n; i++) {
int u, v, l;
scanf("%d%d%d", &u, &v, &l);
++u, ++v;
G[u].push_back(edge{v, l});
G[v].push_back(edge{u, l});
} p10[0] = 1 % m;
for (register int i = 1; i <= n; i++)
p10[i] = p10[i - 1] * 10 % m;
invp[n] = Inv::get(p10[n], m);
for (register int i = n - 1; i; i--)
invp[i] = invp[i + 1] * 10 % m; ans = 0, solve(1);
printf("%lld\n", ans);
return 0;
}

【Codeforces 715C】Digit Tree(点分治)的更多相关文章

  1. [Codeforces 715C] Digit Tree

    [题目链接] https://codeforces.com/contest/715/problem/C [算法] 考虑点分治 一条路径(x , y)合法当且仅当 : d(x) * 10 ^ dep(x ...

  2. CF 716E. Digit Tree [点分治]

    题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除\(P\) 路径统计一般就是点分治了 \[ a*10^{deep} + b \ \equiv \pmod P\ ...

  3. CF716E Digit Tree 点分治

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

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

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

  5. Codeforces 716 E Digit Tree

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

  6. 【题解】Digit Tree

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

  7. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  8. Codeforces 1039D You Are Given a Tree [根号分治,整体二分,贪心]

    洛谷 Codeforces 根号分治真是妙啊. 思路 考虑对于单独的一个\(k\)如何计算答案. 与"赛道修建"非常相似,但那题要求边,这题要求点,所以更加简单. 在每一个点贪心地 ...

  9. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

随机推荐

  1. Python_用PyQt5 建 notepad 界面

    用PyQt5建notepad界面 1 # -*-coding:utf-8 -*- 2 """ 3 简介:用PyQt5做一个对话框,有菜单(2个.有独立图标.快捷键).提示 ...

  2. springboot中aop的使用

    Spring AOP(Aspect Oriented Programming),即面向切面编程,是OOP(Object Oriented Programming,面向对象编程)的补充和完善. OOP引 ...

  3. Ubuntu16.04安装搜狗输入法报错:dkpg:处理归档sogoupinyin.deb(--install)时出错,安装sogoupinyin将破坏fcitx-ui-qimpanel

    系统:ubuntu16.04 事件:安装搜狗拼音时报错 报错信息(ubuntu语言是英文的报错信息): dpkg: regarding sogoupinyin_2.3.2.07_amd64-831.d ...

  4. 思维导图MindManager的过滤主题功能如何使用

    MindManager是一款多功能思维导图工具软件.但有的思维导图繁杂,用户只需要查看自己感兴趣的主题该怎么办呢?接下来,我就为大家详细介绍MindManager思维导图2020版的过滤主题功能,可以 ...

  5. 怎么用MindManager自带的模板和设计画思维导图

    小编知道大家平时工作学习都很忙,思维导图能完成的效率越高越好.所以今天,小编就为大家介绍两个能高效使用思维导图软件完成制作思维导图的小技巧.保证内容充实美观,还不费时间. 一.使用模板 打开MindM ...

  6. FL Studio中的音频剪辑功能讲解

    音频剪辑,是FL Studio中的一个特色功能,音频剪辑的目的是保持在播放列表中显示和触发的音频,可以根据需要对它们进行切片和排列.但需要注意的是音频剪辑这个功能在FL Studio的基础版(果味版) ...

  7. JUC并发工具包之CyclicBarrier

    1.简介 CyclicBarrier是一个同步器,允许多个线程等待彼此直到达一个执行点(barrier). CyclicBarrier都是在多个线程必须等到彼此都到达同一个执行点后才执行一段逻辑时才被 ...

  8. windows安装redis扩展

    Thread Safety enabled 打开phpinfo() 看php版本是ts还是nts,  如上是ts版本的,所以需要安装redis的ts版本, redis的扩展下载地址 https://p ...

  9. 关于String类的一些知识点

    //原链接:https://blog.csdn.net/songylwq/article/details/7297004 String str=new String("abc"); ...

  10. Java基础教程——安装JDK

    视频讲解:https://www.bilibili.com/video/av48196406/?p=3 使用[jdk-8u144-windows-x64.exe] 下载地址: 链接:https://p ...