GastonFontenla

binPow y binModPow

Jun 2nd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. ll binPow(ll base, ll exp)
  8. {
  9.     if(exp == 0) ///Un número elevado a la cero es 1
  10.         return 1;
  11.     if(exp == 1)
  12.         return base;
  13.  
  14.     ll pot = binPow(base, exp/2);
  15.     return pot*pot * binPow(base, exp%2);
  16. }
  17.  
  18. ll binModPow(ll base, ll exp, ll mod)
  19. {
  20.     if(exp == 0) ///Un número elevado a la cero es 1
  21.         return 1;
  22.     if(exp == 1)
  23.         return base%mod;
  24.  
  25.     ll pot = binPow(base, exp/2)%mod;
  26.     return (  ((pot*pot)%mod)  *  (binPow(base, exp%2)%mod)  )%mod;
  27. }
  28.  
  29. int main()
  30. {
  31.     for(int i=0; i<60; i++)
  32.     {
  33.         ///Muestra potencias de 2
  34.         cout << binPow(2, i) << endl;
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment