File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-03/sol/Sujal_Kshatri Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // submission link - https://codeforces.com/contest/343/submission/355607613
2+
3+ #include < bits/stdc++.h>
4+ using namespace std ;
5+
6+ /*
7+ 1. We want to make fraction a/b using smallest number of 1 resistors.
8+ 2. We can add resistor in line (series) or side (parallel).
9+ 3. Instead of making from 1, we go backward from a/b.
10+ 4. If a is bigger than b, then we minus b from a.
11+ 5. If b is bigger than a, then we minus a from b.
12+ 6. Every minus means we used one resistor.
13+ 7. We count how many times we can minus using divide.
14+ 8. This work same like Euclid algo (gcd thing).
15+ 9. When one number become zero, we stop.
16+ 10. Total count is our answer.
17+ */
18+
19+ int main ()
20+ {
21+ unsigned long long a, b;
22+ cin >> a >> b;
23+
24+ long long totalResistors = 0 ;
25+
26+ while (a && b)
27+ {
28+ totalResistors += a / b;
29+ a %= b;
30+ swap (a, b);
31+ }
32+
33+ cout << totalResistors;
34+ return 0 ;
35+ }
You can’t perform that action at this time.
0 commit comments