Problem Type : Number Theory
#include<stdio.h>
#define max 1001
#define len 3000
int fact[max][len];
void factorial()
{
int i,j;
fact[0][0]=1;
fact[1][0]=1;
for(i=2;i<max;i++)
{
for(j=0;j<len;j++)
{
fact[i][j]=fact[i][j]+fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1]=fact[i][j]/10;
fact[i][j]=fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
long int i,j,k,n,sum;
while(scanf("%ld",&n)==1)
{
sum=0;
for(j=len-1;j>=0;j--)
{
if(fact[n][j]!=0)
break;
}
for(i=j;i>=0;i--)
{
sum+=fact[n][i];
}
printf("%ld\n",sum);
}
return 0;
}
#include<stdio.h>
#define max 1001
#define len 3000
int fact[max][len];
void factorial()
{
int i,j;
fact[0][0]=1;
fact[1][0]=1;
for(i=2;i<max;i++)
{
for(j=0;j<len;j++)
{
fact[i][j]=fact[i][j]+fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1]=fact[i][j]/10;
fact[i][j]=fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
long int i,j,k,n,sum;
while(scanf("%ld",&n)==1)
{
sum=0;
for(j=len-1;j>=0;j--)
{
if(fact[n][j]!=0)
break;
}
for(i=j;i>=0;i--)
{
sum+=fact[n][i];
}
printf("%ld\n",sum);
}
return 0;
}
No comments:
Post a Comment