fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int pow_iter(int a, int n) {
  6.  
  7. int res = 1, tmp = a, i = 0;
  8. int l = log2(n)+1;
  9.  
  10. while (i < l) {
  11. int p = pow(2,i++);
  12. if (n&p) res *= tmp;
  13. tmp = tmp*tmp;
  14. }
  15. return res;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int x, y;
  21. cin >> x >> y;
  22. cout << pow_iter(x,y) << " " << pow(x,y);
  23. return 0;
  24. }
Success #stdin #stdout 0s 4728KB
stdin
3 9
stdout
19683 19683