Problem link-click here
Nothing to say. same as problem 623 except some silly matter. read the question carefully.
pre-calculate factorial for 10000 numbers. then have a blast.
an accepted code is given below:
#include<bits/stdc++.h>
#define m 10001
using namespace std;
vector<long long int> v[m];
void generatef()
{
v[0].push_back(1);
v[1].push_back(1);
for(int i=2;i<m;i++)
{
long long int r=0,s,t;
t=v[i-1].size();
for(int j=0;j<t;j++)
{
s=v[i-1][j]*i+r;
if(s>=10)
{
v[i].push_back(s%10);
r=s/10;
}
else
{
v[i].push_back(s);
r=0;
}
}
if(r>0)
{
while(r!=0)
{
v[i].push_back(r%10);
r=r/10;
}
}
}
}
int main()
{
generatef();
int n;
while(scanf("%d",&n)==1)
{
cout.width(5);
cout<<n<<" -> ";
for(int i=0;i<v[n].size();i++)
{
if(v[n][i]!=0)
{
cout<<v[n][i]<<endl;
break;
}
}
}
return 0;
}
Nothing to say. same as problem 623 except some silly matter. read the question carefully.
pre-calculate factorial for 10000 numbers. then have a blast.
an accepted code is given below:
#include<bits/stdc++.h>
#define m 10001
using namespace std;
vector<long long int> v[m];
void generatef()
{
v[0].push_back(1);
v[1].push_back(1);
for(int i=2;i<m;i++)
{
long long int r=0,s,t;
t=v[i-1].size();
for(int j=0;j<t;j++)
{
s=v[i-1][j]*i+r;
if(s>=10)
{
v[i].push_back(s%10);
r=s/10;
}
else
{
v[i].push_back(s);
r=0;
}
}
if(r>0)
{
while(r!=0)
{
v[i].push_back(r%10);
r=r/10;
}
}
}
}
int main()
{
generatef();
int n;
while(scanf("%d",&n)==1)
{
cout.width(5);
cout<<n<<" -> ";
for(int i=0;i<v[n].size();i++)
{
if(v[n][i]!=0)
{
cout<<v[n][i]<<endl;
break;
}
}
}
return 0;
}
No comments:
Post a Comment