Skip to content

Commit 705f762

Browse files
committed
Day 3 Q2 Solution
1 parent 1269f67 commit 705f762

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
We needed to find out the max no. of resistors needed, to get to a target resistance, with only 1 unit resistors.
3+
4+
I wanted to convert the resistance to 1/1, so I took the larger no. and subtracted it by the maximum multiple of lower number, which is still lower than the larger one, adding 1 each time.
5+
6+
Time: O(log(max(a, b)))
7+
Space: O(1)
8+
9+
Submission Link: https://codeforces.com/contest/343/submission/355851930
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
15+
using ll = long long;
16+
17+
int main() {
18+
ll a, b;
19+
cin >> a >> b;
20+
21+
ll ans = 1;
22+
while (a != 1 || b != 1) {
23+
if (a > b) swap(a, b);
24+
// b is always greater
25+
26+
ll count = (b-1) / a;
27+
ans += count;
28+
b -= count * a;
29+
}
30+
31+
cout << ans;
32+
}

0 commit comments

Comments
 (0)