Big Factorials
Limits
1s, 512 MB
Given an integer N, print the trailing 4 digits of (N factorial).
Here are some examples of and their last 4 digits.
Here are some examples of and their last 4 digits.
N | Last 4 Digits | |
---|---|---|
3 | 6 | 6 |
7 | 5040 | 5040 |
11 | 39916800 | 6800 |
15 | 1307674368000 | 8000 |
Input
The input will contain an integer N (0 < N < 1000).
Output
Print the last 4 digits of .
Do not print any leading zeroes.
Do not print any leading zeroes.
#include<iostream>
using namespace std;
void fact(int n)
{
int a[2000],temp,c,i;
a[0]=1;
c=0;
for(;n>=2;n--)
{
temp=0;
for(i=0;i<=c;i++)
{
temp += (a[i]*n);
a[i] = temp%10;
temp = temp/10;
}
while(temp>0)
{
a[++c]=temp%10;
temp/=10;
}
}
if(c>=3)
{
for(i=3;i>=0;i--)
cout<<a[i];
}
else
for(i=c;i>=0;i--)
cout<<a[i];
}
int main()
{
int n;
cin>>n;
fact(n);
}