jakaria_hossain

lightoj - 1189 - Sum of Factorials

Aug 22nd, 2019
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int main()
  5. {
  6. unsigned long long fact[22];
  7. fact[0]=1;
  8. for(int i=1;i<=20;i++)fact[i]=fact[i-1]*i;
  9.  
  10. ll test;
  11. cin>>test;
  12. for(ll c=1;c<=test;c++)
  13. {
  14. ll x,i;
  15. cin>>x;
  16. stack<ll>s;
  17. i=20;
  18. while(i>=0)
  19. {
  20. if(fact[i]<=x)
  21. {
  22. s.push(i);
  23. x-=fact[i];
  24. }
  25. i--;
  26. }
  27. if(x==0)
  28. {
  29. printf("Case %lld: ",c);
  30. while(s.size()!=1)
  31. {
  32. x=s.top();
  33. printf("%d!+",x);
  34. s.pop();
  35. }
  36. x=s.top();
  37. s.pop();
  38. printf("%lld!\n",x);
  39. }
  40. else
  41. {
  42. printf("Case %lld: impossible\n",c);
  43. }
  44. }
  45. }
RAW Paste Data