Please tell me what is wrong with this code . I am getting WA. But I have verified the answers with GMP and they are correct to my knowledge.
include
int mult[200][2],l1,l2,ans[200];
int multiply(int b);
main(){
int b,n,z,i;
long long int fact=1;
scanf("%d",&n);fflush(stdin);
for(z=0;z scanf("%d",&b);fflush(stdin);
fact=1;
for(i=1;i<=20&&i<=b;i++)
fact*=i;
if(b<=20){ printf("%lld",fact);goto x;}
l1=19;
l2=2;
for(i=18;i>=0;i--){
ans[i]=fact%10;
fact/=10;
}
for(i=21;i<=b&&i<100;i++){
l1=multiply(i);
}
for(i=0;i printf("%d",ans[i]);}
if(b==100) printf("00");
x:{if(z+1 }
exit(0);
}
int multiply(int b){
int i,j,k,sum,x1,y1;
for(i=0;i for(j=0;j<2;j++)
mult[i][j]=0;
}
x1=b%10;
for(j=l1-1,k=l1+1;j>=0;j--,k--){
y1=ans[j];
mult[k-1][0]=(x1*y1+mult[k][0])/10;
mult[k][0]=(x1*y1+mult[k][0])%10;
}
x1=(b-b%10)/10;
for(j=l1-1,k=l1;j>=0;j--,k--){
y1=ans[j];
mult[k-1][1]=(x1*y1+mult[k][1])/10;
mult[k][1]=(x1*y1+mult[k][1])%10;
}
for(i=l1+1;i>=0;i--){
sum=ans[i];
for(j=0;j sum+=mult[i][j];
ans[i-1]=sum/10;
ans[i]=sum%10;
}
if(ans[0]==0){
for(i=0;i ans[i]=ans[i+1];
ans[l1+1]=0;
return l1+1;
}
return l1+2;
}
Please do not mind if my method is too round about. I have jus started programming. Please tell me if there is a better method for multiplication.
Bye!