yoyoboyss

Untitled

Mar 1st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define LL long long
  3.  
  4. using namespace std;
  5.  
  6. LL N,M;
  7.  
  8. LL getphi(LL nr){
  9. LL cur = nr;
  10.  
  11. for(LL i = 2; i * i <= nr; ++i){
  12. if (nr % i == 0){
  13. while(nr % i == 0)
  14. nr /= i;
  15. cur = (cur / i) * (i - 1);
  16. }
  17. }
  18.  
  19. if (nr != 1)
  20. cur = cur / nr * (nr - 1);
  21.  
  22. return cur;
  23. }
  24.  
  25. int main(){
  26.  
  27. freopen("inversmodular.in","r",stdin);
  28. freopen("inversmodular.out","w",stdout);
  29.  
  30. scanf("%lld %lld\n",&N,&M);
  31.  
  32. LL put = getphi(M) - 1;
  33. LL nr = N;
  34. LL crt = 1;
  35.  
  36. for(LL p = 1; p <= put; p <<= 1){
  37. if (p & put)
  38. crt = (crt * nr) % M;
  39. nr = (nr * nr) % M;
  40. }
  41.  
  42. printf("%lld\n",crt);
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment