To calculate how many times a certain year repeats itself in the calendar within a given year range
Basically, the "configuration" of a year, if I understand well can be described as a state $Y_i = (D_i, L_i)$ with $D_i$ the weekday of January the $1^{st}$ (0 to 6 for monday to sunday) and $L_i$ which is 1 if $Y_i$ is a leap year and 0 else.

So, comparing the state of 2 years, you can decide wether they have the same configuration.

As a year has 365 days (366 for a leap year) and $365 \mod 7 = 1$ (2 for a leap year), there is a simple relation:

$D_{i+1} = D_i+L_i+1 \mod 7$

As there is one leap year every four years (without 100-exception), it is interesting to also notice there is a 5-step on weekday every 4 years:

$D_{i+4} = D_i+5 \mod 7$

Now, how to not loop on all year ?

Let's consider leap years are all years divisible by 4 without exception. Then, from a starting year $Y_0$, we seek the next year with same configuration $Y_s$. Let's consider all possibles values of $M_0 = Y_0 \mod 4$ :


$M_0 = 0$, then $Y_0$ is a leap year, as $GCD(5, 7) = 1$, 7 of the 5 steps (28 years) are necessary to reach the same configuration: $Y_s = Y_0 + 28$ and $M_s = 0$   
$M_0 = 1$, then $Y_s = Y_0 + 6$ and $M_s = 3$
$M_0 = 2$, then $Y_s = Y_0 + 11$ and $M_s = 1$
$M_0 = 3$, then $Y_s = Y_0 + 11$ and $M_s = 2$


Thus, if the considered year is a leap year, you have exactly the same configuration every 28 years, else, you have 3 times the same configuration every 28 years. In the latter case, the $B-A \mod 28$ reamaining years may be simply considered with above rule.

To consider the 100-exception, you may do the same work adding an extra variable modulo 100 (quite more laborious).