fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. long rep_sq(int b, int p)
  6. {
  7. if(p==1) return b;
  8. if(p%2==0)
  9. return rep_sq(b*b, p/2);
  10. else
  11. return b * rep_sq(b*b, (p-1)/2);
  12. }
  13.  
  14. long int big_mod(int b, int p, int m)
  15. {
  16. if(b==0) return 0;
  17. if(p==1) return b%m;
  18. if(p%2 == 0)
  19. return big_mod(b, p/2, m)%m * big_mod(b, p/2, m)%m;
  20. else
  21. return (b%m) * big_mod((b%m)*(b%m), (p-1)/2, m)%m;
  22.  
  23. }
  24.  
  25. int main()
  26. {
  27.  
  28. int b, p, m;
  29. while( cin >> b )
  30. {
  31. cin >> p;
  32. cin >> m;
  33.  
  34. cout << big_mod(b, p, m) << endl;
  35. }
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 2856KB
stdin
3
18132
17

17
1765
3

2374859
3029382
36123

3
15
4

0
2147483647
10

2147483647
2147483647
10

2147483647
2147483647
46340

0
0
1
stdout
13
2
13195
3
0
3
13903
0