ZS the Coder has a large tree. It can be represented as an undirected connected graph of n vertices numbered from 0 to n - 1 and n - 1edges 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 integer M, 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 by M.
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 vertices ai and ai + 1;
- The integer is divisible by M.
Help ZS the Coder find the number of interesting pairs!
The first line of the input contains two integers, n and M (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 and wi, denoting an edge between vertices ui and vi with digit wi 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 are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. 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), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.
题意:
给一棵带边权的树,问树上有多少条(u,v)点对满足u->v的十进制大数表达形式能被m整除。
题解:
问题求的是路径方案数,我们考虑点分治,每次找到重心作为根后,做两遍dfs分别求出一条路径的十进制前缀形式和后缀形式,然后由后缀形式的模我们可以通过求乘法逆元来得到和它对应的前缀形式的模,用map保留前边遍历过的所有前缀形式的模的方案数,这样顺着做一遍只能求出所有前缀在前遍历后缀在后遍历的方案数,所以我们还要倒着边的顺序再来一遍。总复杂度最多应该是n*log(n)*log(n)。
这个题敲了好久,调试好久才AC啊==,太弱了。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=100000+100;
ll fact[maxn],inv[maxn];
int n;
ll m;
int sz[maxn],mx[maxn];
int vis[maxn];
int root,mi;
ll ans;
map<int,int> mp;
bool flag;
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 init()
{
fact[0]=1;inv[0]=1;
for(int i=1;i<=100000;i++)
{
fact[i]=fact[i-1]*10ll%m;
inv[i]=mod_inverse(fact[i],m);
}
}
vector<PII> G[maxn];
void dfssize(int u,int f)
{
sz[u]=1,mx[u]=0;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first;
if(vis[v]||v==f) continue;
dfssize(v,u);
sz[u]+=sz[v];
if(sz[v]>mx[u]) mx[u]=sz[v];
}
}
void dfsroot(int r,int u,int f)
{
if(sz[r]-sz[u]>mx[u]) mx[u]=sz[r]-sz[u];
if(mx[u]<mi) mi=mx[u],root=u;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first;
if(v==f||vis[v]) continue;
dfsroot(r,v,u);
}
}
void dfs1(int u,int f,int dep,int st)
{
if(!st&&flag) ans++;
int p=(m-st)*inv[dep]%m;
ans+=mp[p];
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first,w=G[u][i].second;
if(vis[v]||v==f) continue;
dfs1(v,u,dep+1,(st*10ll+w)%m);
}
}
void dfs2(int u,int f,int dep,int st)
{
if(!st&&flag) ans++;
mp[st]++;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first,w=G[u][i].second;
if(vis[v]||v==f) continue;
dfs2(v,u,dep+1,(1ll*w*fact[dep]+st)%m);
}
}
void cal(int u)
{
mp.clear();
flag=true;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first,w=G[u][i].second;
if(!vis[v])
{
dfs1(v,u,1,w%m); //
dfs2(v,u,1,w%m);
}
}
mp.clear();
flag=false;
for(int i=G[u].size()-1;i>=0;i--)
{
int v=G[u][i].first,w=G[u][i].second;
if(!vis[v])
{
dfs1(v,u,1,w%m);
dfs2(v,u,1,w%m);
}
}
}
void dfs(int u)
{
mi=inf;
dfssize(u,-1);
dfsroot(u,u,-1);
cal(root);
vis[root]=1;
int p=root; //这个地方要注意!因为root为全局变量,递归调用后再返回root不变,所以要寄存一下!
for(int i=0;i<G[p].size();i++)
{
int v=G[p][i].first;
if(vis[v]) continue;
dfs(v);
}
}
int main()
{
scanf("%d%lld",&n,&m);
init();
rep(i,1,n)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
u++,v++;
G[u].pb(make_pair(v,w)),G[v].pb(make_pair(u,w));
}
ans=0;
memset(vis,0,sizeof(vis));
dfs(1);
printf("%lld\n",ans);
return 0;
}