先上题目

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. multimap的使用 in C++,同一个关键码存在多个值

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  2. 数据结构C++,栈的实现

    #include <iostream>#include <cstdlib> template<class T>void changeLength1D(T*& ...

  3. 10.3.3 WebView的几个常见功能

    (1)背景设置,例如下面的代码. webView.setBackgroundColor(0);//先设置背景色为transparent webView.setBackgroundResource(R. ...

  4. thinkphp方便分页的page方法

    page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 用法 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例 ...

  5. Could not create the view: An unexpected exception was thrown. Myeclipse空间报错

    我的路径D:\MyEclipse 10\.metadata\.plugins\org.eclipse.core.runtime\.settings 我也遇到过这个问题,就是工作空间的问题好像是删除你工 ...

  6. js基础---数组方法

    数组数据的排序及去重 sort无形参的排序方式 arr1=[2,12,3,15]; var a=arr1.sort();console.log(arr1);console.log(a);//排序会改变 ...

  7. MyEclipse 连接Oracle数据库(初学者必看)

    前言:刚接触Oracle数据库,便有一个需求,编写控制台程序,实现主人登录.数据库为Oracle.下面详细介绍一下MyEclipse 连接Oracle数据库.   package DbHelp; im ...

  8. JAVA;使用java.awt.Image的不稳定性

    在使用awt的image时候,不是能时时获取到图像的宽和高, GetWidth()函数偶尔得到的值为-1,暂时没有找到解决方法. 代码: public class picture extends JF ...

  9. BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char str[maxn],strtot[ ...

  10. python mysql连接池

    话不多说,直接撸代码 # coding=utf-8 from DBUtils.PooledDB import PooledDB import pymysql as mysql import trace ...