fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. // Binary Exponentiation'ın kısaltması.
  6. ll bin_exp(ll taban, ll us) {
  7. ll cevap = 1;
  8.  
  9. // Üs 1'den büyükken...
  10. while (us) { // Veya us > 0 / us != 0 vs.
  11. // Üssün sonuncu biti 1 mi?
  12. if (us % 2) { // Veya us & 1
  13. cevap *= taban;
  14. }
  15. // 1 değilse `cevap`ı güncellemeyeceğiz
  16. // ama her türlü `taban`ı kendisiyle çarpacağız.
  17. taban *= taban;
  18.  
  19. us /= 2; // Üssü de bölmeyi unutmayalım.
  20. // Veya us >>= 1
  21. }
  22.  
  23. return cevap;
  24. }
  25.  
  26. int main() {
  27. ll taban, us;
  28. cin >> taban >> us;
  29. cout << taban << "^" << us << " = " << bin_exp(taban, us) << "\n";
  30. }
Success #stdin #stdout 0.01s 5276KB
stdin
12345678910111213 1000000000000000000
stdout
12345678910111213^1000000000000000000 = 2448179987917832193