# nyoj-28 factorials of large numbers (template problem)

Factorial multiplication

Title Description:
We all know how to calculate the factorial of a number, but if the number is large, how can we calculate it and output it?
Enter a description:
Enter an integer m (0 < m < = 5000)
Output Description:
Output the factorial of m and enter a line break after the output

Sample input:
50

Sample output:
30414093201713378043612608166064768844377641568960512000000000000

It's a template problem for finding the factorial of large numbers. Save a wave of templates

AC code

```#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 50010
string bigFactorial(int n)
{
int a[maxn],k=1;
a[0]=1;
for(int i=2; i<=n; i++)
{
int num=0;
for(int j=0; j<k; j++)
{
int temp=a[j]*i+num;
a[j]=temp%10;
num=temp/10;
}
while(num!=0)
{
a[k]=num%10;
num/=10;
k++;
}
}
string s="";
for(int i=k-1; i >= 0; i--)
s+=a[i]+'0';
return s;
}
int main()
{
int n;
scanf("%d",&n);
cout<<bigFactorial(n)<<endl;
cout<<endl;
return 0;
}

```

Other templates about large numbers are also collected

```string sum(string s1,string s2) //Addition of large numbers
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}

```

Subtraction of large numbers

```string bigIntegerSub(char *s1,char *s2) //Subtraction of large numbers
{
if(s1==s2)
return "0"; //Equal
int len1=strlen(s1),len2=strlen(s2);
if(len1>len2)
return subInfo(s1,s2);
else if(len1<len2)
return "-"+subInfo(s2,s1); //negative
else                          //Judge the size when the length is equal
{
for(int i=0; i<len1; i++)
{
if(s1[i]-'0'>s2[i]-'0')
return subInfo(s1,s2);
else if(s1[i]-'0'< s2[i]-'0')
return "-" +subInfo(s2,s1);
}
}
}
```

Large number times integer number

```string Multiply(string s,int x)  //Large number times integer number
{
reverse(s.begin(),s.end());
int cmp=0;
for(int i=0;i<s.size();i++)
{
cmp=(s[i]-'0')*x+cmp;
s[i]=(cmp%10+'0');
cmp/=10;
}
while(cmp)
{
s+=(cmp%10+'0');
cmp/=10;
}
reverse(s.begin(),s.end());
return s;
}

```

Large number divided by integer number

```string Except(string s,int x)  //Large number divided by integer number
{
int cmp=0,ok=0;
string ans="";
for(int i=0;i<s.size();i++)
{
cmp=(cmp*10+s[i]-'0');
if(cmp>=x)
{
ok=1;
ans+=(cmp/x+'0');
cmp%=x;
}
else{
if(ok==1) ans+='0';
}
}
return ans;
}

```

Multiplication of large numbers

```string sum(string s1,string s2)  //Addition of large numbers
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}

string Mult(string s,int x)  //Large number times integer number
{
reverse(s.begin(),s.end());
int cmp=0;
for(int i=0;i<s.size();i++)
{
cmp=(s[i]-'0')*x+cmp;
s[i]=(cmp%10+'0');
cmp/=10;
}
while(cmp)
{
s+=(cmp%10+'0');
cmp/=10;
}
reverse(s.begin(),s.end());
return s;
}
string Multfa(string x,string y)  //Multiplication of large numbers
{
string ans;
for(int i=y.size()-1,j=0;i>=0;i--,j++)
{
string tmp=Mult(x,y[i]-'0');
for(int k=0;k<j;k++)
tmp+='0';
ans=sum(ans,tmp);
}
return ans;
}

```

Posted on Sat, 30 Nov 2019 17:41:35 -0800 by thipse_rahul