title: Finding find a^s mod b
body: <p>We are given 3 numbers: a,s and b, each varying in the range between 1 and 1000000. We need to find pow(a,s)%b. Obviously, we can not use the simple pow function because we could not generate big number such as 1000000<sup>1000000</sup>. This is the solution of the problem: </p>
  
  <pre><code>sol=1
  for(int i=0;i&lt;s;i++) 
  {
              sol = sol * a;
              sol = sol % b;
  }
  
  print sol
  </code></pre>
  
  <p>I don't understand this algorithm. Can someone explain it to me? </p>
  
  <p>P.S. Where can I find more algorithms for solving non-trivialc math problems such as this one?
  Cheers!</p>