Guest User

Untitled

a guest
Feb 10th, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. void test_case(){
  2.     ll x, y;
  3.     cin >> x >> y;
  4.     ll iter = 0;
  5.        
  6.     // 1. Algo : If you are somewhere on (x, y) then you may have come from (x, y - x) or (x - y, y).
  7.     // 2. So in short have come from (x , y%x)  or (x%y, x).
  8.     // 3. One last thing to take keep in mind is that if you reach somewhere (c, 0) or (0, c).
  9.     // 4. Then the solution is only possible if c = 1. And since you have overcounted 1 operation. So ans = ans - 1.
  10.  
  11.  
  12.  
  13.     while(x && y){
  14.         ll here;
  15.         if(x > y){
  16.             here = x/y;
  17.             x %= y;
  18.         }else{
  19.             here = y/x;
  20.             y %= x;
  21.         }
  22.         iter += here;
  23.     }
  24.    
  25.     if(x == 1 || y == 1)
  26.         cout << iter - 1<< endl;
  27.     else
  28.         cout << -1 << endl;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment