Advertisement
Guest User

safdsafsafdsa

a guest
Nov 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream in ("inversmodular.in");
  7. ofstream out ("inversmodular.out");
  8.  
  9. int powrest (int a, int b, int c) {
  10. int rest, p = 1;
  11. while (b > 0) {
  12. if (b % 2 == 1) {
  13. rest = (rest * a) % c;
  14. }
  15. b /= 2;
  16. a = a * a % c;
  17. }
  18. return rest;
  19. }
  20.  
  21. int logpow (int a, int n) {
  22. int p = 1;
  23. while (n > 0) {
  24. if (n % 2 == 1) {
  25. p *= a;
  26. }
  27. a *= a;
  28. n /= 2;
  29. }
  30. return p;
  31. }
  32.  
  33. int phi (int n) {
  34. int p = 2, k, rez = 1;
  35. while (p * p <= n) {
  36. k = 0;
  37. while (n % p == 0) {
  38. n /= p;
  39. k ++;
  40. }
  41. if (k > 0) {
  42. rez *= logpow (p, k - 1) * (p - 1);
  43. }
  44. p ++;
  45. }
  46. if (n > 1) {
  47. rez *= (n - 1);
  48. }
  49. return rez;
  50. }
  51.  
  52. int main() {
  53. int a, n, rez;
  54. in >> a >> n;
  55. rez = powrest (a, phi (n) - 1, n);
  56. out << rez + 1;
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement