Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. typedef long long int ll;
  3.  
  4. ll modPow(ll a,ll p,ll m)
  5. {
  6.     ll res = 1%m;
  7.     while(p>0)
  8.     {
  9.         if(p&1)
  10.             res = (res*a)%m;
  11.         a = (a*a)%m;
  12.         p >>= 1;
  13.     }
  14.     return res;
  15. }
  16. int main()
  17. {
  18.     ll x,a,n,c;
  19.     while(scanf("%lld %lld %lld %lld",&x,&a,&n,&c)==4)
  20.     {
  21.         if(x==0)
  22.             return 0;
  23.         ll mod = modPow(a,n,c);
  24.         ll res = (x*mod)%c;
  25.         ll num = ((1 - mod) % c + c) % c;
  26.         num = (num * a) % c;
  27.         ll modInv = modPow(((1 - a) % c + c) % c, c - 2, c);
  28.         res = ((res - num * modInv) % c + c)%c;
  29.         printf("%lld\n",res);
  30.     }
  31.     return 0;
  32. }
Add Comment
Please, Sign In to add comment