ZS the Coder has a large tree. It can be represented as an undirected connected graph ofn vertices numbered from 0 to n - 1 and n - 1 edges between them. There is a single nonzero digit written on each edge.
One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integerM, which is
coprime to 10, i.e.
.
ZS consider an ordered pair of distinct vertices(u, v) interesting when if he would follow the shortest path from vertex u to vertex v and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible byM.
Formally, ZS consider an ordered pair of distinct vertices (u, v) interesting if the following states true:
- Let a1 = u, a2, ..., ak = v be the sequence of vertices on the shortest path from u to v in the order of encountering them;
- Let di (1 ≤ i < k) be the digit written on the edge between verticesai andai + 1;
- The integer
is divisible byM.
Help ZS the Coder find the number of interesting pairs!
The first line of the input contains two integers, n andM (2 ≤ n ≤ 100 000, 1 ≤ M ≤ 109,) —
the number of vertices and the number ZS has chosen respectively.
The next n - 1 lines contain three integers each.i-th of them contains ui, vi andwi, denoting an edge between verticesui andvi with digitwi written on it (0 ≤ ui, vi < n, 1 ≤ wi ≤ 9).
Print a single integer — the number of interesting (by ZS the Coder's consideration) pairs.
6 7 0 1 2 4 2 4 2 0 1 3 0 9 2 5 7
7
5 11 1 2 3 2 0 3 3 0 3 4 3 3
8
In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of7. Note that (2, 5) and(5, 2) are considered different.

In the second sample case, the interesting pairs are (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4), and6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.

分析:问题求的是路径方案数,我们考虑点分治,每次找到重心作为根后,做两遍dfs分别求出一条路径的十进制前缀形式和后缀形式,然后由后缀形式的模我们可以通过求乘法逆元来得到和它对应的前缀形式的模,用map保留前边遍历过的所有前缀形式的模的方案数,这样顺着做一遍只能求出所有前缀在前遍历后缀在后遍历的方案数,所以我们还要倒着边的顺序再来一遍。总复杂度最多应该是n*log(n)*log(n)。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define MAXN 100005
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
vector<pii> G[MAXN];
vector<int> b;
int n,m,k,root,head,size[MAXN];
ll ans,tx[MAXN],ntx[MAXN];
bool flag,jud[MAXN];
map <int,int> f1;
int got_size(int u,int fa)
{
int num = 1;
for(int i = 0;i < G[u].size();i++)
{
pii vv = G[u][i];
if(vv.first != fa && !jud[vv.first]) num += got_size(vv.first,u);
}
return num;
}
int dfs(int u,int fa,int tot) //找重心
{
bool flag = true;
size[u] = 1;
for(int i = 0;i < G[u].size();i++)
{
pii vv = G[u][i];
if(vv.first != fa && !jud[vv.first])
{
int v = vv.first;
int tmp = dfs(v,u,tot);
if(tmp) return tmp;
size[u] += size[v];
if(size[v] > tot/2 ) flag = false;
}
}
if(tot - size[u] > tot/2) flag = false;
return flag ? u : 0;
}
void dfs2(int u,int fa,int deep,int sta1)
{
if(!sta1 && flag) ans++;
f1[sta1]++;
for(pii it : G[u])
{
int v = it.first,val = it.second;
if(v != fa && !jud[v])
dfs2(v,u,deep+1,(1ll*val*tx[deep] + sta1) % m);
}
}
void dfs3(int u,int fa,int deep,int sta2)
{
if(!sta2 && flag) ans++;
ans += f1[(m*1ll - sta2)*ntx[deep]%m];
for(pii it : G[u])
{
int v = it.first,val = it.second;
if(v != fa && !jud[v])
dfs3(v,u,deep+1,(sta2*10ll + val) % m);
}
}
void deal(int u)
{
flag = true;
f1.clear();
for(int i = 0;i < G[u].size();i++)
{
int v = G[u][i].first,val = G[u][i].second;
if(!jud[v])
{
dfs3(v,u,1,val % m);
dfs2(v,u,1,val % m);
}
}
f1.clear();
flag = false;
for(int i = G[u].size()-1;i >= 0;i--)
{
int v = G[u][i].first,val = G[u][i].second;
if(!jud[v])
{
dfs3(v,u,1,val % m);
dfs2(v,u,1,val % m);
}
}
}
void calc(int u)
{
deal(u);
jud[u] = true;
for(int i = 0;i < G[u].size();i++)
{
pii vv = G[u][i];
if(!jud[vv.first])
{
int v = vv.first,val = vv.second;
int root = dfs(v,u,got_size(v,u));
calc(root);
}
}
}
ll ex_gcd(ll a,ll b,ll &x,ll &y)//扩展欧几里得
{
if (a == 0 && b == 0) return -1;
if (b == 0) {x = 1 ;y = 0;return a;}
ll d = ex_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
}
ll mod_inverse(ll a,ll n)//乘法逆元
{
ll x,y;
ll d = ex_gcd(a,n,x,y);
return (x%n+n)%n;
}
void nextInt(int &x) //输入外挂
{
char c;
do c = getchar(); while ( c < '0' || c > '9');
x = c-'0';
while ('0' <= (c = getchar()) && c<='9') x = x*10 + c - '0';
}
int main()
{
nextInt(n),nextInt(m);
tx[0] = 1ll;
for(int i = 1;i <= 100000;i++)
{
tx[i] = tx[i-1]*10ll % m;
ntx[i] = mod_inverse(tx[i],m);
}
for(int i = 1;i < n;i++)
{
int u,v,val;
nextInt(u),nextInt(v),nextInt(val);
u++,v++;
G[u].push_back(make_pair(v,val));
G[v].push_back(make_pair(u,val));
}
root = dfs(1,0,n); //重心
calc(root); //根
cout<<ans<<endl;
}