CF715C.Digit Tree

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 - 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 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 _a_1 = u, _a_2, ..., a__k = v be the sequence of vertices on the shortest path from u to v in the order of encountering them;
  • Let d__i (1 ≤ i < k) be the digit written on the edge between vertices a__i and a__i + 1;
  • The integer is divisible by M.

Help ZS the Coder find the number of interesting pairs!

ZS 这位程序员有一棵很大的树。该树可以表示为一个包含 $ n $ 个顶点(编号从 $ 0 $ 到 $ n-1 $)和 $ n-1 $ 条边的无向连通图,且每条边上都写有一个非零数字(即 $ 1 $ 至 $ 9 $ 中的一个数字)。

某天,ZS 感到无聊,决定研究这棵树的一些性质。他选择了一个正整数 $ M $,且 $ M $ 与 $ 10 $ 互质,即 。

ZS 将一个有序的、顶点互异的顶点对 $ (u, v) $ 称为“有趣的”,当且仅当他沿着从顶点 $ u $ 到顶点 $ v $ 的最短路径行走,并按经过顺序写下路径上所有边上的数字时,所得到的十进制整数能被 $ M $ 整除。

形式化地,ZS 认为一个有序的、顶点互异的顶点对 $ (u, v) $ 是“有趣的”,当且仅当满足以下条件:

  • 设 $ a_1 = u,, a_2,, \dots,, a_k = v $ 为从 $ u $ 到 $ v $ 的最短路径上按访问顺序排列的顶点序列;
  • 对每个 $ 1 \le i < k $,记 $ d_i $ 为连接顶点 $ a_i $ 与 $ a_{i+1} $ 的边上的数字;
  • 整数 能被 $ M $ 整除。

请帮助 ZS 这位程序员计算“有趣的”顶点对的总数!

输入格式

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 u__i, v__i and w__i, denoting an edge between vertices u__i and v__i with digit w__i written on it (0 ≤ u__i, v__i < n,  1 ≤ w__i ≤ 9).

输入的第一行包含两个整数 nn 和 MM(2≤n≤100 0002 \leq n \leq 100\,000,1≤M≤1091 \leq M \leq 10^9,),分别表示顶点的数量以及 ZS 所选定的数。

接下来的 n−1n-1 行每行包含三个整数。其中第 ii 行包含 uiu_i、viv_i 和 wiw_i,表示在顶点 uiu_i 与 viv_i 之间存在一条边,该边上写有一个数字 wiw_i(0≤ui,vi<n0 \leq u_i, v_i < n,1≤wi≤91 \leq w_i \leq 9)。

输出格式

Print a single integer — the number of interesting (by ZS the Coder's consideration) pairs.

输出一个整数——有趣的(在 ZS the Coder 看来)数对的个数。

输入输出样例

  • 输入#1

    6 7
    0 1 2
    4 2 4
    2 0 1
    3 0 9
    2 5 7

    输出#1

    7
  • 输入#2

    5 11
    1 2 3
    2 0 3
    3 0 3
    4 3 3

    输出#2

    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.

在第一个样例中,有趣的数对有 (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5)(0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5)。这些数对所构成的数字分别为 14, 21, 217, 91, 7, 7, 91714,\,21,\,217,\,91,\,7,\,7,\,917,它们均为 77 的倍数。注意:(2, 5)(2, 5) 与 (5, 2)(5, 2) 被视为不同的数对。

在第二个样例中,有趣的数对有 (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4)(4, 0),\,(0, 4),\,(3, 2),\,(2, 3),\,(0, 1),\,(1, 0),\,(4, 1),\,(1, 4),其中 66 个数对构成的数字为 3333,另外 22 个数对构成的数字为 33333333,它们均为 1111 的倍数。

输入解题思路,AI测评打分。不知道怎么写?

首页