File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-03/sol/Priyanshi_giri Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments