I am trying to solve a Problem code: LASTDIG2 in SPOJ SPOJ.com - Problem LASTDIG2
but every time i am getting 'WA': Wrong Answer
I have tried every posible Test Cases.I've written the code mentioned below.Please help
Code:
#include <stdio.h>
int main() {
int t;
scanf("%d",&t);
while(t--)
{
signed long long int b,c;
scanf("%llu %llu",&b,&c);
b=b%10;
if((c==0)&& (b!=0)) printf("%d\n",1);
else
{
if((c%4)==0)
printf("%d\n",((int)pow(b,4))%10);
else
printf("%d\n",((int)pow(b,c%4))%10);
}
}
return 0;
}
When i googled for the solution i found the below code and the below mentioned code is 'Acepted' by Spoj:
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[1005];
unsigned long long int b,a,ans,len,t;
scanf("%llu",&t);
while(t--){
scanf("%s",str);
scanf("%llu",&b);
len=strlen(str);
a=str[len-1]-'0';
if(b==0&&a!=0) ans=1;
else{
if(a==0){ ans=0; goto end;}
if(a==5) {ans=5; goto end;}
switch(b%4){
case 0: ans=a%2!=0? 1:6;
break;
case 1: ans=a;
break;
case 2: ans=a*a%10;
break;
default : ans=a*a*a%10;
break;
}
}
end :
printf("%llu\n",ans);
}
return 0;
}