LightOj 1189 Sum of Factorials Solution
Problem Link:https://lightoj.com/problem/sum-of-factorials
Solve in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int n,t,i,k=1,r;
cin>>t;
while(t--)
{
long long int fact[30]= {},v=1;
vector<int>result;
fact[0]=1;
for(i=1; i<=20; i++)
{
fact[i]=i*v;
v=fact[i];
}
cin>>n;
printf("Case %lld: ",k++);
for(i=20; i>=0; i--)
{
if(fact[i]<=n)
{
n-=fact[i];
result.push_back(i);
}
}
if(n!=0)
cout<<"impossible"<<endl;
else
{
for(i=result.size()-1; i>0; i--)
{
cout<<result[i]<<"!+";
}
cout<<result[0]<<"!"<<endl;
}
}
}
No comments