title: Possible Paths [HackerRank]
body: <p>Please see the following Question recently posted on HackerRank</p>
  
  <blockquote>
    <p>Adam is standing at point (a,b) in an infinite 2D grid. He wants to know if he can reach point (x,y) or not. The only operation he can do is to move to point (a+b,b), (a,a+b), (a-b,b), or (a,a-b) from some point (a,b). It is given that he can move to any point on this 2D grid,i.e., the points having positive or negative X(or Y) co-ordinates.Tell Adam whether he can reach (x,y) or not.</p>
  </blockquote>
  
  <p><a href="https://www.hackerrank.com/contests/infinitum-jun14/challenges/possible-path" rel="nofollow">https://www.hackerrank.com/contests/infinitum-jun14/challenges/possible-path</a></p>
  
  <p>I realized that both x and y must be a sum of some multiple of a and b...</p>
  
  <p>So x%(a+b) OR x%(a-b) should be divisible by either a or b
  and similarly for y...</p>
  
  <p>But the following does not work ...</p>
  
  <pre><code>    long long int xb,yb,xa,ya;
      xb = x % b;
      xa = x % a;
      yb = y % b;
      ya = y % a;
  
      // for x
      bool cxbaplusb = a+b==0 ? xb == 0: (xb%(a+b))==0;
      bool cxbaminb = a-b==0 ? xb == 0: (xb%(a-b))==0;
  
      // for y
      bool cybaplusb = a+b==0 ? yb == 0: (yb%(a+b))==0;
      bool cybaminb = a-b==0 ? yb == 0: (yb%(a-b))==0;
  
      // for x
      bool cxaaplusb = a+b==0 ? xa == 0: (xa%(a+b))==0;
      bool cxaaminb = a-b==0 ? xa == 0: (xa%(a-b))==0;
  
      // for y
      bool cyaaplusb = a+b==0 ? ya == 0: (ya%(a+b))==0;
      bool cyaaminb = a-b==0 ? ya == 0: (ya%(a-b))==0;
  
      if ( (cxbaplusb || cxbaminb || cxaaplusb || cxaaminb)  &amp;&amp; (cybaplusb || cybaminb || cyaaplusb || cyaaminb) )        
          std::cout &lt;&lt; "YES" &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; "NO" &lt;&lt; std::endl;      
  </code></pre>
  
  <p>But this is not working ...  Am I missing any conditions ? Any suggestions ??     </p>