先上题目

Sum of Factorials

                  Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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

Hint

Be careful about the output format; you may get wrong answer for wrong output format.

  这一题题意就是给你一个数,问哪些数的阶乘等于这个数,并按照样例从小到大输出这些数的每一项,其中每一项只可以用一次,如果不存在这些数,就输出impossible。

一开始看的时候第一个想到的是用dfs,然后就很轻松地写出的代码,可是发现样例的第四个case卡住了很久,然后就加了一个约束条件,不过时间复杂度还是比较大,跑10个case还是要10ms,而题目要求仅有500ms,于是想了很久怎样优化,看榜发现很多人都很快过了这题,心里有点急,可是心越急就越想不出来,后来干脆把这题丢到一边,想其他题,后来又过了一题以后就继续这题,因为题目要求的数据范围是1~10^18,用longlong不会爆,而且这时才想起阶乘数的一个特征,相邻两项的差距是越来越大的,某一个数的前面所有数的和加起来也不够这个数大,于是就以这个为策略使用贪心算法,结果代码不长,然后wa了一次,检查发现是打表数字打错了,= =。后来听人家说这一题我们是做过的,所以其他人才过的这么快,= =。看来以后做完题目要总结一下。

上代码:

 #include <stdio.h>
#include <string.h>
#include <map>
#define MAX 10000
using namespace std; long long p[]={,,,,,,,,,,,,,,,,,,,,};
bool mark[]; bool check(long long n,int q)
{
if(n==) return ;
int i;
for(i=q;i>=;i--)
{
if(n>=p[i]) break;
}
q=i;
if(mark[q]) return ;
mark[q]=;
if(check(n-p[q],q-)) return ;
mark[q]=;
return ;
} int main()
{
//freopen("data.txt","r",stdin);
int i,j,t,c;
long long n;
scanf("%d",&t);
for(i=;i<=t;i++)
{
scanf("%lld",&n);
memset(mark,,sizeof(mark));
printf("Case %d: ",i);
if(!check(n,)) printf("impossible\n");
else
{
c=;
for(j=;j<=;j++)
{
if(mark[j])
{
if(c++) printf("+");
printf("%d!",j);
}
}
printf("\n");
}
} return ;
}

1189

LightOJ - 1189 - Sum of Factorials的更多相关文章

  1. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  2. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  3. 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  4. LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...

  5. LightOJ 1278 - Sum of Consecutive Integers 分解奇因子 + 思维

    http://www.lightoj.com/volume_showproblem.php?problem=1278 题意:问一个数n能表示成几种连续整数相加的形式 如6=1+2+3,1种. 思路:先 ...

  6. POJ 1775 Sum of Factorials (ZOJ 2358)

    http://poj.org/problem?id=1775 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1334 题目大意: ...

  7. zoj 2358,poj 1775 Sum of Factorials(数学题)

    题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...

  8. POJ 1775 Sum of Factorials 数论,基础题

    输入一个小于1000000的正整数,是否能表达成式子:a1!+a2!+a3!+...+an (a1~an互不相等). 因为10!>1000000,所以先打1~10的阶乘表.从a[10]开始递减判 ...

  9. LightOJ Beginners Problems 部分题解

    相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...

随机推荐

  1. B2241 打地鼠 暴力模拟

    大水题!!!30分钟AC(算上思考时间),直接模拟就行,加一个判断约数的剪枝,再多加几个剪枝就可以过(数据巨水) 我也就会做暴力的题了. 题干: Description 打地鼠是这样的一个游戏:地面上 ...

  2. hdu 5823 color II —— 子集DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5823 看博客:http://www.cnblogs.com/SilverNebula/p/5929550. ...

  3. Genesis 多边形闭轮廓填充算法

    通过逐行扫描,计算得出直线与多边形相交点进行求解 原理图形如下所示: 相关函数: /// <summary> /// 求点P到线段L距离 /// </summary> /// ...

  4. 浅谈自学Python之路(day2)

    今天的主要内容是: 标准库 数据类型知识 数据运算 三元运算 bytes类型 字符串操作 字典 集合 标准库 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有 ...

  5. SQLServer2008 关于Group by

    如果我们想知道每个国家有多少种水果,那么我们可以通过如下SQL语句来完成: SELECT COUNT(*) FruitName AS 水果种类, ProductPlace AS 出产国 FROM T_ ...

  6. CDN 内容分发网络

    第一步,HTML的文件引用:HTML的文件头(也有文件中,文件尾)那边常有其他文件引用,比如CSS以及JS的引用. 就以bootstrap常用的引用来举个栗子你常见的引用可能会是这样的: <he ...

  7. bootstrap 网格布局

    一:基本的网格布局 <div class="container"> <div class="row"> <div class=&q ...

  8. ArcGIS API For Android Errors汇总

    API客户端异常错误的发生通常是由于不正确的方法参数,对象状态依赖,或网络条件. //*******推荐使用方法,按下Ctrl+F搜索错误代码,再查看与代码对应的解释.********// ArcGI ...

  9. Linux系统下通过命令行对mysql数据进行备份和还原

    一.备份 1.进入mysql目录 cd /var/lib/mysql (进入mysql目录,根据安装情况会有差别) 2.备份 mysqldump -u root -p密码 数据库名 数据表名 > ...

  10. 【PostgreSQL-9.6.3】如何实现非自动提交

    我们在使用psql工具操作数据库时,事务是自动提交的.也就是说,当我们执行完一条insert或者delete语句后,在不输入commit情况下,这条语句也是提交的.如果不想自动提交,可以使用以下两种方 ...