Skip to content

Commit 9e993da

Browse files
authored
Merge pull request #320 from ayush-mg/main
Create Solution2.cpp
2 parents b136355 + e5a5f81 commit 9e993da

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
3+
// Submission Link: -
4+
// https://codeforces.com/contest/343/submission/355549255
5+
// Explanation of Logic:
6+
7+
// Time Complexity - O(log(min(a,b)))
8+
// Space Complexity - O(1)
9+
10+
// 1. The If Condition (Perfect Division):
11+
// - checks if 'a' is perfectly divisible by 'b' (remainder is 0).
12+
// - If true, this represents a whole number resistance. It means the
13+
// target can be built purely with series resistors, with no parallel
14+
// branches needed.
15+
// - We simply output 'a / b' as the total count.
16+
17+
// 2. The Else Block (Euclidean Division Lemma):
18+
// - If there is a remainder, we enter the loop to handle the fractional part
19+
// (representing the parallel branches).
20+
// - 'count' starts with the integer part of the first division.
21+
// - Inside the while loop:
22+
// 'd / c' finds the integer part of the inverted remainder.
23+
// We add this to 'count'.
24+
// We swap variables to perform the mathematical inversion (1/x)
25+
// for the next iteration.
26+
27+
28+
29+
#include<bits/stdc++.h>
30+
using namespace std;
31+
int main(){
32+
long long a,b;
33+
cin>>a>>b;
34+
if(a%b == 0){
35+
cout<<a/b;
36+
}
37+
else {
38+
long long c = a%b;
39+
long long d = b;
40+
long long count = a/b;
41+
while(c != 0){
42+
count += d/c;
43+
long long temp = d%c;
44+
d = c;
45+
c = temp;
46+
}
47+
cout<<count;
48+
}
49+
return 0;
50+
}

0 commit comments

Comments
 (0)