LightOJ 1189 - Sum of Factorials

Description
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
Sample Output
Case 1: 1!+3!
Case 2: 0!+3!
Case 3: 1!+2!+3!

Case 4: impossible

给出一个数让其表示阶乘和的方式

#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
long long  base[21]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000};
int main()
{
	int t,k=1,a[23],h,j;
	long long n;
	scanf("%d",&t);
	while(t--)
	{j=0;
	h=0;
	  scanf("%lld",&n);
	  for(int i=20;i>=0;i--)
                 {
                   if(n==base[i])
                   {
                     a[h++]=i;
                     j=1;
                     break;
	    }
	    else if(n>base[i])
	    {
	     a[h++]=i;
	     n=n-base[i];
	    }
	  }
	    if(j==0)
	    printf("Case %d: impossible\n",k++);
	    else
	    {printf("Case %d: %d!",k++,a[h-1]);
	      for(int i=h-2;i>=0;i--)
	      printf("+%d!",a[i]);
	      printf("\n");
	    }
	    
	  
	  	
	}
	
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值