本文共 1425 字,大约阅读时间需要 4 分钟。
Given an integer n, you have to find whether it can be expressed as summation of factorials. For given n, you have to report a solution such that
n = x1! + x2! + … + xn! (xi < xj for all i < j)
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.Each case starts with a line containing an integer n (1 ≤ n ≤ 1018).
Output
For each case, print the case number and the solution in summation of factorial form. If there is no solution then print ‘impossible’. There can be multiple solutions, any valid one will do. See the samples for exact formatting.Sample Input
4 7 7 9 11 Output for Sample InputCase 1: 1!+3!
Case 2: 0!+3! Case 3: 1!+2!+3! Case 4: impossible Note Be careful about the output format; you may get wrong answer for wrong output format.原来这种题直接从大到小贪心就行了。。。
#include#include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;long long num[23];void Init(){ num[0]=num[1]=(long long)1; for(int i=2; i<21; ++i) num[i]=num[i-1]*i;}int main(){ Init(); int t,cnt=1; scanf("%d",&t); while(t--) { long long n; vector ans; scanf("%lld",&n); for(int i=20; i>=0; --i) { if(n>=num[i]) { n-=num[i]; ans.push_back(i); } } printf("Case %d: ",cnt++); if(n!=0) { printf("impossible\n"); continue; } sort(ans.begin(),ans.end()); for(int i=0; i
转载地址:http://fpcvb.baihongyu.com/