博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1189——Sum of Factorials(阶乘的和+贪心)
阅读量:2344 次
发布时间:2019-05-10

本文共 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 Input

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.

原来这种题直接从大到小贪心就行了。。。

#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/

你可能感兴趣的文章
修改spring boot 启动logo
查看>>
spring boot-启动及配置文件
查看>>
HttpsURLConnection 安全传输(HTTPS--Secure Hypertext Transfer Protocol-安全超文本传输协议)...
查看>>
ASP.NET跨页面传值的技巧
查看>>
ASP.NET页面之间传递值解析
查看>>
我要学ASP.NET MVC 3.0(八): MVC 3.0 传递和保存你的Model
查看>>
我要学ASP.NET MVC 3.0(九): MVC 3.0 验证你的Model
查看>>
我要学ASP.NET MVC 3.0(十): MVC 3.0 使用 Forms身份验证
查看>>
我要学ASP.NET MVC 3.0(十一): MVC 3.0 使用筛选器
查看>>
ASP.NET MVC3、Pager 分页
查看>>
在 ASP.NET MVC 中创建自定义 HtmlHelper 控件
查看>>
MSDN---扩展方法 (C# 方法中的this参数)
查看>>
我要学ASP.NET MVC 3.0(十四): MVC 3.0 实例系列之创建数据表格
查看>>
我要学ASP.NET MVC 3.0(十五): MVC 3.0 实例系列之表格的排序
查看>>
我要学ASP.NET MVC 3.0(十七): MVC 3.0 实例之表格中数据的筛选
查看>>
Displaying a Sorted, Paged, and Filtered Grid of Data in ASP.NET MVC
查看>>
C#中的操作符
查看>>
ADO.NET Ling to Sql 语法
查看>>
ASP.NET MVC 2博客系列之一:强类型HTML辅助方法
查看>>
详解Asp.net MVC DropDownLists
查看>>