problem link:
Discuss: in this problem you have to careful about big number.for big number you can divide the number and can simply solve the problem recursively.
try your self before see the code
#define ll long long
#include<bits/stdc++.h>
using namespace std;
ll bigMod(ll b,ll p,ll m)
{
ll odd,even,half;
if(p==0)
return 1;
if(p%2==1)
{
odd=b%m;
even=bigMod(b,p-1,m);
return (odd*even)%m;
}
if(p%2==0)
{
half=bigMod(b,p/2,m);
return (half*half)%m;
}
}
int main()
{
long long b,p,m,r;
while(scanf("%lld%lld%lld",&b,&p,&m)!=EOF)
{
cout <<bigMod(b,p,m)<<endl;
}
return 0;
}
Discuss: in this problem you have to careful about big number.for big number you can divide the number and can simply solve the problem recursively.
try your self before see the code
#define ll long long
#include<bits/stdc++.h>
using namespace std;
ll bigMod(ll b,ll p,ll m)
{
ll odd,even,half;
if(p==0)
return 1;
if(p%2==1)
{
odd=b%m;
even=bigMod(b,p-1,m);
return (odd*even)%m;
}
if(p%2==0)
{
half=bigMod(b,p/2,m);
return (half*half)%m;
}
}
int main()
{
long long b,p,m,r;
while(scanf("%lld%lld%lld",&b,&p,&m)!=EOF)
{
cout <<bigMod(b,p,m)<<endl;
}
return 0;
}
No comments:
Post a Comment