Skip to content

Commit 7ded312

Browse files
authored
Merge pull request #370 from amansharma264/day-3-001
Implement solution for "Resistor Time Machine" problem
2 parents 1561c92 + 8f7789e commit 7ded312

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Problem Name: Resistor Time Machine
3+
4+
Short Problem Statement:
5+
Mike has an unlimited number of unit resistors (R0 = 1). Using series and parallel
6+
connections, he wants to construct an equivalent resistance equal to a/b.
7+
The task is to find the minimum number of unit resistors required to obtain
8+
the resistance a/b.
9+
10+
Approach (Using Prefix Sums / Euclidean Algorithm Insight):
11+
The process of building resistance values using series (+1) and parallel
12+
(R / (R + 1)) operations mirrors the steps of the Euclidean Algorithm.
13+
At each step:
14+
- We can add as many series resistors as possible (a / b)
15+
- Reduce the fraction using modulo
16+
- Swap roles of numerator and denominator
17+
18+
The total number of operations corresponds to the total number of unit
19+
resistors required.
20+
21+
This is equivalent to summing all quotients in the Euclidean Algorithm.
22+
23+
Time Complexity:
24+
O(log(min(a, b)))
25+
26+
Space Complexity:
27+
O(1)
28+
29+
Example:
30+
Input: 3 2
31+
Process:
32+
3/2 -> 1 + 1/2
33+
1/2 -> parallel construction
34+
Total resistors used = 3
35+
36+
Submission Link:
37+
https://codeforces.com/submissions/amansharma264
38+
*/
39+
40+
#include <bits/stdc++.h>
41+
using namespace std;
42+
43+
#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr);
44+
45+
int main() {
46+
fastio();
47+
48+
long long a, b;
49+
cin >> a >> b;
50+
51+
long long ans = 0;
52+
53+
while (b != 0) {
54+
ans += a / b;
55+
a %= b;
56+
swap(a, b);
57+
}
58+
59+
cout << ans << '\n';
60+
return 0;
61+
}

0 commit comments

Comments
 (0)