The last digit
Link to the question : LASTDIG
HINT :
No need to worry about the constraints. Take out a pen and paper and notice the pattern of last digit for the powers of number 1 to 9. And then derive a general formula to shorten your code as the source limit is very small.
SOURCE CODE :
#include <stdio.h>
int main(void) {
long long int a,p,q,b;
int t;
scanf("%d",&t);
while(t--) {
scanf("%lld %lld",&a,&b);
p=a%10;
q=b%4;
if(b==0)
printf("1\n");
else if(p==1||p==0||p==5||p==6)
printf("%d\n",p);
else if(q==1)
printf("%d\n",p);
else if(q==2)
printf("%d\n",((p*p)%10));
else if(q==3)
printf("%d\n",((p*p*p)%10));
else if(q==0)
printf("%d\n",((p*p*p*p)%10));
}
return 0;
}
This comment has been removed by the author.
ReplyDeleteThe solution is giving wa by the spoj judges
ReplyDeletegood job!!
ReplyDeletethanks
DeleteTHIS CODE IS GIVING WRONG ANSWER CAN U HELP PLEASE
ReplyDelete#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
long long int b,i,x,p,n;
while(t--)
{
p=-1;
cin>>b>>i;
int v=b%10;
if(i==0)
p=1;
else if(v==1 || v==5 || v==6 || v==0)
p=v;
else if(v==2 || v==3 || v==8 || v==7)
n=4;
else
n=2;
if(p==-1)
{
x=i%n;
if(x==0)
p=1;
else
p=pow(v,x);
}
cout<<(p%10)<<endl;
}
return 0;
}