Skip to content

Commit d16d92d

Browse files
committed
d3q2 done 👍
1 parent 1269f67 commit d16d92d

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// link : https://codeforces.com/contest/343/submission/355845658
2+
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int main() {
8+
long long a,b;
9+
cin>>a>>b;
10+
long long ans=0;
11+
while(a>0 && b>0) {
12+
if(a>b) {
13+
ans+=a/b;
14+
a=a%b;
15+
}
16+
else {
17+
ans+=b/a;
18+
b=b%a;
19+
}
20+
}
21+
cout<<ans<<endl;
22+
}
23+
24+
25+
26+
/*
27+
Explanation:
28+
29+
We need to construct a resistance equal to a / b using unit resistors.
30+
Series and parallel connections correspond to increasing or reducing the
31+
fraction exactly like steps in the Euclidean Algorithm.
32+
33+
If a > b:
34+
- We can add 1 in series floor(a / b) times
35+
- a becomes a % b
36+
37+
If b > a:
38+
- We can add 1 in parallel floor(b / a) times
39+
- b becomes b % a
40+
41+
Each division step adds the quotient to the number of resistors used.
42+
Thus, the answer is the sum of all quotients during the Euclidean process.
43+
44+
The fraction is irreducible, so the algorithm always terminates.
45+
46+
Time Complexity:
47+
O(log(min(a, b)))
48+
49+
Space Complexity:
50+
O(1)
51+
*/

0 commit comments

Comments
 (0)