LOJ 1189 - Sum of Factorials(打表)

                                                                              1189 - Sum of Factorials
Time Limit: 0.5 second(s)Memory Limit: 32 MB

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

Output for Sample Input

4

7

7

9

11

Case 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.

题意:输入一个数n ,使得n = x1! + x2! + ... + xn! (xi < xj for all i < j)
思路:先打阶乘表,然后进行判断即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL num[25];
int a[25];
void dabiao()
{
    num[0] = 1;
    for(int i = 1 ; i < 21 ; i++)
        num[i] = num[i-1] * i;
}
int main()
{
    int t,kcase = 1;
    LL n;
    dabiao();
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%lld",&n);
        printf("Case %d: ",kcase++);
        if(n > num[20])
            printf("impossible\n");
        else
        {
            bool falg = false;
            int k = 0;
            for(int i = 20 ; i >= 0 ; i--)
            {
                if(n >= num[i])
                {
                     n -= num[i];
                     a[k++] = i;
                    if(n == 0)
                    {
                        falg = true;
                        break;
                    }
                }
            }
            if(falg)
            {
                int f = k;
                printf("%d!",a[f-1]);
                for(int i = f-2 ; i >= 0 ; i--)
                    printf("+%d!",a[i]);
                printf("\n");
            }
            else
                printf("impossible\n");
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值