Skip to content

Commit ced7953

Browse files
committed
fix: solve resistor time machine #286
1 parent 68afa62 commit ced7953

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
SHORT PROBLEM STATEMENT:
3+
We need to find the minimum number of 1-ohm resistors to get a resistance of a/b.
4+
Resistors can be connected in series or parallel.
5+
6+
APPROACH USING PREFIX SUMS:
7+
Not applicable here as it's a math/GCD based problem.
8+
The logic is basiclly like the Euclidean algorithm. If we have to reach a/b,
9+
we keep taking the floor part (x/y) as series resistors and then
10+
calculate the remainder and swap to simulate the parallel recipricol logic.
11+
12+
TIME & SPACE COMPLEXITY:
13+
Time: O(log(min(a, b)))
14+
Space: O(1)
15+
16+
SUBMISSION LINK: https://codeforces.com/contest/343/submission/355457508
17+
*/
18+
19+
20+
#include <iostream>
21+
#include <algorithm>
22+
23+
using namespace std;
24+
25+
typedef long long ll;
26+
27+
int main() {
28+
ios_base::sync_with_stdio(false);
29+
cin.tie(NULL);
30+
31+
ll x, y;
32+
if (!(cin >> x >> y)) return 0;
33+
34+
ll count = 0;
35+
36+
while (y > 0) {
37+
count += (x / y);
38+
x %= y;
39+
swap(x, y);
40+
}
41+
42+
cout << count << "\n";
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)