The problem is this.
The tutorial says this:
"If a fraction can be obtained with k resistors, then it is simple to calculate that we can obtain fractions and with k + 1 resistors. So adding one resistor means performing one operation backwards in Euclidean algorithm. That means that the answer is equal to the number of steps in standard Euclidean algorithm.
At first we thought about the major problem (any two elements can be joined), but had a moment of eureka and got that the given problem unexpectedly naturally can be reduced to GCD. "
I do not understand the tutorial. A little help will be really appreciated.
Thanks!
Resistances of all possible elements are:
So if you are given a target resistance , there is only one possible way to construct it:
In the second and the third cases you now need to build an element e', and it can again be done in only one possible way (depending on whether Re' is greater than, less than or equal to 1). By continuing these steps we get a sequence of resistances , such that the next pair (a, b) is obtained by the previous by subtracting the smaller element of the pair from the larger one. We can now build this sequence naively, but it will take too much time in case, for example, a = 1018, b = 1.
We can make a shortcut by subtracting the smaller value multiple times at once. Say, a > b, then a = kb + r, where . So if we subtract b from a k times, we obtain the pair (r, b). This is essentially the GCD algorithm (except we calculate the sum of all values k in addition to 'normal' calculations), so it works in