Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void test_case(){
- ll x, y;
- cin >> x >> y;
- ll iter = 0;
- // 1. Algo : If you are somewhere on (x, y) then you may have come from (x, y - x) or (x - y, y).
- // 2. So in short have come from (x , y%x) or (x%y, x).
- // 3. One last thing to take keep in mind is that if you reach somewhere (c, 0) or (0, c).
- // 4. Then the solution is only possible if c = 1. And since you have overcounted 1 operation. So ans = ans - 1.
- while(x && y){
- ll here;
- if(x > y){
- here = x/y;
- x %= y;
- }else{
- here = y/x;
- y %= x;
- }
- iter += here;
- }
- if(x == 1 || y == 1)
- cout << iter - 1<< endl;
- else
- cout << -1 << endl;
- }
RAW Paste Data