Skip to content

Commit 59b2162

Browse files
authored
Merge pull request #325 from hellopaintinghi-cmd/day03-q002
Day-03 q002
2 parents 9b9923d + 328f0e5 commit 59b2162

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
Problem:
6+
Given two integers x and y, repeatedly add ⌊x / y⌋ to a counter,
7+
replace x with x % y, swap x and y, and continue until y becomes 0.
8+
Print the final counter value.
9+
10+
Approach:
11+
The solution follows the Euclidean algorithm. At each step,
12+
the value (x / y) contributes to the answer. These values are
13+
added cumulatively, similar to a prefix-sum over iterations.
14+
15+
Complexity:
16+
Time: O(log(min(x, y)))
17+
Space: O(1)
18+
19+
Example:
20+
Input: 5 2
21+
Output: 4
22+
23+
24+
25+
https://codeforces.com/contest/343/submission/355484926
26+
*/
27+
28+
int main() {
29+
ios::sync_with_stdio(false);
30+
cin.tie(nullptr);
31+
32+
long long x, y;
33+
cin >> x >> y;
34+
35+
long long ans = 0;
36+
while (y > 0) {
37+
ans += x / y;
38+
x %= y;
39+
swap(x, y);
40+
}
41+
42+
cout << ans << '\n';
43+
return 0;
44+
}

0 commit comments

Comments
 (0)