-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1189 - Sum of Factorials.cpp
50 lines (46 loc) · 1.02 KB
/
1189 - Sum of Factorials.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// ALLAH IS ALMIGHTY //
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll st[25];
vector <ll> ans;
ll fact( ll n ){
if( n <=1 )return 1;
else return n*fact( n-1 );
}
int main()
{
for( ll i=0;i<=21;i++ ){
st[i] = fact( i );
}
cin.sync_with_stdio( false );
ll t, cs = 0;
cin >> t;
while( t-- ){
ll n;
ll p = 0;
cin >> n;
ll j = 20;
while( n ){
if( j < 0 ){
p = 1;
break;
}
if(n>=st[j]){
n -= st[j];
ans.push_back( j );
}
j--;
}
if( !p ){
printf("Case %lld: ",++cs);
for( ll i = ans.size()-1; i >=0; i-- ){
if( i )printf( "%lld!+",ans[i]);
else printf("%lld!\n",ans[i]);
}
}else{
printf( "Case %lld: impossible\n",++cs );
}
ans.clear();
}return 0;
}