Gistrec

Возведение в степень по модулю

Nov 23rd, 2018
444
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. size_t modexp(size_t x, size_t y, size_t N) {
  6.     if (y == 0) return 1;
  7.     size_t z = modexp(x, y / 2, N);
  8.     if (y % 2 == 0) {
  9.         return (z*z) % N;
  10.     } else {
  11.         return (x*z*z) % N;
  12.     }
  13. }
  14.  
  15. int main() {
  16.     size_t x, y, N;
  17.     cout << "x= "; cin >> x;
  18.     cout << "y= "; cin >> y;
  19.     cout << "N= "; cin >> N;
  20.     cout << modexp(x, y, N);
  21.     cin.get(); cin.get();
  22.     return 0;
  23. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment