Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int phi(int n) {
  7.     int res = 1;
  8.     for (int d = 2; d * d <= n; d++) {
  9.         if (n % d == 0) {
  10.             int p = 1;
  11.             while (n % d == 0) {
  12.                 n /= d;
  13.                 p *= d;
  14.             }
  15.  
  16.             res *= (p - p / d);
  17.         }
  18.     }
  19.  
  20.     res *= (n - 1);
  21.  
  22.     return res;
  23. }
  24.  
  25. int powmod(int a, int p, int n) {
  26.     int res = 1;
  27.     for (int x = a; p > 0; p /= 2) {
  28.         if (p % 2 == 1) res = (res * x) % n;
  29.         x = (x * x) % n;
  30.     }
  31.  
  32.     return res;
  33. }
  34.  
  35. int main() {
  36.     int k;
  37.     cin >> k;
  38.     for (int i = 0; i < k; i++) {
  39.         int e, n, c;
  40.         cin >> e >> n >> c;
  41.  
  42.         // if (c < 0) {
  43.         //  c = n - c;
  44.         // }
  45.  
  46.         int phin = phi(n);
  47.         int d = powmod(e, phi(phin) - 1, phin);
  48.         cout << powmod(c, d, n) << endl;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement