In this problem you just have to calculate the frequencies of numbers 0 to 9 and print them same as given in the output. So avoid wasting your time. Just pre-calculate factorials till 370 and do that has been said in the problem statement.
an accepted solution is given below:
#include<bits/stdc++.h>
using namespace std;
vector<int> v[380];
void pre()
{
v[0].push_back(1);
for(int i=1;i<=370;i++)
{
long long int carry=0,s;
for(int j=0;j<v[i-1].size();j++)
{
s=v[i-1][j]*i+carry;
if(s>=10)
{
v[i].push_back(s%10);
carry=s/10;
}
else
{
v[i].push_back(s);
carry=0;
}
}
if(carry>0)
{
while(carry!=0)
{
v[i].push_back(carry%10);
carry=carry/10;
}
}
}
}
int main()
{
pre();
int n;
while(cin>>n && n!=0)
{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,l=0,j=0;
cout<<n<<"! --"<<endl;
for(int i=v[n].size()-1;i>=0;i--)
{
if(v[n][i]==0)
{
a++;
}
else if(v[n][i]==1)
{
b++;
}
else if(v[n][i]==2)
{
c++;
}
else if(v[n][i]==3)
{
d++;
}
else if(v[n][i]==4)
{
e++;
}
else if(v[n][i]==5)
{
f++;
}
else if(v[n][i]==6)
{
g++;
}
else if(v[n][i]==7)
{
h++;
}
else if(v[n][i]==8)
{
l++;
}
else if(v[n][i]==9)
{
j++;
}
}
printf(" (0) %d (1) %d (2) %d (3) %d (4) %d\n (5) %d (6) %d (7) %d (8) %d (9) %d\n",a,b,c,d,e,f,g,h,l,j);
}
return 0;
}
an accepted solution is given below:
#include<bits/stdc++.h>
using namespace std;
vector<int> v[380];
void pre()
{
v[0].push_back(1);
for(int i=1;i<=370;i++)
{
long long int carry=0,s;
for(int j=0;j<v[i-1].size();j++)
{
s=v[i-1][j]*i+carry;
if(s>=10)
{
v[i].push_back(s%10);
carry=s/10;
}
else
{
v[i].push_back(s);
carry=0;
}
}
if(carry>0)
{
while(carry!=0)
{
v[i].push_back(carry%10);
carry=carry/10;
}
}
}
}
int main()
{
pre();
int n;
while(cin>>n && n!=0)
{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,l=0,j=0;
cout<<n<<"! --"<<endl;
for(int i=v[n].size()-1;i>=0;i--)
{
if(v[n][i]==0)
{
a++;
}
else if(v[n][i]==1)
{
b++;
}
else if(v[n][i]==2)
{
c++;
}
else if(v[n][i]==3)
{
d++;
}
else if(v[n][i]==4)
{
e++;
}
else if(v[n][i]==5)
{
f++;
}
else if(v[n][i]==6)
{
g++;
}
else if(v[n][i]==7)
{
h++;
}
else if(v[n][i]==8)
{
l++;
}
else if(v[n][i]==9)
{
j++;
}
}
printf(" (0) %d (1) %d (2) %d (3) %d (4) %d\n (5) %d (6) %d (7) %d (8) %d (9) %d\n",a,b,c,d,e,f,g,h,l,j);
}
return 0;
}