Skip to content

Commit 8f0e89f

Browse files
authored
Add solution for resistor fraction problem
Implemented a solution to calculate the minimum number of resistors needed to form a fraction a/b using a method similar to the Euclidean algorithm.
1 parent b91b85c commit 8f0e89f

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)