fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll bin_exp_recursive(ll taban, ll us, ll mod) {
  6. // Base case (durmamız gereken durum).
  7. if (!us)
  8. return 1;
  9.  
  10. taban %= mod;
  11.  
  12. ll alt_cevap = bin_exp_recursive(taban, us / 2, mod);
  13. ll carp = us % 2 ? taban : 1;
  14.  
  15. return alt_cevap * alt_cevap % mod * carp % mod;
  16. }
  17.  
  18. int main() {
  19. ll taban, us, mod;
  20. cin >> taban >> us >> mod;
  21. cout << taban << "^" << us << " mod " << mod << " = " << bin_exp_recursive(taban, us, mod) << "\n";
  22. }
Success #stdin #stdout 0.01s 5268KB
stdin
1000000000000000000 1000000000000000000 1000000007
stdout
1000000000000000000^1000000000000000000 mod 1000000007 = 504853526