Given a weighted directed graph $G$ with cycles and multiple edges between two nodes, I need to find how many ordered pairs $(x, y)$ exist such that the total path length from $x\ to \ y$ ends with digit $0$.
It might also happen that we can go round and round a cycle and then move further. i.e $1-2-3-1-2-3-1-2$ and $1-2$ can have different path lengths and if any one or more of them ends with $0$, we will count $(1,2) \ once$.
$\therefore$ an ordered pair can represent multiple paths, but it should be counted once.
My Approach:
Lets say $f(u, d)$ be number of ordered pairs $(u,x)$ such that the sum of weights of path from $u$ to $x$ end with digit $d \in \{0,1,2,...,9\}$.
Consider all edges from $u-v$ with weight mod 10 = $w$.
$f(u, d)$ = Sum( $f(v, (d-w+10) mod 10)$ )
This builds a recursion but I don't know about the base case.