Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef long long int ll;
- ll modPow(ll a,ll p,ll m)
- {
- ll res = 1%m;
- while(p>0)
- {
- if(p&1)
- res = (res*a)%m;
- a = (a*a)%m;
- p >>= 1;
- }
- return res;
- }
- int main()
- {
- ll x,a,n,c;
- while(scanf("%lld %lld %lld %lld",&x,&a,&n,&c)==4)
- {
- if(x==0)
- return 0;
- ll mod = modPow(a,n,c);
- ll res = (x*mod)%c;
- ll num = ((1 - mod) % c + c) % c;
- num = (num * a) % c;
- ll modInv = modPow(((1 - a) % c + c) % c, c - 2, c);
- res = ((res - num * modInv) % c + c)%c;
- printf("%lld\n",res);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment